1201806Sbz/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4201806Sbz * Copyright (c) 2010 Bjoern A. Zeeb <bz@FreeBSD.org>
5201806Sbz * All rights reserved.
6201806Sbz *
7201806Sbz * Redistribution and use in source and binary forms, with or without
8201806Sbz * modification, are permitted provided that the following conditions
9201806Sbz * are met:
10201806Sbz * 1. Redistributions of source code must retain the above copyright
11201806Sbz * notice, this list of conditions and the following disclaimer.
12201806Sbz * 2. Redistributions in binary form must reproduce the above copyright
13201806Sbz * notice, this list of conditions and the following disclaimer in the
14201806Sbz * documentation and/or other materials provided with the distribution.
15201806Sbz *
16201806Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17201806Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18201806Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19201806Sbz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20201806Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21201806Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22201806Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23201806Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24201806Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25201806Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26201806Sbz * SUCH DAMAGE.
27201806Sbz *
28201806Sbz * $FreeBSD: stable/11/usr.sbin/traceroute/findsaddr-udp.c 330449 2018-03-05 07:26:05Z eadler $
29201806Sbz */
30201806Sbz
31201806Sbz#include <string.h>
32201806Sbz#include <unistd.h>
33201806Sbz
34201806Sbz#include <sys/types.h>
35201806Sbz#include <sys/socket.h>
36201806Sbz
37201806Sbz#include <netinet/in.h>
38201806Sbz
39201806Sbz#include "findsaddr.h"
40201806Sbz#include "traceroute.h"
41201806Sbz
42201806Sbz/*
43201806Sbz * Return the source address for the given destination address.
44201806Sbz *
45201897Sbz * This makes use of proper source address selection in the FreeBSD kernel
46201806Sbz * even taking jails into account (sys/netinet/in_pcb.c:in_pcbladdr()).
47201806Sbz * We open a UDP socket, and connect to the destination, letting the kernel
48201806Sbz * do the bind and then read the source IPv4 address using getsockname(2).
49201806Sbz * This has multiple advantages: no need to do PF_ROUTE operations possibly
50201806Sbz * needing special privileges, jails properly taken into account and most
51201806Sbz * important - getting the result the kernel would give us rather than
52201806Sbz * best-guessing ourselves.
53201806Sbz */
54201806Sbzconst char *
55201806Sbzfindsaddr(register const struct sockaddr_in *to,
56201806Sbz    register struct sockaddr_in *from)
57201806Sbz{
58201806Sbz	const char *errstr;
59201806Sbz	struct sockaddr_in cto, cfrom;
60201806Sbz	int s;
61201806Sbz	socklen_t len;
62201806Sbz
63201806Sbz	s = socket(AF_INET, SOCK_DGRAM, 0);
64201806Sbz	if (s == -1)
65201806Sbz		return ("failed to open DGRAM socket for src addr selection.");
66201806Sbz
67201806Sbz	errstr = NULL;
68201806Sbz	len = sizeof(struct sockaddr_in);
69201806Sbz	memcpy(&cto, to, len);
70201806Sbz	cto.sin_port = htons(65535);	/* Dummy port for connect(2). */
71201806Sbz	if (connect(s, (struct sockaddr *)&cto, len) == -1) {
72201806Sbz		errstr = "failed to connect to peer for src addr selection.";
73201806Sbz		goto err;
74201806Sbz	}
75201806Sbz
76201806Sbz	if (getsockname(s, (struct sockaddr *)&cfrom, &len) == -1) {
77201806Sbz		errstr = "failed to get socket name for src addr selection.";
78201806Sbz		goto err;
79201806Sbz	}
80201806Sbz
81201806Sbz	if (len != sizeof(struct sockaddr_in) || cfrom.sin_family != AF_INET) {
82201806Sbz		errstr = "unexpected address family in src addr selection.";
83201806Sbz		goto err;
84201806Sbz	}
85201806Sbz
86201806Sbz	/* Update source address for traceroute. */
87201806Sbz	setsin(from, cfrom.sin_addr.s_addr);
88201806Sbz
89201806Sbzerr:
90201806Sbz	(void) close(s);
91201806Sbz
92201806Sbz	/* No error (string) to return. */
93201806Sbz	return (errstr);
94201806Sbz}
95201806Sbz
96201806Sbz/* end */
97