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