xref: /freebsd/usr.sbin/traceroute/findsaddr-udp.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 
35 #include <netinet/in.h>
36 
37 #include "findsaddr.h"
38 #include "traceroute.h"
39 
40 /*
41  * Return the source address for the given destination address.
42  *
43  * This makes use of proper source address selection in the FreeBSD kernel
44  * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
45  * We open a UDP socket, and connect to the destination, letting the kernel
46  * do the bind and then read the source IPv4 address using getsockname(2).
47  * This has multiple advantages: no need to do PF_ROUTE operations possibly
48  * needing special privileges, jails properly taken into account and most
49  * important - getting the result the kernel would give us rather than
50  * best-guessing ourselves.
51  */
52 const char *
53 findsaddr(register const struct sockaddr_in *to,
54     register struct sockaddr_in *from)
55 {
56 	const char *errstr;
57 	struct sockaddr_in cto, cfrom;
58 	int s;
59 	socklen_t len;
60 
61 	s = socket(AF_INET, SOCK_DGRAM, 0);
62 	if (s == -1)
63 		return ("failed to open DGRAM socket for src addr selection.");
64 
65 	errstr = NULL;
66 	len = sizeof(struct sockaddr_in);
67 	memcpy(&cto, to, len);
68 	cto.sin_port = htons(65535);	/* Dummy port for connect(2). */
69 	if (connect(s, (struct sockaddr *)&cto, len) == -1) {
70 		errstr = "failed to connect to peer for src addr selection.";
71 		goto err;
72 	}
73 
74 	if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
75 		errstr = "failed to get socket name for src addr selection.";
76 		goto err;
77 	}
78 
79 	if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
80 		errstr = "unexpected address family in src addr selection.";
81 		goto err;
82 	}
83 
84 	/* Update source address for traceroute. */
85 	setsin(from, cfrom.sin_addr.s_addr);
86 
87 err:
88 	(void) close(s);
89 
90 	/* No error (string) to return. */
91 	return (errstr);
92 }
93 
94 /* end */
95