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