Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.143
      1 /* $NetBSD: udp6_usrreq.c,v 1.143 2018/11/06 04:27:41 ozaki-r 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.143 2018/11/06 04:27:41 ozaki-r 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_len != sizeof(*sin6)) {
    672 			error = EINVAL;
    673 			goto release;
    674 		}
    675 		if (sin6->sin6_family != AF_INET6) {
    676 			error = EAFNOSUPPORT;
    677 			goto release;
    678 		}
    679 
    680 		/* protect *sin6 from overwrites */
    681 		tmp = *sin6;
    682 		sin6 = &tmp;
    683 
    684 		/*
    685 		 * Application should provide a proper zone ID or the use of
    686 		 * default zone IDs should be enabled.  Unfortunately, some
    687 		 * applications do not behave as it should, so we need a
    688 		 * workaround.  Even if an appropriate ID is not determined,
    689 		 * we'll see if we can determine the outgoing interface.  If we
    690 		 * can, determine the zone ID based on the interface below.
    691 		 */
    692 		if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
    693 			scope_ambiguous = 1;
    694 		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
    695 			goto release;
    696 	}
    697 
    698 	if (control) {
    699 		if (__predict_false(l == NULL)) {
    700 			panic("%s: control but no lwp", __func__);
    701 		}
    702 		if ((error = ip6_setpktopts(control, &opt,
    703 		    in6p->in6p_outputopts, l->l_cred, IPPROTO_UDP)) != 0)
    704 			goto release;
    705 		optp = &opt;
    706 	} else
    707 		optp = in6p->in6p_outputopts;
    708 
    709 
    710 	if (sin6) {
    711 		/*
    712 		 * Slightly different than v4 version in that we call
    713 		 * in6_selectsrc and in6_pcbsetport to fill in the local
    714 		 * address and port rather than in_pcbconnect. in_pcbconnect
    715 		 * sets in6p_faddr which causes EISCONN below to be hit on
    716 		 * subsequent sendto.
    717 		 */
    718 		if (sin6->sin6_port == 0) {
    719 			error = EADDRNOTAVAIL;
    720 			goto release;
    721 		}
    722 
    723 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    724 			/* how about ::ffff:0.0.0.0 case? */
    725 			error = EISCONN;
    726 			goto release;
    727 		}
    728 
    729 		faddr = &sin6->sin6_addr;
    730 		fport = sin6->sin6_port; /* allow 0 port */
    731 
    732 		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
    733 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
    734 				/*
    735 				 * I believe we should explicitly discard the
    736 				 * packet when mapped addresses are disabled,
    737 				 * rather than send the packet as an IPv6 one.
    738 				 * If we chose the latter approach, the packet
    739 				 * might be sent out on the wire based on the
    740 				 * default route, the situation which we'd
    741 				 * probably want to avoid.
    742 				 * (20010421 jinmei (at) kame.net)
    743 				 */
    744 				error = EINVAL;
    745 				goto release;
    746 			}
    747 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    748 			    !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
    749 				/*
    750 				 * when remote addr is an IPv4-mapped address,
    751 				 * local addr should not be an IPv6 address,
    752 				 * since you cannot determine how to map IPv6
    753 				 * source address to IPv4.
    754 				 */
    755 				error = EINVAL;
    756 				goto release;
    757 			}
    758 
    759 			af = AF_INET;
    760 		}
    761 
    762 		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
    763 			struct psref psref;
    764 			int bound = curlwp_bind();
    765 
    766 			error = in6_selectsrc(sin6, optp,
    767 			    in6p->in6p_moptions,
    768 			    &in6p->in6p_route,
    769 			    &in6p->in6p_laddr, &oifp, &psref, &_laddr);
    770 			if (error)
    771 				laddr = NULL;
    772 			else
    773 				laddr = &_laddr;
    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 		} else {
    784 			/*
    785 			 * XXX: freebsd[34] does not have in_selectsrc, but
    786 			 * we can omit the whole part because freebsd4 calls
    787 			 * udp_output() directly in this case, and thus we'll
    788 			 * never see this path.
    789 			 */
    790 			if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    791 				struct sockaddr_in sin_dst;
    792 				struct in_addr ina;
    793 				struct in_ifaddr *ia4;
    794 				struct psref _psref;
    795 				int bound;
    796 
    797 				memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
    798 				sockaddr_in_init(&sin_dst, &ina, 0);
    799 				bound = curlwp_bind();
    800 				ia4 = in_selectsrc(&sin_dst, &in6p->in6p_route,
    801 				    in6p->in6p_socket->so_options, NULL,
    802 				    &error, &_psref);
    803 				if (ia4 == NULL) {
    804 					curlwp_bindx(bound);
    805 					if (error == 0)
    806 						error = EADDRNOTAVAIL;
    807 					goto release;
    808 				}
    809 				memset(&laddr_mapped, 0, sizeof(laddr_mapped));
    810 				laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
    811 				memcpy(&laddr_mapped.s6_addr[12],
    812 				      &IA_SIN(ia4)->sin_addr,
    813 				      sizeof(IA_SIN(ia4)->sin_addr));
    814 				ia4_release(ia4, &_psref);
    815 				curlwp_bindx(bound);
    816 				laddr = &laddr_mapped;
    817 			} else
    818 			{
    819 				laddr = &in6p->in6p_laddr;	/* XXX */
    820 			}
    821 		}
    822 		if (laddr == NULL) {
    823 			if (error == 0)
    824 				error = EADDRNOTAVAIL;
    825 			goto release;
    826 		}
    827 		if (in6p->in6p_lport == 0) {
    828 			/*
    829 			 * Craft a sockaddr_in6 for the local endpoint. Use the
    830 			 * "any" as a base, set the address, and recover the
    831 			 * scope.
    832 			 */
    833 			struct sockaddr_in6 lsin6 =
    834 			    *((const struct sockaddr_in6 *)in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
    835 			lsin6.sin6_addr = *laddr;
    836 			error = sa6_recoverscope(&lsin6);
    837 			if (error)
    838 				goto release;
    839 
    840 			error = in6_pcbsetport(&lsin6, in6p, l);
    841 
    842 			if (error) {
    843 				in6p->in6p_laddr = in6addr_any;
    844 				goto release;
    845 			}
    846 		}
    847 	} else {
    848 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    849 			error = ENOTCONN;
    850 			goto release;
    851 		}
    852 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    853 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    854 			{
    855 				/*
    856 				 * XXX: this case would happen when the
    857 				 * application sets the V6ONLY flag after
    858 				 * connecting the foreign address.
    859 				 * Such applications should be fixed,
    860 				 * so we bark here.
    861 				 */
    862 				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
    863 				    "option was set for a connected socket\n");
    864 				error = EINVAL;
    865 				goto release;
    866 			} else
    867 				af = AF_INET;
    868 		}
    869 		laddr = &in6p->in6p_laddr;
    870 		faddr = &in6p->in6p_faddr;
    871 		fport = in6p->in6p_fport;
    872 	}
    873 
    874 	if (af == AF_INET)
    875 		hlen = sizeof(struct ip);
    876 
    877 	/*
    878 	 * Calculate data length and get a mbuf
    879 	 * for UDP and IP6 headers.
    880 	 */
    881 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
    882 	if (m == NULL) {
    883 		error = ENOBUFS;
    884 		goto release;
    885 	}
    886 
    887 	/*
    888 	 * Stuff checksum and output datagram.
    889 	 */
    890 	udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
    891 	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
    892 	udp6->uh_dport = fport;
    893 	if (plen <= 0xffff)
    894 		udp6->uh_ulen = htons((u_int16_t)plen);
    895 	else
    896 		udp6->uh_ulen = 0;
    897 	udp6->uh_sum = 0;
    898 
    899 	switch (af) {
    900 	case AF_INET6:
    901 		ip6 = mtod(m, struct ip6_hdr *);
    902 		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    903 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
    904 		ip6->ip6_vfc 	|= IPV6_VERSION;
    905 #if 0		/* ip6_plen will be filled in ip6_output. */
    906 		ip6->ip6_plen	= htons((u_int16_t)plen);
    907 #endif
    908 		ip6->ip6_nxt	= IPPROTO_UDP;
    909 		ip6->ip6_hlim	= in6_selecthlim_rt(in6p);
    910 		ip6->ip6_src	= *laddr;
    911 		ip6->ip6_dst	= *faddr;
    912 
    913 		udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
    914 		    htonl(plen), htonl(IPPROTO_UDP));
    915 		m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
    916 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
    917 
    918 		UDP6_STATINC(UDP6_STAT_OPACKETS);
    919 		error = ip6_output(m, optp, &in6p->in6p_route, 0,
    920 		    in6p->in6p_moptions, in6p, NULL);
    921 		break;
    922 	case AF_INET:
    923 #ifdef INET
    924 		/* can't transmit jumbogram over IPv4 */
    925 		if (plen > 0xffff) {
    926 			error = EMSGSIZE;
    927 			goto release;
    928 		}
    929 
    930 		ip = mtod(m, struct ip *);
    931 		ui = (struct udpiphdr *)ip;
    932 		memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
    933 		ui->ui_pr = IPPROTO_UDP;
    934 		ui->ui_len = htons(plen);
    935 		memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
    936 		ui->ui_ulen = ui->ui_len;
    937 
    938 		flags = (in6p->in6p_socket->so_options &
    939 			 (SO_DONTROUTE | SO_BROADCAST));
    940 		memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
    941 
    942 		udp6->uh_sum = in_cksum(m, hlen + plen);
    943 		if (udp6->uh_sum == 0)
    944 			udp6->uh_sum = 0xffff;
    945 
    946 		ip->ip_len = htons(hlen + plen);
    947 		ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
    948 		ip->ip_tos = 0;	/* XXX */
    949 
    950 		UDP_STATINC(UDP_STAT_OPACKETS);
    951 		error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
    952 		    in6p->in6p_v4moptions, NULL);
    953 		break;
    954 #else
    955 		error = EAFNOSUPPORT;
    956 		goto release;
    957 #endif
    958 	}
    959 	goto releaseopt;
    960 
    961 release:
    962 	m_freem(m);
    963 
    964 releaseopt:
    965 	if (control) {
    966 		if (optp == &opt)
    967 			ip6_clearpktopts(&opt, -1);
    968 		m_freem(control);
    969 	}
    970 	return (error);
    971 }
    972 
    973 static int
    974 udp6_attach(struct socket *so, int proto)
    975 {
    976 	struct in6pcb *in6p;
    977 	int s, error;
    978 
    979 	KASSERT(sotoin6pcb(so) == NULL);
    980 	sosetlock(so);
    981 
    982 	/*
    983 	 * MAPPED_ADDR implementation spec:
    984 	 *  Always attach for IPv6, and only when necessary for IPv4.
    985 	 */
    986 	s = splsoftnet();
    987 	error = in6_pcballoc(so, &udbtable);
    988 	splx(s);
    989 	if (error) {
    990 		return error;
    991 	}
    992 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
    993 	if (error) {
    994 		return error;
    995 	}
    996 	in6p = sotoin6pcb(so);
    997 	in6p->in6p_cksum = -1;	/* just to be sure */
    998 
    999 	KASSERT(solocked(so));
   1000 	return 0;
   1001 }
   1002 
   1003 static void
   1004 udp6_detach(struct socket *so)
   1005 {
   1006 	struct in6pcb *in6p = sotoin6pcb(so);
   1007 	int s;
   1008 
   1009 	KASSERT(solocked(so));
   1010 	KASSERT(in6p != NULL);
   1011 
   1012 	s = splsoftnet();
   1013 	in6_pcbdetach(in6p);
   1014 	splx(s);
   1015 }
   1016 
   1017 static int
   1018 udp6_accept(struct socket *so, struct sockaddr *nam)
   1019 {
   1020 	KASSERT(solocked(so));
   1021 
   1022 	return EOPNOTSUPP;
   1023 }
   1024 
   1025 static int
   1026 udp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
   1027 {
   1028 	struct in6pcb *in6p = sotoin6pcb(so);
   1029 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1030 	int error = 0;
   1031 	int s;
   1032 
   1033 	KASSERT(solocked(so));
   1034 	KASSERT(in6p != NULL);
   1035 
   1036 	s = splsoftnet();
   1037 	error = in6_pcbbind(in6p, sin6, l);
   1038 	splx(s);
   1039 	return error;
   1040 }
   1041 
   1042 static int
   1043 udp6_listen(struct socket *so, struct lwp *l)
   1044 {
   1045 	KASSERT(solocked(so));
   1046 
   1047 	return EOPNOTSUPP;
   1048 }
   1049 
   1050 static int
   1051 udp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
   1052 {
   1053 	struct in6pcb *in6p = sotoin6pcb(so);
   1054 	int error = 0;
   1055 	int s;
   1056 
   1057 	KASSERT(solocked(so));
   1058 	KASSERT(in6p != NULL);
   1059 
   1060 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1061 		return EISCONN;
   1062 	s = splsoftnet();
   1063 	error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
   1064 	splx(s);
   1065 	if (error == 0)
   1066 		soisconnected(so);
   1067 
   1068 	return error;
   1069 }
   1070 
   1071 static int
   1072 udp6_connect2(struct socket *so, struct socket *so2)
   1073 {
   1074 	KASSERT(solocked(so));
   1075 
   1076 	return EOPNOTSUPP;
   1077 }
   1078 
   1079 static int
   1080 udp6_disconnect(struct socket *so)
   1081 {
   1082 	struct in6pcb *in6p = sotoin6pcb(so);
   1083 	int s;
   1084 
   1085 	KASSERT(solocked(so));
   1086 	KASSERT(in6p != NULL);
   1087 
   1088 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1089 		return ENOTCONN;
   1090 
   1091 	s = splsoftnet();
   1092 	in6_pcbdisconnect(in6p);
   1093 	memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
   1094 	splx(s);
   1095 
   1096 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
   1097 	in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
   1098 	return 0;
   1099 }
   1100 
   1101 static int
   1102 udp6_shutdown(struct socket *so)
   1103 {
   1104 	int s;
   1105 
   1106 	s = splsoftnet();
   1107 	socantsendmore(so);
   1108 	splx(s);
   1109 
   1110 	return 0;
   1111 }
   1112 
   1113 static int
   1114 udp6_abort(struct socket *so)
   1115 {
   1116 	int s;
   1117 
   1118 	KASSERT(solocked(so));
   1119 	KASSERT(sotoin6pcb(so) != NULL);
   1120 
   1121 	s = splsoftnet();
   1122 	soisdisconnected(so);
   1123 	in6_pcbdetach(sotoin6pcb(so));
   1124 	splx(s);
   1125 
   1126 	return 0;
   1127 }
   1128 
   1129 static int
   1130 udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
   1131 {
   1132 	/*
   1133 	 * MAPPED_ADDR implementation info:
   1134 	 *  Mapped addr support for PRU_CONTROL is not necessary.
   1135 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
   1136 	 *  and they don't associate any addr to their socket.  Then
   1137 	 *  socket family is only hint about the PRU_CONTROL'ed address
   1138 	 *  family, especially when getting addrs from kernel.
   1139 	 *  So AF_INET socket need to be used to control AF_INET addrs,
   1140 	 *  and AF_INET6 socket for AF_INET6 addrs.
   1141 	 */
   1142 	return in6_control(so, cmd, addr6, ifp);
   1143 }
   1144 
   1145 static int
   1146 udp6_stat(struct socket *so, struct stat *ub)
   1147 {
   1148 	KASSERT(solocked(so));
   1149 
   1150 	/* stat: don't bother with a blocksize */
   1151 	return 0;
   1152 }
   1153 
   1154 static int
   1155 udp6_peeraddr(struct socket *so, struct sockaddr *nam)
   1156 {
   1157 	KASSERT(solocked(so));
   1158 	KASSERT(sotoin6pcb(so) != NULL);
   1159 	KASSERT(nam != NULL);
   1160 
   1161 	in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
   1162 	return 0;
   1163 }
   1164 
   1165 static int
   1166 udp6_sockaddr(struct socket *so, struct sockaddr *nam)
   1167 {
   1168 	KASSERT(solocked(so));
   1169 	KASSERT(sotoin6pcb(so) != NULL);
   1170 	KASSERT(nam != NULL);
   1171 
   1172 	in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
   1173 	return 0;
   1174 }
   1175 
   1176 static int
   1177 udp6_rcvd(struct socket *so, int flags, struct lwp *l)
   1178 {
   1179 	KASSERT(solocked(so));
   1180 
   1181 	return EOPNOTSUPP;
   1182 }
   1183 
   1184 static int
   1185 udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
   1186 {
   1187 	KASSERT(solocked(so));
   1188 
   1189 	return EOPNOTSUPP;
   1190 }
   1191 
   1192 static int
   1193 udp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
   1194     struct mbuf *control, struct lwp *l)
   1195 {
   1196 	struct in6pcb *in6p = sotoin6pcb(so);
   1197 	int error = 0;
   1198 	int s;
   1199 
   1200 	KASSERT(solocked(so));
   1201 	KASSERT(in6p != NULL);
   1202 	KASSERT(m != NULL);
   1203 
   1204 	s = splsoftnet();
   1205 	error = udp6_output(in6p, m, (struct sockaddr_in6 *)nam, control, l);
   1206 	splx(s);
   1207 
   1208 	return error;
   1209 }
   1210 
   1211 static int
   1212 udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
   1213 {
   1214 	KASSERT(solocked(so));
   1215 
   1216 	if (m)
   1217 		m_freem(m);
   1218 	if (control)
   1219 		m_freem(control);
   1220 
   1221 	return EOPNOTSUPP;
   1222 }
   1223 
   1224 static int
   1225 udp6_purgeif(struct socket *so, struct ifnet *ifp)
   1226 {
   1227 
   1228 	mutex_enter(softnet_lock);
   1229 	in6_pcbpurgeif0(&udbtable, ifp);
   1230 #ifdef NET_MPSAFE
   1231 	mutex_exit(softnet_lock);
   1232 #endif
   1233 	in6_purgeif(ifp);
   1234 #ifdef NET_MPSAFE
   1235 	mutex_enter(softnet_lock);
   1236 #endif
   1237 	in6_pcbpurgeif(&udbtable, ifp);
   1238 	mutex_exit(softnet_lock);
   1239 
   1240 	return 0;
   1241 }
   1242 
   1243 static int
   1244 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
   1245 {
   1246 
   1247 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
   1248 }
   1249 
   1250 static void
   1251 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
   1252 {
   1253 
   1254 	sysctl_createv(clog, 0, NULL, NULL,
   1255 		       CTLFLAG_PERMANENT,
   1256 		       CTLTYPE_NODE, "inet6", NULL,
   1257 		       NULL, 0, NULL, 0,
   1258 		       CTL_NET, PF_INET6, CTL_EOL);
   1259 	sysctl_createv(clog, 0, NULL, NULL,
   1260 		       CTLFLAG_PERMANENT,
   1261 		       CTLTYPE_NODE, "udp6",
   1262 		       SYSCTL_DESCR("UDPv6 related settings"),
   1263 		       NULL, 0, NULL, 0,
   1264 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
   1265 
   1266 	sysctl_createv(clog, 0, NULL, NULL,
   1267 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1268 		       CTLTYPE_INT, "sendspace",
   1269 		       SYSCTL_DESCR("Default UDP send buffer size"),
   1270 		       NULL, 0, &udp6_sendspace, 0,
   1271 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
   1272 		       CTL_EOL);
   1273 	sysctl_createv(clog, 0, NULL, NULL,
   1274 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1275 		       CTLTYPE_INT, "recvspace",
   1276 		       SYSCTL_DESCR("Default UDP receive buffer size"),
   1277 		       NULL, 0, &udp6_recvspace, 0,
   1278 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
   1279 		       CTL_EOL);
   1280 	sysctl_createv(clog, 0, NULL, NULL,
   1281 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1282 		       CTLTYPE_INT, "do_loopback_cksum",
   1283 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
   1284 		       NULL, 0, &udp_do_loopback_cksum, 0,
   1285 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
   1286 		       CTL_EOL);
   1287 	sysctl_createv(clog, 0, NULL, NULL,
   1288 		       CTLFLAG_PERMANENT,
   1289 		       CTLTYPE_STRUCT, "pcblist",
   1290 		       SYSCTL_DESCR("UDP protocol control block list"),
   1291 		       sysctl_inpcblist, 0, &udbtable, 0,
   1292 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
   1293 		       CTL_EOL);
   1294 	sysctl_createv(clog, 0, NULL, NULL,
   1295 		       CTLFLAG_PERMANENT,
   1296 		       CTLTYPE_STRUCT, "stats",
   1297 		       SYSCTL_DESCR("UDPv6 statistics"),
   1298 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
   1299 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
   1300 		       CTL_EOL);
   1301 }
   1302 
   1303 void
   1304 udp6_statinc(u_int stat)
   1305 {
   1306 
   1307 	KASSERT(stat < UDP6_NSTATS);
   1308 	UDP6_STATINC(stat);
   1309 }
   1310 
   1311 PR_WRAP_USRREQS(udp6)
   1312 #define	udp6_attach	udp6_attach_wrapper
   1313 #define	udp6_detach	udp6_detach_wrapper
   1314 #define	udp6_accept	udp6_accept_wrapper
   1315 #define	udp6_bind	udp6_bind_wrapper
   1316 #define	udp6_listen	udp6_listen_wrapper
   1317 #define	udp6_connect	udp6_connect_wrapper
   1318 #define	udp6_connect2	udp6_connect2_wrapper
   1319 #define	udp6_disconnect	udp6_disconnect_wrapper
   1320 #define	udp6_shutdown	udp6_shutdown_wrapper
   1321 #define	udp6_abort	udp6_abort_wrapper
   1322 #define	udp6_ioctl	udp6_ioctl_wrapper
   1323 #define	udp6_stat	udp6_stat_wrapper
   1324 #define	udp6_peeraddr	udp6_peeraddr_wrapper
   1325 #define	udp6_sockaddr	udp6_sockaddr_wrapper
   1326 #define	udp6_rcvd	udp6_rcvd_wrapper
   1327 #define	udp6_recvoob	udp6_recvoob_wrapper
   1328 #define	udp6_send	udp6_send_wrapper
   1329 #define	udp6_sendoob	udp6_sendoob_wrapper
   1330 #define	udp6_purgeif	udp6_purgeif_wrapper
   1331 
   1332 const struct pr_usrreqs udp6_usrreqs = {
   1333 	.pr_attach	= udp6_attach,
   1334 	.pr_detach	= udp6_detach,
   1335 	.pr_accept	= udp6_accept,
   1336 	.pr_bind	= udp6_bind,
   1337 	.pr_listen	= udp6_listen,
   1338 	.pr_connect	= udp6_connect,
   1339 	.pr_connect2	= udp6_connect2,
   1340 	.pr_disconnect	= udp6_disconnect,
   1341 	.pr_shutdown	= udp6_shutdown,
   1342 	.pr_abort	= udp6_abort,
   1343 	.pr_ioctl	= udp6_ioctl,
   1344 	.pr_stat	= udp6_stat,
   1345 	.pr_peeraddr	= udp6_peeraddr,
   1346 	.pr_sockaddr	= udp6_sockaddr,
   1347 	.pr_rcvd	= udp6_rcvd,
   1348 	.pr_recvoob	= udp6_recvoob,
   1349 	.pr_send	= udp6_send,
   1350 	.pr_sendoob	= udp6_sendoob,
   1351 	.pr_purgeif	= udp6_purgeif,
   1352 };
   1353