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