xref: /freebsd/contrib/ntp/libntp/lib/isc/win32/net.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007-2009, 2011, 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 <errno.h>
23*a466cc55SCy Schubert #include <unistd.h>
24*a466cc55SCy Schubert 
25*a466cc55SCy Schubert #include <isc/log.h>
26*a466cc55SCy Schubert #include <isc/msgs.h>
27*a466cc55SCy Schubert #include <isc/net.h>
28*a466cc55SCy Schubert #include <isc/once.h>
29*a466cc55SCy Schubert #include <isc/strerror.h>
30*a466cc55SCy Schubert #include <isc/string.h>
31*a466cc55SCy Schubert #include <isc/util.h>
32*a466cc55SCy Schubert 
33*a466cc55SCy Schubert /*%
34*a466cc55SCy Schubert  * Definitions about UDP port range specification.  This is a total mess of
35*a466cc55SCy Schubert  * portability variants: some use sysctl (but the sysctl names vary), some use
36*a466cc55SCy Schubert  * system-specific interfaces, some have the same interface for IPv4 and IPv6,
37*a466cc55SCy Schubert  * some separate them, etc...
38*a466cc55SCy Schubert  */
39*a466cc55SCy Schubert 
40*a466cc55SCy Schubert /*%
41*a466cc55SCy Schubert  * The last resort defaults: use all non well known port space
42*a466cc55SCy Schubert  */
43*a466cc55SCy Schubert #ifndef ISC_NET_PORTRANGELOW
44*a466cc55SCy Schubert #define ISC_NET_PORTRANGELOW 1024
45*a466cc55SCy Schubert #endif	/* ISC_NET_PORTRANGELOW */
46*a466cc55SCy Schubert #ifndef ISC_NET_PORTRANGEHIGH
47*a466cc55SCy Schubert #define ISC_NET_PORTRANGEHIGH 65535
48*a466cc55SCy Schubert #endif	/* ISC_NET_PORTRANGEHIGH */
49*a466cc55SCy Schubert 
50*a466cc55SCy Schubert #if defined(ISC_PLATFORM_NEEDIN6ADDRANY)
51*a466cc55SCy Schubert const struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
52*a466cc55SCy Schubert #endif
53*a466cc55SCy Schubert 
54*a466cc55SCy Schubert #if defined(ISC_PLATFORM_NEEDIN6ADDRLOOPBACK)
55*a466cc55SCy Schubert const struct in6_addr isc_net_in6addrloop = IN6ADDR_LOOPBACK_INIT;
56*a466cc55SCy Schubert #endif
57*a466cc55SCy Schubert 
58*a466cc55SCy Schubert 
59*a466cc55SCy Schubert static isc_once_t 	once = ISC_ONCE_INIT;
60*a466cc55SCy Schubert static isc_once_t 	once_ipv6only = ISC_ONCE_INIT;
61*a466cc55SCy Schubert static isc_once_t 	once_ipv6pktinfo = ISC_ONCE_INIT;
62*a466cc55SCy Schubert static isc_result_t	ipv4_result = ISC_R_NOTFOUND;
63*a466cc55SCy Schubert static isc_result_t	ipv6_result = ISC_R_NOTFOUND;
64*a466cc55SCy Schubert static isc_result_t	ipv6only_result = ISC_R_NOTFOUND;
65*a466cc55SCy Schubert static isc_result_t	ipv6pktinfo_result = ISC_R_NOTFOUND;
66*a466cc55SCy Schubert 
67*a466cc55SCy Schubert void InitSockets(void);
68*a466cc55SCy Schubert 
69*a466cc55SCy Schubert static isc_result_t
try_proto(int domain)70*a466cc55SCy Schubert try_proto(int domain) {
71*a466cc55SCy Schubert 	SOCKET s;
72*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
73*a466cc55SCy Schubert 	int errval;
74*a466cc55SCy Schubert 
75*a466cc55SCy Schubert 	s = socket(domain, SOCK_STREAM, IPPROTO_TCP);
76*a466cc55SCy Schubert 	if (s == INVALID_SOCKET) {
77*a466cc55SCy Schubert 		errval = WSAGetLastError();
78*a466cc55SCy Schubert 		switch (errval) {
79*a466cc55SCy Schubert 		case WSAEAFNOSUPPORT:
80*a466cc55SCy Schubert 		case WSAEPROTONOSUPPORT:
81*a466cc55SCy Schubert 		case WSAEINVAL:
82*a466cc55SCy Schubert 			return (ISC_R_NOTFOUND);
83*a466cc55SCy Schubert 		default:
84*a466cc55SCy Schubert 			isc__strerror(errval, strbuf, sizeof(strbuf));
85*a466cc55SCy Schubert 			UNEXPECTED_ERROR(__FILE__, __LINE__,
86*a466cc55SCy Schubert 					 "socket() %s: %s",
87*a466cc55SCy Schubert 					 isc_msgcat_get(isc_msgcat,
88*a466cc55SCy Schubert 							ISC_MSGSET_GENERAL,
89*a466cc55SCy Schubert 							ISC_MSG_FAILED,
90*a466cc55SCy Schubert 							"failed"),
91*a466cc55SCy Schubert 					 strbuf);
92*a466cc55SCy Schubert 			return (ISC_R_UNEXPECTED);
93*a466cc55SCy Schubert 		}
94*a466cc55SCy Schubert 	}
95*a466cc55SCy Schubert 
96*a466cc55SCy Schubert 	closesocket(s);
97*a466cc55SCy Schubert 
98*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
99*a466cc55SCy Schubert }
100*a466cc55SCy Schubert 
101*a466cc55SCy Schubert static void
initialize_action(void)102*a466cc55SCy Schubert initialize_action(void) {
103*a466cc55SCy Schubert 	InitSockets();
104*a466cc55SCy Schubert 	ipv4_result = try_proto(PF_INET);
105*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIPV6
106*a466cc55SCy Schubert #ifdef WANT_IPV6
107*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
108*a466cc55SCy Schubert 	ipv6_result = try_proto(PF_INET6);
109*a466cc55SCy Schubert #endif
110*a466cc55SCy Schubert #endif
111*a466cc55SCy Schubert #endif
112*a466cc55SCy Schubert }
113*a466cc55SCy Schubert 
114*a466cc55SCy Schubert static void
initialize(void)115*a466cc55SCy Schubert initialize(void) {
116*a466cc55SCy Schubert 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
117*a466cc55SCy Schubert }
118*a466cc55SCy Schubert 
119*a466cc55SCy Schubert isc_result_t
isc_net_probeipv4(void)120*a466cc55SCy Schubert isc_net_probeipv4(void) {
121*a466cc55SCy Schubert 	initialize();
122*a466cc55SCy Schubert 	return (ipv4_result);
123*a466cc55SCy Schubert }
124*a466cc55SCy Schubert 
125*a466cc55SCy Schubert isc_result_t
isc_net_probeipv6(void)126*a466cc55SCy Schubert isc_net_probeipv6(void) {
127*a466cc55SCy Schubert 	initialize();
128*a466cc55SCy Schubert 	return (ipv6_result);
129*a466cc55SCy Schubert }
130*a466cc55SCy Schubert 
131*a466cc55SCy Schubert isc_result_t
isc_net_probeunix(void)132*a466cc55SCy Schubert isc_net_probeunix(void) {
133*a466cc55SCy Schubert 	return (ISC_R_NOTFOUND);
134*a466cc55SCy Schubert }
135*a466cc55SCy Schubert 
136*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIPV6
137*a466cc55SCy Schubert #ifdef WANT_IPV6
138*a466cc55SCy Schubert static void
try_ipv6only(void)139*a466cc55SCy Schubert try_ipv6only(void) {
140*a466cc55SCy Schubert #ifdef IPV6_V6ONLY
141*a466cc55SCy Schubert 	SOCKET s;
142*a466cc55SCy Schubert 	int on;
143*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
144*a466cc55SCy Schubert #endif
145*a466cc55SCy Schubert 	isc_result_t result;
146*a466cc55SCy Schubert 
147*a466cc55SCy Schubert 	result = isc_net_probeipv6();
148*a466cc55SCy Schubert 	if (result != ISC_R_SUCCESS) {
149*a466cc55SCy Schubert 		ipv6only_result = result;
150*a466cc55SCy Schubert 		return;
151*a466cc55SCy Schubert 	}
152*a466cc55SCy Schubert 
153*a466cc55SCy Schubert #ifndef IPV6_V6ONLY
154*a466cc55SCy Schubert 	ipv6only_result = ISC_R_NOTFOUND;
155*a466cc55SCy Schubert 	return;
156*a466cc55SCy Schubert #else
157*a466cc55SCy Schubert 	/* check for TCP sockets */
158*a466cc55SCy Schubert 	s = socket(PF_INET6, SOCK_STREAM, 0);
159*a466cc55SCy Schubert 	if (s == INVALID_SOCKET) {
160*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
161*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
162*a466cc55SCy Schubert 				 "socket() %s: %s",
163*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat,
164*a466cc55SCy Schubert 						ISC_MSGSET_GENERAL,
165*a466cc55SCy Schubert 						ISC_MSG_FAILED,
166*a466cc55SCy Schubert 						"failed"),
167*a466cc55SCy Schubert 				 strbuf);
168*a466cc55SCy Schubert 		ipv6only_result = ISC_R_UNEXPECTED;
169*a466cc55SCy Schubert 		return;
170*a466cc55SCy Schubert 	}
171*a466cc55SCy Schubert 
172*a466cc55SCy Schubert 	on = 1;
173*a466cc55SCy Schubert 	if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&on,
174*a466cc55SCy Schubert 		       sizeof(on)) < 0) {
175*a466cc55SCy Schubert 		ipv6only_result = ISC_R_NOTFOUND;
176*a466cc55SCy Schubert 		goto close;
177*a466cc55SCy Schubert 	}
178*a466cc55SCy Schubert 
179*a466cc55SCy Schubert 	closesocket(s);
180*a466cc55SCy Schubert 
181*a466cc55SCy Schubert 	/* check for UDP sockets */
182*a466cc55SCy Schubert 	s = socket(PF_INET6, SOCK_DGRAM, 0);
183*a466cc55SCy Schubert 	if (s == INVALID_SOCKET) {
184*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
185*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
186*a466cc55SCy Schubert 				 "socket() %s: %s",
187*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat,
188*a466cc55SCy Schubert 						ISC_MSGSET_GENERAL,
189*a466cc55SCy Schubert 						ISC_MSG_FAILED,
190*a466cc55SCy Schubert 						"failed"),
191*a466cc55SCy Schubert 				 strbuf);
192*a466cc55SCy Schubert 		ipv6only_result = ISC_R_UNEXPECTED;
193*a466cc55SCy Schubert 		return;
194*a466cc55SCy Schubert 	}
195*a466cc55SCy Schubert 
196*a466cc55SCy Schubert 	on = 1;
197*a466cc55SCy Schubert 	if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&on,
198*a466cc55SCy Schubert 		       sizeof(on)) < 0) {
199*a466cc55SCy Schubert 		ipv6only_result = ISC_R_NOTFOUND;
200*a466cc55SCy Schubert 		goto close;
201*a466cc55SCy Schubert 	}
202*a466cc55SCy Schubert 
203*a466cc55SCy Schubert 	ipv6only_result = ISC_R_SUCCESS;
204*a466cc55SCy Schubert 
205*a466cc55SCy Schubert close:
206*a466cc55SCy Schubert 	closesocket(s);
207*a466cc55SCy Schubert 	return;
208*a466cc55SCy Schubert #endif /* IPV6_V6ONLY */
209*a466cc55SCy Schubert }
210*a466cc55SCy Schubert 
211*a466cc55SCy Schubert static void
initialize_ipv6only(void)212*a466cc55SCy Schubert initialize_ipv6only(void) {
213*a466cc55SCy Schubert 	RUNTIME_CHECK(isc_once_do(&once_ipv6only,
214*a466cc55SCy Schubert 				  try_ipv6only) == ISC_R_SUCCESS);
215*a466cc55SCy Schubert }
216*a466cc55SCy Schubert 
217*a466cc55SCy Schubert static void
try_ipv6pktinfo(void)218*a466cc55SCy Schubert try_ipv6pktinfo(void) {
219*a466cc55SCy Schubert 	SOCKET s;
220*a466cc55SCy Schubert 	int on;
221*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
222*a466cc55SCy Schubert 	isc_result_t result;
223*a466cc55SCy Schubert 	int optname;
224*a466cc55SCy Schubert 
225*a466cc55SCy Schubert 	result = isc_net_probeipv6();
226*a466cc55SCy Schubert 	if (result != ISC_R_SUCCESS) {
227*a466cc55SCy Schubert 		ipv6pktinfo_result = result;
228*a466cc55SCy Schubert 		return;
229*a466cc55SCy Schubert 	}
230*a466cc55SCy Schubert 
231*a466cc55SCy Schubert 	/* we only use this for UDP sockets */
232*a466cc55SCy Schubert 	s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
233*a466cc55SCy Schubert 	if (s == INVALID_SOCKET) {
234*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
235*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
236*a466cc55SCy Schubert 				 "socket() %s: %s",
237*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat,
238*a466cc55SCy Schubert 						ISC_MSGSET_GENERAL,
239*a466cc55SCy Schubert 						ISC_MSG_FAILED,
240*a466cc55SCy Schubert 						"failed"),
241*a466cc55SCy Schubert 				 strbuf);
242*a466cc55SCy Schubert 		ipv6pktinfo_result = ISC_R_UNEXPECTED;
243*a466cc55SCy Schubert 		return;
244*a466cc55SCy Schubert 	}
245*a466cc55SCy Schubert 
246*a466cc55SCy Schubert #ifdef IPV6_RECVPKTINFO
247*a466cc55SCy Schubert 	optname = IPV6_RECVPKTINFO;
248*a466cc55SCy Schubert #else
249*a466cc55SCy Schubert 	optname = IPV6_PKTINFO;
250*a466cc55SCy Schubert #endif
251*a466cc55SCy Schubert 	on = 1;
252*a466cc55SCy Schubert 	if (setsockopt(s, IPPROTO_IPV6, optname, (const char *) &on,
253*a466cc55SCy Schubert 		       sizeof(on)) < 0) {
254*a466cc55SCy Schubert 		ipv6pktinfo_result = ISC_R_NOTFOUND;
255*a466cc55SCy Schubert 		goto close;
256*a466cc55SCy Schubert 	}
257*a466cc55SCy Schubert 
258*a466cc55SCy Schubert 	ipv6pktinfo_result = ISC_R_SUCCESS;
259*a466cc55SCy Schubert 
260*a466cc55SCy Schubert close:
261*a466cc55SCy Schubert 	closesocket(s);
262*a466cc55SCy Schubert 	return;
263*a466cc55SCy Schubert }
264*a466cc55SCy Schubert 
265*a466cc55SCy Schubert static void
initialize_ipv6pktinfo(void)266*a466cc55SCy Schubert initialize_ipv6pktinfo(void) {
267*a466cc55SCy Schubert 	RUNTIME_CHECK(isc_once_do(&once_ipv6pktinfo,
268*a466cc55SCy Schubert 				  try_ipv6pktinfo) == ISC_R_SUCCESS);
269*a466cc55SCy Schubert }
270*a466cc55SCy Schubert #endif /* WANT_IPV6 */
271*a466cc55SCy Schubert #endif /* ISC_PLATFORM_HAVEIPV6 */
272*a466cc55SCy Schubert 
273*a466cc55SCy Schubert isc_result_t
isc_net_probe_ipv6only(void)274*a466cc55SCy Schubert isc_net_probe_ipv6only(void) {
275*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIPV6
276*a466cc55SCy Schubert #ifdef WANT_IPV6
277*a466cc55SCy Schubert 	initialize_ipv6only();
278*a466cc55SCy Schubert #else
279*a466cc55SCy Schubert 	ipv6only_result = ISC_R_NOTFOUND;
280*a466cc55SCy Schubert #endif
281*a466cc55SCy Schubert #endif
282*a466cc55SCy Schubert 	return (ipv6only_result);
283*a466cc55SCy Schubert }
284*a466cc55SCy Schubert 
285*a466cc55SCy Schubert isc_result_t
isc_net_probe_ipv6pktinfo(void)286*a466cc55SCy Schubert isc_net_probe_ipv6pktinfo(void) {
287*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIPV6
288*a466cc55SCy Schubert #ifdef WANT_IPV6
289*a466cc55SCy Schubert 	initialize_ipv6pktinfo();
290*a466cc55SCy Schubert #else
291*a466cc55SCy Schubert 	ipv6pktinfo_result = ISC_R_NOTFOUND;
292*a466cc55SCy Schubert #endif
293*a466cc55SCy Schubert #endif
294*a466cc55SCy Schubert 	return (ipv6pktinfo_result);
295*a466cc55SCy Schubert }
296*a466cc55SCy Schubert 
297*a466cc55SCy Schubert isc_result_t
isc_net_getudpportrange(int af,in_port_t * low,in_port_t * high)298*a466cc55SCy Schubert isc_net_getudpportrange(int af, in_port_t *low, in_port_t *high) {
299*a466cc55SCy Schubert 	int result = ISC_R_FAILURE;
300*a466cc55SCy Schubert 
301*a466cc55SCy Schubert 	REQUIRE(low != NULL && high != NULL);
302*a466cc55SCy Schubert 
303*a466cc55SCy Schubert 	UNUSED(af);
304*a466cc55SCy Schubert 
305*a466cc55SCy Schubert 	if (result != ISC_R_SUCCESS) {
306*a466cc55SCy Schubert 		*low = ISC_NET_PORTRANGELOW;
307*a466cc55SCy Schubert 		*high = ISC_NET_PORTRANGEHIGH;
308*a466cc55SCy Schubert 	}
309*a466cc55SCy Schubert 
310*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);	/* we currently never fail in this function */
311*a466cc55SCy Schubert }
312*a466cc55SCy Schubert 
313*a466cc55SCy Schubert void
isc_net_disableipv4(void)314*a466cc55SCy Schubert isc_net_disableipv4(void) {
315*a466cc55SCy Schubert 	initialize();
316*a466cc55SCy Schubert 	if (ipv4_result == ISC_R_SUCCESS)
317*a466cc55SCy Schubert 		ipv4_result = ISC_R_DISABLED;
318*a466cc55SCy Schubert }
319*a466cc55SCy Schubert 
320*a466cc55SCy Schubert void
isc_net_disableipv6(void)321*a466cc55SCy Schubert isc_net_disableipv6(void) {
322*a466cc55SCy Schubert 	initialize();
323*a466cc55SCy Schubert 	if (ipv6_result == ISC_R_SUCCESS)
324*a466cc55SCy Schubert 		ipv6_result = ISC_R_DISABLED;
325*a466cc55SCy Schubert }
326*a466cc55SCy Schubert 
327*a466cc55SCy Schubert void
isc_net_enableipv4(void)328*a466cc55SCy Schubert isc_net_enableipv4(void) {
329*a466cc55SCy Schubert 	initialize();
330*a466cc55SCy Schubert 	if (ipv4_result == ISC_R_DISABLED)
331*a466cc55SCy Schubert 		ipv4_result = ISC_R_SUCCESS;
332*a466cc55SCy Schubert }
333*a466cc55SCy Schubert 
334*a466cc55SCy Schubert void
isc_net_enableipv6(void)335*a466cc55SCy Schubert isc_net_enableipv6(void) {
336*a466cc55SCy Schubert 	initialize();
337*a466cc55SCy Schubert 	if (ipv6_result == ISC_R_DISABLED)
338*a466cc55SCy Schubert 		ipv6_result = ISC_R_SUCCESS;
339*a466cc55SCy Schubert }
340