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