Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.109
      1 /*	$NetBSD: udp6_usrreq.c,v 1.109 2014/07/24 15:12:03 rtr Exp $	*/
      2 /*	$KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.109 2014/07/24 15:12:03 rtr Exp $");
     66 
     67 #include "opt_inet.h"
     68 #include "opt_inet_csum.h"
     69 
     70 #include <sys/param.h>
     71 #include <sys/mbuf.h>
     72 #include <sys/protosw.h>
     73 #include <sys/socket.h>
     74 #include <sys/socketvar.h>
     75 #include <sys/systm.h>
     76 #include <sys/proc.h>
     77 #include <sys/syslog.h>
     78 #include <sys/domain.h>
     79 #include <sys/sysctl.h>
     80 
     81 #include <net/if.h>
     82 #include <net/route.h>
     83 #include <net/if_types.h>
     84 
     85 #include <netinet/in.h>
     86 #include <netinet/in_var.h>
     87 #include <netinet/in_systm.h>
     88 #include <netinet/in_offload.h>
     89 #include <netinet/ip.h>
     90 #include <netinet/ip_var.h>
     91 #include <netinet/in_pcb.h>
     92 #include <netinet/udp.h>
     93 #include <netinet/udp_var.h>
     94 #include <netinet/udp_private.h>
     95 
     96 #include <netinet/ip6.h>
     97 #include <netinet/icmp6.h>
     98 #include <netinet6/ip6_var.h>
     99 #include <netinet6/ip6_private.h>
    100 #include <netinet6/in6_pcb.h>
    101 #include <netinet6/udp6_var.h>
    102 #include <netinet6/udp6_private.h>
    103 #include <netinet6/ip6protosw.h>
    104 #include <netinet6/scope6_var.h>
    105 
    106 #include "faith.h"
    107 #if defined(NFAITH) && NFAITH > 0
    108 #include <net/if_faith.h>
    109 #endif
    110 
    111 /*
    112  * UDP protocol implementation.
    113  * Per RFC 768, August, 1980.
    114  */
    115 
    116 extern struct inpcbtable udbtable;
    117 
    118 percpu_t *udp6stat_percpu;
    119 
    120 /* UDP on IP6 parameters */
    121 static int	udp6_sendspace = 9216;	/* really max datagram size */
    122 static int	udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
    123 					/* 40 1K datagrams */
    124 
    125 static	void udp6_notify(struct in6pcb *, int);
    126 static	void sysctl_net_inet6_udp6_setup(struct sysctllog **);
    127 
    128 #ifdef UDP_CSUM_COUNTERS
    129 #include <sys/device.h>
    130 struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    131     NULL, "udp6", "hwcsum bad");
    132 struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    133     NULL, "udp6", "hwcsum ok");
    134 struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    135     NULL, "udp6", "hwcsum data");
    136 struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    137     NULL, "udp6", "swcsum");
    138 
    139 EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
    140 EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
    141 EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
    142 EVCNT_ATTACH_STATIC(udp6_swcsum);
    143 
    144 #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    145 #else
    146 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
    147 #endif
    148 
    149 void
    150 udp6_init(void)
    151 {
    152 	sysctl_net_inet6_udp6_setup(NULL);
    153 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
    154 
    155 	udp_init_common();
    156 }
    157 
    158 /*
    159  * Notify a udp user of an asynchronous error;
    160  * just wake up so that he can collect error status.
    161  */
    162 static	void
    163 udp6_notify(struct in6pcb *in6p, int errno)
    164 {
    165 	in6p->in6p_socket->so_error = errno;
    166 	sorwakeup(in6p->in6p_socket);
    167 	sowwakeup(in6p->in6p_socket);
    168 }
    169 
    170 void *
    171 udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    172 {
    173 	struct udphdr uh;
    174 	struct ip6_hdr *ip6;
    175 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
    176 	struct mbuf *m;
    177 	int off;
    178 	void *cmdarg;
    179 	struct ip6ctlparam *ip6cp = NULL;
    180 	const struct sockaddr_in6 *sa6_src = NULL;
    181 	void (*notify)(struct in6pcb *, int) = udp6_notify;
    182 	struct udp_portonly {
    183 		u_int16_t uh_sport;
    184 		u_int16_t uh_dport;
    185 	} *uhp;
    186 
    187 	if (sa->sa_family != AF_INET6 ||
    188 	    sa->sa_len != sizeof(struct sockaddr_in6))
    189 		return NULL;
    190 
    191 	if ((unsigned)cmd >= PRC_NCMDS)
    192 		return NULL;
    193 	if (PRC_IS_REDIRECT(cmd))
    194 		notify = in6_rtchange, d = NULL;
    195 	else if (cmd == PRC_HOSTDEAD)
    196 		d = NULL;
    197 	else if (cmd == PRC_MSGSIZE) {
    198 		/* special code is present, see below */
    199 		notify = in6_rtchange;
    200 	}
    201 	else if (inet6ctlerrmap[cmd] == 0)
    202 		return NULL;
    203 
    204 	/* if the parameter is from icmp6, decode it. */
    205 	if (d != NULL) {
    206 		ip6cp = (struct ip6ctlparam *)d;
    207 		m = ip6cp->ip6c_m;
    208 		ip6 = ip6cp->ip6c_ip6;
    209 		off = ip6cp->ip6c_off;
    210 		cmdarg = ip6cp->ip6c_cmdarg;
    211 		sa6_src = ip6cp->ip6c_src;
    212 	} else {
    213 		m = NULL;
    214 		ip6 = NULL;
    215 		cmdarg = NULL;
    216 		sa6_src = &sa6_any;
    217 		off = 0;
    218 	}
    219 
    220 	if (ip6) {
    221 		/*
    222 		 * XXX: We assume that when IPV6 is non NULL,
    223 		 * M and OFF are valid.
    224 		 */
    225 
    226 		/* check if we can safely examine src and dst ports */
    227 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    228 			if (cmd == PRC_MSGSIZE)
    229 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    230 			return NULL;
    231 		}
    232 
    233 		memset(&uh, 0, sizeof(uh));
    234 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
    235 
    236 		if (cmd == PRC_MSGSIZE) {
    237 			int valid = 0;
    238 
    239 			/*
    240 			 * Check to see if we have a valid UDP socket
    241 			 * corresponding to the address in the ICMPv6 message
    242 			 * payload.
    243 			 */
    244 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
    245 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
    246 						  uh.uh_sport, 0, 0))
    247 				valid++;
    248 #if 0
    249 			/*
    250 			 * As the use of sendto(2) is fairly popular,
    251 			 * we may want to allow non-connected pcb too.
    252 			 * But it could be too weak against attacks...
    253 			 * We should at least check if the local address (= s)
    254 			 * is really ours.
    255 			 */
    256 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
    257 			    uh.uh_dport, 0))
    258 				valid++;
    259 #endif
    260 
    261 			/*
    262 			 * Depending on the value of "valid" and routing table
    263 			 * size (mtudisc_{hi,lo}wat), we will:
    264 			 * - recalculate the new MTU and create the
    265 			 *   corresponding routing entry, or
    266 			 * - ignore the MTU change notification.
    267 			 */
    268 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    269 
    270 			/*
    271 			 * regardless of if we called
    272 			 * icmp6_mtudisc_update(), we need to call
    273 			 * in6_pcbnotify(), to notify path MTU change
    274 			 * to the userland (RFC3542), because some
    275 			 * unconnected sockets may share the same
    276 			 * destination and want to know the path MTU.
    277 			 */
    278 		}
    279 
    280 		(void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
    281 		    (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
    282 		    notify);
    283 	} else {
    284 		(void) in6_pcbnotify(&udbtable, sa, 0,
    285 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    286 	}
    287 	return NULL;
    288 }
    289 
    290 int
    291 udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    292 {
    293 	int s;
    294 	int error = 0;
    295 	int family;
    296 
    297 	family = so->so_proto->pr_domain->dom_family;
    298 
    299 	s = splsoftnet();
    300 	switch (family) {
    301 #ifdef INET
    302 	case PF_INET:
    303 		if (sopt->sopt_level != IPPROTO_UDP) {
    304 			error = ip_ctloutput(op, so, sopt);
    305 			goto end;
    306 		}
    307 		break;
    308 #endif
    309 #ifdef INET6
    310 	case PF_INET6:
    311 		if (sopt->sopt_level != IPPROTO_UDP) {
    312 			error = ip6_ctloutput(op, so, sopt);
    313 			goto end;
    314 		}
    315 		break;
    316 #endif
    317 	default:
    318 		error = EAFNOSUPPORT;
    319 		goto end;
    320 	}
    321 	error = EINVAL;
    322 
    323 end:
    324 	splx(s);
    325 	return error;
    326 }
    327 
    328 static void
    329 udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
    330 	struct sockaddr *src, struct socket *so)
    331 {
    332 	struct mbuf *opts = NULL;
    333 	struct mbuf *n;
    334 	struct in6pcb *in6p = NULL;
    335 
    336 	if (!so)
    337 		return;
    338 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
    339 		return;
    340 	in6p = sotoin6pcb(so);
    341 
    342 #if defined(IPSEC)
    343 	/* check AH/ESP integrity. */
    344 	if (ipsec_used && so != NULL && ipsec6_in_reject_so(m, so)) {
    345 		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
    346 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
    347 			icmp6_error(n, ICMP6_DST_UNREACH,
    348 			    ICMP6_DST_UNREACH_ADMIN, 0);
    349 		return;
    350 	}
    351 #endif /*IPSEC*/
    352 
    353 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    354 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
    355 #ifdef SO_OTIMESTAMP
    356 		    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
    357 #endif
    358 		    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
    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 			so->so_rcv.sb_overflowed++;
    369 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
    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, (struct sockaddr *)src,
    457 				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, (struct sockaddr *)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->m_pkthdr.rcvif->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 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    575 	ulen = ntohs((u_short)uh->uh_ulen);
    576 	/*
    577 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    578 	 * iff payload length > 0xffff.
    579 	 */
    580 	if (ulen == 0 && plen > 0xffff)
    581 		ulen = plen;
    582 
    583 	if (plen != ulen) {
    584 		UDP6_STATINC(UDP6_STAT_BADLEN);
    585 		goto bad;
    586 	}
    587 
    588 	/* destination port of 0 is illegal, based on RFC768. */
    589 	if (uh->uh_dport == 0)
    590 		goto bad;
    591 
    592 	/* Be proactive about malicious use of IPv4 mapped address */
    593 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    594 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    595 		/* XXX stat */
    596 		goto bad;
    597 	}
    598 
    599 	/*
    600 	 * Checksum extended UDP header and data.  Maybe skip checksum
    601 	 * on loopback interfaces.
    602 	 */
    603 	if (udp6_input_checksum(m, uh, off, ulen))
    604 		goto bad;
    605 
    606 	/*
    607 	 * Construct source and dst sockaddrs.
    608 	 */
    609 	memset(&src, 0, sizeof(src));
    610 	src.sin6_family = AF_INET6;
    611 	src.sin6_len = sizeof(struct sockaddr_in6);
    612 	src.sin6_addr = ip6->ip6_src;
    613 	src.sin6_port = uh->uh_sport;
    614 	memset(&dst, 0, sizeof(dst));
    615 	dst.sin6_family = AF_INET6;
    616 	dst.sin6_len = sizeof(struct sockaddr_in6);
    617 	dst.sin6_addr = ip6->ip6_dst;
    618 	dst.sin6_port = uh->uh_dport;
    619 
    620 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
    621 		if (m->m_flags & M_MCAST) {
    622 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
    623 			goto bad;
    624 		}
    625 		UDP6_STATINC(UDP6_STAT_NOPORT);
    626 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    627 		m = NULL;
    628 	}
    629 
    630 bad:
    631 	if (m)
    632 		m_freem(m);
    633 	return IPPROTO_DONE;
    634 }
    635 
    636 static int
    637 udp6_attach(struct socket *so, int proto)
    638 {
    639 	struct in6pcb *in6p;
    640 	int s, error;
    641 
    642 	KASSERT(sotoin6pcb(so) == NULL);
    643 	sosetlock(so);
    644 
    645 	/*
    646 	 * MAPPED_ADDR implementation spec:
    647 	 *  Always attach for IPv6, and only when necessary for IPv4.
    648 	 */
    649 	s = splsoftnet();
    650 	error = in6_pcballoc(so, &udbtable);
    651 	splx(s);
    652 	if (error) {
    653 		return error;
    654 	}
    655 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
    656 	if (error) {
    657 		return error;
    658 	}
    659 	in6p = sotoin6pcb(so);
    660 	in6p->in6p_cksum = -1;	/* just to be sure */
    661 
    662 	KASSERT(solocked(so));
    663 	return 0;
    664 }
    665 
    666 static void
    667 udp6_detach(struct socket *so)
    668 {
    669 	struct in6pcb *in6p = sotoin6pcb(so);
    670 	int s;
    671 
    672 	KASSERT(solocked(so));
    673 	KASSERT(in6p != NULL);
    674 
    675 	s = splsoftnet();
    676 	in6_pcbdetach(in6p);
    677 	splx(s);
    678 }
    679 
    680 static int
    681 udp6_accept(struct socket *so, struct mbuf *nam)
    682 {
    683 	KASSERT(solocked(so));
    684 
    685 	return EOPNOTSUPP;
    686 }
    687 
    688 static int
    689 udp6_bind(struct socket *so, struct mbuf *nam)
    690 {
    691 	struct in6pcb *in6p = sotoin6pcb(so);
    692 	int error = 0;
    693 	int s;
    694 
    695 	KASSERT(solocked(so));
    696 	KASSERT(in6p != NULL);
    697 
    698 	s = splsoftnet();
    699 	error = in6_pcbbind(in6p, nam);
    700 	splx(s);
    701 	return error;
    702 }
    703 
    704 static int
    705 udp6_listen(struct socket *so)
    706 {
    707 	KASSERT(solocked(so));
    708 
    709 	return EOPNOTSUPP;
    710 }
    711 
    712 static int
    713 udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
    714 {
    715 	/*
    716 	 * MAPPED_ADDR implementation info:
    717 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    718 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    719 	 *  and they don't associate any addr to their socket.  Then
    720 	 *  socket family is only hint about the PRU_CONTROL'ed address
    721 	 *  family, especially when getting addrs from kernel.
    722 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    723 	 *  and AF_INET6 socket for AF_INET6 addrs.
    724 	 */
    725 	return in6_control(so, cmd, addr6, ifp);
    726 }
    727 
    728 static int
    729 udp6_stat(struct socket *so, struct stat *ub)
    730 {
    731 	KASSERT(solocked(so));
    732 
    733 	/* stat: don't bother with a blocksize */
    734 	return 0;
    735 }
    736 
    737 static int
    738 udp6_peeraddr(struct socket *so, struct mbuf *nam)
    739 {
    740 	KASSERT(solocked(so));
    741 	KASSERT(sotoin6pcb(so) != NULL);
    742 	KASSERT(nam != NULL);
    743 
    744 	in6_setpeeraddr(sotoin6pcb(so), nam);
    745 	return 0;
    746 }
    747 
    748 static int
    749 udp6_sockaddr(struct socket *so, struct mbuf *nam)
    750 {
    751 	KASSERT(solocked(so));
    752 	KASSERT(sotoin6pcb(so) != NULL);
    753 	KASSERT(nam != NULL);
    754 
    755 	in6_setsockaddr(sotoin6pcb(so), nam);
    756 	return 0;
    757 }
    758 
    759 static int
    760 udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
    761 {
    762 	KASSERT(solocked(so));
    763 
    764 	return EOPNOTSUPP;
    765 }
    766 
    767 static int
    768 udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    769 {
    770 	KASSERT(solocked(so));
    771 
    772 	if (m)
    773 		m_freem(m);
    774 	if (control)
    775 		m_freem(control);
    776 
    777 	return EOPNOTSUPP;
    778 }
    779 
    780 int
    781 udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
    782     struct mbuf *control, struct lwp *l)
    783 {
    784 	struct in6pcb *in6p = sotoin6pcb(so);
    785 	int s, error = 0;
    786 
    787 	KASSERT(req != PRU_ATTACH);
    788 	KASSERT(req != PRU_DETACH);
    789 	KASSERT(req != PRU_ACCEPT);
    790 	KASSERT(req != PRU_BIND);
    791 	KASSERT(req != PRU_LISTEN);
    792 	KASSERT(req != PRU_CONTROL);
    793 	KASSERT(req != PRU_SENSE);
    794 	KASSERT(req != PRU_PEERADDR);
    795 	KASSERT(req != PRU_SOCKADDR);
    796 	KASSERT(req != PRU_RCVOOB);
    797 	KASSERT(req != PRU_SENDOOB);
    798 
    799 	if (req == PRU_PURGEIF) {
    800 		mutex_enter(softnet_lock);
    801 		in6_pcbpurgeif0(&udbtable, (struct ifnet *)control);
    802 		in6_purgeif((struct ifnet *)control);
    803 		in6_pcbpurgeif(&udbtable, (struct ifnet *)control);
    804 		mutex_exit(softnet_lock);
    805 		return 0;
    806 	}
    807 	if (in6p == NULL) {
    808 		error = EINVAL;
    809 		goto release;
    810 	}
    811 
    812 	switch (req) {
    813 
    814 	case PRU_CONNECT:
    815 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    816 			error = EISCONN;
    817 			break;
    818 		}
    819 		s = splsoftnet();
    820 		error = in6_pcbconnect(in6p, addr6, l);
    821 		splx(s);
    822 		if (error == 0)
    823 			soisconnected(so);
    824 		break;
    825 
    826 	case PRU_DISCONNECT:
    827 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    828 			error = ENOTCONN;
    829 			break;
    830 		}
    831 		s = splsoftnet();
    832 		in6_pcbdisconnect(in6p);
    833 		memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
    834 		splx(s);
    835 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    836 		in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
    837 		break;
    838 
    839 	case PRU_SHUTDOWN:
    840 		socantsendmore(so);
    841 		break;
    842 
    843 	case PRU_SEND:
    844 		s = splsoftnet();
    845 		error = udp6_output(in6p, m, addr6, control, l);
    846 		splx(s);
    847 		return error;
    848 
    849 	case PRU_ABORT:
    850 		soisdisconnected(so);
    851 		in6_pcbdetach(in6p);
    852 		break;
    853 
    854 	case PRU_CONNECT2:
    855 	case PRU_FASTTIMO:
    856 	case PRU_SLOWTIMO:
    857 	case PRU_PROTORCV:
    858 	case PRU_PROTOSEND:
    859 		error = EOPNOTSUPP;
    860 		break;
    861 
    862 	case PRU_RCVD:
    863 		return EOPNOTSUPP;	/* do not free mbuf's */
    864 
    865 	default:
    866 		panic("udp6_usrreq");
    867 	}
    868 
    869 release:
    870 	if (control != NULL)
    871 		m_freem(control);
    872 	if (m != NULL)
    873 		m_freem(m);
    874 	return error;
    875 }
    876 
    877 static int
    878 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
    879 {
    880 
    881 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
    882 }
    883 
    884 static void
    885 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
    886 {
    887 
    888 	sysctl_createv(clog, 0, NULL, NULL,
    889 		       CTLFLAG_PERMANENT,
    890 		       CTLTYPE_NODE, "inet6", NULL,
    891 		       NULL, 0, NULL, 0,
    892 		       CTL_NET, PF_INET6, CTL_EOL);
    893 	sysctl_createv(clog, 0, NULL, NULL,
    894 		       CTLFLAG_PERMANENT,
    895 		       CTLTYPE_NODE, "udp6",
    896 		       SYSCTL_DESCR("UDPv6 related settings"),
    897 		       NULL, 0, NULL, 0,
    898 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
    899 
    900 	sysctl_createv(clog, 0, NULL, NULL,
    901 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    902 		       CTLTYPE_INT, "sendspace",
    903 		       SYSCTL_DESCR("Default UDP send buffer size"),
    904 		       NULL, 0, &udp6_sendspace, 0,
    905 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
    906 		       CTL_EOL);
    907 	sysctl_createv(clog, 0, NULL, NULL,
    908 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    909 		       CTLTYPE_INT, "recvspace",
    910 		       SYSCTL_DESCR("Default UDP receive buffer size"),
    911 		       NULL, 0, &udp6_recvspace, 0,
    912 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
    913 		       CTL_EOL);
    914 	sysctl_createv(clog, 0, NULL, NULL,
    915 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    916 		       CTLTYPE_INT, "do_loopback_cksum",
    917 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
    918 		       NULL, 0, &udp_do_loopback_cksum, 0,
    919 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
    920 		       CTL_EOL);
    921 	sysctl_createv(clog, 0, NULL, NULL,
    922 		       CTLFLAG_PERMANENT,
    923 		       CTLTYPE_STRUCT, "pcblist",
    924 		       SYSCTL_DESCR("UDP protocol control block list"),
    925 		       sysctl_inpcblist, 0, &udbtable, 0,
    926 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
    927 		       CTL_EOL);
    928 	sysctl_createv(clog, 0, NULL, NULL,
    929 		       CTLFLAG_PERMANENT,
    930 		       CTLTYPE_STRUCT, "stats",
    931 		       SYSCTL_DESCR("UDPv6 statistics"),
    932 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
    933 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
    934 		       CTL_EOL);
    935 }
    936 
    937 void
    938 udp6_statinc(u_int stat)
    939 {
    940 
    941 	KASSERT(stat < UDP6_NSTATS);
    942 	UDP6_STATINC(stat);
    943 }
    944 
    945 PR_WRAP_USRREQS(udp6)
    946 #define	udp6_attach	udp6_attach_wrapper
    947 #define	udp6_detach	udp6_detach_wrapper
    948 #define	udp6_accept	udp6_accept_wrapper
    949 #define	udp6_bind	udp6_bind_wrapper
    950 #define	udp6_listen	udp6_listen_wrapper
    951 #define	udp6_ioctl	udp6_ioctl_wrapper
    952 #define	udp6_stat	udp6_stat_wrapper
    953 #define	udp6_peeraddr	udp6_peeraddr_wrapper
    954 #define	udp6_sockaddr	udp6_sockaddr_wrapper
    955 #define	udp6_recvoob	udp6_recvoob_wrapper
    956 #define	udp6_sendoob	udp6_sendoob_wrapper
    957 #define	udp6_usrreq	udp6_usrreq_wrapper
    958 
    959 const struct pr_usrreqs udp6_usrreqs = {
    960 	.pr_attach	= udp6_attach,
    961 	.pr_detach	= udp6_detach,
    962 	.pr_accept	= udp6_accept,
    963 	.pr_bind	= udp6_bind,
    964 	.pr_listen	= udp6_listen,
    965 	.pr_ioctl	= udp6_ioctl,
    966 	.pr_stat	= udp6_stat,
    967 	.pr_peeraddr	= udp6_peeraddr,
    968 	.pr_sockaddr	= udp6_sockaddr,
    969 	.pr_recvoob	= udp6_recvoob,
    970 	.pr_sendoob	= udp6_sendoob,
    971 	.pr_generic	= udp6_usrreq,
    972 };
    973