Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.40.2.3
      1  1.40.2.3  nathanw /*	$NetBSD: udp6_usrreq.c,v 1.40.2.3 2001/10/22 20:42:06 nathanw Exp $	*/
      2  1.40.2.1  nathanw /*	$KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 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.40.2.1  nathanw #if defined(NFAITH) && NFAITH > 0
    107  1.40.2.1  nathanw #include <net/if_faith.h>
    108  1.40.2.1  nathanw #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.39   itojun 	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.39   itojun 	struct ip6_hdr *ip6;
    160      1.39   itojun 	struct udphdr *uh;
    161      1.39   itojun 	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.40.2.1  nathanw 	ip6 = mtod(m, struct ip6_hdr *);
    168  1.40.2.1  nathanw 
    169       1.2   itojun #if defined(NFAITH) && 0 < NFAITH
    170  1.40.2.1  nathanw 	if (faithprefix(&ip6->ip6_dst)) {
    171  1.40.2.1  nathanw 		/* send icmp6 host unreach? */
    172  1.40.2.1  nathanw 		m_freem(m);
    173  1.40.2.1  nathanw 		return IPPROTO_DONE;
    174       1.2   itojun 	}
    175       1.2   itojun #endif
    176  1.40.2.1  nathanw 
    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.32   itojun #if 0 /*XXX inbound flowinfo */
    262      1.32   itojun 		udp_in6.sin6_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
    263      1.32   itojun #endif
    264      1.32   itojun 		/* KAME hack: recover scopeid */
    265      1.32   itojun 		(void)in6_recoverscope(&udp_in6, &ip6->ip6_src,
    266      1.32   itojun 		    m->m_pkthdr.rcvif);
    267      1.32   itojun 
    268       1.2   itojun 		/*
    269  1.40.2.3  nathanw 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    270       1.2   itojun 		 * We need udphdr for IPsec processing so we do that later.
    271       1.2   itojun 		 */
    272       1.2   itojun 
    273       1.2   itojun 		/*
    274       1.2   itojun 		 * Locate pcb(s) for datagram.
    275       1.2   itojun 		 * (Algorithm copied from raw_intr().)
    276       1.2   itojun 		 */
    277       1.2   itojun 		last = NULL;
    278       1.2   itojun 		for (in6p = udb6.in6p_next;
    279       1.2   itojun 		     in6p != &udb6;
    280       1.2   itojun 		     in6p = in6p->in6p_next) {
    281       1.2   itojun 			if (in6p->in6p_lport != uh->uh_dport)
    282       1.2   itojun 				continue;
    283      1.14   itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    284       1.2   itojun 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    285       1.2   itojun 							&ip6->ip6_dst) &&
    286       1.2   itojun 				    !in6_mcmatch(in6p, &ip6->ip6_dst,
    287       1.2   itojun 						 m->m_pkthdr.rcvif))
    288       1.2   itojun 					continue;
    289       1.2   itojun 			}
    290      1.14   itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    291       1.2   itojun 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    292       1.2   itojun 							&ip6->ip6_src) ||
    293       1.2   itojun 				   in6p->in6p_fport != uh->uh_sport)
    294       1.2   itojun 					continue;
    295       1.2   itojun 			}
    296       1.2   itojun 
    297       1.2   itojun 			if (last != NULL) {
    298       1.2   itojun 				struct	mbuf *n;
    299       1.2   itojun 
    300       1.2   itojun #ifdef IPSEC
    301       1.2   itojun 				/*
    302       1.2   itojun 				 * Check AH/ESP integrity.
    303       1.2   itojun 				 */
    304      1.24   itojun 				if (ipsec6_in_reject(m, last)) {
    305       1.2   itojun 					ipsec6stat.in_polvio++;
    306       1.2   itojun 					/* do not inject data into pcb */
    307       1.2   itojun 				} else
    308       1.2   itojun #endif /*IPSEC*/
    309       1.2   itojun 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    310       1.2   itojun 					/*
    311       1.2   itojun 					 * KAME NOTE: do not
    312       1.2   itojun 					 * m_copy(m, offset, ...) above.
    313       1.2   itojun 					 * sbappendaddr() expects M_PKTHDR,
    314       1.2   itojun 					 * and m_copy() will copy M_PKTHDR
    315       1.2   itojun 					 * only if offset is 0.
    316       1.2   itojun 					 */
    317       1.8   itojun 					if (last->in6p_flags & IN6P_CONTROLOPTS
    318       1.8   itojun 					 || last->in6p_socket->so_options & SO_TIMESTAMP) {
    319       1.2   itojun 						ip6_savecontrol(last, &opts,
    320       1.2   itojun 								ip6, n);
    321       1.2   itojun 					}
    322       1.2   itojun 
    323      1.11   itojun 					m_adj(n, off + sizeof(struct udphdr));
    324       1.2   itojun 					if (sbappendaddr(&last->in6p_socket->so_rcv,
    325       1.2   itojun 							(struct sockaddr *)&udp_in6,
    326       1.2   itojun 							n, opts) == 0) {
    327       1.2   itojun 						m_freem(n);
    328       1.2   itojun 						if (opts)
    329       1.2   itojun 							m_freem(opts);
    330       1.2   itojun 						udp6stat.udp6s_fullsock++;
    331       1.2   itojun 					} else
    332       1.2   itojun 						sorwakeup(last->in6p_socket);
    333       1.2   itojun 					opts = 0;
    334       1.2   itojun 				}
    335       1.2   itojun 			}
    336       1.2   itojun 			last = in6p;
    337       1.2   itojun 			/*
    338       1.2   itojun 			 * Don't look for additional matches if this one does
    339       1.2   itojun 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    340       1.2   itojun 			 * socket options set.  This heuristic avoids searching
    341       1.2   itojun 			 * through all pcbs in the common case of a non-shared
    342       1.2   itojun 			 * port.  It assumes that an application will never
    343       1.2   itojun 			 * clear these options after setting them.
    344       1.2   itojun 			 */
    345       1.2   itojun 			if ((last->in6p_socket->so_options &
    346       1.2   itojun 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    347       1.2   itojun 				break;
    348       1.2   itojun 		}
    349       1.2   itojun 
    350       1.2   itojun 		if (last == NULL) {
    351       1.2   itojun 			/*
    352       1.2   itojun 			 * No matching pcb found; discard datagram.
    353       1.2   itojun 			 * (No need to send an ICMP Port Unreachable
    354       1.2   itojun 			 * for a broadcast or multicast datgram.)
    355       1.2   itojun 			 */
    356       1.2   itojun 			udp6stat.udp6s_noport++;
    357       1.2   itojun 			udp6stat.udp6s_noportmcast++;
    358       1.2   itojun 			goto bad;
    359       1.2   itojun 		}
    360       1.2   itojun #ifdef IPSEC
    361       1.2   itojun 		/*
    362       1.2   itojun 		 * Check AH/ESP integrity.
    363       1.2   itojun 		 */
    364       1.2   itojun 		if (last != NULL && ipsec6_in_reject(m, last)) {
    365       1.2   itojun 			ipsec6stat.in_polvio++;
    366       1.2   itojun 			goto bad;
    367       1.2   itojun 		}
    368       1.2   itojun #endif /*IPSEC*/
    369       1.8   itojun 		if (last->in6p_flags & IN6P_CONTROLOPTS
    370       1.8   itojun 		 || last->in6p_socket->so_options & SO_TIMESTAMP) {
    371       1.2   itojun 			ip6_savecontrol(last, &opts, ip6, m);
    372       1.8   itojun 		}
    373       1.2   itojun 
    374       1.2   itojun 		m_adj(m, off + sizeof(struct udphdr));
    375       1.2   itojun 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    376       1.2   itojun 				(struct sockaddr *)&udp_in6,
    377       1.2   itojun 				m, opts) == 0) {
    378       1.2   itojun 			udp6stat.udp6s_fullsock++;
    379       1.2   itojun 			goto bad;
    380       1.2   itojun 		}
    381       1.2   itojun 		sorwakeup(last->in6p_socket);
    382       1.2   itojun 		return IPPROTO_DONE;
    383       1.2   itojun 	}
    384       1.2   itojun 	/*
    385       1.2   itojun 	 * Locate pcb for datagram.
    386       1.2   itojun 	 */
    387       1.2   itojun 	in6p = udp6_last_in6pcb;
    388       1.2   itojun 	if (in6p->in6p_lport != uh->uh_dport ||
    389       1.2   itojun 	   in6p->in6p_fport != uh->uh_sport ||
    390       1.2   itojun 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) ||
    391       1.2   itojun 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
    392       1.2   itojun 		in6p = in6_pcblookup(&udb6,
    393       1.2   itojun 				     &ip6->ip6_src, uh->uh_sport,
    394       1.2   itojun 				     &ip6->ip6_dst, uh->uh_dport,
    395       1.2   itojun 				     IN6PLOOKUP_WILDCARD);
    396       1.2   itojun 		if (in6p)
    397       1.2   itojun 			udp6_last_in6pcb = in6p;
    398       1.2   itojun 		udp6stat.udp6ps_pcbcachemiss++;
    399       1.2   itojun 	}
    400       1.2   itojun 	if (in6p == 0) {
    401       1.2   itojun 		udp6stat.udp6s_noport++;
    402       1.2   itojun 		if (m->m_flags & M_MCAST) {
    403       1.2   itojun 			udp6stat.udp6s_noportmcast++;
    404       1.2   itojun 			goto bad;
    405       1.2   itojun 		}
    406       1.2   itojun 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    407       1.2   itojun 		return IPPROTO_DONE;
    408       1.2   itojun 	}
    409       1.2   itojun #ifdef IPSEC
    410       1.2   itojun 	/*
    411       1.2   itojun 	 * Check AH/ESP integrity.
    412       1.2   itojun 	 */
    413       1.2   itojun 	if (in6p != NULL && ipsec6_in_reject(m, in6p)) {
    414       1.2   itojun 		ipsec6stat.in_polvio++;
    415       1.2   itojun 		goto bad;
    416       1.2   itojun 	}
    417       1.2   itojun #endif /*IPSEC*/
    418       1.2   itojun 
    419       1.2   itojun 	/*
    420       1.2   itojun 	 * Construct sockaddr format source address.
    421       1.2   itojun 	 * Stuff source address and datagram in user buffer.
    422       1.2   itojun 	 */
    423       1.2   itojun 	bzero(&udp_in6, sizeof(udp_in6));
    424       1.2   itojun 	udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    425       1.2   itojun 	udp_in6.sin6_family = AF_INET6;
    426       1.2   itojun 	udp_in6.sin6_port = uh->uh_sport;
    427      1.32   itojun 	/* KAME hack: recover scopeid */
    428      1.32   itojun 	(void)in6_recoverscope(&udp_in6, &ip6->ip6_src, m->m_pkthdr.rcvif);
    429       1.8   itojun 	if (in6p->in6p_flags & IN6P_CONTROLOPTS
    430       1.8   itojun 	 || in6p->in6p_socket->so_options & SO_TIMESTAMP) {
    431       1.2   itojun 		ip6_savecontrol(in6p, &opts, ip6, m);
    432       1.8   itojun 	}
    433       1.2   itojun 
    434       1.2   itojun 	m_adj(m, off + sizeof(struct udphdr));
    435       1.2   itojun 	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
    436       1.2   itojun 			(struct sockaddr *)&udp_in6,
    437       1.2   itojun 			m, opts) == 0) {
    438       1.2   itojun 		udp6stat.udp6s_fullsock++;
    439       1.2   itojun 		goto bad;
    440       1.2   itojun 	}
    441       1.2   itojun 	sorwakeup(in6p->in6p_socket);
    442       1.2   itojun 	return IPPROTO_DONE;
    443       1.2   itojun bad:
    444       1.2   itojun 	if (m)
    445       1.2   itojun 		m_freem(m);
    446       1.2   itojun 	if (opts)
    447       1.2   itojun 		m_freem(opts);
    448       1.2   itojun 	return IPPROTO_DONE;
    449       1.2   itojun }
    450      1.14   itojun #endif
    451       1.2   itojun 
    452       1.2   itojun /*
    453       1.2   itojun  * Notify a udp user of an asynchronous error;
    454      1.27   itojun  * just wake up so that he can collect error status.
    455       1.2   itojun  */
    456       1.2   itojun static	void
    457       1.2   itojun udp6_notify(in6p, errno)
    458      1.39   itojun 	struct in6pcb *in6p;
    459       1.2   itojun 	int errno;
    460       1.2   itojun {
    461       1.2   itojun 	in6p->in6p_socket->so_error = errno;
    462       1.2   itojun 	sorwakeup(in6p->in6p_socket);
    463       1.2   itojun 	sowwakeup(in6p->in6p_socket);
    464       1.2   itojun }
    465       1.2   itojun 
    466       1.2   itojun void
    467      1.14   itojun udp6_ctlinput(cmd, sa, d)
    468       1.2   itojun 	int cmd;
    469       1.2   itojun 	struct sockaddr *sa;
    470      1.14   itojun 	void *d;
    471       1.2   itojun {
    472       1.2   itojun 	struct udphdr uh;
    473      1.39   itojun 	struct ip6_hdr *ip6;
    474      1.40   itojun 	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
    475      1.14   itojun 	struct mbuf *m;
    476      1.14   itojun 	int off;
    477      1.40   itojun 	void *cmdarg;
    478      1.40   itojun 	struct ip6ctlparam *ip6cp = NULL;
    479      1.40   itojun 	const struct sockaddr_in6 *sa6_src = NULL;
    480      1.25   itojun 	void (*notify) __P((struct in6pcb *, int)) = udp6_notify;
    481      1.40   itojun 	struct udp_portonly {
    482      1.40   itojun 		u_int16_t uh_sport;
    483      1.40   itojun 		u_int16_t uh_dport;
    484      1.40   itojun 	} *uhp;
    485       1.2   itojun 
    486      1.10   itojun 	if (sa->sa_family != AF_INET6 ||
    487      1.10   itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    488      1.10   itojun 		return;
    489      1.14   itojun 
    490      1.25   itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    491      1.25   itojun 		return;
    492      1.25   itojun 	if (PRC_IS_REDIRECT(cmd))
    493      1.25   itojun 		notify = in6_rtchange, d = NULL;
    494      1.25   itojun 	else if (cmd == PRC_HOSTDEAD)
    495      1.25   itojun 		d = NULL;
    496  1.40.2.3  nathanw 	else if (cmd == PRC_MSGSIZE) {
    497  1.40.2.3  nathanw 		/* special code is present, see below */
    498  1.40.2.3  nathanw 		notify = in6_rtchange;
    499  1.40.2.3  nathanw 	}
    500      1.25   itojun 	else if (inet6ctlerrmap[cmd] == 0)
    501       1.2   itojun 		return;
    502       1.7   itojun 
    503      1.14   itojun 	/* if the parameter is from icmp6, decode it. */
    504      1.14   itojun 	if (d != NULL) {
    505      1.40   itojun 		ip6cp = (struct ip6ctlparam *)d;
    506      1.14   itojun 		m = ip6cp->ip6c_m;
    507      1.14   itojun 		ip6 = ip6cp->ip6c_ip6;
    508      1.14   itojun 		off = ip6cp->ip6c_off;
    509      1.40   itojun 		cmdarg = ip6cp->ip6c_cmdarg;
    510      1.40   itojun 		sa6_src = ip6cp->ip6c_src;
    511      1.14   itojun 	} else {
    512      1.14   itojun 		m = NULL;
    513      1.14   itojun 		ip6 = NULL;
    514      1.40   itojun 		cmdarg = NULL;
    515      1.40   itojun 		sa6_src = &sa6_any;
    516      1.14   itojun 	}
    517      1.14   itojun 
    518       1.2   itojun 	if (ip6) {
    519       1.2   itojun 		/*
    520       1.2   itojun 		 * XXX: We assume that when IPV6 is non NULL,
    521       1.2   itojun 		 * M and OFF are valid.
    522       1.2   itojun 		 */
    523      1.33   itojun 
    524      1.33   itojun 		/* check if we can safely examine src and dst ports */
    525  1.40.2.1  nathanw 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    526  1.40.2.1  nathanw 			if (cmd == PRC_MSGSIZE)
    527  1.40.2.1  nathanw 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    528      1.33   itojun 			return;
    529  1.40.2.1  nathanw 		}
    530       1.7   itojun 
    531      1.40   itojun 		bzero(&uh, sizeof(uh));
    532      1.40   itojun 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
    533      1.34   itojun 
    534      1.34   itojun 		if (cmd == PRC_MSGSIZE) {
    535      1.36   itojun 			int valid = 0;
    536      1.40   itojun 
    537      1.34   itojun 			/*
    538      1.34   itojun 			 * Check to see if we have a valid UDP socket
    539      1.34   itojun 			 * corresponding to the address in the ICMPv6 message
    540      1.34   itojun 			 * payload.
    541      1.34   itojun 			 */
    542      1.40   itojun 			if (in6_pcblookup_connect(&udb6, &sa6->sin6_addr,
    543      1.40   itojun 			    uh.uh_dport, (struct in6_addr *)&sa6_src->sin6_addr,
    544      1.40   itojun 			    uh.uh_sport, 0))
    545      1.36   itojun 				valid++;
    546      1.34   itojun #if 0
    547      1.34   itojun 			/*
    548      1.34   itojun 			 * As the use of sendto(2) is fairly popular,
    549      1.34   itojun 			 * we may want to allow non-connected pcb too.
    550      1.34   itojun 			 * But it could be too weak against attacks...
    551      1.34   itojun 			 * We should at least check if the local address (= s)
    552      1.34   itojun 			 * is really ours.
    553      1.34   itojun 			 */
    554      1.40   itojun 			else if (in6_pcblookup_bind(&udb6, &sa6->sin6_addr,
    555      1.40   itojun 						    uh.uh_dport, 0))
    556      1.36   itojun 				valid++;
    557      1.34   itojun #endif
    558      1.34   itojun 
    559      1.34   itojun 			/*
    560      1.40   itojun 			 * Depending on the value of "valid" and routing table
    561      1.40   itojun 			 * size (mtudisc_{hi,lo}wat), we will:
    562  1.40.2.3  nathanw 			 * - recalculate the new MTU and create the
    563      1.40   itojun 			 *   corresponding routing entry, or
    564      1.40   itojun 			 * - ignore the MTU change notification.
    565      1.34   itojun 			 */
    566      1.36   itojun 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    567      1.34   itojun 
    568      1.40   itojun 			/*
    569      1.40   itojun 			 * regardless of if we called icmp6_mtudisc_update(),
    570      1.40   itojun 			 * we need to call in6_pcbnotify(), to notify path
    571      1.40   itojun 			 * MTU change to the userland (2292bis-02), because
    572      1.40   itojun 			 * some unconnected sockets may share the same
    573      1.40   itojun 			 * destination and want to know the path MTU.
    574      1.40   itojun 			 */
    575      1.34   itojun 		}
    576      1.34   itojun 
    577      1.40   itojun 		(void) in6_pcbnotify(&udb6, sa, uh.uh_dport,
    578      1.40   itojun 		    (struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
    579      1.40   itojun 		    notify);
    580       1.2   itojun 	} else {
    581      1.40   itojun 		(void) in6_pcbnotify(&udb6, sa, 0, (struct sockaddr *)sa6_src,
    582      1.40   itojun 		    0, cmd, cmdarg, notify);
    583       1.2   itojun 	}
    584       1.2   itojun }
    585       1.2   itojun 
    586       1.2   itojun extern	int udp6_sendspace;
    587       1.2   itojun extern	int udp6_recvspace;
    588       1.2   itojun 
    589       1.2   itojun int
    590       1.2   itojun udp6_usrreq(so, req, m, addr6, control, p)
    591       1.2   itojun 	struct socket *so;
    592       1.2   itojun 	int req;
    593       1.2   itojun 	struct mbuf *m, *addr6, *control;
    594       1.2   itojun 	struct proc *p;
    595       1.2   itojun {
    596       1.2   itojun 	struct	in6pcb *in6p = sotoin6pcb(so);
    597       1.2   itojun 	int	error = 0;
    598       1.2   itojun 	int	s;
    599       1.2   itojun 
    600      1.27   itojun 	/*
    601       1.2   itojun 	 * MAPPED_ADDR implementation info:
    602       1.2   itojun 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    603       1.2   itojun 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    604       1.2   itojun 	 *  and they don't associate any addr to their socket.  Then
    605       1.2   itojun 	 *  socket family is only hint about the PRU_CONTROL'ed address
    606       1.2   itojun 	 *  family, especially when getting addrs from kernel.
    607       1.2   itojun 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    608       1.2   itojun 	 *  and AF_INET6 socket for AF_INET6 addrs.
    609       1.2   itojun 	 */
    610       1.2   itojun 	if (req == PRU_CONTROL)
    611       1.2   itojun 		return(in6_control(so, (u_long)m, (caddr_t)addr6,
    612       1.2   itojun 				   (struct ifnet *)control, p));
    613      1.20  thorpej 
    614      1.21  thorpej 	if (req == PRU_PURGEIF) {
    615  1.40.2.2  nathanw 		in6_pcbpurgeif0(&udb6, (struct ifnet *)control);
    616      1.21  thorpej 		in6_purgeif((struct ifnet *)control);
    617      1.21  thorpej 		in6_pcbpurgeif(&udb6, (struct ifnet *)control);
    618      1.20  thorpej 		return (0);
    619      1.20  thorpej 	}
    620       1.2   itojun 
    621       1.2   itojun 	if (in6p == NULL && req != PRU_ATTACH) {
    622       1.2   itojun 		error = EINVAL;
    623       1.2   itojun 		goto release;
    624       1.2   itojun 	}
    625       1.2   itojun 
    626       1.2   itojun 	switch (req) {
    627       1.2   itojun 	case PRU_ATTACH:
    628       1.2   itojun 		/*
    629       1.2   itojun 		 * MAPPED_ADDR implementation spec:
    630      1.27   itojun 		 *  Always attach for IPv6,
    631       1.2   itojun 		 *  and only when necessary for IPv4.
    632       1.2   itojun 		 */
    633       1.2   itojun 		if (in6p != NULL) {
    634       1.2   itojun 			error = EINVAL;
    635       1.2   itojun 			break;
    636       1.2   itojun 		}
    637       1.4   itojun 		s = splsoftnet();
    638       1.2   itojun 		error = in6_pcballoc(so, &udb6);
    639       1.2   itojun 		splx(s);
    640       1.2   itojun 		if (error)
    641       1.2   itojun 			break;
    642       1.2   itojun 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
    643       1.2   itojun 		if (error)
    644       1.2   itojun 			break;
    645       1.2   itojun 		in6p = sotoin6pcb(so);
    646       1.2   itojun 		in6p->in6p_cksum = -1;	/* just to be sure */
    647       1.2   itojun 		break;
    648       1.2   itojun 
    649       1.2   itojun 	case PRU_DETACH:
    650       1.2   itojun 		udp6_detach(in6p);
    651       1.2   itojun 		break;
    652       1.2   itojun 
    653       1.2   itojun 	case PRU_BIND:
    654       1.4   itojun 		s = splsoftnet();
    655      1.29   itojun 		error = in6_pcbbind(in6p, addr6, p);
    656       1.2   itojun 		splx(s);
    657       1.2   itojun 		break;
    658       1.2   itojun 
    659       1.2   itojun 	case PRU_LISTEN:
    660       1.2   itojun 		error = EOPNOTSUPP;
    661       1.2   itojun 		break;
    662       1.2   itojun 
    663       1.2   itojun 	case PRU_CONNECT:
    664      1.14   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    665       1.2   itojun 			error = EISCONN;
    666       1.2   itojun 			break;
    667       1.2   itojun 		}
    668       1.4   itojun 		s = splsoftnet();
    669       1.2   itojun 		error = in6_pcbconnect(in6p, addr6);
    670       1.2   itojun 		if (ip6_auto_flowlabel) {
    671       1.2   itojun 			in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    672      1.27   itojun 			in6p->in6p_flowinfo |=
    673       1.2   itojun 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    674       1.2   itojun 		}
    675       1.2   itojun 		splx(s);
    676       1.2   itojun 		if (error == 0)
    677       1.2   itojun 			soisconnected(so);
    678       1.2   itojun 		break;
    679       1.2   itojun 
    680       1.2   itojun 	case PRU_CONNECT2:
    681       1.2   itojun 		error = EOPNOTSUPP;
    682       1.2   itojun 		break;
    683       1.2   itojun 
    684       1.2   itojun 	case PRU_ACCEPT:
    685       1.2   itojun 		error = EOPNOTSUPP;
    686       1.2   itojun 		break;
    687       1.2   itojun 
    688       1.2   itojun 	case PRU_DISCONNECT:
    689       1.2   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    690       1.2   itojun 			error = ENOTCONN;
    691       1.2   itojun 			break;
    692       1.2   itojun 		}
    693       1.4   itojun 		s = splsoftnet();
    694       1.2   itojun 		in6_pcbdisconnect(in6p);
    695       1.2   itojun 		bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
    696       1.2   itojun 		splx(s);
    697       1.2   itojun 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    698       1.2   itojun 		break;
    699       1.2   itojun 
    700       1.2   itojun 	case PRU_SHUTDOWN:
    701       1.2   itojun 		socantsendmore(so);
    702       1.2   itojun 		break;
    703       1.2   itojun 
    704       1.2   itojun 	case PRU_SEND:
    705      1.29   itojun 		return(udp6_output(in6p, m, addr6, control, p));
    706       1.2   itojun 
    707       1.2   itojun 	case PRU_ABORT:
    708       1.2   itojun 		soisdisconnected(so);
    709       1.2   itojun 		udp6_detach(in6p);
    710       1.2   itojun 		break;
    711       1.2   itojun 
    712       1.2   itojun 	case PRU_SOCKADDR:
    713       1.2   itojun 		in6_setsockaddr(in6p, addr6);
    714       1.2   itojun 		break;
    715       1.2   itojun 
    716       1.2   itojun 	case PRU_PEERADDR:
    717       1.2   itojun 		in6_setpeeraddr(in6p, addr6);
    718       1.2   itojun 		break;
    719       1.2   itojun 
    720       1.2   itojun 	case PRU_SENSE:
    721       1.2   itojun 		/*
    722       1.2   itojun 		 * stat: don't bother with a blocksize
    723       1.2   itojun 		 */
    724       1.2   itojun 		return(0);
    725       1.2   itojun 
    726       1.2   itojun 	case PRU_SENDOOB:
    727       1.2   itojun 	case PRU_FASTTIMO:
    728       1.2   itojun 	case PRU_SLOWTIMO:
    729       1.2   itojun 	case PRU_PROTORCV:
    730       1.2   itojun 	case PRU_PROTOSEND:
    731       1.2   itojun 		error = EOPNOTSUPP;
    732       1.2   itojun 		break;
    733       1.2   itojun 
    734       1.2   itojun 	case PRU_RCVD:
    735       1.2   itojun 	case PRU_RCVOOB:
    736       1.2   itojun 		return(EOPNOTSUPP);	/* do not free mbuf's */
    737       1.2   itojun 
    738       1.2   itojun 	default:
    739       1.2   itojun 		panic("udp6_usrreq");
    740       1.2   itojun 	}
    741       1.2   itojun 
    742       1.2   itojun release:
    743  1.40.2.1  nathanw 	if (control)
    744       1.2   itojun 		m_freem(control);
    745       1.2   itojun 	if (m)
    746       1.2   itojun 		m_freem(m);
    747       1.2   itojun 	return(error);
    748       1.2   itojun }
    749       1.2   itojun 
    750       1.2   itojun static void
    751       1.2   itojun udp6_detach(in6p)
    752       1.2   itojun 	struct in6pcb *in6p;
    753       1.2   itojun {
    754      1.27   itojun 	int s = splsoftnet();
    755       1.2   itojun 
    756       1.2   itojun 	if (in6p == udp6_last_in6pcb)
    757       1.2   itojun 		udp6_last_in6pcb = &udb6;
    758       1.2   itojun 	in6_pcbdetach(in6p);
    759       1.2   itojun 	splx(s);
    760       1.2   itojun }
    761       1.2   itojun 
    762      1.31      mrg #include <uvm/uvm_extern.h>
    763       1.2   itojun #include <sys/sysctl.h>
    764       1.2   itojun 
    765       1.2   itojun int
    766       1.2   itojun udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    767       1.2   itojun 	int *name;
    768       1.2   itojun 	u_int namelen;
    769       1.2   itojun 	void *oldp;
    770       1.2   itojun 	size_t *oldlenp;
    771       1.2   itojun 	void *newp;
    772       1.2   itojun 	size_t newlen;
    773       1.2   itojun {
    774       1.2   itojun 	/* All sysctl names at this level are terminal. */
    775       1.2   itojun 	if (namelen != 1)
    776       1.2   itojun 		return ENOTDIR;
    777       1.2   itojun 
    778       1.2   itojun 	switch (name[0]) {
    779       1.2   itojun 
    780      1.28   itojun 	case UDP6CTL_SENDSPACE:
    781       1.2   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    782       1.2   itojun 		    &udp6_sendspace);
    783       1.2   itojun 	case UDP6CTL_RECVSPACE:
    784      1.27   itojun 		return sysctl_int(oldp, oldlenp, newp, newlen,
    785       1.2   itojun 		    &udp6_recvspace);
    786       1.2   itojun 	default:
    787       1.2   itojun 		return ENOPROTOOPT;
    788       1.2   itojun 	}
    789       1.2   itojun 	/* NOTREACHED */
    790       1.2   itojun }
    791