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