xref: /netbsd-src/sys/netinet6/udp6_usrreq.c (revision 7863ba460b0a05b553c754e5dbc29247dddec322)
1 /* $NetBSD: udp6_usrreq.c,v 1.138 2018/03/19 16:26:25 roy Exp $ */
2 /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $ */
3 /* $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 itojun Exp $ */
4 
5 /*
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1989, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
63  */
64 
65 #include <sys/cdefs.h>
66 __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.138 2018/03/19 16:26:25 roy Exp $");
67 
68 #ifdef _KERNEL_OPT
69 #include "opt_inet.h"
70 #include "opt_inet_csum.h"
71 #include "opt_ipsec.h"
72 #include "opt_net_mpsafe.h"
73 #endif
74 
75 #include <sys/param.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/syslog.h>
83 #include <sys/domain.h>
84 #include <sys/sysctl.h>
85 
86 #include <net/if.h>
87 #include <net/if_types.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_offload.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/udp.h>
97 #include <netinet/udp_var.h>
98 #include <netinet/udp_private.h>
99 
100 #include <netinet/ip6.h>
101 #include <netinet/icmp6.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet6/ip6_private.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/udp6_var.h>
106 #include <netinet6/udp6_private.h>
107 #include <netinet6/ip6protosw.h>
108 #include <netinet6/scope6_var.h>
109 
110 #ifdef IPSEC
111 #include <netipsec/ipsec.h>
112 #include <netipsec/ipsec_var.h>
113 #ifdef INET6
114 #include <netipsec/ipsec6.h>
115 #endif
116 #endif
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 		    sin6tocsa(sa6_src), uh.uh_sport, cmd, cmdarg,
294 		    notify);
295 	} else {
296 		(void)in6_pcbnotify(&udbtable, sa, 0,
297 		    sin6tocsa(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;
347 
348 	KASSERT(so != NULL);
349 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
350 	in6p = sotoin6pcb(so);
351 	KASSERT(in6p != NULL);
352 
353 #if defined(IPSEC)
354 	/* check AH/ESP integrity. */
355 	if (ipsec_used && ipsec_in_reject(m, in6p)) {
356 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
357 			icmp6_error(n, ICMP6_DST_UNREACH,
358 			    ICMP6_DST_UNREACH_ADMIN, 0);
359 		return;
360 	}
361 #endif
362 
363 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
364 		if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
365 		    SOOPT_TIMESTAMP(in6p->in6p_socket->so_options)) {
366 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
367 			ip6_savecontrol(in6p, &opts, ip6, n);
368 		}
369 
370 		m_adj(n, off);
371 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
372 			m_freem(n);
373 			if (opts)
374 				m_freem(opts);
375 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
376 			soroverflow(so);
377 		} else
378 			sorwakeup(so);
379 	}
380 }
381 
382 int
383 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
384     struct mbuf *m, int off)
385 {
386 	u_int16_t sport, dport;
387 	int rcvcnt;
388 	struct in6_addr src6, *dst6;
389 	const struct in_addr *dst4;
390 	struct inpcb_hdr *inph;
391 	struct in6pcb *in6p;
392 
393 	rcvcnt = 0;
394 	off += sizeof(struct udphdr);	/* now, offset of payload */
395 
396 	if (af != AF_INET && af != AF_INET6)
397 		goto bad;
398 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
399 		goto bad;
400 
401 	src6 = src->sin6_addr;
402 	if (sa6_recoverscope(src) != 0) {
403 		/* XXX: should be impossible. */
404 		goto bad;
405 	}
406 	sport = src->sin6_port;
407 
408 	dport = dst->sin6_port;
409 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
410 	dst6 = &dst->sin6_addr;
411 
412 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
413 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
414 		/*
415 		 * Deliver a multicast or broadcast datagram to *all* sockets
416 		 * for which the local and remote addresses and ports match
417 		 * those of the incoming datagram.  This allows more than
418 		 * one process to receive multi/broadcasts on the same port.
419 		 * (This really ought to be done for unicast datagrams as
420 		 * well, but that would cause problems with existing
421 		 * applications that open both address-specific sockets and
422 		 * a wildcard socket listening to the same port -- they would
423 		 * end up receiving duplicates of every unicast datagram.
424 		 * Those applications open the multiple sockets to overcome an
425 		 * inadequacy of the UDP socket interface, but for backwards
426 		 * compatibility we avoid the problem here rather than
427 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
428 		 */
429 
430 		/*
431 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
432 		 * we need udpiphdr for IPsec processing so we do that later.
433 		 */
434 		/*
435 		 * Locate pcb(s) for datagram.
436 		 */
437 		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
438 			in6p = (struct in6pcb *)inph;
439 			if (in6p->in6p_af != AF_INET6)
440 				continue;
441 
442 			if (in6p->in6p_lport != dport)
443 				continue;
444 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
445 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
446 				    dst6))
447 					continue;
448 			} else {
449 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
450 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
451 					continue;
452 			}
453 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
454 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
455 				    &src6) || in6p->in6p_fport != sport)
456 					continue;
457 			} else {
458 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
459 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
460 					continue;
461 			}
462 
463 			udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
464 			rcvcnt++;
465 
466 			/*
467 			 * Don't look for additional matches if this one does
468 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
469 			 * socket options set.  This heuristic avoids searching
470 			 * through all pcbs in the common case of a non-shared
471 			 * port.  It assumes that an application will never
472 			 * clear these options after setting them.
473 			 */
474 			if ((in6p->in6p_socket->so_options &
475 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
476 				break;
477 		}
478 	} else {
479 		/*
480 		 * Locate pcb for datagram.
481 		 */
482 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
483 					     dport, 0, 0);
484 		if (in6p == 0) {
485 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
486 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
487 			if (in6p == 0)
488 				return rcvcnt;
489 		}
490 
491 		udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
492 		rcvcnt++;
493 	}
494 
495 bad:
496 	return rcvcnt;
497 }
498 
499 int
500 udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
501 {
502 
503 	/*
504 	 * XXX it's better to record and check if this mbuf is
505 	 * already checked.
506 	 */
507 
508 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
509 		goto good;
510 	}
511 	if (uh->uh_sum == 0) {
512 		UDP6_STATINC(UDP6_STAT_NOSUM);
513 		goto bad;
514 	}
515 
516 	switch (m->m_pkthdr.csum_flags &
517 	    ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv6) |
518 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
519 	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
520 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
521 		UDP6_STATINC(UDP6_STAT_BADSUM);
522 		goto bad;
523 
524 #if 0 /* notyet */
525 	case M_CSUM_UDPv6|M_CSUM_DATA:
526 #endif
527 
528 	case M_CSUM_UDPv6:
529 		/* Checksum was okay. */
530 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
531 		break;
532 
533 	default:
534 		/*
535 		 * Need to compute it ourselves.  Maybe skip checksum
536 		 * on loopback interfaces.
537 		 */
538 		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
539 		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
540 			UDP6_STATINC(UDP6_STAT_BADSUM);
541 			goto bad;
542 		}
543 	}
544 
545 good:
546 	return 0;
547 bad:
548 	return -1;
549 }
550 
551 int
552 udp6_input(struct mbuf **mp, int *offp, int proto)
553 {
554 	struct mbuf *m = *mp;
555 	int off = *offp;
556 	struct sockaddr_in6 src, dst;
557 	struct ip6_hdr *ip6;
558 	struct udphdr *uh;
559 	u_int32_t plen, ulen;
560 
561 	ip6 = mtod(m, struct ip6_hdr *);
562 
563 #if defined(NFAITH) && 0 < NFAITH
564 	if (faithprefix(&ip6->ip6_dst)) {
565 		/* send icmp6 host unreach? */
566 		m_freem(m);
567 		return IPPROTO_DONE;
568 	}
569 #endif
570 
571 	UDP6_STATINC(UDP6_STAT_IPACKETS);
572 
573 	/* Check for jumbogram is done in ip6_input. We can trust pkthdr.len. */
574 	plen = m->m_pkthdr.len - off;
575 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
576 	if (uh == NULL) {
577 		IP6_STATINC(IP6_STAT_TOOSHORT);
578 		return IPPROTO_DONE;
579 	}
580 
581 	/*
582 	 * Enforce alignment requirements that are violated in
583 	 * some cases, see kern/50766 for details.
584 	 */
585 	if (UDP_HDR_ALIGNED_P(uh) == 0) {
586 		m = m_copyup(m, off + sizeof(struct udphdr), 0);
587 		if (m == NULL) {
588 			IP6_STATINC(IP6_STAT_TOOSHORT);
589 			return IPPROTO_DONE;
590 		}
591 		ip6 = mtod(m, struct ip6_hdr *);
592 		uh = (struct udphdr *)(mtod(m, char *) + off);
593 	}
594 	KASSERT(UDP_HDR_ALIGNED_P(uh));
595 	ulen = ntohs((u_short)uh->uh_ulen);
596 
597 	/*
598 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
599 	 * iff payload length > 0xffff.
600 	 */
601 	if (ulen == 0 && plen > 0xffff)
602 		ulen = plen;
603 
604 	if (plen != ulen) {
605 		UDP6_STATINC(UDP6_STAT_BADLEN);
606 		goto bad;
607 	}
608 
609 	/* destination port of 0 is illegal, based on RFC768. */
610 	if (uh->uh_dport == 0)
611 		goto bad;
612 
613 	/*
614 	 * Checksum extended UDP header and data.  Maybe skip checksum
615 	 * on loopback interfaces.
616 	 */
617 	if (udp6_input_checksum(m, uh, off, ulen))
618 		goto bad;
619 
620 	/*
621 	 * Construct source and dst sockaddrs.
622 	 */
623 	memset(&src, 0, sizeof(src));
624 	src.sin6_family = AF_INET6;
625 	src.sin6_len = sizeof(struct sockaddr_in6);
626 	src.sin6_addr = ip6->ip6_src;
627 	src.sin6_port = uh->uh_sport;
628 	memset(&dst, 0, sizeof(dst));
629 	dst.sin6_family = AF_INET6;
630 	dst.sin6_len = sizeof(struct sockaddr_in6);
631 	dst.sin6_addr = ip6->ip6_dst;
632 	dst.sin6_port = uh->uh_dport;
633 
634 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
635 		if (m->m_flags & M_MCAST) {
636 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
637 			goto bad;
638 		}
639 		UDP6_STATINC(UDP6_STAT_NOPORT);
640 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
641 		m = NULL;
642 	}
643 
644 bad:
645 	if (m)
646 		m_freem(m);
647 	return IPPROTO_DONE;
648 }
649 
650 int
651 udp6_output(struct in6pcb * const in6p, struct mbuf *m,
652     struct sockaddr_in6 * const addr6, struct mbuf * const control,
653     struct lwp * const l)
654 {
655 	u_int32_t ulen = m->m_pkthdr.len;
656 	u_int32_t plen = sizeof(struct udphdr) + ulen;
657 	struct ip6_hdr *ip6;
658 	struct udphdr *udp6;
659 	struct in6_addr _laddr, *laddr, *faddr;
660 	struct in6_addr laddr_mapped; /* XXX ugly */
661 	struct sockaddr_in6 *sin6 = NULL;
662 	struct ifnet *oifp = NULL;
663 	int scope_ambiguous = 0;
664 	u_int16_t fport;
665 	int error = 0;
666 	struct ip6_pktopts *optp = NULL;
667 	struct ip6_pktopts opt;
668 	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
669 #ifdef INET
670 	struct ip *ip;
671 	struct udpiphdr *ui;
672 	int flags = 0;
673 #endif
674 	struct sockaddr_in6 tmp;
675 
676 	if (addr6) {
677 		sin6 = addr6;
678 		if (sin6->sin6_family != AF_INET6) {
679 			error = EAFNOSUPPORT;
680 			goto release;
681 		}
682 
683 		/* protect *sin6 from overwrites */
684 		tmp = *sin6;
685 		sin6 = &tmp;
686 
687 		/*
688 		 * Application should provide a proper zone ID or the use of
689 		 * default zone IDs should be enabled.  Unfortunately, some
690 		 * applications do not behave as it should, so we need a
691 		 * workaround.  Even if an appropriate ID is not determined,
692 		 * we'll see if we can determine the outgoing interface.  If we
693 		 * can, determine the zone ID based on the interface below.
694 		 */
695 		if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
696 			scope_ambiguous = 1;
697 		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
698 			goto release;
699 	}
700 
701 	if (control) {
702 		if (__predict_false(l == NULL)) {
703 			panic("%s: control but no lwp", __func__);
704 		}
705 		if ((error = ip6_setpktopts(control, &opt,
706 		    in6p->in6p_outputopts, l->l_cred, IPPROTO_UDP)) != 0)
707 			goto release;
708 		optp = &opt;
709 	} else
710 		optp = in6p->in6p_outputopts;
711 
712 
713 	if (sin6) {
714 		/*
715 		 * Slightly different than v4 version in that we call
716 		 * in6_selectsrc and in6_pcbsetport to fill in the local
717 		 * address and port rather than in_pcbconnect. in_pcbconnect
718 		 * sets in6p_faddr which causes EISCONN below to be hit on
719 		 * subsequent sendto.
720 		 */
721 		if (sin6->sin6_port == 0) {
722 			error = EADDRNOTAVAIL;
723 			goto release;
724 		}
725 
726 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
727 			/* how about ::ffff:0.0.0.0 case? */
728 			error = EISCONN;
729 			goto release;
730 		}
731 
732 		faddr = &sin6->sin6_addr;
733 		fport = sin6->sin6_port; /* allow 0 port */
734 
735 		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
736 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
737 				/*
738 				 * I believe we should explicitly discard the
739 				 * packet when mapped addresses are disabled,
740 				 * rather than send the packet as an IPv6 one.
741 				 * If we chose the latter approach, the packet
742 				 * might be sent out on the wire based on the
743 				 * default route, the situation which we'd
744 				 * probably want to avoid.
745 				 * (20010421 jinmei@kame.net)
746 				 */
747 				error = EINVAL;
748 				goto release;
749 			}
750 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
751 			    !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
752 				/*
753 				 * when remote addr is an IPv4-mapped address,
754 				 * local addr should not be an IPv6 address,
755 				 * since you cannot determine how to map IPv6
756 				 * source address to IPv4.
757 				 */
758 				error = EINVAL;
759 				goto release;
760 			}
761 
762 			af = AF_INET;
763 		}
764 
765 		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
766 			struct psref psref;
767 			int bound = curlwp_bind();
768 
769 			error = in6_selectsrc(sin6, optp,
770 			    in6p->in6p_moptions,
771 			    &in6p->in6p_route,
772 			    &in6p->in6p_laddr, &oifp, &psref, &_laddr);
773 			/* XXX need error check? */
774 			if (oifp && scope_ambiguous &&
775 			    (error = in6_setscope(&sin6->sin6_addr,
776 			    oifp, NULL))) {
777 				if_put(oifp, &psref);
778 				curlwp_bindx(bound);
779 				goto release;
780 			}
781 			if_put(oifp, &psref);
782 			curlwp_bindx(bound);
783 			laddr = &_laddr;
784 		} else {
785 			/*
786 			 * XXX: freebsd[34] does not have in_selectsrc, but
787 			 * we can omit the whole part because freebsd4 calls
788 			 * udp_output() directly in this case, and thus we'll
789 			 * never see this path.
790 			 */
791 			if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
792 				struct sockaddr_in sin_dst;
793 				struct in_addr ina;
794 				struct in_ifaddr *ia4;
795 				struct psref _psref;
796 				int bound;
797 
798 				memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
799 				sockaddr_in_init(&sin_dst, &ina, 0);
800 				bound = curlwp_bind();
801 				ia4 = in_selectsrc(&sin_dst, &in6p->in6p_route,
802 				    in6p->in6p_socket->so_options, NULL,
803 				    &error, &_psref);
804 				if (ia4 == NULL) {
805 					curlwp_bindx(bound);
806 					if (error == 0)
807 						error = EADDRNOTAVAIL;
808 					goto release;
809 				}
810 				memset(&laddr_mapped, 0, sizeof(laddr_mapped));
811 				laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
812 				memcpy(&laddr_mapped.s6_addr[12],
813 				      &IA_SIN(ia4)->sin_addr,
814 				      sizeof(IA_SIN(ia4)->sin_addr));
815 				ia4_release(ia4, &_psref);
816 				curlwp_bindx(bound);
817 				laddr = &laddr_mapped;
818 			} else
819 			{
820 				laddr = &in6p->in6p_laddr;	/* XXX */
821 			}
822 		}
823 		if (laddr == NULL) {
824 			if (error == 0)
825 				error = EADDRNOTAVAIL;
826 			goto release;
827 		}
828 		if (in6p->in6p_lport == 0) {
829 			/*
830 			 * Craft a sockaddr_in6 for the local endpoint. Use the
831 			 * "any" as a base, set the address, and recover the
832 			 * scope.
833 			 */
834 			struct sockaddr_in6 lsin6 =
835 			    *((const struct sockaddr_in6 *)in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
836 			lsin6.sin6_addr = *laddr;
837 			error = sa6_recoverscope(&lsin6);
838 			if (error)
839 				goto release;
840 
841 			error = in6_pcbsetport(&lsin6, in6p, l);
842 
843 			if (error) {
844 				in6p->in6p_laddr = in6addr_any;
845 				goto release;
846 			}
847 		}
848 	} else {
849 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
850 			error = ENOTCONN;
851 			goto release;
852 		}
853 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
854 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
855 			{
856 				/*
857 				 * XXX: this case would happen when the
858 				 * application sets the V6ONLY flag after
859 				 * connecting the foreign address.
860 				 * Such applications should be fixed,
861 				 * so we bark here.
862 				 */
863 				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
864 				    "option was set for a connected socket\n");
865 				error = EINVAL;
866 				goto release;
867 			} else
868 				af = AF_INET;
869 		}
870 		laddr = &in6p->in6p_laddr;
871 		faddr = &in6p->in6p_faddr;
872 		fport = in6p->in6p_fport;
873 	}
874 
875 	if (af == AF_INET)
876 		hlen = sizeof(struct ip);
877 
878 	/*
879 	 * Calculate data length and get a mbuf
880 	 * for UDP and IP6 headers.
881 	 */
882 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
883 	if (m == NULL) {
884 		error = ENOBUFS;
885 		goto release;
886 	}
887 
888 	/*
889 	 * Stuff checksum and output datagram.
890 	 */
891 	udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
892 	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
893 	udp6->uh_dport = fport;
894 	if (plen <= 0xffff)
895 		udp6->uh_ulen = htons((u_int16_t)plen);
896 	else
897 		udp6->uh_ulen = 0;
898 	udp6->uh_sum = 0;
899 
900 	switch (af) {
901 	case AF_INET6:
902 		ip6 = mtod(m, struct ip6_hdr *);
903 		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
904 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
905 		ip6->ip6_vfc 	|= IPV6_VERSION;
906 #if 0		/* ip6_plen will be filled in ip6_output. */
907 		ip6->ip6_plen	= htons((u_int16_t)plen);
908 #endif
909 		ip6->ip6_nxt	= IPPROTO_UDP;
910 		ip6->ip6_hlim	= in6_selecthlim_rt(in6p);
911 		ip6->ip6_src	= *laddr;
912 		ip6->ip6_dst	= *faddr;
913 
914 		udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
915 		    htonl(plen), htonl(IPPROTO_UDP));
916 		m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
917 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
918 
919 		UDP6_STATINC(UDP6_STAT_OPACKETS);
920 		error = ip6_output(m, optp, &in6p->in6p_route, 0,
921 		    in6p->in6p_moptions, in6p, NULL);
922 		break;
923 	case AF_INET:
924 #ifdef INET
925 		/* can't transmit jumbogram over IPv4 */
926 		if (plen > 0xffff) {
927 			error = EMSGSIZE;
928 			goto release;
929 		}
930 
931 		ip = mtod(m, struct ip *);
932 		ui = (struct udpiphdr *)ip;
933 		memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
934 		ui->ui_pr = IPPROTO_UDP;
935 		ui->ui_len = htons(plen);
936 		memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
937 		ui->ui_ulen = ui->ui_len;
938 
939 		flags = (in6p->in6p_socket->so_options &
940 			 (SO_DONTROUTE | SO_BROADCAST));
941 		memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
942 
943 		udp6->uh_sum = in_cksum(m, hlen + plen);
944 		if (udp6->uh_sum == 0)
945 			udp6->uh_sum = 0xffff;
946 
947 		ip->ip_len = htons(hlen + plen);
948 		ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
949 		ip->ip_tos = 0;	/* XXX */
950 
951 		UDP_STATINC(UDP_STAT_OPACKETS);
952 		error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
953 		    in6p->in6p_v4moptions, NULL);
954 		break;
955 #else
956 		error = EAFNOSUPPORT;
957 		goto release;
958 #endif
959 	}
960 	goto releaseopt;
961 
962 release:
963 	m_freem(m);
964 
965 releaseopt:
966 	if (control) {
967 		if (optp == &opt)
968 			ip6_clearpktopts(&opt, -1);
969 		m_freem(control);
970 	}
971 	return (error);
972 }
973 
974 static int
975 udp6_attach(struct socket *so, int proto)
976 {
977 	struct in6pcb *in6p;
978 	int s, error;
979 
980 	KASSERT(sotoin6pcb(so) == NULL);
981 	sosetlock(so);
982 
983 	/*
984 	 * MAPPED_ADDR implementation spec:
985 	 *  Always attach for IPv6, and only when necessary for IPv4.
986 	 */
987 	s = splsoftnet();
988 	error = in6_pcballoc(so, &udbtable);
989 	splx(s);
990 	if (error) {
991 		return error;
992 	}
993 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
994 	if (error) {
995 		return error;
996 	}
997 	in6p = sotoin6pcb(so);
998 	in6p->in6p_cksum = -1;	/* just to be sure */
999 
1000 	KASSERT(solocked(so));
1001 	return 0;
1002 }
1003 
1004 static void
1005 udp6_detach(struct socket *so)
1006 {
1007 	struct in6pcb *in6p = sotoin6pcb(so);
1008 	int s;
1009 
1010 	KASSERT(solocked(so));
1011 	KASSERT(in6p != NULL);
1012 
1013 	s = splsoftnet();
1014 	in6_pcbdetach(in6p);
1015 	splx(s);
1016 }
1017 
1018 static int
1019 udp6_accept(struct socket *so, struct sockaddr *nam)
1020 {
1021 	KASSERT(solocked(so));
1022 
1023 	return EOPNOTSUPP;
1024 }
1025 
1026 static int
1027 udp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
1028 {
1029 	struct in6pcb *in6p = sotoin6pcb(so);
1030 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1031 	int error = 0;
1032 	int s;
1033 
1034 	KASSERT(solocked(so));
1035 	KASSERT(in6p != NULL);
1036 
1037 	s = splsoftnet();
1038 	error = in6_pcbbind(in6p, sin6, l);
1039 	splx(s);
1040 	return error;
1041 }
1042 
1043 static int
1044 udp6_listen(struct socket *so, struct lwp *l)
1045 {
1046 	KASSERT(solocked(so));
1047 
1048 	return EOPNOTSUPP;
1049 }
1050 
1051 static int
1052 udp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
1053 {
1054 	struct in6pcb *in6p = sotoin6pcb(so);
1055 	int error = 0;
1056 	int s;
1057 
1058 	KASSERT(solocked(so));
1059 	KASSERT(in6p != NULL);
1060 
1061 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1062 		return EISCONN;
1063 	s = splsoftnet();
1064 	error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
1065 	splx(s);
1066 	if (error == 0)
1067 		soisconnected(so);
1068 
1069 	return error;
1070 }
1071 
1072 static int
1073 udp6_connect2(struct socket *so, struct socket *so2)
1074 {
1075 	KASSERT(solocked(so));
1076 
1077 	return EOPNOTSUPP;
1078 }
1079 
1080 static int
1081 udp6_disconnect(struct socket *so)
1082 {
1083 	struct in6pcb *in6p = sotoin6pcb(so);
1084 	int s;
1085 
1086 	KASSERT(solocked(so));
1087 	KASSERT(in6p != NULL);
1088 
1089 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1090 		return ENOTCONN;
1091 
1092 	s = splsoftnet();
1093 	in6_pcbdisconnect(in6p);
1094 	memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
1095 	splx(s);
1096 
1097 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
1098 	in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
1099 	return 0;
1100 }
1101 
1102 static int
1103 udp6_shutdown(struct socket *so)
1104 {
1105 	int s;
1106 
1107 	s = splsoftnet();
1108 	socantsendmore(so);
1109 	splx(s);
1110 
1111 	return 0;
1112 }
1113 
1114 static int
1115 udp6_abort(struct socket *so)
1116 {
1117 	int s;
1118 
1119 	KASSERT(solocked(so));
1120 	KASSERT(sotoin6pcb(so) != NULL);
1121 
1122 	s = splsoftnet();
1123 	soisdisconnected(so);
1124 	in6_pcbdetach(sotoin6pcb(so));
1125 	splx(s);
1126 
1127 	return 0;
1128 }
1129 
1130 static int
1131 udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
1132 {
1133 	/*
1134 	 * MAPPED_ADDR implementation info:
1135 	 *  Mapped addr support for PRU_CONTROL is not necessary.
1136 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
1137 	 *  and they don't associate any addr to their socket.  Then
1138 	 *  socket family is only hint about the PRU_CONTROL'ed address
1139 	 *  family, especially when getting addrs from kernel.
1140 	 *  So AF_INET socket need to be used to control AF_INET addrs,
1141 	 *  and AF_INET6 socket for AF_INET6 addrs.
1142 	 */
1143 	return in6_control(so, cmd, addr6, ifp);
1144 }
1145 
1146 static int
1147 udp6_stat(struct socket *so, struct stat *ub)
1148 {
1149 	KASSERT(solocked(so));
1150 
1151 	/* stat: don't bother with a blocksize */
1152 	return 0;
1153 }
1154 
1155 static int
1156 udp6_peeraddr(struct socket *so, struct sockaddr *nam)
1157 {
1158 	KASSERT(solocked(so));
1159 	KASSERT(sotoin6pcb(so) != NULL);
1160 	KASSERT(nam != NULL);
1161 
1162 	in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
1163 	return 0;
1164 }
1165 
1166 static int
1167 udp6_sockaddr(struct socket *so, struct sockaddr *nam)
1168 {
1169 	KASSERT(solocked(so));
1170 	KASSERT(sotoin6pcb(so) != NULL);
1171 	KASSERT(nam != NULL);
1172 
1173 	in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
1174 	return 0;
1175 }
1176 
1177 static int
1178 udp6_rcvd(struct socket *so, int flags, struct lwp *l)
1179 {
1180 	KASSERT(solocked(so));
1181 
1182 	return EOPNOTSUPP;
1183 }
1184 
1185 static int
1186 udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
1187 {
1188 	KASSERT(solocked(so));
1189 
1190 	return EOPNOTSUPP;
1191 }
1192 
1193 static int
1194 udp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
1195     struct mbuf *control, struct lwp *l)
1196 {
1197 	struct in6pcb *in6p = sotoin6pcb(so);
1198 	int error = 0;
1199 	int s;
1200 
1201 	KASSERT(solocked(so));
1202 	KASSERT(in6p != NULL);
1203 	KASSERT(m != NULL);
1204 
1205 	s = splsoftnet();
1206 	error = udp6_output(in6p, m, (struct sockaddr_in6 *)nam, control, l);
1207 	splx(s);
1208 
1209 	return error;
1210 }
1211 
1212 static int
1213 udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
1214 {
1215 	KASSERT(solocked(so));
1216 
1217 	if (m)
1218 		m_freem(m);
1219 	if (control)
1220 		m_freem(control);
1221 
1222 	return EOPNOTSUPP;
1223 }
1224 
1225 static int
1226 udp6_purgeif(struct socket *so, struct ifnet *ifp)
1227 {
1228 
1229 	mutex_enter(softnet_lock);
1230 	in6_pcbpurgeif0(&udbtable, ifp);
1231 #ifdef NET_MPSAFE
1232 	mutex_exit(softnet_lock);
1233 #endif
1234 	in6_purgeif(ifp);
1235 #ifdef NET_MPSAFE
1236 	mutex_enter(softnet_lock);
1237 #endif
1238 	in6_pcbpurgeif(&udbtable, ifp);
1239 	mutex_exit(softnet_lock);
1240 
1241 	return 0;
1242 }
1243 
1244 static int
1245 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
1246 {
1247 
1248 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
1249 }
1250 
1251 static void
1252 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
1253 {
1254 
1255 	sysctl_createv(clog, 0, NULL, NULL,
1256 		       CTLFLAG_PERMANENT,
1257 		       CTLTYPE_NODE, "inet6", NULL,
1258 		       NULL, 0, NULL, 0,
1259 		       CTL_NET, PF_INET6, CTL_EOL);
1260 	sysctl_createv(clog, 0, NULL, NULL,
1261 		       CTLFLAG_PERMANENT,
1262 		       CTLTYPE_NODE, "udp6",
1263 		       SYSCTL_DESCR("UDPv6 related settings"),
1264 		       NULL, 0, NULL, 0,
1265 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
1266 
1267 	sysctl_createv(clog, 0, NULL, NULL,
1268 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1269 		       CTLTYPE_INT, "sendspace",
1270 		       SYSCTL_DESCR("Default UDP send buffer size"),
1271 		       NULL, 0, &udp6_sendspace, 0,
1272 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
1273 		       CTL_EOL);
1274 	sysctl_createv(clog, 0, NULL, NULL,
1275 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1276 		       CTLTYPE_INT, "recvspace",
1277 		       SYSCTL_DESCR("Default UDP receive buffer size"),
1278 		       NULL, 0, &udp6_recvspace, 0,
1279 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
1280 		       CTL_EOL);
1281 	sysctl_createv(clog, 0, NULL, NULL,
1282 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1283 		       CTLTYPE_INT, "do_loopback_cksum",
1284 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
1285 		       NULL, 0, &udp_do_loopback_cksum, 0,
1286 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
1287 		       CTL_EOL);
1288 	sysctl_createv(clog, 0, NULL, NULL,
1289 		       CTLFLAG_PERMANENT,
1290 		       CTLTYPE_STRUCT, "pcblist",
1291 		       SYSCTL_DESCR("UDP protocol control block list"),
1292 		       sysctl_inpcblist, 0, &udbtable, 0,
1293 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
1294 		       CTL_EOL);
1295 	sysctl_createv(clog, 0, NULL, NULL,
1296 		       CTLFLAG_PERMANENT,
1297 		       CTLTYPE_STRUCT, "stats",
1298 		       SYSCTL_DESCR("UDPv6 statistics"),
1299 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
1300 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
1301 		       CTL_EOL);
1302 }
1303 
1304 void
1305 udp6_statinc(u_int stat)
1306 {
1307 
1308 	KASSERT(stat < UDP6_NSTATS);
1309 	UDP6_STATINC(stat);
1310 }
1311 
1312 PR_WRAP_USRREQS(udp6)
1313 #define	udp6_attach	udp6_attach_wrapper
1314 #define	udp6_detach	udp6_detach_wrapper
1315 #define	udp6_accept	udp6_accept_wrapper
1316 #define	udp6_bind	udp6_bind_wrapper
1317 #define	udp6_listen	udp6_listen_wrapper
1318 #define	udp6_connect	udp6_connect_wrapper
1319 #define	udp6_connect2	udp6_connect2_wrapper
1320 #define	udp6_disconnect	udp6_disconnect_wrapper
1321 #define	udp6_shutdown	udp6_shutdown_wrapper
1322 #define	udp6_abort	udp6_abort_wrapper
1323 #define	udp6_ioctl	udp6_ioctl_wrapper
1324 #define	udp6_stat	udp6_stat_wrapper
1325 #define	udp6_peeraddr	udp6_peeraddr_wrapper
1326 #define	udp6_sockaddr	udp6_sockaddr_wrapper
1327 #define	udp6_rcvd	udp6_rcvd_wrapper
1328 #define	udp6_recvoob	udp6_recvoob_wrapper
1329 #define	udp6_send	udp6_send_wrapper
1330 #define	udp6_sendoob	udp6_sendoob_wrapper
1331 #define	udp6_purgeif	udp6_purgeif_wrapper
1332 
1333 const struct pr_usrreqs udp6_usrreqs = {
1334 	.pr_attach	= udp6_attach,
1335 	.pr_detach	= udp6_detach,
1336 	.pr_accept	= udp6_accept,
1337 	.pr_bind	= udp6_bind,
1338 	.pr_listen	= udp6_listen,
1339 	.pr_connect	= udp6_connect,
1340 	.pr_connect2	= udp6_connect2,
1341 	.pr_disconnect	= udp6_disconnect,
1342 	.pr_shutdown	= udp6_shutdown,
1343 	.pr_abort	= udp6_abort,
1344 	.pr_ioctl	= udp6_ioctl,
1345 	.pr_stat	= udp6_stat,
1346 	.pr_peeraddr	= udp6_peeraddr,
1347 	.pr_sockaddr	= udp6_sockaddr,
1348 	.pr_rcvd	= udp6_rcvd,
1349 	.pr_recvoob	= udp6_recvoob,
1350 	.pr_send	= udp6_send,
1351 	.pr_sendoob	= udp6_sendoob,
1352 	.pr_purgeif	= udp6_purgeif,
1353 };
1354