Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.3
      1 /*	$NetBSD: udp6_usrreq.c,v 1.3 1999/07/03 21:30:20 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
     65  */
     66 
     67 #include <sys/param.h>
     68 #include <sys/malloc.h>
     69 #include <sys/mbuf.h>
     70 #include <sys/protosw.h>
     71 #include <sys/socket.h>
     72 #include <sys/socketvar.h>
     73 #include <sys/errno.h>
     74 #include <sys/stat.h>
     75 #include <sys/systm.h>
     76 #ifdef __NetBSD__
     77 #include <sys/proc.h>
     78 #endif
     79 
     80 #include <net/if.h>
     81 #include <net/route.h>
     82 #include <net/if_types.h>
     83 
     84 #include <netinet/in.h>
     85 #include <netinet/in_var.h>
     86 #include <netinet6/in6_systm.h>
     87 #include <netinet6/ip6.h>
     88 #include <netinet6/in6_pcb.h>
     89 #include <netinet6/ip6_var.h>
     90 #include <netinet6/icmp6.h>
     91 #include <netinet6/udp6.h>
     92 #include <netinet6/udp6_var.h>
     93 
     94 #ifdef IPSEC
     95 #include <netinet6/ipsec.h>
     96 #endif /*IPSEC*/
     97 
     98 #include "faith.h"
     99 
    100 /*
    101  * UDP protocol inplementation.
    102  * Per RFC 768, August, 1980.
    103  */
    104 
    105 struct	in6pcb *udp6_last_in6pcb = &udb6;
    106 
    107 static	int in6_mcmatch __P((struct in6pcb *, struct in6_addr *, struct ifnet *));
    108 static	void udp6_detach __P((struct in6pcb *));
    109 static	void udp6_notify __P((struct in6pcb *, int));
    110 
    111 void
    112 udp6_init()
    113 {
    114 	udb6.in6p_next = udb6.in6p_prev = &udb6;
    115 }
    116 
    117 static int
    118 in6_mcmatch(in6p, ia6, ifp)
    119 	struct in6pcb *in6p;
    120 	register struct in6_addr *ia6;
    121 	struct ifnet *ifp;
    122 {
    123 	struct ip6_moptions *im6o = in6p->in6p_moptions;
    124 	struct in6_multi_mship *imm;
    125 
    126 	if (im6o == NULL)
    127 		return 0;
    128 
    129 	for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
    130 	     imm = imm->i6mm_chain.le_next) {
    131 		if ((ifp == NULL ||
    132 		     imm->i6mm_maddr->in6m_ifp == ifp) &&
    133 		    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
    134 				       ia6))
    135 			return 1;
    136 	}
    137 	return 0;
    138 }
    139 
    140 int
    141 udp6_input(mp, offp, proto)
    142 	struct mbuf **mp;
    143 	int *offp, proto;
    144 {
    145 	struct mbuf *m = *mp;
    146 	register struct ip6_hdr *ip6;
    147 	register struct udphdr *uh;
    148 	register struct in6pcb *in6p;
    149 	struct	mbuf *opts = 0;
    150 	int off = *offp;
    151 	int plen, ulen;
    152 	struct sockaddr_in6 udp_in6;
    153 
    154 #if defined(NFAITH) && 0 < NFAITH
    155 	if (m->m_pkthdr.rcvif) {
    156 		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
    157 			/* send icmp6 host unreach? */
    158 			m_freem(m);
    159 			return IPPROTO_DONE;
    160 		}
    161 	}
    162 #endif
    163 	udp6stat.udp6s_ipackets++;
    164 
    165 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
    166 
    167 	ip6 = mtod(m, struct ip6_hdr *);
    168 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
    169 	uh = (struct udphdr *)((caddr_t)ip6 + off);
    170 	ulen = ntohs((u_short)uh->uh_ulen);
    171 
    172 	if (plen != ulen) {
    173 		udp6stat.udp6s_badlen++;
    174 		goto bad;
    175 	}
    176 
    177 	/*
    178 	 * Checksum extended UDP header and data.
    179 	 */
    180 	if (uh->uh_sum == 0)
    181 		udp6stat.udp6s_nosum++;
    182 	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
    183 		udp6stat.udp6s_badsum++;
    184 		goto bad;
    185 	}
    186 
    187 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    188 		struct	in6pcb *last;
    189 
    190 		/*
    191 		 * Deliver a multicast datagram to all sockets
    192 		 * for which the local and remote addresses and ports match
    193 		 * those of the incoming datagram.  This allows more than
    194 		 * one process to receive multicasts on the same port.
    195 		 * (This really ought to be done for unicast datagrams as
    196 		 * well, but that would cause problems with existing
    197 		 * applications that open both address-specific sockets and
    198 		 * a wildcard socket listening to the same port -- they would
    199 		 * end up receiving duplicates of every unicast datagram.
    200 		 * Those applications open the multiple sockets to overcome an
    201 		 * inadequacy of the UDP socket interface, but for backwards
    202 		 * compatibility we avoid the problem here rather than
    203 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    204 		 */
    205 
    206 		/*
    207 		 * In a case that laddr should be set to the link-local
    208 		 * address (this happens in RIPng), the multicast address
    209 		 * specified in the received packet does not match with
    210 		 * laddr. To cure this situation, the matching is relaxed
    211 		 * if the receiving interface is the same as one specified
    212 		 * in the socket and if the destination multicast address
    213 		 * matches one of the multicast groups specified in the socket.
    214 		 */
    215 
    216 		/*
    217 		 * Construct sockaddr format source address.
    218 		 */
    219 		bzero(&udp_in6, sizeof(udp_in6));
    220 		udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    221 		udp_in6.sin6_family = AF_INET6;
    222 		udp_in6.sin6_port = uh->uh_sport;
    223 		udp_in6.sin6_addr = ip6->ip6_src;
    224 		if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
    225 			udp_in6.sin6_addr.s6_addr16[1] = 0;
    226 		if (m->m_pkthdr.rcvif) {
    227 			if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) {
    228 				udp_in6.sin6_scope_id =
    229 					m->m_pkthdr.rcvif->if_index;
    230 			} else
    231 				udp_in6.sin6_scope_id = 0;
    232 		} else
    233 			udp_in6.sin6_scope_id = 0;
    234 		/*
    235 		 * KAME note: usually we drop udphdr from mbuf here.
    236 		 * We need udphdr for IPsec processing so we do that later.
    237 		 */
    238 
    239 		/*
    240 		 * Locate pcb(s) for datagram.
    241 		 * (Algorithm copied from raw_intr().)
    242 		 */
    243 		last = NULL;
    244 		for (in6p = udb6.in6p_next;
    245 		     in6p != &udb6;
    246 		     in6p = in6p->in6p_next) {
    247 			if (in6p->in6p_lport != uh->uh_dport)
    248 				continue;
    249 			if (!IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
    250 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    251 							&ip6->ip6_dst) &&
    252 				    !in6_mcmatch(in6p, &ip6->ip6_dst,
    253 						 m->m_pkthdr.rcvif))
    254 					continue;
    255 			}
    256 			if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
    257 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    258 							&ip6->ip6_src) ||
    259 				   in6p->in6p_fport != uh->uh_sport)
    260 					continue;
    261 			}
    262 
    263 			if (last != NULL) {
    264 				struct	mbuf *n;
    265 
    266 #ifdef IPSEC
    267 				/*
    268 				 * Check AH/ESP integrity.
    269 				 */
    270 				if (last != NULL && ipsec6_in_reject(m, last)) {
    271 					ipsec6stat.in_polvio++;
    272 					/* do not inject data into pcb */
    273 				} else
    274 #endif /*IPSEC*/
    275 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    276 					/*
    277 					 * KAME NOTE: do not
    278 					 * m_copy(m, offset, ...) above.
    279 					 * sbappendaddr() expects M_PKTHDR,
    280 					 * and m_copy() will copy M_PKTHDR
    281 					 * only if offset is 0.
    282 					 */
    283 					if (last->in6p_flags & IN6P_CONTROLOPTS) {
    284 						ip6_savecontrol(last, &opts,
    285 								ip6, n);
    286 					}
    287 
    288 					m_adj(m, off + sizeof(struct udphdr));
    289 					if (sbappendaddr(&last->in6p_socket->so_rcv,
    290 							(struct sockaddr *)&udp_in6,
    291 							n, opts) == 0) {
    292 						m_freem(n);
    293 						if (opts)
    294 							m_freem(opts);
    295 						udp6stat.udp6s_fullsock++;
    296 					} else
    297 						sorwakeup(last->in6p_socket);
    298 					opts = 0;
    299 				}
    300 			}
    301 			last = in6p;
    302 			/*
    303 			 * Don't look for additional matches if this one does
    304 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    305 			 * socket options set.  This heuristic avoids searching
    306 			 * through all pcbs in the common case of a non-shared
    307 			 * port.  It assumes that an application will never
    308 			 * clear these options after setting them.
    309 			 */
    310 			if ((last->in6p_socket->so_options &
    311 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    312 				break;
    313 		}
    314 
    315 		if (last == NULL) {
    316 			/*
    317 			 * No matching pcb found; discard datagram.
    318 			 * (No need to send an ICMP Port Unreachable
    319 			 * for a broadcast or multicast datgram.)
    320 			 */
    321 			udp6stat.udp6s_noport++;
    322 			udp6stat.udp6s_noportmcast++;
    323 			goto bad;
    324 		}
    325 #ifdef IPSEC
    326 		/*
    327 		 * Check AH/ESP integrity.
    328 		 */
    329 		if (last != NULL && ipsec6_in_reject(m, last)) {
    330 			ipsec6stat.in_polvio++;
    331 			goto bad;
    332 		}
    333 #endif /*IPSEC*/
    334 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    335 			ip6_savecontrol(last, &opts, ip6, m);
    336 
    337 		m_adj(m, off + sizeof(struct udphdr));
    338 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    339 				(struct sockaddr *)&udp_in6,
    340 				m, opts) == 0) {
    341 			udp6stat.udp6s_fullsock++;
    342 			goto bad;
    343 		}
    344 		sorwakeup(last->in6p_socket);
    345 		return IPPROTO_DONE;
    346 	}
    347 	/*
    348 	 * Locate pcb for datagram.
    349 	 */
    350 	in6p = udp6_last_in6pcb;
    351 	if (in6p->in6p_lport != uh->uh_dport ||
    352 	   in6p->in6p_fport != uh->uh_sport ||
    353 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) ||
    354 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
    355 		in6p = in6_pcblookup(&udb6,
    356 				     &ip6->ip6_src, uh->uh_sport,
    357 				     &ip6->ip6_dst, uh->uh_dport,
    358 				     IN6PLOOKUP_WILDCARD);
    359 		if (in6p)
    360 			udp6_last_in6pcb = in6p;
    361 		udp6stat.udp6ps_pcbcachemiss++;
    362 	}
    363 	if (in6p == 0) {
    364 		udp6stat.udp6s_noport++;
    365 		if (m->m_flags & M_MCAST) {
    366 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
    367 			udp6stat.udp6s_noportmcast++;
    368 			goto bad;
    369 		}
    370 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    371 		return IPPROTO_DONE;
    372 	}
    373 #ifdef IPSEC
    374 	/*
    375 	 * Check AH/ESP integrity.
    376 	 */
    377 	if (in6p != NULL && ipsec6_in_reject(m, in6p)) {
    378 		ipsec6stat.in_polvio++;
    379 		goto bad;
    380 	}
    381 #endif /*IPSEC*/
    382 
    383 	/*
    384 	 * Construct sockaddr format source address.
    385 	 * Stuff source address and datagram in user buffer.
    386 	 */
    387 	bzero(&udp_in6, sizeof(udp_in6));
    388 	udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    389 	udp_in6.sin6_family = AF_INET6;
    390 	udp_in6.sin6_port = uh->uh_sport;
    391 	udp_in6.sin6_addr = ip6->ip6_src;
    392 	if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
    393 		udp_in6.sin6_addr.s6_addr16[1] = 0;
    394 	if (m->m_pkthdr.rcvif) {
    395 		if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
    396 			udp_in6.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
    397 		else
    398 			udp_in6.sin6_scope_id = 0;
    399 	} else
    400 		udp_in6.sin6_scope_id = 0;
    401 	if (in6p->in6p_flags & IN6P_CONTROLOPTS)
    402 		ip6_savecontrol(in6p, &opts, ip6, m);
    403 
    404 	m_adj(m, off + sizeof(struct udphdr));
    405 	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
    406 			(struct sockaddr *)&udp_in6,
    407 			m, opts) == 0) {
    408 		udp6stat.udp6s_fullsock++;
    409 		goto bad;
    410 	}
    411 	sorwakeup(in6p->in6p_socket);
    412 	return IPPROTO_DONE;
    413 bad:
    414 	if (m)
    415 		m_freem(m);
    416 	if (opts)
    417 		m_freem(opts);
    418 	return IPPROTO_DONE;
    419 }
    420 
    421 /*
    422  * Notify a udp user of an asynchronous error;
    423  * just wake up so tat he can collect error status.
    424  */
    425 static	void
    426 udp6_notify(in6p, errno)
    427 	register struct in6pcb *in6p;
    428 	int errno;
    429 {
    430 	in6p->in6p_socket->so_error = errno;
    431 	sorwakeup(in6p->in6p_socket);
    432 	sowwakeup(in6p->in6p_socket);
    433 }
    434 
    435 void
    436 udp6_ctlinput(cmd, sa, ip6, m, off)
    437 	int cmd;
    438 	struct sockaddr *sa;
    439 	register struct ip6_hdr *ip6;
    440 	struct mbuf *m;
    441 	int off;
    442 {
    443 	register struct udphdr *uhp;
    444 	struct udphdr uh;
    445 
    446 #if 0
    447 	if (cmd == PRC_IFNEWADDR)
    448 		in6_mrejoin(&udb6);
    449 	else
    450 #endif
    451 	if (!PRC_IS_REDIRECT(cmd) &&
    452 	    ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
    453 		return;
    454 	if (ip6) {
    455 		/*
    456 		 * XXX: We assume that when IPV6 is non NULL,
    457 		 * M and OFF are valid.
    458 		 */
    459 		if (m->m_len < off + sizeof(uh)) {
    460 			/*
    461 			 * this should be rare case,
    462 			 * so we compromise on this copy...
    463 			 */
    464 			m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
    465 			uhp = &uh;
    466 		} else
    467 			uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
    468 		(void) in6_pcbnotify(&udb6, sa, uhp->uh_dport, &ip6->ip6_src,
    469 					uhp->uh_sport, cmd, udp6_notify);
    470 	} else {
    471 		(void) in6_pcbnotify(&udb6, sa, 0, &zeroin6_addr, 0, cmd,
    472 					udp6_notify);
    473 	}
    474 }
    475 
    476 int
    477 udp6_output(in6p, m, addr6, control)
    478 	register struct in6pcb *in6p;
    479 	register struct mbuf *m;
    480 	struct mbuf *addr6, *control;
    481 {
    482 	register int ulen = m->m_pkthdr.len;
    483 	int plen = sizeof(struct udphdr) + ulen;
    484 	struct ip6_hdr *ip6;
    485 	struct udphdr *udp6;
    486 	struct	in6_addr laddr6;
    487 	int s = 0, error = 0;
    488 	struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
    489 	int priv = 0;
    490 	struct proc *p = curproc;	/* XXX */
    491 
    492 	if (p && !suser(p->p_ucred, &p->p_acflag))
    493 		priv = 1;
    494 	if (control) {
    495 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
    496 			goto release;
    497 		in6p->in6p_outputopts = &opt;
    498 	}
    499 
    500 	if (addr6) {
    501 		laddr6 = in6p->in6p_laddr;
    502 		if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
    503 			error = EISCONN;
    504 			goto release;
    505 		}
    506 		/*
    507 		 * Must block input while temporarily connected.
    508 		 */
    509 		s = splnet();
    510 		error = in6_pcbconnect(in6p, addr6);
    511 		if (error) {
    512 			splx(s);
    513 			goto release;
    514 		}
    515 	} else {
    516 		if (IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
    517 			error = ENOTCONN;
    518 			goto release;
    519 		}
    520 	}
    521 	/*
    522 	 * Calculate data length and get a mbuf
    523 	 * for UDP and IP6 headers.
    524 	 */
    525 	M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr), M_DONTWAIT);
    526 	if (m == 0) {
    527 		error = ENOBUFS;
    528 		goto release;
    529 	}
    530 
    531 	/*
    532 	 * Stuff checksum and output datagram.
    533 	 */
    534 	ip6 = mtod(m, struct ip6_hdr *);
    535 	ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    536 	ip6->ip6_vfc 	= IPV6_VERSION;
    537 #if 0				/* ip6_plen will be filled in ip6_output. */
    538 	ip6->ip6_plen	= htons((u_short)plen);
    539 #endif
    540 	ip6->ip6_nxt	= IPPROTO_UDP;
    541 	ip6->ip6_hlim	= in6p->in6p_ip6.ip6_hlim; /* XXX */
    542 	ip6->ip6_src	= in6p->in6p_laddr;
    543 	ip6->ip6_dst	= in6p->in6p_faddr;
    544 
    545 	udp6 = (struct udphdr *)(ip6 + 1);
    546 	udp6->uh_sport = in6p->in6p_lport;
    547 	udp6->uh_dport = in6p->in6p_fport;
    548 	udp6->uh_ulen  = htons((u_short)plen);
    549 	udp6->uh_sum   = 0;
    550 
    551 	if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
    552 					sizeof(struct ip6_hdr), plen)) == 0) {
    553 		udp6->uh_sum = 0xffff;
    554 	}
    555 
    556 	udp6stat.udp6s_opackets++;
    557 
    558 #ifdef IPSEC
    559 	m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
    560 #endif /*IPSEC*/
    561 	error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
    562 			    0, in6p->in6p_moptions);
    563 
    564 	if (addr6) {
    565 		in6_pcbdisconnect(in6p);
    566 		in6p->in6p_laddr = laddr6;
    567 		splx(s);
    568 	}
    569 	goto releaseopt;
    570 
    571 release:
    572 	m_freem(m);
    573 
    574 releaseopt:
    575 	if (control) {
    576 		in6p->in6p_outputopts = stickyopt;
    577 		m_freem(control);
    578 	}
    579 	return(error);
    580 }
    581 
    582 extern	int udp6_sendspace;
    583 extern	int udp6_recvspace;
    584 
    585 int
    586 udp6_usrreq(so, req, m, addr6, control, p)
    587 	struct socket *so;
    588 	int req;
    589 	struct mbuf *m, *addr6, *control;
    590 	struct proc *p;
    591 {
    592 	struct	in6pcb *in6p = sotoin6pcb(so);
    593 	int	error = 0;
    594 	int	s;
    595 
    596 	/*
    597 	 * MAPPED_ADDR implementation info:
    598 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    599 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    600 	 *  and they don't associate any addr to their socket.  Then
    601 	 *  socket family is only hint about the PRU_CONTROL'ed address
    602 	 *  family, especially when getting addrs from kernel.
    603 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    604 	 *  and AF_INET6 socket for AF_INET6 addrs.
    605 	 */
    606 	if (req == PRU_CONTROL)
    607 		return(in6_control(so, (u_long)m, (caddr_t)addr6,
    608 				   (struct ifnet *)control, p));
    609 
    610 	if (in6p == NULL && req != PRU_ATTACH) {
    611 		error = EINVAL;
    612 		goto release;
    613 	}
    614 
    615 	switch (req) {
    616 	case PRU_ATTACH:
    617 		/*
    618 		 * MAPPED_ADDR implementation spec:
    619 		 *  Always attach for IPv6,
    620 		 *  and only when necessary for IPv4.
    621 		 */
    622 		if (in6p != NULL) {
    623 			error = EINVAL;
    624 			break;
    625 		}
    626 		s = splnet();
    627 		error = in6_pcballoc(so, &udb6);
    628 		splx(s);
    629 		if (error)
    630 			break;
    631 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
    632 		if (error)
    633 			break;
    634 		in6p = sotoin6pcb(so);
    635 		in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
    636 		in6p->in6p_cksum = -1;	/* just to be sure */
    637 #ifdef IPSEC
    638 		error = ipsec_init_policy(&in6p->in6p_sp);
    639 #endif /*IPSEC*/
    640 		break;
    641 
    642 	case PRU_DETACH:
    643 		udp6_detach(in6p);
    644 		break;
    645 
    646 	case PRU_BIND:
    647 		s = splnet();
    648 		error = in6_pcbbind(in6p, addr6);
    649 		splx(s);
    650 		break;
    651 
    652 	case PRU_LISTEN:
    653 		error = EOPNOTSUPP;
    654 		break;
    655 
    656 	case PRU_CONNECT:
    657 		if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
    658 			error = EISCONN;
    659 			break;
    660 		}
    661 		s = splnet();
    662 		error = in6_pcbconnect(in6p, addr6);
    663 		if (ip6_auto_flowlabel) {
    664 			in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    665 			in6p->in6p_flowinfo |=
    666 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    667 		}
    668 		splx(s);
    669 		if (error == 0)
    670 			soisconnected(so);
    671 		break;
    672 
    673 	case PRU_CONNECT2:
    674 		error = EOPNOTSUPP;
    675 		break;
    676 
    677 	case PRU_ACCEPT:
    678 		error = EOPNOTSUPP;
    679 		break;
    680 
    681 	case PRU_DISCONNECT:
    682 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    683 			error = ENOTCONN;
    684 			break;
    685 		}
    686 		s = splnet();
    687 		in6_pcbdisconnect(in6p);
    688 		bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
    689 		splx(s);
    690 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    691 		break;
    692 
    693 	case PRU_SHUTDOWN:
    694 		socantsendmore(so);
    695 		break;
    696 
    697 	case PRU_SEND:
    698 		return(udp6_output(in6p, m, addr6, control));
    699 
    700 	case PRU_ABORT:
    701 		soisdisconnected(so);
    702 		udp6_detach(in6p);
    703 		break;
    704 
    705 	case PRU_SOCKADDR:
    706 		in6_setsockaddr(in6p, addr6);
    707 		break;
    708 
    709 	case PRU_PEERADDR:
    710 		in6_setpeeraddr(in6p, addr6);
    711 		break;
    712 
    713 	case PRU_SENSE:
    714 		/*
    715 		 * stat: don't bother with a blocksize
    716 		 */
    717 		return(0);
    718 
    719 	case PRU_SENDOOB:
    720 	case PRU_FASTTIMO:
    721 	case PRU_SLOWTIMO:
    722 	case PRU_PROTORCV:
    723 	case PRU_PROTOSEND:
    724 		error = EOPNOTSUPP;
    725 		break;
    726 
    727 	case PRU_RCVD:
    728 	case PRU_RCVOOB:
    729 		return(EOPNOTSUPP);	/* do not free mbuf's */
    730 
    731 	default:
    732 		panic("udp6_usrreq");
    733 	}
    734 
    735 release:
    736 	if (control) {
    737 		printf("udp control data unexpectedly retained\n");
    738 		m_freem(control);
    739 	}
    740 	if (m)
    741 		m_freem(m);
    742 	return(error);
    743 }
    744 
    745 static void
    746 udp6_detach(in6p)
    747 	struct in6pcb *in6p;
    748 {
    749 	int	s = splnet();
    750 
    751 	if (in6p == udp6_last_in6pcb)
    752 		udp6_last_in6pcb = &udb6;
    753 	in6_pcbdetach(in6p);
    754 	splx(s);
    755 }
    756 
    757 #ifdef __bsdi__
    758 int *udp6_sysvars[] = UDP6CTL_VARS;
    759 
    760 int
    761 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    762 	int	*name;
    763 	u_int	namelen;
    764 	void	*oldp;
    765 	size_t	*oldlenp;
    766 	void	*newp;
    767 	size_t	newlen;
    768 {
    769 	if (name[0] >= UDP6CTL_MAXID)
    770 		return (EOPNOTSUPP);
    771 	switch (name[0]) {
    772 	case UDP6CTL_STATS:
    773 		return sysctl_rdtrunc(oldp, oldlenp, newp, &udp6stat,
    774 		    sizeof(udp6stat));
    775 
    776 	default:
    777 		return (sysctl_int_arr(udp6_sysvars, name, namelen,
    778 		    oldp, oldlenp, newp, newlen));
    779 	}
    780 }
    781 #endif /*__bsdi__*/
    782 
    783 #ifdef __NetBSD__
    784 #include <vm/vm.h>
    785 #include <sys/sysctl.h>
    786 
    787 int
    788 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    789 	int *name;
    790 	u_int namelen;
    791 	void *oldp;
    792 	size_t *oldlenp;
    793 	void *newp;
    794 	size_t newlen;
    795 {
    796 	/* All sysctl names at this level are terminal. */
    797 	if (namelen != 1)
    798 		return ENOTDIR;
    799 
    800 	switch (name[0]) {
    801 
    802 	case UDP6CTL_SENDMAX:
    803 		return sysctl_int(oldp, oldlenp, newp, newlen,
    804 		    &udp6_sendspace);
    805 	case UDP6CTL_RECVSPACE:
    806 		return sysctl_int(oldp, oldlenp, newp, newlen,
    807 		    &udp6_recvspace);
    808 	default:
    809 		return ENOPROTOOPT;
    810 	}
    811 	/* NOTREACHED */
    812 }
    813 #endif
    814