Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.178
      1  1.178   minskim /*	$NetBSD: udp_usrreq.c,v 1.178 2009/07/19 23:17:33 minskim Exp $	*/
      2   1.48    itojun 
      3   1.48    itojun /*
      4   1.48    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.48    itojun  * All rights reserved.
      6   1.94    itojun  *
      7   1.48    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.48    itojun  * modification, are permitted provided that the following conditions
      9   1.48    itojun  * are met:
     10   1.48    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.48    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.48    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.48    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.48    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.48    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.48    itojun  *    may be used to endorse or promote products derived from this software
     17   1.48    itojun  *    without specific prior written permission.
     18   1.94    itojun  *
     19   1.48    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.48    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.48    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.48    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.48    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.48    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.48    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.48    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.48    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.48    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.48    itojun  * SUCH DAMAGE.
     30   1.48    itojun  */
     31   1.14       cgd 
     32    1.1       cgd /*
     33   1.44   thorpej  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
     34   1.13   mycroft  *	The Regents of the University of California.  All rights reserved.
     35    1.1       cgd  *
     36    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37    1.1       cgd  * modification, are permitted provided that the following conditions
     38    1.1       cgd  * are met:
     39    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44  1.104       agc  * 3. Neither the name of the University nor the names of its contributors
     45    1.1       cgd  *    may be used to endorse or promote products derived from this software
     46    1.1       cgd  *    without specific prior written permission.
     47    1.1       cgd  *
     48    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58    1.1       cgd  * SUCH DAMAGE.
     59    1.1       cgd  *
     60   1.44   thorpej  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
     61    1.1       cgd  */
     62   1.91     lukem 
     63   1.91     lukem #include <sys/cdefs.h>
     64  1.178   minskim __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.178 2009/07/19 23:17:33 minskim Exp $");
     65   1.50   thorpej 
     66   1.77      soda #include "opt_inet.h"
     67  1.174  christos #include "opt_compat_netbsd.h"
     68   1.50   thorpej #include "opt_ipsec.h"
     69   1.78   thorpej #include "opt_inet_csum.h"
     70   1.64        ws #include "opt_ipkdb.h"
     71  1.101    martin #include "opt_mbuftrace.h"
     72    1.1       cgd 
     73    1.5   mycroft #include <sys/param.h>
     74    1.5   mycroft #include <sys/malloc.h>
     75    1.5   mycroft #include <sys/mbuf.h>
     76    1.5   mycroft #include <sys/protosw.h>
     77    1.5   mycroft #include <sys/socket.h>
     78    1.5   mycroft #include <sys/socketvar.h>
     79   1.13   mycroft #include <sys/errno.h>
     80    1.5   mycroft #include <sys/stat.h>
     81   1.27  christos #include <sys/systm.h>
     82   1.27  christos #include <sys/proc.h>
     83   1.53    itojun #include <sys/domain.h>
     84   1.27  christos #include <sys/sysctl.h>
     85    1.1       cgd 
     86    1.5   mycroft #include <net/if.h>
     87    1.5   mycroft #include <net/route.h>
     88    1.1       cgd 
     89    1.5   mycroft #include <netinet/in.h>
     90    1.5   mycroft #include <netinet/in_systm.h>
     91   1.15       cgd #include <netinet/in_var.h>
     92    1.5   mycroft #include <netinet/ip.h>
     93    1.5   mycroft #include <netinet/in_pcb.h>
     94    1.5   mycroft #include <netinet/ip_var.h>
     95    1.5   mycroft #include <netinet/ip_icmp.h>
     96    1.5   mycroft #include <netinet/udp.h>
     97    1.5   mycroft #include <netinet/udp_var.h>
     98  1.166   thorpej #include <netinet/udp_private.h>
     99    1.1       cgd 
    100   1.53    itojun #ifdef INET6
    101   1.53    itojun #include <netinet/ip6.h>
    102   1.53    itojun #include <netinet/icmp6.h>
    103   1.53    itojun #include <netinet6/ip6_var.h>
    104  1.167   thorpej #include <netinet6/ip6_private.h>
    105   1.53    itojun #include <netinet6/in6_pcb.h>
    106   1.53    itojun #include <netinet6/udp6_var.h>
    107  1.168   thorpej #include <netinet6/udp6_private.h>
    108  1.146    rpaulo #include <netinet6/scope6_var.h>
    109   1.53    itojun #endif
    110   1.53    itojun 
    111   1.53    itojun #ifndef INET6
    112   1.53    itojun /* always need ip6.h for IP6_EXTHDR_GET */
    113   1.53    itojun #include <netinet/ip6.h>
    114   1.53    itojun #endif
    115   1.53    itojun 
    116   1.76    itojun #include "faith.h"
    117   1.76    itojun #if defined(NFAITH) && NFAITH > 0
    118   1.76    itojun #include <net/if_faith.h>
    119   1.76    itojun #endif
    120   1.76    itojun 
    121   1.27  christos #include <machine/stdarg.h>
    122   1.27  christos 
    123  1.105  jonathan #ifdef FAST_IPSEC
    124  1.105  jonathan #include <netipsec/ipsec.h>
    125  1.169   thorpej #include <netipsec/ipsec_var.h>
    126  1.169   thorpej #include <netipsec/ipsec_private.h>
    127  1.147  christos #include <netipsec/esp.h>
    128  1.105  jonathan #ifdef INET6
    129  1.105  jonathan #include <netipsec/ipsec6.h>
    130  1.105  jonathan #endif
    131  1.169   thorpej #endif	/* FAST_IPSEC */
    132  1.105  jonathan 
    133   1.48    itojun #ifdef IPSEC
    134   1.48    itojun #include <netinet6/ipsec.h>
    135  1.169   thorpej #include <netinet6/ipsec_private.h>
    136  1.147  christos #include <netinet6/esp.h>
    137   1.48    itojun #include <netkey/key.h>
    138  1.169   thorpej #endif /* IPSEC */
    139   1.48    itojun 
    140  1.174  christos #ifdef COMPAT_50
    141  1.174  christos #include <compat/sys/socket.h>
    142  1.174  christos #endif
    143  1.174  christos 
    144   1.64        ws #ifdef IPKDB
    145   1.64        ws #include <ipkdb/ipkdb.h>
    146   1.64        ws #endif
    147   1.64        ws 
    148    1.8   mycroft /*
    149    1.8   mycroft  * UDP protocol implementation.
    150    1.8   mycroft  * Per RFC 768, August, 1980.
    151    1.8   mycroft  */
    152    1.8   mycroft int	udpcksum = 1;
    153  1.141      yamt int	udp_do_loopback_cksum = 0;
    154   1.93      matt 
    155   1.93      matt struct	inpcbtable udbtable;
    156  1.164   thorpej 
    157  1.166   thorpej percpu_t *udpstat_percpu;
    158    1.8   mycroft 
    159   1.72    itojun #ifdef INET
    160  1.130      manu #ifdef IPSEC_NAT_T
    161  1.144      manu static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
    162  1.130      manu 	struct socket *);
    163  1.130      manu #endif
    164  1.119      matt static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
    165  1.119      matt 	struct socket *);
    166  1.119      matt static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
    167  1.144      manu 	struct mbuf **, int);
    168  1.129      yamt static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
    169   1.72    itojun #endif
    170   1.53    itojun #ifdef INET6
    171  1.119      matt static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
    172  1.119      matt 	struct socket *);
    173  1.119      matt static int udp6_realinput (int, struct sockaddr_in6 *,
    174  1.119      matt 	struct sockaddr_in6 *, struct mbuf *, int);
    175  1.129      yamt static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
    176   1.53    itojun #endif
    177   1.72    itojun #ifdef INET
    178  1.119      matt static	void udp_notify (struct inpcb *, int);
    179   1.72    itojun #endif
    180    1.7   mycroft 
    181   1.26   mycroft #ifndef UDBHASHSIZE
    182   1.26   mycroft #define	UDBHASHSIZE	128
    183   1.26   mycroft #endif
    184   1.26   mycroft int	udbhashsize = UDBHASHSIZE;
    185   1.26   mycroft 
    186   1.98      matt #ifdef MBUFTRACE
    187  1.150    dogcow struct mowner udp_mowner = MOWNER_INIT("udp", "");
    188  1.150    dogcow struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
    189  1.150    dogcow struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
    190   1.98      matt #endif
    191   1.98      matt 
    192   1.78   thorpej #ifdef UDP_CSUM_COUNTERS
    193   1.78   thorpej #include <sys/device.h>
    194   1.78   thorpej 
    195  1.140      yamt #if defined(INET)
    196   1.78   thorpej struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    197   1.78   thorpej     NULL, "udp", "hwcsum bad");
    198   1.78   thorpej struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    199   1.78   thorpej     NULL, "udp", "hwcsum ok");
    200   1.78   thorpej struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    201   1.78   thorpej     NULL, "udp", "hwcsum data");
    202   1.78   thorpej struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    203   1.78   thorpej     NULL, "udp", "swcsum");
    204   1.78   thorpej 
    205  1.120      matt EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
    206  1.120      matt EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
    207  1.120      matt EVCNT_ATTACH_STATIC(udp_hwcsum_data);
    208  1.120      matt EVCNT_ATTACH_STATIC(udp_swcsum);
    209  1.140      yamt #endif /* defined(INET) */
    210  1.140      yamt 
    211  1.140      yamt #if defined(INET6)
    212  1.140      yamt struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    213  1.140      yamt     NULL, "udp6", "hwcsum bad");
    214  1.140      yamt struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    215  1.140      yamt     NULL, "udp6", "hwcsum ok");
    216  1.140      yamt struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    217  1.140      yamt     NULL, "udp6", "hwcsum data");
    218  1.140      yamt struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    219  1.140      yamt     NULL, "udp6", "swcsum");
    220  1.140      yamt 
    221  1.140      yamt EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
    222  1.140      yamt EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
    223  1.140      yamt EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
    224  1.140      yamt EVCNT_ATTACH_STATIC(udp6_swcsum);
    225  1.140      yamt #endif /* defined(INET6) */
    226  1.140      yamt 
    227  1.140      yamt #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    228  1.120      matt 
    229   1.78   thorpej #else
    230   1.78   thorpej 
    231   1.78   thorpej #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
    232   1.78   thorpej 
    233   1.78   thorpej #endif /* UDP_CSUM_COUNTERS */
    234   1.78   thorpej 
    235    1.7   mycroft void
    236  1.119      matt udp_init(void)
    237    1.1       cgd {
    238   1.18   mycroft 
    239   1.35   mycroft 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
    240   1.78   thorpej 
    241   1.98      matt 	MOWNER_ATTACH(&udp_tx_mowner);
    242   1.98      matt 	MOWNER_ATTACH(&udp_rx_mowner);
    243   1.98      matt 	MOWNER_ATTACH(&udp_mowner);
    244  1.166   thorpej 
    245  1.171      yamt #ifdef INET
    246  1.166   thorpej 	udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
    247  1.171      yamt #endif
    248  1.171      yamt #ifdef INET6
    249  1.171      yamt 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
    250  1.171      yamt #endif
    251    1.1       cgd }
    252    1.1       cgd 
    253  1.129      yamt /*
    254  1.129      yamt  * Checksum extended UDP header and data.
    255  1.129      yamt  */
    256  1.129      yamt 
    257  1.129      yamt int
    258  1.129      yamt udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
    259  1.129      yamt     int iphlen, int len)
    260  1.129      yamt {
    261  1.129      yamt 
    262  1.129      yamt 	switch (af) {
    263   1.72    itojun #ifdef INET
    264  1.129      yamt 	case AF_INET:
    265  1.129      yamt 		return udp4_input_checksum(m, uh, iphlen, len);
    266  1.129      yamt #endif
    267  1.129      yamt #ifdef INET6
    268  1.129      yamt 	case AF_INET6:
    269  1.129      yamt 		return udp6_input_checksum(m, uh, iphlen, len);
    270  1.129      yamt #endif
    271  1.129      yamt 	}
    272  1.129      yamt #ifdef DIAGNOSTIC
    273  1.129      yamt 	panic("udp_input_checksum: unknown af %d", af);
    274  1.129      yamt #endif
    275  1.129      yamt 	/* NOTREACHED */
    276  1.129      yamt 	return -1;
    277  1.129      yamt }
    278  1.129      yamt 
    279  1.129      yamt #ifdef INET
    280  1.129      yamt 
    281  1.129      yamt /*
    282  1.129      yamt  * Checksum extended UDP header and data.
    283  1.129      yamt  */
    284  1.129      yamt 
    285  1.129      yamt static int
    286  1.129      yamt udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
    287  1.129      yamt     int iphlen, int len)
    288  1.129      yamt {
    289  1.129      yamt 
    290  1.129      yamt 	/*
    291  1.129      yamt 	 * XXX it's better to record and check if this mbuf is
    292  1.129      yamt 	 * already checked.
    293  1.129      yamt 	 */
    294  1.129      yamt 
    295  1.129      yamt 	if (uh->uh_sum == 0)
    296  1.129      yamt 		return 0;
    297  1.129      yamt 
    298  1.129      yamt 	switch (m->m_pkthdr.csum_flags &
    299  1.129      yamt 	    ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
    300  1.129      yamt 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    301  1.129      yamt 	case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
    302  1.129      yamt 		UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
    303  1.129      yamt 		goto badcsum;
    304  1.129      yamt 
    305  1.129      yamt 	case M_CSUM_UDPv4|M_CSUM_DATA: {
    306  1.129      yamt 		u_int32_t hw_csum = m->m_pkthdr.csum_data;
    307  1.129      yamt 
    308  1.129      yamt 		UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
    309  1.129      yamt 		if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
    310  1.129      yamt 			const struct ip *ip =
    311  1.129      yamt 			    mtod(m, const struct ip *);
    312  1.129      yamt 
    313  1.129      yamt 			hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
    314  1.129      yamt 			    ip->ip_dst.s_addr,
    315  1.129      yamt 			    htons(hw_csum + len + IPPROTO_UDP));
    316  1.129      yamt 		}
    317  1.129      yamt 		if ((hw_csum ^ 0xffff) != 0)
    318  1.129      yamt 			goto badcsum;
    319  1.129      yamt 		break;
    320  1.129      yamt 	}
    321  1.129      yamt 
    322  1.129      yamt 	case M_CSUM_UDPv4:
    323  1.129      yamt 		/* Checksum was okay. */
    324  1.129      yamt 		UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
    325  1.129      yamt 		break;
    326  1.129      yamt 
    327  1.129      yamt 	default:
    328  1.129      yamt 		/*
    329  1.129      yamt 		 * Need to compute it ourselves.  Maybe skip checksum
    330  1.129      yamt 		 * on loopback interfaces.
    331  1.129      yamt 		 */
    332  1.129      yamt 		if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
    333  1.129      yamt 				     IFF_LOOPBACK) ||
    334  1.129      yamt 				   udp_do_loopback_cksum)) {
    335  1.129      yamt 			UDP_CSUM_COUNTER_INCR(&udp_swcsum);
    336  1.129      yamt 			if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
    337  1.129      yamt 				goto badcsum;
    338  1.129      yamt 		}
    339  1.129      yamt 		break;
    340  1.129      yamt 	}
    341  1.129      yamt 
    342  1.129      yamt 	return 0;
    343  1.129      yamt 
    344  1.129      yamt badcsum:
    345  1.166   thorpej 	UDP_STATINC(UDP_STAT_BADSUM);
    346  1.129      yamt 	return -1;
    347  1.129      yamt }
    348  1.129      yamt 
    349    1.7   mycroft void
    350   1.27  christos udp_input(struct mbuf *m, ...)
    351    1.1       cgd {
    352   1.53    itojun 	va_list ap;
    353   1.53    itojun 	struct sockaddr_in src, dst;
    354   1.53    itojun 	struct ip *ip;
    355   1.53    itojun 	struct udphdr *uh;
    356   1.97    simonb 	int iphlen;
    357   1.53    itojun 	int len;
    358   1.53    itojun 	int n;
    359   1.96    itojun 	u_int16_t ip_len;
    360   1.53    itojun 
    361   1.53    itojun 	va_start(ap, m);
    362   1.53    itojun 	iphlen = va_arg(ap, int);
    363   1.97    simonb 	(void)va_arg(ap, int);		/* ignore value, advance ap */
    364   1.53    itojun 	va_end(ap);
    365   1.53    itojun 
    366   1.98      matt 	MCLAIM(m, &udp_rx_mowner);
    367  1.166   thorpej 	UDP_STATINC(UDP_STAT_IPACKETS);
    368   1.53    itojun 
    369   1.53    itojun 	/*
    370   1.53    itojun 	 * Get IP and UDP header together in first mbuf.
    371   1.53    itojun 	 */
    372   1.53    itojun 	ip = mtod(m, struct ip *);
    373   1.53    itojun 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
    374   1.53    itojun 	if (uh == NULL) {
    375  1.166   thorpej 		UDP_STATINC(UDP_STAT_HDROPS);
    376   1.53    itojun 		return;
    377   1.53    itojun 	}
    378   1.95   thorpej 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    379   1.53    itojun 
    380   1.57    itojun 	/* destination port of 0 is illegal, based on RFC768. */
    381   1.57    itojun 	if (uh->uh_dport == 0)
    382   1.57    itojun 		goto bad;
    383   1.57    itojun 
    384   1.53    itojun 	/*
    385   1.53    itojun 	 * Make mbuf data length reflect UDP length.
    386   1.53    itojun 	 * If not enough data to reflect UDP length, drop.
    387   1.53    itojun 	 */
    388   1.96    itojun 	ip_len = ntohs(ip->ip_len);
    389   1.53    itojun 	len = ntohs((u_int16_t)uh->uh_ulen);
    390   1.96    itojun 	if (ip_len != iphlen + len) {
    391   1.96    itojun 		if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
    392  1.166   thorpej 			UDP_STATINC(UDP_STAT_BADLEN);
    393   1.53    itojun 			goto bad;
    394   1.53    itojun 		}
    395   1.96    itojun 		m_adj(m, iphlen + len - ip_len);
    396   1.53    itojun 	}
    397   1.53    itojun 
    398   1.53    itojun 	/*
    399   1.53    itojun 	 * Checksum extended UDP header and data.
    400   1.53    itojun 	 */
    401  1.129      yamt 	if (udp4_input_checksum(m, uh, iphlen, len))
    402  1.129      yamt 		goto badcsum;
    403   1.53    itojun 
    404   1.53    itojun 	/* construct source and dst sockaddrs. */
    405  1.159    dyoung 	sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
    406  1.159    dyoung 	sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
    407   1.53    itojun 
    408  1.144      manu 	if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
    409  1.166   thorpej 		UDP_STATINC(UDP_STAT_HDROPS);
    410  1.144      manu 		return;
    411  1.144      manu 	}
    412   1.53    itojun #ifdef INET6
    413   1.53    itojun 	if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
    414   1.53    itojun 		struct sockaddr_in6 src6, dst6;
    415   1.53    itojun 
    416  1.175    cegger 		memset(&src6, 0, sizeof(src6));
    417   1.53    itojun 		src6.sin6_family = AF_INET6;
    418   1.53    itojun 		src6.sin6_len = sizeof(struct sockaddr_in6);
    419   1.53    itojun 		src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
    420  1.177   tsutsui 		memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
    421   1.53    itojun 			sizeof(ip->ip_src));
    422   1.53    itojun 		src6.sin6_port = uh->uh_sport;
    423  1.175    cegger 		memset(&dst6, 0, sizeof(dst6));
    424   1.53    itojun 		dst6.sin6_family = AF_INET6;
    425   1.53    itojun 		dst6.sin6_len = sizeof(struct sockaddr_in6);
    426   1.53    itojun 		dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
    427  1.177   tsutsui 		memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
    428   1.53    itojun 			sizeof(ip->ip_dst));
    429   1.53    itojun 		dst6.sin6_port = uh->uh_dport;
    430   1.53    itojun 
    431   1.53    itojun 		n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
    432   1.53    itojun 	}
    433   1.53    itojun #endif
    434   1.53    itojun 
    435   1.53    itojun 	if (n == 0) {
    436   1.53    itojun 		if (m->m_flags & (M_BCAST | M_MCAST)) {
    437  1.166   thorpej 			UDP_STATINC(UDP_STAT_NOPORTBCAST);
    438   1.53    itojun 			goto bad;
    439   1.53    itojun 		}
    440  1.166   thorpej 		UDP_STATINC(UDP_STAT_NOPORT);
    441   1.64        ws #ifdef IPKDB
    442   1.53    itojun 		if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
    443   1.53    itojun 				m, iphlen + sizeof(struct udphdr),
    444   1.53    itojun 				m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
    445   1.53    itojun 			/*
    446   1.53    itojun 			 * It was a debugger connect packet,
    447   1.53    itojun 			 * just drop it now
    448   1.53    itojun 			 */
    449   1.53    itojun 			goto bad;
    450   1.53    itojun 		}
    451   1.53    itojun #endif
    452   1.53    itojun 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
    453   1.53    itojun 		m = NULL;
    454   1.53    itojun 	}
    455   1.53    itojun 
    456   1.53    itojun bad:
    457   1.53    itojun 	if (m)
    458   1.53    itojun 		m_freem(m);
    459   1.78   thorpej 	return;
    460   1.78   thorpej 
    461   1.78   thorpej badcsum:
    462   1.78   thorpej 	m_freem(m);
    463   1.53    itojun }
    464   1.72    itojun #endif
    465   1.53    itojun 
    466   1.53    itojun #ifdef INET6
    467  1.129      yamt static int
    468  1.129      yamt udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
    469  1.129      yamt {
    470  1.129      yamt 
    471  1.140      yamt 	/*
    472  1.140      yamt 	 * XXX it's better to record and check if this mbuf is
    473  1.140      yamt 	 * already checked.
    474  1.140      yamt 	 */
    475  1.140      yamt 
    476  1.129      yamt 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
    477  1.129      yamt 		goto good;
    478  1.129      yamt 	}
    479  1.129      yamt 	if (uh->uh_sum == 0) {
    480  1.168   thorpej 		UDP6_STATINC(UDP6_STAT_NOSUM);
    481  1.129      yamt 		goto bad;
    482  1.129      yamt 	}
    483  1.140      yamt 
    484  1.140      yamt 	switch (m->m_pkthdr.csum_flags &
    485  1.140      yamt 	    ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
    486  1.140      yamt 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    487  1.140      yamt 	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
    488  1.140      yamt 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
    489  1.168   thorpej 		UDP6_STATINC(UDP6_STAT_BADSUM);
    490  1.129      yamt 		goto bad;
    491  1.140      yamt 
    492  1.140      yamt #if 0 /* notyet */
    493  1.140      yamt 	case M_CSUM_UDPv6|M_CSUM_DATA:
    494  1.140      yamt #endif
    495  1.140      yamt 
    496  1.140      yamt 	case M_CSUM_UDPv6:
    497  1.140      yamt 		/* Checksum was okay. */
    498  1.140      yamt 		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
    499  1.140      yamt 		break;
    500  1.140      yamt 
    501  1.140      yamt 	default:
    502  1.140      yamt 		/*
    503  1.140      yamt 		 * Need to compute it ourselves.  Maybe skip checksum
    504  1.140      yamt 		 * on loopback interfaces.
    505  1.140      yamt 		 */
    506  1.140      yamt 		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
    507  1.140      yamt 		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
    508  1.168   thorpej 			UDP6_STATINC(UDP6_STAT_BADSUM);
    509  1.140      yamt 			goto bad;
    510  1.140      yamt 		}
    511  1.129      yamt 	}
    512  1.129      yamt 
    513  1.129      yamt good:
    514  1.129      yamt 	return 0;
    515  1.129      yamt bad:
    516  1.129      yamt 	return -1;
    517  1.129      yamt }
    518  1.129      yamt 
    519   1.53    itojun int
    520  1.154      yamt udp6_input(struct mbuf **mp, int *offp, int proto)
    521   1.53    itojun {
    522   1.53    itojun 	struct mbuf *m = *mp;
    523   1.53    itojun 	int off = *offp;
    524   1.53    itojun 	struct sockaddr_in6 src, dst;
    525   1.53    itojun 	struct ip6_hdr *ip6;
    526   1.53    itojun 	struct udphdr *uh;
    527   1.53    itojun 	u_int32_t plen, ulen;
    528   1.53    itojun 
    529   1.76    itojun 	ip6 = mtod(m, struct ip6_hdr *);
    530   1.76    itojun 
    531   1.53    itojun #if defined(NFAITH) && 0 < NFAITH
    532   1.76    itojun 	if (faithprefix(&ip6->ip6_dst)) {
    533   1.76    itojun 		/* send icmp6 host unreach? */
    534   1.76    itojun 		m_freem(m);
    535   1.76    itojun 		return IPPROTO_DONE;
    536   1.53    itojun 	}
    537   1.53    itojun #endif
    538   1.53    itojun 
    539  1.168   thorpej 	UDP6_STATINC(UDP6_STAT_IPACKETS);
    540   1.53    itojun 
    541   1.53    itojun 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
    542   1.53    itojun 	plen = m->m_pkthdr.len - off;
    543   1.53    itojun 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    544   1.53    itojun 	if (uh == NULL) {
    545  1.167   thorpej 		IP6_STATINC(IP6_STAT_TOOSHORT);
    546   1.53    itojun 		return IPPROTO_DONE;
    547   1.53    itojun 	}
    548   1.95   thorpej 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    549   1.53    itojun 	ulen = ntohs((u_short)uh->uh_ulen);
    550   1.69    itojun 	/*
    551   1.69    itojun 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    552   1.69    itojun 	 * iff payload length > 0xffff.
    553   1.69    itojun 	 */
    554   1.53    itojun 	if (ulen == 0 && plen > 0xffff)
    555   1.53    itojun 		ulen = plen;
    556   1.53    itojun 
    557   1.53    itojun 	if (plen != ulen) {
    558  1.168   thorpej 		UDP6_STATINC(UDP6_STAT_BADLEN);
    559   1.54    itojun 		goto bad;
    560   1.54    itojun 	}
    561   1.54    itojun 
    562   1.57    itojun 	/* destination port of 0 is illegal, based on RFC768. */
    563   1.57    itojun 	if (uh->uh_dport == 0)
    564   1.57    itojun 		goto bad;
    565   1.57    itojun 
    566   1.54    itojun 	/* Be proactive about malicious use of IPv4 mapped address */
    567   1.54    itojun 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    568   1.54    itojun 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    569   1.54    itojun 		/* XXX stat */
    570   1.53    itojun 		goto bad;
    571   1.53    itojun 	}
    572   1.53    itojun 
    573   1.53    itojun 	/*
    574  1.125   thorpej 	 * Checksum extended UDP header and data.  Maybe skip checksum
    575  1.125   thorpej 	 * on loopback interfaces.
    576   1.53    itojun 	 */
    577  1.129      yamt 	if (udp6_input_checksum(m, uh, off, ulen))
    578  1.129      yamt 		goto bad;
    579   1.53    itojun 
    580   1.53    itojun 	/*
    581   1.53    itojun 	 * Construct source and dst sockaddrs.
    582   1.53    itojun 	 */
    583  1.175    cegger 	memset(&src, 0, sizeof(src));
    584   1.53    itojun 	src.sin6_family = AF_INET6;
    585   1.53    itojun 	src.sin6_len = sizeof(struct sockaddr_in6);
    586  1.146    rpaulo 	src.sin6_addr = ip6->ip6_src;
    587   1.53    itojun 	src.sin6_port = uh->uh_sport;
    588  1.175    cegger 	memset(&dst, 0, sizeof(dst));
    589   1.53    itojun 	dst.sin6_family = AF_INET6;
    590   1.53    itojun 	dst.sin6_len = sizeof(struct sockaddr_in6);
    591  1.146    rpaulo 	dst.sin6_addr = ip6->ip6_dst;
    592   1.53    itojun 	dst.sin6_port = uh->uh_dport;
    593   1.53    itojun 
    594   1.53    itojun 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
    595   1.53    itojun 		if (m->m_flags & M_MCAST) {
    596  1.168   thorpej 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
    597   1.53    itojun 			goto bad;
    598   1.53    itojun 		}
    599  1.168   thorpej 		UDP6_STATINC(UDP6_STAT_NOPORT);
    600   1.53    itojun 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    601   1.53    itojun 		m = NULL;
    602   1.53    itojun 	}
    603   1.53    itojun 
    604   1.53    itojun bad:
    605   1.53    itojun 	if (m)
    606   1.53    itojun 		m_freem(m);
    607   1.53    itojun 	return IPPROTO_DONE;
    608   1.53    itojun }
    609   1.53    itojun #endif
    610   1.53    itojun 
    611   1.72    itojun #ifdef INET
    612   1.53    itojun static void
    613  1.119      matt udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
    614  1.119      matt 	struct sockaddr *src, struct socket *so)
    615   1.53    itojun {
    616   1.53    itojun 	struct mbuf *opts = NULL;
    617   1.53    itojun 	struct mbuf *n;
    618   1.53    itojun 	struct inpcb *inp = NULL;
    619   1.53    itojun 
    620   1.53    itojun 	if (!so)
    621   1.53    itojun 		return;
    622   1.53    itojun 	switch (so->so_proto->pr_domain->dom_family) {
    623   1.53    itojun 	case AF_INET:
    624   1.53    itojun 		inp = sotoinpcb(so);
    625   1.53    itojun 		break;
    626   1.53    itojun #ifdef INET6
    627   1.53    itojun 	case AF_INET6:
    628   1.53    itojun 		break;
    629   1.53    itojun #endif
    630   1.53    itojun 	default:
    631   1.53    itojun 		return;
    632   1.53    itojun 	}
    633   1.53    itojun 
    634  1.105  jonathan #if defined(IPSEC) || defined(FAST_IPSEC)
    635   1.53    itojun 	/* check AH/ESP integrity. */
    636   1.53    itojun 	if (so != NULL && ipsec4_in_reject_so(m, so)) {
    637  1.169   thorpej 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    638  1.162    dyoung 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
    639  1.110    itojun 			icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
    640  1.110    itojun 			    0, 0);
    641   1.53    itojun 		return;
    642   1.53    itojun 	}
    643   1.53    itojun #endif /*IPSEC*/
    644   1.53    itojun 
    645  1.162    dyoung 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    646   1.53    itojun 		if (inp && (inp->inp_flags & INP_CONTROLOPTS
    647  1.174  christos #ifdef SO_OTIMESTAMP
    648  1.174  christos 			 || so->so_options & SO_OTIMESTAMP
    649  1.174  christos #endif
    650   1.53    itojun 			 || so->so_options & SO_TIMESTAMP)) {
    651   1.53    itojun 			struct ip *ip = mtod(n, struct ip *);
    652   1.53    itojun 			ip_savecontrol(inp, &opts, ip, n);
    653   1.53    itojun 		}
    654   1.53    itojun 
    655   1.53    itojun 		m_adj(n, off);
    656   1.53    itojun 		if (sbappendaddr(&so->so_rcv, src, n,
    657   1.53    itojun 				opts) == 0) {
    658   1.53    itojun 			m_freem(n);
    659   1.53    itojun 			if (opts)
    660   1.53    itojun 				m_freem(opts);
    661  1.124   darrenr 			so->so_rcv.sb_overflowed++;
    662  1.166   thorpej 			UDP_STATINC(UDP_STAT_FULLSOCK);
    663   1.53    itojun 		} else
    664   1.53    itojun 			sorwakeup(so);
    665   1.53    itojun 	}
    666   1.53    itojun }
    667   1.72    itojun #endif
    668   1.53    itojun 
    669   1.53    itojun #ifdef INET6
    670   1.53    itojun static void
    671  1.119      matt udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
    672  1.119      matt 	struct sockaddr *src, struct socket *so)
    673   1.53    itojun {
    674   1.53    itojun 	struct mbuf *opts = NULL;
    675   1.53    itojun 	struct mbuf *n;
    676   1.53    itojun 	struct in6pcb *in6p = NULL;
    677   1.53    itojun 
    678   1.53    itojun 	if (!so)
    679   1.53    itojun 		return;
    680   1.53    itojun 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
    681   1.53    itojun 		return;
    682   1.53    itojun 	in6p = sotoin6pcb(so);
    683   1.53    itojun 
    684  1.105  jonathan #if defined(IPSEC) || defined(FAST_IPSEC)
    685   1.53    itojun 	/* check AH/ESP integrity. */
    686   1.53    itojun 	if (so != NULL && ipsec6_in_reject_so(m, so)) {
    687  1.169   thorpej 		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
    688  1.162    dyoung 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
    689  1.110    itojun 			icmp6_error(n, ICMP6_DST_UNREACH,
    690  1.110    itojun 			    ICMP6_DST_UNREACH_ADMIN, 0);
    691   1.53    itojun 		return;
    692   1.53    itojun 	}
    693   1.53    itojun #endif /*IPSEC*/
    694   1.53    itojun 
    695  1.162    dyoung 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    696   1.53    itojun 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
    697  1.174  christos #ifdef SO_OTIMESTAMP
    698  1.174  christos 		    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
    699  1.174  christos #endif
    700  1.174  christos 		    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
    701   1.53    itojun 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
    702   1.53    itojun 			ip6_savecontrol(in6p, &opts, ip6, n);
    703   1.53    itojun 		}
    704   1.53    itojun 
    705   1.53    itojun 		m_adj(n, off);
    706   1.53    itojun 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    707   1.53    itojun 			m_freem(n);
    708   1.53    itojun 			if (opts)
    709   1.53    itojun 				m_freem(opts);
    710  1.124   darrenr 			so->so_rcv.sb_overflowed++;
    711  1.168   thorpej 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
    712   1.53    itojun 		} else
    713   1.53    itojun 			sorwakeup(so);
    714   1.53    itojun 	}
    715   1.53    itojun }
    716   1.53    itojun #endif
    717   1.53    itojun 
    718   1.72    itojun #ifdef INET
    719   1.53    itojun static int
    720  1.119      matt udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
    721  1.144      manu 	struct mbuf **mp, int off /* offset of udphdr */)
    722   1.53    itojun {
    723   1.53    itojun 	u_int16_t *sport, *dport;
    724   1.53    itojun 	int rcvcnt;
    725   1.53    itojun 	struct in_addr *src4, *dst4;
    726  1.109    itojun 	struct inpcb_hdr *inph;
    727   1.53    itojun 	struct inpcb *inp;
    728  1.144      manu 	struct mbuf *m = *mp;
    729   1.53    itojun 
    730   1.53    itojun 	rcvcnt = 0;
    731   1.53    itojun 	off += sizeof(struct udphdr);	/* now, offset of payload */
    732   1.53    itojun 
    733   1.53    itojun 	if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
    734   1.53    itojun 		goto bad;
    735   1.53    itojun 
    736   1.53    itojun 	src4 = &src->sin_addr;
    737   1.53    itojun 	sport = &src->sin_port;
    738   1.53    itojun 	dst4 = &dst->sin_addr;
    739   1.53    itojun 	dport = &dst->sin_port;
    740   1.53    itojun 
    741   1.73    itojun 	if (IN_MULTICAST(dst4->s_addr) ||
    742   1.53    itojun 	    in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
    743   1.53    itojun 		/*
    744   1.53    itojun 		 * Deliver a multicast or broadcast datagram to *all* sockets
    745   1.53    itojun 		 * for which the local and remote addresses and ports match
    746   1.53    itojun 		 * those of the incoming datagram.  This allows more than
    747   1.53    itojun 		 * one process to receive multi/broadcasts on the same port.
    748   1.53    itojun 		 * (This really ought to be done for unicast datagrams as
    749   1.53    itojun 		 * well, but that would cause problems with existing
    750   1.53    itojun 		 * applications that open both address-specific sockets and
    751   1.53    itojun 		 * a wildcard socket listening to the same port -- they would
    752   1.53    itojun 		 * end up receiving duplicates of every unicast datagram.
    753   1.53    itojun 		 * Those applications open the multiple sockets to overcome an
    754   1.53    itojun 		 * inadequacy of the UDP socket interface, but for backwards
    755   1.53    itojun 		 * compatibility we avoid the problem here rather than
    756   1.53    itojun 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    757   1.53    itojun 		 */
    758   1.53    itojun 
    759   1.53    itojun 		/*
    760   1.92    itojun 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    761   1.71    itojun 		 * we need udpiphdr for IPsec processing so we do that later.
    762   1.53    itojun 		 */
    763   1.53    itojun 		/*
    764   1.53    itojun 		 * Locate pcb(s) for datagram.
    765   1.53    itojun 		 */
    766  1.109    itojun 		CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
    767  1.109    itojun 			inp = (struct inpcb *)inph;
    768  1.109    itojun 			if (inp->inp_af != AF_INET)
    769  1.109    itojun 				continue;
    770  1.109    itojun 
    771   1.53    itojun 			if (inp->inp_lport != *dport)
    772   1.53    itojun 				continue;
    773   1.53    itojun 			if (!in_nullhost(inp->inp_laddr)) {
    774   1.53    itojun 				if (!in_hosteq(inp->inp_laddr, *dst4))
    775   1.53    itojun 					continue;
    776   1.53    itojun 			}
    777   1.53    itojun 			if (!in_nullhost(inp->inp_faddr)) {
    778   1.53    itojun 				if (!in_hosteq(inp->inp_faddr, *src4) ||
    779   1.53    itojun 				    inp->inp_fport != *sport)
    780   1.53    itojun 					continue;
    781   1.53    itojun 			}
    782   1.53    itojun 
    783   1.53    itojun 			udp4_sendup(m, off, (struct sockaddr *)src,
    784   1.53    itojun 				inp->inp_socket);
    785   1.53    itojun 			rcvcnt++;
    786   1.53    itojun 
    787   1.53    itojun 			/*
    788   1.53    itojun 			 * Don't look for additional matches if this one does
    789   1.53    itojun 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    790   1.53    itojun 			 * socket options set.  This heuristic avoids searching
    791   1.53    itojun 			 * through all pcbs in the common case of a non-shared
    792   1.53    itojun 			 * port.  It assumes that an application will never
    793   1.53    itojun 			 * clear these options after setting them.
    794   1.53    itojun 			 */
    795   1.53    itojun 			if ((inp->inp_socket->so_options &
    796   1.53    itojun 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    797   1.53    itojun 				break;
    798   1.53    itojun 		}
    799   1.53    itojun 	} else {
    800   1.53    itojun 		/*
    801   1.53    itojun 		 * Locate pcb for datagram.
    802   1.53    itojun 		 */
    803   1.53    itojun 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
    804   1.53    itojun 		if (inp == 0) {
    805  1.166   thorpej 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
    806   1.53    itojun 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
    807   1.82    itojun 			if (inp == 0)
    808   1.53    itojun 				return rcvcnt;
    809   1.53    itojun 		}
    810   1.53    itojun 
    811  1.130      manu #ifdef IPSEC_NAT_T
    812  1.130      manu 		/* Handle ESP over UDP */
    813  1.130      manu 		if (inp->inp_flags & INP_ESPINUDP_ALL) {
    814  1.130      manu 			struct sockaddr *sa = (struct sockaddr *)src;
    815  1.130      manu 
    816  1.144      manu 			switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) {
    817  1.144      manu 			case -1: 	/* Error, m was freeed */
    818  1.144      manu 				rcvcnt = -1;
    819  1.144      manu 				goto bad;
    820  1.144      manu 				break;
    821  1.144      manu 
    822  1.144      manu 			case 1:		/* ESP over UDP */
    823  1.130      manu 				rcvcnt++;
    824  1.130      manu 				goto bad;
    825  1.144      manu 				break;
    826  1.144      manu 
    827  1.144      manu 			case 0: 	/* plain UDP */
    828  1.144      manu 			default: 	/* Unexpected */
    829  1.144      manu 				/*
    830  1.144      manu 				 * Normal UDP processing will take place
    831  1.144      manu 				 * m may have changed.
    832  1.144      manu 				 */
    833  1.144      manu 				m = *mp;
    834  1.144      manu 				break;
    835  1.130      manu 			}
    836  1.130      manu 		}
    837  1.130      manu #endif
    838  1.130      manu 
    839  1.178   minskim 		/*
    840  1.178   minskim 		 * Check the minimum TTL for socket.
    841  1.178   minskim 		 */
    842  1.178   minskim 		if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
    843  1.178   minskim 			goto bad;
    844  1.178   minskim 
    845   1.53    itojun 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
    846   1.53    itojun 		rcvcnt++;
    847   1.53    itojun 	}
    848   1.53    itojun 
    849   1.53    itojun bad:
    850   1.53    itojun 	return rcvcnt;
    851   1.53    itojun }
    852   1.72    itojun #endif
    853   1.53    itojun 
    854   1.53    itojun #ifdef INET6
    855   1.53    itojun static int
    856  1.119      matt udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
    857  1.119      matt 	struct mbuf *m, int off)
    858   1.53    itojun {
    859   1.79    itojun 	u_int16_t sport, dport;
    860   1.53    itojun 	int rcvcnt;
    861  1.146    rpaulo 	struct in6_addr src6, *dst6;
    862   1.79    itojun 	const struct in_addr *dst4;
    863  1.109    itojun 	struct inpcb_hdr *inph;
    864   1.53    itojun 	struct in6pcb *in6p;
    865   1.53    itojun 
    866   1.53    itojun 	rcvcnt = 0;
    867   1.53    itojun 	off += sizeof(struct udphdr);	/* now, offset of payload */
    868   1.53    itojun 
    869   1.53    itojun 	if (af != AF_INET && af != AF_INET6)
    870   1.53    itojun 		goto bad;
    871   1.53    itojun 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
    872   1.53    itojun 		goto bad;
    873   1.53    itojun 
    874  1.146    rpaulo 	src6 = src->sin6_addr;
    875  1.146    rpaulo 	if (sa6_recoverscope(src) != 0) {
    876  1.146    rpaulo 		/* XXX: should be impossible. */
    877  1.146    rpaulo 		goto bad;
    878  1.146    rpaulo 	}
    879   1.79    itojun 	sport = src->sin6_port;
    880  1.146    rpaulo 
    881   1.79    itojun 	dport = dst->sin6_port;
    882   1.88    itojun 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
    883  1.146    rpaulo 	dst6 = &dst->sin6_addr;
    884   1.53    itojun 
    885  1.146    rpaulo 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
    886   1.79    itojun 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
    887   1.53    itojun 		/*
    888   1.53    itojun 		 * Deliver a multicast or broadcast datagram to *all* sockets
    889   1.53    itojun 		 * for which the local and remote addresses and ports match
    890   1.53    itojun 		 * those of the incoming datagram.  This allows more than
    891   1.53    itojun 		 * one process to receive multi/broadcasts on the same port.
    892   1.53    itojun 		 * (This really ought to be done for unicast datagrams as
    893   1.53    itojun 		 * well, but that would cause problems with existing
    894   1.53    itojun 		 * applications that open both address-specific sockets and
    895   1.53    itojun 		 * a wildcard socket listening to the same port -- they would
    896   1.53    itojun 		 * end up receiving duplicates of every unicast datagram.
    897   1.53    itojun 		 * Those applications open the multiple sockets to overcome an
    898   1.53    itojun 		 * inadequacy of the UDP socket interface, but for backwards
    899   1.53    itojun 		 * compatibility we avoid the problem here rather than
    900   1.53    itojun 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    901   1.53    itojun 		 */
    902   1.53    itojun 
    903   1.53    itojun 		/*
    904   1.92    itojun 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    905   1.71    itojun 		 * we need udpiphdr for IPsec processing so we do that later.
    906   1.53    itojun 		 */
    907   1.53    itojun 		/*
    908   1.53    itojun 		 * Locate pcb(s) for datagram.
    909   1.53    itojun 		 */
    910  1.109    itojun 		CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
    911  1.109    itojun 			in6p = (struct in6pcb *)inph;
    912  1.109    itojun 			if (in6p->in6p_af != AF_INET6)
    913  1.109    itojun 				continue;
    914  1.109    itojun 
    915   1.79    itojun 			if (in6p->in6p_lport != dport)
    916   1.53    itojun 				continue;
    917   1.53    itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    918  1.146    rpaulo 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    919  1.146    rpaulo 				    dst6))
    920   1.53    itojun 					continue;
    921   1.92    itojun 			} else {
    922  1.146    rpaulo 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
    923   1.85    itojun 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    924   1.55    itojun 					continue;
    925   1.55    itojun 			}
    926   1.53    itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    927   1.79    itojun 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    928   1.79    itojun 				    &src6) || in6p->in6p_fport != sport)
    929   1.53    itojun 					continue;
    930   1.92    itojun 			} else {
    931   1.79    itojun 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
    932   1.85    itojun 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    933   1.55    itojun 					continue;
    934   1.55    itojun 			}
    935   1.53    itojun 
    936   1.53    itojun 			udp6_sendup(m, off, (struct sockaddr *)src,
    937   1.53    itojun 				in6p->in6p_socket);
    938   1.53    itojun 			rcvcnt++;
    939   1.53    itojun 
    940   1.53    itojun 			/*
    941   1.53    itojun 			 * Don't look for additional matches if this one does
    942   1.53    itojun 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    943   1.53    itojun 			 * socket options set.  This heuristic avoids searching
    944   1.53    itojun 			 * through all pcbs in the common case of a non-shared
    945   1.53    itojun 			 * port.  It assumes that an application will never
    946   1.53    itojun 			 * clear these options after setting them.
    947   1.53    itojun 			 */
    948   1.53    itojun 			if ((in6p->in6p_socket->so_options &
    949   1.53    itojun 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    950   1.53    itojun 				break;
    951   1.53    itojun 		}
    952   1.53    itojun 	} else {
    953   1.53    itojun 		/*
    954   1.53    itojun 		 * Locate pcb for datagram.
    955   1.53    itojun 		 */
    956  1.146    rpaulo 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
    957  1.146    rpaulo 		    dport, 0);
    958   1.53    itojun 		if (in6p == 0) {
    959  1.166   thorpej 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
    960  1.146    rpaulo 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
    961   1.82    itojun 			if (in6p == 0)
    962   1.53    itojun 				return rcvcnt;
    963   1.53    itojun 		}
    964   1.53    itojun 
    965   1.53    itojun 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
    966   1.53    itojun 		rcvcnt++;
    967   1.53    itojun 	}
    968   1.53    itojun 
    969   1.53    itojun bad:
    970   1.53    itojun 	return rcvcnt;
    971   1.53    itojun }
    972   1.53    itojun #endif
    973    1.1       cgd 
    974   1.72    itojun #ifdef INET
    975    1.1       cgd /*
    976    1.1       cgd  * Notify a udp user of an asynchronous error;
    977    1.1       cgd  * just wake up so that he can collect error status.
    978    1.1       cgd  */
    979    1.7   mycroft static void
    980  1.119      matt udp_notify(struct inpcb *inp, int errno)
    981    1.1       cgd {
    982    1.1       cgd 	inp->inp_socket->so_error = errno;
    983    1.1       cgd 	sorwakeup(inp->inp_socket);
    984    1.1       cgd 	sowwakeup(inp->inp_socket);
    985    1.1       cgd }
    986    1.1       cgd 
    987   1.27  christos void *
    988  1.157    dyoung udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
    989    1.1       cgd {
    990   1.66  augustss 	struct ip *ip = v;
    991   1.66  augustss 	struct udphdr *uh;
    992  1.119      matt 	void (*notify)(struct inpcb *, int) = udp_notify;
    993   1.21   mycroft 	int errno;
    994    1.1       cgd 
    995   1.53    itojun 	if (sa->sa_family != AF_INET
    996   1.53    itojun 	 || sa->sa_len != sizeof(struct sockaddr_in))
    997   1.51    itojun 		return NULL;
    998   1.20   mycroft 	if ((unsigned)cmd >= PRC_NCMDS)
    999   1.27  christos 		return NULL;
   1000   1.20   mycroft 	errno = inetctlerrmap[cmd];
   1001   1.18   mycroft 	if (PRC_IS_REDIRECT(cmd))
   1002   1.19   mycroft 		notify = in_rtchange, ip = 0;
   1003   1.18   mycroft 	else if (cmd == PRC_HOSTDEAD)
   1004   1.19   mycroft 		ip = 0;
   1005   1.23       cgd 	else if (errno == 0)
   1006   1.27  christos 		return NULL;
   1007   1.19   mycroft 	if (ip) {
   1008  1.158  christos 		uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
   1009  1.157    dyoung 		in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
   1010   1.34   mycroft 		    ip->ip_src, uh->uh_sport, errno, notify);
   1011   1.53    itojun 
   1012   1.53    itojun 		/* XXX mapped address case */
   1013   1.19   mycroft 	} else
   1014  1.157    dyoung 		in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
   1015   1.34   mycroft 		    notify);
   1016   1.27  christos 	return NULL;
   1017    1.1       cgd }
   1018    1.1       cgd 
   1019    1.7   mycroft int
   1020  1.173    plunky udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   1021  1.130      manu {
   1022  1.130      manu 	int s;
   1023  1.130      manu 	int error = 0;
   1024  1.130      manu 	struct inpcb *inp;
   1025  1.130      manu 	int family;
   1026  1.173    plunky 	int optval;
   1027  1.130      manu 
   1028  1.130      manu 	family = so->so_proto->pr_domain->dom_family;
   1029  1.130      manu 
   1030  1.130      manu 	s = splsoftnet();
   1031  1.130      manu 	switch (family) {
   1032  1.130      manu #ifdef INET
   1033  1.130      manu 	case PF_INET:
   1034  1.173    plunky 		if (sopt->sopt_level != IPPROTO_UDP) {
   1035  1.173    plunky 			error = ip_ctloutput(op, so, sopt);
   1036  1.130      manu 			goto end;
   1037  1.130      manu 		}
   1038  1.130      manu 		break;
   1039  1.130      manu #endif
   1040  1.130      manu #ifdef INET6
   1041  1.130      manu 	case PF_INET6:
   1042  1.173    plunky 		if (sopt->sopt_level != IPPROTO_UDP) {
   1043  1.173    plunky 			error = ip6_ctloutput(op, so, sopt);
   1044  1.130      manu 			goto end;
   1045  1.130      manu 		}
   1046  1.130      manu 		break;
   1047  1.130      manu #endif
   1048  1.130      manu 	default:
   1049  1.130      manu 		error = EAFNOSUPPORT;
   1050  1.130      manu 		goto end;
   1051  1.130      manu 	}
   1052  1.130      manu 
   1053  1.130      manu 
   1054  1.130      manu 	switch (op) {
   1055  1.130      manu 	case PRCO_SETOPT:
   1056  1.130      manu 		inp = sotoinpcb(so);
   1057  1.130      manu 
   1058  1.173    plunky 		switch (sopt->sopt_name) {
   1059  1.130      manu 		case UDP_ENCAP:
   1060  1.173    plunky 			error = sockopt_getint(sopt, &optval);
   1061  1.173    plunky 			if (error)
   1062  1.153      yamt 				break;
   1063  1.131     perry 
   1064  1.173    plunky 			switch(optval) {
   1065  1.130      manu #ifdef IPSEC_NAT_T
   1066  1.130      manu 			case 0:
   1067  1.130      manu 				inp->inp_flags &= ~INP_ESPINUDP_ALL;
   1068  1.130      manu 				break;
   1069  1.130      manu 
   1070  1.130      manu 			case UDP_ENCAP_ESPINUDP:
   1071  1.130      manu 				inp->inp_flags &= ~INP_ESPINUDP_ALL;
   1072  1.130      manu 				inp->inp_flags |= INP_ESPINUDP;
   1073  1.130      manu 				break;
   1074  1.131     perry 
   1075  1.130      manu 			case UDP_ENCAP_ESPINUDP_NON_IKE:
   1076  1.130      manu 				inp->inp_flags &= ~INP_ESPINUDP_ALL;
   1077  1.130      manu 				inp->inp_flags |= INP_ESPINUDP_NON_IKE;
   1078  1.130      manu 				break;
   1079  1.130      manu #endif
   1080  1.130      manu 			default:
   1081  1.130      manu 				error = EINVAL;
   1082  1.130      manu 				break;
   1083  1.130      manu 			}
   1084  1.130      manu 			break;
   1085  1.130      manu 
   1086  1.130      manu 		default:
   1087  1.130      manu 			error = ENOPROTOOPT;
   1088  1.130      manu 			break;
   1089  1.130      manu 		}
   1090  1.130      manu 		break;
   1091  1.130      manu 
   1092  1.130      manu 	default:
   1093  1.130      manu 		error = EINVAL;
   1094  1.130      manu 		break;
   1095  1.131     perry 	}
   1096  1.131     perry 
   1097  1.130      manu end:
   1098  1.130      manu 	splx(s);
   1099  1.130      manu 	return error;
   1100  1.130      manu }
   1101  1.131     perry 
   1102  1.130      manu 
   1103  1.130      manu int
   1104   1.27  christos udp_output(struct mbuf *m, ...)
   1105   1.27  christos {
   1106   1.66  augustss 	struct inpcb *inp;
   1107   1.66  augustss 	struct udpiphdr *ui;
   1108  1.125   thorpej 	struct route *ro;
   1109   1.66  augustss 	int len = m->m_pkthdr.len;
   1110   1.31   mycroft 	int error = 0;
   1111   1.27  christos 	va_list ap;
   1112   1.27  christos 
   1113   1.98      matt 	MCLAIM(m, &udp_tx_mowner);
   1114   1.27  christos 	va_start(ap, m);
   1115   1.27  christos 	inp = va_arg(ap, struct inpcb *);
   1116   1.27  christos 	va_end(ap);
   1117    1.1       cgd 
   1118    1.1       cgd 	/*
   1119    1.1       cgd 	 * Calculate data length and get a mbuf
   1120    1.1       cgd 	 * for UDP and IP headers.
   1121    1.1       cgd 	 */
   1122   1.13   mycroft 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
   1123   1.13   mycroft 	if (m == 0) {
   1124   1.13   mycroft 		error = ENOBUFS;
   1125   1.39   thorpej 		goto release;
   1126   1.39   thorpej 	}
   1127   1.39   thorpej 
   1128   1.39   thorpej 	/*
   1129   1.39   thorpej 	 * Compute the packet length of the IP header, and
   1130   1.39   thorpej 	 * punt if the length looks bogus.
   1131   1.39   thorpej 	 */
   1132   1.96    itojun 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
   1133   1.39   thorpej 		error = EMSGSIZE;
   1134   1.13   mycroft 		goto release;
   1135   1.13   mycroft 	}
   1136    1.1       cgd 
   1137    1.1       cgd 	/*
   1138    1.1       cgd 	 * Fill in mbuf with extended UDP header
   1139    1.1       cgd 	 * and addresses and length put into network format.
   1140    1.1       cgd 	 */
   1141    1.1       cgd 	ui = mtod(m, struct udpiphdr *);
   1142    1.1       cgd 	ui->ui_pr = IPPROTO_UDP;
   1143    1.1       cgd 	ui->ui_src = inp->inp_laddr;
   1144    1.1       cgd 	ui->ui_dst = inp->inp_faddr;
   1145    1.1       cgd 	ui->ui_sport = inp->inp_lport;
   1146    1.1       cgd 	ui->ui_dport = inp->inp_fport;
   1147   1.78   thorpej 	ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
   1148    1.1       cgd 
   1149  1.125   thorpej 	ro = &inp->inp_route;
   1150  1.125   thorpej 
   1151    1.1       cgd 	/*
   1152   1.78   thorpej 	 * Set up checksum and output datagram.
   1153    1.1       cgd 	 */
   1154    1.1       cgd 	if (udpcksum) {
   1155   1.78   thorpej 		/*
   1156   1.78   thorpej 		 * XXX Cache pseudo-header checksum part for
   1157   1.78   thorpej 		 * XXX "connected" UDP sockets.
   1158   1.78   thorpej 		 */
   1159   1.78   thorpej 		ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
   1160   1.78   thorpej 		    ui->ui_dst.s_addr, htons((u_int16_t)len +
   1161   1.78   thorpej 		    sizeof(struct udphdr) + IPPROTO_UDP));
   1162  1.135      yamt 		m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
   1163   1.78   thorpej 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
   1164   1.78   thorpej 	} else
   1165   1.78   thorpej 		ui->ui_sum = 0;
   1166   1.96    itojun 	((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
   1167    1.1       cgd 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
   1168    1.1       cgd 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
   1169  1.166   thorpej 	UDP_STATINC(UDP_STAT_OPACKETS);
   1170   1.48    itojun 
   1171  1.125   thorpej 	return (ip_output(m, inp->inp_options, ro,
   1172   1.12   mycroft 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
   1173  1.107    itojun 	    inp->inp_moptions, inp->inp_socket));
   1174    1.1       cgd 
   1175    1.1       cgd release:
   1176    1.1       cgd 	m_freem(m);
   1177    1.1       cgd 	return (error);
   1178    1.1       cgd }
   1179    1.1       cgd 
   1180   1.42   thorpej int	udp_sendspace = 9216;		/* really max datagram size */
   1181   1.42   thorpej int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
   1182    1.1       cgd 					/* 40 1K datagrams */
   1183    1.1       cgd 
   1184    1.1       cgd /*ARGSUSED*/
   1185    1.7   mycroft int
   1186  1.119      matt udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
   1187  1.145  christos 	struct mbuf *control, struct lwp *l)
   1188    1.1       cgd {
   1189   1.66  augustss 	struct inpcb *inp;
   1190    1.1       cgd 	int s;
   1191   1.66  augustss 	int error = 0;
   1192    1.1       cgd 
   1193    1.1       cgd 	if (req == PRU_CONTROL)
   1194  1.158  christos 		return (in_control(so, (long)m, (void *)nam,
   1195  1.148        ad 		    (struct ifnet *)control, l));
   1196   1.59   thorpej 
   1197  1.149       tls 	s = splsoftnet();
   1198  1.149       tls 
   1199   1.60   thorpej 	if (req == PRU_PURGEIF) {
   1200  1.170        ad 		mutex_enter(softnet_lock);
   1201   1.80    itojun 		in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
   1202   1.60   thorpej 		in_purgeif((struct ifnet *)control);
   1203   1.60   thorpej 		in_pcbpurgeif(&udbtable, (struct ifnet *)control);
   1204  1.170        ad 		mutex_exit(softnet_lock);
   1205  1.149       tls 		splx(s);
   1206   1.59   thorpej 		return (0);
   1207   1.59   thorpej 	}
   1208   1.31   mycroft 
   1209   1.31   mycroft 	inp = sotoinpcb(so);
   1210   1.32   mycroft #ifdef DIAGNOSTIC
   1211   1.32   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
   1212   1.32   mycroft 		panic("udp_usrreq: unexpected control mbuf");
   1213   1.32   mycroft #endif
   1214  1.170        ad 	if (req == PRU_ATTACH) {
   1215  1.170        ad 		sosetlock(so);
   1216  1.170        ad 	} else if (inp == 0) {
   1217   1.31   mycroft 		error = EINVAL;
   1218   1.31   mycroft 		goto release;
   1219   1.31   mycroft 	}
   1220   1.31   mycroft 
   1221    1.1       cgd 	/*
   1222    1.1       cgd 	 * Note: need to block udp_input while changing
   1223    1.1       cgd 	 * the udp pcb queue and/or pcb addresses.
   1224    1.1       cgd 	 */
   1225    1.1       cgd 	switch (req) {
   1226    1.1       cgd 
   1227    1.1       cgd 	case PRU_ATTACH:
   1228   1.31   mycroft 		if (inp != 0) {
   1229   1.31   mycroft 			error = EISCONN;
   1230    1.1       cgd 			break;
   1231    1.1       cgd 		}
   1232   1.98      matt #ifdef MBUFTRACE
   1233   1.98      matt 		so->so_mowner = &udp_mowner;
   1234   1.98      matt 		so->so_rcv.sb_mowner = &udp_rx_mowner;
   1235   1.98      matt 		so->so_snd.sb_mowner = &udp_tx_mowner;
   1236   1.98      matt #endif
   1237   1.31   mycroft 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
   1238   1.31   mycroft 			error = soreserve(so, udp_sendspace, udp_recvspace);
   1239   1.31   mycroft 			if (error)
   1240   1.31   mycroft 				break;
   1241   1.31   mycroft 		}
   1242   1.18   mycroft 		error = in_pcballoc(so, &udbtable);
   1243    1.1       cgd 		if (error)
   1244    1.1       cgd 			break;
   1245   1.31   mycroft 		inp = sotoinpcb(so);
   1246   1.31   mycroft 		inp->inp_ip.ip_ttl = ip_defttl;
   1247    1.1       cgd 		break;
   1248    1.1       cgd 
   1249    1.1       cgd 	case PRU_DETACH:
   1250   1.31   mycroft 		in_pcbdetach(inp);
   1251    1.1       cgd 		break;
   1252    1.1       cgd 
   1253    1.1       cgd 	case PRU_BIND:
   1254  1.148        ad 		error = in_pcbbind(inp, nam, l);
   1255    1.1       cgd 		break;
   1256    1.1       cgd 
   1257    1.1       cgd 	case PRU_LISTEN:
   1258    1.1       cgd 		error = EOPNOTSUPP;
   1259    1.1       cgd 		break;
   1260    1.1       cgd 
   1261    1.1       cgd 	case PRU_CONNECT:
   1262  1.148        ad 		error = in_pcbconnect(inp, nam, l);
   1263   1.31   mycroft 		if (error)
   1264    1.1       cgd 			break;
   1265   1.31   mycroft 		soisconnected(so);
   1266    1.1       cgd 		break;
   1267    1.1       cgd 
   1268    1.1       cgd 	case PRU_CONNECT2:
   1269    1.1       cgd 		error = EOPNOTSUPP;
   1270    1.1       cgd 		break;
   1271    1.1       cgd 
   1272    1.1       cgd 	case PRU_DISCONNECT:
   1273   1.31   mycroft 		/*soisdisconnected(so);*/
   1274   1.31   mycroft 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
   1275    1.1       cgd 		in_pcbdisconnect(inp);
   1276   1.34   mycroft 		inp->inp_laddr = zeroin_addr;		/* XXX */
   1277   1.35   mycroft 		in_pcbstate(inp, INP_BOUND);		/* XXX */
   1278    1.1       cgd 		break;
   1279    1.1       cgd 
   1280    1.1       cgd 	case PRU_SHUTDOWN:
   1281    1.1       cgd 		socantsendmore(so);
   1282    1.1       cgd 		break;
   1283    1.1       cgd 
   1284   1.31   mycroft 	case PRU_RCVD:
   1285   1.31   mycroft 		error = EOPNOTSUPP;
   1286    1.1       cgd 		break;
   1287    1.1       cgd 
   1288   1.31   mycroft 	case PRU_SEND:
   1289   1.32   mycroft 		if (control && control->m_len) {
   1290   1.32   mycroft 			m_freem(control);
   1291   1.32   mycroft 			m_freem(m);
   1292   1.32   mycroft 			error = EINVAL;
   1293   1.32   mycroft 			break;
   1294   1.32   mycroft 		}
   1295   1.31   mycroft 	{
   1296   1.35   mycroft 		struct in_addr laddr;			/* XXX */
   1297    1.1       cgd 
   1298   1.31   mycroft 		if (nam) {
   1299   1.35   mycroft 			laddr = inp->inp_laddr;		/* XXX */
   1300   1.31   mycroft 			if ((so->so_state & SS_ISCONNECTED) != 0) {
   1301   1.31   mycroft 				error = EISCONN;
   1302   1.32   mycroft 				goto die;
   1303   1.31   mycroft 			}
   1304  1.148        ad 			error = in_pcbconnect(inp, nam, l);
   1305  1.100      matt 			if (error)
   1306  1.100      matt 				goto die;
   1307   1.31   mycroft 		} else {
   1308   1.31   mycroft 			if ((so->so_state & SS_ISCONNECTED) == 0) {
   1309   1.31   mycroft 				error = ENOTCONN;
   1310   1.32   mycroft 				goto die;
   1311   1.31   mycroft 			}
   1312   1.31   mycroft 		}
   1313   1.33   mycroft 		error = udp_output(m, inp);
   1314  1.100      matt 		m = NULL;
   1315   1.31   mycroft 		if (nam) {
   1316   1.31   mycroft 			in_pcbdisconnect(inp);
   1317   1.35   mycroft 			inp->inp_laddr = laddr;		/* XXX */
   1318   1.35   mycroft 			in_pcbstate(inp, INP_BOUND);	/* XXX */
   1319   1.31   mycroft 		}
   1320  1.100      matt 	  die:
   1321  1.100      matt 		if (m)
   1322  1.100      matt 			m_freem(m);
   1323   1.31   mycroft 	}
   1324    1.1       cgd 		break;
   1325    1.1       cgd 
   1326    1.1       cgd 	case PRU_SENSE:
   1327    1.1       cgd 		/*
   1328    1.1       cgd 		 * stat: don't bother with a blocksize.
   1329    1.1       cgd 		 */
   1330   1.31   mycroft 		splx(s);
   1331    1.1       cgd 		return (0);
   1332    1.1       cgd 
   1333   1.31   mycroft 	case PRU_RCVOOB:
   1334   1.31   mycroft 		error =  EOPNOTSUPP;
   1335   1.31   mycroft 		break;
   1336   1.31   mycroft 
   1337    1.1       cgd 	case PRU_SENDOOB:
   1338   1.32   mycroft 		m_freem(control);
   1339   1.31   mycroft 		m_freem(m);
   1340    1.1       cgd 		error =  EOPNOTSUPP;
   1341    1.1       cgd 		break;
   1342    1.1       cgd 
   1343   1.31   mycroft 	case PRU_SOCKADDR:
   1344   1.31   mycroft 		in_setsockaddr(inp, nam);
   1345   1.31   mycroft 		break;
   1346   1.31   mycroft 
   1347   1.31   mycroft 	case PRU_PEERADDR:
   1348   1.31   mycroft 		in_setpeeraddr(inp, nam);
   1349   1.31   mycroft 		break;
   1350    1.1       cgd 
   1351    1.1       cgd 	default:
   1352    1.1       cgd 		panic("udp_usrreq");
   1353    1.1       cgd 	}
   1354    1.1       cgd 
   1355    1.1       cgd release:
   1356   1.31   mycroft 	splx(s);
   1357    1.1       cgd 	return (error);
   1358   1.13   mycroft }
   1359   1.13   mycroft 
   1360  1.166   thorpej static int
   1361  1.166   thorpej sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
   1362  1.166   thorpej {
   1363  1.166   thorpej 
   1364  1.172   thorpej 	return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
   1365  1.166   thorpej }
   1366  1.166   thorpej 
   1367   1.13   mycroft /*
   1368   1.13   mycroft  * Sysctl for udp variables.
   1369   1.13   mycroft  */
   1370  1.114    atatat SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
   1371   1.13   mycroft {
   1372  1.114    atatat 
   1373  1.116    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1374  1.116    atatat 		       CTLFLAG_PERMANENT,
   1375  1.114    atatat 		       CTLTYPE_NODE, "net", NULL,
   1376  1.114    atatat 		       NULL, 0, NULL, 0,
   1377  1.114    atatat 		       CTL_NET, CTL_EOL);
   1378  1.116    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1379  1.116    atatat 		       CTLFLAG_PERMANENT,
   1380  1.114    atatat 		       CTLTYPE_NODE, "inet", NULL,
   1381  1.114    atatat 		       NULL, 0, NULL, 0,
   1382  1.114    atatat 		       CTL_NET, PF_INET, CTL_EOL);
   1383  1.116    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1384  1.116    atatat 		       CTLFLAG_PERMANENT,
   1385  1.122    atatat 		       CTLTYPE_NODE, "udp",
   1386  1.122    atatat 		       SYSCTL_DESCR("UDPv4 related settings"),
   1387  1.114    atatat 		       NULL, 0, NULL, 0,
   1388  1.114    atatat 		       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
   1389  1.114    atatat 
   1390  1.116    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1391  1.116    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1392  1.122    atatat 		       CTLTYPE_INT, "checksum",
   1393  1.123      heas 		       SYSCTL_DESCR("Compute UDP checksums"),
   1394  1.114    atatat 		       NULL, 0, &udpcksum, 0,
   1395  1.114    atatat 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
   1396  1.114    atatat 		       CTL_EOL);
   1397  1.116    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1398  1.116    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1399  1.122    atatat 		       CTLTYPE_INT, "sendspace",
   1400  1.122    atatat 		       SYSCTL_DESCR("Default UDP send buffer size"),
   1401  1.114    atatat 		       NULL, 0, &udp_sendspace, 0,
   1402  1.114    atatat 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
   1403  1.114    atatat 		       CTL_EOL);
   1404  1.116    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1405  1.116    atatat 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1406  1.122    atatat 		       CTLTYPE_INT, "recvspace",
   1407  1.122    atatat 		       SYSCTL_DESCR("Default UDP receive buffer size"),
   1408  1.114    atatat 		       NULL, 0, &udp_recvspace, 0,
   1409  1.114    atatat 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
   1410  1.114    atatat 		       CTL_EOL);
   1411  1.125   thorpej 	sysctl_createv(clog, 0, NULL, NULL,
   1412  1.125   thorpej 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1413  1.125   thorpej 		       CTLTYPE_INT, "do_loopback_cksum",
   1414  1.125   thorpej 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
   1415  1.125   thorpej 		       NULL, 0, &udp_do_loopback_cksum, 0,
   1416  1.125   thorpej 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
   1417  1.125   thorpej 		       CTL_EOL);
   1418  1.132    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1419  1.132    atatat 		       CTLFLAG_PERMANENT,
   1420  1.134    atatat 		       CTLTYPE_STRUCT, "pcblist",
   1421  1.132    atatat 		       SYSCTL_DESCR("UDP protocol control block list"),
   1422  1.132    atatat 		       sysctl_inpcblist, 0, &udbtable, 0,
   1423  1.132    atatat 		       CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
   1424  1.132    atatat 		       CTL_EOL);
   1425  1.139      elad 	sysctl_createv(clog, 0, NULL, NULL,
   1426  1.139      elad 		       CTLFLAG_PERMANENT,
   1427  1.139      elad 		       CTLTYPE_STRUCT, "stats",
   1428  1.139      elad 		       SYSCTL_DESCR("UDP statistics"),
   1429  1.166   thorpej 		       sysctl_net_inet_udp_stats, 0, NULL, 0,
   1430  1.139      elad 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
   1431  1.139      elad 		       CTL_EOL);
   1432    1.1       cgd }
   1433   1.72    itojun #endif
   1434  1.130      manu 
   1435  1.166   thorpej void
   1436  1.166   thorpej udp_statinc(u_int stat)
   1437  1.166   thorpej {
   1438  1.166   thorpej 
   1439  1.166   thorpej 	KASSERT(stat < UDP_NSTATS);
   1440  1.166   thorpej 	UDP_STATINC(stat);
   1441  1.166   thorpej }
   1442  1.166   thorpej 
   1443  1.130      manu #if (defined INET && defined IPSEC_NAT_T)
   1444  1.130      manu /*
   1445  1.130      manu  * Returns:
   1446  1.130      manu  * 1 if the packet was processed
   1447  1.130      manu  * 0 if normal UDP processing should take place
   1448  1.144      manu  * -1 if an error occurent and m was freed
   1449  1.130      manu  */
   1450  1.130      manu static int
   1451  1.154      yamt udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
   1452  1.151  christos     struct socket *so)
   1453  1.130      manu {
   1454  1.130      manu 	size_t len;
   1455  1.158  christos 	void *data;
   1456  1.130      manu 	struct inpcb *inp;
   1457  1.130      manu 	size_t skip = 0;
   1458  1.130      manu 	size_t minlen;
   1459  1.130      manu 	size_t iphdrlen;
   1460  1.130      manu 	struct ip *ip;
   1461  1.130      manu 	struct mbuf *n;
   1462  1.136      manu 	struct m_tag *tag;
   1463  1.136      manu 	struct udphdr *udphdr;
   1464  1.136      manu 	u_int16_t sport, dport;
   1465  1.144      manu 	struct mbuf *m = *mp;
   1466  1.130      manu 
   1467  1.131     perry 	/*
   1468  1.130      manu 	 * Collapse the mbuf chain if the first mbuf is too short
   1469  1.130      manu 	 * The longest case is: UDP + non ESP marker + ESP
   1470  1.130      manu 	 */
   1471  1.130      manu 	minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
   1472  1.130      manu 	if (minlen > m->m_pkthdr.len)
   1473  1.130      manu 		minlen = m->m_pkthdr.len;
   1474  1.130      manu 
   1475  1.130      manu 	if (m->m_len < minlen) {
   1476  1.144      manu 		if ((*mp = m_pullup(m, minlen)) == NULL) {
   1477  1.130      manu 			printf("udp4_espinudp: m_pullup failed\n");
   1478  1.144      manu 			return -1;
   1479  1.130      manu 		}
   1480  1.144      manu 		m = *mp;
   1481  1.130      manu 	}
   1482  1.130      manu 
   1483  1.131     perry 	len = m->m_len - off;
   1484  1.158  christos 	data = mtod(m, char *) + off;
   1485  1.130      manu 	inp = sotoinpcb(so);
   1486  1.130      manu 
   1487  1.130      manu 	/* Ignore keepalive packets */
   1488  1.158  christos 	if ((len == 1) && (*(unsigned char *)data == 0xff)) {
   1489  1.130      manu 		return 1;
   1490  1.130      manu 	}
   1491  1.130      manu 
   1492  1.131     perry 	/*
   1493  1.131     perry 	 * Check that the payload is long enough to hold
   1494  1.130      manu 	 * an ESP header and compute the length of encapsulation
   1495  1.131     perry 	 * header to remove
   1496  1.130      manu 	 */
   1497  1.130      manu 	if (inp->inp_flags & INP_ESPINUDP) {
   1498  1.130      manu 		u_int32_t *st = (u_int32_t *)data;
   1499  1.130      manu 
   1500  1.130      manu 		if ((len <= sizeof(struct esp)) || (*st == 0))
   1501  1.130      manu 			return 0; /* Normal UDP processing */
   1502  1.130      manu 
   1503  1.130      manu 		skip = sizeof(struct udphdr);
   1504  1.130      manu 	}
   1505  1.130      manu 
   1506  1.130      manu 	if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
   1507  1.142    kleink 		u_int32_t *st = (u_int32_t *)data;
   1508  1.130      manu 
   1509  1.130      manu 		if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
   1510  1.142    kleink 		    || ((st[0] | st[1]) != 0))
   1511  1.130      manu 			return 0; /* Normal UDP processing */
   1512  1.131     perry 
   1513  1.130      manu 		skip = sizeof(struct udphdr) + sizeof(u_int64_t);
   1514  1.130      manu 	}
   1515  1.130      manu 
   1516  1.130      manu 	/*
   1517  1.136      manu 	 * Get the UDP ports. They are handled in network
   1518  1.136      manu 	 * order everywhere in IPSEC_NAT_T code.
   1519  1.136      manu 	 */
   1520  1.158  christos 	udphdr = (struct udphdr *)((char *)data - skip);
   1521  1.136      manu 	sport = udphdr->uh_sport;
   1522  1.136      manu 	dport = udphdr->uh_dport;
   1523  1.136      manu 
   1524  1.136      manu 	/*
   1525  1.130      manu 	 * Remove the UDP header (and possibly the non ESP marker)
   1526  1.130      manu 	 * IP header lendth is iphdrlen
   1527  1.131     perry 	 * Before:
   1528  1.130      manu 	 *   <--- off --->
   1529  1.130      manu 	 *   +----+------+-----+
   1530  1.130      manu 	 *   | IP |  UDP | ESP |
   1531  1.130      manu 	 *   +----+------+-----+
   1532  1.130      manu 	 *        <-skip->
   1533  1.130      manu 	 * After:
   1534  1.130      manu 	 *          +----+-----+
   1535  1.130      manu 	 *          | IP | ESP |
   1536  1.130      manu 	 *          +----+-----+
   1537  1.130      manu 	 *   <-skip->
   1538  1.130      manu 	 */
   1539  1.130      manu 	iphdrlen = off - sizeof(struct udphdr);
   1540  1.158  christos 	memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
   1541  1.130      manu 	m_adj(m, skip);
   1542  1.130      manu 
   1543  1.130      manu 	ip = mtod(m, struct ip *);
   1544  1.130      manu 	ip->ip_len = htons(ntohs(ip->ip_len) - skip);
   1545  1.130      manu 	ip->ip_p = IPPROTO_ESP;
   1546  1.130      manu 
   1547  1.130      manu 	/*
   1548  1.131     perry 	 * Copy the mbuf to avoid multiple free, as both
   1549  1.131     perry 	 * esp4_input (which we call) and udp_input (which
   1550  1.130      manu 	 * called us) free the mbuf.
   1551  1.130      manu 	 */
   1552  1.130      manu 	if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
   1553  1.130      manu 		printf("udp4_espinudp: m_dup failed\n");
   1554  1.130      manu 		return 0;
   1555  1.130      manu 	}
   1556  1.130      manu 
   1557  1.136      manu 	/*
   1558  1.136      manu 	 * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
   1559  1.136      manu 	 * the source UDP port. This is required if we want
   1560  1.136      manu 	 * to select the right SPD for multiple hosts behind
   1561  1.136      manu 	 * same NAT
   1562  1.136      manu 	 */
   1563  1.137      manu 	if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
   1564  1.137      manu 	    sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
   1565  1.137      manu 		printf("udp4_espinudp: m_tag_get failed\n");
   1566  1.138      manu 		m_freem(n);
   1567  1.137      manu 		return 0;
   1568  1.137      manu 	}
   1569  1.136      manu 	((u_int16_t *)(tag + 1))[0] = sport;
   1570  1.136      manu 	((u_int16_t *)(tag + 1))[1] = dport;
   1571  1.136      manu 	m_tag_prepend(n, tag);
   1572  1.136      manu 
   1573  1.147  christos #ifdef FAST_IPSEC
   1574  1.160  degroote 	ipsec4_common_input(n, iphdrlen, IPPROTO_ESP);
   1575  1.147  christos #else
   1576  1.130      manu 	esp4_input(n, iphdrlen);
   1577  1.147  christos #endif
   1578  1.130      manu 
   1579  1.130      manu 	/* We handled it, it shoudln't be handled by UDP */
   1580  1.130      manu 	return 1;
   1581  1.130      manu }
   1582  1.130      manu #endif
   1583