Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.141.2.1
      1  1.141.2.1  christos /* $NetBSD: udp6_usrreq.c,v 1.141.2.1 2019/06/10 22:09:48 christos Exp $ */
      2      1.132      maxv /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $ */
      3      1.132      maxv /* $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 itojun Exp $ */
      4        1.3   thorpej 
      5        1.2    itojun /*
      6        1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7        1.2    itojun  * All rights reserved.
      8       1.27    itojun  *
      9        1.2    itojun  * Redistribution and use in source and binary forms, with or without
     10        1.2    itojun  * modification, are permitted provided that the following conditions
     11        1.2    itojun  * are met:
     12        1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     13        1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     14        1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     16        1.2    itojun  *    documentation and/or other materials provided with the distribution.
     17        1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     18        1.2    itojun  *    may be used to endorse or promote products derived from this software
     19        1.2    itojun  *    without specific prior written permission.
     20       1.27    itojun  *
     21        1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22        1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23        1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24        1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25        1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26        1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27        1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28        1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29        1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30        1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31        1.2    itojun  * SUCH DAMAGE.
     32        1.2    itojun  */
     33        1.2    itojun 
     34        1.2    itojun /*
     35        1.2    itojun  * Copyright (c) 1982, 1986, 1989, 1993
     36        1.2    itojun  *	The Regents of the University of California.  All rights reserved.
     37        1.2    itojun  *
     38        1.2    itojun  * Redistribution and use in source and binary forms, with or without
     39        1.2    itojun  * modification, are permitted provided that the following conditions
     40        1.2    itojun  * are met:
     41        1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     42        1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     43        1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     44        1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     45        1.2    itojun  *    documentation and/or other materials provided with the distribution.
     46       1.56       agc  * 3. Neither the name of the University nor the names of its contributors
     47        1.2    itojun  *    may be used to endorse or promote products derived from this software
     48        1.2    itojun  *    without specific prior written permission.
     49        1.2    itojun  *
     50        1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51        1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52        1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53        1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54        1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55        1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56        1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57        1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58        1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59        1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60        1.2    itojun  * SUCH DAMAGE.
     61        1.2    itojun  *
     62        1.2    itojun  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
     63        1.2    itojun  */
     64       1.51     lukem 
     65       1.51     lukem #include <sys/cdefs.h>
     66  1.141.2.1  christos __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.141.2.1 2019/06/10 22:09:48 christos Exp $");
     67       1.90  christos 
     68      1.121     pooka #ifdef _KERNEL_OPT
     69       1.90  christos #include "opt_inet.h"
     70       1.97     rmind #include "opt_inet_csum.h"
     71      1.116     ozaki #include "opt_ipsec.h"
     72      1.126  knakahar #include "opt_net_mpsafe.h"
     73      1.121     pooka #endif
     74        1.2    itojun 
     75        1.2    itojun #include <sys/param.h>
     76        1.2    itojun #include <sys/mbuf.h>
     77        1.2    itojun #include <sys/protosw.h>
     78        1.2    itojun #include <sys/socket.h>
     79        1.2    itojun #include <sys/socketvar.h>
     80        1.2    itojun #include <sys/systm.h>
     81        1.2    itojun #include <sys/proc.h>
     82        1.8    itojun #include <sys/syslog.h>
     83       1.90  christos #include <sys/domain.h>
     84       1.50    simonb #include <sys/sysctl.h>
     85        1.2    itojun 
     86        1.2    itojun #include <net/if.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.14    itojun #include <netinet/in_systm.h>
     92       1.97     rmind #include <netinet/in_offload.h>
     93       1.14    itojun #include <netinet/ip.h>
     94       1.14    itojun #include <netinet/ip_var.h>
     95       1.14    itojun #include <netinet/in_pcb.h>
     96       1.14    itojun #include <netinet/udp.h>
     97       1.14    itojun #include <netinet/udp_var.h>
     98       1.97     rmind #include <netinet/udp_private.h>
     99       1.97     rmind 
    100       1.23    itojun #include <netinet/ip6.h>
    101       1.97     rmind #include <netinet/icmp6.h>
    102       1.27    itojun #include <netinet6/ip6_var.h>
    103       1.97     rmind #include <netinet6/ip6_private.h>
    104        1.2    itojun #include <netinet6/in6_pcb.h>
    105        1.2    itojun #include <netinet6/udp6_var.h>
    106       1.82   thorpej #include <netinet6/udp6_private.h>
    107       1.14    itojun #include <netinet6/ip6protosw.h>
    108       1.97     rmind #include <netinet6/scope6_var.h>
    109        1.2    itojun 
    110      1.116     ozaki #ifdef IPSEC
    111      1.116     ozaki #include <netipsec/ipsec.h>
    112  1.141.2.1  christos #include <netipsec/esp.h>
    113      1.116     ozaki #ifdef INET6
    114      1.116     ozaki #include <netipsec/ipsec6.h>
    115      1.116     ozaki #endif
    116      1.131      maxv #endif
    117      1.116     ozaki 
    118        1.2    itojun #include "faith.h"
    119       1.41    itojun #if defined(NFAITH) && NFAITH > 0
    120       1.41    itojun #include <net/if_faith.h>
    121       1.41    itojun #endif
    122        1.2    itojun 
    123        1.2    itojun /*
    124       1.73    rpaulo  * UDP protocol implementation.
    125        1.2    itojun  * Per RFC 768, August, 1980.
    126        1.2    itojun  */
    127        1.2    itojun 
    128       1.58    itojun extern struct inpcbtable udbtable;
    129       1.82   thorpej 
    130       1.82   thorpej percpu_t *udp6stat_percpu;
    131        1.2    itojun 
    132       1.97     rmind /* UDP on IP6 parameters */
    133      1.131      maxv static int udp6_sendspace = 9216;	/* really max datagram size */
    134      1.131      maxv static int udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
    135       1.97     rmind 					/* 40 1K datagrams */
    136       1.97     rmind 
    137      1.131      maxv static void udp6_notify(struct in6pcb *, int);
    138      1.131      maxv static void sysctl_net_inet6_udp6_setup(struct sysctllog **);
    139  1.141.2.1  christos #ifdef IPSEC
    140  1.141.2.1  christos static int udp6_espinudp(struct mbuf **, int);
    141  1.141.2.1  christos #endif
    142        1.2    itojun 
    143       1.97     rmind #ifdef UDP_CSUM_COUNTERS
    144       1.97     rmind #include <sys/device.h>
    145       1.97     rmind struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    146       1.97     rmind     NULL, "udp6", "hwcsum bad");
    147       1.97     rmind struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    148       1.97     rmind     NULL, "udp6", "hwcsum ok");
    149       1.97     rmind struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    150       1.97     rmind     NULL, "udp6", "hwcsum data");
    151       1.97     rmind struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    152       1.97     rmind     NULL, "udp6", "swcsum");
    153       1.97     rmind 
    154       1.97     rmind EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
    155       1.97     rmind EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
    156       1.97     rmind EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
    157       1.97     rmind EVCNT_ATTACH_STATIC(udp6_swcsum);
    158       1.97     rmind 
    159       1.97     rmind #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    160       1.97     rmind #else
    161       1.97     rmind #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
    162       1.97     rmind #endif
    163       1.97     rmind 
    164        1.2    itojun void
    165       1.81      matt udp6_init(void)
    166        1.2    itojun {
    167       1.88     pooka 	sysctl_net_inet6_udp6_setup(NULL);
    168       1.92     pooka 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
    169       1.92     pooka 
    170       1.92     pooka 	udp_init_common();
    171        1.2    itojun }
    172        1.2    itojun 
    173        1.2    itojun /*
    174        1.2    itojun  * Notify a udp user of an asynchronous error;
    175       1.27    itojun  * just wake up so that he can collect error status.
    176        1.2    itojun  */
    177        1.2    itojun static	void
    178       1.76    dyoung udp6_notify(struct in6pcb *in6p, int errno)
    179        1.2    itojun {
    180        1.2    itojun 	in6p->in6p_socket->so_error = errno;
    181        1.2    itojun 	sorwakeup(in6p->in6p_socket);
    182        1.2    itojun 	sowwakeup(in6p->in6p_socket);
    183        1.2    itojun }
    184        1.2    itojun 
    185       1.84        ad void *
    186       1.76    dyoung udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    187        1.2    itojun {
    188        1.2    itojun 	struct udphdr uh;
    189       1.39    itojun 	struct ip6_hdr *ip6;
    190       1.76    dyoung 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
    191       1.14    itojun 	struct mbuf *m;
    192       1.14    itojun 	int off;
    193       1.40    itojun 	void *cmdarg;
    194       1.40    itojun 	struct ip6ctlparam *ip6cp = NULL;
    195       1.40    itojun 	const struct sockaddr_in6 *sa6_src = NULL;
    196       1.76    dyoung 	void (*notify)(struct in6pcb *, int) = udp6_notify;
    197       1.40    itojun 	struct udp_portonly {
    198       1.40    itojun 		u_int16_t uh_sport;
    199       1.40    itojun 		u_int16_t uh_dport;
    200       1.40    itojun 	} *uhp;
    201        1.2    itojun 
    202       1.10    itojun 	if (sa->sa_family != AF_INET6 ||
    203       1.10    itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    204       1.84        ad 		return NULL;
    205       1.14    itojun 
    206       1.25    itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    207       1.84        ad 		return NULL;
    208       1.25    itojun 	if (PRC_IS_REDIRECT(cmd))
    209       1.25    itojun 		notify = in6_rtchange, d = NULL;
    210       1.25    itojun 	else if (cmd == PRC_HOSTDEAD)
    211       1.25    itojun 		d = NULL;
    212       1.47    itojun 	else if (cmd == PRC_MSGSIZE) {
    213       1.47    itojun 		/* special code is present, see below */
    214       1.47    itojun 		notify = in6_rtchange;
    215       1.47    itojun 	}
    216       1.25    itojun 	else if (inet6ctlerrmap[cmd] == 0)
    217       1.84        ad 		return NULL;
    218        1.7    itojun 
    219       1.14    itojun 	/* if the parameter is from icmp6, decode it. */
    220       1.14    itojun 	if (d != NULL) {
    221       1.40    itojun 		ip6cp = (struct ip6ctlparam *)d;
    222       1.14    itojun 		m = ip6cp->ip6c_m;
    223       1.14    itojun 		ip6 = ip6cp->ip6c_ip6;
    224       1.14    itojun 		off = ip6cp->ip6c_off;
    225       1.40    itojun 		cmdarg = ip6cp->ip6c_cmdarg;
    226       1.40    itojun 		sa6_src = ip6cp->ip6c_src;
    227       1.14    itojun 	} else {
    228       1.14    itojun 		m = NULL;
    229       1.14    itojun 		ip6 = NULL;
    230       1.40    itojun 		cmdarg = NULL;
    231       1.40    itojun 		sa6_src = &sa6_any;
    232       1.60  christos 		off = 0;
    233       1.14    itojun 	}
    234       1.14    itojun 
    235        1.2    itojun 	if (ip6) {
    236       1.33    itojun 		/* check if we can safely examine src and dst ports */
    237       1.42    itojun 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    238       1.42    itojun 			if (cmd == PRC_MSGSIZE)
    239       1.42    itojun 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    240       1.84        ad 			return NULL;
    241       1.42    itojun 		}
    242        1.7    itojun 
    243       1.87    cegger 		memset(&uh, 0, sizeof(uh));
    244       1.77  christos 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
    245       1.34    itojun 
    246       1.34    itojun 		if (cmd == PRC_MSGSIZE) {
    247       1.36    itojun 			int valid = 0;
    248       1.40    itojun 
    249       1.34    itojun 			/*
    250       1.34    itojun 			 * Check to see if we have a valid UDP socket
    251       1.34    itojun 			 * corresponding to the address in the ICMPv6 message
    252       1.34    itojun 			 * payload.
    253       1.34    itojun 			 */
    254       1.58    itojun 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
    255       1.68  christos 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
    256      1.131      maxv 			    uh.uh_sport, 0, 0))
    257       1.36    itojun 				valid++;
    258       1.34    itojun #if 0
    259       1.34    itojun 			/*
    260       1.34    itojun 			 * As the use of sendto(2) is fairly popular,
    261       1.34    itojun 			 * we may want to allow non-connected pcb too.
    262       1.34    itojun 			 * But it could be too weak against attacks...
    263       1.34    itojun 			 * We should at least check if the local address (= s)
    264       1.34    itojun 			 * is really ours.
    265       1.34    itojun 			 */
    266       1.58    itojun 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
    267       1.58    itojun 			    uh.uh_dport, 0))
    268       1.36    itojun 				valid++;
    269       1.34    itojun #endif
    270       1.34    itojun 
    271       1.34    itojun 			/*
    272       1.40    itojun 			 * Depending on the value of "valid" and routing table
    273       1.40    itojun 			 * size (mtudisc_{hi,lo}wat), we will:
    274       1.46    itojun 			 * - recalculate the new MTU and create the
    275       1.40    itojun 			 *   corresponding routing entry, or
    276       1.40    itojun 			 * - ignore the MTU change notification.
    277       1.34    itojun 			 */
    278       1.36    itojun 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    279       1.34    itojun 
    280       1.40    itojun 			/*
    281       1.74    rpaulo 			 * regardless of if we called
    282       1.74    rpaulo 			 * icmp6_mtudisc_update(), we need to call
    283       1.74    rpaulo 			 * in6_pcbnotify(), to notify path MTU change
    284       1.74    rpaulo 			 * to the userland (RFC3542), because some
    285       1.74    rpaulo 			 * unconnected sockets may share the same
    286       1.40    itojun 			 * destination and want to know the path MTU.
    287       1.40    itojun 			 */
    288       1.34    itojun 		}
    289       1.34    itojun 
    290      1.131      maxv 		(void)in6_pcbnotify(&udbtable, sa, uh.uh_dport,
    291      1.124     ozaki 		    sin6tocsa(sa6_src), uh.uh_sport, cmd, cmdarg,
    292       1.40    itojun 		    notify);
    293        1.2    itojun 	} else {
    294      1.131      maxv 		(void)in6_pcbnotify(&udbtable, sa, 0,
    295      1.124     ozaki 		    sin6tocsa(sa6_src), 0, cmd, cmdarg, notify);
    296        1.2    itojun 	}
    297       1.84        ad 	return NULL;
    298        1.2    itojun }
    299        1.2    itojun 
    300       1.90  christos int
    301       1.90  christos udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    302       1.90  christos {
    303       1.90  christos 	int s;
    304       1.90  christos 	int error = 0;
    305  1.141.2.1  christos 	struct in6pcb *in6p;
    306       1.90  christos 	int family;
    307  1.141.2.1  christos 	int optval;
    308       1.90  christos 
    309       1.90  christos 	family = so->so_proto->pr_domain->dom_family;
    310       1.90  christos 
    311       1.90  christos 	s = splsoftnet();
    312       1.90  christos 	switch (family) {
    313       1.90  christos #ifdef INET
    314       1.90  christos 	case PF_INET:
    315       1.90  christos 		if (sopt->sopt_level != IPPROTO_UDP) {
    316       1.90  christos 			error = ip_ctloutput(op, so, sopt);
    317       1.90  christos 			goto end;
    318       1.90  christos 		}
    319       1.90  christos 		break;
    320       1.90  christos #endif
    321       1.90  christos #ifdef INET6
    322       1.90  christos 	case PF_INET6:
    323       1.90  christos 		if (sopt->sopt_level != IPPROTO_UDP) {
    324       1.90  christos 			error = ip6_ctloutput(op, so, sopt);
    325       1.90  christos 			goto end;
    326       1.90  christos 		}
    327       1.90  christos 		break;
    328       1.90  christos #endif
    329       1.90  christos 	default:
    330       1.90  christos 		error = EAFNOSUPPORT;
    331       1.90  christos 		goto end;
    332       1.90  christos 	}
    333  1.141.2.1  christos 
    334  1.141.2.1  christos 	switch (op) {
    335  1.141.2.1  christos 	case PRCO_SETOPT:
    336  1.141.2.1  christos 		in6p = sotoin6pcb(so);
    337  1.141.2.1  christos 
    338  1.141.2.1  christos 		switch (sopt->sopt_name) {
    339  1.141.2.1  christos 		case UDP_ENCAP:
    340  1.141.2.1  christos 			error = sockopt_getint(sopt, &optval);
    341  1.141.2.1  christos 			if (error)
    342  1.141.2.1  christos 				break;
    343  1.141.2.1  christos 
    344  1.141.2.1  christos 			switch(optval) {
    345  1.141.2.1  christos 			case 0:
    346  1.141.2.1  christos 				in6p->in6p_flags &= ~IN6P_ESPINUDP;
    347  1.141.2.1  christos 				break;
    348  1.141.2.1  christos 
    349  1.141.2.1  christos 			case UDP_ENCAP_ESPINUDP:
    350  1.141.2.1  christos 				in6p->in6p_flags |= IN6P_ESPINUDP;
    351  1.141.2.1  christos 				break;
    352  1.141.2.1  christos 
    353  1.141.2.1  christos 			default:
    354  1.141.2.1  christos 				error = EINVAL;
    355  1.141.2.1  christos 				break;
    356  1.141.2.1  christos 			}
    357  1.141.2.1  christos 			break;
    358  1.141.2.1  christos 
    359  1.141.2.1  christos 		default:
    360  1.141.2.1  christos 			error = ENOPROTOOPT;
    361  1.141.2.1  christos 			break;
    362  1.141.2.1  christos 		}
    363  1.141.2.1  christos 		break;
    364  1.141.2.1  christos 
    365  1.141.2.1  christos 	default:
    366  1.141.2.1  christos 		error = EINVAL;
    367  1.141.2.1  christos 		break;
    368  1.141.2.1  christos 	}
    369       1.90  christos 
    370       1.90  christos end:
    371       1.90  christos 	splx(s);
    372       1.90  christos 	return error;
    373       1.90  christos }
    374       1.90  christos 
    375       1.97     rmind static void
    376       1.97     rmind udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
    377      1.131      maxv     struct sockaddr *src, struct socket *so)
    378       1.97     rmind {
    379       1.97     rmind 	struct mbuf *opts = NULL;
    380       1.97     rmind 	struct mbuf *n;
    381      1.128     ozaki 	struct in6pcb *in6p;
    382       1.97     rmind 
    383      1.128     ozaki 	KASSERT(so != NULL);
    384      1.128     ozaki 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
    385       1.97     rmind 	in6p = sotoin6pcb(so);
    386      1.128     ozaki 	KASSERT(in6p != NULL);
    387       1.97     rmind 
    388       1.97     rmind #if defined(IPSEC)
    389      1.135      maxv 	if (ipsec_used && ipsec_in_reject(m, in6p)) {
    390       1.97     rmind 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
    391       1.97     rmind 			icmp6_error(n, ICMP6_DST_UNREACH,
    392       1.97     rmind 			    ICMP6_DST_UNREACH_ADMIN, 0);
    393       1.97     rmind 		return;
    394       1.97     rmind 	}
    395      1.131      maxv #endif
    396       1.97     rmind 
    397       1.97     rmind 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    398      1.131      maxv 		if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
    399      1.131      maxv 		    SOOPT_TIMESTAMP(in6p->in6p_socket->so_options)) {
    400       1.97     rmind 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
    401       1.97     rmind 			ip6_savecontrol(in6p, &opts, ip6, n);
    402       1.97     rmind 		}
    403       1.97     rmind 
    404       1.97     rmind 		m_adj(n, off);
    405       1.97     rmind 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    406       1.97     rmind 			m_freem(n);
    407       1.97     rmind 			if (opts)
    408       1.97     rmind 				m_freem(opts);
    409       1.97     rmind 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
    410      1.138       roy 			soroverflow(so);
    411       1.97     rmind 		} else
    412       1.97     rmind 			sorwakeup(so);
    413       1.97     rmind 	}
    414       1.97     rmind }
    415       1.97     rmind 
    416       1.97     rmind int
    417       1.97     rmind udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
    418  1.141.2.1  christos     struct mbuf **mp, int off)
    419       1.97     rmind {
    420       1.97     rmind 	u_int16_t sport, dport;
    421       1.97     rmind 	int rcvcnt;
    422       1.97     rmind 	struct in6_addr src6, *dst6;
    423       1.97     rmind 	const struct in_addr *dst4;
    424       1.97     rmind 	struct inpcb_hdr *inph;
    425       1.97     rmind 	struct in6pcb *in6p;
    426  1.141.2.1  christos 	struct mbuf *m = *mp;
    427       1.97     rmind 
    428       1.97     rmind 	rcvcnt = 0;
    429       1.97     rmind 	off += sizeof(struct udphdr);	/* now, offset of payload */
    430       1.97     rmind 
    431       1.97     rmind 	if (af != AF_INET && af != AF_INET6)
    432       1.97     rmind 		goto bad;
    433       1.97     rmind 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
    434       1.97     rmind 		goto bad;
    435       1.97     rmind 
    436       1.97     rmind 	src6 = src->sin6_addr;
    437       1.97     rmind 	if (sa6_recoverscope(src) != 0) {
    438       1.97     rmind 		/* XXX: should be impossible. */
    439       1.97     rmind 		goto bad;
    440       1.97     rmind 	}
    441       1.97     rmind 	sport = src->sin6_port;
    442       1.97     rmind 
    443       1.97     rmind 	dport = dst->sin6_port;
    444       1.97     rmind 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
    445       1.97     rmind 	dst6 = &dst->sin6_addr;
    446       1.97     rmind 
    447       1.97     rmind 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
    448       1.97     rmind 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
    449       1.97     rmind 		/*
    450       1.97     rmind 		 * Deliver a multicast or broadcast datagram to *all* sockets
    451       1.97     rmind 		 * for which the local and remote addresses and ports match
    452       1.97     rmind 		 * those of the incoming datagram.  This allows more than
    453       1.97     rmind 		 * one process to receive multi/broadcasts on the same port.
    454       1.97     rmind 		 * (This really ought to be done for unicast datagrams as
    455       1.97     rmind 		 * well, but that would cause problems with existing
    456       1.97     rmind 		 * applications that open both address-specific sockets and
    457       1.97     rmind 		 * a wildcard socket listening to the same port -- they would
    458       1.97     rmind 		 * end up receiving duplicates of every unicast datagram.
    459       1.97     rmind 		 * Those applications open the multiple sockets to overcome an
    460       1.97     rmind 		 * inadequacy of the UDP socket interface, but for backwards
    461       1.97     rmind 		 * compatibility we avoid the problem here rather than
    462       1.97     rmind 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    463       1.97     rmind 		 */
    464       1.97     rmind 
    465       1.97     rmind 		/*
    466       1.97     rmind 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    467       1.97     rmind 		 * we need udpiphdr for IPsec processing so we do that later.
    468       1.97     rmind 		 */
    469       1.97     rmind 		/*
    470       1.97     rmind 		 * Locate pcb(s) for datagram.
    471       1.97     rmind 		 */
    472       1.97     rmind 		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
    473       1.97     rmind 			in6p = (struct in6pcb *)inph;
    474       1.97     rmind 			if (in6p->in6p_af != AF_INET6)
    475       1.97     rmind 				continue;
    476       1.97     rmind 
    477       1.97     rmind 			if (in6p->in6p_lport != dport)
    478       1.97     rmind 				continue;
    479       1.97     rmind 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    480       1.97     rmind 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    481       1.97     rmind 				    dst6))
    482       1.97     rmind 					continue;
    483       1.97     rmind 			} else {
    484       1.97     rmind 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
    485       1.97     rmind 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    486       1.97     rmind 					continue;
    487       1.97     rmind 			}
    488       1.97     rmind 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    489       1.97     rmind 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    490       1.97     rmind 				    &src6) || in6p->in6p_fport != sport)
    491       1.97     rmind 					continue;
    492       1.97     rmind 			} else {
    493       1.97     rmind 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
    494       1.97     rmind 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    495       1.97     rmind 					continue;
    496       1.97     rmind 			}
    497       1.97     rmind 
    498      1.124     ozaki 			udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
    499       1.97     rmind 			rcvcnt++;
    500       1.97     rmind 
    501       1.97     rmind 			/*
    502       1.97     rmind 			 * Don't look for additional matches if this one does
    503       1.97     rmind 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    504       1.97     rmind 			 * socket options set.  This heuristic avoids searching
    505       1.97     rmind 			 * through all pcbs in the common case of a non-shared
    506       1.97     rmind 			 * port.  It assumes that an application will never
    507       1.97     rmind 			 * clear these options after setting them.
    508       1.97     rmind 			 */
    509       1.97     rmind 			if ((in6p->in6p_socket->so_options &
    510       1.97     rmind 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    511       1.97     rmind 				break;
    512       1.97     rmind 		}
    513       1.97     rmind 	} else {
    514       1.97     rmind 		/*
    515       1.97     rmind 		 * Locate pcb for datagram.
    516       1.97     rmind 		 */
    517       1.97     rmind 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
    518       1.97     rmind 					     dport, 0, 0);
    519       1.97     rmind 		if (in6p == 0) {
    520       1.97     rmind 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
    521       1.97     rmind 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
    522       1.97     rmind 			if (in6p == 0)
    523       1.97     rmind 				return rcvcnt;
    524       1.97     rmind 		}
    525       1.97     rmind 
    526  1.141.2.1  christos #ifdef IPSEC
    527  1.141.2.1  christos 		/* Handle ESP over UDP */
    528  1.141.2.1  christos 		if (in6p->in6p_flags & IN6P_ESPINUDP) {
    529  1.141.2.1  christos 			switch (udp6_espinudp(mp, off)) {
    530  1.141.2.1  christos 			case -1: /* Error, m was freed */
    531  1.141.2.1  christos 				rcvcnt = -1;
    532  1.141.2.1  christos 				goto bad;
    533  1.141.2.1  christos 
    534  1.141.2.1  christos 			case 1: /* ESP over UDP */
    535  1.141.2.1  christos 				rcvcnt++;
    536  1.141.2.1  christos 				goto bad;
    537  1.141.2.1  christos 
    538  1.141.2.1  christos 			case 0: /* plain UDP */
    539  1.141.2.1  christos 			default: /* Unexpected */
    540  1.141.2.1  christos 				/*
    541  1.141.2.1  christos 				 * Normal UDP processing will take place,
    542  1.141.2.1  christos 				 * m may have changed.
    543  1.141.2.1  christos 				 */
    544  1.141.2.1  christos 				m = *mp;
    545  1.141.2.1  christos 				break;
    546  1.141.2.1  christos 			}
    547  1.141.2.1  christos 		}
    548  1.141.2.1  christos #endif
    549  1.141.2.1  christos 
    550      1.124     ozaki 		udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
    551       1.97     rmind 		rcvcnt++;
    552       1.97     rmind 	}
    553       1.97     rmind 
    554       1.97     rmind bad:
    555       1.97     rmind 	return rcvcnt;
    556       1.97     rmind }
    557       1.97     rmind 
    558       1.97     rmind int
    559       1.97     rmind udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
    560       1.97     rmind {
    561       1.97     rmind 
    562       1.97     rmind 	/*
    563       1.97     rmind 	 * XXX it's better to record and check if this mbuf is
    564       1.97     rmind 	 * already checked.
    565       1.97     rmind 	 */
    566       1.97     rmind 
    567       1.97     rmind 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
    568       1.97     rmind 		goto good;
    569       1.97     rmind 	}
    570       1.97     rmind 	if (uh->uh_sum == 0) {
    571       1.97     rmind 		UDP6_STATINC(UDP6_STAT_NOSUM);
    572       1.97     rmind 		goto bad;
    573       1.97     rmind 	}
    574       1.97     rmind 
    575       1.97     rmind 	switch (m->m_pkthdr.csum_flags &
    576      1.123     ozaki 	    ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv6) |
    577       1.97     rmind 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    578       1.97     rmind 	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
    579       1.97     rmind 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
    580       1.97     rmind 		UDP6_STATINC(UDP6_STAT_BADSUM);
    581       1.97     rmind 		goto bad;
    582       1.97     rmind 
    583       1.97     rmind #if 0 /* notyet */
    584       1.97     rmind 	case M_CSUM_UDPv6|M_CSUM_DATA:
    585       1.97     rmind #endif
    586       1.97     rmind 
    587       1.97     rmind 	case M_CSUM_UDPv6:
    588       1.97     rmind 		/* Checksum was okay. */
    589       1.97     rmind 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
    590       1.97     rmind 		break;
    591       1.97     rmind 
    592       1.97     rmind 	default:
    593       1.97     rmind 		/*
    594       1.97     rmind 		 * Need to compute it ourselves.  Maybe skip checksum
    595       1.97     rmind 		 * on loopback interfaces.
    596       1.97     rmind 		 */
    597       1.97     rmind 		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
    598       1.97     rmind 		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
    599       1.97     rmind 			UDP6_STATINC(UDP6_STAT_BADSUM);
    600       1.97     rmind 			goto bad;
    601       1.97     rmind 		}
    602       1.97     rmind 	}
    603       1.97     rmind 
    604       1.97     rmind good:
    605       1.97     rmind 	return 0;
    606       1.97     rmind bad:
    607       1.97     rmind 	return -1;
    608       1.97     rmind }
    609       1.97     rmind 
    610       1.97     rmind int
    611       1.97     rmind udp6_input(struct mbuf **mp, int *offp, int proto)
    612       1.97     rmind {
    613       1.97     rmind 	struct mbuf *m = *mp;
    614       1.97     rmind 	int off = *offp;
    615       1.97     rmind 	struct sockaddr_in6 src, dst;
    616       1.97     rmind 	struct ip6_hdr *ip6;
    617       1.97     rmind 	struct udphdr *uh;
    618       1.97     rmind 	u_int32_t plen, ulen;
    619       1.97     rmind 
    620       1.97     rmind 	ip6 = mtod(m, struct ip6_hdr *);
    621       1.90  christos 
    622       1.97     rmind #if defined(NFAITH) && 0 < NFAITH
    623       1.97     rmind 	if (faithprefix(&ip6->ip6_dst)) {
    624       1.97     rmind 		/* send icmp6 host unreach? */
    625       1.97     rmind 		m_freem(m);
    626       1.97     rmind 		return IPPROTO_DONE;
    627       1.97     rmind 	}
    628       1.97     rmind #endif
    629       1.97     rmind 
    630       1.97     rmind 	UDP6_STATINC(UDP6_STAT_IPACKETS);
    631       1.97     rmind 
    632      1.131      maxv 	/* Check for jumbogram is done in ip6_input. We can trust pkthdr.len. */
    633       1.97     rmind 	plen = m->m_pkthdr.len - off;
    634       1.97     rmind 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    635       1.97     rmind 	if (uh == NULL) {
    636       1.97     rmind 		IP6_STATINC(IP6_STAT_TOOSHORT);
    637       1.97     rmind 		return IPPROTO_DONE;
    638       1.97     rmind 	}
    639      1.131      maxv 
    640      1.125   mlelstv 	/*
    641      1.125   mlelstv 	 * Enforce alignment requirements that are violated in
    642      1.125   mlelstv 	 * some cases, see kern/50766 for details.
    643      1.125   mlelstv 	 */
    644      1.131      maxv 	if (UDP_HDR_ALIGNED_P(uh) == 0) {
    645      1.131      maxv 		m = m_copyup(m, off + sizeof(struct udphdr), 0);
    646      1.131      maxv 		if (m == NULL) {
    647      1.131      maxv 			IP6_STATINC(IP6_STAT_TOOSHORT);
    648      1.131      maxv 			return IPPROTO_DONE;
    649      1.131      maxv 		}
    650      1.125   mlelstv 		ip6 = mtod(m, struct ip6_hdr *);
    651      1.131      maxv 		uh = (struct udphdr *)(mtod(m, char *) + off);
    652      1.131      maxv 	}
    653       1.97     rmind 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    654       1.97     rmind 	ulen = ntohs((u_short)uh->uh_ulen);
    655      1.131      maxv 
    656       1.97     rmind 	/*
    657       1.97     rmind 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    658       1.97     rmind 	 * iff payload length > 0xffff.
    659       1.97     rmind 	 */
    660       1.97     rmind 	if (ulen == 0 && plen > 0xffff)
    661       1.97     rmind 		ulen = plen;
    662       1.97     rmind 
    663       1.97     rmind 	if (plen != ulen) {
    664       1.97     rmind 		UDP6_STATINC(UDP6_STAT_BADLEN);
    665       1.97     rmind 		goto bad;
    666       1.97     rmind 	}
    667       1.97     rmind 
    668       1.97     rmind 	/* destination port of 0 is illegal, based on RFC768. */
    669       1.97     rmind 	if (uh->uh_dport == 0)
    670       1.97     rmind 		goto bad;
    671       1.97     rmind 
    672       1.97     rmind 	/*
    673       1.97     rmind 	 * Checksum extended UDP header and data.  Maybe skip checksum
    674       1.97     rmind 	 * on loopback interfaces.
    675       1.97     rmind 	 */
    676       1.97     rmind 	if (udp6_input_checksum(m, uh, off, ulen))
    677       1.97     rmind 		goto bad;
    678       1.97     rmind 
    679       1.97     rmind 	/*
    680       1.97     rmind 	 * Construct source and dst sockaddrs.
    681       1.97     rmind 	 */
    682       1.97     rmind 	memset(&src, 0, sizeof(src));
    683       1.97     rmind 	src.sin6_family = AF_INET6;
    684       1.97     rmind 	src.sin6_len = sizeof(struct sockaddr_in6);
    685       1.97     rmind 	src.sin6_addr = ip6->ip6_src;
    686       1.97     rmind 	src.sin6_port = uh->uh_sport;
    687       1.97     rmind 	memset(&dst, 0, sizeof(dst));
    688       1.97     rmind 	dst.sin6_family = AF_INET6;
    689       1.97     rmind 	dst.sin6_len = sizeof(struct sockaddr_in6);
    690       1.97     rmind 	dst.sin6_addr = ip6->ip6_dst;
    691       1.97     rmind 	dst.sin6_port = uh->uh_dport;
    692       1.97     rmind 
    693  1.141.2.1  christos 	if (udp6_realinput(AF_INET6, &src, &dst, &m, off) == 0) {
    694       1.97     rmind 		if (m->m_flags & M_MCAST) {
    695       1.97     rmind 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
    696       1.97     rmind 			goto bad;
    697       1.97     rmind 		}
    698       1.97     rmind 		UDP6_STATINC(UDP6_STAT_NOPORT);
    699       1.97     rmind 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    700       1.97     rmind 		m = NULL;
    701       1.97     rmind 	}
    702       1.97     rmind 
    703       1.97     rmind bad:
    704       1.97     rmind 	if (m)
    705       1.97     rmind 		m_freem(m);
    706       1.97     rmind 	return IPPROTO_DONE;
    707       1.97     rmind }
    708        1.2    itojun 
    709      1.132      maxv int
    710      1.132      maxv udp6_output(struct in6pcb * const in6p, struct mbuf *m,
    711      1.132      maxv     struct sockaddr_in6 * const addr6, struct mbuf * const control,
    712      1.132      maxv     struct lwp * const l)
    713      1.132      maxv {
    714      1.132      maxv 	u_int32_t ulen = m->m_pkthdr.len;
    715      1.132      maxv 	u_int32_t plen = sizeof(struct udphdr) + ulen;
    716      1.132      maxv 	struct ip6_hdr *ip6;
    717      1.132      maxv 	struct udphdr *udp6;
    718      1.132      maxv 	struct in6_addr _laddr, *laddr, *faddr;
    719      1.132      maxv 	struct in6_addr laddr_mapped; /* XXX ugly */
    720      1.132      maxv 	struct sockaddr_in6 *sin6 = NULL;
    721      1.132      maxv 	struct ifnet *oifp = NULL;
    722      1.132      maxv 	int scope_ambiguous = 0;
    723      1.132      maxv 	u_int16_t fport;
    724      1.132      maxv 	int error = 0;
    725      1.132      maxv 	struct ip6_pktopts *optp = NULL;
    726      1.132      maxv 	struct ip6_pktopts opt;
    727      1.132      maxv 	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
    728      1.132      maxv #ifdef INET
    729      1.132      maxv 	struct ip *ip;
    730      1.132      maxv 	struct udpiphdr *ui;
    731      1.132      maxv 	int flags = 0;
    732      1.132      maxv #endif
    733      1.132      maxv 	struct sockaddr_in6 tmp;
    734      1.132      maxv 
    735      1.132      maxv 	if (addr6) {
    736      1.132      maxv 		sin6 = addr6;
    737  1.141.2.1  christos 		if (sin6->sin6_len != sizeof(*sin6)) {
    738  1.141.2.1  christos 			error = EINVAL;
    739  1.141.2.1  christos 			goto release;
    740  1.141.2.1  christos 		}
    741      1.132      maxv 		if (sin6->sin6_family != AF_INET6) {
    742      1.132      maxv 			error = EAFNOSUPPORT;
    743      1.132      maxv 			goto release;
    744      1.132      maxv 		}
    745      1.132      maxv 
    746      1.132      maxv 		/* protect *sin6 from overwrites */
    747      1.132      maxv 		tmp = *sin6;
    748      1.132      maxv 		sin6 = &tmp;
    749      1.132      maxv 
    750      1.132      maxv 		/*
    751      1.132      maxv 		 * Application should provide a proper zone ID or the use of
    752      1.132      maxv 		 * default zone IDs should be enabled.  Unfortunately, some
    753      1.132      maxv 		 * applications do not behave as it should, so we need a
    754      1.132      maxv 		 * workaround.  Even if an appropriate ID is not determined,
    755      1.132      maxv 		 * we'll see if we can determine the outgoing interface.  If we
    756      1.132      maxv 		 * can, determine the zone ID based on the interface below.
    757      1.132      maxv 		 */
    758      1.132      maxv 		if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
    759      1.132      maxv 			scope_ambiguous = 1;
    760      1.132      maxv 		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
    761      1.132      maxv 			goto release;
    762      1.132      maxv 	}
    763      1.132      maxv 
    764      1.132      maxv 	if (control) {
    765      1.133      maxv 		if (__predict_false(l == NULL)) {
    766      1.133      maxv 			panic("%s: control but no lwp", __func__);
    767      1.133      maxv 		}
    768      1.132      maxv 		if ((error = ip6_setpktopts(control, &opt,
    769      1.132      maxv 		    in6p->in6p_outputopts, l->l_cred, IPPROTO_UDP)) != 0)
    770      1.132      maxv 			goto release;
    771      1.132      maxv 		optp = &opt;
    772      1.132      maxv 	} else
    773      1.132      maxv 		optp = in6p->in6p_outputopts;
    774      1.132      maxv 
    775      1.132      maxv 
    776      1.132      maxv 	if (sin6) {
    777      1.132      maxv 		/*
    778      1.132      maxv 		 * Slightly different than v4 version in that we call
    779      1.132      maxv 		 * in6_selectsrc and in6_pcbsetport to fill in the local
    780      1.132      maxv 		 * address and port rather than in_pcbconnect. in_pcbconnect
    781      1.132      maxv 		 * sets in6p_faddr which causes EISCONN below to be hit on
    782      1.132      maxv 		 * subsequent sendto.
    783      1.132      maxv 		 */
    784      1.132      maxv 		if (sin6->sin6_port == 0) {
    785      1.132      maxv 			error = EADDRNOTAVAIL;
    786      1.132      maxv 			goto release;
    787      1.132      maxv 		}
    788      1.132      maxv 
    789      1.132      maxv 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    790      1.132      maxv 			/* how about ::ffff:0.0.0.0 case? */
    791      1.132      maxv 			error = EISCONN;
    792      1.132      maxv 			goto release;
    793      1.132      maxv 		}
    794      1.132      maxv 
    795      1.132      maxv 		faddr = &sin6->sin6_addr;
    796      1.132      maxv 		fport = sin6->sin6_port; /* allow 0 port */
    797      1.132      maxv 
    798      1.132      maxv 		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
    799      1.132      maxv 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
    800      1.132      maxv 				/*
    801      1.132      maxv 				 * I believe we should explicitly discard the
    802      1.132      maxv 				 * packet when mapped addresses are disabled,
    803      1.132      maxv 				 * rather than send the packet as an IPv6 one.
    804      1.132      maxv 				 * If we chose the latter approach, the packet
    805      1.132      maxv 				 * might be sent out on the wire based on the
    806      1.132      maxv 				 * default route, the situation which we'd
    807      1.132      maxv 				 * probably want to avoid.
    808      1.132      maxv 				 * (20010421 jinmei (at) kame.net)
    809      1.132      maxv 				 */
    810      1.132      maxv 				error = EINVAL;
    811      1.132      maxv 				goto release;
    812      1.132      maxv 			}
    813      1.132      maxv 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    814      1.132      maxv 			    !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
    815      1.132      maxv 				/*
    816      1.132      maxv 				 * when remote addr is an IPv4-mapped address,
    817      1.132      maxv 				 * local addr should not be an IPv6 address,
    818      1.132      maxv 				 * since you cannot determine how to map IPv6
    819      1.132      maxv 				 * source address to IPv4.
    820      1.132      maxv 				 */
    821      1.132      maxv 				error = EINVAL;
    822      1.132      maxv 				goto release;
    823      1.132      maxv 			}
    824      1.132      maxv 
    825      1.132      maxv 			af = AF_INET;
    826      1.132      maxv 		}
    827      1.132      maxv 
    828      1.132      maxv 		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
    829      1.132      maxv 			struct psref psref;
    830      1.132      maxv 			int bound = curlwp_bind();
    831      1.132      maxv 
    832      1.132      maxv 			error = in6_selectsrc(sin6, optp,
    833      1.132      maxv 			    in6p->in6p_moptions,
    834      1.132      maxv 			    &in6p->in6p_route,
    835      1.132      maxv 			    &in6p->in6p_laddr, &oifp, &psref, &_laddr);
    836  1.141.2.1  christos 			if (error)
    837  1.141.2.1  christos 				laddr = NULL;
    838  1.141.2.1  christos 			else
    839  1.141.2.1  christos 				laddr = &_laddr;
    840      1.132      maxv 			if (oifp && scope_ambiguous &&
    841      1.132      maxv 			    (error = in6_setscope(&sin6->sin6_addr,
    842      1.132      maxv 			    oifp, NULL))) {
    843      1.132      maxv 				if_put(oifp, &psref);
    844      1.132      maxv 				curlwp_bindx(bound);
    845      1.132      maxv 				goto release;
    846      1.132      maxv 			}
    847      1.132      maxv 			if_put(oifp, &psref);
    848      1.132      maxv 			curlwp_bindx(bound);
    849      1.132      maxv 		} else {
    850      1.132      maxv 			/*
    851      1.132      maxv 			 * XXX: freebsd[34] does not have in_selectsrc, but
    852      1.132      maxv 			 * we can omit the whole part because freebsd4 calls
    853      1.132      maxv 			 * udp_output() directly in this case, and thus we'll
    854      1.132      maxv 			 * never see this path.
    855      1.132      maxv 			 */
    856      1.132      maxv 			if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    857      1.132      maxv 				struct sockaddr_in sin_dst;
    858      1.132      maxv 				struct in_addr ina;
    859      1.132      maxv 				struct in_ifaddr *ia4;
    860      1.132      maxv 				struct psref _psref;
    861      1.132      maxv 				int bound;
    862      1.132      maxv 
    863      1.132      maxv 				memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
    864      1.132      maxv 				sockaddr_in_init(&sin_dst, &ina, 0);
    865      1.132      maxv 				bound = curlwp_bind();
    866      1.132      maxv 				ia4 = in_selectsrc(&sin_dst, &in6p->in6p_route,
    867      1.132      maxv 				    in6p->in6p_socket->so_options, NULL,
    868      1.132      maxv 				    &error, &_psref);
    869      1.132      maxv 				if (ia4 == NULL) {
    870      1.132      maxv 					curlwp_bindx(bound);
    871      1.132      maxv 					if (error == 0)
    872      1.132      maxv 						error = EADDRNOTAVAIL;
    873      1.132      maxv 					goto release;
    874      1.132      maxv 				}
    875      1.132      maxv 				memset(&laddr_mapped, 0, sizeof(laddr_mapped));
    876      1.132      maxv 				laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
    877      1.132      maxv 				memcpy(&laddr_mapped.s6_addr[12],
    878      1.132      maxv 				      &IA_SIN(ia4)->sin_addr,
    879      1.132      maxv 				      sizeof(IA_SIN(ia4)->sin_addr));
    880      1.132      maxv 				ia4_release(ia4, &_psref);
    881      1.132      maxv 				curlwp_bindx(bound);
    882      1.132      maxv 				laddr = &laddr_mapped;
    883      1.132      maxv 			} else
    884      1.132      maxv 			{
    885      1.132      maxv 				laddr = &in6p->in6p_laddr;	/* XXX */
    886      1.132      maxv 			}
    887      1.132      maxv 		}
    888      1.132      maxv 		if (laddr == NULL) {
    889      1.132      maxv 			if (error == 0)
    890      1.132      maxv 				error = EADDRNOTAVAIL;
    891      1.132      maxv 			goto release;
    892      1.132      maxv 		}
    893      1.132      maxv 		if (in6p->in6p_lport == 0) {
    894      1.132      maxv 			/*
    895      1.132      maxv 			 * Craft a sockaddr_in6 for the local endpoint. Use the
    896      1.132      maxv 			 * "any" as a base, set the address, and recover the
    897      1.132      maxv 			 * scope.
    898      1.132      maxv 			 */
    899      1.132      maxv 			struct sockaddr_in6 lsin6 =
    900      1.132      maxv 			    *((const struct sockaddr_in6 *)in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
    901      1.132      maxv 			lsin6.sin6_addr = *laddr;
    902      1.132      maxv 			error = sa6_recoverscope(&lsin6);
    903      1.132      maxv 			if (error)
    904      1.132      maxv 				goto release;
    905      1.132      maxv 
    906      1.132      maxv 			error = in6_pcbsetport(&lsin6, in6p, l);
    907      1.132      maxv 
    908      1.132      maxv 			if (error) {
    909      1.132      maxv 				in6p->in6p_laddr = in6addr_any;
    910      1.132      maxv 				goto release;
    911      1.132      maxv 			}
    912      1.132      maxv 		}
    913      1.132      maxv 	} else {
    914      1.132      maxv 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    915      1.132      maxv 			error = ENOTCONN;
    916      1.132      maxv 			goto release;
    917      1.132      maxv 		}
    918      1.132      maxv 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
    919      1.132      maxv 			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    920      1.132      maxv 			{
    921      1.132      maxv 				/*
    922      1.132      maxv 				 * XXX: this case would happen when the
    923      1.132      maxv 				 * application sets the V6ONLY flag after
    924      1.132      maxv 				 * connecting the foreign address.
    925      1.132      maxv 				 * Such applications should be fixed,
    926      1.132      maxv 				 * so we bark here.
    927      1.132      maxv 				 */
    928      1.132      maxv 				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
    929      1.132      maxv 				    "option was set for a connected socket\n");
    930      1.132      maxv 				error = EINVAL;
    931      1.132      maxv 				goto release;
    932      1.132      maxv 			} else
    933      1.132      maxv 				af = AF_INET;
    934      1.132      maxv 		}
    935      1.132      maxv 		laddr = &in6p->in6p_laddr;
    936      1.132      maxv 		faddr = &in6p->in6p_faddr;
    937      1.132      maxv 		fport = in6p->in6p_fport;
    938      1.132      maxv 	}
    939      1.132      maxv 
    940      1.132      maxv 	if (af == AF_INET)
    941      1.132      maxv 		hlen = sizeof(struct ip);
    942      1.132      maxv 
    943      1.132      maxv 	/*
    944      1.132      maxv 	 * Calculate data length and get a mbuf
    945      1.132      maxv 	 * for UDP and IP6 headers.
    946      1.132      maxv 	 */
    947      1.132      maxv 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
    948      1.132      maxv 	if (m == NULL) {
    949      1.132      maxv 		error = ENOBUFS;
    950      1.132      maxv 		goto release;
    951      1.132      maxv 	}
    952      1.132      maxv 
    953      1.132      maxv 	/*
    954      1.132      maxv 	 * Stuff checksum and output datagram.
    955      1.132      maxv 	 */
    956      1.132      maxv 	udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
    957      1.132      maxv 	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
    958      1.132      maxv 	udp6->uh_dport = fport;
    959      1.132      maxv 	if (plen <= 0xffff)
    960      1.132      maxv 		udp6->uh_ulen = htons((u_int16_t)plen);
    961      1.132      maxv 	else
    962      1.132      maxv 		udp6->uh_ulen = 0;
    963      1.132      maxv 	udp6->uh_sum = 0;
    964      1.132      maxv 
    965      1.132      maxv 	switch (af) {
    966      1.132      maxv 	case AF_INET6:
    967      1.132      maxv 		ip6 = mtod(m, struct ip6_hdr *);
    968      1.132      maxv 		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    969      1.132      maxv 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
    970      1.132      maxv 		ip6->ip6_vfc 	|= IPV6_VERSION;
    971      1.132      maxv #if 0		/* ip6_plen will be filled in ip6_output. */
    972      1.132      maxv 		ip6->ip6_plen	= htons((u_int16_t)plen);
    973      1.132      maxv #endif
    974      1.132      maxv 		ip6->ip6_nxt	= IPPROTO_UDP;
    975      1.132      maxv 		ip6->ip6_hlim	= in6_selecthlim_rt(in6p);
    976      1.132      maxv 		ip6->ip6_src	= *laddr;
    977      1.132      maxv 		ip6->ip6_dst	= *faddr;
    978      1.132      maxv 
    979      1.132      maxv 		udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
    980      1.132      maxv 		    htonl(plen), htonl(IPPROTO_UDP));
    981      1.132      maxv 		m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
    982      1.132      maxv 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
    983      1.132      maxv 
    984      1.132      maxv 		UDP6_STATINC(UDP6_STAT_OPACKETS);
    985      1.132      maxv 		error = ip6_output(m, optp, &in6p->in6p_route, 0,
    986      1.132      maxv 		    in6p->in6p_moptions, in6p, NULL);
    987      1.132      maxv 		break;
    988      1.132      maxv 	case AF_INET:
    989      1.132      maxv #ifdef INET
    990      1.132      maxv 		/* can't transmit jumbogram over IPv4 */
    991      1.132      maxv 		if (plen > 0xffff) {
    992      1.132      maxv 			error = EMSGSIZE;
    993      1.132      maxv 			goto release;
    994      1.132      maxv 		}
    995      1.132      maxv 
    996      1.132      maxv 		ip = mtod(m, struct ip *);
    997      1.132      maxv 		ui = (struct udpiphdr *)ip;
    998      1.132      maxv 		memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
    999      1.132      maxv 		ui->ui_pr = IPPROTO_UDP;
   1000      1.132      maxv 		ui->ui_len = htons(plen);
   1001      1.132      maxv 		memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
   1002      1.132      maxv 		ui->ui_ulen = ui->ui_len;
   1003      1.132      maxv 
   1004      1.132      maxv 		flags = (in6p->in6p_socket->so_options &
   1005      1.132      maxv 			 (SO_DONTROUTE | SO_BROADCAST));
   1006      1.132      maxv 		memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
   1007      1.132      maxv 
   1008      1.132      maxv 		udp6->uh_sum = in_cksum(m, hlen + plen);
   1009      1.132      maxv 		if (udp6->uh_sum == 0)
   1010      1.132      maxv 			udp6->uh_sum = 0xffff;
   1011      1.132      maxv 
   1012      1.132      maxv 		ip->ip_len = htons(hlen + plen);
   1013      1.132      maxv 		ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
   1014      1.132      maxv 		ip->ip_tos = 0;	/* XXX */
   1015      1.132      maxv 
   1016      1.132      maxv 		UDP_STATINC(UDP_STAT_OPACKETS);
   1017      1.132      maxv 		error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
   1018      1.132      maxv 		    in6p->in6p_v4moptions, NULL);
   1019      1.132      maxv 		break;
   1020      1.132      maxv #else
   1021      1.132      maxv 		error = EAFNOSUPPORT;
   1022      1.132      maxv 		goto release;
   1023      1.132      maxv #endif
   1024      1.132      maxv 	}
   1025      1.132      maxv 	goto releaseopt;
   1026      1.132      maxv 
   1027      1.132      maxv release:
   1028      1.132      maxv 	m_freem(m);
   1029      1.132      maxv 
   1030      1.132      maxv releaseopt:
   1031      1.132      maxv 	if (control) {
   1032      1.132      maxv 		if (optp == &opt)
   1033      1.132      maxv 			ip6_clearpktopts(&opt, -1);
   1034      1.132      maxv 		m_freem(control);
   1035      1.132      maxv 	}
   1036      1.132      maxv 	return (error);
   1037      1.132      maxv }
   1038      1.132      maxv 
   1039       1.95     rmind static int
   1040       1.95     rmind udp6_attach(struct socket *so, int proto)
   1041       1.95     rmind {
   1042       1.95     rmind 	struct in6pcb *in6p;
   1043       1.95     rmind 	int s, error;
   1044       1.95     rmind 
   1045       1.95     rmind 	KASSERT(sotoin6pcb(so) == NULL);
   1046       1.95     rmind 	sosetlock(so);
   1047       1.95     rmind 
   1048  1.141.2.1  christos 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
   1049  1.141.2.1  christos 	if (error) {
   1050  1.141.2.1  christos 		return error;
   1051  1.141.2.1  christos 	}
   1052  1.141.2.1  christos 
   1053       1.95     rmind 	/*
   1054       1.95     rmind 	 * MAPPED_ADDR implementation spec:
   1055       1.95     rmind 	 *  Always attach for IPv6, and only when necessary for IPv4.
   1056       1.95     rmind 	 */
   1057       1.95     rmind 	s = splsoftnet();
   1058       1.95     rmind 	error = in6_pcballoc(so, &udbtable);
   1059       1.95     rmind 	splx(s);
   1060       1.95     rmind 	if (error) {
   1061       1.95     rmind 		return error;
   1062       1.95     rmind 	}
   1063  1.141.2.1  christos 
   1064       1.95     rmind 	in6p = sotoin6pcb(so);
   1065       1.95     rmind 	in6p->in6p_cksum = -1;	/* just to be sure */
   1066       1.95     rmind 
   1067       1.95     rmind 	KASSERT(solocked(so));
   1068       1.95     rmind 	return 0;
   1069       1.95     rmind }
   1070       1.95     rmind 
   1071       1.95     rmind static void
   1072       1.95     rmind udp6_detach(struct socket *so)
   1073       1.95     rmind {
   1074       1.95     rmind 	struct in6pcb *in6p = sotoin6pcb(so);
   1075       1.95     rmind 	int s;
   1076       1.95     rmind 
   1077       1.95     rmind 	KASSERT(solocked(so));
   1078       1.95     rmind 	KASSERT(in6p != NULL);
   1079       1.95     rmind 
   1080       1.95     rmind 	s = splsoftnet();
   1081       1.95     rmind 	in6_pcbdetach(in6p);
   1082       1.95     rmind 	splx(s);
   1083       1.95     rmind }
   1084       1.95     rmind 
   1085       1.99       rtr static int
   1086      1.118       rtr udp6_accept(struct socket *so, struct sockaddr *nam)
   1087      1.107       rtr {
   1088      1.107       rtr 	KASSERT(solocked(so));
   1089      1.107       rtr 
   1090      1.107       rtr 	return EOPNOTSUPP;
   1091      1.107       rtr }
   1092      1.107       rtr 
   1093      1.107       rtr static int
   1094      1.117       rtr udp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
   1095      1.109       rtr {
   1096      1.109       rtr 	struct in6pcb *in6p = sotoin6pcb(so);
   1097      1.117       rtr 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1098      1.109       rtr 	int error = 0;
   1099      1.109       rtr 	int s;
   1100      1.109       rtr 
   1101      1.109       rtr 	KASSERT(solocked(so));
   1102      1.109       rtr 	KASSERT(in6p != NULL);
   1103      1.109       rtr 
   1104      1.109       rtr 	s = splsoftnet();
   1105      1.117       rtr 	error = in6_pcbbind(in6p, sin6, l);
   1106      1.109       rtr 	splx(s);
   1107      1.109       rtr 	return error;
   1108      1.109       rtr }
   1109      1.109       rtr 
   1110      1.109       rtr static int
   1111      1.112       rtr udp6_listen(struct socket *so, struct lwp *l)
   1112      1.109       rtr {
   1113      1.109       rtr 	KASSERT(solocked(so));
   1114      1.109       rtr 
   1115      1.109       rtr 	return EOPNOTSUPP;
   1116      1.109       rtr }
   1117      1.109       rtr 
   1118      1.109       rtr static int
   1119      1.120       rtr udp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
   1120      1.110       rtr {
   1121      1.110       rtr 	struct in6pcb *in6p = sotoin6pcb(so);
   1122      1.110       rtr 	int error = 0;
   1123      1.110       rtr 	int s;
   1124      1.110       rtr 
   1125      1.110       rtr 	KASSERT(solocked(so));
   1126      1.110       rtr 	KASSERT(in6p != NULL);
   1127      1.110       rtr 
   1128      1.110       rtr 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1129      1.110       rtr 		return EISCONN;
   1130      1.110       rtr 	s = splsoftnet();
   1131      1.120       rtr 	error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
   1132      1.110       rtr 	splx(s);
   1133      1.110       rtr 	if (error == 0)
   1134      1.110       rtr 		soisconnected(so);
   1135      1.110       rtr 
   1136      1.110       rtr 	return error;
   1137      1.110       rtr }
   1138      1.110       rtr 
   1139      1.110       rtr static int
   1140      1.115       rtr udp6_connect2(struct socket *so, struct socket *so2)
   1141      1.115       rtr {
   1142      1.115       rtr 	KASSERT(solocked(so));
   1143      1.115       rtr 
   1144      1.115       rtr 	return EOPNOTSUPP;
   1145      1.115       rtr }
   1146      1.115       rtr 
   1147      1.115       rtr static int
   1148      1.111       rtr udp6_disconnect(struct socket *so)
   1149      1.111       rtr {
   1150      1.111       rtr 	struct in6pcb *in6p = sotoin6pcb(so);
   1151      1.111       rtr 	int s;
   1152      1.111       rtr 
   1153      1.111       rtr 	KASSERT(solocked(so));
   1154      1.111       rtr 	KASSERT(in6p != NULL);
   1155      1.111       rtr 
   1156      1.111       rtr 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1157      1.111       rtr 		return ENOTCONN;
   1158      1.111       rtr 
   1159      1.111       rtr 	s = splsoftnet();
   1160      1.111       rtr 	in6_pcbdisconnect(in6p);
   1161      1.111       rtr 	memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
   1162      1.111       rtr 	splx(s);
   1163      1.111       rtr 
   1164      1.111       rtr 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
   1165      1.111       rtr 	in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
   1166      1.111       rtr 	return 0;
   1167      1.111       rtr }
   1168      1.111       rtr 
   1169      1.111       rtr static int
   1170      1.111       rtr udp6_shutdown(struct socket *so)
   1171      1.111       rtr {
   1172      1.111       rtr 	int s;
   1173      1.111       rtr 
   1174      1.111       rtr 	s = splsoftnet();
   1175      1.111       rtr 	socantsendmore(so);
   1176      1.111       rtr 	splx(s);
   1177      1.111       rtr 
   1178      1.111       rtr 	return 0;
   1179      1.111       rtr }
   1180      1.111       rtr 
   1181      1.111       rtr static int
   1182      1.111       rtr udp6_abort(struct socket *so)
   1183      1.111       rtr {
   1184      1.111       rtr 	int s;
   1185      1.111       rtr 
   1186      1.111       rtr 	KASSERT(solocked(so));
   1187      1.111       rtr 	KASSERT(sotoin6pcb(so) != NULL);
   1188      1.111       rtr 
   1189      1.111       rtr 	s = splsoftnet();
   1190      1.111       rtr 	soisdisconnected(so);
   1191      1.111       rtr 	in6_pcbdetach(sotoin6pcb(so));
   1192      1.111       rtr 	splx(s);
   1193      1.111       rtr 
   1194      1.111       rtr 	return 0;
   1195      1.111       rtr }
   1196      1.111       rtr 
   1197      1.111       rtr static int
   1198      1.101       rtr udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
   1199        1.2    itojun {
   1200       1.27    itojun 	/*
   1201        1.2    itojun 	 * MAPPED_ADDR implementation info:
   1202        1.2    itojun 	 *  Mapped addr support for PRU_CONTROL is not necessary.
   1203        1.2    itojun 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
   1204        1.2    itojun 	 *  and they don't associate any addr to their socket.  Then
   1205        1.2    itojun 	 *  socket family is only hint about the PRU_CONTROL'ed address
   1206        1.2    itojun 	 *  family, especially when getting addrs from kernel.
   1207        1.2    itojun 	 *  So AF_INET socket need to be used to control AF_INET addrs,
   1208        1.2    itojun 	 *  and AF_INET6 socket for AF_INET6 addrs.
   1209        1.2    itojun 	 */
   1210      1.101       rtr 	return in6_control(so, cmd, addr6, ifp);
   1211       1.99       rtr }
   1212       1.99       rtr 
   1213      1.102       rtr static int
   1214      1.102       rtr udp6_stat(struct socket *so, struct stat *ub)
   1215      1.102       rtr {
   1216      1.105       rtr 	KASSERT(solocked(so));
   1217      1.105       rtr 
   1218      1.104       rtr 	/* stat: don't bother with a blocksize */
   1219      1.104       rtr 	return 0;
   1220      1.102       rtr }
   1221      1.102       rtr 
   1222      1.106       rtr static int
   1223      1.118       rtr udp6_peeraddr(struct socket *so, struct sockaddr *nam)
   1224      1.106       rtr {
   1225      1.106       rtr 	KASSERT(solocked(so));
   1226      1.106       rtr 	KASSERT(sotoin6pcb(so) != NULL);
   1227      1.106       rtr 	KASSERT(nam != NULL);
   1228      1.106       rtr 
   1229      1.118       rtr 	in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
   1230      1.106       rtr 	return 0;
   1231      1.106       rtr }
   1232      1.106       rtr 
   1233      1.106       rtr static int
   1234      1.118       rtr udp6_sockaddr(struct socket *so, struct sockaddr *nam)
   1235      1.106       rtr {
   1236      1.106       rtr 	KASSERT(solocked(so));
   1237      1.106       rtr 	KASSERT(sotoin6pcb(so) != NULL);
   1238      1.106       rtr 	KASSERT(nam != NULL);
   1239      1.106       rtr 
   1240      1.118       rtr 	in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
   1241      1.106       rtr 	return 0;
   1242      1.106       rtr }
   1243      1.106       rtr 
   1244      1.108       rtr static int
   1245      1.114       rtr udp6_rcvd(struct socket *so, int flags, struct lwp *l)
   1246      1.114       rtr {
   1247      1.114       rtr 	KASSERT(solocked(so));
   1248      1.114       rtr 
   1249      1.114       rtr 	return EOPNOTSUPP;
   1250      1.114       rtr }
   1251      1.114       rtr 
   1252      1.114       rtr static int
   1253      1.108       rtr udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
   1254      1.108       rtr {
   1255      1.108       rtr 	KASSERT(solocked(so));
   1256      1.108       rtr 
   1257      1.108       rtr 	return EOPNOTSUPP;
   1258      1.108       rtr }
   1259      1.108       rtr 
   1260      1.108       rtr static int
   1261      1.120       rtr udp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
   1262      1.113       rtr     struct mbuf *control, struct lwp *l)
   1263      1.113       rtr {
   1264      1.113       rtr 	struct in6pcb *in6p = sotoin6pcb(so);
   1265      1.113       rtr 	int error = 0;
   1266      1.113       rtr 	int s;
   1267      1.113       rtr 
   1268      1.113       rtr 	KASSERT(solocked(so));
   1269      1.113       rtr 	KASSERT(in6p != NULL);
   1270      1.113       rtr 	KASSERT(m != NULL);
   1271      1.113       rtr 
   1272      1.113       rtr 	s = splsoftnet();
   1273      1.120       rtr 	error = udp6_output(in6p, m, (struct sockaddr_in6 *)nam, control, l);
   1274      1.113       rtr 	splx(s);
   1275      1.113       rtr 
   1276      1.113       rtr 	return error;
   1277      1.113       rtr }
   1278      1.113       rtr 
   1279      1.113       rtr static int
   1280      1.108       rtr udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
   1281      1.108       rtr {
   1282      1.108       rtr 	KASSERT(solocked(so));
   1283      1.108       rtr 
   1284  1.141.2.1  christos 	m_freem(m);
   1285  1.141.2.1  christos 	m_freem(control);
   1286      1.108       rtr 
   1287      1.108       rtr 	return EOPNOTSUPP;
   1288      1.108       rtr }
   1289      1.108       rtr 
   1290      1.115       rtr static int
   1291      1.115       rtr udp6_purgeif(struct socket *so, struct ifnet *ifp)
   1292      1.115       rtr {
   1293      1.115       rtr 
   1294      1.115       rtr 	mutex_enter(softnet_lock);
   1295      1.127     ozaki 	in6_pcbpurgeif0(&udbtable, ifp);
   1296      1.127     ozaki #ifdef NET_MPSAFE
   1297      1.127     ozaki 	mutex_exit(softnet_lock);
   1298      1.126  knakahar #endif
   1299      1.115       rtr 	in6_purgeif(ifp);
   1300      1.127     ozaki #ifdef NET_MPSAFE
   1301      1.127     ozaki 	mutex_enter(softnet_lock);
   1302      1.127     ozaki #endif
   1303      1.115       rtr 	in6_pcbpurgeif(&udbtable, ifp);
   1304      1.115       rtr 	mutex_exit(softnet_lock);
   1305      1.115       rtr 
   1306      1.115       rtr 	return 0;
   1307      1.115       rtr }
   1308      1.115       rtr 
   1309       1.82   thorpej static int
   1310       1.82   thorpej sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
   1311       1.82   thorpej {
   1312       1.82   thorpej 
   1313       1.86   thorpej 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
   1314       1.82   thorpej }
   1315       1.82   thorpej 
   1316       1.88     pooka static void
   1317       1.88     pooka sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
   1318        1.2    itojun {
   1319       1.93     pooka 
   1320       1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1321       1.62    atatat 		       CTLFLAG_PERMANENT,
   1322       1.61    atatat 		       CTLTYPE_NODE, "inet6", NULL,
   1323       1.61    atatat 		       NULL, 0, NULL, 0,
   1324       1.61    atatat 		       CTL_NET, PF_INET6, CTL_EOL);
   1325       1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1326       1.62    atatat 		       CTLFLAG_PERMANENT,
   1327       1.63    atatat 		       CTLTYPE_NODE, "udp6",
   1328       1.63    atatat 		       SYSCTL_DESCR("UDPv6 related settings"),
   1329       1.61    atatat 		       NULL, 0, NULL, 0,
   1330       1.61    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
   1331       1.61    atatat 
   1332       1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1333       1.62    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1334       1.63    atatat 		       CTLTYPE_INT, "sendspace",
   1335       1.63    atatat 		       SYSCTL_DESCR("Default UDP send buffer size"),
   1336       1.61    atatat 		       NULL, 0, &udp6_sendspace, 0,
   1337       1.61    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
   1338       1.61    atatat 		       CTL_EOL);
   1339       1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1340       1.62    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1341       1.63    atatat 		       CTLTYPE_INT, "recvspace",
   1342       1.63    atatat 		       SYSCTL_DESCR("Default UDP receive buffer size"),
   1343       1.61    atatat 		       NULL, 0, &udp6_recvspace, 0,
   1344       1.61    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
   1345       1.61    atatat 		       CTL_EOL);
   1346       1.64   thorpej 	sysctl_createv(clog, 0, NULL, NULL,
   1347       1.64   thorpej 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1348       1.64   thorpej 		       CTLTYPE_INT, "do_loopback_cksum",
   1349       1.64   thorpej 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
   1350       1.64   thorpej 		       NULL, 0, &udp_do_loopback_cksum, 0,
   1351       1.64   thorpej 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
   1352       1.64   thorpej 		       CTL_EOL);
   1353       1.65    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1354       1.65    atatat 		       CTLFLAG_PERMANENT,
   1355       1.67    atatat 		       CTLTYPE_STRUCT, "pcblist",
   1356       1.65    atatat 		       SYSCTL_DESCR("UDP protocol control block list"),
   1357       1.65    atatat 		       sysctl_inpcblist, 0, &udbtable, 0,
   1358       1.65    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
   1359       1.65    atatat 		       CTL_EOL);
   1360       1.70    rpaulo 	sysctl_createv(clog, 0, NULL, NULL,
   1361       1.70    rpaulo 		       CTLFLAG_PERMANENT,
   1362       1.70    rpaulo 		       CTLTYPE_STRUCT, "stats",
   1363       1.70    rpaulo 		       SYSCTL_DESCR("UDPv6 statistics"),
   1364       1.82   thorpej 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
   1365       1.70    rpaulo 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
   1366       1.70    rpaulo 		       CTL_EOL);
   1367        1.2    itojun }
   1368       1.82   thorpej 
   1369       1.82   thorpej void
   1370       1.82   thorpej udp6_statinc(u_int stat)
   1371       1.82   thorpej {
   1372       1.82   thorpej 
   1373       1.82   thorpej 	KASSERT(stat < UDP6_NSTATS);
   1374       1.82   thorpej 	UDP6_STATINC(stat);
   1375       1.82   thorpej }
   1376       1.94     rmind 
   1377  1.141.2.1  christos #ifdef IPSEC
   1378  1.141.2.1  christos /*
   1379  1.141.2.1  christos  * Returns:
   1380  1.141.2.1  christos  *     1 if the packet was processed
   1381  1.141.2.1  christos  *     0 if normal UDP processing should take place
   1382  1.141.2.1  christos  *    -1 if an error occurred and m was freed
   1383  1.141.2.1  christos  */
   1384  1.141.2.1  christos static int
   1385  1.141.2.1  christos udp6_espinudp(struct mbuf **mp, int off)
   1386  1.141.2.1  christos {
   1387  1.141.2.1  christos 	const size_t skip = sizeof(struct udphdr);
   1388  1.141.2.1  christos 	size_t len;
   1389  1.141.2.1  christos 	void *data;
   1390  1.141.2.1  christos 	size_t minlen;
   1391  1.141.2.1  christos 	int ip6hdrlen;
   1392  1.141.2.1  christos 	struct ip6_hdr *ip6;
   1393  1.141.2.1  christos 	struct m_tag *tag;
   1394  1.141.2.1  christos 	struct udphdr *udphdr;
   1395  1.141.2.1  christos 	u_int16_t sport, dport;
   1396  1.141.2.1  christos 	struct mbuf *m = *mp;
   1397  1.141.2.1  christos 	uint32_t *marker;
   1398  1.141.2.1  christos 
   1399  1.141.2.1  christos 	/*
   1400  1.141.2.1  christos 	 * Collapse the mbuf chain if the first mbuf is too short
   1401  1.141.2.1  christos 	 * The longest case is: UDP + non ESP marker + ESP
   1402  1.141.2.1  christos 	 */
   1403  1.141.2.1  christos 	minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
   1404  1.141.2.1  christos 	if (minlen > m->m_pkthdr.len)
   1405  1.141.2.1  christos 		minlen = m->m_pkthdr.len;
   1406  1.141.2.1  christos 
   1407  1.141.2.1  christos 	if (m->m_len < minlen) {
   1408  1.141.2.1  christos 		if ((*mp = m_pullup(m, minlen)) == NULL) {
   1409  1.141.2.1  christos 			return -1;
   1410  1.141.2.1  christos 		}
   1411  1.141.2.1  christos 		m = *mp;
   1412  1.141.2.1  christos 	}
   1413  1.141.2.1  christos 
   1414  1.141.2.1  christos 	len = m->m_len - off;
   1415  1.141.2.1  christos 	data = mtod(m, char *) + off;
   1416  1.141.2.1  christos 
   1417  1.141.2.1  christos 	/* Ignore keepalive packets */
   1418  1.141.2.1  christos 	if ((len == 1) && (*(unsigned char *)data == 0xff)) {
   1419  1.141.2.1  christos 		m_freem(m);
   1420  1.141.2.1  christos 		*mp = NULL; /* avoid any further processing by caller ... */
   1421  1.141.2.1  christos 		return 1;
   1422  1.141.2.1  christos 	}
   1423  1.141.2.1  christos 
   1424  1.141.2.1  christos 	/* Handle Non-ESP marker (32bit). If zero, then IKE. */
   1425  1.141.2.1  christos 	marker = (uint32_t *)data;
   1426  1.141.2.1  christos 	if (len <= sizeof(uint32_t))
   1427  1.141.2.1  christos 		return 0;
   1428  1.141.2.1  christos 	if (marker[0] == 0)
   1429  1.141.2.1  christos 		return 0;
   1430  1.141.2.1  christos 
   1431  1.141.2.1  christos 	/*
   1432  1.141.2.1  christos 	 * Get the UDP ports. They are handled in network
   1433  1.141.2.1  christos 	 * order everywhere in IPSEC_NAT_T code.
   1434  1.141.2.1  christos 	 */
   1435  1.141.2.1  christos 	udphdr = (struct udphdr *)((char *)data - skip);
   1436  1.141.2.1  christos 	sport = udphdr->uh_sport;
   1437  1.141.2.1  christos 	dport = udphdr->uh_dport;
   1438  1.141.2.1  christos 
   1439  1.141.2.1  christos 	/*
   1440  1.141.2.1  christos 	 * Remove the UDP header (and possibly the non ESP marker)
   1441  1.141.2.1  christos 	 * IPv6 header length is ip6hdrlen
   1442  1.141.2.1  christos 	 * Before:
   1443  1.141.2.1  christos 	 *   <---- off --->
   1444  1.141.2.1  christos 	 *   +-----+------+-----+
   1445  1.141.2.1  christos 	 *   | IP6 |  UDP | ESP |
   1446  1.141.2.1  christos 	 *   +-----+------+-----+
   1447  1.141.2.1  christos 	 *         <-skip->
   1448  1.141.2.1  christos 	 * After:
   1449  1.141.2.1  christos 	 *          +-----+-----+
   1450  1.141.2.1  christos 	 *          | IP6 | ESP |
   1451  1.141.2.1  christos 	 *          +-----+-----+
   1452  1.141.2.1  christos 	 *   <-skip->
   1453  1.141.2.1  christos 	 */
   1454  1.141.2.1  christos 	ip6hdrlen = off - sizeof(struct udphdr);
   1455  1.141.2.1  christos 	memmove(mtod(m, char *) + skip, mtod(m, void *), ip6hdrlen);
   1456  1.141.2.1  christos 	m_adj(m, skip);
   1457  1.141.2.1  christos 
   1458  1.141.2.1  christos 	ip6 = mtod(m, struct ip6_hdr *);
   1459  1.141.2.1  christos 	ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - skip);
   1460  1.141.2.1  christos 	ip6->ip6_nxt = IPPROTO_ESP;
   1461  1.141.2.1  christos 
   1462  1.141.2.1  christos 	/*
   1463  1.141.2.1  christos 	 * We have modified the packet - it is now ESP, so we should not
   1464  1.141.2.1  christos 	 * return to UDP processing ...
   1465  1.141.2.1  christos 	 *
   1466  1.141.2.1  christos 	 * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
   1467  1.141.2.1  christos 	 * the source UDP port. This is required if we want
   1468  1.141.2.1  christos 	 * to select the right SPD for multiple hosts behind
   1469  1.141.2.1  christos 	 * same NAT
   1470  1.141.2.1  christos 	 */
   1471  1.141.2.1  christos 	if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
   1472  1.141.2.1  christos 	    sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
   1473  1.141.2.1  christos 		m_freem(m);
   1474  1.141.2.1  christos 		return -1;
   1475  1.141.2.1  christos 	}
   1476  1.141.2.1  christos 	((u_int16_t *)(tag + 1))[0] = sport;
   1477  1.141.2.1  christos 	((u_int16_t *)(tag + 1))[1] = dport;
   1478  1.141.2.1  christos 	m_tag_prepend(m, tag);
   1479  1.141.2.1  christos 
   1480  1.141.2.1  christos 	if (ipsec_used)
   1481  1.141.2.1  christos 		ipsec6_common_input(&m, &ip6hdrlen, IPPROTO_ESP);
   1482  1.141.2.1  christos 	else
   1483  1.141.2.1  christos 		m_freem(m);
   1484  1.141.2.1  christos 
   1485  1.141.2.1  christos 	/* We handled it, it shouldn't be handled by UDP */
   1486  1.141.2.1  christos 	*mp = NULL; /* avoid free by caller ... */
   1487  1.141.2.1  christos 	return 1;
   1488  1.141.2.1  christos }
   1489  1.141.2.1  christos #endif /* IPSEC */
   1490  1.141.2.1  christos 
   1491       1.96     rmind PR_WRAP_USRREQS(udp6)
   1492       1.96     rmind #define	udp6_attach	udp6_attach_wrapper
   1493       1.96     rmind #define	udp6_detach	udp6_detach_wrapper
   1494      1.107       rtr #define	udp6_accept	udp6_accept_wrapper
   1495      1.109       rtr #define	udp6_bind	udp6_bind_wrapper
   1496      1.109       rtr #define	udp6_listen	udp6_listen_wrapper
   1497      1.110       rtr #define	udp6_connect	udp6_connect_wrapper
   1498      1.115       rtr #define	udp6_connect2	udp6_connect2_wrapper
   1499      1.111       rtr #define	udp6_disconnect	udp6_disconnect_wrapper
   1500      1.111       rtr #define	udp6_shutdown	udp6_shutdown_wrapper
   1501      1.111       rtr #define	udp6_abort	udp6_abort_wrapper
   1502       1.99       rtr #define	udp6_ioctl	udp6_ioctl_wrapper
   1503      1.102       rtr #define	udp6_stat	udp6_stat_wrapper
   1504      1.106       rtr #define	udp6_peeraddr	udp6_peeraddr_wrapper
   1505      1.106       rtr #define	udp6_sockaddr	udp6_sockaddr_wrapper
   1506      1.114       rtr #define	udp6_rcvd	udp6_rcvd_wrapper
   1507      1.108       rtr #define	udp6_recvoob	udp6_recvoob_wrapper
   1508      1.113       rtr #define	udp6_send	udp6_send_wrapper
   1509      1.108       rtr #define	udp6_sendoob	udp6_sendoob_wrapper
   1510      1.115       rtr #define	udp6_purgeif	udp6_purgeif_wrapper
   1511       1.94     rmind 
   1512       1.94     rmind const struct pr_usrreqs udp6_usrreqs = {
   1513       1.95     rmind 	.pr_attach	= udp6_attach,
   1514       1.95     rmind 	.pr_detach	= udp6_detach,
   1515      1.107       rtr 	.pr_accept	= udp6_accept,
   1516      1.109       rtr 	.pr_bind	= udp6_bind,
   1517      1.109       rtr 	.pr_listen	= udp6_listen,
   1518      1.110       rtr 	.pr_connect	= udp6_connect,
   1519      1.115       rtr 	.pr_connect2	= udp6_connect2,
   1520      1.111       rtr 	.pr_disconnect	= udp6_disconnect,
   1521      1.111       rtr 	.pr_shutdown	= udp6_shutdown,
   1522      1.111       rtr 	.pr_abort	= udp6_abort,
   1523       1.99       rtr 	.pr_ioctl	= udp6_ioctl,
   1524      1.102       rtr 	.pr_stat	= udp6_stat,
   1525      1.106       rtr 	.pr_peeraddr	= udp6_peeraddr,
   1526      1.106       rtr 	.pr_sockaddr	= udp6_sockaddr,
   1527      1.114       rtr 	.pr_rcvd	= udp6_rcvd,
   1528      1.108       rtr 	.pr_recvoob	= udp6_recvoob,
   1529      1.113       rtr 	.pr_send	= udp6_send,
   1530      1.108       rtr 	.pr_sendoob	= udp6_sendoob,
   1531      1.115       rtr 	.pr_purgeif	= udp6_purgeif,
   1532       1.94     rmind };
   1533