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