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