Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.93.2.1
      1  1.93.2.1       tls /*	$NetBSD: udp6_usrreq.c,v 1.93.2.1 2014/08/10 06:56:30 tls Exp $	*/
      2      1.43    itojun /*	$KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $	*/
      3       1.3   thorpej 
      4       1.2    itojun /*
      5       1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6       1.2    itojun  * All rights reserved.
      7      1.27    itojun  *
      8       1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9       1.2    itojun  * modification, are permitted provided that the following conditions
     10       1.2    itojun  * are met:
     11       1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12       1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13       1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15       1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16       1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17       1.2    itojun  *    may be used to endorse or promote products derived from this software
     18       1.2    itojun  *    without specific prior written permission.
     19      1.27    itojun  *
     20       1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21       1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22       1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23       1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24       1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25       1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26       1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27       1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28       1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29       1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30       1.2    itojun  * SUCH DAMAGE.
     31       1.2    itojun  */
     32       1.2    itojun 
     33       1.2    itojun /*
     34       1.2    itojun  * Copyright (c) 1982, 1986, 1989, 1993
     35       1.2    itojun  *	The Regents of the University of California.  All rights reserved.
     36       1.2    itojun  *
     37       1.2    itojun  * Redistribution and use in source and binary forms, with or without
     38       1.2    itojun  * modification, are permitted provided that the following conditions
     39       1.2    itojun  * are met:
     40       1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     41       1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     42       1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     43       1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     44       1.2    itojun  *    documentation and/or other materials provided with the distribution.
     45      1.56       agc  * 3. Neither the name of the University nor the names of its contributors
     46       1.2    itojun  *    may be used to endorse or promote products derived from this software
     47       1.2    itojun  *    without specific prior written permission.
     48       1.2    itojun  *
     49       1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50       1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51       1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52       1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53       1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54       1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55       1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56       1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57       1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58       1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59       1.2    itojun  * SUCH DAMAGE.
     60       1.2    itojun  *
     61       1.2    itojun  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
     62       1.2    itojun  */
     63      1.51     lukem 
     64      1.51     lukem #include <sys/cdefs.h>
     65  1.93.2.1       tls __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.93.2.1 2014/08/10 06:56:30 tls Exp $");
     66      1.90  christos 
     67      1.90  christos #include "opt_inet.h"
     68  1.93.2.1       tls #include "opt_inet_csum.h"
     69       1.2    itojun 
     70       1.2    itojun #include <sys/param.h>
     71       1.2    itojun #include <sys/mbuf.h>
     72       1.2    itojun #include <sys/protosw.h>
     73       1.2    itojun #include <sys/socket.h>
     74       1.2    itojun #include <sys/socketvar.h>
     75       1.2    itojun #include <sys/systm.h>
     76       1.2    itojun #include <sys/proc.h>
     77       1.8    itojun #include <sys/syslog.h>
     78      1.90  christos #include <sys/domain.h>
     79      1.50    simonb #include <sys/sysctl.h>
     80       1.2    itojun 
     81       1.2    itojun #include <net/if.h>
     82       1.2    itojun #include <net/route.h>
     83       1.2    itojun #include <net/if_types.h>
     84       1.2    itojun 
     85       1.2    itojun #include <netinet/in.h>
     86       1.2    itojun #include <netinet/in_var.h>
     87      1.14    itojun #include <netinet/in_systm.h>
     88  1.93.2.1       tls #include <netinet/in_offload.h>
     89      1.14    itojun #include <netinet/ip.h>
     90      1.14    itojun #include <netinet/ip_var.h>
     91      1.14    itojun #include <netinet/in_pcb.h>
     92      1.14    itojun #include <netinet/udp.h>
     93      1.14    itojun #include <netinet/udp_var.h>
     94  1.93.2.1       tls #include <netinet/udp_private.h>
     95  1.93.2.1       tls 
     96      1.23    itojun #include <netinet/ip6.h>
     97  1.93.2.1       tls #include <netinet/icmp6.h>
     98      1.27    itojun #include <netinet6/ip6_var.h>
     99  1.93.2.1       tls #include <netinet6/ip6_private.h>
    100       1.2    itojun #include <netinet6/in6_pcb.h>
    101       1.2    itojun #include <netinet6/udp6_var.h>
    102      1.82   thorpej #include <netinet6/udp6_private.h>
    103      1.14    itojun #include <netinet6/ip6protosw.h>
    104  1.93.2.1       tls #include <netinet6/scope6_var.h>
    105       1.2    itojun 
    106       1.2    itojun #include "faith.h"
    107      1.41    itojun #if defined(NFAITH) && NFAITH > 0
    108      1.41    itojun #include <net/if_faith.h>
    109      1.41    itojun #endif
    110       1.2    itojun 
    111       1.2    itojun /*
    112      1.73    rpaulo  * UDP protocol implementation.
    113       1.2    itojun  * Per RFC 768, August, 1980.
    114       1.2    itojun  */
    115       1.2    itojun 
    116      1.58    itojun extern struct inpcbtable udbtable;
    117      1.82   thorpej 
    118      1.82   thorpej percpu_t *udp6stat_percpu;
    119       1.2    itojun 
    120  1.93.2.1       tls /* UDP on IP6 parameters */
    121  1.93.2.1       tls static int	udp6_sendspace = 9216;	/* really max datagram size */
    122  1.93.2.1       tls static int	udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
    123  1.93.2.1       tls 					/* 40 1K datagrams */
    124  1.93.2.1       tls 
    125      1.78    dyoung static	void udp6_notify(struct in6pcb *, int);
    126      1.88     pooka static	void sysctl_net_inet6_udp6_setup(struct sysctllog **);
    127       1.2    itojun 
    128  1.93.2.1       tls #ifdef UDP_CSUM_COUNTERS
    129  1.93.2.1       tls #include <sys/device.h>
    130  1.93.2.1       tls struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    131  1.93.2.1       tls     NULL, "udp6", "hwcsum bad");
    132  1.93.2.1       tls struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    133  1.93.2.1       tls     NULL, "udp6", "hwcsum ok");
    134  1.93.2.1       tls struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    135  1.93.2.1       tls     NULL, "udp6", "hwcsum data");
    136  1.93.2.1       tls struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    137  1.93.2.1       tls     NULL, "udp6", "swcsum");
    138  1.93.2.1       tls 
    139  1.93.2.1       tls EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
    140  1.93.2.1       tls EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
    141  1.93.2.1       tls EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
    142  1.93.2.1       tls EVCNT_ATTACH_STATIC(udp6_swcsum);
    143  1.93.2.1       tls 
    144  1.93.2.1       tls #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    145  1.93.2.1       tls #else
    146  1.93.2.1       tls #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
    147  1.93.2.1       tls #endif
    148  1.93.2.1       tls 
    149       1.2    itojun void
    150      1.81      matt udp6_init(void)
    151       1.2    itojun {
    152      1.88     pooka 	sysctl_net_inet6_udp6_setup(NULL);
    153      1.92     pooka 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
    154      1.92     pooka 
    155      1.92     pooka 	udp_init_common();
    156       1.2    itojun }
    157       1.2    itojun 
    158       1.2    itojun /*
    159       1.2    itojun  * Notify a udp user of an asynchronous error;
    160      1.27    itojun  * just wake up so that he can collect error status.
    161       1.2    itojun  */
    162       1.2    itojun static	void
    163      1.76    dyoung udp6_notify(struct in6pcb *in6p, int errno)
    164       1.2    itojun {
    165       1.2    itojun 	in6p->in6p_socket->so_error = errno;
    166       1.2    itojun 	sorwakeup(in6p->in6p_socket);
    167       1.2    itojun 	sowwakeup(in6p->in6p_socket);
    168       1.2    itojun }
    169       1.2    itojun 
    170      1.84        ad void *
    171      1.76    dyoung udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    172       1.2    itojun {
    173       1.2    itojun 	struct udphdr uh;
    174      1.39    itojun 	struct ip6_hdr *ip6;
    175      1.76    dyoung 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
    176      1.14    itojun 	struct mbuf *m;
    177      1.14    itojun 	int off;
    178      1.40    itojun 	void *cmdarg;
    179      1.40    itojun 	struct ip6ctlparam *ip6cp = NULL;
    180      1.40    itojun 	const struct sockaddr_in6 *sa6_src = NULL;
    181      1.76    dyoung 	void (*notify)(struct in6pcb *, int) = udp6_notify;
    182      1.40    itojun 	struct udp_portonly {
    183      1.40    itojun 		u_int16_t uh_sport;
    184      1.40    itojun 		u_int16_t uh_dport;
    185      1.40    itojun 	} *uhp;
    186       1.2    itojun 
    187      1.10    itojun 	if (sa->sa_family != AF_INET6 ||
    188      1.10    itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    189      1.84        ad 		return NULL;
    190      1.14    itojun 
    191      1.25    itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    192      1.84        ad 		return NULL;
    193      1.25    itojun 	if (PRC_IS_REDIRECT(cmd))
    194      1.25    itojun 		notify = in6_rtchange, d = NULL;
    195      1.25    itojun 	else if (cmd == PRC_HOSTDEAD)
    196      1.25    itojun 		d = NULL;
    197      1.47    itojun 	else if (cmd == PRC_MSGSIZE) {
    198      1.47    itojun 		/* special code is present, see below */
    199      1.47    itojun 		notify = in6_rtchange;
    200      1.47    itojun 	}
    201      1.25    itojun 	else if (inet6ctlerrmap[cmd] == 0)
    202      1.84        ad 		return NULL;
    203       1.7    itojun 
    204      1.14    itojun 	/* if the parameter is from icmp6, decode it. */
    205      1.14    itojun 	if (d != NULL) {
    206      1.40    itojun 		ip6cp = (struct ip6ctlparam *)d;
    207      1.14    itojun 		m = ip6cp->ip6c_m;
    208      1.14    itojun 		ip6 = ip6cp->ip6c_ip6;
    209      1.14    itojun 		off = ip6cp->ip6c_off;
    210      1.40    itojun 		cmdarg = ip6cp->ip6c_cmdarg;
    211      1.40    itojun 		sa6_src = ip6cp->ip6c_src;
    212      1.14    itojun 	} else {
    213      1.14    itojun 		m = NULL;
    214      1.14    itojun 		ip6 = NULL;
    215      1.40    itojun 		cmdarg = NULL;
    216      1.40    itojun 		sa6_src = &sa6_any;
    217      1.60  christos 		off = 0;
    218      1.14    itojun 	}
    219      1.14    itojun 
    220       1.2    itojun 	if (ip6) {
    221       1.2    itojun 		/*
    222       1.2    itojun 		 * XXX: We assume that when IPV6 is non NULL,
    223       1.2    itojun 		 * M and OFF are valid.
    224       1.2    itojun 		 */
    225      1.33    itojun 
    226      1.33    itojun 		/* check if we can safely examine src and dst ports */
    227      1.42    itojun 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    228      1.42    itojun 			if (cmd == PRC_MSGSIZE)
    229      1.42    itojun 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    230      1.84        ad 			return NULL;
    231      1.42    itojun 		}
    232       1.7    itojun 
    233      1.87    cegger 		memset(&uh, 0, sizeof(uh));
    234      1.77  christos 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
    235      1.34    itojun 
    236      1.34    itojun 		if (cmd == PRC_MSGSIZE) {
    237      1.36    itojun 			int valid = 0;
    238      1.40    itojun 
    239      1.34    itojun 			/*
    240      1.34    itojun 			 * Check to see if we have a valid UDP socket
    241      1.34    itojun 			 * corresponding to the address in the ICMPv6 message
    242      1.34    itojun 			 * payload.
    243      1.34    itojun 			 */
    244      1.58    itojun 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
    245      1.68  christos 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
    246      1.89    dyoung 						  uh.uh_sport, 0, 0))
    247      1.36    itojun 				valid++;
    248      1.34    itojun #if 0
    249      1.34    itojun 			/*
    250      1.34    itojun 			 * As the use of sendto(2) is fairly popular,
    251      1.34    itojun 			 * we may want to allow non-connected pcb too.
    252      1.34    itojun 			 * But it could be too weak against attacks...
    253      1.34    itojun 			 * We should at least check if the local address (= s)
    254      1.34    itojun 			 * is really ours.
    255      1.34    itojun 			 */
    256      1.58    itojun 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
    257      1.58    itojun 			    uh.uh_dport, 0))
    258      1.36    itojun 				valid++;
    259      1.34    itojun #endif
    260      1.34    itojun 
    261      1.34    itojun 			/*
    262      1.40    itojun 			 * Depending on the value of "valid" and routing table
    263      1.40    itojun 			 * size (mtudisc_{hi,lo}wat), we will:
    264      1.46    itojun 			 * - recalculate the new MTU and create the
    265      1.40    itojun 			 *   corresponding routing entry, or
    266      1.40    itojun 			 * - ignore the MTU change notification.
    267      1.34    itojun 			 */
    268      1.36    itojun 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    269      1.34    itojun 
    270      1.40    itojun 			/*
    271      1.74    rpaulo 			 * regardless of if we called
    272      1.74    rpaulo 			 * icmp6_mtudisc_update(), we need to call
    273      1.74    rpaulo 			 * in6_pcbnotify(), to notify path MTU change
    274      1.74    rpaulo 			 * to the userland (RFC3542), because some
    275      1.74    rpaulo 			 * unconnected sockets may share the same
    276      1.40    itojun 			 * destination and want to know the path MTU.
    277      1.40    itojun 			 */
    278      1.34    itojun 		}
    279      1.34    itojun 
    280      1.58    itojun 		(void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
    281      1.68  christos 		    (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
    282      1.40    itojun 		    notify);
    283       1.2    itojun 	} else {
    284      1.58    itojun 		(void) in6_pcbnotify(&udbtable, sa, 0,
    285      1.68  christos 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    286       1.2    itojun 	}
    287      1.84        ad 	return NULL;
    288       1.2    itojun }
    289       1.2    itojun 
    290      1.90  christos int
    291      1.90  christos udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    292      1.90  christos {
    293      1.90  christos 	int s;
    294      1.90  christos 	int error = 0;
    295      1.90  christos 	int family;
    296      1.90  christos 
    297      1.90  christos 	family = so->so_proto->pr_domain->dom_family;
    298      1.90  christos 
    299      1.90  christos 	s = splsoftnet();
    300      1.90  christos 	switch (family) {
    301      1.90  christos #ifdef INET
    302      1.90  christos 	case PF_INET:
    303      1.90  christos 		if (sopt->sopt_level != IPPROTO_UDP) {
    304      1.90  christos 			error = ip_ctloutput(op, so, sopt);
    305      1.90  christos 			goto end;
    306      1.90  christos 		}
    307      1.90  christos 		break;
    308      1.90  christos #endif
    309      1.90  christos #ifdef INET6
    310      1.90  christos 	case PF_INET6:
    311      1.90  christos 		if (sopt->sopt_level != IPPROTO_UDP) {
    312      1.90  christos 			error = ip6_ctloutput(op, so, sopt);
    313      1.90  christos 			goto end;
    314      1.90  christos 		}
    315      1.90  christos 		break;
    316      1.90  christos #endif
    317      1.90  christos 	default:
    318      1.90  christos 		error = EAFNOSUPPORT;
    319      1.90  christos 		goto end;
    320      1.90  christos 	}
    321      1.91  christos 	error = EINVAL;
    322      1.90  christos 
    323      1.90  christos end:
    324      1.90  christos 	splx(s);
    325      1.90  christos 	return error;
    326      1.90  christos }
    327      1.90  christos 
    328  1.93.2.1       tls static void
    329  1.93.2.1       tls udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
    330  1.93.2.1       tls 	struct sockaddr *src, struct socket *so)
    331  1.93.2.1       tls {
    332  1.93.2.1       tls 	struct mbuf *opts = NULL;
    333  1.93.2.1       tls 	struct mbuf *n;
    334  1.93.2.1       tls 	struct in6pcb *in6p = NULL;
    335  1.93.2.1       tls 
    336  1.93.2.1       tls 	if (!so)
    337  1.93.2.1       tls 		return;
    338  1.93.2.1       tls 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
    339  1.93.2.1       tls 		return;
    340  1.93.2.1       tls 	in6p = sotoin6pcb(so);
    341  1.93.2.1       tls 
    342  1.93.2.1       tls #if defined(IPSEC)
    343  1.93.2.1       tls 	/* check AH/ESP integrity. */
    344  1.93.2.1       tls 	if (ipsec_used && so != NULL && ipsec6_in_reject_so(m, so)) {
    345  1.93.2.1       tls 		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
    346  1.93.2.1       tls 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
    347  1.93.2.1       tls 			icmp6_error(n, ICMP6_DST_UNREACH,
    348  1.93.2.1       tls 			    ICMP6_DST_UNREACH_ADMIN, 0);
    349  1.93.2.1       tls 		return;
    350  1.93.2.1       tls 	}
    351  1.93.2.1       tls #endif /*IPSEC*/
    352  1.93.2.1       tls 
    353  1.93.2.1       tls 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    354  1.93.2.1       tls 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
    355  1.93.2.1       tls #ifdef SO_OTIMESTAMP
    356  1.93.2.1       tls 		    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
    357  1.93.2.1       tls #endif
    358  1.93.2.1       tls 		    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
    359  1.93.2.1       tls 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
    360  1.93.2.1       tls 			ip6_savecontrol(in6p, &opts, ip6, n);
    361  1.93.2.1       tls 		}
    362      1.90  christos 
    363  1.93.2.1       tls 		m_adj(n, off);
    364  1.93.2.1       tls 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    365  1.93.2.1       tls 			m_freem(n);
    366  1.93.2.1       tls 			if (opts)
    367  1.93.2.1       tls 				m_freem(opts);
    368  1.93.2.1       tls 			so->so_rcv.sb_overflowed++;
    369  1.93.2.1       tls 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
    370  1.93.2.1       tls 		} else
    371  1.93.2.1       tls 			sorwakeup(so);
    372  1.93.2.1       tls 	}
    373  1.93.2.1       tls }
    374       1.2    itojun 
    375       1.2    itojun int
    376  1.93.2.1       tls udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
    377  1.93.2.1       tls 	struct mbuf *m, int off)
    378  1.93.2.1       tls {
    379  1.93.2.1       tls 	u_int16_t sport, dport;
    380  1.93.2.1       tls 	int rcvcnt;
    381  1.93.2.1       tls 	struct in6_addr src6, *dst6;
    382  1.93.2.1       tls 	const struct in_addr *dst4;
    383  1.93.2.1       tls 	struct inpcb_hdr *inph;
    384  1.93.2.1       tls 	struct in6pcb *in6p;
    385  1.93.2.1       tls 
    386  1.93.2.1       tls 	rcvcnt = 0;
    387  1.93.2.1       tls 	off += sizeof(struct udphdr);	/* now, offset of payload */
    388  1.93.2.1       tls 
    389  1.93.2.1       tls 	if (af != AF_INET && af != AF_INET6)
    390  1.93.2.1       tls 		goto bad;
    391  1.93.2.1       tls 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
    392  1.93.2.1       tls 		goto bad;
    393  1.93.2.1       tls 
    394  1.93.2.1       tls 	src6 = src->sin6_addr;
    395  1.93.2.1       tls 	if (sa6_recoverscope(src) != 0) {
    396  1.93.2.1       tls 		/* XXX: should be impossible. */
    397  1.93.2.1       tls 		goto bad;
    398  1.93.2.1       tls 	}
    399  1.93.2.1       tls 	sport = src->sin6_port;
    400  1.93.2.1       tls 
    401  1.93.2.1       tls 	dport = dst->sin6_port;
    402  1.93.2.1       tls 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
    403  1.93.2.1       tls 	dst6 = &dst->sin6_addr;
    404  1.93.2.1       tls 
    405  1.93.2.1       tls 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
    406  1.93.2.1       tls 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
    407  1.93.2.1       tls 		/*
    408  1.93.2.1       tls 		 * Deliver a multicast or broadcast datagram to *all* sockets
    409  1.93.2.1       tls 		 * for which the local and remote addresses and ports match
    410  1.93.2.1       tls 		 * those of the incoming datagram.  This allows more than
    411  1.93.2.1       tls 		 * one process to receive multi/broadcasts on the same port.
    412  1.93.2.1       tls 		 * (This really ought to be done for unicast datagrams as
    413  1.93.2.1       tls 		 * well, but that would cause problems with existing
    414  1.93.2.1       tls 		 * applications that open both address-specific sockets and
    415  1.93.2.1       tls 		 * a wildcard socket listening to the same port -- they would
    416  1.93.2.1       tls 		 * end up receiving duplicates of every unicast datagram.
    417  1.93.2.1       tls 		 * Those applications open the multiple sockets to overcome an
    418  1.93.2.1       tls 		 * inadequacy of the UDP socket interface, but for backwards
    419  1.93.2.1       tls 		 * compatibility we avoid the problem here rather than
    420  1.93.2.1       tls 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    421  1.93.2.1       tls 		 */
    422  1.93.2.1       tls 
    423  1.93.2.1       tls 		/*
    424  1.93.2.1       tls 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    425  1.93.2.1       tls 		 * we need udpiphdr for IPsec processing so we do that later.
    426  1.93.2.1       tls 		 */
    427  1.93.2.1       tls 		/*
    428  1.93.2.1       tls 		 * Locate pcb(s) for datagram.
    429  1.93.2.1       tls 		 */
    430  1.93.2.1       tls 		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
    431  1.93.2.1       tls 			in6p = (struct in6pcb *)inph;
    432  1.93.2.1       tls 			if (in6p->in6p_af != AF_INET6)
    433  1.93.2.1       tls 				continue;
    434  1.93.2.1       tls 
    435  1.93.2.1       tls 			if (in6p->in6p_lport != dport)
    436  1.93.2.1       tls 				continue;
    437  1.93.2.1       tls 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    438  1.93.2.1       tls 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    439  1.93.2.1       tls 				    dst6))
    440  1.93.2.1       tls 					continue;
    441  1.93.2.1       tls 			} else {
    442  1.93.2.1       tls 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
    443  1.93.2.1       tls 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    444  1.93.2.1       tls 					continue;
    445  1.93.2.1       tls 			}
    446  1.93.2.1       tls 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    447  1.93.2.1       tls 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    448  1.93.2.1       tls 				    &src6) || in6p->in6p_fport != sport)
    449  1.93.2.1       tls 					continue;
    450  1.93.2.1       tls 			} else {
    451  1.93.2.1       tls 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
    452  1.93.2.1       tls 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    453  1.93.2.1       tls 					continue;
    454  1.93.2.1       tls 			}
    455  1.93.2.1       tls 
    456  1.93.2.1       tls 			udp6_sendup(m, off, (struct sockaddr *)src,
    457  1.93.2.1       tls 				in6p->in6p_socket);
    458  1.93.2.1       tls 			rcvcnt++;
    459  1.93.2.1       tls 
    460  1.93.2.1       tls 			/*
    461  1.93.2.1       tls 			 * Don't look for additional matches if this one does
    462  1.93.2.1       tls 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    463  1.93.2.1       tls 			 * socket options set.  This heuristic avoids searching
    464  1.93.2.1       tls 			 * through all pcbs in the common case of a non-shared
    465  1.93.2.1       tls 			 * port.  It assumes that an application will never
    466  1.93.2.1       tls 			 * clear these options after setting them.
    467  1.93.2.1       tls 			 */
    468  1.93.2.1       tls 			if ((in6p->in6p_socket->so_options &
    469  1.93.2.1       tls 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    470  1.93.2.1       tls 				break;
    471  1.93.2.1       tls 		}
    472  1.93.2.1       tls 	} else {
    473  1.93.2.1       tls 		/*
    474  1.93.2.1       tls 		 * Locate pcb for datagram.
    475  1.93.2.1       tls 		 */
    476  1.93.2.1       tls 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
    477  1.93.2.1       tls 					     dport, 0, 0);
    478  1.93.2.1       tls 		if (in6p == 0) {
    479  1.93.2.1       tls 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
    480  1.93.2.1       tls 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
    481  1.93.2.1       tls 			if (in6p == 0)
    482  1.93.2.1       tls 				return rcvcnt;
    483  1.93.2.1       tls 		}
    484  1.93.2.1       tls 
    485  1.93.2.1       tls 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
    486  1.93.2.1       tls 		rcvcnt++;
    487  1.93.2.1       tls 	}
    488  1.93.2.1       tls 
    489  1.93.2.1       tls bad:
    490  1.93.2.1       tls 	return rcvcnt;
    491  1.93.2.1       tls }
    492  1.93.2.1       tls 
    493  1.93.2.1       tls int
    494  1.93.2.1       tls udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
    495  1.93.2.1       tls {
    496  1.93.2.1       tls 
    497  1.93.2.1       tls 	/*
    498  1.93.2.1       tls 	 * XXX it's better to record and check if this mbuf is
    499  1.93.2.1       tls 	 * already checked.
    500  1.93.2.1       tls 	 */
    501  1.93.2.1       tls 
    502  1.93.2.1       tls 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
    503  1.93.2.1       tls 		goto good;
    504  1.93.2.1       tls 	}
    505  1.93.2.1       tls 	if (uh->uh_sum == 0) {
    506  1.93.2.1       tls 		UDP6_STATINC(UDP6_STAT_NOSUM);
    507  1.93.2.1       tls 		goto bad;
    508  1.93.2.1       tls 	}
    509  1.93.2.1       tls 
    510  1.93.2.1       tls 	switch (m->m_pkthdr.csum_flags &
    511  1.93.2.1       tls 	    ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
    512  1.93.2.1       tls 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    513  1.93.2.1       tls 	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
    514  1.93.2.1       tls 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
    515  1.93.2.1       tls 		UDP6_STATINC(UDP6_STAT_BADSUM);
    516  1.93.2.1       tls 		goto bad;
    517  1.93.2.1       tls 
    518  1.93.2.1       tls #if 0 /* notyet */
    519  1.93.2.1       tls 	case M_CSUM_UDPv6|M_CSUM_DATA:
    520  1.93.2.1       tls #endif
    521  1.93.2.1       tls 
    522  1.93.2.1       tls 	case M_CSUM_UDPv6:
    523  1.93.2.1       tls 		/* Checksum was okay. */
    524  1.93.2.1       tls 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
    525  1.93.2.1       tls 		break;
    526  1.93.2.1       tls 
    527  1.93.2.1       tls 	default:
    528  1.93.2.1       tls 		/*
    529  1.93.2.1       tls 		 * Need to compute it ourselves.  Maybe skip checksum
    530  1.93.2.1       tls 		 * on loopback interfaces.
    531  1.93.2.1       tls 		 */
    532  1.93.2.1       tls 		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
    533  1.93.2.1       tls 		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
    534  1.93.2.1       tls 			UDP6_STATINC(UDP6_STAT_BADSUM);
    535  1.93.2.1       tls 			goto bad;
    536  1.93.2.1       tls 		}
    537  1.93.2.1       tls 	}
    538  1.93.2.1       tls 
    539  1.93.2.1       tls good:
    540  1.93.2.1       tls 	return 0;
    541  1.93.2.1       tls bad:
    542  1.93.2.1       tls 	return -1;
    543  1.93.2.1       tls }
    544  1.93.2.1       tls 
    545  1.93.2.1       tls int
    546  1.93.2.1       tls udp6_input(struct mbuf **mp, int *offp, int proto)
    547  1.93.2.1       tls {
    548  1.93.2.1       tls 	struct mbuf *m = *mp;
    549  1.93.2.1       tls 	int off = *offp;
    550  1.93.2.1       tls 	struct sockaddr_in6 src, dst;
    551  1.93.2.1       tls 	struct ip6_hdr *ip6;
    552  1.93.2.1       tls 	struct udphdr *uh;
    553  1.93.2.1       tls 	u_int32_t plen, ulen;
    554  1.93.2.1       tls 
    555  1.93.2.1       tls 	ip6 = mtod(m, struct ip6_hdr *);
    556  1.93.2.1       tls 
    557  1.93.2.1       tls #if defined(NFAITH) && 0 < NFAITH
    558  1.93.2.1       tls 	if (faithprefix(&ip6->ip6_dst)) {
    559  1.93.2.1       tls 		/* send icmp6 host unreach? */
    560  1.93.2.1       tls 		m_freem(m);
    561  1.93.2.1       tls 		return IPPROTO_DONE;
    562  1.93.2.1       tls 	}
    563  1.93.2.1       tls #endif
    564  1.93.2.1       tls 
    565  1.93.2.1       tls 	UDP6_STATINC(UDP6_STAT_IPACKETS);
    566  1.93.2.1       tls 
    567  1.93.2.1       tls 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
    568  1.93.2.1       tls 	plen = m->m_pkthdr.len - off;
    569  1.93.2.1       tls 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    570  1.93.2.1       tls 	if (uh == NULL) {
    571  1.93.2.1       tls 		IP6_STATINC(IP6_STAT_TOOSHORT);
    572  1.93.2.1       tls 		return IPPROTO_DONE;
    573  1.93.2.1       tls 	}
    574  1.93.2.1       tls 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    575  1.93.2.1       tls 	ulen = ntohs((u_short)uh->uh_ulen);
    576  1.93.2.1       tls 	/*
    577  1.93.2.1       tls 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    578  1.93.2.1       tls 	 * iff payload length > 0xffff.
    579  1.93.2.1       tls 	 */
    580  1.93.2.1       tls 	if (ulen == 0 && plen > 0xffff)
    581  1.93.2.1       tls 		ulen = plen;
    582  1.93.2.1       tls 
    583  1.93.2.1       tls 	if (plen != ulen) {
    584  1.93.2.1       tls 		UDP6_STATINC(UDP6_STAT_BADLEN);
    585  1.93.2.1       tls 		goto bad;
    586  1.93.2.1       tls 	}
    587  1.93.2.1       tls 
    588  1.93.2.1       tls 	/* destination port of 0 is illegal, based on RFC768. */
    589  1.93.2.1       tls 	if (uh->uh_dport == 0)
    590  1.93.2.1       tls 		goto bad;
    591  1.93.2.1       tls 
    592  1.93.2.1       tls 	/* Be proactive about malicious use of IPv4 mapped address */
    593  1.93.2.1       tls 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    594  1.93.2.1       tls 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    595  1.93.2.1       tls 		/* XXX stat */
    596  1.93.2.1       tls 		goto bad;
    597  1.93.2.1       tls 	}
    598  1.93.2.1       tls 
    599  1.93.2.1       tls 	/*
    600  1.93.2.1       tls 	 * Checksum extended UDP header and data.  Maybe skip checksum
    601  1.93.2.1       tls 	 * on loopback interfaces.
    602  1.93.2.1       tls 	 */
    603  1.93.2.1       tls 	if (udp6_input_checksum(m, uh, off, ulen))
    604  1.93.2.1       tls 		goto bad;
    605  1.93.2.1       tls 
    606  1.93.2.1       tls 	/*
    607  1.93.2.1       tls 	 * Construct source and dst sockaddrs.
    608  1.93.2.1       tls 	 */
    609  1.93.2.1       tls 	memset(&src, 0, sizeof(src));
    610  1.93.2.1       tls 	src.sin6_family = AF_INET6;
    611  1.93.2.1       tls 	src.sin6_len = sizeof(struct sockaddr_in6);
    612  1.93.2.1       tls 	src.sin6_addr = ip6->ip6_src;
    613  1.93.2.1       tls 	src.sin6_port = uh->uh_sport;
    614  1.93.2.1       tls 	memset(&dst, 0, sizeof(dst));
    615  1.93.2.1       tls 	dst.sin6_family = AF_INET6;
    616  1.93.2.1       tls 	dst.sin6_len = sizeof(struct sockaddr_in6);
    617  1.93.2.1       tls 	dst.sin6_addr = ip6->ip6_dst;
    618  1.93.2.1       tls 	dst.sin6_port = uh->uh_dport;
    619  1.93.2.1       tls 
    620  1.93.2.1       tls 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
    621  1.93.2.1       tls 		if (m->m_flags & M_MCAST) {
    622  1.93.2.1       tls 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
    623  1.93.2.1       tls 			goto bad;
    624  1.93.2.1       tls 		}
    625  1.93.2.1       tls 		UDP6_STATINC(UDP6_STAT_NOPORT);
    626  1.93.2.1       tls 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    627  1.93.2.1       tls 		m = NULL;
    628  1.93.2.1       tls 	}
    629  1.93.2.1       tls 
    630  1.93.2.1       tls bad:
    631  1.93.2.1       tls 	if (m)
    632  1.93.2.1       tls 		m_freem(m);
    633  1.93.2.1       tls 	return IPPROTO_DONE;
    634  1.93.2.1       tls }
    635  1.93.2.1       tls 
    636  1.93.2.1       tls static int
    637  1.93.2.1       tls udp6_attach(struct socket *so, int proto)
    638  1.93.2.1       tls {
    639  1.93.2.1       tls 	struct in6pcb *in6p;
    640  1.93.2.1       tls 	int s, error;
    641  1.93.2.1       tls 
    642  1.93.2.1       tls 	KASSERT(sotoin6pcb(so) == NULL);
    643  1.93.2.1       tls 	sosetlock(so);
    644  1.93.2.1       tls 
    645  1.93.2.1       tls 	/*
    646  1.93.2.1       tls 	 * MAPPED_ADDR implementation spec:
    647  1.93.2.1       tls 	 *  Always attach for IPv6, and only when necessary for IPv4.
    648  1.93.2.1       tls 	 */
    649  1.93.2.1       tls 	s = splsoftnet();
    650  1.93.2.1       tls 	error = in6_pcballoc(so, &udbtable);
    651  1.93.2.1       tls 	splx(s);
    652  1.93.2.1       tls 	if (error) {
    653  1.93.2.1       tls 		return error;
    654  1.93.2.1       tls 	}
    655  1.93.2.1       tls 	error = soreserve(so, udp6_sendspace, udp6_recvspace);
    656  1.93.2.1       tls 	if (error) {
    657  1.93.2.1       tls 		return error;
    658  1.93.2.1       tls 	}
    659  1.93.2.1       tls 	in6p = sotoin6pcb(so);
    660  1.93.2.1       tls 	in6p->in6p_cksum = -1;	/* just to be sure */
    661  1.93.2.1       tls 
    662  1.93.2.1       tls 	KASSERT(solocked(so));
    663  1.93.2.1       tls 	return 0;
    664  1.93.2.1       tls }
    665  1.93.2.1       tls 
    666  1.93.2.1       tls static void
    667  1.93.2.1       tls udp6_detach(struct socket *so)
    668  1.93.2.1       tls {
    669  1.93.2.1       tls 	struct in6pcb *in6p = sotoin6pcb(so);
    670  1.93.2.1       tls 	int s;
    671  1.93.2.1       tls 
    672  1.93.2.1       tls 	KASSERT(solocked(so));
    673  1.93.2.1       tls 	KASSERT(in6p != NULL);
    674  1.93.2.1       tls 
    675  1.93.2.1       tls 	s = splsoftnet();
    676  1.93.2.1       tls 	in6_pcbdetach(in6p);
    677  1.93.2.1       tls 	splx(s);
    678  1.93.2.1       tls }
    679  1.93.2.1       tls 
    680  1.93.2.1       tls static int
    681  1.93.2.1       tls udp6_accept(struct socket *so, struct mbuf *nam)
    682  1.93.2.1       tls {
    683  1.93.2.1       tls 	KASSERT(solocked(so));
    684  1.93.2.1       tls 
    685  1.93.2.1       tls 	return EOPNOTSUPP;
    686  1.93.2.1       tls }
    687  1.93.2.1       tls 
    688  1.93.2.1       tls static int
    689  1.93.2.1       tls udp6_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
    690  1.93.2.1       tls {
    691  1.93.2.1       tls 	struct in6pcb *in6p = sotoin6pcb(so);
    692  1.93.2.1       tls 	int error = 0;
    693  1.93.2.1       tls 	int s;
    694  1.93.2.1       tls 
    695  1.93.2.1       tls 	KASSERT(solocked(so));
    696  1.93.2.1       tls 	KASSERT(in6p != NULL);
    697  1.93.2.1       tls 
    698  1.93.2.1       tls 	s = splsoftnet();
    699  1.93.2.1       tls 	error = in6_pcbbind(in6p, nam, l);
    700  1.93.2.1       tls 	splx(s);
    701  1.93.2.1       tls 	return error;
    702  1.93.2.1       tls }
    703  1.93.2.1       tls 
    704  1.93.2.1       tls static int
    705  1.93.2.1       tls udp6_listen(struct socket *so, struct lwp *l)
    706  1.93.2.1       tls {
    707  1.93.2.1       tls 	KASSERT(solocked(so));
    708  1.93.2.1       tls 
    709  1.93.2.1       tls 	return EOPNOTSUPP;
    710  1.93.2.1       tls }
    711  1.93.2.1       tls 
    712  1.93.2.1       tls static int
    713  1.93.2.1       tls udp6_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    714  1.93.2.1       tls {
    715  1.93.2.1       tls 	struct in6pcb *in6p = sotoin6pcb(so);
    716  1.93.2.1       tls 	int error = 0;
    717  1.93.2.1       tls 	int s;
    718  1.93.2.1       tls 
    719  1.93.2.1       tls 	KASSERT(solocked(so));
    720  1.93.2.1       tls 	KASSERT(in6p != NULL);
    721  1.93.2.1       tls 
    722  1.93.2.1       tls 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
    723  1.93.2.1       tls 		return EISCONN;
    724  1.93.2.1       tls 	s = splsoftnet();
    725  1.93.2.1       tls 	error = in6_pcbconnect(in6p, nam, l);
    726  1.93.2.1       tls 	splx(s);
    727  1.93.2.1       tls 	if (error == 0)
    728  1.93.2.1       tls 		soisconnected(so);
    729  1.93.2.1       tls 
    730  1.93.2.1       tls 	return error;
    731  1.93.2.1       tls }
    732  1.93.2.1       tls 
    733  1.93.2.1       tls static int
    734  1.93.2.1       tls udp6_connect2(struct socket *so, struct socket *so2)
    735  1.93.2.1       tls {
    736  1.93.2.1       tls 	KASSERT(solocked(so));
    737  1.93.2.1       tls 
    738  1.93.2.1       tls 	return EOPNOTSUPP;
    739  1.93.2.1       tls }
    740  1.93.2.1       tls 
    741  1.93.2.1       tls static int
    742  1.93.2.1       tls udp6_disconnect(struct socket *so)
    743  1.93.2.1       tls {
    744  1.93.2.1       tls 	struct in6pcb *in6p = sotoin6pcb(so);
    745  1.93.2.1       tls 	int s;
    746  1.93.2.1       tls 
    747  1.93.2.1       tls 	KASSERT(solocked(so));
    748  1.93.2.1       tls 	KASSERT(in6p != NULL);
    749  1.93.2.1       tls 
    750  1.93.2.1       tls 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
    751  1.93.2.1       tls 		return ENOTCONN;
    752  1.93.2.1       tls 
    753  1.93.2.1       tls 	s = splsoftnet();
    754  1.93.2.1       tls 	in6_pcbdisconnect(in6p);
    755  1.93.2.1       tls 	memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
    756  1.93.2.1       tls 	splx(s);
    757  1.93.2.1       tls 
    758  1.93.2.1       tls 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    759  1.93.2.1       tls 	in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
    760  1.93.2.1       tls 	return 0;
    761  1.93.2.1       tls }
    762  1.93.2.1       tls 
    763  1.93.2.1       tls static int
    764  1.93.2.1       tls udp6_shutdown(struct socket *so)
    765       1.2    itojun {
    766  1.93.2.1       tls 	int s;
    767       1.2    itojun 
    768  1.93.2.1       tls 	s = splsoftnet();
    769  1.93.2.1       tls 	socantsendmore(so);
    770  1.93.2.1       tls 	splx(s);
    771  1.93.2.1       tls 
    772  1.93.2.1       tls 	return 0;
    773  1.93.2.1       tls }
    774  1.93.2.1       tls 
    775  1.93.2.1       tls static int
    776  1.93.2.1       tls udp6_abort(struct socket *so)
    777  1.93.2.1       tls {
    778  1.93.2.1       tls 	int s;
    779  1.93.2.1       tls 
    780  1.93.2.1       tls 	KASSERT(solocked(so));
    781  1.93.2.1       tls 	KASSERT(sotoin6pcb(so) != NULL);
    782  1.93.2.1       tls 
    783  1.93.2.1       tls 	s = splsoftnet();
    784  1.93.2.1       tls 	soisdisconnected(so);
    785  1.93.2.1       tls 	in6_pcbdetach(sotoin6pcb(so));
    786  1.93.2.1       tls 	splx(s);
    787  1.93.2.1       tls 
    788  1.93.2.1       tls 	return 0;
    789  1.93.2.1       tls }
    790  1.93.2.1       tls 
    791  1.93.2.1       tls static int
    792  1.93.2.1       tls udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
    793  1.93.2.1       tls {
    794      1.27    itojun 	/*
    795       1.2    itojun 	 * MAPPED_ADDR implementation info:
    796       1.2    itojun 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    797       1.2    itojun 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    798       1.2    itojun 	 *  and they don't associate any addr to their socket.  Then
    799       1.2    itojun 	 *  socket family is only hint about the PRU_CONTROL'ed address
    800       1.2    itojun 	 *  family, especially when getting addrs from kernel.
    801       1.2    itojun 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    802       1.2    itojun 	 *  and AF_INET6 socket for AF_INET6 addrs.
    803       1.2    itojun 	 */
    804  1.93.2.1       tls 	return in6_control(so, cmd, addr6, ifp);
    805  1.93.2.1       tls }
    806       1.2    itojun 
    807  1.93.2.1       tls static int
    808  1.93.2.1       tls udp6_stat(struct socket *so, struct stat *ub)
    809  1.93.2.1       tls {
    810  1.93.2.1       tls 	KASSERT(solocked(so));
    811       1.2    itojun 
    812  1.93.2.1       tls 	/* stat: don't bother with a blocksize */
    813  1.93.2.1       tls 	return 0;
    814  1.93.2.1       tls }
    815       1.2    itojun 
    816  1.93.2.1       tls static int
    817  1.93.2.1       tls udp6_peeraddr(struct socket *so, struct mbuf *nam)
    818  1.93.2.1       tls {
    819  1.93.2.1       tls 	KASSERT(solocked(so));
    820  1.93.2.1       tls 	KASSERT(sotoin6pcb(so) != NULL);
    821  1.93.2.1       tls 	KASSERT(nam != NULL);
    822       1.2    itojun 
    823  1.93.2.1       tls 	in6_setpeeraddr(sotoin6pcb(so), nam);
    824  1.93.2.1       tls 	return 0;
    825  1.93.2.1       tls }
    826       1.2    itojun 
    827  1.93.2.1       tls static int
    828  1.93.2.1       tls udp6_sockaddr(struct socket *so, struct mbuf *nam)
    829  1.93.2.1       tls {
    830  1.93.2.1       tls 	KASSERT(solocked(so));
    831  1.93.2.1       tls 	KASSERT(sotoin6pcb(so) != NULL);
    832  1.93.2.1       tls 	KASSERT(nam != NULL);
    833       1.2    itojun 
    834  1.93.2.1       tls 	in6_setsockaddr(sotoin6pcb(so), nam);
    835  1.93.2.1       tls 	return 0;
    836  1.93.2.1       tls }
    837       1.2    itojun 
    838  1.93.2.1       tls static int
    839  1.93.2.1       tls udp6_rcvd(struct socket *so, int flags, struct lwp *l)
    840  1.93.2.1       tls {
    841  1.93.2.1       tls 	KASSERT(solocked(so));
    842       1.2    itojun 
    843  1.93.2.1       tls 	return EOPNOTSUPP;
    844  1.93.2.1       tls }
    845       1.2    itojun 
    846  1.93.2.1       tls static int
    847  1.93.2.1       tls udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
    848  1.93.2.1       tls {
    849  1.93.2.1       tls 	KASSERT(solocked(so));
    850       1.2    itojun 
    851  1.93.2.1       tls 	return EOPNOTSUPP;
    852  1.93.2.1       tls }
    853       1.2    itojun 
    854  1.93.2.1       tls static int
    855  1.93.2.1       tls udp6_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
    856  1.93.2.1       tls     struct mbuf *control, struct lwp *l)
    857  1.93.2.1       tls {
    858  1.93.2.1       tls 	struct in6pcb *in6p = sotoin6pcb(so);
    859  1.93.2.1       tls 	int error = 0;
    860  1.93.2.1       tls 	int s;
    861  1.93.2.1       tls 
    862  1.93.2.1       tls 	KASSERT(solocked(so));
    863  1.93.2.1       tls 	KASSERT(in6p != NULL);
    864  1.93.2.1       tls 	KASSERT(m != NULL);
    865  1.93.2.1       tls 
    866  1.93.2.1       tls 	s = splsoftnet();
    867  1.93.2.1       tls 	error = udp6_output(in6p, m, nam, control, l);
    868  1.93.2.1       tls 	splx(s);
    869  1.93.2.1       tls 
    870  1.93.2.1       tls 	return error;
    871  1.93.2.1       tls }
    872  1.93.2.1       tls 
    873  1.93.2.1       tls static int
    874  1.93.2.1       tls udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    875  1.93.2.1       tls {
    876  1.93.2.1       tls 	KASSERT(solocked(so));
    877  1.93.2.1       tls 
    878  1.93.2.1       tls 	if (m)
    879  1.93.2.1       tls 		m_freem(m);
    880  1.93.2.1       tls 	if (control)
    881  1.93.2.1       tls 		m_freem(control);
    882  1.93.2.1       tls 
    883  1.93.2.1       tls 	return EOPNOTSUPP;
    884  1.93.2.1       tls }
    885  1.93.2.1       tls 
    886  1.93.2.1       tls static int
    887  1.93.2.1       tls udp6_purgeif(struct socket *so, struct ifnet *ifp)
    888  1.93.2.1       tls {
    889  1.93.2.1       tls 
    890  1.93.2.1       tls 	mutex_enter(softnet_lock);
    891  1.93.2.1       tls 	in6_pcbpurgeif0(&udbtable, ifp);
    892  1.93.2.1       tls 	in6_purgeif(ifp);
    893  1.93.2.1       tls 	in6_pcbpurgeif(&udbtable, ifp);
    894  1.93.2.1       tls 	mutex_exit(softnet_lock);
    895       1.2    itojun 
    896  1.93.2.1       tls 	return 0;
    897  1.93.2.1       tls }
    898  1.93.2.1       tls 
    899  1.93.2.1       tls int
    900  1.93.2.1       tls udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
    901  1.93.2.1       tls     struct mbuf *control, struct lwp *l)
    902  1.93.2.1       tls {
    903  1.93.2.1       tls 	int error = 0;
    904  1.93.2.1       tls 
    905  1.93.2.1       tls 	KASSERT(req != PRU_ATTACH);
    906  1.93.2.1       tls 	KASSERT(req != PRU_DETACH);
    907  1.93.2.1       tls 	KASSERT(req != PRU_ACCEPT);
    908  1.93.2.1       tls 	KASSERT(req != PRU_BIND);
    909  1.93.2.1       tls 	KASSERT(req != PRU_LISTEN);
    910  1.93.2.1       tls 	KASSERT(req != PRU_CONNECT);
    911  1.93.2.1       tls 	KASSERT(req != PRU_CONNECT2);
    912  1.93.2.1       tls 	KASSERT(req != PRU_DISCONNECT);
    913  1.93.2.1       tls 	KASSERT(req != PRU_SHUTDOWN);
    914  1.93.2.1       tls 	KASSERT(req != PRU_ABORT);
    915  1.93.2.1       tls 	KASSERT(req != PRU_CONTROL);
    916  1.93.2.1       tls 	KASSERT(req != PRU_SENSE);
    917  1.93.2.1       tls 	KASSERT(req != PRU_PEERADDR);
    918  1.93.2.1       tls 	KASSERT(req != PRU_SOCKADDR);
    919  1.93.2.1       tls 	KASSERT(req != PRU_RCVD);
    920  1.93.2.1       tls 	KASSERT(req != PRU_RCVOOB);
    921  1.93.2.1       tls 	KASSERT(req != PRU_SEND);
    922  1.93.2.1       tls 	KASSERT(req != PRU_SENDOOB);
    923  1.93.2.1       tls 	KASSERT(req != PRU_PURGEIF);
    924  1.93.2.1       tls 
    925  1.93.2.1       tls 	if (sotoin6pcb(so) == NULL) {
    926  1.93.2.1       tls 		error = EINVAL;
    927  1.93.2.1       tls 		goto release;
    928  1.93.2.1       tls 	}
    929  1.93.2.1       tls 
    930  1.93.2.1       tls 	switch (req) {
    931       1.2    itojun 	case PRU_FASTTIMO:
    932       1.2    itojun 	case PRU_SLOWTIMO:
    933       1.2    itojun 	case PRU_PROTORCV:
    934       1.2    itojun 	case PRU_PROTOSEND:
    935       1.2    itojun 		error = EOPNOTSUPP;
    936       1.2    itojun 		break;
    937       1.2    itojun 
    938       1.2    itojun 	default:
    939       1.2    itojun 		panic("udp6_usrreq");
    940       1.2    itojun 	}
    941       1.2    itojun 
    942       1.2    itojun release:
    943      1.80    dyoung 	if (control != NULL)
    944       1.2    itojun 		m_freem(control);
    945      1.80    dyoung 	if (m != NULL)
    946       1.2    itojun 		m_freem(m);
    947      1.80    dyoung 	return error;
    948       1.2    itojun }
    949       1.2    itojun 
    950      1.82   thorpej static int
    951      1.82   thorpej sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
    952      1.82   thorpej {
    953      1.82   thorpej 
    954      1.86   thorpej 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
    955      1.82   thorpej }
    956      1.82   thorpej 
    957      1.88     pooka static void
    958      1.88     pooka sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
    959       1.2    itojun {
    960      1.93     pooka 
    961      1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    962      1.62    atatat 		       CTLFLAG_PERMANENT,
    963      1.61    atatat 		       CTLTYPE_NODE, "inet6", NULL,
    964      1.61    atatat 		       NULL, 0, NULL, 0,
    965      1.61    atatat 		       CTL_NET, PF_INET6, CTL_EOL);
    966      1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    967      1.62    atatat 		       CTLFLAG_PERMANENT,
    968      1.63    atatat 		       CTLTYPE_NODE, "udp6",
    969      1.63    atatat 		       SYSCTL_DESCR("UDPv6 related settings"),
    970      1.61    atatat 		       NULL, 0, NULL, 0,
    971      1.61    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
    972      1.61    atatat 
    973      1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    974      1.62    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    975      1.63    atatat 		       CTLTYPE_INT, "sendspace",
    976      1.63    atatat 		       SYSCTL_DESCR("Default UDP send buffer size"),
    977      1.61    atatat 		       NULL, 0, &udp6_sendspace, 0,
    978      1.61    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
    979      1.61    atatat 		       CTL_EOL);
    980      1.62    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    981      1.62    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    982      1.63    atatat 		       CTLTYPE_INT, "recvspace",
    983      1.63    atatat 		       SYSCTL_DESCR("Default UDP receive buffer size"),
    984      1.61    atatat 		       NULL, 0, &udp6_recvspace, 0,
    985      1.61    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
    986      1.61    atatat 		       CTL_EOL);
    987      1.64   thorpej 	sysctl_createv(clog, 0, NULL, NULL,
    988      1.64   thorpej 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    989      1.64   thorpej 		       CTLTYPE_INT, "do_loopback_cksum",
    990      1.64   thorpej 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
    991      1.64   thorpej 		       NULL, 0, &udp_do_loopback_cksum, 0,
    992      1.64   thorpej 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
    993      1.64   thorpej 		       CTL_EOL);
    994      1.65    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    995      1.65    atatat 		       CTLFLAG_PERMANENT,
    996      1.67    atatat 		       CTLTYPE_STRUCT, "pcblist",
    997      1.65    atatat 		       SYSCTL_DESCR("UDP protocol control block list"),
    998      1.65    atatat 		       sysctl_inpcblist, 0, &udbtable, 0,
    999      1.65    atatat 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
   1000      1.65    atatat 		       CTL_EOL);
   1001      1.70    rpaulo 	sysctl_createv(clog, 0, NULL, NULL,
   1002      1.70    rpaulo 		       CTLFLAG_PERMANENT,
   1003      1.70    rpaulo 		       CTLTYPE_STRUCT, "stats",
   1004      1.70    rpaulo 		       SYSCTL_DESCR("UDPv6 statistics"),
   1005      1.82   thorpej 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
   1006      1.70    rpaulo 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
   1007      1.70    rpaulo 		       CTL_EOL);
   1008       1.2    itojun }
   1009      1.82   thorpej 
   1010      1.82   thorpej void
   1011      1.82   thorpej udp6_statinc(u_int stat)
   1012      1.82   thorpej {
   1013      1.82   thorpej 
   1014      1.82   thorpej 	KASSERT(stat < UDP6_NSTATS);
   1015      1.82   thorpej 	UDP6_STATINC(stat);
   1016      1.82   thorpej }
   1017  1.93.2.1       tls 
   1018  1.93.2.1       tls PR_WRAP_USRREQS(udp6)
   1019  1.93.2.1       tls #define	udp6_attach	udp6_attach_wrapper
   1020  1.93.2.1       tls #define	udp6_detach	udp6_detach_wrapper
   1021  1.93.2.1       tls #define	udp6_accept	udp6_accept_wrapper
   1022  1.93.2.1       tls #define	udp6_bind	udp6_bind_wrapper
   1023  1.93.2.1       tls #define	udp6_listen	udp6_listen_wrapper
   1024  1.93.2.1       tls #define	udp6_connect	udp6_connect_wrapper
   1025  1.93.2.1       tls #define	udp6_connect2	udp6_connect2_wrapper
   1026  1.93.2.1       tls #define	udp6_disconnect	udp6_disconnect_wrapper
   1027  1.93.2.1       tls #define	udp6_shutdown	udp6_shutdown_wrapper
   1028  1.93.2.1       tls #define	udp6_abort	udp6_abort_wrapper
   1029  1.93.2.1       tls #define	udp6_ioctl	udp6_ioctl_wrapper
   1030  1.93.2.1       tls #define	udp6_stat	udp6_stat_wrapper
   1031  1.93.2.1       tls #define	udp6_peeraddr	udp6_peeraddr_wrapper
   1032  1.93.2.1       tls #define	udp6_sockaddr	udp6_sockaddr_wrapper
   1033  1.93.2.1       tls #define	udp6_rcvd	udp6_rcvd_wrapper
   1034  1.93.2.1       tls #define	udp6_recvoob	udp6_recvoob_wrapper
   1035  1.93.2.1       tls #define	udp6_send	udp6_send_wrapper
   1036  1.93.2.1       tls #define	udp6_sendoob	udp6_sendoob_wrapper
   1037  1.93.2.1       tls #define	udp6_purgeif	udp6_purgeif_wrapper
   1038  1.93.2.1       tls #define	udp6_usrreq	udp6_usrreq_wrapper
   1039  1.93.2.1       tls 
   1040  1.93.2.1       tls const struct pr_usrreqs udp6_usrreqs = {
   1041  1.93.2.1       tls 	.pr_attach	= udp6_attach,
   1042  1.93.2.1       tls 	.pr_detach	= udp6_detach,
   1043  1.93.2.1       tls 	.pr_accept	= udp6_accept,
   1044  1.93.2.1       tls 	.pr_bind	= udp6_bind,
   1045  1.93.2.1       tls 	.pr_listen	= udp6_listen,
   1046  1.93.2.1       tls 	.pr_connect	= udp6_connect,
   1047  1.93.2.1       tls 	.pr_connect2	= udp6_connect2,
   1048  1.93.2.1       tls 	.pr_disconnect	= udp6_disconnect,
   1049  1.93.2.1       tls 	.pr_shutdown	= udp6_shutdown,
   1050  1.93.2.1       tls 	.pr_abort	= udp6_abort,
   1051  1.93.2.1       tls 	.pr_ioctl	= udp6_ioctl,
   1052  1.93.2.1       tls 	.pr_stat	= udp6_stat,
   1053  1.93.2.1       tls 	.pr_peeraddr	= udp6_peeraddr,
   1054  1.93.2.1       tls 	.pr_sockaddr	= udp6_sockaddr,
   1055  1.93.2.1       tls 	.pr_rcvd	= udp6_rcvd,
   1056  1.93.2.1       tls 	.pr_recvoob	= udp6_recvoob,
   1057  1.93.2.1       tls 	.pr_send	= udp6_send,
   1058  1.93.2.1       tls 	.pr_sendoob	= udp6_sendoob,
   1059  1.93.2.1       tls 	.pr_purgeif	= udp6_purgeif,
   1060  1.93.2.1       tls 	.pr_generic	= udp6_usrreq,
   1061  1.93.2.1       tls };
   1062