xref: /netbsd-src/sys/netinet6/udp6_usrreq.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: udp6_usrreq.c,v 1.80 2007/11/14 22:58:27 dyoung Exp $	*/
2 /*	$KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1989, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.80 2007/11/14 22:58:27 dyoung Exp $");
66 
67 #include <sys/param.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/errno.h>
74 #include <sys/stat.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/syslog.h>
78 #include <sys/sysctl.h>
79 
80 #include <net/if.h>
81 #include <net/route.h>
82 #include <net/if_types.h>
83 
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/udp.h>
91 #include <netinet/udp_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet/icmp6.h>
96 #include <netinet6/udp6_var.h>
97 #include <netinet6/ip6protosw.h>
98 #include <netinet/in_offload.h>
99 
100 #include "faith.h"
101 #if defined(NFAITH) && NFAITH > 0
102 #include <net/if_faith.h>
103 #endif
104 
105 /*
106  * UDP protocol implementation.
107  * Per RFC 768, August, 1980.
108  */
109 
110 extern struct inpcbtable udbtable;
111 struct	udp6stat udp6stat;
112 
113 static	void udp6_notify(struct in6pcb *, int);
114 
115 void
116 udp6_init()
117 {
118 	/* initialization done in udp_input() due to initialization order */
119 }
120 
121 /*
122  * Notify a udp user of an asynchronous error;
123  * just wake up so that he can collect error status.
124  */
125 static	void
126 udp6_notify(struct in6pcb *in6p, int errno)
127 {
128 	in6p->in6p_socket->so_error = errno;
129 	sorwakeup(in6p->in6p_socket);
130 	sowwakeup(in6p->in6p_socket);
131 }
132 
133 void
134 udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
135 {
136 	struct udphdr uh;
137 	struct ip6_hdr *ip6;
138 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
139 	struct mbuf *m;
140 	int off;
141 	void *cmdarg;
142 	struct ip6ctlparam *ip6cp = NULL;
143 	const struct sockaddr_in6 *sa6_src = NULL;
144 	void (*notify)(struct in6pcb *, int) = udp6_notify;
145 	struct udp_portonly {
146 		u_int16_t uh_sport;
147 		u_int16_t uh_dport;
148 	} *uhp;
149 
150 	if (sa->sa_family != AF_INET6 ||
151 	    sa->sa_len != sizeof(struct sockaddr_in6))
152 		return;
153 
154 	if ((unsigned)cmd >= PRC_NCMDS)
155 		return;
156 	if (PRC_IS_REDIRECT(cmd))
157 		notify = in6_rtchange, d = NULL;
158 	else if (cmd == PRC_HOSTDEAD)
159 		d = NULL;
160 	else if (cmd == PRC_MSGSIZE) {
161 		/* special code is present, see below */
162 		notify = in6_rtchange;
163 	}
164 	else if (inet6ctlerrmap[cmd] == 0)
165 		return;
166 
167 	/* if the parameter is from icmp6, decode it. */
168 	if (d != NULL) {
169 		ip6cp = (struct ip6ctlparam *)d;
170 		m = ip6cp->ip6c_m;
171 		ip6 = ip6cp->ip6c_ip6;
172 		off = ip6cp->ip6c_off;
173 		cmdarg = ip6cp->ip6c_cmdarg;
174 		sa6_src = ip6cp->ip6c_src;
175 	} else {
176 		m = NULL;
177 		ip6 = NULL;
178 		cmdarg = NULL;
179 		sa6_src = &sa6_any;
180 		off = 0;
181 	}
182 
183 	if (ip6) {
184 		/*
185 		 * XXX: We assume that when IPV6 is non NULL,
186 		 * M and OFF are valid.
187 		 */
188 
189 		/* check if we can safely examine src and dst ports */
190 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
191 			if (cmd == PRC_MSGSIZE)
192 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
193 			return;
194 		}
195 
196 		bzero(&uh, sizeof(uh));
197 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
198 
199 		if (cmd == PRC_MSGSIZE) {
200 			int valid = 0;
201 
202 			/*
203 			 * Check to see if we have a valid UDP socket
204 			 * corresponding to the address in the ICMPv6 message
205 			 * payload.
206 			 */
207 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
208 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
209 			    uh.uh_sport, 0))
210 				valid++;
211 #if 0
212 			/*
213 			 * As the use of sendto(2) is fairly popular,
214 			 * we may want to allow non-connected pcb too.
215 			 * But it could be too weak against attacks...
216 			 * We should at least check if the local address (= s)
217 			 * is really ours.
218 			 */
219 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
220 			    uh.uh_dport, 0))
221 				valid++;
222 #endif
223 
224 			/*
225 			 * Depending on the value of "valid" and routing table
226 			 * size (mtudisc_{hi,lo}wat), we will:
227 			 * - recalculate the new MTU and create the
228 			 *   corresponding routing entry, or
229 			 * - ignore the MTU change notification.
230 			 */
231 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
232 
233 			/*
234 			 * regardless of if we called
235 			 * icmp6_mtudisc_update(), we need to call
236 			 * in6_pcbnotify(), to notify path MTU change
237 			 * to the userland (RFC3542), because some
238 			 * unconnected sockets may share the same
239 			 * destination and want to know the path MTU.
240 			 */
241 		}
242 
243 		(void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
244 		    (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
245 		    notify);
246 	} else {
247 		(void) in6_pcbnotify(&udbtable, sa, 0,
248 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
249 	}
250 }
251 
252 extern	int udp6_sendspace;
253 extern	int udp6_recvspace;
254 
255 int
256 udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
257     struct mbuf *control, struct lwp *l)
258 {
259 	struct	in6pcb *in6p = sotoin6pcb(so);
260 	int	error = 0;
261 	int	s;
262 
263 	/*
264 	 * MAPPED_ADDR implementation info:
265 	 *  Mapped addr support for PRU_CONTROL is not necessary.
266 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
267 	 *  and they don't associate any addr to their socket.  Then
268 	 *  socket family is only hint about the PRU_CONTROL'ed address
269 	 *  family, especially when getting addrs from kernel.
270 	 *  So AF_INET socket need to be used to control AF_INET addrs,
271 	 *  and AF_INET6 socket for AF_INET6 addrs.
272 	 */
273 	if (req == PRU_CONTROL)
274 		return in6_control(so, (u_long)m, (void *)addr6,
275 				   (struct ifnet *)control, l);
276 
277 	if (req == PRU_PURGEIF) {
278 		s = splsoftnet();
279 		in6_pcbpurgeif0(&udbtable, (struct ifnet *)control);
280 		in6_purgeif((struct ifnet *)control);
281 		in6_pcbpurgeif(&udbtable, (struct ifnet *)control);
282 		splx(s);
283 		return 0;
284 	}
285 
286 	if (in6p == NULL && req != PRU_ATTACH) {
287 		error = EINVAL;
288 		goto release;
289 	}
290 
291 	switch (req) {
292 	case PRU_ATTACH:
293 		/*
294 		 * MAPPED_ADDR implementation spec:
295 		 *  Always attach for IPv6,
296 		 *  and only when necessary for IPv4.
297 		 */
298 		if (in6p != NULL) {
299 			error = EINVAL;
300 			break;
301 		}
302 		s = splsoftnet();
303 		error = in6_pcballoc(so, &udbtable);
304 		splx(s);
305 		if (error)
306 			break;
307 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
308 		if (error)
309 			break;
310 		in6p = sotoin6pcb(so);
311 		in6p->in6p_cksum = -1;	/* just to be sure */
312 		break;
313 
314 	case PRU_DETACH:
315 		in6_pcbdetach(in6p);
316 		break;
317 
318 	case PRU_BIND:
319 		s = splsoftnet();
320 		error = in6_pcbbind(in6p, addr6, l);
321 		splx(s);
322 		break;
323 
324 	case PRU_CONNECT:
325 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
326 			error = EISCONN;
327 			break;
328 		}
329 		s = splsoftnet();
330 		error = in6_pcbconnect(in6p, addr6, l);
331 		splx(s);
332 		if (error == 0)
333 			soisconnected(so);
334 		break;
335 
336 	case PRU_DISCONNECT:
337 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
338 			error = ENOTCONN;
339 			break;
340 		}
341 		s = splsoftnet();
342 		in6_pcbdisconnect(in6p);
343 		bzero((void *)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
344 		splx(s);
345 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
346 		in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
347 		break;
348 
349 	case PRU_SHUTDOWN:
350 		socantsendmore(so);
351 		break;
352 
353 	case PRU_SEND:
354 		s = splsoftnet();
355 		error = udp6_output(in6p, m, addr6, control, l);
356 		splx(s);
357 		return error;
358 
359 	case PRU_ABORT:
360 		soisdisconnected(so);
361 		in6_pcbdetach(in6p);
362 		break;
363 
364 	case PRU_SOCKADDR:
365 		in6_setsockaddr(in6p, addr6);
366 		break;
367 
368 	case PRU_PEERADDR:
369 		in6_setpeeraddr(in6p, addr6);
370 		break;
371 
372 	case PRU_SENSE:
373 		/*
374 		 * stat: don't bother with a blocksize
375 		 */
376 		return 0;
377 
378 	case PRU_LISTEN:
379 	case PRU_CONNECT2:
380 	case PRU_ACCEPT:
381 	case PRU_SENDOOB:
382 	case PRU_FASTTIMO:
383 	case PRU_SLOWTIMO:
384 	case PRU_PROTORCV:
385 	case PRU_PROTOSEND:
386 		error = EOPNOTSUPP;
387 		break;
388 
389 	case PRU_RCVD:
390 	case PRU_RCVOOB:
391 		return EOPNOTSUPP;	/* do not free mbuf's */
392 
393 	default:
394 		panic("udp6_usrreq");
395 	}
396 
397 release:
398 	if (control != NULL)
399 		m_freem(control);
400 	if (m != NULL)
401 		m_freem(m);
402 	return error;
403 }
404 
405 SYSCTL_SETUP(sysctl_net_inet6_udp6_setup, "sysctl net.inet6.udp6 subtree setup")
406 {
407 	sysctl_createv(clog, 0, NULL, NULL,
408 		       CTLFLAG_PERMANENT,
409 		       CTLTYPE_NODE, "net", NULL,
410 		       NULL, 0, NULL, 0,
411 		       CTL_NET, CTL_EOL);
412 	sysctl_createv(clog, 0, NULL, NULL,
413 		       CTLFLAG_PERMANENT,
414 		       CTLTYPE_NODE, "inet6", NULL,
415 		       NULL, 0, NULL, 0,
416 		       CTL_NET, PF_INET6, CTL_EOL);
417 	sysctl_createv(clog, 0, NULL, NULL,
418 		       CTLFLAG_PERMANENT,
419 		       CTLTYPE_NODE, "udp6",
420 		       SYSCTL_DESCR("UDPv6 related settings"),
421 		       NULL, 0, NULL, 0,
422 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
423 
424 	sysctl_createv(clog, 0, NULL, NULL,
425 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
426 		       CTLTYPE_INT, "sendspace",
427 		       SYSCTL_DESCR("Default UDP send buffer size"),
428 		       NULL, 0, &udp6_sendspace, 0,
429 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
430 		       CTL_EOL);
431 	sysctl_createv(clog, 0, NULL, NULL,
432 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
433 		       CTLTYPE_INT, "recvspace",
434 		       SYSCTL_DESCR("Default UDP receive buffer size"),
435 		       NULL, 0, &udp6_recvspace, 0,
436 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
437 		       CTL_EOL);
438 	sysctl_createv(clog, 0, NULL, NULL,
439 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
440 		       CTLTYPE_INT, "do_loopback_cksum",
441 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
442 		       NULL, 0, &udp_do_loopback_cksum, 0,
443 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
444 		       CTL_EOL);
445 	sysctl_createv(clog, 0, NULL, NULL,
446 		       CTLFLAG_PERMANENT,
447 		       CTLTYPE_STRUCT, "pcblist",
448 		       SYSCTL_DESCR("UDP protocol control block list"),
449 		       sysctl_inpcblist, 0, &udbtable, 0,
450 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
451 		       CTL_EOL);
452 	sysctl_createv(clog, 0, NULL, NULL,
453 		       CTLFLAG_PERMANENT,
454 		       CTLTYPE_STRUCT, "stats",
455 		       SYSCTL_DESCR("UDPv6 statistics"),
456 		       NULL, 0, &udp6stat, sizeof(udp6stat),
457 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
458 		       CTL_EOL);
459 }
460