xref: /freebsd/sys/netinet/udp_usrreq.c (revision 8a538743b5061c0ec0a023a7e827f9852ae03ecb)
1df8bae1dSRodney W. Grimes /*
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
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  *
336dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
39bdb3fa18SRobert Watson #include "opt_mac.h"
40cfa1ca9dSYoshinobu Inoue 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
4226f9a767SRodney W. Grimes #include <sys/systm.h>
43960ed29cSSeigo Tanimura #include <sys/domain.h>
44960ed29cSSeigo Tanimura #include <sys/jail.h>
45b110a8a2SGarrett Wollman #include <sys/kernel.h>
46960ed29cSSeigo Tanimura #include <sys/lock.h>
47bdb3fa18SRobert Watson #include <sys/mac.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50490d50b6SBrian Feldman #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
52960ed29cSSeigo Tanimura #include <sys/signalvar.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55960ed29cSSeigo Tanimura #include <sys/sx.h>
56b5e8ce9fSBruce Evans #include <sys/sysctl.h>
57816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
588781d8e9SBruce Evans 
5969c2d429SJeff Roberson #include <vm/uma.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <net/if.h>
62df8bae1dSRodney W. Grimes #include <net/route.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
67960ed29cSSeigo Tanimura #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip.h>
69cfa1ca9dSYoshinobu Inoue #ifdef INET6
70cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
71cfa1ca9dSYoshinobu Inoue #endif
72960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
73960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
75cfa1ca9dSYoshinobu Inoue #ifdef INET6
76cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
77cfa1ca9dSYoshinobu Inoue #endif
78df8bae1dSRodney W. Grimes #include <netinet/udp.h>
79df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
80df8bae1dSRodney W. Grimes 
81b9234fafSSam Leffler #ifdef FAST_IPSEC
82b9234fafSSam Leffler #include <netipsec/ipsec.h>
83b9234fafSSam Leffler #endif /*FAST_IPSEC*/
84b9234fafSSam Leffler 
85cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
86cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
87cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
88cfa1ca9dSYoshinobu Inoue 
89db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
90db4f9cc7SJonathan Lemon 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * UDP protocol implementation.
93df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
94df8bae1dSRodney W. Grimes  */
95df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
960312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
97df8bae1dSRodney W. Grimes #else
980312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
99df8bae1dSRodney W. Grimes #endif
1000312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
1010312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
102df8bae1dSRodney W. Grimes 
103032dcc76SLuigi Rizzo int	log_in_vain = 0;
104816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1053d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
106816a3d83SPoul-Henning Kamp 
107032dcc76SLuigi Rizzo static int	blackhole = 0;
10816f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10916f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
11016f7f31fSGeoff Rehmet 
11176429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
112cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1137a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
11415bd2b43SDavid Greenman 
11515bd2b43SDavid Greenman #ifndef UDBHASHSIZE
116c3229e05SDavid Greenman #define UDBHASHSIZE 16
11715bd2b43SDavid Greenman #endif
11815bd2b43SDavid Greenman 
11976429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
120c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1213d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
122f2ea20e6SGarrett Wollman 
1230312fbe9SPoul-Henning Kamp static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
124cfa1ca9dSYoshinobu Inoue #ifdef INET6
125cfa1ca9dSYoshinobu Inoue struct udp_in6 {
126cfa1ca9dSYoshinobu Inoue 	struct sockaddr_in6	uin6_sin;
127cfa1ca9dSYoshinobu Inoue 	u_char			uin6_init_done : 1;
128cfa1ca9dSYoshinobu Inoue } udp_in6 = {
129cfa1ca9dSYoshinobu Inoue 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
130cfa1ca9dSYoshinobu Inoue 	0
131cfa1ca9dSYoshinobu Inoue };
132cfa1ca9dSYoshinobu Inoue struct udp_ip6 {
133cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr		uip6_ip6;
134cfa1ca9dSYoshinobu Inoue 	u_char			uip6_init_done : 1;
135cfa1ca9dSYoshinobu Inoue } udp_ip6;
136cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
137df8bae1dSRodney W. Grimes 
138c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
139c1cd65baSBruce Evans 		int off);
140cfa1ca9dSYoshinobu Inoue #ifdef INET6
1414d77a549SAlfred Perlstein static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
142cfa1ca9dSYoshinobu Inoue #endif
143cfa1ca9dSYoshinobu Inoue 
1444d77a549SAlfred Perlstein static int udp_detach(struct socket *so);
1454d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1464d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes void
149032dcc76SLuigi Rizzo udp_init()
150df8bae1dSRodney W. Grimes {
151f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
15215bd2b43SDavid Greenman 	LIST_INIT(&udb);
15315bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
154ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1558781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1568781d8e9SBruce Evans 					&udbinfo.porthashmask);
15769c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
15869c2d429SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
15969c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes void
163032dcc76SLuigi Rizzo udp_input(m, off)
164032dcc76SLuigi Rizzo 	register struct mbuf *m;
165032dcc76SLuigi Rizzo 	int off;
166df8bae1dSRodney W. Grimes {
167cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
168032dcc76SLuigi Rizzo 	register struct ip *ip;
169032dcc76SLuigi Rizzo 	register struct udphdr *uh;
170032dcc76SLuigi Rizzo 	register struct inpcb *inp;
171df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
172df8bae1dSRodney W. Grimes 	int len;
173df8bae1dSRodney W. Grimes 	struct ip save_ip;
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
176df8bae1dSRodney W. Grimes 
177df8bae1dSRodney W. Grimes 	/*
178df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
179df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
180df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
181df8bae1dSRodney W. Grimes 	 * with options still present.
182df8bae1dSRodney W. Grimes 	 */
183df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
184df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
185df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
186df8bae1dSRodney W. Grimes 	}
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes 	/*
189df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
190df8bae1dSRodney W. Grimes 	 */
191df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
192df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
193df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
194df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
195df8bae1dSRodney W. Grimes 			return;
196df8bae1dSRodney W. Grimes 		}
197df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
198df8bae1dSRodney W. Grimes 	}
199df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
200df8bae1dSRodney W. Grimes 
201686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
202686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
203f76fcf6dSJeffrey Hsu 		goto badunlocked;
204686cdd19SJun-ichiro itojun Hagino 
205df8bae1dSRodney W. Grimes 	/*
206b9234fafSSam Leffler 	 * Construct sockaddr format source address.
207b9234fafSSam Leffler 	 * Stuff source address and datagram in user buffer.
208b9234fafSSam Leffler 	 */
209b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
210b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
211b9234fafSSam Leffler #ifdef INET6
212b9234fafSSam Leffler 	udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
213b9234fafSSam Leffler #endif
214b9234fafSSam Leffler 
215b9234fafSSam Leffler 	/*
216df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
217df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
218df8bae1dSRodney W. Grimes 	 */
219df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
220df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2217eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
222df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
223f76fcf6dSJeffrey Hsu 			goto badunlocked;
224df8bae1dSRodney W. Grimes 		}
225df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
226df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
227df8bae1dSRodney W. Grimes 	}
228df8bae1dSRodney W. Grimes 	/*
229df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
230df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
231df8bae1dSRodney W. Grimes 	 */
23248cb400fSRuslan Ermilov 	if (!blackhole)
233df8bae1dSRodney W. Grimes 		save_ip = *ip;
234df8bae1dSRodney W. Grimes 
235df8bae1dSRodney W. Grimes 	/*
236df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
237df8bae1dSRodney W. Grimes 	 */
2386dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
239db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
240db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
241db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
242db4f9cc7SJonathan Lemon 			else
243db4f9cc7SJonathan Lemon 	                	uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
244506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
245db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
246db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
247db4f9cc7SJonathan Lemon 		} else {
248cb342100SHajimu UMEMOTO 			char b[9];
249cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2506effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
251df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
252623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
253cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
254db4f9cc7SJonathan Lemon 		}
255623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
256df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
257df8bae1dSRodney W. Grimes 			m_freem(m);
258df8bae1dSRodney W. Grimes 			return;
259df8bae1dSRodney W. Grimes 		}
260fb9aaba0SRuslan Ermilov 	} else
261fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
262df8bae1dSRodney W. Grimes 
263f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
264f76fcf6dSJeffrey Hsu 
265df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
266df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
26782c23ebaSBill Fenner 		struct inpcb *last;
268df8bae1dSRodney W. Grimes 		/*
269df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
270df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
271df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
272df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
273df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
274df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
275df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
276df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
277df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
278df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
279df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
280df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
281df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
282df8bae1dSRodney W. Grimes 		 */
283df8bae1dSRodney W. Grimes 
284df8bae1dSRodney W. Grimes 		/*
285df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
286df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
287df8bae1dSRodney W. Grimes 		 */
288df8bae1dSRodney W. Grimes 		last = NULL;
289cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
290f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
291f76fcf6dSJeffrey Hsu 			if (inp->inp_lport != uh->uh_dport) {
292f76fcf6dSJeffrey Hsu 		docontinue:
293f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
294f76fcf6dSJeffrey Hsu 				continue;
295f76fcf6dSJeffrey Hsu 			}
296cfa1ca9dSYoshinobu Inoue #ifdef INET6
297369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
298f76fcf6dSJeffrey Hsu 				goto docontinue;
299cfa1ca9dSYoshinobu Inoue #endif
300df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
301f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
302f76fcf6dSJeffrey Hsu 					goto docontinue;
303df8bae1dSRodney W. Grimes 			}
304df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
305df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
306df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
307df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
308f76fcf6dSJeffrey Hsu 					goto docontinue;
309df8bae1dSRodney W. Grimes 			}
310df8bae1dSRodney W. Grimes 
311df8bae1dSRodney W. Grimes 			if (last != NULL) {
312df8bae1dSRodney W. Grimes 				struct mbuf *n;
313df8bae1dSRodney W. Grimes 
314032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
315365433d9SRobert Watson 				if (n != NULL)
316cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
317cfa1ca9dSYoshinobu Inoue 						   iphlen +
318cfa1ca9dSYoshinobu Inoue 						   sizeof(struct udphdr));
319f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
320df8bae1dSRodney W. Grimes 			}
32182c23ebaSBill Fenner 			last = inp;
322df8bae1dSRodney W. Grimes 			/*
323df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
324df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
325df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
326df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
327df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
328df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
329df8bae1dSRodney W. Grimes 			 */
3304cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
331df8bae1dSRodney W. Grimes 				break;
332df8bae1dSRodney W. Grimes 		}
333df8bae1dSRodney W. Grimes 
334df8bae1dSRodney W. Grimes 		if (last == NULL) {
335df8bae1dSRodney W. Grimes 			/*
336df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
337df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
338df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
339df8bae1dSRodney W. Grimes 			 */
340df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
34161ffc0b1SJeffrey Hsu 			goto badheadlocked;
342df8bae1dSRodney W. Grimes 		}
343f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
344cfa1ca9dSYoshinobu Inoue 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
3459b657230SSam Leffler 		INP_UNLOCK(last);
346df8bae1dSRodney W. Grimes 		return;
347df8bae1dSRodney W. Grimes 	}
348df8bae1dSRodney W. Grimes 	/*
3496d6a026bSDavid Greenman 	 * Locate pcb for datagram.
350df8bae1dSRodney W. Grimes 	 */
351c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
352cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
35315bd2b43SDavid Greenman 	if (inp == NULL) {
35475cfc95fSAndrey A. Chernov 		if (log_in_vain) {
355df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
35675cfc95fSAndrey A. Chernov 
35775cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
358592071e8SBruce Evans 			log(LOG_INFO,
359592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
360592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
361592071e8SBruce Evans 			    ntohs(uh->uh_sport));
36275cfc95fSAndrey A. Chernov 		}
363df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
364df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
365df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
36661ffc0b1SJeffrey Hsu 			goto badheadlocked;
367df8bae1dSRodney W. Grimes 		}
368582a7760SBruce Evans 		if (blackhole)
36961ffc0b1SJeffrey Hsu 			goto badheadlocked;
3701cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
3711cbd978eSLuigi Rizzo 			goto badheadlocked;
37204287599SRuslan Ermilov 		*ip = save_ip;
37304287599SRuslan Ermilov 		ip->ip_len += iphlen;
374582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
375f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
376df8bae1dSRodney W. Grimes 		return;
377df8bae1dSRodney W. Grimes 	}
37861ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
37961ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
380b9234fafSSam Leffler 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
381f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
382df8bae1dSRodney W. Grimes 	return;
38361ffc0b1SJeffrey Hsu 
38461ffc0b1SJeffrey Hsu badheadlocked:
38561ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
386f76fcf6dSJeffrey Hsu 	if (inp)
387f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
388f76fcf6dSJeffrey Hsu badunlocked:
389df8bae1dSRodney W. Grimes 	m_freem(m);
390df8bae1dSRodney W. Grimes 	if (opts)
391df8bae1dSRodney W. Grimes 		m_freem(opts);
392cfa1ca9dSYoshinobu Inoue 	return;
393cfa1ca9dSYoshinobu Inoue }
394cfa1ca9dSYoshinobu Inoue 
395686cdd19SJun-ichiro itojun Hagino #ifdef INET6
396cfa1ca9dSYoshinobu Inoue static void
397032dcc76SLuigi Rizzo ip_2_ip6_hdr(ip6, ip)
398032dcc76SLuigi Rizzo 	struct ip6_hdr *ip6;
399032dcc76SLuigi Rizzo 	struct ip *ip;
400cfa1ca9dSYoshinobu Inoue {
401cfa1ca9dSYoshinobu Inoue 	bzero(ip6, sizeof(*ip6));
402cfa1ca9dSYoshinobu Inoue 
403cfa1ca9dSYoshinobu Inoue 	ip6->ip6_vfc = IPV6_VERSION;
404cfa1ca9dSYoshinobu Inoue 	ip6->ip6_plen = ip->ip_len;
405cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt = ip->ip_p;
406cfa1ca9dSYoshinobu Inoue 	ip6->ip6_hlim = ip->ip_ttl;
407cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
408cfa1ca9dSYoshinobu Inoue 		IPV6_ADDR_INT32_SMP;
409cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
410cfa1ca9dSYoshinobu Inoue 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
411cfa1ca9dSYoshinobu Inoue }
412cfa1ca9dSYoshinobu Inoue #endif
413cfa1ca9dSYoshinobu Inoue 
414cfa1ca9dSYoshinobu Inoue /*
415cfa1ca9dSYoshinobu Inoue  * subroutine of udp_input(), mainly for source code readability.
416cfa1ca9dSYoshinobu Inoue  * caller must properly init udp_ip6 and udp_in6 beforehand.
417cfa1ca9dSYoshinobu Inoue  */
418cfa1ca9dSYoshinobu Inoue static void
419032dcc76SLuigi Rizzo udp_append(last, ip, n, off)
420032dcc76SLuigi Rizzo 	struct inpcb *last;
421032dcc76SLuigi Rizzo 	struct ip *ip;
422032dcc76SLuigi Rizzo 	struct mbuf *n;
423032dcc76SLuigi Rizzo 	int off;
424cfa1ca9dSYoshinobu Inoue {
425cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
426cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
427cfa1ca9dSYoshinobu Inoue 
428b9234fafSSam Leffler #ifdef IPSEC
429b9234fafSSam Leffler 	/* check AH/ESP integrity. */
430b9234fafSSam Leffler 	if (ipsec4_in_reject_so(n, last->inp_socket)) {
431b9234fafSSam Leffler 		ipsecstat.in_polvio++;
432b9234fafSSam Leffler 		m_freem(n);
433b9234fafSSam Leffler 		return;
434b9234fafSSam Leffler 	}
435b9234fafSSam Leffler #endif /*IPSEC*/
436b9234fafSSam Leffler #ifdef FAST_IPSEC
437b9234fafSSam Leffler 	/* check AH/ESP integrity. */
438b9234fafSSam Leffler 	if (ipsec4_in_reject(n, last)) {
439b9234fafSSam Leffler 		m_freem(n);
440b9234fafSSam Leffler 		return;
441b9234fafSSam Leffler 	}
442b9234fafSSam Leffler #endif /*FAST_IPSEC*/
443b9234fafSSam Leffler #ifdef MAC
444b9234fafSSam Leffler 	if (mac_check_socket_deliver(last->inp_socket, n) != 0) {
445b9234fafSSam Leffler 		m_freem(n);
446b9234fafSSam Leffler 		return;
447b9234fafSSam Leffler 	}
448b9234fafSSam Leffler #endif
449cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
450cfa1ca9dSYoshinobu Inoue 	    last->inp_socket->so_options & SO_TIMESTAMP) {
451cfa1ca9dSYoshinobu Inoue #ifdef INET6
452cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
453cfa1ca9dSYoshinobu Inoue 			int savedflags;
454cfa1ca9dSYoshinobu Inoue 
455cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
456cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
457cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
458cfa1ca9dSYoshinobu Inoue 			}
459cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
460cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
461cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
462cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
463cfa1ca9dSYoshinobu Inoue 		} else
464cfa1ca9dSYoshinobu Inoue #endif
465cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4664cc20ab1SSeigo Tanimura 	}
467cfa1ca9dSYoshinobu Inoue #ifdef INET6
468cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
469cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
470cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
471cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
472cfa1ca9dSYoshinobu Inoue 		}
473cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
474cfa1ca9dSYoshinobu Inoue 	} else
475cfa1ca9dSYoshinobu Inoue #endif
476cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
477cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
478cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
479cfa1ca9dSYoshinobu Inoue 		m_freem(n);
480cfa1ca9dSYoshinobu Inoue 		if (opts)
481cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
482cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
4834cc20ab1SSeigo Tanimura 	} else
484cfa1ca9dSYoshinobu Inoue 		sorwakeup(last->inp_socket);
485df8bae1dSRodney W. Grimes }
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes /*
488df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
489df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
490df8bae1dSRodney W. Grimes  */
4913ce144eaSJeffrey Hsu struct inpcb *
492032dcc76SLuigi Rizzo udp_notify(inp, errno)
493032dcc76SLuigi Rizzo 	register struct inpcb *inp;
494032dcc76SLuigi Rizzo 	int errno;
495df8bae1dSRodney W. Grimes {
496df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
497df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
498df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
4993ce144eaSJeffrey Hsu 	return inp;
500df8bae1dSRodney W. Grimes }
501df8bae1dSRodney W. Grimes 
502df8bae1dSRodney W. Grimes void
503032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip)
504032dcc76SLuigi Rizzo 	int cmd;
505032dcc76SLuigi Rizzo 	struct sockaddr *sa;
506032dcc76SLuigi Rizzo 	void *vip;
507df8bae1dSRodney W. Grimes {
508c693a045SJonathan Lemon 	struct ip *ip = vip;
509c693a045SJonathan Lemon 	struct udphdr *uh;
5103ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
511c693a045SJonathan Lemon         struct in_addr faddr;
512c693a045SJonathan Lemon 	struct inpcb *inp;
513c693a045SJonathan Lemon 	int s;
514c693a045SJonathan Lemon 
515c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
516c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
517c693a045SJonathan Lemon         	return;
518df8bae1dSRodney W. Grimes 
519d1c54148SJesper Skriver 	if (PRC_IS_REDIRECT(cmd)) {
520d1c54148SJesper Skriver 		ip = 0;
521d1c54148SJesper Skriver 		notify = in_rtchange;
522d1c54148SJesper Skriver 	} else if (cmd == PRC_HOSTDEAD)
523d1c54148SJesper Skriver 		ip = 0;
524d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
525df8bae1dSRodney W. Grimes 		return;
526df8bae1dSRodney W. Grimes 	if (ip) {
527c693a045SJonathan Lemon 		s = splnet();
528df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
529f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
530c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
531c693a045SJonathan Lemon                     ip->ip_src, uh->uh_sport, 0, NULL);
532f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
533f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
534f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
535c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
536f76fcf6dSJeffrey Hsu 			}
537f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
538f76fcf6dSJeffrey Hsu 		}
539f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
540c693a045SJonathan Lemon 		splx(s);
541df8bae1dSRodney W. Grimes 	} else
542f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
543df8bae1dSRodney W. Grimes }
544df8bae1dSRodney W. Grimes 
5450312fbe9SPoul-Henning Kamp static int
54682d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
54798271db4SGarrett Wollman {
54898271db4SGarrett Wollman 	int error, i, n, s;
54998271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
55098271db4SGarrett Wollman 	inp_gen_t gencnt;
55198271db4SGarrett Wollman 	struct xinpgen xig;
55298271db4SGarrett Wollman 
55398271db4SGarrett Wollman 	/*
55498271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
55598271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
55698271db4SGarrett Wollman 	 */
55798271db4SGarrett Wollman 	if (req->oldptr == 0) {
55898271db4SGarrett Wollman 		n = udbinfo.ipi_count;
55998271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
56098271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
56198271db4SGarrett Wollman 		return 0;
56298271db4SGarrett Wollman 	}
56398271db4SGarrett Wollman 
56498271db4SGarrett Wollman 	if (req->newptr != 0)
56598271db4SGarrett Wollman 		return EPERM;
56698271db4SGarrett Wollman 
56798271db4SGarrett Wollman 	/*
56898271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
56998271db4SGarrett Wollman 	 */
57098271db4SGarrett Wollman 	s = splnet();
5714b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
57298271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
57398271db4SGarrett Wollman 	n = udbinfo.ipi_count;
5744b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
57598271db4SGarrett Wollman 	splx(s);
57698271db4SGarrett Wollman 
5775c38b6dbSDon Lewis 	sysctl_wire_old_buffer(req, 2 * (sizeof xig)
5785c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
5795c38b6dbSDon Lewis 
58098271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
58198271db4SGarrett Wollman 	xig.xig_count = n;
58298271db4SGarrett Wollman 	xig.xig_gen = gencnt;
58398271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
58498271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
58598271db4SGarrett Wollman 	if (error)
58698271db4SGarrett Wollman 		return error;
58798271db4SGarrett Wollman 
588a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
58998271db4SGarrett Wollman 	if (inp_list == 0)
59098271db4SGarrett Wollman 		return ENOMEM;
59198271db4SGarrett Wollman 
59298271db4SGarrett Wollman 	s = splnet();
593f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
594fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
595fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
596f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
5972ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
5982ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
59998271db4SGarrett Wollman 			inp_list[i++] = inp;
600f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6014787fd37SPaul Saab 	}
602f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
60398271db4SGarrett Wollman 	splx(s);
60498271db4SGarrett Wollman 	n = i;
60598271db4SGarrett Wollman 
60698271db4SGarrett Wollman 	error = 0;
60798271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
60898271db4SGarrett Wollman 		inp = inp_list[i];
60998271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
61098271db4SGarrett Wollman 			struct xinpcb xi;
61198271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
61298271db4SGarrett Wollman 			/* XXX should avoid extra copy */
61398271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
61498271db4SGarrett Wollman 			if (inp->inp_socket)
61598271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6164b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
61798271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
61898271db4SGarrett Wollman 		}
61998271db4SGarrett Wollman 	}
62098271db4SGarrett Wollman 	if (!error) {
62198271db4SGarrett Wollman 		/*
62298271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
62398271db4SGarrett Wollman 		 * If the generation differs from what we told
62498271db4SGarrett Wollman 		 * her before, she knows that something happened
62598271db4SGarrett Wollman 		 * while we were processing this request, and it
62698271db4SGarrett Wollman 		 * might be necessary to retry.
62798271db4SGarrett Wollman 		 */
62898271db4SGarrett Wollman 		s = splnet();
629f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
63098271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
63198271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
63298271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
633f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
63498271db4SGarrett Wollman 		splx(s);
63598271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
63698271db4SGarrett Wollman 	}
63798271db4SGarrett Wollman 	free(inp_list, M_TEMP);
63898271db4SGarrett Wollman 	return error;
63998271db4SGarrett Wollman }
64098271db4SGarrett Wollman 
64198271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
64298271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
64398271db4SGarrett Wollman 
64498271db4SGarrett Wollman static int
64582d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
646490d50b6SBrian Feldman {
647c0511d3bSBrian Feldman 	struct xucred xuc;
648490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
649490d50b6SBrian Feldman 	struct inpcb *inp;
650490d50b6SBrian Feldman 	int error, s;
651490d50b6SBrian Feldman 
65244731cabSJohn Baldwin 	error = suser_cred(req->td->td_ucred, PRISON_ROOT);
653490d50b6SBrian Feldman 	if (error)
654490d50b6SBrian Feldman 		return (error);
655490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
656490d50b6SBrian Feldman 	if (error)
657490d50b6SBrian Feldman 		return (error);
658490d50b6SBrian Feldman 	s = splnet();
659f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
660490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
661cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6622d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
663490d50b6SBrian Feldman 		error = ENOENT;
664490d50b6SBrian Feldman 		goto out;
665490d50b6SBrian Feldman 	}
66629dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
6677ce87f12SDavid Malone 	if (error)
6687ce87f12SDavid Malone 		goto out;
66976183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
670490d50b6SBrian Feldman out:
671f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
672490d50b6SBrian Feldman 	splx(s);
6730e1eebb8SDon Lewis 	if (error == 0)
6740e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
675490d50b6SBrian Feldman 	return (error);
676490d50b6SBrian Feldman }
677490d50b6SBrian Feldman 
6787ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
6797ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
6807ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
681490d50b6SBrian Feldman 
682490d50b6SBrian Feldman static int
683032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
684032dcc76SLuigi Rizzo 	register struct inpcb *inp;
685032dcc76SLuigi Rizzo 	struct mbuf *m;
686032dcc76SLuigi Rizzo 	struct sockaddr *addr;
687032dcc76SLuigi Rizzo 	struct mbuf *control;
688032dcc76SLuigi Rizzo 	struct thread *td;
689df8bae1dSRodney W. Grimes {
690032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
691032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
69290162a4eSIan Dowse 	struct in_addr faddr, laddr;
693c557ae16SIan Dowse 	struct cmsghdr *cm;
694c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
69590162a4eSIan Dowse 	int error = 0;
6968afa2304SBruce M Simpson 	int ipflags;
69790162a4eSIan Dowse 	u_short fport, lport;
698df8bae1dSRodney W. Grimes 
699bdb3fa18SRobert Watson #ifdef MAC
700bdb3fa18SRobert Watson 	mac_create_mbuf_from_socket(inp->inp_socket, m);
701bdb3fa18SRobert Watson #endif
702bdb3fa18SRobert Watson 
703430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
704430d30d8SBill Fenner 		error = EMSGSIZE;
705c557ae16SIan Dowse 		if (control)
706c557ae16SIan Dowse 			m_freem(control);
707430d30d8SBill Fenner 		goto release;
708430d30d8SBill Fenner 	}
709430d30d8SBill Fenner 
710c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
711c557ae16SIan Dowse 	if (control != NULL) {
712c557ae16SIan Dowse 		/*
713c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
714c557ae16SIan Dowse 		 * is stored in a single mbuf.
715c557ae16SIan Dowse 		 */
716c557ae16SIan Dowse 		if (control->m_next) {
717c557ae16SIan Dowse 			error = EINVAL;
718c557ae16SIan Dowse 			m_freem(control);
719c557ae16SIan Dowse 			goto release;
720c557ae16SIan Dowse 		}
721c557ae16SIan Dowse 		for (; control->m_len > 0;
722c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
723c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
724c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
725c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
726c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
727c557ae16SIan Dowse 				error = EINVAL;
728c557ae16SIan Dowse 				break;
729c557ae16SIan Dowse 			}
730c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
731c557ae16SIan Dowse 				continue;
732c557ae16SIan Dowse 
733c557ae16SIan Dowse 			switch (cm->cmsg_type) {
734c557ae16SIan Dowse 			case IP_SENDSRCADDR:
735c557ae16SIan Dowse 				if (cm->cmsg_len !=
736c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
737c557ae16SIan Dowse 					error = EINVAL;
738c557ae16SIan Dowse 					break;
739c557ae16SIan Dowse 				}
740c557ae16SIan Dowse 				bzero(&src, sizeof(src));
741c557ae16SIan Dowse 				src.sin_family = AF_INET;
742c557ae16SIan Dowse 				src.sin_len = sizeof(src);
743c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
744c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
745c557ae16SIan Dowse 				break;
746c557ae16SIan Dowse 			default:
747c557ae16SIan Dowse 				error = ENOPROTOOPT;
748c557ae16SIan Dowse 				break;
749c557ae16SIan Dowse 			}
750c557ae16SIan Dowse 			if (error)
751c557ae16SIan Dowse 				break;
752c557ae16SIan Dowse 		}
753c557ae16SIan Dowse 		m_freem(control);
754c557ae16SIan Dowse 	}
755c557ae16SIan Dowse 	if (error)
756c557ae16SIan Dowse 		goto release;
75790162a4eSIan Dowse 	laddr = inp->inp_laddr;
75890162a4eSIan Dowse 	lport = inp->inp_lport;
759c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
760c557ae16SIan Dowse 		if (lport == 0) {
761c557ae16SIan Dowse 			error = EINVAL;
762c557ae16SIan Dowse 			goto release;
763c557ae16SIan Dowse 		}
764c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
765c557ae16SIan Dowse 		    &laddr.s_addr, &lport, td);
766c557ae16SIan Dowse 		if (error)
767c557ae16SIan Dowse 			goto release;
768c557ae16SIan Dowse 	}
769c557ae16SIan Dowse 
770df8bae1dSRodney W. Grimes 	if (addr) {
77175c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
772a854ed98SJohn Baldwin 		if (td && jailed(td->td_ucred))
773a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
774df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
775df8bae1dSRodney W. Grimes 			error = EISCONN;
776df8bae1dSRodney W. Grimes 			goto release;
777df8bae1dSRodney W. Grimes 		}
77890162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
77990162a4eSIan Dowse 		    &faddr.s_addr, &fport, NULL, td);
78090162a4eSIan Dowse 		if (error)
78190162a4eSIan Dowse 			goto release;
78290162a4eSIan Dowse 
78390162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
78490162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
78590162a4eSIan Dowse 		    inp->inp_lport == 0) {
78690162a4eSIan Dowse 			inp->inp_lport = lport;
78790162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
78890162a4eSIan Dowse 				inp->inp_lport = 0;
78990162a4eSIan Dowse 				error = EAGAIN;
790df8bae1dSRodney W. Grimes 				goto release;
791df8bae1dSRodney W. Grimes 			}
79290162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
79390162a4eSIan Dowse 		}
794df8bae1dSRodney W. Grimes 	} else {
79590162a4eSIan Dowse 		faddr = inp->inp_faddr;
79690162a4eSIan Dowse 		fport = inp->inp_fport;
79790162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
798df8bae1dSRodney W. Grimes 			error = ENOTCONN;
799df8bae1dSRodney W. Grimes 			goto release;
800df8bae1dSRodney W. Grimes 		}
801df8bae1dSRodney W. Grimes 	}
802df8bae1dSRodney W. Grimes 	/*
803df8bae1dSRodney W. Grimes 	 * Calculate data length and get a mbuf
804df8bae1dSRodney W. Grimes 	 * for UDP and IP headers.
805df8bae1dSRodney W. Grimes 	 */
806a163d034SWarner Losh 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
807df8bae1dSRodney W. Grimes 	if (m == 0) {
808df8bae1dSRodney W. Grimes 		error = ENOBUFS;
809df8bae1dSRodney W. Grimes 		goto release;
810df8bae1dSRodney W. Grimes 	}
811df8bae1dSRodney W. Grimes 
812df8bae1dSRodney W. Grimes 	/*
813df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
814df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
815df8bae1dSRodney W. Grimes 	 */
816df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
817db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
818df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
81990162a4eSIan Dowse 	ui->ui_src = laddr;
82090162a4eSIan Dowse 	ui->ui_dst = faddr;
82190162a4eSIan Dowse 	ui->ui_sport = lport;
82290162a4eSIan Dowse 	ui->ui_dport = fport;
823db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
824df8bae1dSRodney W. Grimes 
8258afa2304SBruce M Simpson 	ipflags = inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST);
8268afa2304SBruce M Simpson 	if (inp->inp_flags & INP_ONESBCAST)
8278afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
8288afa2304SBruce M Simpson 
829df8bae1dSRodney W. Grimes 	/*
830db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
831df8bae1dSRodney W. Grimes 	 */
832df8bae1dSRodney W. Grimes 	if (udpcksum) {
8338a538743SBruce M Simpson 		if (inp->inp_flags & INP_ONESBCAST)
8348a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
8358a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
836db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
837db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
838db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
839db4f9cc7SJonathan Lemon 	} else {
840db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
841df8bae1dSRodney W. Grimes 	}
842df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
843ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
844ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
845df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
846cfa1ca9dSYoshinobu Inoue 
8478afa2304SBruce M Simpson 	error = ip_output(m, inp->inp_options, &inp->inp_route, ipflags,
8485d846453SSam Leffler 	    inp->inp_moptions, inp);
849df8bae1dSRodney W. Grimes 	return (error);
850df8bae1dSRodney W. Grimes 
851df8bae1dSRodney W. Grimes release:
852df8bae1dSRodney W. Grimes 	m_freem(m);
853df8bae1dSRodney W. Grimes 	return (error);
854df8bae1dSRodney W. Grimes }
855df8bae1dSRodney W. Grimes 
85676429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
857032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
8580312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
8593d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
8600312fbe9SPoul-Henning Kamp 
861cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
862cfa1ca9dSYoshinobu Inoue #ifdef INET6
863cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
864cfa1ca9dSYoshinobu Inoue #else
865cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
866cfa1ca9dSYoshinobu Inoue #endif
867cfa1ca9dSYoshinobu Inoue 				      );
8680312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
8693d177f46SBill Fumerola     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
870df8bae1dSRodney W. Grimes 
871d0390e05SGarrett Wollman static int
872d0390e05SGarrett Wollman udp_abort(struct socket *so)
873df8bae1dSRodney W. Grimes {
874d0390e05SGarrett Wollman 	struct inpcb *inp;
875df8bae1dSRodney W. Grimes 	int s;
876df8bae1dSRodney W. Grimes 
877f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
878d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
879f76fcf6dSJeffrey Hsu 	if (inp == 0) {
880f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
881d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
882f76fcf6dSJeffrey Hsu 	}
883f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
884d0390e05SGarrett Wollman 	soisdisconnected(so);
885d0390e05SGarrett Wollman 	s = splnet();
886d0390e05SGarrett Wollman 	in_pcbdetach(inp);
887f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
888d0390e05SGarrett Wollman 	splx(s);
889d0390e05SGarrett Wollman 	return 0;
890df8bae1dSRodney W. Grimes }
891df8bae1dSRodney W. Grimes 
892d0390e05SGarrett Wollman static int
893b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
894d0390e05SGarrett Wollman {
895d0390e05SGarrett Wollman 	struct inpcb *inp;
896d0390e05SGarrett Wollman 	int s, error;
897d0390e05SGarrett Wollman 
898f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
899d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
900f76fcf6dSJeffrey Hsu 	if (inp != 0) {
901f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
902d0390e05SGarrett Wollman 		return EINVAL;
903f76fcf6dSJeffrey Hsu 	}
904cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
905f76fcf6dSJeffrey Hsu 	if (error) {
906f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
907cfa1ca9dSYoshinobu Inoue 		return error;
908f76fcf6dSJeffrey Hsu 	}
909df8bae1dSRodney W. Grimes 	s = splnet();
910b40ce416SJulian Elischer 	error = in_pcballoc(so, &udbinfo, td);
911df8bae1dSRodney W. Grimes 	splx(s);
91253b57cd1SSam Leffler 	if (error) {
91353b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
914d0390e05SGarrett Wollman 		return error;
91553b57cd1SSam Leffler 	}
916cfa1ca9dSYoshinobu Inoue 
917cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
918f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
919f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
920cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
921cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
922f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
923d0390e05SGarrett Wollman 	return 0;
924df8bae1dSRodney W. Grimes }
925d0390e05SGarrett Wollman 
926d0390e05SGarrett Wollman static int
927b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
928d0390e05SGarrett Wollman {
929d0390e05SGarrett Wollman 	struct inpcb *inp;
930d0390e05SGarrett Wollman 	int s, error;
931d0390e05SGarrett Wollman 
932f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
933d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
934f76fcf6dSJeffrey Hsu 	if (inp == 0) {
935f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
936d0390e05SGarrett Wollman 		return EINVAL;
937f76fcf6dSJeffrey Hsu 	}
938f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
939df8bae1dSRodney W. Grimes 	s = splnet();
940b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
941d0390e05SGarrett Wollman 	splx(s);
942f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
943f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
944d0390e05SGarrett Wollman 	return error;
945d0390e05SGarrett Wollman }
946d0390e05SGarrett Wollman 
947d0390e05SGarrett Wollman static int
948b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
949d0390e05SGarrett Wollman {
950d0390e05SGarrett Wollman 	struct inpcb *inp;
951d0390e05SGarrett Wollman 	int s, error;
95275c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
953d0390e05SGarrett Wollman 
954f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
955d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
956f76fcf6dSJeffrey Hsu 	if (inp == 0) {
957f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
958d0390e05SGarrett Wollman 		return EINVAL;
959f76fcf6dSJeffrey Hsu 	}
960f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
961f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
962f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
963f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
964d0390e05SGarrett Wollman 		return EISCONN;
965f76fcf6dSJeffrey Hsu 	}
966d0390e05SGarrett Wollman 	s = splnet();
96775c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
968a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
969a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
970b40ce416SJulian Elischer 	error = in_pcbconnect(inp, nam, td);
971df8bae1dSRodney W. Grimes 	splx(s);
9724cc20ab1SSeigo Tanimura 	if (error == 0)
973df8bae1dSRodney W. Grimes 		soisconnected(so);
974f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
975f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
976d0390e05SGarrett Wollman 	return error;
977df8bae1dSRodney W. Grimes }
978d0390e05SGarrett Wollman 
979d0390e05SGarrett Wollman static int
980d0390e05SGarrett Wollman udp_detach(struct socket *so)
981d0390e05SGarrett Wollman {
982d0390e05SGarrett Wollman 	struct inpcb *inp;
983d0390e05SGarrett Wollman 	int s;
984d0390e05SGarrett Wollman 
985f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
986d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
987f76fcf6dSJeffrey Hsu 	if (inp == 0) {
988f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
989d0390e05SGarrett Wollman 		return EINVAL;
990f76fcf6dSJeffrey Hsu 	}
991f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
992d0390e05SGarrett Wollman 	s = splnet();
993d0390e05SGarrett Wollman 	in_pcbdetach(inp);
994f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
995d0390e05SGarrett Wollman 	splx(s);
996d0390e05SGarrett Wollman 	return 0;
997d0390e05SGarrett Wollman }
998d0390e05SGarrett Wollman 
999d0390e05SGarrett Wollman static int
1000d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1001d0390e05SGarrett Wollman {
1002d0390e05SGarrett Wollman 	struct inpcb *inp;
1003d0390e05SGarrett Wollman 	int s;
1004d0390e05SGarrett Wollman 
1005f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1006d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1007f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1008f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1009d0390e05SGarrett Wollman 		return EINVAL;
1010f76fcf6dSJeffrey Hsu 	}
1011f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1012f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1013f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1014f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1015d0390e05SGarrett Wollman 		return ENOTCONN;
1016f76fcf6dSJeffrey Hsu 	}
1017d0390e05SGarrett Wollman 
1018df8bae1dSRodney W. Grimes 	s = splnet();
1019df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1020df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1021f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1022f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1023df8bae1dSRodney W. Grimes 	splx(s);
1024df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1025d0390e05SGarrett Wollman 	return 0;
1026df8bae1dSRodney W. Grimes }
1027df8bae1dSRodney W. Grimes 
1028d0390e05SGarrett Wollman static int
102957bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1030b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1031d0390e05SGarrett Wollman {
1032d0390e05SGarrett Wollman 	struct inpcb *inp;
1033f76fcf6dSJeffrey Hsu 	int ret;
1034d0390e05SGarrett Wollman 
1035f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1036d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1037d0390e05SGarrett Wollman 	if (inp == 0) {
1038f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1039d0390e05SGarrett Wollman 		m_freem(m);
1040d0390e05SGarrett Wollman 		return EINVAL;
1041d0390e05SGarrett Wollman 	}
1042f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1043f76fcf6dSJeffrey Hsu 	ret = udp_output(inp, m, addr, control, td);
1044f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1045f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1046f76fcf6dSJeffrey Hsu 	return ret;
1047d0390e05SGarrett Wollman }
1048d0390e05SGarrett Wollman 
104976429de4SYoshinobu Inoue int
1050d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1051d0390e05SGarrett Wollman {
1052d0390e05SGarrett Wollman 	struct inpcb *inp;
1053d0390e05SGarrett Wollman 
1054f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1055d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1056f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1057f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1058d0390e05SGarrett Wollman 		return EINVAL;
1059f76fcf6dSJeffrey Hsu 	}
1060f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1061f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1062d0390e05SGarrett Wollman 	socantsendmore(so);
1063f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1064d0390e05SGarrett Wollman 	return 0;
1065d0390e05SGarrett Wollman }
1066d0390e05SGarrett Wollman 
1067f76fcf6dSJeffrey Hsu /*
1068f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1069f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1070f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1071f76fcf6dSJeffrey Hsu  */
1072f76fcf6dSJeffrey Hsu static int
1073f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1074f76fcf6dSJeffrey Hsu {
1075f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1076f76fcf6dSJeffrey Hsu }
1077f76fcf6dSJeffrey Hsu 
1078f76fcf6dSJeffrey Hsu /*
1079f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1080f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1081f76fcf6dSJeffrey Hsu  */
1082f76fcf6dSJeffrey Hsu static int
1083f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1084f76fcf6dSJeffrey Hsu {
1085f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1086f76fcf6dSJeffrey Hsu }
1087f76fcf6dSJeffrey Hsu 
1088d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1089117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1090117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1091f76fcf6dSJeffrey Hsu 	pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1092117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1093f76fcf6dSJeffrey Hsu 	udp_sockaddr, sosend, soreceive, sopoll
1094d0390e05SGarrett Wollman };
1095