xref: /freebsd/sys/netinet/udp_usrreq.c (revision 1cbd978e96176e5cb8e785952ed34d1aba66b11d)
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 
81cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
82cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
83cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
84cfa1ca9dSYoshinobu Inoue 
85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
86db4f9cc7SJonathan Lemon 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * UDP protocol implementation.
89df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
90df8bae1dSRodney W. Grimes  */
91df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
920312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
93df8bae1dSRodney W. Grimes #else
940312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
95df8bae1dSRodney W. Grimes #endif
960312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
970312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
98df8bae1dSRodney W. Grimes 
9976429de4SYoshinobu Inoue int	log_in_vain = 0;
100816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1013d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
102816a3d83SPoul-Henning Kamp 
10316f7f31fSGeoff Rehmet static int	blackhole = 0;
10416f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10516f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
10616f7f31fSGeoff Rehmet 
10776429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
108cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1097a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
11015bd2b43SDavid Greenman 
11115bd2b43SDavid Greenman #ifndef UDBHASHSIZE
112c3229e05SDavid Greenman #define UDBHASHSIZE 16
11315bd2b43SDavid Greenman #endif
11415bd2b43SDavid Greenman 
11576429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
116c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1173d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
118f2ea20e6SGarrett Wollman 
1190312fbe9SPoul-Henning Kamp static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
120cfa1ca9dSYoshinobu Inoue #ifdef INET6
121cfa1ca9dSYoshinobu Inoue struct udp_in6 {
122cfa1ca9dSYoshinobu Inoue 	struct sockaddr_in6	uin6_sin;
123cfa1ca9dSYoshinobu Inoue 	u_char			uin6_init_done : 1;
124cfa1ca9dSYoshinobu Inoue } udp_in6 = {
125cfa1ca9dSYoshinobu Inoue 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
126cfa1ca9dSYoshinobu Inoue 	0
127cfa1ca9dSYoshinobu Inoue };
128cfa1ca9dSYoshinobu Inoue struct udp_ip6 {
129cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr		uip6_ip6;
130cfa1ca9dSYoshinobu Inoue 	u_char			uip6_init_done : 1;
131cfa1ca9dSYoshinobu Inoue } udp_ip6;
132cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
133df8bae1dSRodney W. Grimes 
134c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
135c1cd65baSBruce Evans 		int off);
136cfa1ca9dSYoshinobu Inoue #ifdef INET6
1374d77a549SAlfred Perlstein static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
138cfa1ca9dSYoshinobu Inoue #endif
139cfa1ca9dSYoshinobu Inoue 
1404d77a549SAlfred Perlstein static int udp_detach(struct socket *so);
1414d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1424d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes void
145df8bae1dSRodney W. Grimes udp_init()
146df8bae1dSRodney W. Grimes {
147f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
14815bd2b43SDavid Greenman 	LIST_INIT(&udb);
14915bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
150ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1518781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1528781d8e9SBruce Evans 					&udbinfo.porthashmask);
15369c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
15469c2d429SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
15569c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes void
159f0ffb944SJulian Elischer udp_input(m, off)
160df8bae1dSRodney W. Grimes 	register struct mbuf *m;
161f0ffb944SJulian Elischer 	int off;
162df8bae1dSRodney W. Grimes {
163cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
164df8bae1dSRodney W. Grimes 	register struct ip *ip;
165df8bae1dSRodney W. Grimes 	register struct udphdr *uh;
166df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
167df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
168df8bae1dSRodney W. Grimes 	int len;
169df8bae1dSRodney W. Grimes 	struct ip save_ip;
170cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
171bdb3fa18SRobert Watson #ifdef MAC
172bdb3fa18SRobert Watson 	int error;
173bdb3fa18SRobert Watson #endif
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 	/*
206df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
207df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
208df8bae1dSRodney W. Grimes 	 */
209df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
210df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2117eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
212df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
213f76fcf6dSJeffrey Hsu 			goto badunlocked;
214df8bae1dSRodney W. Grimes 		}
215df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
216df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
217df8bae1dSRodney W. Grimes 	}
218df8bae1dSRodney W. Grimes 	/*
219df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
220df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
221df8bae1dSRodney W. Grimes 	 */
22248cb400fSRuslan Ermilov 	if (!blackhole)
223df8bae1dSRodney W. Grimes 		save_ip = *ip;
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes 	/*
226df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
227df8bae1dSRodney W. Grimes 	 */
2286dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
229db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
230db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
231db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
232db4f9cc7SJonathan Lemon 			else
233db4f9cc7SJonathan Lemon 	                	uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
234506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
235db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
236db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
237db4f9cc7SJonathan Lemon 		} else {
238cb342100SHajimu UMEMOTO 			char b[9];
239cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2406effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
241df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
242623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
243cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
244db4f9cc7SJonathan Lemon 		}
245623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
246df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
247df8bae1dSRodney W. Grimes 			m_freem(m);
248df8bae1dSRodney W. Grimes 			return;
249df8bae1dSRodney W. Grimes 		}
250fb9aaba0SRuslan Ermilov 	} else
251fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
252df8bae1dSRodney W. Grimes 
253f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
254f76fcf6dSJeffrey Hsu 
255df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
256df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
25782c23ebaSBill Fenner 		struct inpcb *last;
258df8bae1dSRodney W. Grimes 		/*
259df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
260df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
261df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
262df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
263df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
264df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
265df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
266df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
267df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
268df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
269df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
270df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
271df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
272df8bae1dSRodney W. Grimes 		 */
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes 		/*
275df8bae1dSRodney W. Grimes 		 * Construct sockaddr format source address.
276df8bae1dSRodney W. Grimes 		 */
277df8bae1dSRodney W. Grimes 		udp_in.sin_port = uh->uh_sport;
278df8bae1dSRodney W. Grimes 		udp_in.sin_addr = ip->ip_src;
279df8bae1dSRodney W. Grimes 		/*
280df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
281df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
282df8bae1dSRodney W. Grimes 		 */
283df8bae1dSRodney W. Grimes 		last = NULL;
284cfa1ca9dSYoshinobu Inoue #ifdef INET6
285cfa1ca9dSYoshinobu Inoue 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
286cfa1ca9dSYoshinobu Inoue #endif
287cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
288f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
289f76fcf6dSJeffrey Hsu 			if (inp->inp_lport != uh->uh_dport) {
290f76fcf6dSJeffrey Hsu 		docontinue:
291f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
292f76fcf6dSJeffrey Hsu 				continue;
293f76fcf6dSJeffrey Hsu 			}
294cfa1ca9dSYoshinobu Inoue #ifdef INET6
295369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
296f76fcf6dSJeffrey Hsu 				goto docontinue;
297cfa1ca9dSYoshinobu Inoue #endif
298df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
299f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
300f76fcf6dSJeffrey Hsu 					goto docontinue;
301df8bae1dSRodney W. Grimes 			}
302df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
303df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
304df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
305df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
306f76fcf6dSJeffrey Hsu 					goto docontinue;
307df8bae1dSRodney W. Grimes 			}
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 			if (last != NULL) {
310df8bae1dSRodney W. Grimes 				struct mbuf *n;
311bdb3fa18SRobert Watson 				int policyfail;
312df8bae1dSRodney W. Grimes 
313bdb3fa18SRobert Watson 				policyfail = 0;
314cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
315cfa1ca9dSYoshinobu Inoue 				/* check AH/ESP integrity. */
316bdb3fa18SRobert Watson 				if (ipsec4_in_reject_so(m, last->inp_socket)) {
317cfa1ca9dSYoshinobu Inoue 					ipsecstat.in_polvio++;
318bdb3fa18SRobert Watson 					policyfail = 1;
319cfa1ca9dSYoshinobu Inoue 					/* do not inject data to pcb */
320bdb3fa18SRobert Watson 				}
321cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
322bdb3fa18SRobert Watson #ifdef MAC
323bdb3fa18SRobert Watson 				if (mac_check_socket_receive(last->inp_socket,
324bdb3fa18SRobert Watson 				    m) != 0)
325bdb3fa18SRobert Watson 					policyfail = 1;
326bdb3fa18SRobert Watson #endif
327bdb3fa18SRobert Watson 				if (!policyfail) {
328bdb3fa18SRobert Watson 					if ((n = m_copy(m, 0, M_COPYALL))
329bdb3fa18SRobert Watson 					    != NULL)
330cfa1ca9dSYoshinobu Inoue 						udp_append(last, ip, n,
331cfa1ca9dSYoshinobu Inoue 						   iphlen +
332cfa1ca9dSYoshinobu Inoue 						   sizeof(struct udphdr));
333bdb3fa18SRobert Watson 				}
334f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
335df8bae1dSRodney W. Grimes 			}
33682c23ebaSBill Fenner 			last = inp;
337df8bae1dSRodney W. Grimes 			/*
338df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
339df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
340df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
341df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
342df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
343df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
344df8bae1dSRodney W. Grimes 			 */
3454cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
346df8bae1dSRodney W. Grimes 				break;
347df8bae1dSRodney W. Grimes 		}
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes 		if (last == NULL) {
350df8bae1dSRodney W. Grimes 			/*
351df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
352df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
353df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
354df8bae1dSRodney W. Grimes 			 */
355df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
35661ffc0b1SJeffrey Hsu 			goto badheadlocked;
357df8bae1dSRodney W. Grimes 		}
358cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
359cfa1ca9dSYoshinobu Inoue 		/* check AH/ESP integrity. */
360cfa1ca9dSYoshinobu Inoue 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
361cfa1ca9dSYoshinobu Inoue 			ipsecstat.in_polvio++;
36261ffc0b1SJeffrey Hsu 			goto badheadlocked;
363df8bae1dSRodney W. Grimes 		}
364cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
365f76fcf6dSJeffrey Hsu 		INP_UNLOCK(last);
366f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
367cfa1ca9dSYoshinobu Inoue 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
368df8bae1dSRodney W. Grimes 		return;
369df8bae1dSRodney W. Grimes 	}
370df8bae1dSRodney W. Grimes 	/*
3716d6a026bSDavid Greenman 	 * Locate pcb for datagram.
372df8bae1dSRodney W. Grimes 	 */
373c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
374cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
37515bd2b43SDavid Greenman 	if (inp == NULL) {
37675cfc95fSAndrey A. Chernov 		if (log_in_vain) {
377df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
37875cfc95fSAndrey A. Chernov 
37975cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
380592071e8SBruce Evans 			log(LOG_INFO,
381592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
382592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
383592071e8SBruce Evans 			    ntohs(uh->uh_sport));
38475cfc95fSAndrey A. Chernov 		}
385df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
386df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
387df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
38861ffc0b1SJeffrey Hsu 			goto badheadlocked;
389df8bae1dSRodney W. Grimes 		}
390582a7760SBruce Evans 		if (blackhole)
39161ffc0b1SJeffrey Hsu 			goto badheadlocked;
3921cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
3931cbd978eSLuigi Rizzo 			goto badheadlocked;
39404287599SRuslan Ermilov 		*ip = save_ip;
39504287599SRuslan Ermilov 		ip->ip_len += iphlen;
396582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
397f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
398df8bae1dSRodney W. Grimes 		return;
399df8bae1dSRodney W. Grimes 	}
40061ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
40161ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
402cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
403cfa1ca9dSYoshinobu Inoue 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
404cfa1ca9dSYoshinobu Inoue 		ipsecstat.in_polvio++;
405cfa1ca9dSYoshinobu Inoue 		goto bad;
406cfa1ca9dSYoshinobu Inoue 	}
407cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
408bdb3fa18SRobert Watson #ifdef MAC
409bdb3fa18SRobert Watson 	error = mac_check_socket_receive(inp->inp_socket, m);
410bdb3fa18SRobert Watson 	if (error)
411bdb3fa18SRobert Watson 		goto bad;
412bdb3fa18SRobert Watson #endif
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 	/*
415df8bae1dSRodney W. Grimes 	 * Construct sockaddr format source address.
416df8bae1dSRodney W. Grimes 	 * Stuff source address and datagram in user buffer.
417df8bae1dSRodney W. Grimes 	 */
418df8bae1dSRodney W. Grimes 	udp_in.sin_port = uh->uh_sport;
419df8bae1dSRodney W. Grimes 	udp_in.sin_addr = ip->ip_src;
42082dab6ceSGarrett Wollman 	if (inp->inp_flags & INP_CONTROLOPTS
421cfa1ca9dSYoshinobu Inoue 	    || inp->inp_socket->so_options & SO_TIMESTAMP) {
422cfa1ca9dSYoshinobu Inoue #ifdef INET6
423cfa1ca9dSYoshinobu Inoue 		if (inp->inp_vflag & INP_IPV6) {
424cfa1ca9dSYoshinobu Inoue 			int savedflags;
425cfa1ca9dSYoshinobu Inoue 
426cfa1ca9dSYoshinobu Inoue 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
427cfa1ca9dSYoshinobu Inoue 			savedflags = inp->inp_flags;
428cfa1ca9dSYoshinobu Inoue 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
429cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
430cfa1ca9dSYoshinobu Inoue 			inp->inp_flags = savedflags;
431cfa1ca9dSYoshinobu Inoue 		} else
432cfa1ca9dSYoshinobu Inoue #endif
43382c23ebaSBill Fenner 		ip_savecontrol(inp, &opts, ip, m);
4344cc20ab1SSeigo Tanimura 	}
43533841545SHajimu UMEMOTO  	m_adj(m, iphlen + sizeof(struct udphdr));
436cfa1ca9dSYoshinobu Inoue #ifdef INET6
437cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6) {
438cfa1ca9dSYoshinobu Inoue 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
439cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6;
440cfa1ca9dSYoshinobu Inoue 	} else
441cfa1ca9dSYoshinobu Inoue #endif
442cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
443cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
444df8bae1dSRodney W. Grimes 		udpstat.udps_fullsock++;
445df8bae1dSRodney W. Grimes 		goto bad;
446df8bae1dSRodney W. Grimes 	}
447df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
448f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
449df8bae1dSRodney W. Grimes 	return;
45061ffc0b1SJeffrey Hsu 
45161ffc0b1SJeffrey Hsu badheadlocked:
45261ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
453df8bae1dSRodney W. Grimes bad:
454f76fcf6dSJeffrey Hsu 	if (inp)
455f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
456f76fcf6dSJeffrey Hsu badunlocked:
457df8bae1dSRodney W. Grimes 	m_freem(m);
458df8bae1dSRodney W. Grimes 	if (opts)
459df8bae1dSRodney W. Grimes 		m_freem(opts);
460cfa1ca9dSYoshinobu Inoue 	return;
461cfa1ca9dSYoshinobu Inoue }
462cfa1ca9dSYoshinobu Inoue 
463686cdd19SJun-ichiro itojun Hagino #ifdef INET6
464cfa1ca9dSYoshinobu Inoue static void
465cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(ip6, ip)
466cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr *ip6;
467cfa1ca9dSYoshinobu Inoue 	struct ip *ip;
468cfa1ca9dSYoshinobu Inoue {
469cfa1ca9dSYoshinobu Inoue 	bzero(ip6, sizeof(*ip6));
470cfa1ca9dSYoshinobu Inoue 
471cfa1ca9dSYoshinobu Inoue 	ip6->ip6_vfc = IPV6_VERSION;
472cfa1ca9dSYoshinobu Inoue 	ip6->ip6_plen = ip->ip_len;
473cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt = ip->ip_p;
474cfa1ca9dSYoshinobu Inoue 	ip6->ip6_hlim = ip->ip_ttl;
475cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
476cfa1ca9dSYoshinobu Inoue 		IPV6_ADDR_INT32_SMP;
477cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
478cfa1ca9dSYoshinobu Inoue 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
479cfa1ca9dSYoshinobu Inoue }
480cfa1ca9dSYoshinobu Inoue #endif
481cfa1ca9dSYoshinobu Inoue 
482cfa1ca9dSYoshinobu Inoue /*
483cfa1ca9dSYoshinobu Inoue  * subroutine of udp_input(), mainly for source code readability.
484cfa1ca9dSYoshinobu Inoue  * caller must properly init udp_ip6 and udp_in6 beforehand.
485cfa1ca9dSYoshinobu Inoue  */
486cfa1ca9dSYoshinobu Inoue static void
487cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, off)
488cfa1ca9dSYoshinobu Inoue 	struct inpcb *last;
489cfa1ca9dSYoshinobu Inoue 	struct ip *ip;
490cfa1ca9dSYoshinobu Inoue 	struct mbuf *n;
491cfa1ca9dSYoshinobu Inoue 	int off;
492cfa1ca9dSYoshinobu Inoue {
493cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
494cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
495cfa1ca9dSYoshinobu Inoue 
496cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
497cfa1ca9dSYoshinobu Inoue 	    last->inp_socket->so_options & SO_TIMESTAMP) {
498cfa1ca9dSYoshinobu Inoue #ifdef INET6
499cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
500cfa1ca9dSYoshinobu Inoue 			int savedflags;
501cfa1ca9dSYoshinobu Inoue 
502cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
503cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
504cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
505cfa1ca9dSYoshinobu Inoue 			}
506cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
507cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
508cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
509cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
510cfa1ca9dSYoshinobu Inoue 		} else
511cfa1ca9dSYoshinobu Inoue #endif
512cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
5134cc20ab1SSeigo Tanimura 	}
514cfa1ca9dSYoshinobu Inoue #ifdef INET6
515cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
516cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
517cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
518cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
519cfa1ca9dSYoshinobu Inoue 		}
520cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
521cfa1ca9dSYoshinobu Inoue 	} else
522cfa1ca9dSYoshinobu Inoue #endif
523cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
524cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
525cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
526cfa1ca9dSYoshinobu Inoue 		m_freem(n);
527cfa1ca9dSYoshinobu Inoue 		if (opts)
528cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
529cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5304cc20ab1SSeigo Tanimura 	} else
531cfa1ca9dSYoshinobu Inoue 		sorwakeup(last->inp_socket);
532df8bae1dSRodney W. Grimes }
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes /*
535df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
536df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
537df8bae1dSRodney W. Grimes  */
5383ce144eaSJeffrey Hsu struct inpcb *
539df8bae1dSRodney W. Grimes udp_notify(inp, errno)
540df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
541df8bae1dSRodney W. Grimes 	int errno;
542df8bae1dSRodney W. Grimes {
543df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
544df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
545df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5463ce144eaSJeffrey Hsu 	return inp;
547df8bae1dSRodney W. Grimes }
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes void
550b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip)
551df8bae1dSRodney W. Grimes 	int cmd;
552df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
553b62d102cSBruce Evans 	void *vip;
554df8bae1dSRodney W. Grimes {
555c693a045SJonathan Lemon 	struct ip *ip = vip;
556c693a045SJonathan Lemon 	struct udphdr *uh;
5573ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
558c693a045SJonathan Lemon         struct in_addr faddr;
559c693a045SJonathan Lemon 	struct inpcb *inp;
560c693a045SJonathan Lemon 	int s;
561c693a045SJonathan Lemon 
562c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
563c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
564c693a045SJonathan Lemon         	return;
565df8bae1dSRodney W. Grimes 
566d1c54148SJesper Skriver 	if (PRC_IS_REDIRECT(cmd)) {
567d1c54148SJesper Skriver 		ip = 0;
568d1c54148SJesper Skriver 		notify = in_rtchange;
569d1c54148SJesper Skriver 	} else if (cmd == PRC_HOSTDEAD)
570d1c54148SJesper Skriver 		ip = 0;
571d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
572df8bae1dSRodney W. Grimes 		return;
573df8bae1dSRodney W. Grimes 	if (ip) {
574c693a045SJonathan Lemon 		s = splnet();
575df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
576f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
577c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
578c693a045SJonathan Lemon                     ip->ip_src, uh->uh_sport, 0, NULL);
579f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
580f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
581f76fcf6dSJeffrey Hsu 			if(inp->inp_socket != NULL) {
582c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
583f76fcf6dSJeffrey Hsu 			}
584f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
585f76fcf6dSJeffrey Hsu 		}
586f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
587c693a045SJonathan Lemon 		splx(s);
588df8bae1dSRodney W. Grimes 	} else
589f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
590df8bae1dSRodney W. Grimes }
591df8bae1dSRodney W. Grimes 
5920312fbe9SPoul-Henning Kamp static int
59382d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
59498271db4SGarrett Wollman {
59598271db4SGarrett Wollman 	int error, i, n, s;
59698271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
59798271db4SGarrett Wollman 	inp_gen_t gencnt;
59898271db4SGarrett Wollman 	struct xinpgen xig;
59998271db4SGarrett Wollman 
60098271db4SGarrett Wollman 	/*
60198271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
60298271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
60398271db4SGarrett Wollman 	 */
60498271db4SGarrett Wollman 	if (req->oldptr == 0) {
60598271db4SGarrett Wollman 		n = udbinfo.ipi_count;
60698271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
60798271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
60898271db4SGarrett Wollman 		return 0;
60998271db4SGarrett Wollman 	}
61098271db4SGarrett Wollman 
61198271db4SGarrett Wollman 	if (req->newptr != 0)
61298271db4SGarrett Wollman 		return EPERM;
61398271db4SGarrett Wollman 
61498271db4SGarrett Wollman 	/*
61598271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
61698271db4SGarrett Wollman 	 */
61798271db4SGarrett Wollman 	s = splnet();
61898271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
61998271db4SGarrett Wollman 	n = udbinfo.ipi_count;
62098271db4SGarrett Wollman 	splx(s);
62198271db4SGarrett Wollman 
6225c38b6dbSDon Lewis 	sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6235c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
6245c38b6dbSDon Lewis 
62598271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
62698271db4SGarrett Wollman 	xig.xig_count = n;
62798271db4SGarrett Wollman 	xig.xig_gen = gencnt;
62898271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
62998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
63098271db4SGarrett Wollman 	if (error)
63198271db4SGarrett Wollman 		return error;
63298271db4SGarrett Wollman 
63398271db4SGarrett Wollman 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
63498271db4SGarrett Wollman 	if (inp_list == 0)
63598271db4SGarrett Wollman 		return ENOMEM;
63698271db4SGarrett Wollman 
63798271db4SGarrett Wollman 	s = splnet();
638f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
639fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
640fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
641f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6422ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6432ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
64498271db4SGarrett Wollman 			inp_list[i++] = inp;
645f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6464787fd37SPaul Saab 	}
647f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
64898271db4SGarrett Wollman 	splx(s);
64998271db4SGarrett Wollman 	n = i;
65098271db4SGarrett Wollman 
65198271db4SGarrett Wollman 	error = 0;
65298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
65398271db4SGarrett Wollman 		inp = inp_list[i];
654f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
65598271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
65698271db4SGarrett Wollman 			struct xinpcb xi;
65798271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
65898271db4SGarrett Wollman 			/* XXX should avoid extra copy */
65998271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
66098271db4SGarrett Wollman 			if (inp->inp_socket)
66198271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
66298271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
66398271db4SGarrett Wollman 		}
664f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
66598271db4SGarrett Wollman 	}
66698271db4SGarrett Wollman 	if (!error) {
66798271db4SGarrett Wollman 		/*
66898271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
66998271db4SGarrett Wollman 		 * If the generation differs from what we told
67098271db4SGarrett Wollman 		 * her before, she knows that something happened
67198271db4SGarrett Wollman 		 * while we were processing this request, and it
67298271db4SGarrett Wollman 		 * might be necessary to retry.
67398271db4SGarrett Wollman 		 */
67498271db4SGarrett Wollman 		s = splnet();
675f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
67698271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
67798271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
67898271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
679f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
68098271db4SGarrett Wollman 		splx(s);
68198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
68298271db4SGarrett Wollman 	}
68398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
68498271db4SGarrett Wollman 	return error;
68598271db4SGarrett Wollman }
68698271db4SGarrett Wollman 
68798271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
68898271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
68998271db4SGarrett Wollman 
69098271db4SGarrett Wollman static int
69182d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
692490d50b6SBrian Feldman {
693c0511d3bSBrian Feldman 	struct xucred xuc;
694490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
695490d50b6SBrian Feldman 	struct inpcb *inp;
696490d50b6SBrian Feldman 	int error, s;
697490d50b6SBrian Feldman 
69844731cabSJohn Baldwin 	error = suser_cred(req->td->td_ucred, PRISON_ROOT);
699490d50b6SBrian Feldman 	if (error)
700490d50b6SBrian Feldman 		return (error);
701490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
702490d50b6SBrian Feldman 	if (error)
703490d50b6SBrian Feldman 		return (error);
704490d50b6SBrian Feldman 	s = splnet();
705f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
706490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
707cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
7082d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
709490d50b6SBrian Feldman 		error = ENOENT;
710490d50b6SBrian Feldman 		goto out;
711490d50b6SBrian Feldman 	}
71229dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
7137ce87f12SDavid Malone 	if (error)
7147ce87f12SDavid Malone 		goto out;
71576183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
716490d50b6SBrian Feldman out:
717f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
718490d50b6SBrian Feldman 	splx(s);
7190e1eebb8SDon Lewis 	if (error == 0)
7200e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
721490d50b6SBrian Feldman 	return (error);
722490d50b6SBrian Feldman }
723490d50b6SBrian Feldman 
7247ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7257ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7267ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
727490d50b6SBrian Feldman 
728490d50b6SBrian Feldman static int
729b40ce416SJulian Elischer udp_output(inp, m, addr, control, td)
730df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
731d25f3712SBrian Feldman 	struct mbuf *m;
73257bf258eSGarrett Wollman 	struct sockaddr *addr;
73357bf258eSGarrett Wollman 	struct mbuf *control;
734b40ce416SJulian Elischer 	struct thread *td;
735df8bae1dSRodney W. Grimes {
736df8bae1dSRodney W. Grimes 	register struct udpiphdr *ui;
737df8bae1dSRodney W. Grimes 	register int len = m->m_pkthdr.len;
738df8bae1dSRodney W. Grimes 	struct in_addr laddr;
73975c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
7404cc20ab1SSeigo Tanimura 	int s = 0, error = 0;
741df8bae1dSRodney W. Grimes 
742bdb3fa18SRobert Watson #ifdef MAC
743bdb3fa18SRobert Watson 	mac_create_mbuf_from_socket(inp->inp_socket, m);
744bdb3fa18SRobert Watson #endif
745bdb3fa18SRobert Watson 
746df8bae1dSRodney W. Grimes 	if (control)
747df8bae1dSRodney W. Grimes 		m_freem(control);		/* XXX */
748df8bae1dSRodney W. Grimes 
749430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
750430d30d8SBill Fenner 		error = EMSGSIZE;
751430d30d8SBill Fenner 		goto release;
752430d30d8SBill Fenner 	}
753430d30d8SBill Fenner 
754df8bae1dSRodney W. Grimes 	if (addr) {
75575c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
756a854ed98SJohn Baldwin 		if (td && jailed(td->td_ucred))
757a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
758df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
759df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
760df8bae1dSRodney W. Grimes 			error = EISCONN;
761df8bae1dSRodney W. Grimes 			goto release;
762df8bae1dSRodney W. Grimes 		}
763df8bae1dSRodney W. Grimes 		/*
764df8bae1dSRodney W. Grimes 		 * Must block input while temporarily connected.
765df8bae1dSRodney W. Grimes 		 */
766df8bae1dSRodney W. Grimes 		s = splnet();
767b40ce416SJulian Elischer 		error = in_pcbconnect(inp, addr, td);
768df8bae1dSRodney W. Grimes 		if (error) {
769df8bae1dSRodney W. Grimes 			splx(s);
770df8bae1dSRodney W. Grimes 			goto release;
771df8bae1dSRodney W. Grimes 		}
772df8bae1dSRodney W. Grimes 	} else {
773df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
774df8bae1dSRodney W. Grimes 			error = ENOTCONN;
775df8bae1dSRodney W. Grimes 			goto release;
776df8bae1dSRodney W. Grimes 		}
777df8bae1dSRodney W. Grimes 	}
778df8bae1dSRodney W. Grimes 	/*
779df8bae1dSRodney W. Grimes 	 * Calculate data length and get a mbuf
780df8bae1dSRodney W. Grimes 	 * for UDP and IP headers.
781df8bae1dSRodney W. Grimes 	 */
782df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
783df8bae1dSRodney W. Grimes 	if (m == 0) {
784df8bae1dSRodney W. Grimes 		error = ENOBUFS;
7851c09f774SGarrett Wollman 		if (addr)
7861c09f774SGarrett Wollman 			splx(s);
787df8bae1dSRodney W. Grimes 		goto release;
788df8bae1dSRodney W. Grimes 	}
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	/*
791df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
792df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
793df8bae1dSRodney W. Grimes 	 */
794df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
795db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
796df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
797df8bae1dSRodney W. Grimes 	ui->ui_src = inp->inp_laddr;
798df8bae1dSRodney W. Grimes 	ui->ui_dst = inp->inp_faddr;
799df8bae1dSRodney W. Grimes 	ui->ui_sport = inp->inp_lport;
800df8bae1dSRodney W. Grimes 	ui->ui_dport = inp->inp_fport;
801db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
802df8bae1dSRodney W. Grimes 
803df8bae1dSRodney W. Grimes 	/*
804db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
805df8bae1dSRodney W. Grimes 	 */
806df8bae1dSRodney W. Grimes 	if (udpcksum) {
807db4f9cc7SJonathan Lemon         	ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
808db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
809db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
810db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
811db4f9cc7SJonathan Lemon 	} else {
812db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
813df8bae1dSRodney W. Grimes 	}
814df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
815ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
816ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
817df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
818cfa1ca9dSYoshinobu Inoue 
819cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
82033841545SHajimu UMEMOTO 	if (ipsec_setsocket(m, inp->inp_socket) != 0) {
82133841545SHajimu UMEMOTO 		error = ENOBUFS;
82233841545SHajimu UMEMOTO 		goto release;
82333841545SHajimu UMEMOTO 	}
824cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
825df8bae1dSRodney W. Grimes 	error = ip_output(m, inp->inp_options, &inp->inp_route,
8264cc20ab1SSeigo Tanimura 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
827df8bae1dSRodney W. Grimes 	    inp->inp_moptions);
828df8bae1dSRodney W. Grimes 
829df8bae1dSRodney W. Grimes 	if (addr) {
830df8bae1dSRodney W. Grimes 		in_pcbdisconnect(inp);
83157bf258eSGarrett Wollman 		inp->inp_laddr = laddr;	/* XXX rehash? */
832df8bae1dSRodney W. Grimes 		splx(s);
833df8bae1dSRodney W. Grimes 	}
834df8bae1dSRodney W. Grimes 	return (error);
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes release:
837df8bae1dSRodney W. Grimes 	m_freem(m);
838df8bae1dSRodney W. Grimes 	return (error);
839df8bae1dSRodney W. Grimes }
840df8bae1dSRodney W. Grimes 
84176429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
842df8bae1dSRodney W. Grimes 					/* 40 1K datagrams */
8430312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
8443d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
8450312fbe9SPoul-Henning Kamp 
846cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
847cfa1ca9dSYoshinobu Inoue #ifdef INET6
848cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
849cfa1ca9dSYoshinobu Inoue #else
850cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
851cfa1ca9dSYoshinobu Inoue #endif
852cfa1ca9dSYoshinobu Inoue 				      );
8530312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
8543d177f46SBill Fumerola     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
855df8bae1dSRodney W. Grimes 
856d0390e05SGarrett Wollman static int
857d0390e05SGarrett Wollman udp_abort(struct socket *so)
858df8bae1dSRodney W. Grimes {
859d0390e05SGarrett Wollman 	struct inpcb *inp;
860df8bae1dSRodney W. Grimes 	int s;
861df8bae1dSRodney W. Grimes 
862f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
863d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
864f76fcf6dSJeffrey Hsu 	if (inp == 0) {
865f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
866d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
867f76fcf6dSJeffrey Hsu 	}
868f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
869d0390e05SGarrett Wollman 	soisdisconnected(so);
870d0390e05SGarrett Wollman 	s = splnet();
871d0390e05SGarrett Wollman 	in_pcbdetach(inp);
872f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
873d0390e05SGarrett Wollman 	splx(s);
874d0390e05SGarrett Wollman 	return 0;
875df8bae1dSRodney W. Grimes }
876df8bae1dSRodney W. Grimes 
877d0390e05SGarrett Wollman static int
878b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
879d0390e05SGarrett Wollman {
880d0390e05SGarrett Wollman 	struct inpcb *inp;
881d0390e05SGarrett Wollman 	int s, error;
882d0390e05SGarrett Wollman 
883f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
884d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
885f76fcf6dSJeffrey Hsu 	if (inp != 0) {
886f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
887d0390e05SGarrett Wollman 		return EINVAL;
888f76fcf6dSJeffrey Hsu 	}
889cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
890f76fcf6dSJeffrey Hsu 	if (error) {
891f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
892cfa1ca9dSYoshinobu Inoue 		return error;
893f76fcf6dSJeffrey Hsu 	}
894df8bae1dSRodney W. Grimes 	s = splnet();
895b40ce416SJulian Elischer 	error = in_pcballoc(so, &udbinfo, td);
896df8bae1dSRodney W. Grimes 	splx(s);
897df8bae1dSRodney W. Grimes 	if (error)
898d0390e05SGarrett Wollman 		return error;
899cfa1ca9dSYoshinobu Inoue 
900cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
901f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
902f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
903cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
904cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
905f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
906d0390e05SGarrett Wollman 	return 0;
907df8bae1dSRodney W. Grimes }
908d0390e05SGarrett Wollman 
909d0390e05SGarrett Wollman static int
910b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
911d0390e05SGarrett Wollman {
912d0390e05SGarrett Wollman 	struct inpcb *inp;
913d0390e05SGarrett Wollman 	int s, error;
914d0390e05SGarrett Wollman 
915f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
916d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
917f76fcf6dSJeffrey Hsu 	if (inp == 0) {
918f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
919d0390e05SGarrett Wollman 		return EINVAL;
920f76fcf6dSJeffrey Hsu 	}
921f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
922df8bae1dSRodney W. Grimes 	s = splnet();
923b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
924d0390e05SGarrett Wollman 	splx(s);
925f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
926f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
927d0390e05SGarrett Wollman 	return error;
928d0390e05SGarrett Wollman }
929d0390e05SGarrett Wollman 
930d0390e05SGarrett Wollman static int
931b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
932d0390e05SGarrett Wollman {
933d0390e05SGarrett Wollman 	struct inpcb *inp;
934d0390e05SGarrett Wollman 	int s, error;
93575c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
936d0390e05SGarrett Wollman 
937f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
938d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
939f76fcf6dSJeffrey Hsu 	if (inp == 0) {
940f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
941d0390e05SGarrett Wollman 		return EINVAL;
942f76fcf6dSJeffrey Hsu 	}
943f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
944f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
945f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
946f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
947d0390e05SGarrett Wollman 		return EISCONN;
948f76fcf6dSJeffrey Hsu 	}
949d0390e05SGarrett Wollman 	s = splnet();
95075c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
951a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
952a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
953b40ce416SJulian Elischer 	error = in_pcbconnect(inp, nam, td);
954df8bae1dSRodney W. Grimes 	splx(s);
9554cc20ab1SSeigo Tanimura 	if (error == 0)
956df8bae1dSRodney W. Grimes 		soisconnected(so);
957f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
958f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
959d0390e05SGarrett Wollman 	return error;
960df8bae1dSRodney W. Grimes }
961d0390e05SGarrett Wollman 
962d0390e05SGarrett Wollman static int
963d0390e05SGarrett Wollman udp_detach(struct socket *so)
964d0390e05SGarrett Wollman {
965d0390e05SGarrett Wollman 	struct inpcb *inp;
966d0390e05SGarrett Wollman 	int s;
967d0390e05SGarrett Wollman 
968f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
969d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
970f76fcf6dSJeffrey Hsu 	if (inp == 0) {
971f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
972d0390e05SGarrett Wollman 		return EINVAL;
973f76fcf6dSJeffrey Hsu 	}
974f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
975d0390e05SGarrett Wollman 	s = splnet();
976d0390e05SGarrett Wollman 	in_pcbdetach(inp);
977f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
978d0390e05SGarrett Wollman 	splx(s);
979d0390e05SGarrett Wollman 	return 0;
980d0390e05SGarrett Wollman }
981d0390e05SGarrett Wollman 
982d0390e05SGarrett Wollman static int
983d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
984d0390e05SGarrett Wollman {
985d0390e05SGarrett Wollman 	struct inpcb *inp;
986d0390e05SGarrett Wollman 	int s;
987d0390e05SGarrett Wollman 
988f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
989d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
990f76fcf6dSJeffrey Hsu 	if (inp == 0) {
991f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
992d0390e05SGarrett Wollman 		return EINVAL;
993f76fcf6dSJeffrey Hsu 	}
994f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
995f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
996f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
997f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
998d0390e05SGarrett Wollman 		return ENOTCONN;
999f76fcf6dSJeffrey Hsu 	}
1000d0390e05SGarrett Wollman 
1001df8bae1dSRodney W. Grimes 	s = splnet();
1002df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1003df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1004f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1005f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1006df8bae1dSRodney W. Grimes 	splx(s);
1007df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1008d0390e05SGarrett Wollman 	return 0;
1009df8bae1dSRodney W. Grimes }
1010df8bae1dSRodney W. Grimes 
1011d0390e05SGarrett Wollman static int
101257bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1013b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1014d0390e05SGarrett Wollman {
1015d0390e05SGarrett Wollman 	struct inpcb *inp;
1016f76fcf6dSJeffrey Hsu 	int ret;
1017d0390e05SGarrett Wollman 
1018f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1019d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1020d0390e05SGarrett Wollman 	if (inp == 0) {
1021f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1022d0390e05SGarrett Wollman 		m_freem(m);
1023d0390e05SGarrett Wollman 		return EINVAL;
1024d0390e05SGarrett Wollman 	}
1025f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1026f76fcf6dSJeffrey Hsu 	ret = udp_output(inp, m, addr, control, td);
1027f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1028f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1029f76fcf6dSJeffrey Hsu 	return ret;
1030d0390e05SGarrett Wollman }
1031d0390e05SGarrett Wollman 
103276429de4SYoshinobu Inoue int
1033d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1034d0390e05SGarrett Wollman {
1035d0390e05SGarrett Wollman 	struct inpcb *inp;
1036d0390e05SGarrett Wollman 
1037f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1038d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1039f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1040f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1041d0390e05SGarrett Wollman 		return EINVAL;
1042f76fcf6dSJeffrey Hsu 	}
1043f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1044f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1045d0390e05SGarrett Wollman 	socantsendmore(so);
1046f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1047d0390e05SGarrett Wollman 	return 0;
1048d0390e05SGarrett Wollman }
1049d0390e05SGarrett Wollman 
1050f76fcf6dSJeffrey Hsu /*
1051f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1052f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1053f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1054f76fcf6dSJeffrey Hsu  */
1055f76fcf6dSJeffrey Hsu static int
1056f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1057f76fcf6dSJeffrey Hsu {
1058f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1059f76fcf6dSJeffrey Hsu }
1060f76fcf6dSJeffrey Hsu 
1061f76fcf6dSJeffrey Hsu /*
1062f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1063f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1064f76fcf6dSJeffrey Hsu  */
1065f76fcf6dSJeffrey Hsu static int
1066f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1067f76fcf6dSJeffrey Hsu {
1068f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1069f76fcf6dSJeffrey Hsu }
1070f76fcf6dSJeffrey Hsu 
1071d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1072117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1073117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1074f76fcf6dSJeffrey Hsu 	pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1075117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1076f76fcf6dSJeffrey Hsu 	udp_sockaddr, sosend, soreceive, sopoll
1077d0390e05SGarrett Wollman };
1078