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