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