xref: /netbsd-src/sys/netinet6/udp6_usrreq.c (revision 6cd39ddb8550f6fa1bff3fed32053d7f19fd0453)
1 /*	$NetBSD: udp6_usrreq.c,v 1.121 2015/08/24 22:21:27 pooka 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.121 2015/08/24 22:21:27 pooka Exp $");
66 
67 #ifdef _KERNEL_OPT
68 #include "opt_inet.h"
69 #include "opt_inet_csum.h"
70 #include "opt_ipsec.h"
71 #endif
72 
73 #include <sys/param.h>
74 #include <sys/mbuf.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/syslog.h>
81 #include <sys/domain.h>
82 #include <sys/sysctl.h>
83 
84 #include <net/if.h>
85 #include <net/route.h>
86 #include <net/if_types.h>
87 
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_offload.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_var.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/udp.h>
96 #include <netinet/udp_var.h>
97 #include <netinet/udp_private.h>
98 
99 #include <netinet/ip6.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/ip6_private.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/udp6_var.h>
105 #include <netinet6/udp6_private.h>
106 #include <netinet6/ip6protosw.h>
107 #include <netinet6/scope6_var.h>
108 
109 #ifdef IPSEC
110 #include <netipsec/ipsec.h>
111 #include <netipsec/ipsec_var.h>
112 #include <netipsec/ipsec_private.h>
113 #ifdef INET6
114 #include <netipsec/ipsec6.h>
115 #endif
116 #endif	/* IPSEC */
117 
118 #include "faith.h"
119 #if defined(NFAITH) && NFAITH > 0
120 #include <net/if_faith.h>
121 #endif
122 
123 /*
124  * UDP protocol implementation.
125  * Per RFC 768, August, 1980.
126  */
127 
128 extern struct inpcbtable udbtable;
129 
130 percpu_t *udp6stat_percpu;
131 
132 /* UDP on IP6 parameters */
133 static int	udp6_sendspace = 9216;	/* really max datagram size */
134 static int	udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
135 					/* 40 1K datagrams */
136 
137 static	void udp6_notify(struct in6pcb *, int);
138 static	void sysctl_net_inet6_udp6_setup(struct sysctllog **);
139 
140 #ifdef UDP_CSUM_COUNTERS
141 #include <sys/device.h>
142 struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
143     NULL, "udp6", "hwcsum bad");
144 struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
145     NULL, "udp6", "hwcsum ok");
146 struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
147     NULL, "udp6", "hwcsum data");
148 struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
149     NULL, "udp6", "swcsum");
150 
151 EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
152 EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
153 EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
154 EVCNT_ATTACH_STATIC(udp6_swcsum);
155 
156 #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
157 #else
158 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
159 #endif
160 
161 void
162 udp6_init(void)
163 {
164 	sysctl_net_inet6_udp6_setup(NULL);
165 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
166 
167 	udp_init_common();
168 }
169 
170 /*
171  * Notify a udp user of an asynchronous error;
172  * just wake up so that he can collect error status.
173  */
174 static	void
175 udp6_notify(struct in6pcb *in6p, int errno)
176 {
177 	in6p->in6p_socket->so_error = errno;
178 	sorwakeup(in6p->in6p_socket);
179 	sowwakeup(in6p->in6p_socket);
180 }
181 
182 void *
183 udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
184 {
185 	struct udphdr uh;
186 	struct ip6_hdr *ip6;
187 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
188 	struct mbuf *m;
189 	int off;
190 	void *cmdarg;
191 	struct ip6ctlparam *ip6cp = NULL;
192 	const struct sockaddr_in6 *sa6_src = NULL;
193 	void (*notify)(struct in6pcb *, int) = udp6_notify;
194 	struct udp_portonly {
195 		u_int16_t uh_sport;
196 		u_int16_t uh_dport;
197 	} *uhp;
198 
199 	if (sa->sa_family != AF_INET6 ||
200 	    sa->sa_len != sizeof(struct sockaddr_in6))
201 		return NULL;
202 
203 	if ((unsigned)cmd >= PRC_NCMDS)
204 		return NULL;
205 	if (PRC_IS_REDIRECT(cmd))
206 		notify = in6_rtchange, d = NULL;
207 	else if (cmd == PRC_HOSTDEAD)
208 		d = NULL;
209 	else if (cmd == PRC_MSGSIZE) {
210 		/* special code is present, see below */
211 		notify = in6_rtchange;
212 	}
213 	else if (inet6ctlerrmap[cmd] == 0)
214 		return NULL;
215 
216 	/* if the parameter is from icmp6, decode it. */
217 	if (d != NULL) {
218 		ip6cp = (struct ip6ctlparam *)d;
219 		m = ip6cp->ip6c_m;
220 		ip6 = ip6cp->ip6c_ip6;
221 		off = ip6cp->ip6c_off;
222 		cmdarg = ip6cp->ip6c_cmdarg;
223 		sa6_src = ip6cp->ip6c_src;
224 	} else {
225 		m = NULL;
226 		ip6 = NULL;
227 		cmdarg = NULL;
228 		sa6_src = &sa6_any;
229 		off = 0;
230 	}
231 
232 	if (ip6) {
233 		/*
234 		 * XXX: We assume that when IPV6 is non NULL,
235 		 * M and OFF are valid.
236 		 */
237 
238 		/* check if we can safely examine src and dst ports */
239 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
240 			if (cmd == PRC_MSGSIZE)
241 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
242 			return NULL;
243 		}
244 
245 		memset(&uh, 0, sizeof(uh));
246 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
247 
248 		if (cmd == PRC_MSGSIZE) {
249 			int valid = 0;
250 
251 			/*
252 			 * Check to see if we have a valid UDP socket
253 			 * corresponding to the address in the ICMPv6 message
254 			 * payload.
255 			 */
256 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
257 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
258 						  uh.uh_sport, 0, 0))
259 				valid++;
260 #if 0
261 			/*
262 			 * As the use of sendto(2) is fairly popular,
263 			 * we may want to allow non-connected pcb too.
264 			 * But it could be too weak against attacks...
265 			 * We should at least check if the local address (= s)
266 			 * is really ours.
267 			 */
268 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
269 			    uh.uh_dport, 0))
270 				valid++;
271 #endif
272 
273 			/*
274 			 * Depending on the value of "valid" and routing table
275 			 * size (mtudisc_{hi,lo}wat), we will:
276 			 * - recalculate the new MTU and create the
277 			 *   corresponding routing entry, or
278 			 * - ignore the MTU change notification.
279 			 */
280 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
281 
282 			/*
283 			 * regardless of if we called
284 			 * icmp6_mtudisc_update(), we need to call
285 			 * in6_pcbnotify(), to notify path MTU change
286 			 * to the userland (RFC3542), because some
287 			 * unconnected sockets may share the same
288 			 * destination and want to know the path MTU.
289 			 */
290 		}
291 
292 		(void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
293 		    (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
294 		    notify);
295 	} else {
296 		(void) in6_pcbnotify(&udbtable, sa, 0,
297 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
298 	}
299 	return NULL;
300 }
301 
302 int
303 udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
304 {
305 	int s;
306 	int error = 0;
307 	int family;
308 
309 	family = so->so_proto->pr_domain->dom_family;
310 
311 	s = splsoftnet();
312 	switch (family) {
313 #ifdef INET
314 	case PF_INET:
315 		if (sopt->sopt_level != IPPROTO_UDP) {
316 			error = ip_ctloutput(op, so, sopt);
317 			goto end;
318 		}
319 		break;
320 #endif
321 #ifdef INET6
322 	case PF_INET6:
323 		if (sopt->sopt_level != IPPROTO_UDP) {
324 			error = ip6_ctloutput(op, so, sopt);
325 			goto end;
326 		}
327 		break;
328 #endif
329 	default:
330 		error = EAFNOSUPPORT;
331 		goto end;
332 	}
333 	error = EINVAL;
334 
335 end:
336 	splx(s);
337 	return error;
338 }
339 
340 static void
341 udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
342 	struct sockaddr *src, struct socket *so)
343 {
344 	struct mbuf *opts = NULL;
345 	struct mbuf *n;
346 	struct in6pcb *in6p = NULL;
347 
348 	if (!so)
349 		return;
350 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
351 		return;
352 	in6p = sotoin6pcb(so);
353 
354 #if defined(IPSEC)
355 	/* check AH/ESP integrity. */
356 	if (ipsec_used && so != NULL && ipsec6_in_reject_so(m, so)) {
357 		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
358 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
359 			icmp6_error(n, ICMP6_DST_UNREACH,
360 			    ICMP6_DST_UNREACH_ADMIN, 0);
361 		return;
362 	}
363 #endif /*IPSEC*/
364 
365 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
366 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
367 #ifdef SO_OTIMESTAMP
368 		    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
369 #endif
370 		    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
371 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
372 			ip6_savecontrol(in6p, &opts, ip6, n);
373 		}
374 
375 		m_adj(n, off);
376 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
377 			m_freem(n);
378 			if (opts)
379 				m_freem(opts);
380 			so->so_rcv.sb_overflowed++;
381 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
382 		} else
383 			sorwakeup(so);
384 	}
385 }
386 
387 int
388 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
389 	struct mbuf *m, int off)
390 {
391 	u_int16_t sport, dport;
392 	int rcvcnt;
393 	struct in6_addr src6, *dst6;
394 	const struct in_addr *dst4;
395 	struct inpcb_hdr *inph;
396 	struct in6pcb *in6p;
397 
398 	rcvcnt = 0;
399 	off += sizeof(struct udphdr);	/* now, offset of payload */
400 
401 	if (af != AF_INET && af != AF_INET6)
402 		goto bad;
403 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
404 		goto bad;
405 
406 	src6 = src->sin6_addr;
407 	if (sa6_recoverscope(src) != 0) {
408 		/* XXX: should be impossible. */
409 		goto bad;
410 	}
411 	sport = src->sin6_port;
412 
413 	dport = dst->sin6_port;
414 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
415 	dst6 = &dst->sin6_addr;
416 
417 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
418 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
419 		/*
420 		 * Deliver a multicast or broadcast datagram to *all* sockets
421 		 * for which the local and remote addresses and ports match
422 		 * those of the incoming datagram.  This allows more than
423 		 * one process to receive multi/broadcasts on the same port.
424 		 * (This really ought to be done for unicast datagrams as
425 		 * well, but that would cause problems with existing
426 		 * applications that open both address-specific sockets and
427 		 * a wildcard socket listening to the same port -- they would
428 		 * end up receiving duplicates of every unicast datagram.
429 		 * Those applications open the multiple sockets to overcome an
430 		 * inadequacy of the UDP socket interface, but for backwards
431 		 * compatibility we avoid the problem here rather than
432 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
433 		 */
434 
435 		/*
436 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
437 		 * we need udpiphdr for IPsec processing so we do that later.
438 		 */
439 		/*
440 		 * Locate pcb(s) for datagram.
441 		 */
442 		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
443 			in6p = (struct in6pcb *)inph;
444 			if (in6p->in6p_af != AF_INET6)
445 				continue;
446 
447 			if (in6p->in6p_lport != dport)
448 				continue;
449 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
450 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
451 				    dst6))
452 					continue;
453 			} else {
454 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
455 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
456 					continue;
457 			}
458 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
459 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
460 				    &src6) || in6p->in6p_fport != sport)
461 					continue;
462 			} else {
463 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
464 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
465 					continue;
466 			}
467 
468 			udp6_sendup(m, off, (struct sockaddr *)src,
469 				in6p->in6p_socket);
470 			rcvcnt++;
471 
472 			/*
473 			 * Don't look for additional matches if this one does
474 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
475 			 * socket options set.  This heuristic avoids searching
476 			 * through all pcbs in the common case of a non-shared
477 			 * port.  It assumes that an application will never
478 			 * clear these options after setting them.
479 			 */
480 			if ((in6p->in6p_socket->so_options &
481 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
482 				break;
483 		}
484 	} else {
485 		/*
486 		 * Locate pcb for datagram.
487 		 */
488 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
489 					     dport, 0, 0);
490 		if (in6p == 0) {
491 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
492 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
493 			if (in6p == 0)
494 				return rcvcnt;
495 		}
496 
497 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
498 		rcvcnt++;
499 	}
500 
501 bad:
502 	return rcvcnt;
503 }
504 
505 int
506 udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
507 {
508 
509 	/*
510 	 * XXX it's better to record and check if this mbuf is
511 	 * already checked.
512 	 */
513 
514 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
515 		goto good;
516 	}
517 	if (uh->uh_sum == 0) {
518 		UDP6_STATINC(UDP6_STAT_NOSUM);
519 		goto bad;
520 	}
521 
522 	switch (m->m_pkthdr.csum_flags &
523 	    ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
524 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
525 	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
526 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
527 		UDP6_STATINC(UDP6_STAT_BADSUM);
528 		goto bad;
529 
530 #if 0 /* notyet */
531 	case M_CSUM_UDPv6|M_CSUM_DATA:
532 #endif
533 
534 	case M_CSUM_UDPv6:
535 		/* Checksum was okay. */
536 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
537 		break;
538 
539 	default:
540 		/*
541 		 * Need to compute it ourselves.  Maybe skip checksum
542 		 * on loopback interfaces.
543 		 */
544 		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
545 		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
546 			UDP6_STATINC(UDP6_STAT_BADSUM);
547 			goto bad;
548 		}
549 	}
550 
551 good:
552 	return 0;
553 bad:
554 	return -1;
555 }
556 
557 int
558 udp6_input(struct mbuf **mp, int *offp, int proto)
559 {
560 	struct mbuf *m = *mp;
561 	int off = *offp;
562 	struct sockaddr_in6 src, dst;
563 	struct ip6_hdr *ip6;
564 	struct udphdr *uh;
565 	u_int32_t plen, ulen;
566 
567 	ip6 = mtod(m, struct ip6_hdr *);
568 
569 #if defined(NFAITH) && 0 < NFAITH
570 	if (faithprefix(&ip6->ip6_dst)) {
571 		/* send icmp6 host unreach? */
572 		m_freem(m);
573 		return IPPROTO_DONE;
574 	}
575 #endif
576 
577 	UDP6_STATINC(UDP6_STAT_IPACKETS);
578 
579 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
580 	plen = m->m_pkthdr.len - off;
581 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
582 	if (uh == NULL) {
583 		IP6_STATINC(IP6_STAT_TOOSHORT);
584 		return IPPROTO_DONE;
585 	}
586 	KASSERT(UDP_HDR_ALIGNED_P(uh));
587 	ulen = ntohs((u_short)uh->uh_ulen);
588 	/*
589 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
590 	 * iff payload length > 0xffff.
591 	 */
592 	if (ulen == 0 && plen > 0xffff)
593 		ulen = plen;
594 
595 	if (plen != ulen) {
596 		UDP6_STATINC(UDP6_STAT_BADLEN);
597 		goto bad;
598 	}
599 
600 	/* destination port of 0 is illegal, based on RFC768. */
601 	if (uh->uh_dport == 0)
602 		goto bad;
603 
604 	/* Be proactive about malicious use of IPv4 mapped address */
605 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
606 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
607 		/* XXX stat */
608 		goto bad;
609 	}
610 
611 	/*
612 	 * Checksum extended UDP header and data.  Maybe skip checksum
613 	 * on loopback interfaces.
614 	 */
615 	if (udp6_input_checksum(m, uh, off, ulen))
616 		goto bad;
617 
618 	/*
619 	 * Construct source and dst sockaddrs.
620 	 */
621 	memset(&src, 0, sizeof(src));
622 	src.sin6_family = AF_INET6;
623 	src.sin6_len = sizeof(struct sockaddr_in6);
624 	src.sin6_addr = ip6->ip6_src;
625 	src.sin6_port = uh->uh_sport;
626 	memset(&dst, 0, sizeof(dst));
627 	dst.sin6_family = AF_INET6;
628 	dst.sin6_len = sizeof(struct sockaddr_in6);
629 	dst.sin6_addr = ip6->ip6_dst;
630 	dst.sin6_port = uh->uh_dport;
631 
632 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
633 		if (m->m_flags & M_MCAST) {
634 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
635 			goto bad;
636 		}
637 		UDP6_STATINC(UDP6_STAT_NOPORT);
638 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
639 		m = NULL;
640 	}
641 
642 bad:
643 	if (m)
644 		m_freem(m);
645 	return IPPROTO_DONE;
646 }
647 
648 static int
649 udp6_attach(struct socket *so, int proto)
650 {
651 	struct in6pcb *in6p;
652 	int s, error;
653 
654 	KASSERT(sotoin6pcb(so) == NULL);
655 	sosetlock(so);
656 
657 	/*
658 	 * MAPPED_ADDR implementation spec:
659 	 *  Always attach for IPv6, and only when necessary for IPv4.
660 	 */
661 	s = splsoftnet();
662 	error = in6_pcballoc(so, &udbtable);
663 	splx(s);
664 	if (error) {
665 		return error;
666 	}
667 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
668 	if (error) {
669 		return error;
670 	}
671 	in6p = sotoin6pcb(so);
672 	in6p->in6p_cksum = -1;	/* just to be sure */
673 
674 	KASSERT(solocked(so));
675 	return 0;
676 }
677 
678 static void
679 udp6_detach(struct socket *so)
680 {
681 	struct in6pcb *in6p = sotoin6pcb(so);
682 	int s;
683 
684 	KASSERT(solocked(so));
685 	KASSERT(in6p != NULL);
686 
687 	s = splsoftnet();
688 	in6_pcbdetach(in6p);
689 	splx(s);
690 }
691 
692 static int
693 udp6_accept(struct socket *so, struct sockaddr *nam)
694 {
695 	KASSERT(solocked(so));
696 
697 	return EOPNOTSUPP;
698 }
699 
700 static int
701 udp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
702 {
703 	struct in6pcb *in6p = sotoin6pcb(so);
704 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
705 	int error = 0;
706 	int s;
707 
708 	KASSERT(solocked(so));
709 	KASSERT(in6p != NULL);
710 
711 	s = splsoftnet();
712 	error = in6_pcbbind(in6p, sin6, l);
713 	splx(s);
714 	return error;
715 }
716 
717 static int
718 udp6_listen(struct socket *so, struct lwp *l)
719 {
720 	KASSERT(solocked(so));
721 
722 	return EOPNOTSUPP;
723 }
724 
725 static int
726 udp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
727 {
728 	struct in6pcb *in6p = sotoin6pcb(so);
729 	int error = 0;
730 	int s;
731 
732 	KASSERT(solocked(so));
733 	KASSERT(in6p != NULL);
734 
735 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
736 		return EISCONN;
737 	s = splsoftnet();
738 	error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
739 	splx(s);
740 	if (error == 0)
741 		soisconnected(so);
742 
743 	return error;
744 }
745 
746 static int
747 udp6_connect2(struct socket *so, struct socket *so2)
748 {
749 	KASSERT(solocked(so));
750 
751 	return EOPNOTSUPP;
752 }
753 
754 static int
755 udp6_disconnect(struct socket *so)
756 {
757 	struct in6pcb *in6p = sotoin6pcb(so);
758 	int s;
759 
760 	KASSERT(solocked(so));
761 	KASSERT(in6p != NULL);
762 
763 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
764 		return ENOTCONN;
765 
766 	s = splsoftnet();
767 	in6_pcbdisconnect(in6p);
768 	memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
769 	splx(s);
770 
771 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
772 	in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
773 	return 0;
774 }
775 
776 static int
777 udp6_shutdown(struct socket *so)
778 {
779 	int s;
780 
781 	s = splsoftnet();
782 	socantsendmore(so);
783 	splx(s);
784 
785 	return 0;
786 }
787 
788 static int
789 udp6_abort(struct socket *so)
790 {
791 	int s;
792 
793 	KASSERT(solocked(so));
794 	KASSERT(sotoin6pcb(so) != NULL);
795 
796 	s = splsoftnet();
797 	soisdisconnected(so);
798 	in6_pcbdetach(sotoin6pcb(so));
799 	splx(s);
800 
801 	return 0;
802 }
803 
804 static int
805 udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
806 {
807 	/*
808 	 * MAPPED_ADDR implementation info:
809 	 *  Mapped addr support for PRU_CONTROL is not necessary.
810 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
811 	 *  and they don't associate any addr to their socket.  Then
812 	 *  socket family is only hint about the PRU_CONTROL'ed address
813 	 *  family, especially when getting addrs from kernel.
814 	 *  So AF_INET socket need to be used to control AF_INET addrs,
815 	 *  and AF_INET6 socket for AF_INET6 addrs.
816 	 */
817 	return in6_control(so, cmd, addr6, ifp);
818 }
819 
820 static int
821 udp6_stat(struct socket *so, struct stat *ub)
822 {
823 	KASSERT(solocked(so));
824 
825 	/* stat: don't bother with a blocksize */
826 	return 0;
827 }
828 
829 static int
830 udp6_peeraddr(struct socket *so, struct sockaddr *nam)
831 {
832 	KASSERT(solocked(so));
833 	KASSERT(sotoin6pcb(so) != NULL);
834 	KASSERT(nam != NULL);
835 
836 	in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
837 	return 0;
838 }
839 
840 static int
841 udp6_sockaddr(struct socket *so, struct sockaddr *nam)
842 {
843 	KASSERT(solocked(so));
844 	KASSERT(sotoin6pcb(so) != NULL);
845 	KASSERT(nam != NULL);
846 
847 	in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
848 	return 0;
849 }
850 
851 static int
852 udp6_rcvd(struct socket *so, int flags, struct lwp *l)
853 {
854 	KASSERT(solocked(so));
855 
856 	return EOPNOTSUPP;
857 }
858 
859 static int
860 udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
861 {
862 	KASSERT(solocked(so));
863 
864 	return EOPNOTSUPP;
865 }
866 
867 static int
868 udp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
869     struct mbuf *control, struct lwp *l)
870 {
871 	struct in6pcb *in6p = sotoin6pcb(so);
872 	int error = 0;
873 	int s;
874 
875 	KASSERT(solocked(so));
876 	KASSERT(in6p != NULL);
877 	KASSERT(m != NULL);
878 
879 	s = splsoftnet();
880 	error = udp6_output(in6p, m, (struct sockaddr_in6 *)nam, control, l);
881 	splx(s);
882 
883 	return error;
884 }
885 
886 static int
887 udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
888 {
889 	KASSERT(solocked(so));
890 
891 	if (m)
892 		m_freem(m);
893 	if (control)
894 		m_freem(control);
895 
896 	return EOPNOTSUPP;
897 }
898 
899 static int
900 udp6_purgeif(struct socket *so, struct ifnet *ifp)
901 {
902 
903 	mutex_enter(softnet_lock);
904 	in6_pcbpurgeif0(&udbtable, ifp);
905 	in6_purgeif(ifp);
906 	in6_pcbpurgeif(&udbtable, ifp);
907 	mutex_exit(softnet_lock);
908 
909 	return 0;
910 }
911 
912 static int
913 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
914 {
915 
916 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
917 }
918 
919 static void
920 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
921 {
922 
923 	sysctl_createv(clog, 0, NULL, NULL,
924 		       CTLFLAG_PERMANENT,
925 		       CTLTYPE_NODE, "inet6", NULL,
926 		       NULL, 0, NULL, 0,
927 		       CTL_NET, PF_INET6, CTL_EOL);
928 	sysctl_createv(clog, 0, NULL, NULL,
929 		       CTLFLAG_PERMANENT,
930 		       CTLTYPE_NODE, "udp6",
931 		       SYSCTL_DESCR("UDPv6 related settings"),
932 		       NULL, 0, NULL, 0,
933 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
934 
935 	sysctl_createv(clog, 0, NULL, NULL,
936 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
937 		       CTLTYPE_INT, "sendspace",
938 		       SYSCTL_DESCR("Default UDP send buffer size"),
939 		       NULL, 0, &udp6_sendspace, 0,
940 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
941 		       CTL_EOL);
942 	sysctl_createv(clog, 0, NULL, NULL,
943 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
944 		       CTLTYPE_INT, "recvspace",
945 		       SYSCTL_DESCR("Default UDP receive buffer size"),
946 		       NULL, 0, &udp6_recvspace, 0,
947 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
948 		       CTL_EOL);
949 	sysctl_createv(clog, 0, NULL, NULL,
950 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
951 		       CTLTYPE_INT, "do_loopback_cksum",
952 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
953 		       NULL, 0, &udp_do_loopback_cksum, 0,
954 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
955 		       CTL_EOL);
956 	sysctl_createv(clog, 0, NULL, NULL,
957 		       CTLFLAG_PERMANENT,
958 		       CTLTYPE_STRUCT, "pcblist",
959 		       SYSCTL_DESCR("UDP protocol control block list"),
960 		       sysctl_inpcblist, 0, &udbtable, 0,
961 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
962 		       CTL_EOL);
963 	sysctl_createv(clog, 0, NULL, NULL,
964 		       CTLFLAG_PERMANENT,
965 		       CTLTYPE_STRUCT, "stats",
966 		       SYSCTL_DESCR("UDPv6 statistics"),
967 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
968 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
969 		       CTL_EOL);
970 }
971 
972 void
973 udp6_statinc(u_int stat)
974 {
975 
976 	KASSERT(stat < UDP6_NSTATS);
977 	UDP6_STATINC(stat);
978 }
979 
980 PR_WRAP_USRREQS(udp6)
981 #define	udp6_attach	udp6_attach_wrapper
982 #define	udp6_detach	udp6_detach_wrapper
983 #define	udp6_accept	udp6_accept_wrapper
984 #define	udp6_bind	udp6_bind_wrapper
985 #define	udp6_listen	udp6_listen_wrapper
986 #define	udp6_connect	udp6_connect_wrapper
987 #define	udp6_connect2	udp6_connect2_wrapper
988 #define	udp6_disconnect	udp6_disconnect_wrapper
989 #define	udp6_shutdown	udp6_shutdown_wrapper
990 #define	udp6_abort	udp6_abort_wrapper
991 #define	udp6_ioctl	udp6_ioctl_wrapper
992 #define	udp6_stat	udp6_stat_wrapper
993 #define	udp6_peeraddr	udp6_peeraddr_wrapper
994 #define	udp6_sockaddr	udp6_sockaddr_wrapper
995 #define	udp6_rcvd	udp6_rcvd_wrapper
996 #define	udp6_recvoob	udp6_recvoob_wrapper
997 #define	udp6_send	udp6_send_wrapper
998 #define	udp6_sendoob	udp6_sendoob_wrapper
999 #define	udp6_purgeif	udp6_purgeif_wrapper
1000 
1001 const struct pr_usrreqs udp6_usrreqs = {
1002 	.pr_attach	= udp6_attach,
1003 	.pr_detach	= udp6_detach,
1004 	.pr_accept	= udp6_accept,
1005 	.pr_bind	= udp6_bind,
1006 	.pr_listen	= udp6_listen,
1007 	.pr_connect	= udp6_connect,
1008 	.pr_connect2	= udp6_connect2,
1009 	.pr_disconnect	= udp6_disconnect,
1010 	.pr_shutdown	= udp6_shutdown,
1011 	.pr_abort	= udp6_abort,
1012 	.pr_ioctl	= udp6_ioctl,
1013 	.pr_stat	= udp6_stat,
1014 	.pr_peeraddr	= udp6_peeraddr,
1015 	.pr_sockaddr	= udp6_sockaddr,
1016 	.pr_rcvd	= udp6_rcvd,
1017 	.pr_recvoob	= udp6_recvoob,
1018 	.pr_send	= udp6_send,
1019 	.pr_sendoob	= udp6_sendoob,
1020 	.pr_purgeif	= udp6_purgeif,
1021 };
1022