Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.48
      1  1.48    itojun /*	$NetBSD: udp_usrreq.c,v 1.48 1999/07/01 08:12:52 itojun 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.48    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.48    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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     45   1.1       cgd  *    must display the following acknowledgement:
     46   1.1       cgd  *	This product includes software developed by the University of
     47   1.1       cgd  *	California, Berkeley and its contributors.
     48   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     49   1.1       cgd  *    may be used to endorse or promote products derived from this software
     50   1.1       cgd  *    without specific prior written permission.
     51   1.1       cgd  *
     52   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.1       cgd  * SUCH DAMAGE.
     63   1.1       cgd  *
     64  1.44   thorpej  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
     65   1.1       cgd  */
     66  1.38        ws #include "ipkdb.h"
     67   1.1       cgd 
     68  1.48    itojun /* XXX MAPPED_ADDR_ENABLED should be revisited */
     69  1.48    itojun 
     70   1.5   mycroft #include <sys/param.h>
     71   1.5   mycroft #include <sys/malloc.h>
     72   1.5   mycroft #include <sys/mbuf.h>
     73  1.48    itojun #ifdef MAPPED_ADDR_ENABLED
     74  1.48    itojun #include <sys/domain.h>
     75  1.48    itojun #endif /* MAPPED_ADDR_ENABLED */
     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.27  christos 
     84  1.27  christos #include <vm/vm.h>
     85  1.27  christos #include <sys/sysctl.h>
     86   1.1       cgd 
     87   1.5   mycroft #include <net/if.h>
     88   1.5   mycroft #include <net/route.h>
     89   1.1       cgd 
     90   1.5   mycroft #include <netinet/in.h>
     91   1.5   mycroft #include <netinet/in_systm.h>
     92  1.15       cgd #include <netinet/in_var.h>
     93   1.5   mycroft #include <netinet/ip.h>
     94   1.5   mycroft #include <netinet/in_pcb.h>
     95   1.5   mycroft #include <netinet/ip_var.h>
     96   1.5   mycroft #include <netinet/ip_icmp.h>
     97   1.5   mycroft #include <netinet/udp.h>
     98   1.5   mycroft #include <netinet/udp_var.h>
     99   1.1       cgd 
    100  1.27  christos #include <machine/stdarg.h>
    101  1.27  christos 
    102  1.48    itojun #ifdef IPSEC
    103  1.48    itojun #include <netinet6/ipsec.h>
    104  1.48    itojun #include <netkey/key.h>
    105  1.48    itojun #include <netkey/key_debug.h>
    106  1.48    itojun #endif /*IPSEC*/
    107  1.48    itojun 
    108   1.8   mycroft /*
    109   1.8   mycroft  * UDP protocol implementation.
    110   1.8   mycroft  * Per RFC 768, August, 1980.
    111   1.8   mycroft  */
    112   1.8   mycroft #ifndef	COMPAT_42
    113   1.8   mycroft int	udpcksum = 1;
    114   1.8   mycroft #else
    115   1.8   mycroft int	udpcksum = 0;		/* XXX */
    116   1.8   mycroft #endif
    117   1.8   mycroft 
    118  1.13   mycroft static	void udp_notify __P((struct inpcb *, int));
    119   1.7   mycroft 
    120  1.26   mycroft #ifndef UDBHASHSIZE
    121  1.26   mycroft #define	UDBHASHSIZE	128
    122  1.26   mycroft #endif
    123  1.26   mycroft int	udbhashsize = UDBHASHSIZE;
    124  1.26   mycroft 
    125   1.7   mycroft void
    126   1.1       cgd udp_init()
    127   1.1       cgd {
    128  1.18   mycroft 
    129  1.35   mycroft 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
    130   1.1       cgd }
    131   1.1       cgd 
    132   1.7   mycroft void
    133  1.27  christos #if __STDC__
    134  1.27  christos udp_input(struct mbuf *m, ...)
    135  1.27  christos #else
    136  1.27  christos udp_input(m, va_alist)
    137  1.27  christos 	struct mbuf *m;
    138  1.27  christos 	va_dcl
    139  1.27  christos #endif
    140   1.1       cgd {
    141  1.48    itojun 	int proto;
    142   1.1       cgd 	register struct ip *ip;
    143   1.1       cgd 	register struct udphdr *uh;
    144   1.1       cgd 	register struct inpcb *inp;
    145   1.1       cgd 	struct mbuf *opts = 0;
    146   1.1       cgd 	int len;
    147   1.1       cgd 	struct ip save_ip;
    148  1.27  christos 	int iphlen;
    149  1.27  christos 	va_list ap;
    150  1.34   mycroft 	struct sockaddr_in udpsrc;
    151  1.48    itojun #ifdef MAPPED_ADDR_ENABLED
    152  1.48    itojun 	struct sockaddr_in6 mapped;
    153  1.48    itojun #endif
    154  1.48    itojun 	struct sockaddr *sa;
    155  1.27  christos 
    156  1.27  christos 	va_start(ap, m);
    157  1.27  christos 	iphlen = va_arg(ap, int);
    158  1.48    itojun 	proto = va_arg(ap, int);
    159  1.27  christos 	va_end(ap);
    160   1.1       cgd 
    161   1.1       cgd 	udpstat.udps_ipackets++;
    162   1.1       cgd 
    163   1.1       cgd 	/*
    164   1.1       cgd 	 * Strip IP options, if any; should skip this,
    165   1.1       cgd 	 * make available to user, and use on returned packets,
    166   1.1       cgd 	 * but we don't yet have a way to check the checksum
    167   1.1       cgd 	 * with options still present.
    168   1.1       cgd 	 */
    169   1.1       cgd 	if (iphlen > sizeof (struct ip)) {
    170   1.1       cgd 		ip_stripoptions(m, (struct mbuf *)0);
    171   1.1       cgd 		iphlen = sizeof(struct ip);
    172   1.1       cgd 	}
    173   1.1       cgd 
    174   1.1       cgd 	/*
    175   1.1       cgd 	 * Get IP and UDP header together in first mbuf.
    176   1.1       cgd 	 */
    177   1.1       cgd 	ip = mtod(m, struct ip *);
    178   1.1       cgd 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
    179   1.1       cgd 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
    180   1.1       cgd 			udpstat.udps_hdrops++;
    181   1.1       cgd 			return;
    182   1.1       cgd 		}
    183   1.1       cgd 		ip = mtod(m, struct ip *);
    184   1.1       cgd 	}
    185   1.1       cgd 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
    186   1.1       cgd 
    187   1.1       cgd 	/*
    188   1.1       cgd 	 * Make mbuf data length reflect UDP length.
    189   1.1       cgd 	 * If not enough data to reflect UDP length, drop.
    190   1.1       cgd 	 */
    191  1.15       cgd 	len = ntohs((u_int16_t)uh->uh_ulen);
    192  1.47   mycroft 	if (ip->ip_len != iphlen + len) {
    193  1.47   mycroft 		if (ip->ip_len < iphlen + len) {
    194   1.1       cgd 			udpstat.udps_badlen++;
    195   1.1       cgd 			goto bad;
    196   1.1       cgd 		}
    197  1.47   mycroft 		m_adj(m, iphlen + len - ip->ip_len);
    198   1.1       cgd 	}
    199   1.1       cgd 	/*
    200   1.1       cgd 	 * Save a copy of the IP header in case we want restore it
    201   1.1       cgd 	 * for sending an ICMP error message in response.
    202   1.1       cgd 	 */
    203   1.1       cgd 	save_ip = *ip;
    204   1.1       cgd 
    205   1.1       cgd 	/*
    206   1.1       cgd 	 * Checksum extended UDP header and data.
    207   1.1       cgd 	 */
    208  1.29       mrg 	if (uh->uh_sum) {
    209  1.25       cgd 		bzero(((struct ipovly *)ip)->ih_x1,
    210  1.25       cgd 		    sizeof ((struct ipovly *)ip)->ih_x1);
    211   1.1       cgd 		((struct ipovly *)ip)->ih_len = uh->uh_ulen;
    212  1.46   mycroft 		if (in_cksum(m, len + sizeof (struct ip)) != 0) {
    213   1.1       cgd 			udpstat.udps_badsum++;
    214   1.1       cgd 			m_freem(m);
    215   1.1       cgd 			return;
    216   1.1       cgd 		}
    217   1.1       cgd 	}
    218  1.13   mycroft 
    219  1.48    itojun 	/*
    220  1.48    itojun 	 * Construct sockaddr format source address.
    221  1.48    itojun 	 */
    222  1.48    itojun 	udpsrc.sin_family = AF_INET;
    223  1.48    itojun 	udpsrc.sin_len = sizeof(struct sockaddr_in);
    224  1.48    itojun 	udpsrc.sin_addr = ip->ip_src;
    225  1.48    itojun 	udpsrc.sin_port = uh->uh_sport;
    226  1.48    itojun 	bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
    227  1.48    itojun 
    228  1.16   mycroft 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    229  1.13   mycroft 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
    230  1.40   thorpej 		struct inpcb *last;
    231   1.4   hpeyerl 		/*
    232   1.4   hpeyerl 		 * Deliver a multicast or broadcast datagram to *all* sockets
    233   1.4   hpeyerl 		 * for which the local and remote addresses and ports match
    234   1.4   hpeyerl 		 * those of the incoming datagram.  This allows more than
    235   1.4   hpeyerl 		 * one process to receive multi/broadcasts on the same port.
    236   1.4   hpeyerl 		 * (This really ought to be done for unicast datagrams as
    237   1.4   hpeyerl 		 * well, but that would cause problems with existing
    238   1.4   hpeyerl 		 * applications that open both address-specific sockets and
    239   1.4   hpeyerl 		 * a wildcard socket listening to the same port -- they would
    240   1.4   hpeyerl 		 * end up receiving duplicates of every unicast datagram.
    241   1.4   hpeyerl 		 * Those applications open the multiple sockets to overcome an
    242   1.4   hpeyerl 		 * inadequacy of the UDP socket interface, but for backwards
    243   1.4   hpeyerl 		 * compatibility we avoid the problem here rather than
    244  1.13   mycroft 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    245   1.4   hpeyerl 		 */
    246  1.13   mycroft 
    247  1.48    itojun 		iphlen += sizeof(struct udphdr);
    248   1.4   hpeyerl 		/*
    249  1.48    itojun 		 * KAME note: usually we drop udpiphdr from mbuf here.
    250  1.48    itojun 		 * we need udpiphdr for iPsec processing so we do that later.
    251   1.4   hpeyerl 		 */
    252   1.4   hpeyerl 		/*
    253   1.4   hpeyerl 		 * Locate pcb(s) for datagram.
    254   1.4   hpeyerl 		 * (Algorithm copied from raw_intr().)
    255   1.4   hpeyerl 		 */
    256   1.4   hpeyerl 		last = NULL;
    257  1.22       cgd 		for (inp = udbtable.inpt_queue.cqh_first;
    258  1.22       cgd 		    inp != (struct inpcb *)&udbtable.inpt_queue;
    259  1.22       cgd 		    inp = inp->inp_queue.cqe_next) {
    260   1.4   hpeyerl 			if (inp->inp_lport != uh->uh_dport)
    261   1.4   hpeyerl 				continue;
    262  1.34   mycroft 			if (!in_nullhost(inp->inp_laddr)) {
    263  1.34   mycroft 				if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
    264   1.4   hpeyerl 					continue;
    265   1.4   hpeyerl 			}
    266  1.34   mycroft 			if (!in_nullhost(inp->inp_faddr)) {
    267  1.34   mycroft 				if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
    268   1.4   hpeyerl 				    inp->inp_fport != uh->uh_sport)
    269   1.4   hpeyerl 					continue;
    270   1.4   hpeyerl 			}
    271   1.4   hpeyerl 
    272   1.4   hpeyerl 			if (last != NULL) {
    273   1.4   hpeyerl 				struct mbuf *n;
    274   1.4   hpeyerl 
    275  1.48    itojun #ifdef IPSEC
    276  1.48    itojun 				/* check AH/ESP integrity. */
    277  1.48    itojun 				if (last != NULL && ipsec4_in_reject(m, last)) {
    278  1.48    itojun 					ipsecstat.in_polvio++;
    279  1.48    itojun 					/* do not inject data to pcb */
    280  1.48    itojun 				} else
    281  1.48    itojun #endif /*IPSEC*/
    282   1.4   hpeyerl 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    283  1.48    itojun 					m_adj(m, iphlen);
    284  1.40   thorpej 					if (last->inp_flags & INP_CONTROLOPTS
    285  1.40   thorpej 					    || last->inp_socket->so_options &
    286  1.40   thorpej 					       SO_TIMESTAMP) {
    287  1.40   thorpej 						ip_savecontrol(last, &opts,
    288  1.40   thorpej 						    ip, n);
    289  1.40   thorpej 					}
    290  1.48    itojun 					sa = (struct sockaddr *)&udpsrc;
    291  1.48    itojun #ifdef MAPPED_ADDR_ENABLED
    292  1.48    itojun 					if (last->inp_socket->so_proto->pr_domain->dom_family == AF_INET6) {
    293  1.48    itojun 						in6_sin_2_v4mapsin6(&udpsrc,
    294  1.48    itojun 								    &mapped);
    295  1.48    itojun 						sa = (struct sockaddr *)&mapped;
    296  1.48    itojun 					}
    297  1.48    itojun #endif /* MAPPED_ADDR_ENABLED */
    298  1.40   thorpej 					if (sbappendaddr(
    299  1.40   thorpej 					    &last->inp_socket->so_rcv,
    300  1.48    itojun 					    sa, n, opts) == 0) {
    301   1.4   hpeyerl 						m_freem(n);
    302  1.40   thorpej 						if (opts)
    303  1.40   thorpej 							m_freem(opts);
    304  1.13   mycroft 					} else
    305  1.40   thorpej 						sorwakeup(last->inp_socket);
    306  1.40   thorpej 					opts = 0;
    307   1.4   hpeyerl 				}
    308   1.4   hpeyerl 			}
    309  1.40   thorpej 			last = inp;
    310   1.4   hpeyerl 			/*
    311  1.13   mycroft 			 * Don't look for additional matches if this one does
    312  1.13   mycroft 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    313  1.13   mycroft 			 * socket options set.  This heuristic avoids searching
    314  1.13   mycroft 			 * through all pcbs in the common case of a non-shared
    315  1.13   mycroft 			 * port.  It * assumes that an application will never
    316  1.13   mycroft 			 * clear these options after setting them.
    317   1.4   hpeyerl 			 */
    318  1.40   thorpej 			if ((last->inp_socket->so_options &
    319  1.40   thorpej 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    320   1.4   hpeyerl 				break;
    321   1.4   hpeyerl 		}
    322   1.6   mycroft 
    323   1.4   hpeyerl 		if (last == NULL) {
    324   1.4   hpeyerl 			/*
    325   1.4   hpeyerl 			 * No matching pcb found; discard datagram.
    326   1.4   hpeyerl 			 * (No need to send an ICMP Port Unreachable
    327   1.4   hpeyerl 			 * for a broadcast or multicast datgram.)
    328   1.4   hpeyerl 			 */
    329  1.48    itojun 			udpstat.udps_noport++;
    330  1.13   mycroft 			udpstat.udps_noportbcast++;
    331   1.4   hpeyerl 			goto bad;
    332   1.4   hpeyerl 		}
    333  1.48    itojun #ifdef IPSEC
    334  1.48    itojun 		/* check AH/ESP integrity. */
    335  1.48    itojun 		if (last != NULL && ipsec4_in_reject(m, last)) {
    336  1.48    itojun 			ipsecstat.in_polvio++;
    337  1.48    itojun 			goto bad;
    338  1.48    itojun 		}
    339  1.48    itojun #endif /*IPSEC*/
    340  1.40   thorpej 		if (last->inp_flags & INP_CONTROLOPTS ||
    341  1.40   thorpej 		    last->inp_socket->so_options & SO_TIMESTAMP)
    342  1.40   thorpej 			ip_savecontrol(last, &opts, ip, m);
    343  1.48    itojun 		sa = (struct sockaddr *)&udpsrc;
    344  1.48    itojun #ifdef MAPPED_ADDR_ENABLED
    345  1.48    itojun 		if (last->inp_socket->so_proto->pr_domain->dom_family ==
    346  1.48    itojun 		    AF_INET6) {
    347  1.48    itojun 			in6_sin_2_v4mapsin6(&udpsrc, &mapped);
    348  1.48    itojun 			sa = (struct sockaddr *)&mapped;
    349  1.48    itojun 		}
    350  1.48    itojun #endif /* MAPPED_ADDR_ENABLED */
    351  1.48    itojun 		if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
    352  1.13   mycroft 			udpstat.udps_fullsock++;
    353   1.4   hpeyerl 			goto bad;
    354  1.13   mycroft 		}
    355  1.40   thorpej 		sorwakeup(last->inp_socket);
    356   1.4   hpeyerl 		return;
    357   1.4   hpeyerl 	}
    358   1.1       cgd 	/*
    359   1.1       cgd 	 * Locate pcb for datagram.
    360   1.1       cgd 	 */
    361  1.35   mycroft 	inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
    362  1.26   mycroft 	    ip->ip_dst, uh->uh_dport);
    363  1.26   mycroft 	if (inp == 0) {
    364  1.26   mycroft 		++udpstat.udps_pcbhashmiss;
    365  1.35   mycroft 		inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
    366  1.18   mycroft 		if (inp == 0) {
    367  1.18   mycroft 			udpstat.udps_noport++;
    368  1.18   mycroft 			if (m->m_flags & (M_BCAST | M_MCAST)) {
    369  1.18   mycroft 				udpstat.udps_noportbcast++;
    370  1.18   mycroft 				goto bad;
    371  1.18   mycroft 			}
    372  1.18   mycroft 			*ip = save_ip;
    373  1.38        ws #if NIPKDB > 0
    374  1.38        ws 			if (checkipkdb(&ip->ip_src,
    375  1.38        ws 				       uh->uh_sport,
    376  1.38        ws 				       uh->uh_dport,
    377  1.38        ws 				       m,
    378  1.38        ws 				       iphlen + sizeof(struct udphdr),
    379  1.38        ws 				       len - sizeof(struct udphdr)))
    380  1.37        ws 			/* It was a debugger connect packet, just drop it now */
    381  1.37        ws 				goto bad;
    382  1.37        ws #endif
    383  1.18   mycroft 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
    384  1.18   mycroft 			return;
    385  1.13   mycroft 		}
    386   1.1       cgd 	}
    387  1.48    itojun #ifdef IPSEC
    388  1.48    itojun 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
    389  1.48    itojun 		ipsecstat.in_polvio++;
    390  1.48    itojun 		goto bad;
    391  1.48    itojun 	}
    392  1.48    itojun #endif /*IPSEC*/
    393   1.1       cgd 
    394   1.1       cgd 	/*
    395   1.1       cgd 	 * Stuff source address and datagram in user buffer.
    396   1.1       cgd 	 */
    397  1.40   thorpej 	if (inp->inp_flags & INP_CONTROLOPTS ||
    398  1.40   thorpej 	    inp->inp_socket->so_options & SO_TIMESTAMP)
    399  1.40   thorpej 		ip_savecontrol(inp, &opts, ip, m);
    400   1.1       cgd 	iphlen += sizeof(struct udphdr);
    401   1.1       cgd 	m->m_len -= iphlen;
    402   1.1       cgd 	m->m_pkthdr.len -= iphlen;
    403   1.1       cgd 	m->m_data += iphlen;
    404  1.48    itojun 	sa = (struct sockaddr *)&udpsrc;
    405  1.48    itojun #ifdef MAPPED_ADDR_ENABLED
    406  1.48    itojun 	if (inp->inp_socket->so_proto->pr_domain->dom_family == AF_INET6) {
    407  1.48    itojun 		in6_sin_2_v4mapsin6(&udpsrc, &mapped);
    408  1.48    itojun 		sa = (struct sockaddr *)&mapped;
    409  1.48    itojun 	}
    410  1.48    itojun #endif /* MAPPED_ADDR_ENABLED */
    411  1.48    itojun 	if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
    412   1.1       cgd 		udpstat.udps_fullsock++;
    413   1.1       cgd 		goto bad;
    414   1.1       cgd 	}
    415   1.1       cgd 	sorwakeup(inp->inp_socket);
    416   1.1       cgd 	return;
    417   1.1       cgd bad:
    418   1.1       cgd 	m_freem(m);
    419   1.1       cgd 	if (opts)
    420   1.1       cgd 		m_freem(opts);
    421   1.1       cgd }
    422   1.1       cgd 
    423   1.1       cgd /*
    424   1.1       cgd  * Notify a udp user of an asynchronous error;
    425   1.1       cgd  * just wake up so that he can collect error status.
    426   1.1       cgd  */
    427   1.7   mycroft static void
    428   1.1       cgd udp_notify(inp, errno)
    429   1.1       cgd 	register struct inpcb *inp;
    430   1.7   mycroft 	int errno;
    431   1.1       cgd {
    432  1.34   mycroft 
    433   1.1       cgd 	inp->inp_socket->so_error = errno;
    434   1.1       cgd 	sorwakeup(inp->inp_socket);
    435   1.1       cgd 	sowwakeup(inp->inp_socket);
    436   1.1       cgd }
    437   1.1       cgd 
    438  1.27  christos void *
    439  1.27  christos udp_ctlinput(cmd, sa, v)
    440   1.1       cgd 	int cmd;
    441   1.1       cgd 	struct sockaddr *sa;
    442  1.27  christos 	void *v;
    443   1.1       cgd {
    444  1.27  christos 	register struct ip *ip = v;
    445   1.1       cgd 	register struct udphdr *uh;
    446  1.21   mycroft 	extern int inetctlerrmap[];
    447  1.18   mycroft 	void (*notify) __P((struct inpcb *, int)) = udp_notify;
    448  1.21   mycroft 	int errno;
    449   1.1       cgd 
    450  1.20   mycroft 	if ((unsigned)cmd >= PRC_NCMDS)
    451  1.27  christos 		return NULL;
    452  1.20   mycroft 	errno = inetctlerrmap[cmd];
    453  1.18   mycroft 	if (PRC_IS_REDIRECT(cmd))
    454  1.19   mycroft 		notify = in_rtchange, ip = 0;
    455  1.18   mycroft 	else if (cmd == PRC_HOSTDEAD)
    456  1.19   mycroft 		ip = 0;
    457  1.23       cgd 	else if (errno == 0)
    458  1.27  christos 		return NULL;
    459  1.19   mycroft 	if (ip) {
    460   1.1       cgd 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    461  1.34   mycroft 		in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
    462  1.34   mycroft 		    ip->ip_src, uh->uh_sport, errno, notify);
    463  1.19   mycroft 	} else
    464  1.34   mycroft 		in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
    465  1.34   mycroft 		    notify);
    466  1.27  christos 	return NULL;
    467   1.1       cgd }
    468   1.1       cgd 
    469   1.7   mycroft int
    470  1.27  christos #if __STDC__
    471  1.27  christos udp_output(struct mbuf *m, ...)
    472  1.27  christos #else
    473  1.27  christos udp_output(m, va_alist)
    474  1.27  christos 	struct mbuf *m;
    475  1.27  christos 	va_dcl
    476  1.27  christos #endif
    477  1.27  christos {
    478   1.1       cgd 	register struct inpcb *inp;
    479   1.1       cgd 	register struct udpiphdr *ui;
    480   1.1       cgd 	register int len = m->m_pkthdr.len;
    481  1.31   mycroft 	int error = 0;
    482  1.27  christos 	va_list ap;
    483  1.27  christos 
    484  1.27  christos 	va_start(ap, m);
    485  1.27  christos 	inp = va_arg(ap, struct inpcb *);
    486  1.27  christos 	va_end(ap);
    487   1.1       cgd 
    488   1.1       cgd 	/*
    489   1.1       cgd 	 * Calculate data length and get a mbuf
    490   1.1       cgd 	 * for UDP and IP headers.
    491   1.1       cgd 	 */
    492  1.13   mycroft 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
    493  1.13   mycroft 	if (m == 0) {
    494  1.13   mycroft 		error = ENOBUFS;
    495  1.39   thorpej 		goto release;
    496  1.39   thorpej 	}
    497  1.39   thorpej 
    498  1.39   thorpej 	/*
    499  1.39   thorpej 	 * Compute the packet length of the IP header, and
    500  1.39   thorpej 	 * punt if the length looks bogus.
    501  1.39   thorpej 	 */
    502  1.39   thorpej 	if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
    503  1.39   thorpej 		error = EMSGSIZE;
    504  1.13   mycroft 		goto release;
    505  1.13   mycroft 	}
    506   1.1       cgd 
    507   1.1       cgd 	/*
    508   1.1       cgd 	 * Fill in mbuf with extended UDP header
    509   1.1       cgd 	 * and addresses and length put into network format.
    510   1.1       cgd 	 */
    511   1.1       cgd 	ui = mtod(m, struct udpiphdr *);
    512  1.25       cgd 	bzero(ui->ui_x1, sizeof ui->ui_x1);
    513   1.1       cgd 	ui->ui_pr = IPPROTO_UDP;
    514  1.15       cgd 	ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
    515   1.1       cgd 	ui->ui_src = inp->inp_laddr;
    516   1.1       cgd 	ui->ui_dst = inp->inp_faddr;
    517   1.1       cgd 	ui->ui_sport = inp->inp_lport;
    518   1.1       cgd 	ui->ui_dport = inp->inp_fport;
    519   1.1       cgd 	ui->ui_ulen = ui->ui_len;
    520   1.1       cgd 
    521   1.1       cgd 	/*
    522   1.1       cgd 	 * Stuff checksum and output datagram.
    523   1.1       cgd 	 */
    524   1.1       cgd 	ui->ui_sum = 0;
    525   1.1       cgd 	if (udpcksum) {
    526   1.1       cgd 	    if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
    527   1.1       cgd 		ui->ui_sum = 0xffff;
    528   1.1       cgd 	}
    529   1.1       cgd 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
    530   1.1       cgd 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
    531   1.1       cgd 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
    532   1.1       cgd 	udpstat.udps_opackets++;
    533  1.48    itojun 
    534  1.48    itojun #ifdef IPSEC
    535  1.48    itojun 	m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
    536  1.48    itojun #endif /*IPSEC*/
    537  1.48    itojun 
    538  1.31   mycroft 	return (ip_output(m, inp->inp_options, &inp->inp_route,
    539  1.12   mycroft 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
    540  1.31   mycroft 	    inp->inp_moptions));
    541   1.1       cgd 
    542   1.1       cgd release:
    543   1.1       cgd 	m_freem(m);
    544   1.1       cgd 	return (error);
    545   1.1       cgd }
    546   1.1       cgd 
    547  1.42   thorpej int	udp_sendspace = 9216;		/* really max datagram size */
    548  1.42   thorpej int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
    549   1.1       cgd 					/* 40 1K datagrams */
    550   1.1       cgd 
    551   1.1       cgd /*ARGSUSED*/
    552   1.7   mycroft int
    553  1.31   mycroft udp_usrreq(so, req, m, nam, control, p)
    554   1.1       cgd 	struct socket *so;
    555   1.1       cgd 	int req;
    556  1.31   mycroft 	struct mbuf *m, *nam, *control;
    557  1.30   mycroft 	struct proc *p;
    558   1.1       cgd {
    559  1.31   mycroft 	register struct inpcb *inp;
    560   1.1       cgd 	int s;
    561  1.31   mycroft 	register int error = 0;
    562   1.1       cgd 
    563   1.1       cgd 	if (req == PRU_CONTROL)
    564  1.31   mycroft 		return (in_control(so, (long)m, (caddr_t)nam,
    565  1.30   mycroft 		    (struct ifnet *)control, p));
    566  1.31   mycroft 
    567  1.31   mycroft 	s = splsoftnet();
    568  1.31   mycroft 	inp = sotoinpcb(so);
    569  1.32   mycroft #ifdef DIAGNOSTIC
    570  1.32   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    571  1.32   mycroft 		panic("udp_usrreq: unexpected control mbuf");
    572  1.32   mycroft #endif
    573  1.31   mycroft 	if (inp == 0 && req != PRU_ATTACH) {
    574  1.31   mycroft 		error = EINVAL;
    575  1.31   mycroft 		goto release;
    576  1.31   mycroft 	}
    577  1.31   mycroft 
    578   1.1       cgd 	/*
    579   1.1       cgd 	 * Note: need to block udp_input while changing
    580   1.1       cgd 	 * the udp pcb queue and/or pcb addresses.
    581   1.1       cgd 	 */
    582   1.1       cgd 	switch (req) {
    583   1.1       cgd 
    584   1.1       cgd 	case PRU_ATTACH:
    585  1.31   mycroft 		if (inp != 0) {
    586  1.31   mycroft 			error = EISCONN;
    587   1.1       cgd 			break;
    588   1.1       cgd 		}
    589  1.31   mycroft 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    590  1.31   mycroft 			error = soreserve(so, udp_sendspace, udp_recvspace);
    591  1.31   mycroft 			if (error)
    592  1.31   mycroft 				break;
    593  1.31   mycroft 		}
    594  1.18   mycroft 		error = in_pcballoc(so, &udbtable);
    595   1.1       cgd 		if (error)
    596   1.1       cgd 			break;
    597  1.31   mycroft 		inp = sotoinpcb(so);
    598  1.31   mycroft 		inp->inp_ip.ip_ttl = ip_defttl;
    599  1.48    itojun #ifdef IPSEC
    600  1.48    itojun 		inp = (struct inpcb *)so->so_pcb;
    601  1.48    itojun 		error = ipsec_init_policy(&inp->inp_sp);
    602  1.48    itojun #endif /*IPSEC*/
    603   1.1       cgd 		break;
    604   1.1       cgd 
    605   1.1       cgd 	case PRU_DETACH:
    606  1.31   mycroft 		in_pcbdetach(inp);
    607   1.1       cgd 		break;
    608   1.1       cgd 
    609   1.1       cgd 	case PRU_BIND:
    610  1.31   mycroft 		error = in_pcbbind(inp, nam, p);
    611   1.1       cgd 		break;
    612   1.1       cgd 
    613   1.1       cgd 	case PRU_LISTEN:
    614   1.1       cgd 		error = EOPNOTSUPP;
    615   1.1       cgd 		break;
    616   1.1       cgd 
    617   1.1       cgd 	case PRU_CONNECT:
    618  1.31   mycroft 		error = in_pcbconnect(inp, nam);
    619  1.31   mycroft 		if (error)
    620   1.1       cgd 			break;
    621  1.31   mycroft 		soisconnected(so);
    622   1.1       cgd 		break;
    623   1.1       cgd 
    624   1.1       cgd 	case PRU_CONNECT2:
    625   1.1       cgd 		error = EOPNOTSUPP;
    626   1.1       cgd 		break;
    627   1.1       cgd 
    628   1.1       cgd 	case PRU_DISCONNECT:
    629  1.31   mycroft 		/*soisdisconnected(so);*/
    630  1.31   mycroft 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    631   1.1       cgd 		in_pcbdisconnect(inp);
    632  1.34   mycroft 		inp->inp_laddr = zeroin_addr;		/* XXX */
    633  1.35   mycroft 		in_pcbstate(inp, INP_BOUND);		/* XXX */
    634   1.1       cgd 		break;
    635   1.1       cgd 
    636   1.1       cgd 	case PRU_SHUTDOWN:
    637   1.1       cgd 		socantsendmore(so);
    638   1.1       cgd 		break;
    639   1.1       cgd 
    640  1.31   mycroft 	case PRU_RCVD:
    641  1.31   mycroft 		error = EOPNOTSUPP;
    642   1.1       cgd 		break;
    643   1.1       cgd 
    644  1.31   mycroft 	case PRU_SEND:
    645  1.32   mycroft 		if (control && control->m_len) {
    646  1.32   mycroft 			m_freem(control);
    647  1.32   mycroft 			m_freem(m);
    648  1.32   mycroft 			error = EINVAL;
    649  1.32   mycroft 			break;
    650  1.32   mycroft 		}
    651  1.31   mycroft 	{
    652  1.35   mycroft 		struct in_addr laddr;			/* XXX */
    653   1.1       cgd 
    654  1.31   mycroft 		if (nam) {
    655  1.35   mycroft 			laddr = inp->inp_laddr;		/* XXX */
    656  1.31   mycroft 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    657  1.31   mycroft 				error = EISCONN;
    658  1.32   mycroft 				goto die;
    659  1.31   mycroft 			}
    660  1.31   mycroft 			error = in_pcbconnect(inp, nam);
    661  1.32   mycroft 			if (error) {
    662  1.32   mycroft 			die:
    663  1.32   mycroft 				m_freem(m);
    664  1.31   mycroft 				break;
    665  1.32   mycroft 			}
    666  1.31   mycroft 		} else {
    667  1.31   mycroft 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    668  1.31   mycroft 				error = ENOTCONN;
    669  1.32   mycroft 				goto die;
    670  1.31   mycroft 			}
    671  1.31   mycroft 		}
    672  1.33   mycroft 		error = udp_output(m, inp);
    673  1.31   mycroft 		if (nam) {
    674  1.31   mycroft 			in_pcbdisconnect(inp);
    675  1.35   mycroft 			inp->inp_laddr = laddr;		/* XXX */
    676  1.35   mycroft 			in_pcbstate(inp, INP_BOUND);	/* XXX */
    677  1.31   mycroft 		}
    678  1.31   mycroft 	}
    679   1.1       cgd 		break;
    680   1.1       cgd 
    681   1.1       cgd 	case PRU_SENSE:
    682   1.1       cgd 		/*
    683   1.1       cgd 		 * stat: don't bother with a blocksize.
    684   1.1       cgd 		 */
    685  1.31   mycroft 		splx(s);
    686   1.1       cgd 		return (0);
    687   1.1       cgd 
    688  1.31   mycroft 	case PRU_RCVOOB:
    689  1.31   mycroft 		error =  EOPNOTSUPP;
    690  1.31   mycroft 		break;
    691  1.31   mycroft 
    692   1.1       cgd 	case PRU_SENDOOB:
    693  1.32   mycroft 		m_freem(control);
    694  1.31   mycroft 		m_freem(m);
    695   1.1       cgd 		error =  EOPNOTSUPP;
    696   1.1       cgd 		break;
    697   1.1       cgd 
    698  1.31   mycroft 	case PRU_SOCKADDR:
    699  1.31   mycroft 		in_setsockaddr(inp, nam);
    700  1.31   mycroft 		break;
    701  1.31   mycroft 
    702  1.31   mycroft 	case PRU_PEERADDR:
    703  1.31   mycroft 		in_setpeeraddr(inp, nam);
    704  1.31   mycroft 		break;
    705   1.1       cgd 
    706   1.1       cgd 	default:
    707   1.1       cgd 		panic("udp_usrreq");
    708   1.1       cgd 	}
    709   1.1       cgd 
    710   1.1       cgd release:
    711  1.31   mycroft 	splx(s);
    712   1.1       cgd 	return (error);
    713  1.13   mycroft }
    714  1.13   mycroft 
    715  1.13   mycroft /*
    716  1.13   mycroft  * Sysctl for udp variables.
    717  1.13   mycroft  */
    718  1.27  christos int
    719  1.13   mycroft udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    720  1.13   mycroft 	int *name;
    721  1.13   mycroft 	u_int namelen;
    722  1.13   mycroft 	void *oldp;
    723  1.13   mycroft 	size_t *oldlenp;
    724  1.13   mycroft 	void *newp;
    725  1.13   mycroft 	size_t newlen;
    726  1.13   mycroft {
    727  1.13   mycroft 	/* All sysctl names at this level are terminal. */
    728  1.13   mycroft 	if (namelen != 1)
    729  1.13   mycroft 		return (ENOTDIR);
    730  1.13   mycroft 
    731  1.13   mycroft 	switch (name[0]) {
    732  1.13   mycroft 	case UDPCTL_CHECKSUM:
    733  1.13   mycroft 		return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
    734  1.42   thorpej 	case UDPCTL_SENDSPACE:
    735  1.42   thorpej 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    736  1.42   thorpej 		    &udp_sendspace));
    737  1.42   thorpej 	case UDPCTL_RECVSPACE:
    738  1.42   thorpej 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    739  1.42   thorpej 		    &udp_recvspace));
    740  1.13   mycroft 	default:
    741  1.13   mycroft 		return (ENOPROTOOPT);
    742  1.13   mycroft 	}
    743  1.13   mycroft 	/* NOTREACHED */
    744   1.1       cgd }
    745