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