1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
29 */
30
31#include <string.h>
32#include <unistd.h>
33
34#include <sys/types.h>
35#include <sys/socket.h>
36
37#include <netinet/in.h>
38
39#include "findsaddr.h"
40#include "traceroute.h"
41
42/*
43 * Return the source address for the given destination address.
44 *
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.
53 */
54const char *
55findsaddr(register const struct sockaddr_in *to,
56    register struct sockaddr_in *from)
57{
58	const char *errstr;
59	struct sockaddr_in cto, cfrom;
60	int s;
61	socklen_t len;
62
63	s = socket(AF_INET, SOCK_DGRAM, 0);
64	if (s == -1)
65		return ("failed to open DGRAM socket for src addr selection.");
66
67	errstr = NULL;
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.";
73		goto err;
74	}
75
76	if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
77		errstr = "failed to get socket name for src addr selection.";
78		goto err;
79	}
80
81	if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
82		errstr = "unexpected address family in src addr selection.";
83		goto err;
84	}
85
86	/* Update source address for traceroute. */
87	setsin(from, cfrom.sin_addr.s_addr);
88
89err:
90	(void) close(s);
91
92	/* No error (string) to return. */
93	return (errstr);
94}
95
96/* end */
97