File: kernel_iface.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (260 lines) | stat: -rw-r--r-- 8,330 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* iface, for libreswan
 *
 * Copyright (C) 1997 Angelos D. Keromytis.
 * Copyright (C) 1998-2002, 2013,2016 D. Hugh Redelmeier <hugh@mimosa.com>
 * Copyright (C) 2003-2008 Michael C Richardson <mcr@xelerance.com>
 * Copyright (C) 2003-2010 Paul Wouters <paul@xelerance.com>
 * Copyright (C) 2008-2009 David McCullough <david_mccullough@securecomputing.com>
 * Copyright (C) 2009 Avesh Agarwal <avagarwa@redhat.com>
 * Copyright (C) 2010 Tuomo Soini <tis@foobar.fi>
 * Copyright (C) 2012-2017 Paul Wouters <pwouters@redhat.com>
 * Copyright (C) 2013 Wolfgang Nothdurft <wolfgang@linogate.de>
 * Copyright (C) 2016-2019 Andrew Cagney <cagney@gnu.org>
 * Copyright (C) 2017 D. Hugh Redelmeier <hugh@mimosa.com>
 * Copyright (C) 2017 Mayank Totale <mtotale@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>
#include <unistd.h>

#include "lsw_socket.h"

#include "ip_info.h"

#include "defs.h"
#include "log.h"
#include "kernel_iface.h"

/*
 * Process the updated list of interfaces.
 *
 * On linux, see netdevice(7) (and note that it clearly documents that
 * the below code only works with IPv4).
 *
 * On BSD, see netintro(4).  On BSD, <<struct ifreq>> includes
 * sockaddr_storage in its union of addresses and that is big enough
 * for any address so this should also work for IPv6.
 */

struct kernel_iface *find_kernel_ifaces(const struct ip_info *afi, struct logger *logger)
{
	/* Get a UDP socket */
	dbg("finding raw interfaces of type %s", afi->ip_name);


	int udp_sock = cloexec_socket(afi->socket.domain, SOCK_DGRAM, IPPROTO_UDP);
	if (udp_sock == -1) {
		fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
			    "find %s interfaces failed calling cloexec_socket(%s, SOCK_DGRAM, IPPROTO_UDP)",
			    afi->ip_name, afi->socket.domain_name);
	}

	/*
	 * Without SO_REUSEADDR, bind() of udp_sock will cause
	 * 'address already in use?
	 */
	static const int on = true;     /* by-reference parameter; constant, we hope */
	if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR,
		       (const void *)&on, sizeof(on)) < 0) {
		fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
			    "find %s interfaces failed calling setsockopt(SOL_SOCKET, SO_REUSEADDR)",
			    afi->ip_name);
	}

	/*
	 * Build an "any" endpoint and then bind the socket.
	 */
	ip_endpoint any_ep = endpoint_from_address_protocol_port(afi->address.unspec,
								 &ip_protocol_udp,
								 ip_hport(IKE_UDP_PORT));
	ip_sockaddr any_sa = sockaddr_from_endpoint(any_ep);
	if (bind(udp_sock, &any_sa.sa.sa, any_sa.len) < 0) {
		endpoint_buf eb;
		fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
			    "find %s interfaces failed calling bind(%s)",
			    afi->ip_name, str_endpoint(&any_ep, &eb));
	}

	/*
	 * Load buf with array of raw interfaces from kernel.
	 *
	 * We have to guess at the upper bound (num).  If we guess
	 * low, double num and repeat.  But don't go crazy: stop
	 * before 1024**2.
	 *
	 * Tricky: num is a static so that we won't have to start from
	 * 64 in subsequent calls to find_kernel_ifaces4.
	 *
	 * See netdevice(7) (linux) or netintro(4) (BSD).
	 */
	static volatile int num = 64; /* not very thread safe */
	struct ifconf ifconf = { .ifc_len = 0, };
	void *buf = NULL;	/* for list of interfaces -- arbitrary limit */
	for (; num < (1024 * 1024); num *= 2) {
		int len = num * sizeof(struct ifreq);
		dbg("  allocated %d buffer for SIOCGIFCONF", len);
		realloc_bytes(&buf, ifconf.ifc_len, len, "ifreq");

		ifconf = (struct ifconf) {
			.ifc_len = len,
			.ifc_buf = (void*)buf,
		};

		if (ioctl(udp_sock, SIOCGIFCONF, &ifconf) == -1) {
			fatal_errno(PLUTO_EXIT_FAIL, logger, errno,
				    "find %s interfaces failed calling ioctl(SIOCGIFCONF)",
				    afi->ip_name);
		}

		/* if we got back less than we asked for, we have them all */
		if (ifconf.ifc_len < len) {
			break;
		}
	}

	/*
	 * Because, on FreeBSD and OpenBSD the <<struct ifreq>> isn't
	 * big enough to hold an IPv[46] address, the entries can be
	 * larger than sizeof(struct ifreq).
	 *
	 * Code below gets to figure out the true size.
	 */
	unsigned nr_req = (ifconf.ifc_len / sizeof(struct ifreq));
	dbg("  ioctl(SIOCGIFCONF) returned %u bytes (roughly %u %s interfaces)",
	    ifconf.ifc_len, nr_req, afi->ip_name);

	/*
	 * For each interesting interface, add an entry to rifaces.
	 */
	struct kernel_iface *rifaces = NULL;
	const void *ifrp = buf;
	while (ifrp < buf + ifconf.ifc_len) {

		/*
		 * Current offset into the buffer.
		 */
		const struct ifreq *ifr = ifrp;

		/*
		 * Determine the true size of the structure at IFR and
		 * use that to advance the buffer pointer.
		 *
		 * While NetBSD an Linux can use sizeof(struct ifreq)
		 * directly, FreeBSD, OpenBSD and Darwin need to look
		 * at .sa_len (equation, below, also also works on
		 * NetBSD).
		 *
		 * Darwin defines the macro _SIZEOF_ADDR_IFREQ() in
		 * <net/if.h> with an equivalent equation.
		 */
#ifdef USE_SOCKADDR_LEN
		ifrp += (ifr->ifr_ifru.ifru_addr.sa_len <= sizeof(ifr->ifr_ifru) ? sizeof(struct ifreq) :
			 (sizeof(struct ifreq) - sizeof(ifr->ifr_ifru) + ifr->ifr_ifru.ifru_addr.sa_len));
#else
		ifrp += sizeof(struct ifreq);
#endif

		/*
		 * Because .ifr_name may not be NUL terminated, build
		 * a NUL-terminated copy of its value.
		 */
		char ifname[IFNAMSIZ + 1];
		memcpy(ifname, ifr->ifr_name, IFNAMSIZ);
		ifname[IFNAMSIZ] = '\0';

		/* ignore all but AF_INET interfaces */
		if (ifr->ifr_addr.sa_family != afi->af) {
			dbg("  ignoring non-%s interface %s with family %d",
			    afi->ip_name, ifname, ifr->ifr_addr.sa_family);
			continue; /* not interesting */
		}

		ip_address addr;
		err_t e = sockaddr_to_address_port(&ifr->ifr_addr,
						   ifrp - (const void*)&ifr->ifr_addr,
						   &addr, NULL/*port*/);
		if (e != NULL) {
			dbg("  ignoring %s interface %s: %s",
			    afi->ip_name, ifname, e);
			continue; /* ignore slave interfaces; they share IPs with their master */
		}

		/* ignore all but AF_INET interfaces (redundant) */
		if (!pexpect(address_info(addr) == afi)) {
			dbg("  ignoring non-%s interface %s",
			    afi->ip_name, ifname);
			continue; /* not interesting */
		}

		/* ignore unconfigured interfaces */
		if (!address_is_specified(addr)) {
			address_buf ab;
			dbg("  ignoring %s interface %s with unspecified address %s",
			    afi->ip_name, ifname, str_address(&addr, &ab));
			continue;
		}

#if 0
		if (address_is_loopback(addr)) {
			address_buf ab;
			dbg("  ignoring %s interface %s with loopback address %s",
			    afi->ip_name, ifname, str_address(&addr, &ab));
			continue;
		}
#endif

		/*
		 * Find out stuff about this interface.  See
		 * netdevice(7) or netintro(4).
		 */
		struct ifreq auxinfo = {0};
		passert(sizeof(auxinfo.ifr_name) == sizeof(ifr->ifr_name)); /* duh! */
		memcpy(auxinfo.ifr_name, ifr->ifr_name, IFNAMSIZ);
		if (ioctl(udp_sock, SIOCGIFFLAGS, &auxinfo) == -1) {
			llog_error(logger, errno,
				   "ignored %s interface %s - ioctl(SIOCGIFFLAGS) failed",
				   afi->ip_name, ifname);
			continue; /* happens when using device with label? */
		}

		if (!(auxinfo.ifr_flags & IFF_UP)) {
			dbg("  ignoring non-up %s interface %s",
			    afi->ip_name, ifname);
			continue; /* ignore an interface that isn't UP */
		}
#ifdef IFF_SLAVE
		/* only linux ... */
		if (auxinfo.ifr_flags & IFF_SLAVE) {
			dbg("  ignoring slave %s interface %s",
			    afi->ip_name, ifname);
			continue; /* ignore slave interfaces; they share IPs with their master */
		}
#endif

		struct kernel_iface *ri =
			overalloc_thing(struct kernel_iface, strlen(ifname) + 1);
		ri->addr = addr;
		strcpy(ri->name, ifname);
		ri->next = rifaces;
		rifaces = ri;
		address_buf b;
		dbg("  found %s interface %s with address %s",
		    afi->ip_name, ri->name, str_address(&ri->addr, &b));
	}

	pfree(buf);
	close(udp_sock);
	return rifaces;
}