2 * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
24 #if defined(HAVE_SYS_SYSCTL_H)
25 #if defined(HAVE_SYS_PARAM_H)
26 #include <sys/param.h>
28 #include <sys/sysctl.h>
38 #include <isc/strerror.h>
39 #include <isc/string.h>
43 * Definitions about UDP port range specification. This is a total mess of
44 * portability variants: some use sysctl (but the sysctl names vary), some use
45 * system-specific interfaces, some have the same interface for IPv4 and IPv6,
46 * some separate them, etc...
50 * The last resort defaults: use all non well known port space
52 #ifndef ISC_NET_PORTRANGELOW
53 #define ISC_NET_PORTRANGELOW 1024
54 #endif /* ISC_NET_PORTRANGELOW */
55 #ifndef ISC_NET_PORTRANGEHIGH
56 #define ISC_NET_PORTRANGEHIGH 65535
57 #endif /* ISC_NET_PORTRANGEHIGH */
59 #ifdef HAVE_SYSCTLBYNAME
64 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
65 #define USE_SYSCTL_PORTRANGE
66 #define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
67 #define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
68 #define SYSCTL_V6PORTRANGE_LOW "net.inet.ip.portrange.hifirst"
69 #define SYSCTL_V6PORTRANGE_HIGH "net.inet.ip.portrange.hilast"
73 #define USE_SYSCTL_PORTRANGE
74 #define SYSCTL_V4PORTRANGE_LOW "net.inet.ip.anonportmin"
75 #define SYSCTL_V4PORTRANGE_HIGH "net.inet.ip.anonportmax"
76 #define SYSCTL_V6PORTRANGE_LOW "net.inet6.ip6.anonportmin"
77 #define SYSCTL_V6PORTRANGE_HIGH "net.inet6.ip6.anonportmax"
80 #else /* !HAVE_SYSCTLBYNAME */
83 #define USE_SYSCTL_PORTRANGE
84 #define SYSCTL_V4PORTRANGE_LOW { CTL_NET, PF_INET, IPPROTO_IP, \
85 IPCTL_IPPORT_HIFIRSTAUTO }
86 #define SYSCTL_V4PORTRANGE_HIGH { CTL_NET, PF_INET, IPPROTO_IP, \
87 IPCTL_IPPORT_HILASTAUTO }
89 #define SYSCTL_V6PORTRANGE_LOW SYSCTL_V4PORTRANGE_LOW
90 #define SYSCTL_V6PORTRANGE_HIGH SYSCTL_V4PORTRANGE_HIGH
93 #endif /* HAVE_SYSCTLBYNAME */
95 #if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
96 const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
99 #if defined(ISC_PLATFORM_HAVEIPV6)
101 # if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
102 const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
105 # if defined(WANT_IPV6)
106 static isc_once_t once_ipv6only = ISC_ONCE_INIT;
109 # if defined(ISC_PLATFORM_HAVEIPV6) && \
110 defined(WANT_IPV6) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
111 static isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
113 #endif /* ISC_PLATFORM_HAVEIPV6 */
115 static isc_once_t once = ISC_ONCE_INIT;
117 static isc_result_t ipv4_result = ISC_R_NOTFOUND;
118 static isc_result_t ipv6_result = ISC_R_NOTFOUND;
119 static isc_result_t unix_result = ISC_R_NOTFOUND;
120 static isc_result_t ipv6only_result = ISC_R_NOTFOUND;
121 static isc_result_t ipv6pktinfo_result = ISC_R_NOTFOUND;
124 try_proto(int domain) {
126 isc_result_t result = ISC_R_SUCCESS;
127 char strbuf[ISC_STRERRORSIZE];
129 s = socket(domain, SOCK_STREAM, 0);
135 #ifdef EPROTONOSUPPORT
136 case EPROTONOSUPPORT:
141 return (ISC_R_NOTFOUND);
143 isc__strerror(errno, strbuf, sizeof(strbuf));
144 UNEXPECTED_ERROR(__FILE__, __LINE__,
146 isc_msgcat_get(isc_msgcat,
151 return (ISC_R_UNEXPECTED);
155 #ifdef ISC_PLATFORM_HAVEIPV6
157 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
158 if (domain == PF_INET6) {
159 struct sockaddr_in6 sin6;
160 GETSOCKNAME_SOCKLEN_TYPE len; /* NTP local change */
163 * Check to see if IPv6 is broken, as is common on Linux.
166 if (getsockname(s, (struct sockaddr *)&sin6, &len) < 0)
168 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
169 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
170 "retrieving the address of an IPv6 "
171 "socket from the kernel failed.");
172 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
173 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
174 "IPv6 is not supported.");
175 result = ISC_R_NOTFOUND;
177 if (len == sizeof(struct sockaddr_in6))
178 result = ISC_R_SUCCESS;
180 isc_log_write(isc_lctx,
181 ISC_LOGCATEGORY_GENERAL,
182 ISC_LOGMODULE_SOCKET,
184 "IPv6 structures in kernel and "
185 "user space do not match.");
186 isc_log_write(isc_lctx,
187 ISC_LOGCATEGORY_GENERAL,
188 ISC_LOGMODULE_SOCKET,
190 "IPv6 is not supported.");
191 result = ISC_R_NOTFOUND;
205 initialize_action(void) {
206 ipv4_result = try_proto(PF_INET);
207 #ifdef ISC_PLATFORM_HAVEIPV6
209 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
210 ipv6_result = try_proto(PF_INET6);
214 #ifdef ISC_PLATFORM_HAVESYSUNH
215 unix_result = try_proto(PF_UNIX);
221 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
225 isc_net_probeipv4(void) {
227 return (ipv4_result);
231 isc_net_probeipv6(void) {
233 return (ipv6_result);
237 isc_net_probeunix(void) {
239 return (unix_result);
242 #ifdef ISC_PLATFORM_HAVEIPV6
248 char strbuf[ISC_STRERRORSIZE];
252 result = isc_net_probeipv6();
253 if (result != ISC_R_SUCCESS) {
254 ipv6only_result = result;
259 ipv6only_result = ISC_R_NOTFOUND;
262 /* check for TCP sockets */
263 s = socket(PF_INET6, SOCK_STREAM, 0);
265 isc__strerror(errno, strbuf, sizeof(strbuf));
266 UNEXPECTED_ERROR(__FILE__, __LINE__,
268 isc_msgcat_get(isc_msgcat,
273 ipv6only_result = ISC_R_UNEXPECTED;
278 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
279 ipv6only_result = ISC_R_NOTFOUND;
285 /* check for UDP sockets */
286 s = socket(PF_INET6, SOCK_DGRAM, 0);
288 isc__strerror(errno, strbuf, sizeof(strbuf));
289 UNEXPECTED_ERROR(__FILE__, __LINE__,
291 isc_msgcat_get(isc_msgcat,
296 ipv6only_result = ISC_R_UNEXPECTED;
301 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
302 ipv6only_result = ISC_R_NOTFOUND;
306 ipv6only_result = ISC_R_SUCCESS;
311 #endif /* IPV6_V6ONLY */
315 initialize_ipv6only(void) {
316 RUNTIME_CHECK(isc_once_do(&once_ipv6only,
317 try_ipv6only) == ISC_R_SUCCESS);
320 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
322 try_ipv6pktinfo(void) {
324 char strbuf[ISC_STRERRORSIZE];
328 result = isc_net_probeipv6();
329 if (result != ISC_R_SUCCESS) {
330 ipv6pktinfo_result = result;
334 /* we only use this for UDP sockets */
335 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
337 isc__strerror(errno, strbuf, sizeof(strbuf));
338 UNEXPECTED_ERROR(__FILE__, __LINE__,
340 isc_msgcat_get(isc_msgcat,
345 ipv6pktinfo_result = ISC_R_UNEXPECTED;
349 #ifdef IPV6_RECVPKTINFO
350 optname = IPV6_RECVPKTINFO;
352 optname = IPV6_PKTINFO;
355 if (setsockopt(s, IPPROTO_IPV6, optname, &on, sizeof(on)) < 0) {
356 ipv6pktinfo_result = ISC_R_NOTFOUND;
360 ipv6pktinfo_result = ISC_R_SUCCESS;
368 initialize_ipv6pktinfo(void) {
369 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
370 try_ipv6pktinfo) == ISC_R_SUCCESS);
372 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
373 #endif /* WANT_IPV6 */
374 #endif /* ISC_PLATFORM_HAVEIPV6 */
377 isc_net_probe_ipv6only(void) {
378 #ifdef ISC_PLATFORM_HAVEIPV6
380 initialize_ipv6only();
382 ipv6only_result = ISC_R_NOTFOUND;
385 return (ipv6only_result);
389 isc_net_probe_ipv6pktinfo(void) {
390 #ifdef ISC_PLATFORM_HAVEIPV6
391 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
393 initialize_ipv6pktinfo();
395 ipv6pktinfo_result = ISC_R_NOTFOUND;
399 return (ipv6pktinfo_result);
402 #if defined(USE_SYSCTL_PORTRANGE)
403 #if defined(HAVE_SYSCTLBYNAME)
405 getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
406 int port_low, port_high;
408 const char *sysctlname_lowport, *sysctlname_hiport;
411 sysctlname_lowport = SYSCTL_V4PORTRANGE_LOW;
412 sysctlname_hiport = SYSCTL_V4PORTRANGE_HIGH;
414 sysctlname_lowport = SYSCTL_V6PORTRANGE_LOW;
415 sysctlname_hiport = SYSCTL_V6PORTRANGE_HIGH;
417 portlen = sizeof(portlen);
418 if (sysctlbyname(sysctlname_lowport, &port_low, &portlen,
420 return (ISC_R_FAILURE);
422 portlen = sizeof(portlen);
423 if (sysctlbyname(sysctlname_hiport, &port_high, &portlen,
425 return (ISC_R_FAILURE);
427 if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
428 return (ISC_R_RANGE);
430 *low = (in_port_t)port_low;
431 *high = (in_port_t)port_high;
433 return (ISC_R_SUCCESS);
435 #else /* !HAVE_SYSCTLBYNAME */
437 getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
438 int mib_lo4[4] = SYSCTL_V4PORTRANGE_LOW;
439 int mib_hi4[4] = SYSCTL_V4PORTRANGE_HIGH;
440 int mib_lo6[4] = SYSCTL_V6PORTRANGE_LOW;
441 int mib_hi6[4] = SYSCTL_V6PORTRANGE_HIGH;
442 int *mib_lo, *mib_hi, miblen;
443 int port_low, port_high;
449 miblen = sizeof(mib_lo4) / sizeof(mib_lo4[0]);
453 miblen = sizeof(mib_lo6) / sizeof(mib_lo6[0]);
456 portlen = sizeof(portlen);
457 if (sysctl(mib_lo, miblen, &port_low, &portlen, NULL, 0) < 0) {
458 return (ISC_R_FAILURE);
461 portlen = sizeof(portlen);
462 if (sysctl(mib_hi, miblen, &port_high, &portlen, NULL, 0) < 0) {
463 return (ISC_R_FAILURE);
466 if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
467 return (ISC_R_RANGE);
469 *low = (in_port_t) port_low;
470 *high = (in_port_t) port_high;
472 return (ISC_R_SUCCESS);
474 #endif /* HAVE_SYSCTLBYNAME */
475 #endif /* USE_SYSCTL_PORTRANGE */
478 isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
479 int result = ISC_R_FAILURE;
481 REQUIRE(low != NULL && high != NULL);
483 #if defined(USE_SYSCTL_PORTRANGE)
484 result = getudpportrange_sysctl(af, low, high);
489 if (result != ISC_R_SUCCESS) {
490 *low = ISC_NET_PORTRANGELOW;
491 *high = ISC_NET_PORTRANGEHIGH;
494 return (ISC_R_SUCCESS); /* we currently never fail in this function */
498 isc_net_disableipv4(void) {
500 if (ipv4_result == ISC_R_SUCCESS)
501 ipv4_result = ISC_R_DISABLED;
505 isc_net_disableipv6(void) {
507 if (ipv6_result == ISC_R_SUCCESS)
508 ipv6_result = ISC_R_DISABLED;
512 isc_net_enableipv4(void) {
514 if (ipv4_result == ISC_R_DISABLED)
515 ipv4_result = ISC_R_SUCCESS;
519 isc_net_enableipv6(void) {
521 if (ipv6_result == ISC_R_DISABLED)
522 ipv6_result = ISC_R_SUCCESS;