xref: /netbsd/external/bsd/ntp/dist/lib/isc/unix/net.c (revision 6550d01e)
1 /*	$NetBSD: net.c,v 1.2 2011/01/09 14:49:39 kardel Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: net.c,v 1.40 2008/07/04 05:52:31 each Exp */
21 
22 #include <config.h>
23 
24 #include <sys/types.h>
25 
26 #if defined(HAVE_SYS_SYSCTL_H)
27 #if defined(HAVE_SYS_PARAM_H)
28 #include <sys/param.h>
29 #endif
30 #include <sys/sysctl.h>
31 #endif
32 
33 #include <errno.h>
34 #include <unistd.h>
35 
36 #include <isc/log.h>
37 #include <isc/msgs.h>
38 #include <isc/net.h>
39 #include <isc/once.h>
40 #include <isc/strerror.h>
41 #include <isc/string.h>
42 #include <isc/util.h>
43 
44 /*%
45  * Definitions about UDP port range specification.  This is a total mess of
46  * portability variants: some use sysctl (but the sysctl names vary), some use
47  * system-specific interfaces, some have the same interface for IPv4 and IPv6,
48  * some separate them, etc...
49  */
50 
51 /*%
52  * The last resort defaults: use all non well known port space
53  */
54 #ifndef ISC_NET_PORTRANGELOW
55 #define ISC_NET_PORTRANGELOW 1024
56 #endif	/* ISC_NET_PORTRANGELOW */
57 #ifndef ISC_NET_PORTRANGEHIGH
58 #define ISC_NET_PORTRANGEHIGH 65535
59 #endif	/* ISC_NET_PORTRANGEHIGH */
60 
61 #ifdef HAVE_SYSCTLBYNAME
62 
63 /*%
64  * sysctl variants
65  */
66 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
67 #define USE_SYSCTL_PORTRANGE
68 #define SYSCTL_V4PORTRANGE_LOW	"net.inet.ip.portrange.hifirst"
69 #define SYSCTL_V4PORTRANGE_HIGH	"net.inet.ip.portrange.hilast"
70 #define SYSCTL_V6PORTRANGE_LOW	"net.inet.ip.portrange.hifirst"
71 #define SYSCTL_V6PORTRANGE_HIGH	"net.inet.ip.portrange.hilast"
72 #endif
73 
74 #ifdef __NetBSD__
75 #define USE_SYSCTL_PORTRANGE
76 #define SYSCTL_V4PORTRANGE_LOW	"net.inet.ip.anonportmin"
77 #define SYSCTL_V4PORTRANGE_HIGH	"net.inet.ip.anonportmax"
78 #define SYSCTL_V6PORTRANGE_LOW	"net.inet6.ip6.anonportmin"
79 #define SYSCTL_V6PORTRANGE_HIGH	"net.inet6.ip6.anonportmax"
80 #endif
81 
82 #else /* !HAVE_SYSCTLBYNAME */
83 
84 #ifdef __OpenBSD__
85 #define USE_SYSCTL_PORTRANGE
86 #define SYSCTL_V4PORTRANGE_LOW	{ CTL_NET, PF_INET, IPPROTO_IP, \
87 				  IPCTL_IPPORT_HIFIRSTAUTO }
88 #define SYSCTL_V4PORTRANGE_HIGH	{ CTL_NET, PF_INET, IPPROTO_IP, \
89 				  IPCTL_IPPORT_HILASTAUTO }
90 /* Same for IPv6 */
91 #define SYSCTL_V6PORTRANGE_LOW	SYSCTL_V4PORTRANGE_LOW
92 #define SYSCTL_V6PORTRANGE_HIGH	SYSCTL_V4PORTRANGE_HIGH
93 #endif
94 
95 #endif /* HAVE_SYSCTLBYNAME */
96 
97 #if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
98 const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
99 #endif
100 
101 #if defined(ISC_PLATFORM_HAVEIPV6)
102 
103 # if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
104 const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
105 # endif
106 
107 # if defined(WANT_IPV6)
108 static isc_once_t 	once_ipv6only = ISC_ONCE_INIT;
109 
110 # if defined(ISC_PLATFORM_HAVEIN6PKTINFO)
111 static isc_once_t 	once_ipv6pktinfo = ISC_ONCE_INIT;
112 # endif
113 # endif
114 #endif /* ISC_PLATFORM_HAVEIPV6 */
115 
116 static isc_once_t 	once = ISC_ONCE_INIT;
117 
118 static isc_result_t	ipv4_result = ISC_R_NOTFOUND;
119 static isc_result_t	ipv6_result = ISC_R_NOTFOUND;
120 static isc_result_t	unix_result = ISC_R_NOTFOUND;
121 static isc_result_t	ipv6only_result = ISC_R_NOTFOUND;
122 static isc_result_t	ipv6pktinfo_result = ISC_R_NOTFOUND;
123 
124 static isc_result_t
125 try_proto(int domain) {
126 	int s;
127 	isc_result_t result = ISC_R_SUCCESS;
128 	char strbuf[ISC_STRERRORSIZE];
129 
130 	s = socket(domain, SOCK_STREAM, 0);
131 	if (s == -1) {
132 		switch (errno) {
133 #ifdef EAFNOSUPPORT
134 		case EAFNOSUPPORT:
135 #endif
136 #ifdef EPROTONOSUPPORT
137 		case EPROTONOSUPPORT:
138 #endif
139 #ifdef EINVAL
140 		case EINVAL:
141 #endif
142 			return (ISC_R_NOTFOUND);
143 		default:
144 			isc__strerror(errno, strbuf, sizeof(strbuf));
145 			UNEXPECTED_ERROR(__FILE__, __LINE__,
146 					 "socket() %s: %s",
147 					 isc_msgcat_get(isc_msgcat,
148 							ISC_MSGSET_GENERAL,
149 							ISC_MSG_FAILED,
150 							"failed"),
151 					 strbuf);
152 			return (ISC_R_UNEXPECTED);
153 		}
154 	}
155 
156 #ifdef ISC_PLATFORM_HAVEIPV6
157 #ifdef WANT_IPV6
158 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
159 	if (domain == PF_INET6) {
160 		struct sockaddr_in6 sin6;
161 		GETSOCKNAME_SOCKLEN_TYPE len;	/* NTP local change */
162 
163 		/*
164 		 * Check to see if IPv6 is broken, as is common on Linux.
165 		 */
166 		len = sizeof(sin6);
167 		if (getsockname(s, (struct sockaddr *)&sin6, &len) < 0)
168 		{
169 			isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
170 				      ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
171 				      "retrieving the address of an IPv6 "
172 				      "socket from the kernel failed.");
173 			isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
174 				      ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
175 				      "IPv6 is not supported.");
176 			result = ISC_R_NOTFOUND;
177 		} else {
178 			if (len == sizeof(struct sockaddr_in6))
179 				result = ISC_R_SUCCESS;
180 			else {
181 				isc_log_write(isc_lctx,
182 					      ISC_LOGCATEGORY_GENERAL,
183 					      ISC_LOGMODULE_SOCKET,
184 					      ISC_LOG_ERROR,
185 					      "IPv6 structures in kernel and "
186 					      "user space do not match.");
187 				isc_log_write(isc_lctx,
188 					      ISC_LOGCATEGORY_GENERAL,
189 					      ISC_LOGMODULE_SOCKET,
190 					      ISC_LOG_ERROR,
191 					      "IPv6 is not supported.");
192 				result = ISC_R_NOTFOUND;
193 			}
194 		}
195 	}
196 #endif
197 #endif
198 #endif
199 
200 	(void)close(s);
201 
202 	return (result);
203 }
204 
205 static void
206 initialize_action(void) {
207 	ipv4_result = try_proto(PF_INET);
208 #ifdef ISC_PLATFORM_HAVEIPV6
209 #ifdef WANT_IPV6
210 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
211 	ipv6_result = try_proto(PF_INET6);
212 #endif
213 #endif
214 #endif
215 #ifdef ISC_PLATFORM_HAVESYSUNH
216 	unix_result = try_proto(PF_UNIX);
217 #endif
218 }
219 
220 static void
221 initialize(void) {
222 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
223 }
224 
225 isc_result_t
226 isc_net_probeipv4(void) {
227 	initialize();
228 	return (ipv4_result);
229 }
230 
231 isc_result_t
232 isc_net_probeipv6(void) {
233 	initialize();
234 	return (ipv6_result);
235 }
236 
237 isc_result_t
238 isc_net_probeunix(void) {
239 	initialize();
240 	return (unix_result);
241 }
242 
243 #ifdef ISC_PLATFORM_HAVEIPV6
244 #ifdef WANT_IPV6
245 static void
246 try_ipv6only(void) {
247 #ifdef IPV6_V6ONLY
248 	int s, on;
249 	char strbuf[ISC_STRERRORSIZE];
250 #endif
251 	isc_result_t result;
252 
253 	result = isc_net_probeipv6();
254 	if (result != ISC_R_SUCCESS) {
255 		ipv6only_result = result;
256 		return;
257 	}
258 
259 #ifndef IPV6_V6ONLY
260 	ipv6only_result = ISC_R_NOTFOUND;
261 	return;
262 #else
263 	/* check for TCP sockets */
264 	s = socket(PF_INET6, SOCK_STREAM, 0);
265 	if (s == -1) {
266 		isc__strerror(errno, strbuf, sizeof(strbuf));
267 		UNEXPECTED_ERROR(__FILE__, __LINE__,
268 				 "socket() %s: %s",
269 				 isc_msgcat_get(isc_msgcat,
270 						ISC_MSGSET_GENERAL,
271 						ISC_MSG_FAILED,
272 						"failed"),
273 				 strbuf);
274 		ipv6only_result = ISC_R_UNEXPECTED;
275 		return;
276 	}
277 
278 	on = 1;
279 	if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
280 		ipv6only_result = ISC_R_NOTFOUND;
281 		goto close;
282 	}
283 
284 	close(s);
285 
286 	/* check for UDP sockets */
287 	s = socket(PF_INET6, SOCK_DGRAM, 0);
288 	if (s == -1) {
289 		isc__strerror(errno, strbuf, sizeof(strbuf));
290 		UNEXPECTED_ERROR(__FILE__, __LINE__,
291 				 "socket() %s: %s",
292 				 isc_msgcat_get(isc_msgcat,
293 						ISC_MSGSET_GENERAL,
294 						ISC_MSG_FAILED,
295 						"failed"),
296 				 strbuf);
297 		ipv6only_result = ISC_R_UNEXPECTED;
298 		return;
299 	}
300 
301 	on = 1;
302 	if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
303 		ipv6only_result = ISC_R_NOTFOUND;
304 		goto close;
305 	}
306 
307 	close(s);
308 
309 	ipv6only_result = ISC_R_SUCCESS;
310 
311 close:
312 	close(s);
313 	return;
314 #endif /* IPV6_V6ONLY */
315 }
316 
317 static void
318 initialize_ipv6only(void) {
319 	RUNTIME_CHECK(isc_once_do(&once_ipv6only,
320 				  try_ipv6only) == ISC_R_SUCCESS);
321 }
322 
323 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
324 static void
325 try_ipv6pktinfo(void) {
326 	int s, on;
327 	char strbuf[ISC_STRERRORSIZE];
328 	isc_result_t result;
329 	int optname;
330 
331 	result = isc_net_probeipv6();
332 	if (result != ISC_R_SUCCESS) {
333 		ipv6pktinfo_result = result;
334 		return;
335 	}
336 
337 	/* we only use this for UDP sockets */
338 	s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
339 	if (s == -1) {
340 		isc__strerror(errno, strbuf, sizeof(strbuf));
341 		UNEXPECTED_ERROR(__FILE__, __LINE__,
342 				 "socket() %s: %s",
343 				 isc_msgcat_get(isc_msgcat,
344 						ISC_MSGSET_GENERAL,
345 						ISC_MSG_FAILED,
346 						"failed"),
347 				 strbuf);
348 		ipv6pktinfo_result = ISC_R_UNEXPECTED;
349 		return;
350 	}
351 
352 #ifdef IPV6_RECVPKTINFO
353 	optname = IPV6_RECVPKTINFO;
354 #else
355 	optname = IPV6_PKTINFO;
356 #endif
357 	on = 1;
358 	if (setsockopt(s, IPPROTO_IPV6, optname, &on, sizeof(on)) < 0) {
359 		ipv6pktinfo_result = ISC_R_NOTFOUND;
360 		goto close;
361 	}
362 
363 	close(s);
364 	ipv6pktinfo_result = ISC_R_SUCCESS;
365 
366 close:
367 	close(s);
368 	return;
369 }
370 
371 static void
372 initialize_ipv6pktinfo(void) {
373 	RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
374 				  try_ipv6pktinfo) == ISC_R_SUCCESS);
375 }
376 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
377 #endif /* ISC_PLATFORM_HAVEIPV6 */
378 #endif /* WANT_IPV6 */
379 
380 isc_result_t
381 isc_net_probe_ipv6only(void) {
382 #ifdef ISC_PLATFORM_HAVEIPV6
383 #ifdef WANT_IPV6
384 	initialize_ipv6only();
385 #else
386 	ipv6only_result = ISC_R_NOTFOUND;
387 #endif
388 #endif
389 	return (ipv6only_result);
390 }
391 
392 isc_result_t
393 isc_net_probe_ipv6pktinfo(void) {
394 #ifdef ISC_PLATFORM_HAVEIPV6
395 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
396 #ifdef WANT_IPV6
397 	initialize_ipv6pktinfo();
398 #else
399 	ipv6pktinfo_result = ISC_R_NOTFOUND;
400 #endif
401 #endif
402 #endif
403 	return (ipv6pktinfo_result);
404 }
405 
406 #if defined(USE_SYSCTL_PORTRANGE)
407 #if defined(HAVE_SYSCTLBYNAME)
408 static isc_result_t
409 getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
410 	int port_low, port_high;
411 	size_t portlen;
412 	const char *sysctlname_lowport, *sysctlname_hiport;
413 
414 	if (af == AF_INET) {
415 		sysctlname_lowport = SYSCTL_V4PORTRANGE_LOW;
416 		sysctlname_hiport = SYSCTL_V4PORTRANGE_HIGH;
417 	} else {
418 		sysctlname_lowport = SYSCTL_V6PORTRANGE_LOW;
419 		sysctlname_hiport = SYSCTL_V6PORTRANGE_HIGH;
420 	}
421 	portlen = sizeof(portlen);
422 	if (sysctlbyname(sysctlname_lowport, &port_low, &portlen,
423 			 NULL, 0) < 0) {
424 		return (ISC_R_FAILURE);
425 	}
426 	portlen = sizeof(portlen);
427 	if (sysctlbyname(sysctlname_hiport, &port_high, &portlen,
428 			 NULL, 0) < 0) {
429 		return (ISC_R_FAILURE);
430 	}
431 	if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
432 		return (ISC_R_RANGE);
433 
434 	*low = (in_port_t)port_low;
435 	*high = (in_port_t)port_high;
436 
437 	return (ISC_R_SUCCESS);
438 }
439 #else /* !HAVE_SYSCTLBYNAME */
440 static isc_result_t
441 getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
442 	int mib_lo4[4] = SYSCTL_V4PORTRANGE_LOW;
443 	int mib_hi4[4] = SYSCTL_V4PORTRANGE_HIGH;
444 	int mib_lo6[4] = SYSCTL_V6PORTRANGE_LOW;
445 	int mib_hi6[4] = SYSCTL_V6PORTRANGE_HIGH;
446 	int *mib_lo, *mib_hi, miblen;
447 	int port_low, port_high;
448 	size_t portlen;
449 
450 	if (af == AF_INET) {
451 		mib_lo = mib_lo4;
452 		mib_hi = mib_hi4;
453 		miblen = sizeof(mib_lo4) / sizeof(mib_lo4[0]);
454 	} else {
455 		mib_lo = mib_lo6;
456 		mib_hi = mib_hi6;
457 		miblen = sizeof(mib_lo6) / sizeof(mib_lo6[0]);
458 	}
459 
460 	portlen = sizeof(portlen);
461 	if (sysctl(mib_lo, miblen, &port_low, &portlen, NULL, 0) < 0) {
462 		return (ISC_R_FAILURE);
463 	}
464 
465 	portlen = sizeof(portlen);
466 	if (sysctl(mib_hi, miblen, &port_high, &portlen, NULL, 0) < 0) {
467 		return (ISC_R_FAILURE);
468 	}
469 
470 	if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
471 		return (ISC_R_RANGE);
472 
473 	*low = (in_port_t) port_low;
474 	*high = (in_port_t) port_high;
475 
476 	return (ISC_R_SUCCESS);
477 }
478 #endif /* HAVE_SYSCTLBYNAME */
479 #endif /* USE_SYSCTL_PORTRANGE */
480 
481 isc_result_t
482 isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
483 	int result = ISC_R_FAILURE;
484 
485 	REQUIRE(low != NULL && high != NULL);
486 
487 #if defined(USE_SYSCTL_PORTRANGE)
488 	result = getudpportrange_sysctl(af, low, high);
489 #else
490 	UNUSED(af);
491 #endif
492 
493 	if (result != ISC_R_SUCCESS) {
494 		*low = ISC_NET_PORTRANGELOW;
495 		*high = ISC_NET_PORTRANGEHIGH;
496 	}
497 
498 	return (ISC_R_SUCCESS);	/* we currently never fail in this function */
499 }
500 
501 void
502 isc_net_disableipv4(void) {
503 	initialize();
504 	if (ipv4_result == ISC_R_SUCCESS)
505 		ipv4_result = ISC_R_DISABLED;
506 }
507 
508 void
509 isc_net_disableipv6(void) {
510 	initialize();
511 	if (ipv6_result == ISC_R_SUCCESS)
512 		ipv6_result = ISC_R_DISABLED;
513 }
514 
515 void
516 isc_net_enableipv4(void) {
517 	initialize();
518 	if (ipv4_result == ISC_R_DISABLED)
519 		ipv4_result = ISC_R_SUCCESS;
520 }
521 
522 void
523 isc_net_enableipv6(void) {
524 	initialize();
525 	if (ipv6_result == ISC_R_DISABLED)
526 		ipv6_result = ISC_R_SUCCESS;
527 }
528