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