Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / unix / net.c
blobde7672fc5c0107e9f78bd86a3d468e915b9e58b6
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
20 /* Id: net.c,v 1.40 2008/07/04 05:52:31 each Exp */
22 #include <config.h>
24 #include <sys/types.h>
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
33 #include <errno.h>
34 #include <unistd.h>
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>
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...
51 /*%
52 * The last resort defaults: use all non well known port space
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 */
61 #ifdef HAVE_SYSCTLBYNAME
63 /*%
64 * sysctl variants
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
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
82 #else /* !HAVE_SYSCTLBYNAME */
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
95 #endif /* HAVE_SYSCTLBYNAME */
97 #if defined(ISC_PLATFORM_HAVEIPV6)
98 # if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
99 const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
100 # endif
102 # if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
103 const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
104 # endif
106 # if defined(WANT_IPV6)
107 static isc_once_t once_ipv6only = ISC_ONCE_INIT;
108 # endif
110 # if defined(ISC_PLATFORM_HAVEIN6PKTINFO)
111 static isc_once_t once_ipv6pktinfo = ISC_ONCE_INIT;
112 # endif
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;
123 static isc_result_t
124 try_proto(int domain) {
125 int s;
126 isc_result_t result = ISC_R_SUCCESS;
127 char strbuf[ISC_STRERRORSIZE];
129 s = socket(domain, SOCK_STREAM, 0);
130 if (s == -1) {
131 switch (errno) {
132 #ifdef EAFNOSUPPORT
133 case EAFNOSUPPORT:
134 #endif
135 #ifdef EPROTONOSUPPORT
136 case EPROTONOSUPPORT:
137 #endif
138 #ifdef EINVAL
139 case EINVAL:
140 #endif
141 return (ISC_R_NOTFOUND);
142 default:
143 isc__strerror(errno, strbuf, sizeof(strbuf));
144 UNEXPECTED_ERROR(__FILE__, __LINE__,
145 "socket() %s: %s",
146 isc_msgcat_get(isc_msgcat,
147 ISC_MSGSET_GENERAL,
148 ISC_MSG_FAILED,
149 "failed"),
150 strbuf);
151 return (ISC_R_UNEXPECTED);
155 #ifdef ISC_PLATFORM_HAVEIPV6
156 #ifdef WANT_IPV6
157 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
158 if (domain == PF_INET6) {
159 struct sockaddr_in6 sin6;
160 unsigned int len;
163 * Check to see if IPv6 is broken, as is common on Linux.
165 len = sizeof(sin6);
166 if (getsockname(s, (struct sockaddr *)&sin6, (void *)&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;
176 } else {
177 if (len == sizeof(struct sockaddr_in6))
178 result = ISC_R_SUCCESS;
179 else {
180 isc_log_write(isc_lctx,
181 ISC_LOGCATEGORY_GENERAL,
182 ISC_LOGMODULE_SOCKET,
183 ISC_LOG_ERROR,
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,
189 ISC_LOG_ERROR,
190 "IPv6 is not supported.");
191 result = ISC_R_NOTFOUND;
195 #endif
196 #endif
197 #endif
199 (void)close(s);
201 return (result);
204 static void
205 initialize_action(void) {
206 ipv4_result = try_proto(PF_INET);
207 #ifdef ISC_PLATFORM_HAVEIPV6
208 #ifdef WANT_IPV6
209 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
210 ipv6_result = try_proto(PF_INET6);
211 #endif
212 #endif
213 #endif
214 #ifdef ISC_PLATFORM_HAVESYSUNH
215 unix_result = try_proto(PF_UNIX);
216 #endif
219 static void
220 initialize(void) {
221 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
224 isc_result_t
225 isc_net_probeipv4(void) {
226 initialize();
227 return (ipv4_result);
230 isc_result_t
231 isc_net_probeipv6(void) {
232 initialize();
233 return (ipv6_result);
236 isc_result_t
237 isc_net_probeunix(void) {
238 initialize();
239 return (unix_result);
242 #ifdef ISC_PLATFORM_HAVEIPV6
243 #ifdef WANT_IPV6
244 static void
245 try_ipv6only(void) {
246 #ifdef IPV6_V6ONLY
247 int s, on;
248 char strbuf[ISC_STRERRORSIZE];
249 #endif
250 isc_result_t result;
252 result = isc_net_probeipv6();
253 if (result != ISC_R_SUCCESS) {
254 ipv6only_result = result;
255 return;
258 #ifndef IPV6_V6ONLY
259 ipv6only_result = ISC_R_NOTFOUND;
260 return;
261 #else
262 /* check for TCP sockets */
263 s = socket(PF_INET6, SOCK_STREAM, 0);
264 if (s == -1) {
265 isc__strerror(errno, strbuf, sizeof(strbuf));
266 UNEXPECTED_ERROR(__FILE__, __LINE__,
267 "socket() %s: %s",
268 isc_msgcat_get(isc_msgcat,
269 ISC_MSGSET_GENERAL,
270 ISC_MSG_FAILED,
271 "failed"),
272 strbuf);
273 ipv6only_result = ISC_R_UNEXPECTED;
274 return;
277 on = 1;
278 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
279 ipv6only_result = ISC_R_NOTFOUND;
280 goto close;
283 close(s);
285 /* check for UDP sockets */
286 s = socket(PF_INET6, SOCK_DGRAM, 0);
287 if (s == -1) {
288 isc__strerror(errno, strbuf, sizeof(strbuf));
289 UNEXPECTED_ERROR(__FILE__, __LINE__,
290 "socket() %s: %s",
291 isc_msgcat_get(isc_msgcat,
292 ISC_MSGSET_GENERAL,
293 ISC_MSG_FAILED,
294 "failed"),
295 strbuf);
296 ipv6only_result = ISC_R_UNEXPECTED;
297 return;
300 on = 1;
301 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) {
302 ipv6only_result = ISC_R_NOTFOUND;
303 goto close;
306 close(s);
308 ipv6only_result = ISC_R_SUCCESS;
310 close:
311 close(s);
312 return;
313 #endif /* IPV6_V6ONLY */
316 static void
317 initialize_ipv6only(void) {
318 RUNTIME_CHECK(isc_once_do(&once_ipv6only,
319 try_ipv6only) == ISC_R_SUCCESS);
321 #endif /* WANT_IPV6 */
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;
331 result = isc_net_probeipv6();
332 if (result != ISC_R_SUCCESS) {
333 ipv6pktinfo_result = result;
334 return;
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;
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;
363 close(s);
364 ipv6pktinfo_result = ISC_R_SUCCESS;
366 close:
367 close(s);
368 return;
371 static void
372 initialize_ipv6pktinfo(void) {
373 RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
374 try_ipv6pktinfo) == ISC_R_SUCCESS);
376 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
377 #endif /* ISC_PLATFORM_HAVEIPV6 */
379 isc_result_t
380 isc_net_probe_ipv6only(void) {
381 #ifdef ISC_PLATFORM_HAVEIPV6
382 #ifdef WANT_IPV6
383 initialize_ipv6only();
384 #else
385 ipv6only_result = ISC_R_NOTFOUND;
386 #endif
387 #endif
388 return (ipv6only_result);
391 isc_result_t
392 isc_net_probe_ipv6pktinfo(void) {
393 #ifdef ISC_PLATFORM_HAVEIPV6
394 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
395 #ifdef WANT_IPV6
396 initialize_ipv6pktinfo();
397 #else
398 ipv6pktinfo_result = ISC_R_NOTFOUND;
399 #endif
400 #endif
401 #endif
402 return (ipv6pktinfo_result);
405 #if defined(USE_SYSCTL_PORTRANGE)
406 #if defined(HAVE_SYSCTLBYNAME)
407 static isc_result_t
408 getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
409 int port_low, port_high;
410 size_t portlen;
411 const char *sysctlname_lowport, *sysctlname_hiport;
413 if (af == AF_INET) {
414 sysctlname_lowport = SYSCTL_V4PORTRANGE_LOW;
415 sysctlname_hiport = SYSCTL_V4PORTRANGE_HIGH;
416 } else {
417 sysctlname_lowport = SYSCTL_V6PORTRANGE_LOW;
418 sysctlname_hiport = SYSCTL_V6PORTRANGE_HIGH;
420 portlen = sizeof(portlen);
421 if (sysctlbyname(sysctlname_lowport, &port_low, &portlen,
422 NULL, 0) < 0) {
423 return (ISC_R_FAILURE);
425 portlen = sizeof(portlen);
426 if (sysctlbyname(sysctlname_hiport, &port_high, &portlen,
427 NULL, 0) < 0) {
428 return (ISC_R_FAILURE);
430 if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
431 return (ISC_R_RANGE);
433 *low = (in_port_t)port_low;
434 *high = (in_port_t)port_high;
436 return (ISC_R_SUCCESS);
438 #else /* !HAVE_SYSCTLBYNAME */
439 static isc_result_t
440 getudpportrange_sysctl(int af, in_port_t *low, in_port_t *high) {
441 int mib_lo4[4] = SYSCTL_V4PORTRANGE_LOW;
442 int mib_hi4[4] = SYSCTL_V4PORTRANGE_HIGH;
443 int mib_lo6[4] = SYSCTL_V6PORTRANGE_LOW;
444 int mib_hi6[4] = SYSCTL_V6PORTRANGE_HIGH;
445 int *mib_lo, *mib_hi, miblen;
446 int port_low, port_high;
447 size_t portlen;
449 if (af == AF_INET) {
450 mib_lo = mib_lo4;
451 mib_hi = mib_hi4;
452 miblen = sizeof(mib_lo4) / sizeof(mib_lo4[0]);
453 } else {
454 mib_lo = mib_lo6;
455 mib_hi = mib_hi6;
456 miblen = sizeof(mib_lo6) / sizeof(mib_lo6[0]);
459 portlen = sizeof(portlen);
460 if (sysctl(mib_lo, miblen, &port_low, &portlen, NULL, 0) < 0) {
461 return (ISC_R_FAILURE);
464 portlen = sizeof(portlen);
465 if (sysctl(mib_hi, miblen, &port_high, &portlen, NULL, 0) < 0) {
466 return (ISC_R_FAILURE);
469 if ((port_low & ~0xffff) != 0 || (port_high & ~0xffff) != 0)
470 return (ISC_R_RANGE);
472 *low = (in_port_t) port_low;
473 *high = (in_port_t) port_high;
475 return (ISC_R_SUCCESS);
477 #endif /* HAVE_SYSCTLBYNAME */
478 #endif /* USE_SYSCTL_PORTRANGE */
480 isc_result_t
481 isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
482 int result = ISC_R_FAILURE;
484 REQUIRE(low != NULL && high != NULL);
486 #if defined(USE_SYSCTL_PORTRANGE)
487 result = getudpportrange_sysctl(af, low, high);
488 #else
489 UNUSED(af);
490 #endif
492 if (result != ISC_R_SUCCESS) {
493 *low = ISC_NET_PORTRANGELOW;
494 *high = ISC_NET_PORTRANGEHIGH;
497 return (ISC_R_SUCCESS); /* we currently never fail in this function */
500 void
501 isc_net_disableipv4(void) {
502 initialize();
503 if (ipv4_result == ISC_R_SUCCESS)
504 ipv4_result = ISC_R_DISABLED;
507 void
508 isc_net_disableipv6(void) {
509 initialize();
510 if (ipv6_result == ISC_R_SUCCESS)
511 ipv6_result = ISC_R_DISABLED;
514 void
515 isc_net_enableipv4(void) {
516 initialize();
517 if (ipv4_result == ISC_R_DISABLED)
518 ipv4_result = ISC_R_SUCCESS;
521 void
522 isc_net_enableipv6(void) {
523 initialize();
524 if (ipv6_result == ISC_R_DISABLED)
525 ipv6_result = ISC_R_SUCCESS;