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