xref: /freebsd/sys/netinet/udp_usrreq.c (revision 97d8d152c28bb596e1c310d9842db5d0314207b2)
1df8bae1dSRodney W. Grimes /*
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
336dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
39bdb3fa18SRobert Watson #include "opt_mac.h"
40cfa1ca9dSYoshinobu Inoue 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
4226f9a767SRodney W. Grimes #include <sys/systm.h>
43960ed29cSSeigo Tanimura #include <sys/domain.h>
44960ed29cSSeigo Tanimura #include <sys/jail.h>
45b110a8a2SGarrett Wollman #include <sys/kernel.h>
46960ed29cSSeigo Tanimura #include <sys/lock.h>
47bdb3fa18SRobert Watson #include <sys/mac.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50490d50b6SBrian Feldman #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
52960ed29cSSeigo Tanimura #include <sys/signalvar.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55960ed29cSSeigo Tanimura #include <sys/sx.h>
56b5e8ce9fSBruce Evans #include <sys/sysctl.h>
57816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
588781d8e9SBruce Evans 
5969c2d429SJeff Roberson #include <vm/uma.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <net/if.h>
62df8bae1dSRodney W. Grimes #include <net/route.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
67960ed29cSSeigo Tanimura #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip.h>
69cfa1ca9dSYoshinobu Inoue #ifdef INET6
70cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
71cfa1ca9dSYoshinobu Inoue #endif
72960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
73960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
75cfa1ca9dSYoshinobu Inoue #ifdef INET6
76cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
77cfa1ca9dSYoshinobu Inoue #endif
78df8bae1dSRodney W. Grimes #include <netinet/udp.h>
79df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
80df8bae1dSRodney W. Grimes 
81b9234fafSSam Leffler #ifdef FAST_IPSEC
82b9234fafSSam Leffler #include <netipsec/ipsec.h>
83b9234fafSSam Leffler #endif /*FAST_IPSEC*/
84b9234fafSSam Leffler 
85cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
86cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
87cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
88cfa1ca9dSYoshinobu Inoue 
89db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
90db4f9cc7SJonathan Lemon 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * UDP protocol implementation.
93df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
94df8bae1dSRodney W. Grimes  */
95df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
960312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
97df8bae1dSRodney W. Grimes #else
980312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
99df8bae1dSRodney W. Grimes #endif
1000312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
1010312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
102df8bae1dSRodney W. Grimes 
103032dcc76SLuigi Rizzo int	log_in_vain = 0;
104816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1053d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
106816a3d83SPoul-Henning Kamp 
107032dcc76SLuigi Rizzo static int	blackhole = 0;
10816f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10916f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
11016f7f31fSGeoff Rehmet 
11183453a06SBruce M Simpson static int	strict_mcast_mship = 0;
11283453a06SBruce M Simpson SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
11383453a06SBruce M Simpson 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
11483453a06SBruce M Simpson 
11576429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
116cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1177a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
11815bd2b43SDavid Greenman 
11915bd2b43SDavid Greenman #ifndef UDBHASHSIZE
120c3229e05SDavid Greenman #define UDBHASHSIZE 16
12115bd2b43SDavid Greenman #endif
12215bd2b43SDavid Greenman 
12376429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
124c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1253d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
126f2ea20e6SGarrett Wollman 
1270312fbe9SPoul-Henning Kamp static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
128cfa1ca9dSYoshinobu Inoue #ifdef INET6
129cfa1ca9dSYoshinobu Inoue struct udp_in6 {
130cfa1ca9dSYoshinobu Inoue 	struct sockaddr_in6	uin6_sin;
131cfa1ca9dSYoshinobu Inoue 	u_char			uin6_init_done : 1;
132cfa1ca9dSYoshinobu Inoue } udp_in6 = {
133cfa1ca9dSYoshinobu Inoue 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
134cfa1ca9dSYoshinobu Inoue 	0
135cfa1ca9dSYoshinobu Inoue };
136cfa1ca9dSYoshinobu Inoue struct udp_ip6 {
137cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr		uip6_ip6;
138cfa1ca9dSYoshinobu Inoue 	u_char			uip6_init_done : 1;
139cfa1ca9dSYoshinobu Inoue } udp_ip6;
140cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
141df8bae1dSRodney W. Grimes 
142c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
143c1cd65baSBruce Evans 		int off);
144cfa1ca9dSYoshinobu Inoue #ifdef INET6
1454d77a549SAlfred Perlstein static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
146cfa1ca9dSYoshinobu Inoue #endif
147cfa1ca9dSYoshinobu Inoue 
1484d77a549SAlfred Perlstein static int udp_detach(struct socket *so);
1494d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1504d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes void
153032dcc76SLuigi Rizzo udp_init()
154df8bae1dSRodney W. Grimes {
155f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
15615bd2b43SDavid Greenman 	LIST_INIT(&udb);
15715bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
158ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1598781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1608781d8e9SBruce Evans 					&udbinfo.porthashmask);
16169c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
16269c2d429SJeff Roberson 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
16369c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
164df8bae1dSRodney W. Grimes }
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes void
167032dcc76SLuigi Rizzo udp_input(m, off)
168032dcc76SLuigi Rizzo 	register struct mbuf *m;
169032dcc76SLuigi Rizzo 	int off;
170df8bae1dSRodney W. Grimes {
171cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
172032dcc76SLuigi Rizzo 	register struct ip *ip;
173032dcc76SLuigi Rizzo 	register struct udphdr *uh;
174032dcc76SLuigi Rizzo 	register struct inpcb *inp;
175df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
176df8bae1dSRodney W. Grimes 	int len;
177df8bae1dSRodney W. Grimes 	struct ip save_ip;
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes 	/*
182df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
183df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
184df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
185df8bae1dSRodney W. Grimes 	 * with options still present.
186df8bae1dSRodney W. Grimes 	 */
187df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
188df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
189df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
190df8bae1dSRodney W. Grimes 	}
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes 	/*
193df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
194df8bae1dSRodney W. Grimes 	 */
195df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
196df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
197df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
198df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
199df8bae1dSRodney W. Grimes 			return;
200df8bae1dSRodney W. Grimes 		}
201df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
202df8bae1dSRodney W. Grimes 	}
203df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
204df8bae1dSRodney W. Grimes 
205686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
206686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
207f76fcf6dSJeffrey Hsu 		goto badunlocked;
208686cdd19SJun-ichiro itojun Hagino 
209df8bae1dSRodney W. Grimes 	/*
210b9234fafSSam Leffler 	 * Construct sockaddr format source address.
211b9234fafSSam Leffler 	 * Stuff source address and datagram in user buffer.
212b9234fafSSam Leffler 	 */
213b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
214b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
215b9234fafSSam Leffler #ifdef INET6
216b9234fafSSam Leffler 	udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
217b9234fafSSam Leffler #endif
218b9234fafSSam Leffler 
219b9234fafSSam Leffler 	/*
220df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
221df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
222df8bae1dSRodney W. Grimes 	 */
223df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
224df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2257eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
226df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
227f76fcf6dSJeffrey Hsu 			goto badunlocked;
228df8bae1dSRodney W. Grimes 		}
229df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
230df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
231df8bae1dSRodney W. Grimes 	}
232df8bae1dSRodney W. Grimes 	/*
233df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
234df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
235df8bae1dSRodney W. Grimes 	 */
23648cb400fSRuslan Ermilov 	if (!blackhole)
237df8bae1dSRodney W. Grimes 		save_ip = *ip;
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 	/*
240df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
241df8bae1dSRodney W. Grimes 	 */
2426dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
243db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
244db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
245db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
246db4f9cc7SJonathan Lemon 			else
247db4f9cc7SJonathan Lemon 	                	uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
248506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
249db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
250db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
251db4f9cc7SJonathan Lemon 		} else {
252cb342100SHajimu UMEMOTO 			char b[9];
253cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2546effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
255df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
256623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
257cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
258db4f9cc7SJonathan Lemon 		}
259623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
260df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
261df8bae1dSRodney W. Grimes 			m_freem(m);
262df8bae1dSRodney W. Grimes 			return;
263df8bae1dSRodney W. Grimes 		}
264fb9aaba0SRuslan Ermilov 	} else
265fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
266df8bae1dSRodney W. Grimes 
267f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
268f76fcf6dSJeffrey Hsu 
269df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
270df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
27182c23ebaSBill Fenner 		struct inpcb *last;
272df8bae1dSRodney W. Grimes 		/*
273df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
274df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
275df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
276df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
277df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
278df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
279df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
280df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
281df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
282df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
283df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
284df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
285df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
286df8bae1dSRodney W. Grimes 		 */
287df8bae1dSRodney W. Grimes 
288df8bae1dSRodney W. Grimes 		/*
289df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
290df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
291df8bae1dSRodney W. Grimes 		 */
292df8bae1dSRodney W. Grimes 		last = NULL;
293cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
294f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
295f76fcf6dSJeffrey Hsu 			if (inp->inp_lport != uh->uh_dport) {
296f76fcf6dSJeffrey Hsu 		docontinue:
297f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
298f76fcf6dSJeffrey Hsu 				continue;
299f76fcf6dSJeffrey Hsu 			}
300cfa1ca9dSYoshinobu Inoue #ifdef INET6
301369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
302f76fcf6dSJeffrey Hsu 				goto docontinue;
303cfa1ca9dSYoshinobu Inoue #endif
304df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
305f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
306f76fcf6dSJeffrey Hsu 					goto docontinue;
307df8bae1dSRodney W. Grimes 			}
308df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
309df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
310df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
311df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
312f76fcf6dSJeffrey Hsu 					goto docontinue;
313df8bae1dSRodney W. Grimes 			}
314df8bae1dSRodney W. Grimes 
31583453a06SBruce M Simpson 			/*
31683453a06SBruce M Simpson 			 * Check multicast packets to make sure they are only
31783453a06SBruce M Simpson 			 * sent to sockets with multicast memberships for the
31883453a06SBruce M Simpson 			 * packet's destination address and arrival interface
31983453a06SBruce M Simpson 			 */
32083453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
32183453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
32283453a06SBruce M Simpson 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
32383453a06SBruce M Simpson 				int mship, foundmship = 0;
32483453a06SBruce M Simpson 
32583453a06SBruce M Simpson 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
32683453a06SBruce M Simpson 					if (MSHIP(inp, mship)->inm_addr.s_addr
32783453a06SBruce M Simpson 					    == ip->ip_dst.s_addr &&
32883453a06SBruce M Simpson 					    MSHIP(inp, mship)->inm_ifp
32983453a06SBruce M Simpson 					    == m->m_pkthdr.rcvif) {
33083453a06SBruce M Simpson 						foundmship = 1;
33183453a06SBruce M Simpson 						break;
33283453a06SBruce M Simpson 					}
33383453a06SBruce M Simpson 				}
33483453a06SBruce M Simpson 				if (foundmship == 0)
33583453a06SBruce M Simpson 					goto docontinue;
33683453a06SBruce M Simpson 			}
33783453a06SBruce M Simpson #undef NMSHIPS
33883453a06SBruce M Simpson #undef MSHIP
339df8bae1dSRodney W. Grimes 			if (last != NULL) {
340df8bae1dSRodney W. Grimes 				struct mbuf *n;
341df8bae1dSRodney W. Grimes 
342032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
343365433d9SRobert Watson 				if (n != NULL)
344cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
345cfa1ca9dSYoshinobu Inoue 						   iphlen +
346cfa1ca9dSYoshinobu Inoue 						   sizeof(struct udphdr));
347f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
348df8bae1dSRodney W. Grimes 			}
34982c23ebaSBill Fenner 			last = inp;
350df8bae1dSRodney W. Grimes 			/*
351df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
352df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
353df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
354df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
355df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
356df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
357df8bae1dSRodney W. Grimes 			 */
3584cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
359df8bae1dSRodney W. Grimes 				break;
360df8bae1dSRodney W. Grimes 		}
361df8bae1dSRodney W. Grimes 
362df8bae1dSRodney W. Grimes 		if (last == NULL) {
363df8bae1dSRodney W. Grimes 			/*
364df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
365df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
366df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
367df8bae1dSRodney W. Grimes 			 */
368df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
36961ffc0b1SJeffrey Hsu 			goto badheadlocked;
370df8bae1dSRodney W. Grimes 		}
371f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
372cfa1ca9dSYoshinobu Inoue 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
3739b657230SSam Leffler 		INP_UNLOCK(last);
374df8bae1dSRodney W. Grimes 		return;
375df8bae1dSRodney W. Grimes 	}
376df8bae1dSRodney W. Grimes 	/*
3776d6a026bSDavid Greenman 	 * Locate pcb for datagram.
378df8bae1dSRodney W. Grimes 	 */
379c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
380cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
38115bd2b43SDavid Greenman 	if (inp == NULL) {
38275cfc95fSAndrey A. Chernov 		if (log_in_vain) {
383df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
38475cfc95fSAndrey A. Chernov 
38575cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
386592071e8SBruce Evans 			log(LOG_INFO,
387592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
388592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
389592071e8SBruce Evans 			    ntohs(uh->uh_sport));
39075cfc95fSAndrey A. Chernov 		}
391df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
392df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
393df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
39461ffc0b1SJeffrey Hsu 			goto badheadlocked;
395df8bae1dSRodney W. Grimes 		}
396582a7760SBruce Evans 		if (blackhole)
39761ffc0b1SJeffrey Hsu 			goto badheadlocked;
3981cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
3991cbd978eSLuigi Rizzo 			goto badheadlocked;
40004287599SRuslan Ermilov 		*ip = save_ip;
40104287599SRuslan Ermilov 		ip->ip_len += iphlen;
402582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
403f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
404df8bae1dSRodney W. Grimes 		return;
405df8bae1dSRodney W. Grimes 	}
40661ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
40761ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
408b9234fafSSam Leffler 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
409f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
410df8bae1dSRodney W. Grimes 	return;
41161ffc0b1SJeffrey Hsu 
41261ffc0b1SJeffrey Hsu badheadlocked:
41361ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
414f76fcf6dSJeffrey Hsu 	if (inp)
415f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
416f76fcf6dSJeffrey Hsu badunlocked:
417df8bae1dSRodney W. Grimes 	m_freem(m);
418df8bae1dSRodney W. Grimes 	if (opts)
419df8bae1dSRodney W. Grimes 		m_freem(opts);
420cfa1ca9dSYoshinobu Inoue 	return;
421cfa1ca9dSYoshinobu Inoue }
422cfa1ca9dSYoshinobu Inoue 
423686cdd19SJun-ichiro itojun Hagino #ifdef INET6
424cfa1ca9dSYoshinobu Inoue static void
425032dcc76SLuigi Rizzo ip_2_ip6_hdr(ip6, ip)
426032dcc76SLuigi Rizzo 	struct ip6_hdr *ip6;
427032dcc76SLuigi Rizzo 	struct ip *ip;
428cfa1ca9dSYoshinobu Inoue {
429cfa1ca9dSYoshinobu Inoue 	bzero(ip6, sizeof(*ip6));
430cfa1ca9dSYoshinobu Inoue 
431cfa1ca9dSYoshinobu Inoue 	ip6->ip6_vfc = IPV6_VERSION;
432cfa1ca9dSYoshinobu Inoue 	ip6->ip6_plen = ip->ip_len;
433cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt = ip->ip_p;
434cfa1ca9dSYoshinobu Inoue 	ip6->ip6_hlim = ip->ip_ttl;
435cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
436cfa1ca9dSYoshinobu Inoue 		IPV6_ADDR_INT32_SMP;
437cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
438cfa1ca9dSYoshinobu Inoue 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
439cfa1ca9dSYoshinobu Inoue }
440cfa1ca9dSYoshinobu Inoue #endif
441cfa1ca9dSYoshinobu Inoue 
442cfa1ca9dSYoshinobu Inoue /*
443cfa1ca9dSYoshinobu Inoue  * subroutine of udp_input(), mainly for source code readability.
444cfa1ca9dSYoshinobu Inoue  * caller must properly init udp_ip6 and udp_in6 beforehand.
445cfa1ca9dSYoshinobu Inoue  */
446cfa1ca9dSYoshinobu Inoue static void
447032dcc76SLuigi Rizzo udp_append(last, ip, n, off)
448032dcc76SLuigi Rizzo 	struct inpcb *last;
449032dcc76SLuigi Rizzo 	struct ip *ip;
450032dcc76SLuigi Rizzo 	struct mbuf *n;
451032dcc76SLuigi Rizzo 	int off;
452cfa1ca9dSYoshinobu Inoue {
453cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
454cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
455cfa1ca9dSYoshinobu Inoue 
456b9234fafSSam Leffler #ifdef IPSEC
457b9234fafSSam Leffler 	/* check AH/ESP integrity. */
458b9234fafSSam Leffler 	if (ipsec4_in_reject_so(n, last->inp_socket)) {
459b9234fafSSam Leffler 		ipsecstat.in_polvio++;
460b9234fafSSam Leffler 		m_freem(n);
461b9234fafSSam Leffler 		return;
462b9234fafSSam Leffler 	}
463b9234fafSSam Leffler #endif /*IPSEC*/
464b9234fafSSam Leffler #ifdef FAST_IPSEC
465b9234fafSSam Leffler 	/* check AH/ESP integrity. */
466b9234fafSSam Leffler 	if (ipsec4_in_reject(n, last)) {
467b9234fafSSam Leffler 		m_freem(n);
468b9234fafSSam Leffler 		return;
469b9234fafSSam Leffler 	}
470b9234fafSSam Leffler #endif /*FAST_IPSEC*/
471b9234fafSSam Leffler #ifdef MAC
472a557af22SRobert Watson 	if (mac_check_inpcb_deliver(last, n) != 0) {
473b9234fafSSam Leffler 		m_freem(n);
474b9234fafSSam Leffler 		return;
475b9234fafSSam Leffler 	}
476b9234fafSSam Leffler #endif
477cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
478cfa1ca9dSYoshinobu Inoue 	    last->inp_socket->so_options & SO_TIMESTAMP) {
479cfa1ca9dSYoshinobu Inoue #ifdef INET6
480cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
481cfa1ca9dSYoshinobu Inoue 			int savedflags;
482cfa1ca9dSYoshinobu Inoue 
483cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
484cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
485cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
486cfa1ca9dSYoshinobu Inoue 			}
487cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
488cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
48911de19f4SHajimu UMEMOTO 			ip6_savecontrol(last, n, &opts);
490cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
491cfa1ca9dSYoshinobu Inoue 		} else
492cfa1ca9dSYoshinobu Inoue #endif
493cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4944cc20ab1SSeigo Tanimura 	}
495cfa1ca9dSYoshinobu Inoue #ifdef INET6
496cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
497cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
498cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
499cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
500cfa1ca9dSYoshinobu Inoue 		}
501cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
502cfa1ca9dSYoshinobu Inoue 	} else
503cfa1ca9dSYoshinobu Inoue #endif
504cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
505cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
506cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
507cfa1ca9dSYoshinobu Inoue 		m_freem(n);
508cfa1ca9dSYoshinobu Inoue 		if (opts)
509cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
510cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5114cc20ab1SSeigo Tanimura 	} else
512cfa1ca9dSYoshinobu Inoue 		sorwakeup(last->inp_socket);
513df8bae1dSRodney W. Grimes }
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes /*
516df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
517df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
518df8bae1dSRodney W. Grimes  */
5193ce144eaSJeffrey Hsu struct inpcb *
520032dcc76SLuigi Rizzo udp_notify(inp, errno)
521032dcc76SLuigi Rizzo 	register struct inpcb *inp;
522032dcc76SLuigi Rizzo 	int errno;
523df8bae1dSRodney W. Grimes {
524df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
525df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
526df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5273ce144eaSJeffrey Hsu 	return inp;
528df8bae1dSRodney W. Grimes }
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes void
531032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip)
532032dcc76SLuigi Rizzo 	int cmd;
533032dcc76SLuigi Rizzo 	struct sockaddr *sa;
534032dcc76SLuigi Rizzo 	void *vip;
535df8bae1dSRodney W. Grimes {
536c693a045SJonathan Lemon 	struct ip *ip = vip;
537c693a045SJonathan Lemon 	struct udphdr *uh;
5383ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
539c693a045SJonathan Lemon         struct in_addr faddr;
540c693a045SJonathan Lemon 	struct inpcb *inp;
541c693a045SJonathan Lemon 	int s;
542c693a045SJonathan Lemon 
543c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
544c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
545c693a045SJonathan Lemon         	return;
546df8bae1dSRodney W. Grimes 
54797d8d152SAndre Oppermann 	/*
54897d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
54997d8d152SAndre Oppermann 	 */
55097d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
55197d8d152SAndre Oppermann 		return;
55297d8d152SAndre Oppermann 	/*
55397d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
55497d8d152SAndre Oppermann 	 * XXX: We never get this from ICMP, otherwise it makes an
55597d8d152SAndre Oppermann 	 * excellent DoS attack on machines with many connections.
55697d8d152SAndre Oppermann 	 */
55797d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
558d1c54148SJesper Skriver 		ip = 0;
559d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
560df8bae1dSRodney W. Grimes 		return;
561df8bae1dSRodney W. Grimes 	if (ip) {
562c693a045SJonathan Lemon 		s = splnet();
563df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
564f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
565c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
566c693a045SJonathan Lemon                     ip->ip_src, uh->uh_sport, 0, NULL);
567f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
568f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
569f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
570c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
571f76fcf6dSJeffrey Hsu 			}
572f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
573f76fcf6dSJeffrey Hsu 		}
574f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
575c693a045SJonathan Lemon 		splx(s);
576df8bae1dSRodney W. Grimes 	} else
577f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
5800312fbe9SPoul-Henning Kamp static int
58182d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
58298271db4SGarrett Wollman {
58398271db4SGarrett Wollman 	int error, i, n, s;
58498271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
58598271db4SGarrett Wollman 	inp_gen_t gencnt;
58698271db4SGarrett Wollman 	struct xinpgen xig;
58798271db4SGarrett Wollman 
58898271db4SGarrett Wollman 	/*
58998271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
59098271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
59198271db4SGarrett Wollman 	 */
59298271db4SGarrett Wollman 	if (req->oldptr == 0) {
59398271db4SGarrett Wollman 		n = udbinfo.ipi_count;
59498271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
59598271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
59698271db4SGarrett Wollman 		return 0;
59798271db4SGarrett Wollman 	}
59898271db4SGarrett Wollman 
59998271db4SGarrett Wollman 	if (req->newptr != 0)
60098271db4SGarrett Wollman 		return EPERM;
60198271db4SGarrett Wollman 
60298271db4SGarrett Wollman 	/*
60398271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
60498271db4SGarrett Wollman 	 */
60598271db4SGarrett Wollman 	s = splnet();
6064b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
60798271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
60898271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6094b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
61098271db4SGarrett Wollman 	splx(s);
61198271db4SGarrett Wollman 
6125c38b6dbSDon Lewis 	sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6135c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
6145c38b6dbSDon Lewis 
61598271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
61698271db4SGarrett Wollman 	xig.xig_count = n;
61798271db4SGarrett Wollman 	xig.xig_gen = gencnt;
61898271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
61998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
62098271db4SGarrett Wollman 	if (error)
62198271db4SGarrett Wollman 		return error;
62298271db4SGarrett Wollman 
623a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
62498271db4SGarrett Wollman 	if (inp_list == 0)
62598271db4SGarrett Wollman 		return ENOMEM;
62698271db4SGarrett Wollman 
62798271db4SGarrett Wollman 	s = splnet();
628f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
629fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
630fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
631f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6322ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6332ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
63498271db4SGarrett Wollman 			inp_list[i++] = inp;
635f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6364787fd37SPaul Saab 	}
637f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
63898271db4SGarrett Wollman 	splx(s);
63998271db4SGarrett Wollman 	n = i;
64098271db4SGarrett Wollman 
64198271db4SGarrett Wollman 	error = 0;
64298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
64398271db4SGarrett Wollman 		inp = inp_list[i];
64498271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
64598271db4SGarrett Wollman 			struct xinpcb xi;
64698271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
64798271db4SGarrett Wollman 			/* XXX should avoid extra copy */
64898271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
64998271db4SGarrett Wollman 			if (inp->inp_socket)
65098271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6514b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
65298271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
65398271db4SGarrett Wollman 		}
65498271db4SGarrett Wollman 	}
65598271db4SGarrett Wollman 	if (!error) {
65698271db4SGarrett Wollman 		/*
65798271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
65898271db4SGarrett Wollman 		 * If the generation differs from what we told
65998271db4SGarrett Wollman 		 * her before, she knows that something happened
66098271db4SGarrett Wollman 		 * while we were processing this request, and it
66198271db4SGarrett Wollman 		 * might be necessary to retry.
66298271db4SGarrett Wollman 		 */
66398271db4SGarrett Wollman 		s = splnet();
664f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
66598271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
66698271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
66798271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
668f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
66998271db4SGarrett Wollman 		splx(s);
67098271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
67198271db4SGarrett Wollman 	}
67298271db4SGarrett Wollman 	free(inp_list, M_TEMP);
67398271db4SGarrett Wollman 	return error;
67498271db4SGarrett Wollman }
67598271db4SGarrett Wollman 
67698271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
67798271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
67898271db4SGarrett Wollman 
67998271db4SGarrett Wollman static int
68082d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
681490d50b6SBrian Feldman {
682c0511d3bSBrian Feldman 	struct xucred xuc;
683490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
684490d50b6SBrian Feldman 	struct inpcb *inp;
685490d50b6SBrian Feldman 	int error, s;
686490d50b6SBrian Feldman 
68744731cabSJohn Baldwin 	error = suser_cred(req->td->td_ucred, PRISON_ROOT);
688490d50b6SBrian Feldman 	if (error)
689490d50b6SBrian Feldman 		return (error);
690490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
691490d50b6SBrian Feldman 	if (error)
692490d50b6SBrian Feldman 		return (error);
693490d50b6SBrian Feldman 	s = splnet();
694f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
695490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
696cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6972d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
698490d50b6SBrian Feldman 		error = ENOENT;
699490d50b6SBrian Feldman 		goto out;
700490d50b6SBrian Feldman 	}
70129dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
7027ce87f12SDavid Malone 	if (error)
7037ce87f12SDavid Malone 		goto out;
70476183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
705490d50b6SBrian Feldman out:
706f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
707490d50b6SBrian Feldman 	splx(s);
7080e1eebb8SDon Lewis 	if (error == 0)
7090e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
710490d50b6SBrian Feldman 	return (error);
711490d50b6SBrian Feldman }
712490d50b6SBrian Feldman 
7137ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7147ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7157ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
716490d50b6SBrian Feldman 
717490d50b6SBrian Feldman static int
718032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
719032dcc76SLuigi Rizzo 	register struct inpcb *inp;
720032dcc76SLuigi Rizzo 	struct mbuf *m;
721032dcc76SLuigi Rizzo 	struct sockaddr *addr;
722032dcc76SLuigi Rizzo 	struct mbuf *control;
723032dcc76SLuigi Rizzo 	struct thread *td;
724df8bae1dSRodney W. Grimes {
725032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
726032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
72790162a4eSIan Dowse 	struct in_addr faddr, laddr;
728c557ae16SIan Dowse 	struct cmsghdr *cm;
729c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
73090162a4eSIan Dowse 	int error = 0;
7318afa2304SBruce M Simpson 	int ipflags;
73290162a4eSIan Dowse 	u_short fport, lport;
733df8bae1dSRodney W. Grimes 
7343c47a187SSam Leffler 	INP_LOCK_ASSERT(inp);
735bdb3fa18SRobert Watson #ifdef MAC
736bdb3fa18SRobert Watson 	mac_create_mbuf_from_socket(inp->inp_socket, m);
737bdb3fa18SRobert Watson #endif
738bdb3fa18SRobert Watson 
739430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
740430d30d8SBill Fenner 		error = EMSGSIZE;
741c557ae16SIan Dowse 		if (control)
742c557ae16SIan Dowse 			m_freem(control);
743430d30d8SBill Fenner 		goto release;
744430d30d8SBill Fenner 	}
745430d30d8SBill Fenner 
746c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
747c557ae16SIan Dowse 	if (control != NULL) {
748c557ae16SIan Dowse 		/*
749c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
750c557ae16SIan Dowse 		 * is stored in a single mbuf.
751c557ae16SIan Dowse 		 */
752c557ae16SIan Dowse 		if (control->m_next) {
753c557ae16SIan Dowse 			error = EINVAL;
754c557ae16SIan Dowse 			m_freem(control);
755c557ae16SIan Dowse 			goto release;
756c557ae16SIan Dowse 		}
757c557ae16SIan Dowse 		for (; control->m_len > 0;
758c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
759c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
760c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
761c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
762c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
763c557ae16SIan Dowse 				error = EINVAL;
764c557ae16SIan Dowse 				break;
765c557ae16SIan Dowse 			}
766c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
767c557ae16SIan Dowse 				continue;
768c557ae16SIan Dowse 
769c557ae16SIan Dowse 			switch (cm->cmsg_type) {
770c557ae16SIan Dowse 			case IP_SENDSRCADDR:
771c557ae16SIan Dowse 				if (cm->cmsg_len !=
772c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
773c557ae16SIan Dowse 					error = EINVAL;
774c557ae16SIan Dowse 					break;
775c557ae16SIan Dowse 				}
776c557ae16SIan Dowse 				bzero(&src, sizeof(src));
777c557ae16SIan Dowse 				src.sin_family = AF_INET;
778c557ae16SIan Dowse 				src.sin_len = sizeof(src);
779c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
780c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
781c557ae16SIan Dowse 				break;
782c557ae16SIan Dowse 			default:
783c557ae16SIan Dowse 				error = ENOPROTOOPT;
784c557ae16SIan Dowse 				break;
785c557ae16SIan Dowse 			}
786c557ae16SIan Dowse 			if (error)
787c557ae16SIan Dowse 				break;
788c557ae16SIan Dowse 		}
789c557ae16SIan Dowse 		m_freem(control);
790c557ae16SIan Dowse 	}
791c557ae16SIan Dowse 	if (error)
792c557ae16SIan Dowse 		goto release;
79390162a4eSIan Dowse 	laddr = inp->inp_laddr;
79490162a4eSIan Dowse 	lport = inp->inp_lport;
795c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
796c557ae16SIan Dowse 		if (lport == 0) {
797c557ae16SIan Dowse 			error = EINVAL;
798c557ae16SIan Dowse 			goto release;
799c557ae16SIan Dowse 		}
800c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
801c557ae16SIan Dowse 		    &laddr.s_addr, &lport, td);
802c557ae16SIan Dowse 		if (error)
803c557ae16SIan Dowse 			goto release;
804c557ae16SIan Dowse 	}
805c557ae16SIan Dowse 
806df8bae1dSRodney W. Grimes 	if (addr) {
80775c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
808a854ed98SJohn Baldwin 		if (td && jailed(td->td_ucred))
809a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
810df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
811df8bae1dSRodney W. Grimes 			error = EISCONN;
812df8bae1dSRodney W. Grimes 			goto release;
813df8bae1dSRodney W. Grimes 		}
81490162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
81590162a4eSIan Dowse 		    &faddr.s_addr, &fport, NULL, td);
81690162a4eSIan Dowse 		if (error)
81790162a4eSIan Dowse 			goto release;
81890162a4eSIan Dowse 
81990162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
82090162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
82190162a4eSIan Dowse 		    inp->inp_lport == 0) {
82290162a4eSIan Dowse 			inp->inp_lport = lport;
82390162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
82490162a4eSIan Dowse 				inp->inp_lport = 0;
82590162a4eSIan Dowse 				error = EAGAIN;
826df8bae1dSRodney W. Grimes 				goto release;
827df8bae1dSRodney W. Grimes 			}
82890162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
82990162a4eSIan Dowse 		}
830df8bae1dSRodney W. Grimes 	} else {
83190162a4eSIan Dowse 		faddr = inp->inp_faddr;
83290162a4eSIan Dowse 		fport = inp->inp_fport;
83390162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
834df8bae1dSRodney W. Grimes 			error = ENOTCONN;
835df8bae1dSRodney W. Grimes 			goto release;
836df8bae1dSRodney W. Grimes 		}
837df8bae1dSRodney W. Grimes 	}
838df8bae1dSRodney W. Grimes 	/*
839df8bae1dSRodney W. Grimes 	 * Calculate data length and get a mbuf
840df8bae1dSRodney W. Grimes 	 * for UDP and IP headers.
841df8bae1dSRodney W. Grimes 	 */
842a163d034SWarner Losh 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
843df8bae1dSRodney W. Grimes 	if (m == 0) {
844df8bae1dSRodney W. Grimes 		error = ENOBUFS;
845df8bae1dSRodney W. Grimes 		goto release;
846df8bae1dSRodney W. Grimes 	}
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	/*
849df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
850df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
851df8bae1dSRodney W. Grimes 	 */
852df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
853db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
854df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
85590162a4eSIan Dowse 	ui->ui_src = laddr;
85690162a4eSIan Dowse 	ui->ui_dst = faddr;
85790162a4eSIan Dowse 	ui->ui_sport = lport;
85890162a4eSIan Dowse 	ui->ui_dport = fport;
859db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
860df8bae1dSRodney W. Grimes 
8618afa2304SBruce M Simpson 	ipflags = inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST);
8628afa2304SBruce M Simpson 	if (inp->inp_flags & INP_ONESBCAST)
8638afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
8648afa2304SBruce M Simpson 
865df8bae1dSRodney W. Grimes 	/*
866db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
867df8bae1dSRodney W. Grimes 	 */
868df8bae1dSRodney W. Grimes 	if (udpcksum) {
8698a538743SBruce M Simpson 		if (inp->inp_flags & INP_ONESBCAST)
8708a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
8718a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
872db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
873db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
874db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
875db4f9cc7SJonathan Lemon 	} else {
876db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
877df8bae1dSRodney W. Grimes 	}
878df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
879ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
880ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
881df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
882cfa1ca9dSYoshinobu Inoue 
88397d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
8845d846453SSam Leffler 	    inp->inp_moptions, inp);
885df8bae1dSRodney W. Grimes 	return (error);
886df8bae1dSRodney W. Grimes 
887df8bae1dSRodney W. Grimes release:
888df8bae1dSRodney W. Grimes 	m_freem(m);
889df8bae1dSRodney W. Grimes 	return (error);
890df8bae1dSRodney W. Grimes }
891df8bae1dSRodney W. Grimes 
89276429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
893032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
8940312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
8953d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
8960312fbe9SPoul-Henning Kamp 
897cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
898cfa1ca9dSYoshinobu Inoue #ifdef INET6
899cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
900cfa1ca9dSYoshinobu Inoue #else
901cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
902cfa1ca9dSYoshinobu Inoue #endif
903cfa1ca9dSYoshinobu Inoue 				      );
9040312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9053d177f46SBill Fumerola     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
906df8bae1dSRodney W. Grimes 
907d0390e05SGarrett Wollman static int
908d0390e05SGarrett Wollman udp_abort(struct socket *so)
909df8bae1dSRodney W. Grimes {
910d0390e05SGarrett Wollman 	struct inpcb *inp;
911df8bae1dSRodney W. Grimes 	int s;
912df8bae1dSRodney W. Grimes 
913f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
914d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
915f76fcf6dSJeffrey Hsu 	if (inp == 0) {
916f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
917d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
918f76fcf6dSJeffrey Hsu 	}
919f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
920d0390e05SGarrett Wollman 	soisdisconnected(so);
921d0390e05SGarrett Wollman 	s = splnet();
922d0390e05SGarrett Wollman 	in_pcbdetach(inp);
923f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
924d0390e05SGarrett Wollman 	splx(s);
925d0390e05SGarrett Wollman 	return 0;
926df8bae1dSRodney W. Grimes }
927df8bae1dSRodney W. Grimes 
928d0390e05SGarrett Wollman static int
929b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
930d0390e05SGarrett Wollman {
931d0390e05SGarrett Wollman 	struct inpcb *inp;
932d0390e05SGarrett Wollman 	int s, error;
933d0390e05SGarrett Wollman 
934f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
935d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
936f76fcf6dSJeffrey Hsu 	if (inp != 0) {
937f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
938d0390e05SGarrett Wollman 		return EINVAL;
939f76fcf6dSJeffrey Hsu 	}
940cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
941f76fcf6dSJeffrey Hsu 	if (error) {
942f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
943cfa1ca9dSYoshinobu Inoue 		return error;
944f76fcf6dSJeffrey Hsu 	}
945df8bae1dSRodney W. Grimes 	s = splnet();
946b40ce416SJulian Elischer 	error = in_pcballoc(so, &udbinfo, td);
947df8bae1dSRodney W. Grimes 	splx(s);
94853b57cd1SSam Leffler 	if (error) {
94953b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
950d0390e05SGarrett Wollman 		return error;
95153b57cd1SSam Leffler 	}
952cfa1ca9dSYoshinobu Inoue 
953cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
954f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
955f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
956cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
957cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
958f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
959d0390e05SGarrett Wollman 	return 0;
960df8bae1dSRodney W. Grimes }
961d0390e05SGarrett Wollman 
962d0390e05SGarrett Wollman static int
963b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
964d0390e05SGarrett Wollman {
965d0390e05SGarrett Wollman 	struct inpcb *inp;
966d0390e05SGarrett Wollman 	int s, error;
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);
975df8bae1dSRodney W. Grimes 	s = splnet();
976b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
977d0390e05SGarrett Wollman 	splx(s);
978f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
979f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
980d0390e05SGarrett Wollman 	return error;
981d0390e05SGarrett Wollman }
982d0390e05SGarrett Wollman 
983d0390e05SGarrett Wollman static int
984b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
985d0390e05SGarrett Wollman {
986d0390e05SGarrett Wollman 	struct inpcb *inp;
987d0390e05SGarrett Wollman 	int s, error;
98875c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
989d0390e05SGarrett Wollman 
990f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
991d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
992f76fcf6dSJeffrey Hsu 	if (inp == 0) {
993f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
994d0390e05SGarrett Wollman 		return EINVAL;
995f76fcf6dSJeffrey Hsu 	}
996f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
997f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
998f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
999f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1000d0390e05SGarrett Wollman 		return EISCONN;
1001f76fcf6dSJeffrey Hsu 	}
1002d0390e05SGarrett Wollman 	s = splnet();
100375c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1004a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
1005a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1006b40ce416SJulian Elischer 	error = in_pcbconnect(inp, nam, td);
1007df8bae1dSRodney W. Grimes 	splx(s);
10084cc20ab1SSeigo Tanimura 	if (error == 0)
1009df8bae1dSRodney W. Grimes 		soisconnected(so);
1010f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1011f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1012d0390e05SGarrett Wollman 	return error;
1013df8bae1dSRodney W. Grimes }
1014d0390e05SGarrett Wollman 
1015d0390e05SGarrett Wollman static int
1016d0390e05SGarrett Wollman udp_detach(struct socket *so)
1017d0390e05SGarrett Wollman {
1018d0390e05SGarrett Wollman 	struct inpcb *inp;
1019d0390e05SGarrett Wollman 	int s;
1020d0390e05SGarrett Wollman 
1021f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1022d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1023f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1024f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1025d0390e05SGarrett Wollman 		return EINVAL;
1026f76fcf6dSJeffrey Hsu 	}
1027f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1028d0390e05SGarrett Wollman 	s = splnet();
1029d0390e05SGarrett Wollman 	in_pcbdetach(inp);
1030f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1031d0390e05SGarrett Wollman 	splx(s);
1032d0390e05SGarrett Wollman 	return 0;
1033d0390e05SGarrett Wollman }
1034d0390e05SGarrett Wollman 
1035d0390e05SGarrett Wollman static int
1036d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1037d0390e05SGarrett Wollman {
1038d0390e05SGarrett Wollman 	struct inpcb *inp;
1039d0390e05SGarrett Wollman 	int s;
1040d0390e05SGarrett Wollman 
1041f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1042d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1043f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1044f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1045d0390e05SGarrett Wollman 		return EINVAL;
1046f76fcf6dSJeffrey Hsu 	}
1047f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1048f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1049f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1050f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1051d0390e05SGarrett Wollman 		return ENOTCONN;
1052f76fcf6dSJeffrey Hsu 	}
1053d0390e05SGarrett Wollman 
1054df8bae1dSRodney W. Grimes 	s = splnet();
1055df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1056df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1057f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1058f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1059df8bae1dSRodney W. Grimes 	splx(s);
1060df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1061d0390e05SGarrett Wollman 	return 0;
1062df8bae1dSRodney W. Grimes }
1063df8bae1dSRodney W. Grimes 
1064d0390e05SGarrett Wollman static int
106557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1066b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1067d0390e05SGarrett Wollman {
1068d0390e05SGarrett Wollman 	struct inpcb *inp;
1069f76fcf6dSJeffrey Hsu 	int ret;
1070d0390e05SGarrett Wollman 
1071f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1072d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1073d0390e05SGarrett Wollman 	if (inp == 0) {
1074f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1075d0390e05SGarrett Wollman 		m_freem(m);
1076d0390e05SGarrett Wollman 		return EINVAL;
1077d0390e05SGarrett Wollman 	}
1078f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1079f76fcf6dSJeffrey Hsu 	ret = udp_output(inp, m, addr, control, td);
1080f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1081f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1082f76fcf6dSJeffrey Hsu 	return ret;
1083d0390e05SGarrett Wollman }
1084d0390e05SGarrett Wollman 
108576429de4SYoshinobu Inoue int
1086d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1087d0390e05SGarrett Wollman {
1088d0390e05SGarrett Wollman 	struct inpcb *inp;
1089d0390e05SGarrett Wollman 
1090f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1091d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1092f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1093f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1094d0390e05SGarrett Wollman 		return EINVAL;
1095f76fcf6dSJeffrey Hsu 	}
1096f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1097f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1098d0390e05SGarrett Wollman 	socantsendmore(so);
1099f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1100d0390e05SGarrett Wollman 	return 0;
1101d0390e05SGarrett Wollman }
1102d0390e05SGarrett Wollman 
1103f76fcf6dSJeffrey Hsu /*
1104f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1105f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1106f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1107f76fcf6dSJeffrey Hsu  */
1108f76fcf6dSJeffrey Hsu static int
1109f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1110f76fcf6dSJeffrey Hsu {
1111f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1112f76fcf6dSJeffrey Hsu }
1113f76fcf6dSJeffrey Hsu 
1114f76fcf6dSJeffrey Hsu /*
1115f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1116f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1117f76fcf6dSJeffrey Hsu  */
1118f76fcf6dSJeffrey Hsu static int
1119f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1120f76fcf6dSJeffrey Hsu {
1121f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1122f76fcf6dSJeffrey Hsu }
1123f76fcf6dSJeffrey Hsu 
1124d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1125117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1126117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1127f76fcf6dSJeffrey Hsu 	pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1128117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1129a557af22SRobert Watson 	udp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
1130d0390e05SGarrett Wollman };
1131