Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.30.2.4
      1  1.30.2.4       he /*	$NetBSD: udp6_usrreq.c,v 1.30.2.4 2001/05/09 19:38:47 he Exp $	*/
      2  1.30.2.2       tv /*	$KAME: udp6_usrreq.c,v 1.62 2000/10/19 01:11:05 itojun Exp $	*/
      3       1.3  thorpej 
      4       1.2   itojun /*
      5       1.2   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6       1.2   itojun  * All rights reserved.
      7      1.27   itojun  *
      8       1.2   itojun  * Redistribution and use in source and binary forms, with or without
      9       1.2   itojun  * modification, are permitted provided that the following conditions
     10       1.2   itojun  * are met:
     11       1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     12       1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     13       1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     15       1.2   itojun  *    documentation and/or other materials provided with the distribution.
     16       1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     17       1.2   itojun  *    may be used to endorse or promote products derived from this software
     18       1.2   itojun  *    without specific prior written permission.
     19      1.27   itojun  *
     20       1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21       1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22       1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23       1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24       1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25       1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26       1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27       1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28       1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29       1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30       1.2   itojun  * SUCH DAMAGE.
     31       1.2   itojun  */
     32       1.2   itojun 
     33       1.2   itojun /*
     34       1.2   itojun  * Copyright (c) 1982, 1986, 1989, 1993
     35       1.2   itojun  *	The Regents of the University of California.  All rights reserved.
     36       1.2   itojun  *
     37       1.2   itojun  * Redistribution and use in source and binary forms, with or without
     38       1.2   itojun  * modification, are permitted provided that the following conditions
     39       1.2   itojun  * are met:
     40       1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     41       1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     42       1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     43       1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     44       1.2   itojun  *    documentation and/or other materials provided with the distribution.
     45       1.2   itojun  * 3. All advertising materials mentioning features or use of this software
     46       1.2   itojun  *    must display the following acknowledgement:
     47       1.2   itojun  *	This product includes software developed by the University of
     48       1.2   itojun  *	California, Berkeley and its contributors.
     49       1.2   itojun  * 4. Neither the name of the University nor the names of its contributors
     50       1.2   itojun  *    may be used to endorse or promote products derived from this software
     51       1.2   itojun  *    without specific prior written permission.
     52       1.2   itojun  *
     53       1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54       1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55       1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56       1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57       1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58       1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59       1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60       1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61       1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62       1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63       1.2   itojun  * SUCH DAMAGE.
     64       1.2   itojun  *
     65       1.2   itojun  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
     66       1.2   itojun  */
     67       1.5  thorpej 
     68       1.5  thorpej #include "opt_ipsec.h"
     69       1.2   itojun 
     70       1.2   itojun #include <sys/param.h>
     71       1.2   itojun #include <sys/malloc.h>
     72       1.2   itojun #include <sys/mbuf.h>
     73       1.2   itojun #include <sys/protosw.h>
     74       1.2   itojun #include <sys/socket.h>
     75       1.2   itojun #include <sys/socketvar.h>
     76       1.2   itojun #include <sys/errno.h>
     77       1.2   itojun #include <sys/stat.h>
     78       1.2   itojun #include <sys/systm.h>
     79       1.2   itojun #include <sys/proc.h>
     80       1.8   itojun #include <sys/syslog.h>
     81       1.2   itojun 
     82       1.2   itojun #include <net/if.h>
     83       1.2   itojun #include <net/route.h>
     84       1.2   itojun #include <net/if_types.h>
     85       1.2   itojun 
     86       1.2   itojun #include <netinet/in.h>
     87       1.2   itojun #include <netinet/in_var.h>
     88      1.14   itojun #include <netinet/in_systm.h>
     89      1.14   itojun #include <netinet/ip.h>
     90      1.14   itojun #include <netinet/ip_var.h>
     91      1.14   itojun #include <netinet/in_pcb.h>
     92      1.14   itojun #include <netinet/udp.h>
     93      1.14   itojun #include <netinet/udp_var.h>
     94      1.23   itojun #include <netinet/ip6.h>
     95      1.27   itojun #include <netinet6/ip6_var.h>
     96       1.2   itojun #include <netinet6/in6_pcb.h>
     97      1.23   itojun #include <netinet/icmp6.h>
     98       1.2   itojun #include <netinet6/udp6_var.h>
     99      1.14   itojun #include <netinet6/ip6protosw.h>
    100       1.2   itojun 
    101       1.2   itojun #ifdef IPSEC
    102       1.2   itojun #include <netinet6/ipsec.h>
    103       1.2   itojun #endif /*IPSEC*/
    104       1.2   itojun 
    105       1.2   itojun #include "faith.h"
    106  1.30.2.4       he #if defined(NFAITH) && NFAITH > 0
    107  1.30.2.4       he #include <net/if_faith.h>
    108  1.30.2.4       he #endif
    109       1.2   itojun 
    110       1.2   itojun /*
    111       1.2   itojun  * UDP protocol inplementation.
    112       1.2   itojun  * Per RFC 768, August, 1980.
    113       1.2   itojun  */
    114       1.2   itojun 
    115       1.2   itojun struct	in6pcb *udp6_last_in6pcb = &udb6;
    116       1.2   itojun 
    117      1.17   itojun #ifdef UDP6
    118       1.2   itojun static	int in6_mcmatch __P((struct in6pcb *, struct in6_addr *, struct ifnet *));
    119      1.14   itojun #endif
    120       1.2   itojun static	void udp6_detach __P((struct in6pcb *));
    121       1.2   itojun static	void udp6_notify __P((struct in6pcb *, int));
    122       1.2   itojun 
    123       1.2   itojun void
    124       1.2   itojun udp6_init()
    125       1.2   itojun {
    126       1.2   itojun 	udb6.in6p_next = udb6.in6p_prev = &udb6;
    127       1.2   itojun }
    128       1.2   itojun 
    129      1.17   itojun #ifdef UDP6
    130       1.2   itojun static int
    131       1.2   itojun in6_mcmatch(in6p, ia6, ifp)
    132       1.2   itojun 	struct in6pcb *in6p;
    133       1.2   itojun 	register struct in6_addr *ia6;
    134       1.2   itojun 	struct ifnet *ifp;
    135       1.2   itojun {
    136       1.2   itojun 	struct ip6_moptions *im6o = in6p->in6p_moptions;
    137       1.2   itojun 	struct in6_multi_mship *imm;
    138       1.2   itojun 
    139       1.2   itojun 	if (im6o == NULL)
    140       1.2   itojun 		return 0;
    141       1.2   itojun 
    142       1.2   itojun 	for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
    143       1.2   itojun 	     imm = imm->i6mm_chain.le_next) {
    144       1.2   itojun 		if ((ifp == NULL ||
    145       1.2   itojun 		     imm->i6mm_maddr->in6m_ifp == ifp) &&
    146       1.2   itojun 		    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
    147       1.2   itojun 				       ia6))
    148       1.2   itojun 			return 1;
    149       1.2   itojun 	}
    150       1.2   itojun 	return 0;
    151       1.2   itojun }
    152       1.2   itojun 
    153       1.2   itojun int
    154       1.2   itojun udp6_input(mp, offp, proto)
    155       1.2   itojun 	struct mbuf **mp;
    156       1.2   itojun 	int *offp, proto;
    157       1.2   itojun {
    158       1.2   itojun 	struct mbuf *m = *mp;
    159       1.2   itojun 	register struct ip6_hdr *ip6;
    160       1.2   itojun 	register struct udphdr *uh;
    161       1.2   itojun 	register struct in6pcb *in6p;
    162       1.2   itojun 	struct	mbuf *opts = 0;
    163       1.2   itojun 	int off = *offp;
    164      1.14   itojun 	u_int32_t plen, ulen;
    165       1.2   itojun 	struct sockaddr_in6 udp_in6;
    166       1.2   itojun 
    167  1.30.2.4       he 	ip6 = mtod(m, struct ip6_hdr *);
    168  1.30.2.4       he 
    169       1.2   itojun #if defined(NFAITH) && 0 < NFAITH
    170  1.30.2.4       he 	if (faithprefix(&ip6->ip6_dst)) {
    171  1.30.2.4       he 		/* send icmp6 host unreach? */
    172  1.30.2.4       he 		m_freem(m);
    173  1.30.2.4       he 		return IPPROTO_DONE;
    174       1.2   itojun 	}
    175       1.2   itojun #endif
    176  1.30.2.4       he 
    177       1.2   itojun 	udp6stat.udp6s_ipackets++;
    178       1.2   itojun 
    179      1.14   itojun 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
    180      1.14   itojun 	plen = m->m_pkthdr.len - off;
    181      1.14   itojun #ifndef PULLDOWN_TEST
    182       1.2   itojun 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
    183       1.2   itojun 	uh = (struct udphdr *)((caddr_t)ip6 + off);
    184      1.14   itojun #else
    185      1.24   itojun 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    186      1.14   itojun 	if (uh == NULL) {
    187      1.14   itojun 		udp6stat.udp6s_hdrops++;
    188      1.14   itojun 		return IPPROTO_DONE;
    189      1.14   itojun 	}
    190      1.14   itojun #endif
    191       1.2   itojun 	ulen = ntohs((u_short)uh->uh_ulen);
    192      1.27   itojun 	/*
    193      1.27   itojun 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    194      1.27   itojun 	 * iff payload length > 0xffff.
    195      1.27   itojun 	 */
    196      1.27   itojun 	if (ulen == 0 && plen > 0xffff)
    197      1.14   itojun 		ulen = plen;
    198       1.2   itojun 
    199       1.2   itojun 	if (plen != ulen) {
    200       1.2   itojun 		udp6stat.udp6s_badlen++;
    201      1.16   itojun 		goto bad;
    202      1.16   itojun 	}
    203      1.18   itojun 
    204      1.18   itojun 	/* destination port of 0 is illegal, based on RFC768. */
    205      1.18   itojun 	if (uh->uh_dport == 0)
    206      1.18   itojun 		goto bad;
    207      1.16   itojun 
    208      1.16   itojun 	/* Be proactive about malicious use of IPv4 mapped address */
    209      1.16   itojun 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    210      1.16   itojun 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    211      1.16   itojun 		/* XXX stat */
    212       1.2   itojun 		goto bad;
    213       1.2   itojun 	}
    214       1.2   itojun 
    215       1.2   itojun 	/*
    216       1.2   itojun 	 * Checksum extended UDP header and data.
    217       1.2   itojun 	 */
    218       1.2   itojun 	if (uh->uh_sum == 0)
    219       1.2   itojun 		udp6stat.udp6s_nosum++;
    220       1.2   itojun 	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
    221       1.2   itojun 		udp6stat.udp6s_badsum++;
    222       1.2   itojun 		goto bad;
    223       1.2   itojun 	}
    224       1.2   itojun 
    225       1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    226       1.2   itojun 		struct	in6pcb *last;
    227       1.2   itojun 
    228       1.2   itojun 		/*
    229       1.2   itojun 		 * Deliver a multicast datagram to all sockets
    230       1.2   itojun 		 * for which the local and remote addresses and ports match
    231       1.2   itojun 		 * those of the incoming datagram.  This allows more than
    232       1.2   itojun 		 * one process to receive multicasts on the same port.
    233       1.2   itojun 		 * (This really ought to be done for unicast datagrams as
    234       1.2   itojun 		 * well, but that would cause problems with existing
    235       1.2   itojun 		 * applications that open both address-specific sockets and
    236       1.2   itojun 		 * a wildcard socket listening to the same port -- they would
    237       1.2   itojun 		 * end up receiving duplicates of every unicast datagram.
    238       1.2   itojun 		 * Those applications open the multiple sockets to overcome an
    239       1.2   itojun 		 * inadequacy of the UDP socket interface, but for backwards
    240       1.2   itojun 		 * compatibility we avoid the problem here rather than
    241       1.2   itojun 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    242       1.2   itojun 		 */
    243       1.2   itojun 
    244       1.2   itojun 		/*
    245       1.2   itojun 		 * In a case that laddr should be set to the link-local
    246       1.2   itojun 		 * address (this happens in RIPng), the multicast address
    247       1.2   itojun 		 * specified in the received packet does not match with
    248       1.2   itojun 		 * laddr. To cure this situation, the matching is relaxed
    249       1.2   itojun 		 * if the receiving interface is the same as one specified
    250       1.2   itojun 		 * in the socket and if the destination multicast address
    251       1.2   itojun 		 * matches one of the multicast groups specified in the socket.
    252       1.2   itojun 		 */
    253       1.2   itojun 
    254      1.27   itojun 		/*
    255       1.2   itojun 		 * Construct sockaddr format source address.
    256       1.2   itojun 		 */
    257       1.2   itojun 		bzero(&udp_in6, sizeof(udp_in6));
    258       1.2   itojun 		udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    259       1.2   itojun 		udp_in6.sin6_family = AF_INET6;
    260       1.2   itojun 		udp_in6.sin6_port = uh->uh_sport;
    261       1.2   itojun 		udp_in6.sin6_addr = ip6->ip6_src;
    262       1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
    263       1.2   itojun 			udp_in6.sin6_addr.s6_addr16[1] = 0;
    264       1.2   itojun 		if (m->m_pkthdr.rcvif) {
    265       1.2   itojun 			if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) {
    266       1.2   itojun 				udp_in6.sin6_scope_id =
    267       1.2   itojun 					m->m_pkthdr.rcvif->if_index;
    268       1.2   itojun 			} else
    269       1.2   itojun 				udp_in6.sin6_scope_id = 0;
    270       1.2   itojun 		} else
    271       1.2   itojun 			udp_in6.sin6_scope_id = 0;
    272       1.2   itojun 		/*
    273       1.2   itojun 		 * KAME note: usually we drop udphdr from mbuf here.
    274       1.2   itojun 		 * We need udphdr for IPsec processing so we do that later.
    275       1.2   itojun 		 */
    276       1.2   itojun 
    277       1.2   itojun 		/*
    278       1.2   itojun 		 * Locate pcb(s) for datagram.
    279       1.2   itojun 		 * (Algorithm copied from raw_intr().)
    280       1.2   itojun 		 */
    281       1.2   itojun 		last = NULL;
    282       1.2   itojun 		for (in6p = udb6.in6p_next;
    283       1.2   itojun 		     in6p != &udb6;
    284       1.2   itojun 		     in6p = in6p->in6p_next) {
    285       1.2   itojun 			if (in6p->in6p_lport != uh->uh_dport)
    286       1.2   itojun 				continue;
    287      1.14   itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    288       1.2   itojun 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    289       1.2   itojun 							&ip6->ip6_dst) &&
    290       1.2   itojun 				    !in6_mcmatch(in6p, &ip6->ip6_dst,
    291       1.2   itojun 						 m->m_pkthdr.rcvif))
    292       1.2   itojun 					continue;
    293       1.2   itojun 			}
    294      1.14   itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    295       1.2   itojun 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    296       1.2   itojun 							&ip6->ip6_src) ||
    297       1.2   itojun 				   in6p->in6p_fport != uh->uh_sport)
    298       1.2   itojun 					continue;
    299       1.2   itojun 			}
    300       1.2   itojun 
    301       1.2   itojun 			if (last != NULL) {
    302       1.2   itojun 				struct	mbuf *n;
    303       1.2   itojun 
    304       1.2   itojun #ifdef IPSEC
    305       1.2   itojun 				/*
    306       1.2   itojun 				 * Check AH/ESP integrity.
    307       1.2   itojun 				 */
    308      1.24   itojun 				if (ipsec6_in_reject(m, last)) {
    309       1.2   itojun 					ipsec6stat.in_polvio++;
    310       1.2   itojun 					/* do not inject data into pcb */
    311       1.2   itojun 				} else
    312       1.2   itojun #endif /*IPSEC*/
    313       1.2   itojun 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    314       1.2   itojun 					/*
    315       1.2   itojun 					 * KAME NOTE: do not
    316       1.2   itojun 					 * m_copy(m, offset, ...) above.
    317       1.2   itojun 					 * sbappendaddr() expects M_PKTHDR,
    318       1.2   itojun 					 * and m_copy() will copy M_PKTHDR
    319       1.2   itojun 					 * only if offset is 0.
    320       1.2   itojun 					 */
    321       1.8   itojun 					if (last->in6p_flags & IN6P_CONTROLOPTS
    322       1.8   itojun 					 || last->in6p_socket->so_options & SO_TIMESTAMP) {
    323       1.2   itojun 						ip6_savecontrol(last, &opts,
    324       1.2   itojun 								ip6, n);
    325       1.2   itojun 					}
    326       1.2   itojun 
    327      1.11   itojun 					m_adj(n, off + sizeof(struct udphdr));
    328       1.2   itojun 					if (sbappendaddr(&last->in6p_socket->so_rcv,
    329       1.2   itojun 							(struct sockaddr *)&udp_in6,
    330       1.2   itojun 							n, opts) == 0) {
    331       1.2   itojun 						m_freem(n);
    332       1.2   itojun 						if (opts)
    333       1.2   itojun 							m_freem(opts);
    334       1.2   itojun 						udp6stat.udp6s_fullsock++;
    335       1.2   itojun 					} else
    336       1.2   itojun 						sorwakeup(last->in6p_socket);
    337       1.2   itojun 					opts = 0;
    338       1.2   itojun 				}
    339       1.2   itojun 			}
    340       1.2   itojun 			last = in6p;
    341       1.2   itojun 			/*
    342       1.2   itojun 			 * Don't look for additional matches if this one does
    343       1.2   itojun 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    344       1.2   itojun 			 * socket options set.  This heuristic avoids searching
    345       1.2   itojun 			 * through all pcbs in the common case of a non-shared
    346       1.2   itojun 			 * port.  It assumes that an application will never
    347       1.2   itojun 			 * clear these options after setting them.
    348       1.2   itojun 			 */
    349       1.2   itojun 			if ((last->in6p_socket->so_options &
    350       1.2   itojun 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    351       1.2   itojun 				break;
    352       1.2   itojun 		}
    353       1.2   itojun 
    354       1.2   itojun 		if (last == NULL) {
    355       1.2   itojun 			/*
    356       1.2   itojun 			 * No matching pcb found; discard datagram.
    357       1.2   itojun 			 * (No need to send an ICMP Port Unreachable
    358       1.2   itojun 			 * for a broadcast or multicast datgram.)
    359       1.2   itojun 			 */
    360       1.2   itojun 			udp6stat.udp6s_noport++;
    361       1.2   itojun 			udp6stat.udp6s_noportmcast++;
    362       1.2   itojun 			goto bad;
    363       1.2   itojun 		}
    364       1.2   itojun #ifdef IPSEC
    365       1.2   itojun 		/*
    366       1.2   itojun 		 * Check AH/ESP integrity.
    367       1.2   itojun 		 */
    368       1.2   itojun 		if (last != NULL && ipsec6_in_reject(m, last)) {
    369       1.2   itojun 			ipsec6stat.in_polvio++;
    370       1.2   itojun 			goto bad;
    371       1.2   itojun 		}
    372       1.2   itojun #endif /*IPSEC*/
    373       1.8   itojun 		if (last->in6p_flags & IN6P_CONTROLOPTS
    374       1.8   itojun 		 || last->in6p_socket->so_options & SO_TIMESTAMP) {
    375       1.2   itojun 			ip6_savecontrol(last, &opts, ip6, m);
    376       1.8   itojun 		}
    377       1.2   itojun 
    378       1.2   itojun 		m_adj(m, off + sizeof(struct udphdr));
    379       1.2   itojun 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    380       1.2   itojun 				(struct sockaddr *)&udp_in6,
    381       1.2   itojun 				m, opts) == 0) {
    382       1.2   itojun 			udp6stat.udp6s_fullsock++;
    383       1.2   itojun 			goto bad;
    384       1.2   itojun 		}
    385       1.2   itojun 		sorwakeup(last->in6p_socket);
    386       1.2   itojun 		return IPPROTO_DONE;
    387       1.2   itojun 	}
    388       1.2   itojun 	/*
    389       1.2   itojun 	 * Locate pcb for datagram.
    390       1.2   itojun 	 */
    391       1.2   itojun 	in6p = udp6_last_in6pcb;
    392       1.2   itojun 	if (in6p->in6p_lport != uh->uh_dport ||
    393       1.2   itojun 	   in6p->in6p_fport != uh->uh_sport ||
    394       1.2   itojun 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) ||
    395       1.2   itojun 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
    396       1.2   itojun 		in6p = in6_pcblookup(&udb6,
    397       1.2   itojun 				     &ip6->ip6_src, uh->uh_sport,
    398       1.2   itojun 				     &ip6->ip6_dst, uh->uh_dport,
    399       1.2   itojun 				     IN6PLOOKUP_WILDCARD);
    400       1.2   itojun 		if (in6p)
    401       1.2   itojun 			udp6_last_in6pcb = in6p;
    402       1.2   itojun 		udp6stat.udp6ps_pcbcachemiss++;
    403       1.2   itojun 	}
    404       1.2   itojun 	if (in6p == 0) {
    405       1.2   itojun 		udp6stat.udp6s_noport++;
    406       1.2   itojun 		if (m->m_flags & M_MCAST) {
    407       1.2   itojun 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
    408       1.2   itojun 			udp6stat.udp6s_noportmcast++;
    409       1.2   itojun 			goto bad;
    410       1.2   itojun 		}
    411       1.2   itojun 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    412       1.2   itojun 		return IPPROTO_DONE;
    413       1.2   itojun 	}
    414       1.2   itojun #ifdef IPSEC
    415       1.2   itojun 	/*
    416       1.2   itojun 	 * Check AH/ESP integrity.
    417       1.2   itojun 	 */
    418       1.2   itojun 	if (in6p != NULL && ipsec6_in_reject(m, in6p)) {
    419       1.2   itojun 		ipsec6stat.in_polvio++;
    420       1.2   itojun 		goto bad;
    421       1.2   itojun 	}
    422       1.2   itojun #endif /*IPSEC*/
    423       1.2   itojun 
    424       1.2   itojun 	/*
    425       1.2   itojun 	 * Construct sockaddr format source address.
    426       1.2   itojun 	 * Stuff source address and datagram in user buffer.
    427       1.2   itojun 	 */
    428       1.2   itojun 	bzero(&udp_in6, sizeof(udp_in6));
    429       1.2   itojun 	udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    430       1.2   itojun 	udp_in6.sin6_family = AF_INET6;
    431       1.2   itojun 	udp_in6.sin6_port = uh->uh_sport;
    432       1.2   itojun 	udp_in6.sin6_addr = ip6->ip6_src;
    433       1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
    434       1.2   itojun 		udp_in6.sin6_addr.s6_addr16[1] = 0;
    435       1.2   itojun 	if (m->m_pkthdr.rcvif) {
    436       1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
    437       1.2   itojun 			udp_in6.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
    438       1.2   itojun 		else
    439       1.2   itojun 			udp_in6.sin6_scope_id = 0;
    440       1.2   itojun 	} else
    441       1.2   itojun 		udp_in6.sin6_scope_id = 0;
    442       1.8   itojun 	if (in6p->in6p_flags & IN6P_CONTROLOPTS
    443       1.8   itojun 	 || in6p->in6p_socket->so_options & SO_TIMESTAMP) {
    444       1.2   itojun 		ip6_savecontrol(in6p, &opts, ip6, m);
    445       1.8   itojun 	}
    446       1.2   itojun 
    447       1.2   itojun 	m_adj(m, off + sizeof(struct udphdr));
    448       1.2   itojun 	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
    449       1.2   itojun 			(struct sockaddr *)&udp_in6,
    450       1.2   itojun 			m, opts) == 0) {
    451       1.2   itojun 		udp6stat.udp6s_fullsock++;
    452       1.2   itojun 		goto bad;
    453       1.2   itojun 	}
    454       1.2   itojun 	sorwakeup(in6p->in6p_socket);
    455       1.2   itojun 	return IPPROTO_DONE;
    456       1.2   itojun bad:
    457       1.2   itojun 	if (m)
    458       1.2   itojun 		m_freem(m);
    459       1.2   itojun 	if (opts)
    460       1.2   itojun 		m_freem(opts);
    461       1.2   itojun 	return IPPROTO_DONE;
    462       1.2   itojun }
    463      1.14   itojun #endif
    464       1.2   itojun 
    465       1.2   itojun /*
    466       1.2   itojun  * Notify a udp user of an asynchronous error;
    467      1.27   itojun  * just wake up so that he can collect error status.
    468       1.2   itojun  */
    469       1.2   itojun static	void
    470       1.2   itojun udp6_notify(in6p, errno)
    471       1.2   itojun 	register struct in6pcb *in6p;
    472       1.2   itojun 	int errno;
    473       1.2   itojun {
    474       1.2   itojun 	in6p->in6p_socket->so_error = errno;
    475       1.2   itojun 	sorwakeup(in6p->in6p_socket);
    476       1.2   itojun 	sowwakeup(in6p->in6p_socket);
    477       1.2   itojun }
    478       1.2   itojun 
    479       1.2   itojun void
    480      1.14   itojun udp6_ctlinput(cmd, sa, d)
    481       1.2   itojun 	int cmd;
    482       1.2   itojun 	struct sockaddr *sa;
    483      1.14   itojun 	void *d;
    484       1.2   itojun {
    485       1.2   itojun 	register struct udphdr *uhp;
    486       1.2   itojun 	struct udphdr uh;
    487       1.7   itojun 	struct sockaddr_in6 sa6;
    488      1.14   itojun 	register struct ip6_hdr *ip6;
    489      1.14   itojun 	struct mbuf *m;
    490      1.14   itojun 	int off;
    491      1.25   itojun 	void (*notify) __P((struct in6pcb *, int)) = udp6_notify;
    492       1.2   itojun 
    493      1.10   itojun 	if (sa->sa_family != AF_INET6 ||
    494      1.10   itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    495      1.10   itojun 		return;
    496      1.14   itojun 
    497      1.25   itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    498      1.25   itojun 		return;
    499      1.25   itojun 	if (PRC_IS_REDIRECT(cmd))
    500      1.25   itojun 		notify = in6_rtchange, d = NULL;
    501      1.25   itojun 	else if (cmd == PRC_HOSTDEAD)
    502      1.25   itojun 		d = NULL;
    503      1.25   itojun 	else if (inet6ctlerrmap[cmd] == 0)
    504       1.2   itojun 		return;
    505       1.7   itojun 
    506      1.14   itojun 	/* if the parameter is from icmp6, decode it. */
    507      1.14   itojun 	if (d != NULL) {
    508      1.14   itojun 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
    509      1.14   itojun 		m = ip6cp->ip6c_m;
    510      1.14   itojun 		ip6 = ip6cp->ip6c_ip6;
    511      1.14   itojun 		off = ip6cp->ip6c_off;
    512      1.14   itojun 	} else {
    513      1.14   itojun 		m = NULL;
    514      1.14   itojun 		ip6 = NULL;
    515      1.14   itojun 	}
    516      1.14   itojun 
    517       1.7   itojun 	/* translate addresses into internal form */
    518       1.7   itojun 	sa6 = *(struct sockaddr_in6 *)sa;
    519      1.22   itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif)
    520       1.7   itojun 		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
    521       1.7   itojun 
    522       1.2   itojun 	if (ip6) {
    523       1.2   itojun 		/*
    524       1.2   itojun 		 * XXX: We assume that when IPV6 is non NULL,
    525       1.2   itojun 		 * M and OFF are valid.
    526       1.2   itojun 		 */
    527       1.7   itojun 		struct in6_addr s;
    528       1.7   itojun 
    529       1.7   itojun 		/* translate addresses into internal form */
    530      1.12   itojun 		memcpy(&s, &ip6->ip6_src, sizeof(s));
    531       1.7   itojun 		if (IN6_IS_ADDR_LINKLOCAL(&s))
    532       1.7   itojun 			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
    533  1.30.2.1       tv 
    534  1.30.2.1       tv 		/* check if we can safely examine src and dst ports */
    535  1.30.2.1       tv 		if (m->m_pkthdr.len < off + sizeof(uh))
    536  1.30.2.1       tv 			return;
    537       1.7   itojun 
    538       1.2   itojun 		if (m->m_len < off + sizeof(uh)) {
    539       1.2   itojun 			/*
    540       1.2   itojun 			 * this should be rare case,
    541       1.2   itojun 			 * so we compromise on this copy...
    542       1.2   itojun 			 */
    543       1.2   itojun 			m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
    544       1.2   itojun 			uhp = &uh;
    545       1.2   itojun 		} else
    546       1.2   itojun 			uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
    547       1.7   itojun 		(void) in6_pcbnotify(&udb6, (struct sockaddr *)&sa6,
    548       1.7   itojun 					uhp->uh_dport, &s,
    549      1.25   itojun 					uhp->uh_sport, cmd, notify);
    550       1.2   itojun 	} else {
    551       1.7   itojun 		(void) in6_pcbnotify(&udb6, (struct sockaddr *)&sa6, 0,
    552      1.25   itojun 					&zeroin6_addr, 0, cmd, notify);
    553       1.2   itojun 	}
    554       1.2   itojun }
    555       1.2   itojun 
    556       1.2   itojun int
    557      1.29   itojun udp6_output(in6p, m, addr6, control, p)
    558       1.2   itojun 	register struct in6pcb *in6p;
    559       1.2   itojun 	register struct mbuf *m;
    560       1.2   itojun 	struct mbuf *addr6, *control;
    561      1.29   itojun 	struct proc *p;
    562       1.2   itojun {
    563      1.14   itojun 	register u_int32_t ulen = m->m_pkthdr.len;
    564      1.14   itojun 	u_int32_t plen = sizeof(struct udphdr) + ulen;
    565       1.2   itojun 	struct ip6_hdr *ip6;
    566       1.2   itojun 	struct udphdr *udp6;
    567      1.14   itojun 	struct	in6_addr *laddr, *faddr;
    568      1.14   itojun 	u_short fport;
    569      1.14   itojun 	int error = 0;
    570       1.2   itojun 	struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
    571      1.14   itojun 	int priv;
    572      1.14   itojun 	int af, hlen;
    573      1.14   itojun #ifdef INET
    574      1.14   itojun 	struct ip *ip;
    575      1.14   itojun #endif
    576      1.30   itojun 	struct sockaddr_in6 tmp;
    577       1.2   itojun 
    578      1.14   itojun 	priv = 0;
    579       1.2   itojun 	if (p && !suser(p->p_ucred, &p->p_acflag))
    580       1.2   itojun 		priv = 1;
    581       1.2   itojun 	if (control) {
    582       1.2   itojun 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
    583       1.2   itojun 			goto release;
    584       1.2   itojun 		in6p->in6p_outputopts = &opt;
    585       1.2   itojun 	}
    586       1.2   itojun 
    587       1.2   itojun 	if (addr6) {
    588      1.14   itojun 		/*
    589      1.14   itojun 		 * IPv4 version of udp_output calls in_pcbconnect in this case,
    590      1.14   itojun 		 * which needs splnet and affects performance.
    591      1.14   itojun 		 * Since we saw no essential reason for calling in_pcbconnect,
    592      1.14   itojun 		 * we get rid of such kind of logic, and call in6_selectsrc
    593      1.14   itojun 		 * and In6_pcbsetport in order to fill in the local address
    594      1.14   itojun 		 * and the local port.
    595      1.14   itojun 		 */
    596      1.14   itojun 		struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *);
    597      1.14   itojun 
    598      1.14   itojun 		if (addr6->m_len != sizeof(*sin6)) {
    599      1.14   itojun 			error = EINVAL;
    600      1.14   itojun 			goto release;
    601      1.14   itojun 		}
    602      1.14   itojun 		if (sin6->sin6_family != AF_INET6) {
    603      1.14   itojun 			error = EAFNOSUPPORT;
    604      1.14   itojun 			goto release;
    605      1.14   itojun 		}
    606      1.14   itojun 		if (sin6->sin6_port == 0) {
    607      1.14   itojun 			error = EADDRNOTAVAIL;
    608      1.14   itojun 			goto release;
    609      1.14   itojun 		}
    610      1.14   itojun 
    611      1.14   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    612       1.2   itojun 			error = EISCONN;
    613       1.2   itojun 			goto release;
    614       1.2   itojun 		}
    615      1.30   itojun 
    616      1.30   itojun 		/* protect *sin6 from overwrites */
    617      1.30   itojun 		tmp = *sin6;
    618      1.30   itojun 		sin6 = &tmp;
    619      1.14   itojun 
    620      1.14   itojun 		faddr = &sin6->sin6_addr;
    621      1.14   itojun 		fport = sin6->sin6_port; /* allow 0 port */
    622       1.2   itojun 		/*
    623      1.14   itojun 		 * If the scope of the destination is link-local,
    624      1.27   itojun 		 * embed the interface index in the address.
    625      1.14   itojun 		 *
    626      1.27   itojun 		 * XXX advanced-api value overrides sin6_scope_id
    627       1.2   itojun 		 */
    628      1.14   itojun 		if (IN6_IS_ADDR_LINKLOCAL(faddr) ||
    629      1.14   itojun 		    IN6_IS_ADDR_MC_LINKLOCAL(faddr)) {
    630      1.14   itojun 			struct ip6_pktopts *optp = in6p->in6p_outputopts;
    631      1.14   itojun 			struct in6_pktinfo *pi = NULL;
    632      1.14   itojun 			struct ifnet *oifp = NULL;
    633      1.14   itojun 			struct ip6_moptions *mopt = NULL;
    634      1.14   itojun 
    635      1.14   itojun 			/*
    636      1.14   itojun 			 * XXX Boundary check is assumed to be already done in
    637      1.14   itojun 			 * ip6_setpktoptions().
    638      1.14   itojun 			 */
    639      1.14   itojun 			if (optp && (pi = optp->ip6po_pktinfo) &&
    640      1.14   itojun 			    pi->ipi6_ifindex) {
    641      1.14   itojun 				faddr->s6_addr16[1] = htons(pi->ipi6_ifindex);
    642      1.14   itojun 				oifp = ifindex2ifnet[pi->ipi6_ifindex];
    643      1.27   itojun 			} else if (IN6_IS_ADDR_MULTICAST(faddr) &&
    644      1.14   itojun 				 (mopt = in6p->in6p_moptions) &&
    645      1.14   itojun 				 mopt->im6o_multicast_ifp) {
    646      1.14   itojun 				oifp = mopt->im6o_multicast_ifp;
    647      1.27   itojun 				faddr->s6_addr16[1] = htons(oifp->if_index);
    648      1.14   itojun 			} else if (sin6->sin6_scope_id) {
    649      1.14   itojun 				/* boundary check */
    650      1.27   itojun 				if (sin6->sin6_scope_id < 0
    651      1.14   itojun 				    || if_index < sin6->sin6_scope_id) {
    652      1.14   itojun 					error = ENXIO;  /* XXX EINVAL? */
    653      1.14   itojun 					goto release;
    654      1.14   itojun 				}
    655      1.14   itojun 				/* XXX */
    656      1.14   itojun 				faddr->s6_addr16[1] =
    657      1.14   itojun 					htons(sin6->sin6_scope_id & 0xffff);
    658      1.14   itojun 			}
    659      1.14   itojun 		}
    660      1.14   itojun 
    661      1.14   itojun 		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
    662      1.14   itojun 			laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
    663      1.14   itojun 					      in6p->in6p_moptions,
    664      1.14   itojun 					      &in6p->in6p_route,
    665      1.14   itojun 					      &in6p->in6p_laddr, &error);
    666      1.14   itojun 		} else
    667      1.14   itojun 			laddr = &in6p->in6p_laddr;	/*XXX*/
    668      1.14   itojun 		if (laddr == NULL) {
    669      1.14   itojun 			if (error == 0)
    670      1.14   itojun 				error = EADDRNOTAVAIL;
    671       1.2   itojun 			goto release;
    672       1.2   itojun 		}
    673      1.14   itojun 		if (in6p->in6p_lport == 0 &&
    674      1.14   itojun 		    (error = in6_pcbsetport(laddr, in6p)) != 0)
    675      1.14   itojun 			goto release;
    676       1.2   itojun 	} else {
    677      1.14   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    678       1.2   itojun 			error = ENOTCONN;
    679       1.2   itojun 			goto release;
    680       1.2   itojun 		}
    681      1.14   itojun 		laddr = &in6p->in6p_laddr;
    682      1.14   itojun 		faddr = &in6p->in6p_faddr;
    683      1.14   itojun 		fport = in6p->in6p_fport;
    684       1.2   itojun 	}
    685      1.14   itojun 
    686      1.14   itojun 	if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
    687      1.14   itojun 		af = AF_INET6;
    688      1.14   itojun 		hlen = sizeof(struct ip6_hdr);
    689      1.14   itojun 	} else {
    690      1.14   itojun 		af = AF_INET;
    691      1.14   itojun 		hlen = sizeof(struct ip);
    692      1.14   itojun 	}
    693      1.14   itojun 
    694       1.2   itojun 	/*
    695       1.2   itojun 	 * Calculate data length and get a mbuf
    696       1.2   itojun 	 * for UDP and IP6 headers.
    697       1.2   itojun 	 */
    698      1.14   itojun 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
    699       1.2   itojun 	if (m == 0) {
    700       1.2   itojun 		error = ENOBUFS;
    701       1.2   itojun 		goto release;
    702       1.2   itojun 	}
    703       1.2   itojun 
    704       1.2   itojun 	/*
    705       1.2   itojun 	 * Stuff checksum and output datagram.
    706       1.2   itojun 	 */
    707      1.14   itojun 	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
    708      1.14   itojun 	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
    709      1.14   itojun 	udp6->uh_dport = fport;
    710      1.14   itojun 	if (plen <= 0xffff)
    711      1.14   itojun 		udp6->uh_ulen = htons((u_short)plen);
    712      1.14   itojun 	else
    713      1.14   itojun 		udp6->uh_ulen = 0;
    714      1.14   itojun 	udp6->uh_sum = 0;
    715      1.14   itojun 
    716      1.14   itojun 	switch (af) {
    717      1.14   itojun 	case AF_INET6:
    718      1.14   itojun 		ip6 = mtod(m, struct ip6_hdr *);
    719      1.14   itojun 		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    720      1.15   itojun 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
    721      1.15   itojun 		ip6->ip6_vfc 	|= IPV6_VERSION;
    722       1.2   itojun #if 0				/* ip6_plen will be filled in ip6_output. */
    723      1.14   itojun 		ip6->ip6_plen	= htons((u_short)plen);
    724       1.2   itojun #endif
    725      1.14   itojun 		ip6->ip6_nxt	= IPPROTO_UDP;
    726      1.14   itojun 		ip6->ip6_hlim	= in6_selecthlim(in6p,
    727      1.14   itojun 						 in6p->in6p_route.ro_rt ?
    728      1.14   itojun 						 in6p->in6p_route.ro_rt->rt_ifp : NULL);
    729      1.14   itojun 		ip6->ip6_src	= *laddr;
    730      1.14   itojun 		ip6->ip6_dst	= *faddr;
    731      1.14   itojun 
    732      1.14   itojun 		if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
    733      1.14   itojun 				sizeof(struct ip6_hdr), plen)) == 0) {
    734      1.14   itojun 			udp6->uh_sum = 0xffff;
    735      1.14   itojun 		}
    736      1.14   itojun 
    737      1.14   itojun 		udp6stat.udp6s_opackets++;
    738      1.14   itojun #ifdef IPSEC
    739  1.30.2.3       he 		if (ipsec_setsocket(m, in6p->in6p_socket) != 0) {
    740  1.30.2.3       he 			error = ENOBUFS;
    741  1.30.2.3       he 			goto release;
    742  1.30.2.3       he 		}
    743      1.14   itojun #endif /*IPSEC*/
    744      1.14   itojun 		error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
    745      1.14   itojun 			    0, in6p->in6p_moptions, NULL);
    746      1.14   itojun 		break;
    747      1.14   itojun 	case AF_INET:
    748      1.14   itojun #ifdef INET
    749      1.14   itojun 		/* can't transmit jumbogram over IPv4 */
    750      1.14   itojun 		if (plen > 0xffff) {
    751      1.14   itojun 			error = EMSGSIZE;
    752      1.14   itojun 			goto release;
    753      1.14   itojun 		}
    754      1.14   itojun 
    755      1.14   itojun 		ip = mtod(m, struct ip *);
    756       1.2   itojun 
    757      1.14   itojun 		ip->ip_len = plen;
    758      1.14   itojun 		ip->ip_p = IPPROTO_UDP;
    759  1.30.2.2       tv 		ip->ip_ttl = in6_selecthlim(in6p, NULL);	/*XXX*/
    760      1.14   itojun 		ip->ip_tos = 0;			/*XXX*/
    761      1.14   itojun 		bcopy(&laddr->s6_addr[12], &ip->ip_src, sizeof(ip->ip_src));
    762      1.14   itojun 		bcopy(&faddr->s6_addr[12], &ip->ip_dst, sizeof(ip->ip_dst));
    763      1.14   itojun 
    764      1.14   itojun 		udp6->uh_sum = 0;
    765      1.14   itojun 		if ((udp6->uh_sum = in_cksum(m, ulen)) == 0)
    766      1.14   itojun 			udp6->uh_sum = 0xffff;
    767       1.2   itojun 
    768      1.14   itojun 		udpstat.udps_opackets++;
    769       1.2   itojun #ifdef IPSEC
    770  1.30.2.3       he 		(void)ipsec_setsocket(m, NULL);	/*XXX*/
    771       1.2   itojun #endif /*IPSEC*/
    772      1.14   itojun 		error = ip_output(m, NULL, &in6p->in6p_route, 0 /*XXX*/);
    773      1.14   itojun 		break;
    774      1.14   itojun #else
    775      1.14   itojun 		error = EAFNOSUPPORT;
    776      1.14   itojun 		goto release;
    777      1.14   itojun #endif
    778       1.2   itojun 	}
    779       1.2   itojun 	goto releaseopt;
    780       1.2   itojun 
    781       1.2   itojun release:
    782       1.2   itojun 	m_freem(m);
    783       1.2   itojun 
    784       1.2   itojun releaseopt:
    785       1.2   itojun 	if (control) {
    786       1.2   itojun 		in6p->in6p_outputopts = stickyopt;
    787       1.2   itojun 		m_freem(control);
    788       1.2   itojun 	}
    789       1.2   itojun 	return(error);
    790       1.2   itojun }
    791       1.2   itojun 
    792       1.2   itojun extern	int udp6_sendspace;
    793       1.2   itojun extern	int udp6_recvspace;
    794       1.2   itojun 
    795       1.2   itojun int
    796       1.2   itojun udp6_usrreq(so, req, m, addr6, control, p)
    797       1.2   itojun 	struct socket *so;
    798       1.2   itojun 	int req;
    799       1.2   itojun 	struct mbuf *m, *addr6, *control;
    800       1.2   itojun 	struct proc *p;
    801       1.2   itojun {
    802       1.2   itojun 	struct	in6pcb *in6p = sotoin6pcb(so);
    803       1.2   itojun 	int	error = 0;
    804       1.2   itojun 	int	s;
    805       1.2   itojun 
    806      1.27   itojun 	/*
    807       1.2   itojun 	 * MAPPED_ADDR implementation info:
    808       1.2   itojun 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    809       1.2   itojun 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    810       1.2   itojun 	 *  and they don't associate any addr to their socket.  Then
    811       1.2   itojun 	 *  socket family is only hint about the PRU_CONTROL'ed address
    812       1.2   itojun 	 *  family, especially when getting addrs from kernel.
    813       1.2   itojun 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    814       1.2   itojun 	 *  and AF_INET6 socket for AF_INET6 addrs.
    815       1.2   itojun 	 */
    816       1.2   itojun 	if (req == PRU_CONTROL)
    817       1.2   itojun 		return(in6_control(so, (u_long)m, (caddr_t)addr6,
    818       1.2   itojun 				   (struct ifnet *)control, p));
    819      1.20  thorpej 
    820      1.21  thorpej 	if (req == PRU_PURGEIF) {
    821      1.21  thorpej 		in6_purgeif((struct ifnet *)control);
    822      1.21  thorpej 		in6_pcbpurgeif(&udb6, (struct ifnet *)control);
    823      1.20  thorpej 		return (0);
    824      1.20  thorpej 	}
    825       1.2   itojun 
    826       1.2   itojun 	if (in6p == NULL && req != PRU_ATTACH) {
    827       1.2   itojun 		error = EINVAL;
    828       1.2   itojun 		goto release;
    829       1.2   itojun 	}
    830       1.2   itojun 
    831       1.2   itojun 	switch (req) {
    832       1.2   itojun 	case PRU_ATTACH:
    833       1.2   itojun 		/*
    834       1.2   itojun 		 * MAPPED_ADDR implementation spec:
    835      1.27   itojun 		 *  Always attach for IPv6,
    836       1.2   itojun 		 *  and only when necessary for IPv4.
    837       1.2   itojun 		 */
    838       1.2   itojun 		if (in6p != NULL) {
    839       1.2   itojun 			error = EINVAL;
    840       1.2   itojun 			break;
    841       1.2   itojun 		}
    842       1.4   itojun 		s = splsoftnet();
    843       1.2   itojun 		error = in6_pcballoc(so, &udb6);
    844       1.2   itojun 		splx(s);
    845       1.2   itojun 		if (error)
    846       1.2   itojun 			break;
    847       1.2   itojun 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
    848       1.2   itojun 		if (error)
    849       1.2   itojun 			break;
    850       1.2   itojun 		in6p = sotoin6pcb(so);
    851       1.2   itojun 		in6p->in6p_cksum = -1;	/* just to be sure */
    852       1.2   itojun #ifdef IPSEC
    853      1.19   itojun 		error = ipsec_init_policy(so, &in6p->in6p_sp);
    854      1.14   itojun 		if (error != 0) {
    855      1.13   itojun 			in6_pcbdetach(in6p);
    856      1.14   itojun 			break;
    857      1.14   itojun 		}
    858       1.2   itojun #endif /*IPSEC*/
    859       1.2   itojun 		break;
    860       1.2   itojun 
    861       1.2   itojun 	case PRU_DETACH:
    862       1.2   itojun 		udp6_detach(in6p);
    863       1.2   itojun 		break;
    864       1.2   itojun 
    865       1.2   itojun 	case PRU_BIND:
    866       1.4   itojun 		s = splsoftnet();
    867      1.29   itojun 		error = in6_pcbbind(in6p, addr6, p);
    868       1.2   itojun 		splx(s);
    869       1.2   itojun 		break;
    870       1.2   itojun 
    871       1.2   itojun 	case PRU_LISTEN:
    872       1.2   itojun 		error = EOPNOTSUPP;
    873       1.2   itojun 		break;
    874       1.2   itojun 
    875       1.2   itojun 	case PRU_CONNECT:
    876      1.14   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    877       1.2   itojun 			error = EISCONN;
    878       1.2   itojun 			break;
    879       1.2   itojun 		}
    880       1.4   itojun 		s = splsoftnet();
    881       1.2   itojun 		error = in6_pcbconnect(in6p, addr6);
    882       1.2   itojun 		if (ip6_auto_flowlabel) {
    883       1.2   itojun 			in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    884      1.27   itojun 			in6p->in6p_flowinfo |=
    885       1.2   itojun 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    886       1.2   itojun 		}
    887       1.2   itojun 		splx(s);
    888       1.2   itojun 		if (error == 0)
    889       1.2   itojun 			soisconnected(so);
    890       1.2   itojun 		break;
    891       1.2   itojun 
    892       1.2   itojun 	case PRU_CONNECT2:
    893       1.2   itojun 		error = EOPNOTSUPP;
    894       1.2   itojun 		break;
    895       1.2   itojun 
    896       1.2   itojun 	case PRU_ACCEPT:
    897       1.2   itojun 		error = EOPNOTSUPP;
    898       1.2   itojun 		break;
    899       1.2   itojun 
    900       1.2   itojun 	case PRU_DISCONNECT:
    901       1.2   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    902       1.2   itojun 			error = ENOTCONN;
    903       1.2   itojun 			break;
    904       1.2   itojun 		}
    905       1.4   itojun 		s = splsoftnet();
    906       1.2   itojun 		in6_pcbdisconnect(in6p);
    907       1.2   itojun 		bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
    908       1.2   itojun 		splx(s);
    909       1.2   itojun 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    910       1.2   itojun 		break;
    911       1.2   itojun 
    912       1.2   itojun 	case PRU_SHUTDOWN:
    913       1.2   itojun 		socantsendmore(so);
    914       1.2   itojun 		break;
    915       1.2   itojun 
    916       1.2   itojun 	case PRU_SEND:
    917      1.29   itojun 		return(udp6_output(in6p, m, addr6, control, p));
    918       1.2   itojun 
    919       1.2   itojun 	case PRU_ABORT:
    920       1.2   itojun 		soisdisconnected(so);
    921       1.2   itojun 		udp6_detach(in6p);
    922       1.2   itojun 		break;
    923       1.2   itojun 
    924       1.2   itojun 	case PRU_SOCKADDR:
    925       1.2   itojun 		in6_setsockaddr(in6p, addr6);
    926       1.2   itojun 		break;
    927       1.2   itojun 
    928       1.2   itojun 	case PRU_PEERADDR:
    929       1.2   itojun 		in6_setpeeraddr(in6p, addr6);
    930       1.2   itojun 		break;
    931       1.2   itojun 
    932       1.2   itojun 	case PRU_SENSE:
    933       1.2   itojun 		/*
    934       1.2   itojun 		 * stat: don't bother with a blocksize
    935       1.2   itojun 		 */
    936       1.2   itojun 		return(0);
    937       1.2   itojun 
    938       1.2   itojun 	case PRU_SENDOOB:
    939       1.2   itojun 	case PRU_FASTTIMO:
    940       1.2   itojun 	case PRU_SLOWTIMO:
    941       1.2   itojun 	case PRU_PROTORCV:
    942       1.2   itojun 	case PRU_PROTOSEND:
    943       1.2   itojun 		error = EOPNOTSUPP;
    944       1.2   itojun 		break;
    945       1.2   itojun 
    946       1.2   itojun 	case PRU_RCVD:
    947       1.2   itojun 	case PRU_RCVOOB:
    948       1.2   itojun 		return(EOPNOTSUPP);	/* do not free mbuf's */
    949       1.2   itojun 
    950       1.2   itojun 	default:
    951       1.2   itojun 		panic("udp6_usrreq");
    952       1.2   itojun 	}
    953       1.2   itojun 
    954       1.2   itojun release:
    955       1.2   itojun 	if (control) {
    956       1.2   itojun 		printf("udp control data unexpectedly retained\n");
    957       1.2   itojun 		m_freem(control);
    958       1.2   itojun 	}
    959       1.2   itojun 	if (m)
    960       1.2   itojun 		m_freem(m);
    961       1.2   itojun 	return(error);
    962       1.2   itojun }
    963       1.2   itojun 
    964       1.2   itojun static void
    965       1.2   itojun udp6_detach(in6p)
    966       1.2   itojun 	struct in6pcb *in6p;
    967       1.2   itojun {
    968      1.27   itojun 	int s = splsoftnet();
    969       1.2   itojun 
    970       1.2   itojun 	if (in6p == udp6_last_in6pcb)
    971       1.2   itojun 		udp6_last_in6pcb = &udb6;
    972       1.2   itojun 	in6_pcbdetach(in6p);
    973       1.2   itojun 	splx(s);
    974       1.2   itojun }
    975       1.2   itojun 
    976       1.2   itojun #include <vm/vm.h>
    977       1.2   itojun #include <sys/sysctl.h>
    978       1.2   itojun 
    979       1.2   itojun int
    980       1.2   itojun udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    981       1.2   itojun 	int *name;
    982       1.2   itojun 	u_int namelen;
    983       1.2   itojun 	void *oldp;
    984       1.2   itojun 	size_t *oldlenp;
    985       1.2   itojun 	void *newp;
    986       1.2   itojun 	size_t newlen;
    987       1.2   itojun {
    988       1.2   itojun 	/* All sysctl names at this level are terminal. */
    989       1.2   itojun 	if (namelen != 1)
    990       1.2   itojun 		return ENOTDIR;
    991       1.2   itojun 
    992       1.2   itojun 	switch (name[0]) {
    993       1.2   itojun 
    994      1.28   itojun 	case UDP6CTL_SENDSPACE:
    995       1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    996       1.2   itojun 		    &udp6_sendspace);
    997       1.2   itojun 	case UDP6CTL_RECVSPACE:
    998      1.27   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    999       1.2   itojun 		    &udp6_recvspace);
   1000       1.2   itojun 	default:
   1001       1.2   itojun 		return ENOPROTOOPT;
   1002       1.2   itojun 	}
   1003       1.2   itojun 	/* NOTREACHED */
   1004       1.2   itojun }
   1005