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