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