2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include "findsaddr.h"
40 #include "traceroute.h"
43 * Return the source address for the given destination address.
45 * This makes use of proper source address selection in the FreeBSD kernel
46 * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
47 * We open a UDP socket, and connect to the destination, letting the kernel
48 * do the bind and then read the source IPv4 address using getsockname(2).
49 * This has multiple advantages: no need to do PF_ROUTE operations possibly
50 * needing special privileges, jails properly taken into account and most
51 * important - getting the result the kernel would give us rather than
52 * best-guessing ourselves.
55 findsaddr(register const struct sockaddr_in *to,
56 register struct sockaddr_in *from)
59 struct sockaddr_in cto, cfrom;
63 s = socket(AF_INET, SOCK_DGRAM, 0);
65 return ("failed to open DGRAM socket for src addr selection.");
68 len = sizeof(struct sockaddr_in);
69 memcpy(&cto, to, len);
70 cto.sin_port = htons(65535); /* Dummy port for connect(2). */
71 if (connect(s, (struct sockaddr *)&cto, len) == -1) {
72 errstr = "failed to connect to peer for src addr selection.";
76 if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
77 errstr = "failed to get socket name for src addr selection.";
81 if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
82 errstr = "unexpected address family in src addr selection.";
86 /* Update source address for traceroute. */
87 setsin(from, cfrom.sin_addr.s_addr);
92 /* No error (string) to return. */