xref: /netbsd-src/sys/netinet6/udp6_usrreq.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: udp6_usrreq.c,v 1.141 2018/04/28 13:26:57 maxv 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.141 2018/04/28 13:26:57 maxv 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 #ifdef INET6
113 #include <netipsec/ipsec6.h>
114 #endif
115 #endif
116 
117 #include "faith.h"
118 #if defined(NFAITH) && NFAITH > 0
119 #include <net/if_faith.h>
120 #endif
121 
122 /*
123  * UDP protocol implementation.
124  * Per RFC 768, August, 1980.
125  */
126 
127 extern struct inpcbtable udbtable;
128 
129 percpu_t *udp6stat_percpu;
130 
131 /* UDP on IP6 parameters */
132 static int udp6_sendspace = 9216;	/* really max datagram size */
133 static int udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
134 					/* 40 1K datagrams */
135 
136 static void udp6_notify(struct in6pcb *, int);
137 static void sysctl_net_inet6_udp6_setup(struct sysctllog **);
138 
139 #ifdef UDP_CSUM_COUNTERS
140 #include <sys/device.h>
141 struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
142     NULL, "udp6", "hwcsum bad");
143 struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
144     NULL, "udp6", "hwcsum ok");
145 struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
146     NULL, "udp6", "hwcsum data");
147 struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
148     NULL, "udp6", "swcsum");
149 
150 EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
151 EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
152 EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
153 EVCNT_ATTACH_STATIC(udp6_swcsum);
154 
155 #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
156 #else
157 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
158 #endif
159 
160 void
161 udp6_init(void)
162 {
163 	sysctl_net_inet6_udp6_setup(NULL);
164 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
165 
166 	udp_init_common();
167 }
168 
169 /*
170  * Notify a udp user of an asynchronous error;
171  * just wake up so that he can collect error status.
172  */
173 static	void
174 udp6_notify(struct in6pcb *in6p, int errno)
175 {
176 	in6p->in6p_socket->so_error = errno;
177 	sorwakeup(in6p->in6p_socket);
178 	sowwakeup(in6p->in6p_socket);
179 }
180 
181 void *
182 udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
183 {
184 	struct udphdr uh;
185 	struct ip6_hdr *ip6;
186 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
187 	struct mbuf *m;
188 	int off;
189 	void *cmdarg;
190 	struct ip6ctlparam *ip6cp = NULL;
191 	const struct sockaddr_in6 *sa6_src = NULL;
192 	void (*notify)(struct in6pcb *, int) = udp6_notify;
193 	struct udp_portonly {
194 		u_int16_t uh_sport;
195 		u_int16_t uh_dport;
196 	} *uhp;
197 
198 	if (sa->sa_family != AF_INET6 ||
199 	    sa->sa_len != sizeof(struct sockaddr_in6))
200 		return NULL;
201 
202 	if ((unsigned)cmd >= PRC_NCMDS)
203 		return NULL;
204 	if (PRC_IS_REDIRECT(cmd))
205 		notify = in6_rtchange, d = NULL;
206 	else if (cmd == PRC_HOSTDEAD)
207 		d = NULL;
208 	else if (cmd == PRC_MSGSIZE) {
209 		/* special code is present, see below */
210 		notify = in6_rtchange;
211 	}
212 	else if (inet6ctlerrmap[cmd] == 0)
213 		return NULL;
214 
215 	/* if the parameter is from icmp6, decode it. */
216 	if (d != NULL) {
217 		ip6cp = (struct ip6ctlparam *)d;
218 		m = ip6cp->ip6c_m;
219 		ip6 = ip6cp->ip6c_ip6;
220 		off = ip6cp->ip6c_off;
221 		cmdarg = ip6cp->ip6c_cmdarg;
222 		sa6_src = ip6cp->ip6c_src;
223 	} else {
224 		m = NULL;
225 		ip6 = NULL;
226 		cmdarg = NULL;
227 		sa6_src = &sa6_any;
228 		off = 0;
229 	}
230 
231 	if (ip6) {
232 		/* check if we can safely examine src and dst ports */
233 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
234 			if (cmd == PRC_MSGSIZE)
235 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
236 			return NULL;
237 		}
238 
239 		memset(&uh, 0, sizeof(uh));
240 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
241 
242 		if (cmd == PRC_MSGSIZE) {
243 			int valid = 0;
244 
245 			/*
246 			 * Check to see if we have a valid UDP socket
247 			 * corresponding to the address in the ICMPv6 message
248 			 * payload.
249 			 */
250 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
251 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
252 			    uh.uh_sport, 0, 0))
253 				valid++;
254 #if 0
255 			/*
256 			 * As the use of sendto(2) is fairly popular,
257 			 * we may want to allow non-connected pcb too.
258 			 * But it could be too weak against attacks...
259 			 * We should at least check if the local address (= s)
260 			 * is really ours.
261 			 */
262 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
263 			    uh.uh_dport, 0))
264 				valid++;
265 #endif
266 
267 			/*
268 			 * Depending on the value of "valid" and routing table
269 			 * size (mtudisc_{hi,lo}wat), we will:
270 			 * - recalculate the new MTU and create the
271 			 *   corresponding routing entry, or
272 			 * - ignore the MTU change notification.
273 			 */
274 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
275 
276 			/*
277 			 * regardless of if we called
278 			 * icmp6_mtudisc_update(), we need to call
279 			 * in6_pcbnotify(), to notify path MTU change
280 			 * to the userland (RFC3542), because some
281 			 * unconnected sockets may share the same
282 			 * destination and want to know the path MTU.
283 			 */
284 		}
285 
286 		(void)in6_pcbnotify(&udbtable, sa, uh.uh_dport,
287 		    sin6tocsa(sa6_src), uh.uh_sport, cmd, cmdarg,
288 		    notify);
289 	} else {
290 		(void)in6_pcbnotify(&udbtable, sa, 0,
291 		    sin6tocsa(sa6_src), 0, cmd, cmdarg, notify);
292 	}
293 	return NULL;
294 }
295 
296 int
297 udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
298 {
299 	int s;
300 	int error = 0;
301 	int family;
302 
303 	family = so->so_proto->pr_domain->dom_family;
304 
305 	s = splsoftnet();
306 	switch (family) {
307 #ifdef INET
308 	case PF_INET:
309 		if (sopt->sopt_level != IPPROTO_UDP) {
310 			error = ip_ctloutput(op, so, sopt);
311 			goto end;
312 		}
313 		break;
314 #endif
315 #ifdef INET6
316 	case PF_INET6:
317 		if (sopt->sopt_level != IPPROTO_UDP) {
318 			error = ip6_ctloutput(op, so, sopt);
319 			goto end;
320 		}
321 		break;
322 #endif
323 	default:
324 		error = EAFNOSUPPORT;
325 		goto end;
326 	}
327 	error = EINVAL;
328 
329 end:
330 	splx(s);
331 	return error;
332 }
333 
334 static void
335 udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
336     struct sockaddr *src, struct socket *so)
337 {
338 	struct mbuf *opts = NULL;
339 	struct mbuf *n;
340 	struct in6pcb *in6p;
341 
342 	KASSERT(so != NULL);
343 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
344 	in6p = sotoin6pcb(so);
345 	KASSERT(in6p != NULL);
346 
347 #if defined(IPSEC)
348 	if (ipsec_used && ipsec_in_reject(m, in6p)) {
349 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
350 			icmp6_error(n, ICMP6_DST_UNREACH,
351 			    ICMP6_DST_UNREACH_ADMIN, 0);
352 		return;
353 	}
354 #endif
355 
356 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
357 		if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
358 		    SOOPT_TIMESTAMP(in6p->in6p_socket->so_options)) {
359 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
360 			ip6_savecontrol(in6p, &opts, ip6, n);
361 		}
362 
363 		m_adj(n, off);
364 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
365 			m_freem(n);
366 			if (opts)
367 				m_freem(opts);
368 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
369 			soroverflow(so);
370 		} else
371 			sorwakeup(so);
372 	}
373 }
374 
375 int
376 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
377     struct mbuf *m, int off)
378 {
379 	u_int16_t sport, dport;
380 	int rcvcnt;
381 	struct in6_addr src6, *dst6;
382 	const struct in_addr *dst4;
383 	struct inpcb_hdr *inph;
384 	struct in6pcb *in6p;
385 
386 	rcvcnt = 0;
387 	off += sizeof(struct udphdr);	/* now, offset of payload */
388 
389 	if (af != AF_INET && af != AF_INET6)
390 		goto bad;
391 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
392 		goto bad;
393 
394 	src6 = src->sin6_addr;
395 	if (sa6_recoverscope(src) != 0) {
396 		/* XXX: should be impossible. */
397 		goto bad;
398 	}
399 	sport = src->sin6_port;
400 
401 	dport = dst->sin6_port;
402 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
403 	dst6 = &dst->sin6_addr;
404 
405 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
406 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
407 		/*
408 		 * Deliver a multicast or broadcast datagram to *all* sockets
409 		 * for which the local and remote addresses and ports match
410 		 * those of the incoming datagram.  This allows more than
411 		 * one process to receive multi/broadcasts on the same port.
412 		 * (This really ought to be done for unicast datagrams as
413 		 * well, but that would cause problems with existing
414 		 * applications that open both address-specific sockets and
415 		 * a wildcard socket listening to the same port -- they would
416 		 * end up receiving duplicates of every unicast datagram.
417 		 * Those applications open the multiple sockets to overcome an
418 		 * inadequacy of the UDP socket interface, but for backwards
419 		 * compatibility we avoid the problem here rather than
420 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
421 		 */
422 
423 		/*
424 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
425 		 * we need udpiphdr for IPsec processing so we do that later.
426 		 */
427 		/*
428 		 * Locate pcb(s) for datagram.
429 		 */
430 		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
431 			in6p = (struct in6pcb *)inph;
432 			if (in6p->in6p_af != AF_INET6)
433 				continue;
434 
435 			if (in6p->in6p_lport != dport)
436 				continue;
437 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
438 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
439 				    dst6))
440 					continue;
441 			} else {
442 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
443 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
444 					continue;
445 			}
446 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
447 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
448 				    &src6) || in6p->in6p_fport != sport)
449 					continue;
450 			} else {
451 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
452 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
453 					continue;
454 			}
455 
456 			udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
457 			rcvcnt++;
458 
459 			/*
460 			 * Don't look for additional matches if this one does
461 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
462 			 * socket options set.  This heuristic avoids searching
463 			 * through all pcbs in the common case of a non-shared
464 			 * port.  It assumes that an application will never
465 			 * clear these options after setting them.
466 			 */
467 			if ((in6p->in6p_socket->so_options &
468 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
469 				break;
470 		}
471 	} else {
472 		/*
473 		 * Locate pcb for datagram.
474 		 */
475 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
476 					     dport, 0, 0);
477 		if (in6p == 0) {
478 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
479 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
480 			if (in6p == 0)
481 				return rcvcnt;
482 		}
483 
484 		udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
485 		rcvcnt++;
486 	}
487 
488 bad:
489 	return rcvcnt;
490 }
491 
492 int
493 udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
494 {
495 
496 	/*
497 	 * XXX it's better to record and check if this mbuf is
498 	 * already checked.
499 	 */
500 
501 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
502 		goto good;
503 	}
504 	if (uh->uh_sum == 0) {
505 		UDP6_STATINC(UDP6_STAT_NOSUM);
506 		goto bad;
507 	}
508 
509 	switch (m->m_pkthdr.csum_flags &
510 	    ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv6) |
511 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
512 	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
513 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
514 		UDP6_STATINC(UDP6_STAT_BADSUM);
515 		goto bad;
516 
517 #if 0 /* notyet */
518 	case M_CSUM_UDPv6|M_CSUM_DATA:
519 #endif
520 
521 	case M_CSUM_UDPv6:
522 		/* Checksum was okay. */
523 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
524 		break;
525 
526 	default:
527 		/*
528 		 * Need to compute it ourselves.  Maybe skip checksum
529 		 * on loopback interfaces.
530 		 */
531 		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
532 		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
533 			UDP6_STATINC(UDP6_STAT_BADSUM);
534 			goto bad;
535 		}
536 	}
537 
538 good:
539 	return 0;
540 bad:
541 	return -1;
542 }
543 
544 int
545 udp6_input(struct mbuf **mp, int *offp, int proto)
546 {
547 	struct mbuf *m = *mp;
548 	int off = *offp;
549 	struct sockaddr_in6 src, dst;
550 	struct ip6_hdr *ip6;
551 	struct udphdr *uh;
552 	u_int32_t plen, ulen;
553 
554 	ip6 = mtod(m, struct ip6_hdr *);
555 
556 #if defined(NFAITH) && 0 < NFAITH
557 	if (faithprefix(&ip6->ip6_dst)) {
558 		/* send icmp6 host unreach? */
559 		m_freem(m);
560 		return IPPROTO_DONE;
561 	}
562 #endif
563 
564 	UDP6_STATINC(UDP6_STAT_IPACKETS);
565 
566 	/* Check for jumbogram is done in ip6_input. We can trust pkthdr.len. */
567 	plen = m->m_pkthdr.len - off;
568 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
569 	if (uh == NULL) {
570 		IP6_STATINC(IP6_STAT_TOOSHORT);
571 		return IPPROTO_DONE;
572 	}
573 
574 	/*
575 	 * Enforce alignment requirements that are violated in
576 	 * some cases, see kern/50766 for details.
577 	 */
578 	if (UDP_HDR_ALIGNED_P(uh) == 0) {
579 		m = m_copyup(m, off + sizeof(struct udphdr), 0);
580 		if (m == NULL) {
581 			IP6_STATINC(IP6_STAT_TOOSHORT);
582 			return IPPROTO_DONE;
583 		}
584 		ip6 = mtod(m, struct ip6_hdr *);
585 		uh = (struct udphdr *)(mtod(m, char *) + off);
586 	}
587 	KASSERT(UDP_HDR_ALIGNED_P(uh));
588 	ulen = ntohs((u_short)uh->uh_ulen);
589 
590 	/*
591 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
592 	 * iff payload length > 0xffff.
593 	 */
594 	if (ulen == 0 && plen > 0xffff)
595 		ulen = plen;
596 
597 	if (plen != ulen) {
598 		UDP6_STATINC(UDP6_STAT_BADLEN);
599 		goto bad;
600 	}
601 
602 	/* destination port of 0 is illegal, based on RFC768. */
603 	if (uh->uh_dport == 0)
604 		goto bad;
605 
606 	/*
607 	 * Checksum extended UDP header and data.  Maybe skip checksum
608 	 * on loopback interfaces.
609 	 */
610 	if (udp6_input_checksum(m, uh, off, ulen))
611 		goto bad;
612 
613 	/*
614 	 * Construct source and dst sockaddrs.
615 	 */
616 	memset(&src, 0, sizeof(src));
617 	src.sin6_family = AF_INET6;
618 	src.sin6_len = sizeof(struct sockaddr_in6);
619 	src.sin6_addr = ip6->ip6_src;
620 	src.sin6_port = uh->uh_sport;
621 	memset(&dst, 0, sizeof(dst));
622 	dst.sin6_family = AF_INET6;
623 	dst.sin6_len = sizeof(struct sockaddr_in6);
624 	dst.sin6_addr = ip6->ip6_dst;
625 	dst.sin6_port = uh->uh_dport;
626 
627 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
628 		if (m->m_flags & M_MCAST) {
629 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
630 			goto bad;
631 		}
632 		UDP6_STATINC(UDP6_STAT_NOPORT);
633 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
634 		m = NULL;
635 	}
636 
637 bad:
638 	if (m)
639 		m_freem(m);
640 	return IPPROTO_DONE;
641 }
642 
643 int
644 udp6_output(struct in6pcb * const in6p, struct mbuf *m,
645     struct sockaddr_in6 * const addr6, struct mbuf * const control,
646     struct lwp * const l)
647 {
648 	u_int32_t ulen = m->m_pkthdr.len;
649 	u_int32_t plen = sizeof(struct udphdr) + ulen;
650 	struct ip6_hdr *ip6;
651 	struct udphdr *udp6;
652 	struct in6_addr _laddr, *laddr, *faddr;
653 	struct in6_addr laddr_mapped; /* XXX ugly */
654 	struct sockaddr_in6 *sin6 = NULL;
655 	struct ifnet *oifp = NULL;
656 	int scope_ambiguous = 0;
657 	u_int16_t fport;
658 	int error = 0;
659 	struct ip6_pktopts *optp = NULL;
660 	struct ip6_pktopts opt;
661 	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
662 #ifdef INET
663 	struct ip *ip;
664 	struct udpiphdr *ui;
665 	int flags = 0;
666 #endif
667 	struct sockaddr_in6 tmp;
668 
669 	if (addr6) {
670 		sin6 = addr6;
671 		if (sin6->sin6_family != AF_INET6) {
672 			error = EAFNOSUPPORT;
673 			goto release;
674 		}
675 
676 		/* protect *sin6 from overwrites */
677 		tmp = *sin6;
678 		sin6 = &tmp;
679 
680 		/*
681 		 * Application should provide a proper zone ID or the use of
682 		 * default zone IDs should be enabled.  Unfortunately, some
683 		 * applications do not behave as it should, so we need a
684 		 * workaround.  Even if an appropriate ID is not determined,
685 		 * we'll see if we can determine the outgoing interface.  If we
686 		 * can, determine the zone ID based on the interface below.
687 		 */
688 		if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
689 			scope_ambiguous = 1;
690 		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
691 			goto release;
692 	}
693 
694 	if (control) {
695 		if (__predict_false(l == NULL)) {
696 			panic("%s: control but no lwp", __func__);
697 		}
698 		if ((error = ip6_setpktopts(control, &opt,
699 		    in6p->in6p_outputopts, l->l_cred, IPPROTO_UDP)) != 0)
700 			goto release;
701 		optp = &opt;
702 	} else
703 		optp = in6p->in6p_outputopts;
704 
705 
706 	if (sin6) {
707 		/*
708 		 * Slightly different than v4 version in that we call
709 		 * in6_selectsrc and in6_pcbsetport to fill in the local
710 		 * address and port rather than in_pcbconnect. in_pcbconnect
711 		 * sets in6p_faddr which causes EISCONN below to be hit on
712 		 * subsequent sendto.
713 		 */
714 		if (sin6->sin6_port == 0) {
715 			error = EADDRNOTAVAIL;
716 			goto release;
717 		}
718 
719 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
720 			/* how about ::ffff:0.0.0.0 case? */
721 			error = EISCONN;
722 			goto release;
723 		}
724 
725 		faddr = &sin6->sin6_addr;
726 		fport = sin6->sin6_port; /* allow 0 port */
727 
728 		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
729 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
730 				/*
731 				 * I believe we should explicitly discard the
732 				 * packet when mapped addresses are disabled,
733 				 * rather than send the packet as an IPv6 one.
734 				 * If we chose the latter approach, the packet
735 				 * might be sent out on the wire based on the
736 				 * default route, the situation which we'd
737 				 * probably want to avoid.
738 				 * (20010421 jinmei@kame.net)
739 				 */
740 				error = EINVAL;
741 				goto release;
742 			}
743 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
744 			    !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
745 				/*
746 				 * when remote addr is an IPv4-mapped address,
747 				 * local addr should not be an IPv6 address,
748 				 * since you cannot determine how to map IPv6
749 				 * source address to IPv4.
750 				 */
751 				error = EINVAL;
752 				goto release;
753 			}
754 
755 			af = AF_INET;
756 		}
757 
758 		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
759 			struct psref psref;
760 			int bound = curlwp_bind();
761 
762 			error = in6_selectsrc(sin6, optp,
763 			    in6p->in6p_moptions,
764 			    &in6p->in6p_route,
765 			    &in6p->in6p_laddr, &oifp, &psref, &_laddr);
766 			/* XXX need error check? */
767 			if (oifp && scope_ambiguous &&
768 			    (error = in6_setscope(&sin6->sin6_addr,
769 			    oifp, NULL))) {
770 				if_put(oifp, &psref);
771 				curlwp_bindx(bound);
772 				goto release;
773 			}
774 			if_put(oifp, &psref);
775 			curlwp_bindx(bound);
776 			laddr = &_laddr;
777 		} else {
778 			/*
779 			 * XXX: freebsd[34] does not have in_selectsrc, but
780 			 * we can omit the whole part because freebsd4 calls
781 			 * udp_output() directly in this case, and thus we'll
782 			 * never see this path.
783 			 */
784 			if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
785 				struct sockaddr_in sin_dst;
786 				struct in_addr ina;
787 				struct in_ifaddr *ia4;
788 				struct psref _psref;
789 				int bound;
790 
791 				memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
792 				sockaddr_in_init(&sin_dst, &ina, 0);
793 				bound = curlwp_bind();
794 				ia4 = in_selectsrc(&sin_dst, &in6p->in6p_route,
795 				    in6p->in6p_socket->so_options, NULL,
796 				    &error, &_psref);
797 				if (ia4 == NULL) {
798 					curlwp_bindx(bound);
799 					if (error == 0)
800 						error = EADDRNOTAVAIL;
801 					goto release;
802 				}
803 				memset(&laddr_mapped, 0, sizeof(laddr_mapped));
804 				laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
805 				memcpy(&laddr_mapped.s6_addr[12],
806 				      &IA_SIN(ia4)->sin_addr,
807 				      sizeof(IA_SIN(ia4)->sin_addr));
808 				ia4_release(ia4, &_psref);
809 				curlwp_bindx(bound);
810 				laddr = &laddr_mapped;
811 			} else
812 			{
813 				laddr = &in6p->in6p_laddr;	/* XXX */
814 			}
815 		}
816 		if (laddr == NULL) {
817 			if (error == 0)
818 				error = EADDRNOTAVAIL;
819 			goto release;
820 		}
821 		if (in6p->in6p_lport == 0) {
822 			/*
823 			 * Craft a sockaddr_in6 for the local endpoint. Use the
824 			 * "any" as a base, set the address, and recover the
825 			 * scope.
826 			 */
827 			struct sockaddr_in6 lsin6 =
828 			    *((const struct sockaddr_in6 *)in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
829 			lsin6.sin6_addr = *laddr;
830 			error = sa6_recoverscope(&lsin6);
831 			if (error)
832 				goto release;
833 
834 			error = in6_pcbsetport(&lsin6, in6p, l);
835 
836 			if (error) {
837 				in6p->in6p_laddr = in6addr_any;
838 				goto release;
839 			}
840 		}
841 	} else {
842 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
843 			error = ENOTCONN;
844 			goto release;
845 		}
846 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
847 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
848 			{
849 				/*
850 				 * XXX: this case would happen when the
851 				 * application sets the V6ONLY flag after
852 				 * connecting the foreign address.
853 				 * Such applications should be fixed,
854 				 * so we bark here.
855 				 */
856 				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
857 				    "option was set for a connected socket\n");
858 				error = EINVAL;
859 				goto release;
860 			} else
861 				af = AF_INET;
862 		}
863 		laddr = &in6p->in6p_laddr;
864 		faddr = &in6p->in6p_faddr;
865 		fport = in6p->in6p_fport;
866 	}
867 
868 	if (af == AF_INET)
869 		hlen = sizeof(struct ip);
870 
871 	/*
872 	 * Calculate data length and get a mbuf
873 	 * for UDP and IP6 headers.
874 	 */
875 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
876 	if (m == NULL) {
877 		error = ENOBUFS;
878 		goto release;
879 	}
880 
881 	/*
882 	 * Stuff checksum and output datagram.
883 	 */
884 	udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
885 	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
886 	udp6->uh_dport = fport;
887 	if (plen <= 0xffff)
888 		udp6->uh_ulen = htons((u_int16_t)plen);
889 	else
890 		udp6->uh_ulen = 0;
891 	udp6->uh_sum = 0;
892 
893 	switch (af) {
894 	case AF_INET6:
895 		ip6 = mtod(m, struct ip6_hdr *);
896 		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
897 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
898 		ip6->ip6_vfc 	|= IPV6_VERSION;
899 #if 0		/* ip6_plen will be filled in ip6_output. */
900 		ip6->ip6_plen	= htons((u_int16_t)plen);
901 #endif
902 		ip6->ip6_nxt	= IPPROTO_UDP;
903 		ip6->ip6_hlim	= in6_selecthlim_rt(in6p);
904 		ip6->ip6_src	= *laddr;
905 		ip6->ip6_dst	= *faddr;
906 
907 		udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
908 		    htonl(plen), htonl(IPPROTO_UDP));
909 		m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
910 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
911 
912 		UDP6_STATINC(UDP6_STAT_OPACKETS);
913 		error = ip6_output(m, optp, &in6p->in6p_route, 0,
914 		    in6p->in6p_moptions, in6p, NULL);
915 		break;
916 	case AF_INET:
917 #ifdef INET
918 		/* can't transmit jumbogram over IPv4 */
919 		if (plen > 0xffff) {
920 			error = EMSGSIZE;
921 			goto release;
922 		}
923 
924 		ip = mtod(m, struct ip *);
925 		ui = (struct udpiphdr *)ip;
926 		memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
927 		ui->ui_pr = IPPROTO_UDP;
928 		ui->ui_len = htons(plen);
929 		memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
930 		ui->ui_ulen = ui->ui_len;
931 
932 		flags = (in6p->in6p_socket->so_options &
933 			 (SO_DONTROUTE | SO_BROADCAST));
934 		memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
935 
936 		udp6->uh_sum = in_cksum(m, hlen + plen);
937 		if (udp6->uh_sum == 0)
938 			udp6->uh_sum = 0xffff;
939 
940 		ip->ip_len = htons(hlen + plen);
941 		ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
942 		ip->ip_tos = 0;	/* XXX */
943 
944 		UDP_STATINC(UDP_STAT_OPACKETS);
945 		error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
946 		    in6p->in6p_v4moptions, NULL);
947 		break;
948 #else
949 		error = EAFNOSUPPORT;
950 		goto release;
951 #endif
952 	}
953 	goto releaseopt;
954 
955 release:
956 	m_freem(m);
957 
958 releaseopt:
959 	if (control) {
960 		if (optp == &opt)
961 			ip6_clearpktopts(&opt, -1);
962 		m_freem(control);
963 	}
964 	return (error);
965 }
966 
967 static int
968 udp6_attach(struct socket *so, int proto)
969 {
970 	struct in6pcb *in6p;
971 	int s, error;
972 
973 	KASSERT(sotoin6pcb(so) == NULL);
974 	sosetlock(so);
975 
976 	/*
977 	 * MAPPED_ADDR implementation spec:
978 	 *  Always attach for IPv6, and only when necessary for IPv4.
979 	 */
980 	s = splsoftnet();
981 	error = in6_pcballoc(so, &udbtable);
982 	splx(s);
983 	if (error) {
984 		return error;
985 	}
986 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
987 	if (error) {
988 		return error;
989 	}
990 	in6p = sotoin6pcb(so);
991 	in6p->in6p_cksum = -1;	/* just to be sure */
992 
993 	KASSERT(solocked(so));
994 	return 0;
995 }
996 
997 static void
998 udp6_detach(struct socket *so)
999 {
1000 	struct in6pcb *in6p = sotoin6pcb(so);
1001 	int s;
1002 
1003 	KASSERT(solocked(so));
1004 	KASSERT(in6p != NULL);
1005 
1006 	s = splsoftnet();
1007 	in6_pcbdetach(in6p);
1008 	splx(s);
1009 }
1010 
1011 static int
1012 udp6_accept(struct socket *so, struct sockaddr *nam)
1013 {
1014 	KASSERT(solocked(so));
1015 
1016 	return EOPNOTSUPP;
1017 }
1018 
1019 static int
1020 udp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
1021 {
1022 	struct in6pcb *in6p = sotoin6pcb(so);
1023 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1024 	int error = 0;
1025 	int s;
1026 
1027 	KASSERT(solocked(so));
1028 	KASSERT(in6p != NULL);
1029 
1030 	s = splsoftnet();
1031 	error = in6_pcbbind(in6p, sin6, l);
1032 	splx(s);
1033 	return error;
1034 }
1035 
1036 static int
1037 udp6_listen(struct socket *so, struct lwp *l)
1038 {
1039 	KASSERT(solocked(so));
1040 
1041 	return EOPNOTSUPP;
1042 }
1043 
1044 static int
1045 udp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
1046 {
1047 	struct in6pcb *in6p = sotoin6pcb(so);
1048 	int error = 0;
1049 	int s;
1050 
1051 	KASSERT(solocked(so));
1052 	KASSERT(in6p != NULL);
1053 
1054 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1055 		return EISCONN;
1056 	s = splsoftnet();
1057 	error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
1058 	splx(s);
1059 	if (error == 0)
1060 		soisconnected(so);
1061 
1062 	return error;
1063 }
1064 
1065 static int
1066 udp6_connect2(struct socket *so, struct socket *so2)
1067 {
1068 	KASSERT(solocked(so));
1069 
1070 	return EOPNOTSUPP;
1071 }
1072 
1073 static int
1074 udp6_disconnect(struct socket *so)
1075 {
1076 	struct in6pcb *in6p = sotoin6pcb(so);
1077 	int s;
1078 
1079 	KASSERT(solocked(so));
1080 	KASSERT(in6p != NULL);
1081 
1082 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1083 		return ENOTCONN;
1084 
1085 	s = splsoftnet();
1086 	in6_pcbdisconnect(in6p);
1087 	memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
1088 	splx(s);
1089 
1090 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
1091 	in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
1092 	return 0;
1093 }
1094 
1095 static int
1096 udp6_shutdown(struct socket *so)
1097 {
1098 	int s;
1099 
1100 	s = splsoftnet();
1101 	socantsendmore(so);
1102 	splx(s);
1103 
1104 	return 0;
1105 }
1106 
1107 static int
1108 udp6_abort(struct socket *so)
1109 {
1110 	int s;
1111 
1112 	KASSERT(solocked(so));
1113 	KASSERT(sotoin6pcb(so) != NULL);
1114 
1115 	s = splsoftnet();
1116 	soisdisconnected(so);
1117 	in6_pcbdetach(sotoin6pcb(so));
1118 	splx(s);
1119 
1120 	return 0;
1121 }
1122 
1123 static int
1124 udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
1125 {
1126 	/*
1127 	 * MAPPED_ADDR implementation info:
1128 	 *  Mapped addr support for PRU_CONTROL is not necessary.
1129 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
1130 	 *  and they don't associate any addr to their socket.  Then
1131 	 *  socket family is only hint about the PRU_CONTROL'ed address
1132 	 *  family, especially when getting addrs from kernel.
1133 	 *  So AF_INET socket need to be used to control AF_INET addrs,
1134 	 *  and AF_INET6 socket for AF_INET6 addrs.
1135 	 */
1136 	return in6_control(so, cmd, addr6, ifp);
1137 }
1138 
1139 static int
1140 udp6_stat(struct socket *so, struct stat *ub)
1141 {
1142 	KASSERT(solocked(so));
1143 
1144 	/* stat: don't bother with a blocksize */
1145 	return 0;
1146 }
1147 
1148 static int
1149 udp6_peeraddr(struct socket *so, struct sockaddr *nam)
1150 {
1151 	KASSERT(solocked(so));
1152 	KASSERT(sotoin6pcb(so) != NULL);
1153 	KASSERT(nam != NULL);
1154 
1155 	in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
1156 	return 0;
1157 }
1158 
1159 static int
1160 udp6_sockaddr(struct socket *so, struct sockaddr *nam)
1161 {
1162 	KASSERT(solocked(so));
1163 	KASSERT(sotoin6pcb(so) != NULL);
1164 	KASSERT(nam != NULL);
1165 
1166 	in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
1167 	return 0;
1168 }
1169 
1170 static int
1171 udp6_rcvd(struct socket *so, int flags, struct lwp *l)
1172 {
1173 	KASSERT(solocked(so));
1174 
1175 	return EOPNOTSUPP;
1176 }
1177 
1178 static int
1179 udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
1180 {
1181 	KASSERT(solocked(so));
1182 
1183 	return EOPNOTSUPP;
1184 }
1185 
1186 static int
1187 udp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
1188     struct mbuf *control, struct lwp *l)
1189 {
1190 	struct in6pcb *in6p = sotoin6pcb(so);
1191 	int error = 0;
1192 	int s;
1193 
1194 	KASSERT(solocked(so));
1195 	KASSERT(in6p != NULL);
1196 	KASSERT(m != NULL);
1197 
1198 	s = splsoftnet();
1199 	error = udp6_output(in6p, m, (struct sockaddr_in6 *)nam, control, l);
1200 	splx(s);
1201 
1202 	return error;
1203 }
1204 
1205 static int
1206 udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
1207 {
1208 	KASSERT(solocked(so));
1209 
1210 	if (m)
1211 		m_freem(m);
1212 	if (control)
1213 		m_freem(control);
1214 
1215 	return EOPNOTSUPP;
1216 }
1217 
1218 static int
1219 udp6_purgeif(struct socket *so, struct ifnet *ifp)
1220 {
1221 
1222 	mutex_enter(softnet_lock);
1223 	in6_pcbpurgeif0(&udbtable, ifp);
1224 #ifdef NET_MPSAFE
1225 	mutex_exit(softnet_lock);
1226 #endif
1227 	in6_purgeif(ifp);
1228 #ifdef NET_MPSAFE
1229 	mutex_enter(softnet_lock);
1230 #endif
1231 	in6_pcbpurgeif(&udbtable, ifp);
1232 	mutex_exit(softnet_lock);
1233 
1234 	return 0;
1235 }
1236 
1237 static int
1238 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
1239 {
1240 
1241 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
1242 }
1243 
1244 static void
1245 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
1246 {
1247 
1248 	sysctl_createv(clog, 0, NULL, NULL,
1249 		       CTLFLAG_PERMANENT,
1250 		       CTLTYPE_NODE, "inet6", NULL,
1251 		       NULL, 0, NULL, 0,
1252 		       CTL_NET, PF_INET6, CTL_EOL);
1253 	sysctl_createv(clog, 0, NULL, NULL,
1254 		       CTLFLAG_PERMANENT,
1255 		       CTLTYPE_NODE, "udp6",
1256 		       SYSCTL_DESCR("UDPv6 related settings"),
1257 		       NULL, 0, NULL, 0,
1258 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
1259 
1260 	sysctl_createv(clog, 0, NULL, NULL,
1261 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1262 		       CTLTYPE_INT, "sendspace",
1263 		       SYSCTL_DESCR("Default UDP send buffer size"),
1264 		       NULL, 0, &udp6_sendspace, 0,
1265 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
1266 		       CTL_EOL);
1267 	sysctl_createv(clog, 0, NULL, NULL,
1268 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1269 		       CTLTYPE_INT, "recvspace",
1270 		       SYSCTL_DESCR("Default UDP receive buffer size"),
1271 		       NULL, 0, &udp6_recvspace, 0,
1272 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
1273 		       CTL_EOL);
1274 	sysctl_createv(clog, 0, NULL, NULL,
1275 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1276 		       CTLTYPE_INT, "do_loopback_cksum",
1277 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
1278 		       NULL, 0, &udp_do_loopback_cksum, 0,
1279 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
1280 		       CTL_EOL);
1281 	sysctl_createv(clog, 0, NULL, NULL,
1282 		       CTLFLAG_PERMANENT,
1283 		       CTLTYPE_STRUCT, "pcblist",
1284 		       SYSCTL_DESCR("UDP protocol control block list"),
1285 		       sysctl_inpcblist, 0, &udbtable, 0,
1286 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
1287 		       CTL_EOL);
1288 	sysctl_createv(clog, 0, NULL, NULL,
1289 		       CTLFLAG_PERMANENT,
1290 		       CTLTYPE_STRUCT, "stats",
1291 		       SYSCTL_DESCR("UDPv6 statistics"),
1292 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
1293 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
1294 		       CTL_EOL);
1295 }
1296 
1297 void
1298 udp6_statinc(u_int stat)
1299 {
1300 
1301 	KASSERT(stat < UDP6_NSTATS);
1302 	UDP6_STATINC(stat);
1303 }
1304 
1305 PR_WRAP_USRREQS(udp6)
1306 #define	udp6_attach	udp6_attach_wrapper
1307 #define	udp6_detach	udp6_detach_wrapper
1308 #define	udp6_accept	udp6_accept_wrapper
1309 #define	udp6_bind	udp6_bind_wrapper
1310 #define	udp6_listen	udp6_listen_wrapper
1311 #define	udp6_connect	udp6_connect_wrapper
1312 #define	udp6_connect2	udp6_connect2_wrapper
1313 #define	udp6_disconnect	udp6_disconnect_wrapper
1314 #define	udp6_shutdown	udp6_shutdown_wrapper
1315 #define	udp6_abort	udp6_abort_wrapper
1316 #define	udp6_ioctl	udp6_ioctl_wrapper
1317 #define	udp6_stat	udp6_stat_wrapper
1318 #define	udp6_peeraddr	udp6_peeraddr_wrapper
1319 #define	udp6_sockaddr	udp6_sockaddr_wrapper
1320 #define	udp6_rcvd	udp6_rcvd_wrapper
1321 #define	udp6_recvoob	udp6_recvoob_wrapper
1322 #define	udp6_send	udp6_send_wrapper
1323 #define	udp6_sendoob	udp6_sendoob_wrapper
1324 #define	udp6_purgeif	udp6_purgeif_wrapper
1325 
1326 const struct pr_usrreqs udp6_usrreqs = {
1327 	.pr_attach	= udp6_attach,
1328 	.pr_detach	= udp6_detach,
1329 	.pr_accept	= udp6_accept,
1330 	.pr_bind	= udp6_bind,
1331 	.pr_listen	= udp6_listen,
1332 	.pr_connect	= udp6_connect,
1333 	.pr_connect2	= udp6_connect2,
1334 	.pr_disconnect	= udp6_disconnect,
1335 	.pr_shutdown	= udp6_shutdown,
1336 	.pr_abort	= udp6_abort,
1337 	.pr_ioctl	= udp6_ioctl,
1338 	.pr_stat	= udp6_stat,
1339 	.pr_peeraddr	= udp6_peeraddr,
1340 	.pr_sockaddr	= udp6_sockaddr,
1341 	.pr_rcvd	= udp6_rcvd,
1342 	.pr_recvoob	= udp6_recvoob,
1343 	.pr_send	= udp6_send,
1344 	.pr_sendoob	= udp6_sendoob,
1345 	.pr_purgeif	= udp6_purgeif,
1346 };
1347