2 * Copyright (C) 2004, 2005, 2007, 2008 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.
18 /* $Id: net.c,v 1.36.60.4 2008/07/04 05:52:16 each Exp $ */
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_HAVEIPV6)
96 # if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
97 const struct in6_addr isc_net_in6addrany
= IN6ADDR_ANY_INIT
;
100 # if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
101 const struct in6_addr isc_net_in6addrloop
= IN6ADDR_LOOPBACK_INIT
;
104 # if defined(WANT_IPV6)
105 static isc_once_t once_ipv6only
= ISC_ONCE_INIT
;
108 # if defined(ISC_PLATFORM_HAVEIN6PKTINFO)
109 static isc_once_t once_ipv6pktinfo
= ISC_ONCE_INIT
;
111 #endif /* ISC_PLATFORM_HAVEIPV6 */
113 static isc_once_t once
= ISC_ONCE_INIT
;
115 static isc_result_t ipv4_result
= ISC_R_NOTFOUND
;
116 static isc_result_t ipv6_result
= ISC_R_NOTFOUND
;
117 static isc_result_t unix_result
= ISC_R_NOTFOUND
;
118 static isc_result_t ipv6only_result
= ISC_R_NOTFOUND
;
119 static isc_result_t ipv6pktinfo_result
= ISC_R_NOTFOUND
;
122 try_proto(int domain
) {
124 isc_result_t result
= ISC_R_SUCCESS
;
125 char strbuf
[ISC_STRERRORSIZE
];
127 s
= socket(domain
, SOCK_STREAM
, 0);
133 #ifdef EPROTONOSUPPORT
134 case EPROTONOSUPPORT
:
139 return (ISC_R_NOTFOUND
);
141 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
142 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
144 isc_msgcat_get(isc_msgcat
,
149 return (ISC_R_UNEXPECTED
);
153 #ifdef ISC_PLATFORM_HAVEIPV6
155 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
156 if (domain
== PF_INET6
) {
157 struct sockaddr_in6 sin6
;
161 * Check to see if IPv6 is broken, as is common on Linux.
164 if (getsockname(s
, (struct sockaddr
*)&sin6
, (void *)&len
) < 0)
166 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
167 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
168 "retrieving the address of an IPv6 "
169 "socket from the kernel failed.");
170 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
171 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
172 "IPv6 is not supported.");
173 result
= ISC_R_NOTFOUND
;
175 if (len
== sizeof(struct sockaddr_in6
))
176 result
= ISC_R_SUCCESS
;
178 isc_log_write(isc_lctx
,
179 ISC_LOGCATEGORY_GENERAL
,
180 ISC_LOGMODULE_SOCKET
,
182 "IPv6 structures in kernel and "
183 "user space do not match.");
184 isc_log_write(isc_lctx
,
185 ISC_LOGCATEGORY_GENERAL
,
186 ISC_LOGMODULE_SOCKET
,
188 "IPv6 is not supported.");
189 result
= ISC_R_NOTFOUND
;
203 initialize_action(void) {
204 ipv4_result
= try_proto(PF_INET
);
205 #ifdef ISC_PLATFORM_HAVEIPV6
207 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
208 ipv6_result
= try_proto(PF_INET6
);
212 #ifdef ISC_PLATFORM_HAVESYSUNH
213 unix_result
= try_proto(PF_UNIX
);
219 RUNTIME_CHECK(isc_once_do(&once
, initialize_action
) == ISC_R_SUCCESS
);
223 isc_net_probeipv4(void) {
225 return (ipv4_result
);
229 isc_net_probeipv6(void) {
231 return (ipv6_result
);
235 isc_net_probeunix(void) {
237 return (unix_result
);
240 #ifdef ISC_PLATFORM_HAVEIPV6
246 char strbuf
[ISC_STRERRORSIZE
];
250 result
= isc_net_probeipv6();
251 if (result
!= ISC_R_SUCCESS
) {
252 ipv6only_result
= result
;
257 ipv6only_result
= ISC_R_NOTFOUND
;
260 /* check for TCP sockets */
261 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
263 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
264 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
266 isc_msgcat_get(isc_msgcat
,
271 ipv6only_result
= ISC_R_UNEXPECTED
;
276 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
)) < 0) {
277 ipv6only_result
= ISC_R_NOTFOUND
;
283 /* check for UDP sockets */
284 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
286 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
287 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
289 isc_msgcat_get(isc_msgcat
,
294 ipv6only_result
= ISC_R_UNEXPECTED
;
299 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
)) < 0) {
300 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
);
319 #endif /* WANT_IPV6 */
321 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
323 try_ipv6pktinfo(void) {
325 char strbuf
[ISC_STRERRORSIZE
];
329 result
= isc_net_probeipv6();
330 if (result
!= ISC_R_SUCCESS
) {
331 ipv6pktinfo_result
= result
;
335 /* we only use this for UDP sockets */
336 s
= socket(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
);
338 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
339 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
341 isc_msgcat_get(isc_msgcat
,
346 ipv6pktinfo_result
= ISC_R_UNEXPECTED
;
350 #ifdef IPV6_RECVPKTINFO
351 optname
= IPV6_RECVPKTINFO
;
353 optname
= IPV6_PKTINFO
;
356 if (setsockopt(s
, IPPROTO_IPV6
, optname
, &on
, sizeof(on
)) < 0) {
357 ipv6pktinfo_result
= ISC_R_NOTFOUND
;
362 ipv6pktinfo_result
= ISC_R_SUCCESS
;
370 initialize_ipv6pktinfo(void) {
371 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo
,
372 try_ipv6pktinfo
) == ISC_R_SUCCESS
);
374 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
375 #endif /* ISC_PLATFORM_HAVEIPV6 */
378 isc_net_probe_ipv6only(void) {
379 #ifdef ISC_PLATFORM_HAVEIPV6
381 initialize_ipv6only();
383 ipv6only_result
= ISC_R_NOTFOUND
;
386 return (ipv6only_result
);
390 isc_net_probe_ipv6pktinfo(void) {
391 #ifdef ISC_PLATFORM_HAVEIPV6
392 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
394 initialize_ipv6pktinfo();
396 ipv6pktinfo_result
= ISC_R_NOTFOUND
;
400 return (ipv6pktinfo_result
);
403 #if defined(USE_SYSCTL_PORTRANGE)
404 #if defined(HAVE_SYSCTLBYNAME)
406 getudpportrange_sysctl(int af
, in_port_t
*low
, in_port_t
*high
) {
407 int port_low
, port_high
;
409 const char *sysctlname_lowport
, *sysctlname_hiport
;
412 sysctlname_lowport
= SYSCTL_V4PORTRANGE_LOW
;
413 sysctlname_hiport
= SYSCTL_V4PORTRANGE_HIGH
;
415 sysctlname_lowport
= SYSCTL_V6PORTRANGE_LOW
;
416 sysctlname_hiport
= SYSCTL_V6PORTRANGE_HIGH
;
418 portlen
= sizeof(portlen
);
419 if (sysctlbyname(sysctlname_lowport
, &port_low
, &portlen
,
421 return (ISC_R_FAILURE
);
423 portlen
= sizeof(portlen
);
424 if (sysctlbyname(sysctlname_hiport
, &port_high
, &portlen
,
426 return (ISC_R_FAILURE
);
428 if ((port_low
& ~0xffff) != 0 || (port_high
& ~0xffff) != 0)
429 return (ISC_R_RANGE
);
431 *low
= (in_port_t
)port_low
;
432 *high
= (in_port_t
)port_high
;
434 return (ISC_R_SUCCESS
);
436 #else /* !HAVE_SYSCTLBYNAME */
438 getudpportrange_sysctl(int af
, in_port_t
*low
, in_port_t
*high
) {
439 int mib_lo4
[4] = SYSCTL_V4PORTRANGE_LOW
;
440 int mib_hi4
[4] = SYSCTL_V4PORTRANGE_HIGH
;
441 int mib_lo6
[4] = SYSCTL_V6PORTRANGE_LOW
;
442 int mib_hi6
[4] = SYSCTL_V6PORTRANGE_HIGH
;
443 int *mib_lo
, *mib_hi
, miblen
;
444 int port_low
, port_high
;
450 miblen
= sizeof(mib_lo4
) / sizeof(mib_lo4
[0]);
454 miblen
= sizeof(mib_lo6
) / sizeof(mib_lo6
[0]);
457 portlen
= sizeof(portlen
);
458 if (sysctl(mib_lo
, miblen
, &port_low
, &portlen
, NULL
, 0) < 0) {
459 return (ISC_R_FAILURE
);
462 portlen
= sizeof(portlen
);
463 if (sysctl(mib_hi
, miblen
, &port_high
, &portlen
, NULL
, 0) < 0) {
464 return (ISC_R_FAILURE
);
467 if ((port_low
& ~0xffff) != 0 || (port_high
& ~0xffff) != 0)
468 return (ISC_R_RANGE
);
470 *low
= (in_port_t
) port_low
;
471 *high
= (in_port_t
) port_high
;
473 return (ISC_R_SUCCESS
);
475 #endif /* HAVE_SYSCTLBYNAME */
476 #endif /* USE_SYSCTL_PORTRANGE */
479 isc_net_getudpportrange(int af
, in_port_t
*low
, in_port_t
*high
) {
480 int result
= ISC_R_FAILURE
;
482 REQUIRE(low
!= NULL
&& high
!= NULL
);
484 #if defined(USE_SYSCTL_PORTRANGE)
485 result
= getudpportrange_sysctl(af
, low
, high
);
490 if (result
!= ISC_R_SUCCESS
) {
491 *low
= ISC_NET_PORTRANGELOW
;
492 *high
= ISC_NET_PORTRANGEHIGH
;
495 return (ISC_R_SUCCESS
); /* we currently never fail in this function */
499 isc_net_disableipv4(void) {
501 if (ipv4_result
== ISC_R_SUCCESS
)
502 ipv4_result
= ISC_R_DISABLED
;
506 isc_net_disableipv6(void) {
508 if (ipv6_result
== ISC_R_SUCCESS
)
509 ipv6_result
= ISC_R_DISABLED
;
513 isc_net_enableipv4(void) {
515 if (ipv4_result
== ISC_R_DISABLED
)
516 ipv4_result
= ISC_R_SUCCESS
;
520 isc_net_enableipv6(void) {
522 if (ipv6_result
== ISC_R_DISABLED
)
523 ipv6_result
= ISC_R_SUCCESS
;