xref: /freebsd/sys/netinet/udp_usrreq.c (revision f76fcf6d4c205c6bc580eae0aca8dc4a335f6a8a)
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"
39cfa1ca9dSYoshinobu Inoue 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
4126f9a767SRodney W. Grimes #include <sys/systm.h>
42960ed29cSSeigo Tanimura #include <sys/domain.h>
43960ed29cSSeigo Tanimura #include <sys/jail.h>
44b110a8a2SGarrett Wollman #include <sys/kernel.h>
45960ed29cSSeigo Tanimura #include <sys/lock.h>
46df8bae1dSRodney W. Grimes #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48490d50b6SBrian Feldman #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50960ed29cSSeigo Tanimura #include <sys/signalvar.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53960ed29cSSeigo Tanimura #include <sys/sx.h>
54b5e8ce9fSBruce Evans #include <sys/sysctl.h>
55816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
568781d8e9SBruce Evans 
5769c2d429SJeff Roberson #include <vm/uma.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
65960ed29cSSeigo Tanimura #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip.h>
67cfa1ca9dSYoshinobu Inoue #ifdef INET6
68cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
69cfa1ca9dSYoshinobu Inoue #endif
70960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
71960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73cfa1ca9dSYoshinobu Inoue #ifdef INET6
74cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
75cfa1ca9dSYoshinobu Inoue #endif
76df8bae1dSRodney W. Grimes #include <netinet/udp.h>
77df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
78df8bae1dSRodney W. Grimes 
79cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
80cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
81cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
82cfa1ca9dSYoshinobu Inoue 
83db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
84db4f9cc7SJonathan Lemon 
85df8bae1dSRodney W. Grimes /*
86df8bae1dSRodney W. Grimes  * UDP protocol implementation.
87df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
88df8bae1dSRodney W. Grimes  */
89df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
900312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
91df8bae1dSRodney W. Grimes #else
920312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
93df8bae1dSRodney W. Grimes #endif
940312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
950312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
96df8bae1dSRodney W. Grimes 
9776429de4SYoshinobu Inoue int	log_in_vain = 0;
98816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
993d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
100816a3d83SPoul-Henning Kamp 
10116f7f31fSGeoff Rehmet static int	blackhole = 0;
10216f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10316f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
10416f7f31fSGeoff Rehmet 
10576429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
106cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1077a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
10815bd2b43SDavid Greenman 
10915bd2b43SDavid Greenman #ifndef UDBHASHSIZE
110c3229e05SDavid Greenman #define UDBHASHSIZE 16
11115bd2b43SDavid Greenman #endif
11215bd2b43SDavid Greenman 
11376429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
114c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1153d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
116f2ea20e6SGarrett Wollman 
1170312fbe9SPoul-Henning Kamp static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
118cfa1ca9dSYoshinobu Inoue #ifdef INET6
119cfa1ca9dSYoshinobu Inoue struct udp_in6 {
120cfa1ca9dSYoshinobu Inoue 	struct sockaddr_in6	uin6_sin;
121cfa1ca9dSYoshinobu Inoue 	u_char			uin6_init_done : 1;
122cfa1ca9dSYoshinobu Inoue } udp_in6 = {
123cfa1ca9dSYoshinobu Inoue 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
124cfa1ca9dSYoshinobu Inoue 	0
125cfa1ca9dSYoshinobu Inoue };
126cfa1ca9dSYoshinobu Inoue struct udp_ip6 {
127cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr		uip6_ip6;
128cfa1ca9dSYoshinobu Inoue 	u_char			uip6_init_done : 1;
129cfa1ca9dSYoshinobu Inoue } udp_ip6;
130cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
131df8bae1dSRodney W. Grimes 
132c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
133c1cd65baSBruce Evans 		int off);
134cfa1ca9dSYoshinobu Inoue #ifdef INET6
1354d77a549SAlfred Perlstein static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
136cfa1ca9dSYoshinobu Inoue #endif
137cfa1ca9dSYoshinobu Inoue 
1384d77a549SAlfred Perlstein static int udp_detach(struct socket *so);
1394d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1404d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
141df8bae1dSRodney W. Grimes 
142df8bae1dSRodney W. Grimes void
143df8bae1dSRodney W. Grimes udp_init()
144df8bae1dSRodney W. Grimes {
145f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
14615bd2b43SDavid Greenman 	LIST_INIT(&udb);
14715bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
148ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1498781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1508781d8e9SBruce Evans 					&udbinfo.porthashmask);
15169c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
15269c2d429SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
15369c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes void
157f0ffb944SJulian Elischer udp_input(m, off)
158df8bae1dSRodney W. Grimes 	register struct mbuf *m;
159f0ffb944SJulian Elischer 	int off;
160df8bae1dSRodney W. Grimes {
161cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
162df8bae1dSRodney W. Grimes 	register struct ip *ip;
163df8bae1dSRodney W. Grimes 	register struct udphdr *uh;
164df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
165df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
166df8bae1dSRodney W. Grimes 	int len;
167df8bae1dSRodney W. Grimes 	struct ip save_ip;
168cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
171df8bae1dSRodney W. Grimes 
172df8bae1dSRodney W. Grimes 	/*
173df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
174df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
175df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
176df8bae1dSRodney W. Grimes 	 * with options still present.
177df8bae1dSRodney W. Grimes 	 */
178df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
179df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
180df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
181df8bae1dSRodney W. Grimes 	}
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes 	/*
184df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
185df8bae1dSRodney W. Grimes 	 */
186df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
187df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
188df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
189df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
190df8bae1dSRodney W. Grimes 			return;
191df8bae1dSRodney W. Grimes 		}
192df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
193df8bae1dSRodney W. Grimes 	}
194df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
195df8bae1dSRodney W. Grimes 
196686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
197686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
198f76fcf6dSJeffrey Hsu 		goto badunlocked;
199686cdd19SJun-ichiro itojun Hagino 
200df8bae1dSRodney W. Grimes 	/*
201df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
202df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
203df8bae1dSRodney W. Grimes 	 */
204df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
205df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2067eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
207df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
208f76fcf6dSJeffrey Hsu 			goto badunlocked;
209df8bae1dSRodney W. Grimes 		}
210df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
211df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
212df8bae1dSRodney W. Grimes 	}
213df8bae1dSRodney W. Grimes 	/*
214df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
215df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
216df8bae1dSRodney W. Grimes 	 */
21748cb400fSRuslan Ermilov 	if (!blackhole)
218df8bae1dSRodney W. Grimes 		save_ip = *ip;
219df8bae1dSRodney W. Grimes 
220df8bae1dSRodney W. Grimes 	/*
221df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
222df8bae1dSRodney W. Grimes 	 */
2236dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
224db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
225db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
226db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
227db4f9cc7SJonathan Lemon 			else
228db4f9cc7SJonathan Lemon 	                	uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
229506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
230db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
231db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
232db4f9cc7SJonathan Lemon 		} else {
233cb342100SHajimu UMEMOTO 			char b[9];
234cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2356effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
236df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
237623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
238cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
239db4f9cc7SJonathan Lemon 		}
240623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
241df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
242df8bae1dSRodney W. Grimes 			m_freem(m);
243df8bae1dSRodney W. Grimes 			return;
244df8bae1dSRodney W. Grimes 		}
245fb9aaba0SRuslan Ermilov 	} else
246fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
247df8bae1dSRodney W. Grimes 
248f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
249f76fcf6dSJeffrey Hsu 
250df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
251df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
25282c23ebaSBill Fenner 		struct inpcb *last;
253df8bae1dSRodney W. Grimes 		/*
254df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
255df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
256df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
257df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
258df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
259df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
260df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
261df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
262df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
263df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
264df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
265df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
266df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
267df8bae1dSRodney W. Grimes 		 */
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes 		/*
270df8bae1dSRodney W. Grimes 		 * Construct sockaddr format source address.
271df8bae1dSRodney W. Grimes 		 */
272df8bae1dSRodney W. Grimes 		udp_in.sin_port = uh->uh_sport;
273df8bae1dSRodney W. Grimes 		udp_in.sin_addr = ip->ip_src;
274df8bae1dSRodney W. Grimes 		/*
275df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
276df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
277df8bae1dSRodney W. Grimes 		 */
278df8bae1dSRodney W. Grimes 		last = NULL;
279cfa1ca9dSYoshinobu Inoue #ifdef INET6
280cfa1ca9dSYoshinobu Inoue 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
281cfa1ca9dSYoshinobu Inoue #endif
282cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
283f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
284f76fcf6dSJeffrey Hsu 			if (inp->inp_lport != uh->uh_dport) {
285f76fcf6dSJeffrey Hsu 		docontinue:
286f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
287f76fcf6dSJeffrey Hsu 				continue;
288f76fcf6dSJeffrey Hsu 			}
289cfa1ca9dSYoshinobu Inoue #ifdef INET6
290369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
291f76fcf6dSJeffrey Hsu 				goto docontinue;
292cfa1ca9dSYoshinobu Inoue #endif
293df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
294f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
295f76fcf6dSJeffrey Hsu 					goto docontinue;
296df8bae1dSRodney W. Grimes 			}
297df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
298df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
299df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
300df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
301f76fcf6dSJeffrey Hsu 					goto docontinue;
302df8bae1dSRodney W. Grimes 			}
303df8bae1dSRodney W. Grimes 
304df8bae1dSRodney W. Grimes 			if (last != NULL) {
305df8bae1dSRodney W. Grimes 				struct mbuf *n;
306df8bae1dSRodney W. Grimes 
307cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
308cfa1ca9dSYoshinobu Inoue 				/* check AH/ESP integrity. */
309cfa1ca9dSYoshinobu Inoue 				if (ipsec4_in_reject_so(m, last->inp_socket))
310cfa1ca9dSYoshinobu Inoue 					ipsecstat.in_polvio++;
311cfa1ca9dSYoshinobu Inoue 					/* do not inject data to pcb */
312cfa1ca9dSYoshinobu Inoue 				else
313cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
314cfa1ca9dSYoshinobu Inoue 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
315cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
316cfa1ca9dSYoshinobu Inoue 						   iphlen +
317cfa1ca9dSYoshinobu Inoue 						   sizeof(struct udphdr));
318f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
319df8bae1dSRodney W. Grimes 			}
32082c23ebaSBill Fenner 			last = inp;
321df8bae1dSRodney W. Grimes 			/*
322df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
323df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
324df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
325df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
326df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
327df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
328df8bae1dSRodney W. Grimes 			 */
3294cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
330df8bae1dSRodney W. Grimes 				break;
331df8bae1dSRodney W. Grimes 		}
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes 		if (last == NULL) {
334df8bae1dSRodney W. Grimes 			/*
335df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
336df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
337df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
338df8bae1dSRodney W. Grimes 			 */
339df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
340f76fcf6dSJeffrey Hsu 			INP_INFO_RUNLOCK(&udbinfo);
341df8bae1dSRodney W. Grimes 			goto bad;
342df8bae1dSRodney W. Grimes 		}
343cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
344cfa1ca9dSYoshinobu Inoue 		/* check AH/ESP integrity. */
345cfa1ca9dSYoshinobu Inoue 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
346cfa1ca9dSYoshinobu Inoue 			ipsecstat.in_polvio++;
347f76fcf6dSJeffrey Hsu 			INP_INFO_RUNLOCK(&udbinfo);
348df8bae1dSRodney W. Grimes 			goto bad;
349df8bae1dSRodney W. Grimes 		}
350cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
351f76fcf6dSJeffrey Hsu 		INP_UNLOCK(last);
352f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
353cfa1ca9dSYoshinobu Inoue 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
354df8bae1dSRodney W. Grimes 		return;
355df8bae1dSRodney W. Grimes 	}
356df8bae1dSRodney W. Grimes 	/*
3576d6a026bSDavid Greenman 	 * Locate pcb for datagram.
358df8bae1dSRodney W. Grimes 	 */
359c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
360cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
361f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
36215bd2b43SDavid Greenman 	if (inp == NULL) {
36375cfc95fSAndrey A. Chernov 		if (log_in_vain) {
364df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
36575cfc95fSAndrey A. Chernov 
36675cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
367592071e8SBruce Evans 			log(LOG_INFO,
368592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
369592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
370592071e8SBruce Evans 			    ntohs(uh->uh_sport));
37175cfc95fSAndrey A. Chernov 		}
372df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
373df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
374df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
375df8bae1dSRodney W. Grimes 			goto bad;
376df8bae1dSRodney W. Grimes 		}
377a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
37851508de1SMatthew Dillon 			goto bad;
379582a7760SBruce Evans 		if (blackhole)
38012b4fd06SPoul-Henning Kamp 			goto bad;
38104287599SRuslan Ermilov 		*ip = save_ip;
38204287599SRuslan Ermilov 		ip->ip_len += iphlen;
383582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
384f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
385df8bae1dSRodney W. Grimes 		return;
386df8bae1dSRodney W. Grimes 	}
387cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
388cfa1ca9dSYoshinobu Inoue 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
389cfa1ca9dSYoshinobu Inoue 		ipsecstat.in_polvio++;
390f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
391cfa1ca9dSYoshinobu Inoue 		goto bad;
392cfa1ca9dSYoshinobu Inoue 	}
393cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
394df8bae1dSRodney W. Grimes 
395f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
396df8bae1dSRodney W. Grimes 	/*
397df8bae1dSRodney W. Grimes 	 * Construct sockaddr format source address.
398df8bae1dSRodney W. Grimes 	 * Stuff source address and datagram in user buffer.
399df8bae1dSRodney W. Grimes 	 */
400df8bae1dSRodney W. Grimes 	udp_in.sin_port = uh->uh_sport;
401df8bae1dSRodney W. Grimes 	udp_in.sin_addr = ip->ip_src;
40282dab6ceSGarrett Wollman 	if (inp->inp_flags & INP_CONTROLOPTS
403cfa1ca9dSYoshinobu Inoue 	    || inp->inp_socket->so_options & SO_TIMESTAMP) {
404cfa1ca9dSYoshinobu Inoue #ifdef INET6
405cfa1ca9dSYoshinobu Inoue 		if (inp->inp_vflag & INP_IPV6) {
406cfa1ca9dSYoshinobu Inoue 			int savedflags;
407cfa1ca9dSYoshinobu Inoue 
408cfa1ca9dSYoshinobu Inoue 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
409cfa1ca9dSYoshinobu Inoue 			savedflags = inp->inp_flags;
410cfa1ca9dSYoshinobu Inoue 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
411cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
412cfa1ca9dSYoshinobu Inoue 			inp->inp_flags = savedflags;
413cfa1ca9dSYoshinobu Inoue 		} else
414cfa1ca9dSYoshinobu Inoue #endif
41582c23ebaSBill Fenner 		ip_savecontrol(inp, &opts, ip, m);
4164cc20ab1SSeigo Tanimura 	}
41733841545SHajimu UMEMOTO  	m_adj(m, iphlen + sizeof(struct udphdr));
418cfa1ca9dSYoshinobu Inoue #ifdef INET6
419cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6) {
420cfa1ca9dSYoshinobu Inoue 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
421cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6;
422cfa1ca9dSYoshinobu Inoue 	} else
423cfa1ca9dSYoshinobu Inoue #endif
424cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
425cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
426df8bae1dSRodney W. Grimes 		udpstat.udps_fullsock++;
427df8bae1dSRodney W. Grimes 		goto bad;
428df8bae1dSRodney W. Grimes 	}
429df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
430f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
431df8bae1dSRodney W. Grimes 	return;
432df8bae1dSRodney W. Grimes bad:
433f76fcf6dSJeffrey Hsu 	if (inp)
434f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
435f76fcf6dSJeffrey Hsu badunlocked:
436df8bae1dSRodney W. Grimes 	m_freem(m);
437df8bae1dSRodney W. Grimes 	if (opts)
438df8bae1dSRodney W. Grimes 		m_freem(opts);
439cfa1ca9dSYoshinobu Inoue 	return;
440cfa1ca9dSYoshinobu Inoue }
441cfa1ca9dSYoshinobu Inoue 
442686cdd19SJun-ichiro itojun Hagino #ifdef INET6
443cfa1ca9dSYoshinobu Inoue static void
444cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(ip6, ip)
445cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr *ip6;
446cfa1ca9dSYoshinobu Inoue 	struct ip *ip;
447cfa1ca9dSYoshinobu Inoue {
448cfa1ca9dSYoshinobu Inoue 	bzero(ip6, sizeof(*ip6));
449cfa1ca9dSYoshinobu Inoue 
450cfa1ca9dSYoshinobu Inoue 	ip6->ip6_vfc = IPV6_VERSION;
451cfa1ca9dSYoshinobu Inoue 	ip6->ip6_plen = ip->ip_len;
452cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt = ip->ip_p;
453cfa1ca9dSYoshinobu Inoue 	ip6->ip6_hlim = ip->ip_ttl;
454cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
455cfa1ca9dSYoshinobu Inoue 		IPV6_ADDR_INT32_SMP;
456cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
457cfa1ca9dSYoshinobu Inoue 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
458cfa1ca9dSYoshinobu Inoue }
459cfa1ca9dSYoshinobu Inoue #endif
460cfa1ca9dSYoshinobu Inoue 
461cfa1ca9dSYoshinobu Inoue /*
462cfa1ca9dSYoshinobu Inoue  * subroutine of udp_input(), mainly for source code readability.
463cfa1ca9dSYoshinobu Inoue  * caller must properly init udp_ip6 and udp_in6 beforehand.
464cfa1ca9dSYoshinobu Inoue  */
465cfa1ca9dSYoshinobu Inoue static void
466cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, off)
467cfa1ca9dSYoshinobu Inoue 	struct inpcb *last;
468cfa1ca9dSYoshinobu Inoue 	struct ip *ip;
469cfa1ca9dSYoshinobu Inoue 	struct mbuf *n;
470cfa1ca9dSYoshinobu Inoue 	int off;
471cfa1ca9dSYoshinobu Inoue {
472cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
473cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
474cfa1ca9dSYoshinobu Inoue 
475cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
476cfa1ca9dSYoshinobu Inoue 	    last->inp_socket->so_options & SO_TIMESTAMP) {
477cfa1ca9dSYoshinobu Inoue #ifdef INET6
478cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
479cfa1ca9dSYoshinobu Inoue 			int savedflags;
480cfa1ca9dSYoshinobu Inoue 
481cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
482cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
483cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
484cfa1ca9dSYoshinobu Inoue 			}
485cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
486cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
487cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
488cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
489cfa1ca9dSYoshinobu Inoue 		} else
490cfa1ca9dSYoshinobu Inoue #endif
491cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4924cc20ab1SSeigo Tanimura 	}
493cfa1ca9dSYoshinobu Inoue #ifdef INET6
494cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
495cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
496cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
497cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
498cfa1ca9dSYoshinobu Inoue 		}
499cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
500cfa1ca9dSYoshinobu Inoue 	} else
501cfa1ca9dSYoshinobu Inoue #endif
502cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
503cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
504cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
505cfa1ca9dSYoshinobu Inoue 		m_freem(n);
506cfa1ca9dSYoshinobu Inoue 		if (opts)
507cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
508cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5094cc20ab1SSeigo Tanimura 	} else
510cfa1ca9dSYoshinobu Inoue 		sorwakeup(last->inp_socket);
511df8bae1dSRodney W. Grimes }
512df8bae1dSRodney W. Grimes 
513df8bae1dSRodney W. Grimes /*
514df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
515df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
516df8bae1dSRodney W. Grimes  */
51776429de4SYoshinobu Inoue void
518df8bae1dSRodney W. Grimes udp_notify(inp, errno)
519df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
520df8bae1dSRodney W. Grimes 	int errno;
521df8bae1dSRodney W. Grimes {
522df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
523df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
524df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
525df8bae1dSRodney W. Grimes }
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes void
528b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip)
529df8bae1dSRodney W. Grimes 	int cmd;
530df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
531b62d102cSBruce Evans 	void *vip;
532df8bae1dSRodney W. Grimes {
533c693a045SJonathan Lemon 	struct ip *ip = vip;
534c693a045SJonathan Lemon 	struct udphdr *uh;
5354d77a549SAlfred Perlstein 	void (*notify)(struct inpcb *, int) = udp_notify;
536c693a045SJonathan Lemon         struct in_addr faddr;
537c693a045SJonathan Lemon 	struct inpcb *inp;
538c693a045SJonathan Lemon 	int s;
539c693a045SJonathan Lemon 
540c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
541c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
542c693a045SJonathan Lemon         	return;
543df8bae1dSRodney W. Grimes 
544d1c54148SJesper Skriver 	if (PRC_IS_REDIRECT(cmd)) {
545d1c54148SJesper Skriver 		ip = 0;
546d1c54148SJesper Skriver 		notify = in_rtchange;
547d1c54148SJesper Skriver 	} else if (cmd == PRC_HOSTDEAD)
548d1c54148SJesper Skriver 		ip = 0;
549d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
550df8bae1dSRodney W. Grimes 		return;
551df8bae1dSRodney W. Grimes 	if (ip) {
552c693a045SJonathan Lemon 		s = splnet();
553df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
554f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
555c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
556c693a045SJonathan Lemon                     ip->ip_src, uh->uh_sport, 0, NULL);
557f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
558f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
559f76fcf6dSJeffrey Hsu 			if(inp->inp_socket != NULL) {
560c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
561f76fcf6dSJeffrey Hsu 			}
562f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
563f76fcf6dSJeffrey Hsu 		}
564f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
565c693a045SJonathan Lemon 		splx(s);
566df8bae1dSRodney W. Grimes 	} else
567f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
5700312fbe9SPoul-Henning Kamp static int
57182d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
57298271db4SGarrett Wollman {
57398271db4SGarrett Wollman 	int error, i, n, s;
57498271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
57598271db4SGarrett Wollman 	inp_gen_t gencnt;
57698271db4SGarrett Wollman 	struct xinpgen xig;
57798271db4SGarrett Wollman 
57898271db4SGarrett Wollman 	/*
57998271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
58098271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
58198271db4SGarrett Wollman 	 */
58298271db4SGarrett Wollman 	if (req->oldptr == 0) {
58398271db4SGarrett Wollman 		n = udbinfo.ipi_count;
58498271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
58598271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
58698271db4SGarrett Wollman 		return 0;
58798271db4SGarrett Wollman 	}
58898271db4SGarrett Wollman 
58998271db4SGarrett Wollman 	if (req->newptr != 0)
59098271db4SGarrett Wollman 		return EPERM;
59198271db4SGarrett Wollman 
59298271db4SGarrett Wollman 	/*
59398271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
59498271db4SGarrett Wollman 	 */
59598271db4SGarrett Wollman 	s = splnet();
59698271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
59798271db4SGarrett Wollman 	n = udbinfo.ipi_count;
59898271db4SGarrett Wollman 	splx(s);
59998271db4SGarrett Wollman 
60098271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
60198271db4SGarrett Wollman 	xig.xig_count = n;
60298271db4SGarrett Wollman 	xig.xig_gen = gencnt;
60398271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
60498271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
60598271db4SGarrett Wollman 	if (error)
60698271db4SGarrett Wollman 		return error;
60798271db4SGarrett Wollman 
60898271db4SGarrett Wollman 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
60998271db4SGarrett Wollman 	if (inp_list == 0)
61098271db4SGarrett Wollman 		return ENOMEM;
61198271db4SGarrett Wollman 
61298271db4SGarrett Wollman 	s = splnet();
613f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
614fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
615fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
616f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6178a7d8cc6SRobert Watson 		if (inp->inp_gencnt <= gencnt) {
61829dc1288SRobert Watson 			if (cr_canseesocket(req->td->td_ucred,
61929dc1288SRobert Watson 			    inp->inp_socket))
6204787fd37SPaul Saab 				continue;
62198271db4SGarrett Wollman 			inp_list[i++] = inp;
62298271db4SGarrett Wollman 		}
623f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6244787fd37SPaul Saab 	}
625f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
62698271db4SGarrett Wollman 	splx(s);
62798271db4SGarrett Wollman 	n = i;
62898271db4SGarrett Wollman 
62998271db4SGarrett Wollman 	error = 0;
63098271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
63198271db4SGarrett Wollman 		inp = inp_list[i];
632f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
63398271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
63498271db4SGarrett Wollman 			struct xinpcb xi;
63598271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
63698271db4SGarrett Wollman 			/* XXX should avoid extra copy */
63798271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
63898271db4SGarrett Wollman 			if (inp->inp_socket)
63998271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
64098271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
64198271db4SGarrett Wollman 		}
642f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
64398271db4SGarrett Wollman 	}
64498271db4SGarrett Wollman 	if (!error) {
64598271db4SGarrett Wollman 		/*
64698271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
64798271db4SGarrett Wollman 		 * If the generation differs from what we told
64898271db4SGarrett Wollman 		 * her before, she knows that something happened
64998271db4SGarrett Wollman 		 * while we were processing this request, and it
65098271db4SGarrett Wollman 		 * might be necessary to retry.
65198271db4SGarrett Wollman 		 */
65298271db4SGarrett Wollman 		s = splnet();
653f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
65498271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
65598271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
65698271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
657f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
65898271db4SGarrett Wollman 		splx(s);
65998271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
66098271db4SGarrett Wollman 	}
66198271db4SGarrett Wollman 	free(inp_list, M_TEMP);
66298271db4SGarrett Wollman 	return error;
66398271db4SGarrett Wollman }
66498271db4SGarrett Wollman 
66598271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
66698271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
66798271db4SGarrett Wollman 
66898271db4SGarrett Wollman static int
66982d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
670490d50b6SBrian Feldman {
671c0511d3bSBrian Feldman 	struct xucred xuc;
672490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
673490d50b6SBrian Feldman 	struct inpcb *inp;
674490d50b6SBrian Feldman 	int error, s;
675490d50b6SBrian Feldman 
67644731cabSJohn Baldwin 	error = suser_cred(req->td->td_ucred, PRISON_ROOT);
677490d50b6SBrian Feldman 	if (error)
678490d50b6SBrian Feldman 		return (error);
679490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
680490d50b6SBrian Feldman 	if (error)
681490d50b6SBrian Feldman 		return (error);
682490d50b6SBrian Feldman 	s = splnet();
683f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
684490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
685cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6862f9a2132SBrian Feldman 	if (inp == NULL || inp->inp_socket == NULL) {
687490d50b6SBrian Feldman 		error = ENOENT;
688490d50b6SBrian Feldman 		goto out;
689490d50b6SBrian Feldman 	}
69029dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
6917ce87f12SDavid Malone 	if (error)
6927ce87f12SDavid Malone 		goto out;
69376183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
694c0511d3bSBrian Feldman 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
695490d50b6SBrian Feldman out:
696f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
697490d50b6SBrian Feldman 	splx(s);
698490d50b6SBrian Feldman 	return (error);
699490d50b6SBrian Feldman }
700490d50b6SBrian Feldman 
7017ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7027ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7037ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
704490d50b6SBrian Feldman 
705490d50b6SBrian Feldman static int
706b40ce416SJulian Elischer udp_output(inp, m, addr, control, td)
707df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
708d25f3712SBrian Feldman 	struct mbuf *m;
70957bf258eSGarrett Wollman 	struct sockaddr *addr;
71057bf258eSGarrett Wollman 	struct mbuf *control;
711b40ce416SJulian Elischer 	struct thread *td;
712df8bae1dSRodney W. Grimes {
713df8bae1dSRodney W. Grimes 	register struct udpiphdr *ui;
714df8bae1dSRodney W. Grimes 	register int len = m->m_pkthdr.len;
715df8bae1dSRodney W. Grimes 	struct in_addr laddr;
71675c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
7174cc20ab1SSeigo Tanimura 	int s = 0, error = 0;
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	if (control)
720df8bae1dSRodney W. Grimes 		m_freem(control);		/* XXX */
721df8bae1dSRodney W. Grimes 
722430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
723430d30d8SBill Fenner 		error = EMSGSIZE;
724430d30d8SBill Fenner 		goto release;
725430d30d8SBill Fenner 	}
726430d30d8SBill Fenner 
727df8bae1dSRodney W. Grimes 	if (addr) {
72875c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
729a854ed98SJohn Baldwin 		if (td && jailed(td->td_ucred))
730a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
731df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
732df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
733df8bae1dSRodney W. Grimes 			error = EISCONN;
734df8bae1dSRodney W. Grimes 			goto release;
735df8bae1dSRodney W. Grimes 		}
736df8bae1dSRodney W. Grimes 		/*
737df8bae1dSRodney W. Grimes 		 * Must block input while temporarily connected.
738df8bae1dSRodney W. Grimes 		 */
739df8bae1dSRodney W. Grimes 		s = splnet();
740b40ce416SJulian Elischer 		error = in_pcbconnect(inp, addr, td);
741df8bae1dSRodney W. Grimes 		if (error) {
742df8bae1dSRodney W. Grimes 			splx(s);
743df8bae1dSRodney W. Grimes 			goto release;
744df8bae1dSRodney W. Grimes 		}
745df8bae1dSRodney W. Grimes 	} else {
746df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
747df8bae1dSRodney W. Grimes 			error = ENOTCONN;
748df8bae1dSRodney W. Grimes 			goto release;
749df8bae1dSRodney W. Grimes 		}
750df8bae1dSRodney W. Grimes 	}
751df8bae1dSRodney W. Grimes 	/*
752df8bae1dSRodney W. Grimes 	 * Calculate data length and get a mbuf
753df8bae1dSRodney W. Grimes 	 * for UDP and IP headers.
754df8bae1dSRodney W. Grimes 	 */
755df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
756df8bae1dSRodney W. Grimes 	if (m == 0) {
757df8bae1dSRodney W. Grimes 		error = ENOBUFS;
7581c09f774SGarrett Wollman 		if (addr)
7591c09f774SGarrett Wollman 			splx(s);
760df8bae1dSRodney W. Grimes 		goto release;
761df8bae1dSRodney W. Grimes 	}
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	/*
764df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
765df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
766df8bae1dSRodney W. Grimes 	 */
767df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
768db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
769df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
770df8bae1dSRodney W. Grimes 	ui->ui_src = inp->inp_laddr;
771df8bae1dSRodney W. Grimes 	ui->ui_dst = inp->inp_faddr;
772df8bae1dSRodney W. Grimes 	ui->ui_sport = inp->inp_lport;
773df8bae1dSRodney W. Grimes 	ui->ui_dport = inp->inp_fport;
774db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes 	/*
777db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
778df8bae1dSRodney W. Grimes 	 */
779df8bae1dSRodney W. Grimes 	if (udpcksum) {
780db4f9cc7SJonathan Lemon         	ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
781db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
782db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
783db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
784db4f9cc7SJonathan Lemon 	} else {
785db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
786df8bae1dSRodney W. Grimes 	}
787df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
788ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
789ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
790df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
791cfa1ca9dSYoshinobu Inoue 
792cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
79333841545SHajimu UMEMOTO 	if (ipsec_setsocket(m, inp->inp_socket) != 0) {
79433841545SHajimu UMEMOTO 		error = ENOBUFS;
79533841545SHajimu UMEMOTO 		goto release;
79633841545SHajimu UMEMOTO 	}
797cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
798df8bae1dSRodney W. Grimes 	error = ip_output(m, inp->inp_options, &inp->inp_route,
7994cc20ab1SSeigo Tanimura 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
800df8bae1dSRodney W. Grimes 	    inp->inp_moptions);
801df8bae1dSRodney W. Grimes 
802df8bae1dSRodney W. Grimes 	if (addr) {
803df8bae1dSRodney W. Grimes 		in_pcbdisconnect(inp);
80457bf258eSGarrett Wollman 		inp->inp_laddr = laddr;	/* XXX rehash? */
805df8bae1dSRodney W. Grimes 		splx(s);
806df8bae1dSRodney W. Grimes 	}
807df8bae1dSRodney W. Grimes 	return (error);
808df8bae1dSRodney W. Grimes 
809df8bae1dSRodney W. Grimes release:
810df8bae1dSRodney W. Grimes 	m_freem(m);
811df8bae1dSRodney W. Grimes 	return (error);
812df8bae1dSRodney W. Grimes }
813df8bae1dSRodney W. Grimes 
81476429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
815df8bae1dSRodney W. Grimes 					/* 40 1K datagrams */
8160312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
8173d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
8180312fbe9SPoul-Henning Kamp 
819cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
820cfa1ca9dSYoshinobu Inoue #ifdef INET6
821cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
822cfa1ca9dSYoshinobu Inoue #else
823cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
824cfa1ca9dSYoshinobu Inoue #endif
825cfa1ca9dSYoshinobu Inoue 				      );
8260312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
8273d177f46SBill Fumerola     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
828df8bae1dSRodney W. Grimes 
829d0390e05SGarrett Wollman static int
830d0390e05SGarrett Wollman udp_abort(struct socket *so)
831df8bae1dSRodney W. Grimes {
832d0390e05SGarrett Wollman 	struct inpcb *inp;
833df8bae1dSRodney W. Grimes 	int s;
834df8bae1dSRodney W. Grimes 
835f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
836d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
837f76fcf6dSJeffrey Hsu 	if (inp == 0) {
838f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
839d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
840f76fcf6dSJeffrey Hsu 	}
841f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
842d0390e05SGarrett Wollman 	soisdisconnected(so);
843d0390e05SGarrett Wollman 	s = splnet();
844d0390e05SGarrett Wollman 	in_pcbdetach(inp);
845f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
846d0390e05SGarrett Wollman 	splx(s);
847d0390e05SGarrett Wollman 	return 0;
848df8bae1dSRodney W. Grimes }
849df8bae1dSRodney W. Grimes 
850d0390e05SGarrett Wollman static int
851b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
852d0390e05SGarrett Wollman {
853d0390e05SGarrett Wollman 	struct inpcb *inp;
854d0390e05SGarrett Wollman 	int s, error;
855d0390e05SGarrett Wollman 
856f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
857d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
858f76fcf6dSJeffrey Hsu 	if (inp != 0) {
859f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
860d0390e05SGarrett Wollman 		return EINVAL;
861f76fcf6dSJeffrey Hsu 	}
862cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
863f76fcf6dSJeffrey Hsu 	if (error) {
864f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
865cfa1ca9dSYoshinobu Inoue 		return error;
866f76fcf6dSJeffrey Hsu 	}
867df8bae1dSRodney W. Grimes 	s = splnet();
868b40ce416SJulian Elischer 	error = in_pcballoc(so, &udbinfo, td);
869df8bae1dSRodney W. Grimes 	splx(s);
870df8bae1dSRodney W. Grimes 	if (error)
871d0390e05SGarrett Wollman 		return error;
872cfa1ca9dSYoshinobu Inoue 
873cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
874f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
875f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
876cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
877cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
878f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
879d0390e05SGarrett Wollman 	return 0;
880df8bae1dSRodney W. Grimes }
881d0390e05SGarrett Wollman 
882d0390e05SGarrett Wollman static int
883b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
884d0390e05SGarrett Wollman {
885d0390e05SGarrett Wollman 	struct inpcb *inp;
886d0390e05SGarrett Wollman 	int s, error;
887d0390e05SGarrett Wollman 
888f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
889d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
890f76fcf6dSJeffrey Hsu 	if (inp == 0) {
891f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
892d0390e05SGarrett Wollman 		return EINVAL;
893f76fcf6dSJeffrey Hsu 	}
894f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
895df8bae1dSRodney W. Grimes 	s = splnet();
896b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
897d0390e05SGarrett Wollman 	splx(s);
898f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
899f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
900d0390e05SGarrett Wollman 	return error;
901d0390e05SGarrett Wollman }
902d0390e05SGarrett Wollman 
903d0390e05SGarrett Wollman static int
904b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
905d0390e05SGarrett Wollman {
906d0390e05SGarrett Wollman 	struct inpcb *inp;
907d0390e05SGarrett Wollman 	int s, error;
90875c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
909d0390e05SGarrett Wollman 
910f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
911d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
912f76fcf6dSJeffrey Hsu 	if (inp == 0) {
913f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
914d0390e05SGarrett Wollman 		return EINVAL;
915f76fcf6dSJeffrey Hsu 	}
916f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
917f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
918f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
919f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
920d0390e05SGarrett Wollman 		return EISCONN;
921f76fcf6dSJeffrey Hsu 	}
922d0390e05SGarrett Wollman 	s = splnet();
92375c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
924a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
925a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
926b40ce416SJulian Elischer 	error = in_pcbconnect(inp, nam, td);
927df8bae1dSRodney W. Grimes 	splx(s);
9284cc20ab1SSeigo Tanimura 	if (error == 0)
929df8bae1dSRodney W. Grimes 		soisconnected(so);
930f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
931f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
932d0390e05SGarrett Wollman 	return error;
933df8bae1dSRodney W. Grimes }
934d0390e05SGarrett Wollman 
935d0390e05SGarrett Wollman static int
936d0390e05SGarrett Wollman udp_detach(struct socket *so)
937d0390e05SGarrett Wollman {
938d0390e05SGarrett Wollman 	struct inpcb *inp;
939d0390e05SGarrett Wollman 	int s;
940d0390e05SGarrett Wollman 
941f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
942d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
943f76fcf6dSJeffrey Hsu 	if (inp == 0) {
944f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
945d0390e05SGarrett Wollman 		return EINVAL;
946f76fcf6dSJeffrey Hsu 	}
947f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
948d0390e05SGarrett Wollman 	s = splnet();
949d0390e05SGarrett Wollman 	in_pcbdetach(inp);
950f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
951d0390e05SGarrett Wollman 	splx(s);
952d0390e05SGarrett Wollman 	return 0;
953d0390e05SGarrett Wollman }
954d0390e05SGarrett Wollman 
955d0390e05SGarrett Wollman static int
956d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
957d0390e05SGarrett Wollman {
958d0390e05SGarrett Wollman 	struct inpcb *inp;
959d0390e05SGarrett Wollman 	int s;
960d0390e05SGarrett Wollman 
961f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
962d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
963f76fcf6dSJeffrey Hsu 	if (inp == 0) {
964f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
965d0390e05SGarrett Wollman 		return EINVAL;
966f76fcf6dSJeffrey Hsu 	}
967f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
968f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
969f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
970f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
971d0390e05SGarrett Wollman 		return ENOTCONN;
972f76fcf6dSJeffrey Hsu 	}
973d0390e05SGarrett Wollman 
974df8bae1dSRodney W. Grimes 	s = splnet();
975df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
976df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
977f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
978f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
979df8bae1dSRodney W. Grimes 	splx(s);
980df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
981d0390e05SGarrett Wollman 	return 0;
982df8bae1dSRodney W. Grimes }
983df8bae1dSRodney W. Grimes 
984d0390e05SGarrett Wollman static int
98557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
986b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
987d0390e05SGarrett Wollman {
988d0390e05SGarrett Wollman 	struct inpcb *inp;
989f76fcf6dSJeffrey Hsu 	int ret;
990d0390e05SGarrett Wollman 
991f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
992d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
993d0390e05SGarrett Wollman 	if (inp == 0) {
994f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
995d0390e05SGarrett Wollman 		m_freem(m);
996d0390e05SGarrett Wollman 		return EINVAL;
997d0390e05SGarrett Wollman 	}
998f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
999f76fcf6dSJeffrey Hsu 	ret = udp_output(inp, m, addr, control, td);
1000f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1001f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1002f76fcf6dSJeffrey Hsu 	return ret;
1003d0390e05SGarrett Wollman }
1004d0390e05SGarrett Wollman 
100576429de4SYoshinobu Inoue int
1006d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1007d0390e05SGarrett Wollman {
1008d0390e05SGarrett Wollman 	struct inpcb *inp;
1009d0390e05SGarrett Wollman 
1010f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1011d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1012f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1013f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1014d0390e05SGarrett Wollman 		return EINVAL;
1015f76fcf6dSJeffrey Hsu 	}
1016f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1017f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1018d0390e05SGarrett Wollman 	socantsendmore(so);
1019f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1020d0390e05SGarrett Wollman 	return 0;
1021d0390e05SGarrett Wollman }
1022d0390e05SGarrett Wollman 
1023f76fcf6dSJeffrey Hsu /*
1024f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1025f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1026f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1027f76fcf6dSJeffrey Hsu  */
1028f76fcf6dSJeffrey Hsu static int
1029f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1030f76fcf6dSJeffrey Hsu {
1031f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1032f76fcf6dSJeffrey Hsu }
1033f76fcf6dSJeffrey Hsu 
1034f76fcf6dSJeffrey Hsu /*
1035f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1036f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1037f76fcf6dSJeffrey Hsu  */
1038f76fcf6dSJeffrey Hsu static int
1039f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1040f76fcf6dSJeffrey Hsu {
1041f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1042f76fcf6dSJeffrey Hsu }
1043f76fcf6dSJeffrey Hsu 
1044d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1045117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1046117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1047f76fcf6dSJeffrey Hsu 	pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1048117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1049f76fcf6dSJeffrey Hsu 	udp_sockaddr, sosend, soreceive, sopoll
1050d0390e05SGarrett Wollman };
1051