xref: /freebsd/sys/netinet/udp_usrreq.c (revision 47934cef8f8eae0614799485658acebe190602f7)
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 
456da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
457da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
458da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
459b9234fafSSam Leffler #ifdef IPSEC
460b9234fafSSam Leffler 		ipsecstat.in_polvio++;
461b9234fafSSam Leffler #endif /*IPSEC*/
462b9234fafSSam Leffler 		m_freem(n);
463b9234fafSSam Leffler 		return;
464b9234fafSSam Leffler 	}
465da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
466b9234fafSSam Leffler #ifdef MAC
467a557af22SRobert Watson 	if (mac_check_inpcb_deliver(last, n) != 0) {
468b9234fafSSam Leffler 		m_freem(n);
469b9234fafSSam Leffler 		return;
470b9234fafSSam Leffler 	}
471b9234fafSSam Leffler #endif
472cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
473be8a62e8SPoul-Henning Kamp 	    last->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
474cfa1ca9dSYoshinobu Inoue #ifdef INET6
475cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
476cfa1ca9dSYoshinobu Inoue 			int savedflags;
477cfa1ca9dSYoshinobu Inoue 
478cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
479cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
480cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
481cfa1ca9dSYoshinobu Inoue 			}
482cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
483cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
48411de19f4SHajimu UMEMOTO 			ip6_savecontrol(last, n, &opts);
485cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
486cfa1ca9dSYoshinobu Inoue 		} else
487cfa1ca9dSYoshinobu Inoue #endif
488cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4894cc20ab1SSeigo Tanimura 	}
490cfa1ca9dSYoshinobu Inoue #ifdef INET6
491cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
492cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
493cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
494cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
495cfa1ca9dSYoshinobu Inoue 		}
496cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
497cfa1ca9dSYoshinobu Inoue 	} else
498cfa1ca9dSYoshinobu Inoue #endif
499cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
500cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
501cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
502cfa1ca9dSYoshinobu Inoue 		m_freem(n);
503cfa1ca9dSYoshinobu Inoue 		if (opts)
504cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
505cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5064cc20ab1SSeigo Tanimura 	} else
507cfa1ca9dSYoshinobu Inoue 		sorwakeup(last->inp_socket);
508df8bae1dSRodney W. Grimes }
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes /*
511df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
512df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
513df8bae1dSRodney W. Grimes  */
5143ce144eaSJeffrey Hsu struct inpcb *
515032dcc76SLuigi Rizzo udp_notify(inp, errno)
516032dcc76SLuigi Rizzo 	register struct inpcb *inp;
517032dcc76SLuigi Rizzo 	int errno;
518df8bae1dSRodney W. Grimes {
519df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
520df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
521df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5223ce144eaSJeffrey Hsu 	return inp;
523df8bae1dSRodney W. Grimes }
524df8bae1dSRodney W. Grimes 
525df8bae1dSRodney W. Grimes void
526032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip)
527032dcc76SLuigi Rizzo 	int cmd;
528032dcc76SLuigi Rizzo 	struct sockaddr *sa;
529032dcc76SLuigi Rizzo 	void *vip;
530df8bae1dSRodney W. Grimes {
531c693a045SJonathan Lemon 	struct ip *ip = vip;
532c693a045SJonathan Lemon 	struct udphdr *uh;
5333ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
534c693a045SJonathan Lemon         struct in_addr faddr;
535c693a045SJonathan Lemon 	struct inpcb *inp;
536c693a045SJonathan Lemon 	int s;
537c693a045SJonathan Lemon 
538c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
539c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
540c693a045SJonathan Lemon         	return;
541df8bae1dSRodney W. Grimes 
54297d8d152SAndre Oppermann 	/*
54397d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
54497d8d152SAndre Oppermann 	 */
54597d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
54697d8d152SAndre Oppermann 		return;
54797d8d152SAndre Oppermann 	/*
54897d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
54997d8d152SAndre Oppermann 	 * XXX: We never get this from ICMP, otherwise it makes an
55097d8d152SAndre Oppermann 	 * excellent DoS attack on machines with many connections.
55197d8d152SAndre Oppermann 	 */
55297d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
553d1c54148SJesper Skriver 		ip = 0;
554d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
555df8bae1dSRodney W. Grimes 		return;
556df8bae1dSRodney W. Grimes 	if (ip) {
557c693a045SJonathan Lemon 		s = splnet();
558df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
559f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
560c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
561c693a045SJonathan Lemon                     ip->ip_src, uh->uh_sport, 0, NULL);
562f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
563f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
564f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
565c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
566f76fcf6dSJeffrey Hsu 			}
567f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
568f76fcf6dSJeffrey Hsu 		}
569f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
570c693a045SJonathan Lemon 		splx(s);
571df8bae1dSRodney W. Grimes 	} else
572f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
573df8bae1dSRodney W. Grimes }
574df8bae1dSRodney W. Grimes 
5750312fbe9SPoul-Henning Kamp static int
57682d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
57798271db4SGarrett Wollman {
57898271db4SGarrett Wollman 	int error, i, n, s;
57998271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
58098271db4SGarrett Wollman 	inp_gen_t gencnt;
58198271db4SGarrett Wollman 	struct xinpgen xig;
58298271db4SGarrett Wollman 
58398271db4SGarrett Wollman 	/*
58498271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
58598271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
58698271db4SGarrett Wollman 	 */
58798271db4SGarrett Wollman 	if (req->oldptr == 0) {
58898271db4SGarrett Wollman 		n = udbinfo.ipi_count;
58998271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
59098271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
59198271db4SGarrett Wollman 		return 0;
59298271db4SGarrett Wollman 	}
59398271db4SGarrett Wollman 
59498271db4SGarrett Wollman 	if (req->newptr != 0)
59598271db4SGarrett Wollman 		return EPERM;
59698271db4SGarrett Wollman 
59798271db4SGarrett Wollman 	/*
59898271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
59998271db4SGarrett Wollman 	 */
60098271db4SGarrett Wollman 	s = splnet();
6014b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
60298271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
60398271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6044b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
60598271db4SGarrett Wollman 	splx(s);
60698271db4SGarrett Wollman 
60747934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6085c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
60947934cefSDon Lewis 	if (error != 0)
61047934cefSDon Lewis 		return (error);
6115c38b6dbSDon Lewis 
61298271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
61398271db4SGarrett Wollman 	xig.xig_count = n;
61498271db4SGarrett Wollman 	xig.xig_gen = gencnt;
61598271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
61698271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
61798271db4SGarrett Wollman 	if (error)
61898271db4SGarrett Wollman 		return error;
61998271db4SGarrett Wollman 
620a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
62198271db4SGarrett Wollman 	if (inp_list == 0)
62298271db4SGarrett Wollman 		return ENOMEM;
62398271db4SGarrett Wollman 
62498271db4SGarrett Wollman 	s = splnet();
625f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
626fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
627fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
628f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6292ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6302ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
63198271db4SGarrett Wollman 			inp_list[i++] = inp;
632f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6334787fd37SPaul Saab 	}
634f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
63598271db4SGarrett Wollman 	splx(s);
63698271db4SGarrett Wollman 	n = i;
63798271db4SGarrett Wollman 
63898271db4SGarrett Wollman 	error = 0;
63998271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
64098271db4SGarrett Wollman 		inp = inp_list[i];
64198271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
64298271db4SGarrett Wollman 			struct xinpcb xi;
64398271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
64498271db4SGarrett Wollman 			/* XXX should avoid extra copy */
64598271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
64698271db4SGarrett Wollman 			if (inp->inp_socket)
64798271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6484b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
64998271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
65098271db4SGarrett Wollman 		}
65198271db4SGarrett Wollman 	}
65298271db4SGarrett Wollman 	if (!error) {
65398271db4SGarrett Wollman 		/*
65498271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
65598271db4SGarrett Wollman 		 * If the generation differs from what we told
65698271db4SGarrett Wollman 		 * her before, she knows that something happened
65798271db4SGarrett Wollman 		 * while we were processing this request, and it
65898271db4SGarrett Wollman 		 * might be necessary to retry.
65998271db4SGarrett Wollman 		 */
66098271db4SGarrett Wollman 		s = splnet();
661f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
66298271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
66398271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
66498271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
665f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
66698271db4SGarrett Wollman 		splx(s);
66798271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
66898271db4SGarrett Wollman 	}
66998271db4SGarrett Wollman 	free(inp_list, M_TEMP);
67098271db4SGarrett Wollman 	return error;
67198271db4SGarrett Wollman }
67298271db4SGarrett Wollman 
67398271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
67498271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
67598271db4SGarrett Wollman 
67698271db4SGarrett Wollman static int
67782d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
678490d50b6SBrian Feldman {
679c0511d3bSBrian Feldman 	struct xucred xuc;
680490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
681490d50b6SBrian Feldman 	struct inpcb *inp;
682490d50b6SBrian Feldman 	int error, s;
683490d50b6SBrian Feldman 
68444731cabSJohn Baldwin 	error = suser_cred(req->td->td_ucred, PRISON_ROOT);
685490d50b6SBrian Feldman 	if (error)
686490d50b6SBrian Feldman 		return (error);
687490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
688490d50b6SBrian Feldman 	if (error)
689490d50b6SBrian Feldman 		return (error);
690490d50b6SBrian Feldman 	s = splnet();
691f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
692490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
693cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6942d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
695490d50b6SBrian Feldman 		error = ENOENT;
696490d50b6SBrian Feldman 		goto out;
697490d50b6SBrian Feldman 	}
69829dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
6997ce87f12SDavid Malone 	if (error)
7007ce87f12SDavid Malone 		goto out;
70176183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
702490d50b6SBrian Feldman out:
703f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
704490d50b6SBrian Feldman 	splx(s);
7050e1eebb8SDon Lewis 	if (error == 0)
7060e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
707490d50b6SBrian Feldman 	return (error);
708490d50b6SBrian Feldman }
709490d50b6SBrian Feldman 
7107ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7117ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7127ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
713490d50b6SBrian Feldman 
714490d50b6SBrian Feldman static int
715032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
716032dcc76SLuigi Rizzo 	register struct inpcb *inp;
717032dcc76SLuigi Rizzo 	struct mbuf *m;
718032dcc76SLuigi Rizzo 	struct sockaddr *addr;
719032dcc76SLuigi Rizzo 	struct mbuf *control;
720032dcc76SLuigi Rizzo 	struct thread *td;
721df8bae1dSRodney W. Grimes {
722032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
723032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
72490162a4eSIan Dowse 	struct in_addr faddr, laddr;
725c557ae16SIan Dowse 	struct cmsghdr *cm;
726c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
72790162a4eSIan Dowse 	int error = 0;
7288afa2304SBruce M Simpson 	int ipflags;
72990162a4eSIan Dowse 	u_short fport, lport;
730df8bae1dSRodney W. Grimes 
7313c47a187SSam Leffler 	INP_LOCK_ASSERT(inp);
732bdb3fa18SRobert Watson #ifdef MAC
733bdb3fa18SRobert Watson 	mac_create_mbuf_from_socket(inp->inp_socket, m);
734bdb3fa18SRobert Watson #endif
735bdb3fa18SRobert Watson 
736430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
737430d30d8SBill Fenner 		error = EMSGSIZE;
738c557ae16SIan Dowse 		if (control)
739c557ae16SIan Dowse 			m_freem(control);
740430d30d8SBill Fenner 		goto release;
741430d30d8SBill Fenner 	}
742430d30d8SBill Fenner 
743c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
744c557ae16SIan Dowse 	if (control != NULL) {
745c557ae16SIan Dowse 		/*
746c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
747c557ae16SIan Dowse 		 * is stored in a single mbuf.
748c557ae16SIan Dowse 		 */
749c557ae16SIan Dowse 		if (control->m_next) {
750c557ae16SIan Dowse 			error = EINVAL;
751c557ae16SIan Dowse 			m_freem(control);
752c557ae16SIan Dowse 			goto release;
753c557ae16SIan Dowse 		}
754c557ae16SIan Dowse 		for (; control->m_len > 0;
755c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
756c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
757c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
758c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
759c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
760c557ae16SIan Dowse 				error = EINVAL;
761c557ae16SIan Dowse 				break;
762c557ae16SIan Dowse 			}
763c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
764c557ae16SIan Dowse 				continue;
765c557ae16SIan Dowse 
766c557ae16SIan Dowse 			switch (cm->cmsg_type) {
767c557ae16SIan Dowse 			case IP_SENDSRCADDR:
768c557ae16SIan Dowse 				if (cm->cmsg_len !=
769c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
770c557ae16SIan Dowse 					error = EINVAL;
771c557ae16SIan Dowse 					break;
772c557ae16SIan Dowse 				}
773c557ae16SIan Dowse 				bzero(&src, sizeof(src));
774c557ae16SIan Dowse 				src.sin_family = AF_INET;
775c557ae16SIan Dowse 				src.sin_len = sizeof(src);
776c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
777c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
778c557ae16SIan Dowse 				break;
779c557ae16SIan Dowse 			default:
780c557ae16SIan Dowse 				error = ENOPROTOOPT;
781c557ae16SIan Dowse 				break;
782c557ae16SIan Dowse 			}
783c557ae16SIan Dowse 			if (error)
784c557ae16SIan Dowse 				break;
785c557ae16SIan Dowse 		}
786c557ae16SIan Dowse 		m_freem(control);
787c557ae16SIan Dowse 	}
788c557ae16SIan Dowse 	if (error)
789c557ae16SIan Dowse 		goto release;
79090162a4eSIan Dowse 	laddr = inp->inp_laddr;
79190162a4eSIan Dowse 	lport = inp->inp_lport;
792c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
793c557ae16SIan Dowse 		if (lport == 0) {
794c557ae16SIan Dowse 			error = EINVAL;
795c557ae16SIan Dowse 			goto release;
796c557ae16SIan Dowse 		}
797c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
798c557ae16SIan Dowse 		    &laddr.s_addr, &lport, td);
799c557ae16SIan Dowse 		if (error)
800c557ae16SIan Dowse 			goto release;
801c557ae16SIan Dowse 	}
802c557ae16SIan Dowse 
803df8bae1dSRodney W. Grimes 	if (addr) {
80475c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
805a854ed98SJohn Baldwin 		if (td && jailed(td->td_ucred))
806a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
807df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
808df8bae1dSRodney W. Grimes 			error = EISCONN;
809df8bae1dSRodney W. Grimes 			goto release;
810df8bae1dSRodney W. Grimes 		}
81190162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
81290162a4eSIan Dowse 		    &faddr.s_addr, &fport, NULL, td);
81390162a4eSIan Dowse 		if (error)
81490162a4eSIan Dowse 			goto release;
81590162a4eSIan Dowse 
81690162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
81790162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
81890162a4eSIan Dowse 		    inp->inp_lport == 0) {
81990162a4eSIan Dowse 			inp->inp_lport = lport;
82090162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
82190162a4eSIan Dowse 				inp->inp_lport = 0;
82290162a4eSIan Dowse 				error = EAGAIN;
823df8bae1dSRodney W. Grimes 				goto release;
824df8bae1dSRodney W. Grimes 			}
82590162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
82690162a4eSIan Dowse 		}
827df8bae1dSRodney W. Grimes 	} else {
82890162a4eSIan Dowse 		faddr = inp->inp_faddr;
82990162a4eSIan Dowse 		fport = inp->inp_fport;
83090162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
831df8bae1dSRodney W. Grimes 			error = ENOTCONN;
832df8bae1dSRodney W. Grimes 			goto release;
833df8bae1dSRodney W. Grimes 		}
834df8bae1dSRodney W. Grimes 	}
835df8bae1dSRodney W. Grimes 	/*
836df8bae1dSRodney W. Grimes 	 * Calculate data length and get a mbuf
837df8bae1dSRodney W. Grimes 	 * for UDP and IP headers.
838df8bae1dSRodney W. Grimes 	 */
839a163d034SWarner Losh 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
840df8bae1dSRodney W. Grimes 	if (m == 0) {
841df8bae1dSRodney W. Grimes 		error = ENOBUFS;
842df8bae1dSRodney W. Grimes 		goto release;
843df8bae1dSRodney W. Grimes 	}
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	/*
846df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
847df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
848df8bae1dSRodney W. Grimes 	 */
849df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
850db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
851df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
85290162a4eSIan Dowse 	ui->ui_src = laddr;
85390162a4eSIan Dowse 	ui->ui_dst = faddr;
85490162a4eSIan Dowse 	ui->ui_sport = lport;
85590162a4eSIan Dowse 	ui->ui_dport = fport;
856db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
857df8bae1dSRodney W. Grimes 
8588afa2304SBruce M Simpson 	ipflags = inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST);
8598afa2304SBruce M Simpson 	if (inp->inp_flags & INP_ONESBCAST)
8608afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
8618afa2304SBruce M Simpson 
862df8bae1dSRodney W. Grimes 	/*
863db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
864df8bae1dSRodney W. Grimes 	 */
865df8bae1dSRodney W. Grimes 	if (udpcksum) {
8668a538743SBruce M Simpson 		if (inp->inp_flags & INP_ONESBCAST)
8678a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
8688a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
869db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
870db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
871db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
872db4f9cc7SJonathan Lemon 	} else {
873db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
874df8bae1dSRodney W. Grimes 	}
875df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
876ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
877ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
878df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
879cfa1ca9dSYoshinobu Inoue 
88097d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
8815d846453SSam Leffler 	    inp->inp_moptions, inp);
882df8bae1dSRodney W. Grimes 	return (error);
883df8bae1dSRodney W. Grimes 
884df8bae1dSRodney W. Grimes release:
885df8bae1dSRodney W. Grimes 	m_freem(m);
886df8bae1dSRodney W. Grimes 	return (error);
887df8bae1dSRodney W. Grimes }
888df8bae1dSRodney W. Grimes 
88976429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
890032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
8910312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
8923d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
8930312fbe9SPoul-Henning Kamp 
894cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
895cfa1ca9dSYoshinobu Inoue #ifdef INET6
896cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
897cfa1ca9dSYoshinobu Inoue #else
898cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
899cfa1ca9dSYoshinobu Inoue #endif
900cfa1ca9dSYoshinobu Inoue 				      );
9010312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9020ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
903df8bae1dSRodney W. Grimes 
904d0390e05SGarrett Wollman static int
905d0390e05SGarrett Wollman udp_abort(struct socket *so)
906df8bae1dSRodney W. Grimes {
907d0390e05SGarrett Wollman 	struct inpcb *inp;
908df8bae1dSRodney W. Grimes 	int s;
909df8bae1dSRodney W. Grimes 
910f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
911d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
912f76fcf6dSJeffrey Hsu 	if (inp == 0) {
913f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
914d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
915f76fcf6dSJeffrey Hsu 	}
916f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
917d0390e05SGarrett Wollman 	soisdisconnected(so);
918d0390e05SGarrett Wollman 	s = splnet();
919d0390e05SGarrett Wollman 	in_pcbdetach(inp);
920f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
921d0390e05SGarrett Wollman 	splx(s);
922d0390e05SGarrett Wollman 	return 0;
923df8bae1dSRodney W. Grimes }
924df8bae1dSRodney W. Grimes 
925d0390e05SGarrett Wollman static int
926b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
927d0390e05SGarrett Wollman {
928d0390e05SGarrett Wollman 	struct inpcb *inp;
929d0390e05SGarrett Wollman 	int s, error;
930d0390e05SGarrett Wollman 
931f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
932d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
933f76fcf6dSJeffrey Hsu 	if (inp != 0) {
934f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
935d0390e05SGarrett Wollman 		return EINVAL;
936f76fcf6dSJeffrey Hsu 	}
937cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
938f76fcf6dSJeffrey Hsu 	if (error) {
939f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
940cfa1ca9dSYoshinobu Inoue 		return error;
941f76fcf6dSJeffrey Hsu 	}
942df8bae1dSRodney W. Grimes 	s = splnet();
9435bd311a5SSam Leffler 	error = in_pcballoc(so, &udbinfo, td, "udpinp");
944df8bae1dSRodney W. Grimes 	splx(s);
94553b57cd1SSam Leffler 	if (error) {
94653b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
947d0390e05SGarrett Wollman 		return error;
94853b57cd1SSam Leffler 	}
949cfa1ca9dSYoshinobu Inoue 
950cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
951f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
952f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
953cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
954cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
955f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
956d0390e05SGarrett Wollman 	return 0;
957df8bae1dSRodney W. Grimes }
958d0390e05SGarrett Wollman 
959d0390e05SGarrett Wollman static int
960b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
961d0390e05SGarrett Wollman {
962d0390e05SGarrett Wollman 	struct inpcb *inp;
963d0390e05SGarrett Wollman 	int s, error;
964d0390e05SGarrett Wollman 
965f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
966d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
967f76fcf6dSJeffrey Hsu 	if (inp == 0) {
968f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
969d0390e05SGarrett Wollman 		return EINVAL;
970f76fcf6dSJeffrey Hsu 	}
971f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
972df8bae1dSRodney W. Grimes 	s = splnet();
973b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
974d0390e05SGarrett Wollman 	splx(s);
975f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
976f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
977d0390e05SGarrett Wollman 	return error;
978d0390e05SGarrett Wollman }
979d0390e05SGarrett Wollman 
980d0390e05SGarrett Wollman static int
981b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
982d0390e05SGarrett Wollman {
983d0390e05SGarrett Wollman 	struct inpcb *inp;
984d0390e05SGarrett Wollman 	int s, error;
98575c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
986d0390e05SGarrett Wollman 
987f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
988d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
989f76fcf6dSJeffrey Hsu 	if (inp == 0) {
990f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
991d0390e05SGarrett Wollman 		return EINVAL;
992f76fcf6dSJeffrey Hsu 	}
993f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
994f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
995f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
996f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
997d0390e05SGarrett Wollman 		return EISCONN;
998f76fcf6dSJeffrey Hsu 	}
999d0390e05SGarrett Wollman 	s = splnet();
100075c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1001a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
1002a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1003b40ce416SJulian Elischer 	error = in_pcbconnect(inp, nam, td);
1004df8bae1dSRodney W. Grimes 	splx(s);
10054cc20ab1SSeigo Tanimura 	if (error == 0)
1006df8bae1dSRodney W. Grimes 		soisconnected(so);
1007f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1008f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1009d0390e05SGarrett Wollman 	return error;
1010df8bae1dSRodney W. Grimes }
1011d0390e05SGarrett Wollman 
1012d0390e05SGarrett Wollman static int
1013d0390e05SGarrett Wollman udp_detach(struct socket *so)
1014d0390e05SGarrett Wollman {
1015d0390e05SGarrett Wollman 	struct inpcb *inp;
1016d0390e05SGarrett Wollman 	int s;
1017d0390e05SGarrett Wollman 
1018f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1019d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1020f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1021f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1022d0390e05SGarrett Wollman 		return EINVAL;
1023f76fcf6dSJeffrey Hsu 	}
1024f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1025d0390e05SGarrett Wollman 	s = splnet();
1026d0390e05SGarrett Wollman 	in_pcbdetach(inp);
1027f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1028d0390e05SGarrett Wollman 	splx(s);
1029d0390e05SGarrett Wollman 	return 0;
1030d0390e05SGarrett Wollman }
1031d0390e05SGarrett Wollman 
1032d0390e05SGarrett Wollman static int
1033d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1034d0390e05SGarrett Wollman {
1035d0390e05SGarrett Wollman 	struct inpcb *inp;
1036d0390e05SGarrett Wollman 	int s;
1037d0390e05SGarrett Wollman 
1038f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1039d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1040f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1041f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1042d0390e05SGarrett Wollman 		return EINVAL;
1043f76fcf6dSJeffrey Hsu 	}
1044f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1045f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1046f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1047f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1048d0390e05SGarrett Wollman 		return ENOTCONN;
1049f76fcf6dSJeffrey Hsu 	}
1050d0390e05SGarrett Wollman 
1051df8bae1dSRodney W. Grimes 	s = splnet();
1052df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1053df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1054f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1055f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1056df8bae1dSRodney W. Grimes 	splx(s);
1057df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1058d0390e05SGarrett Wollman 	return 0;
1059df8bae1dSRodney W. Grimes }
1060df8bae1dSRodney W. Grimes 
1061d0390e05SGarrett Wollman static int
106257bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1063b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1064d0390e05SGarrett Wollman {
1065d0390e05SGarrett Wollman 	struct inpcb *inp;
1066f76fcf6dSJeffrey Hsu 	int ret;
1067d0390e05SGarrett Wollman 
1068f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1069d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1070d0390e05SGarrett Wollman 	if (inp == 0) {
1071f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1072d0390e05SGarrett Wollman 		m_freem(m);
1073d0390e05SGarrett Wollman 		return EINVAL;
1074d0390e05SGarrett Wollman 	}
1075f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1076f76fcf6dSJeffrey Hsu 	ret = udp_output(inp, m, addr, control, td);
1077f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1078f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1079f76fcf6dSJeffrey Hsu 	return ret;
1080d0390e05SGarrett Wollman }
1081d0390e05SGarrett Wollman 
108276429de4SYoshinobu Inoue int
1083d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1084d0390e05SGarrett Wollman {
1085d0390e05SGarrett Wollman 	struct inpcb *inp;
1086d0390e05SGarrett Wollman 
1087f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1088d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1089f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1090f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1091d0390e05SGarrett Wollman 		return EINVAL;
1092f76fcf6dSJeffrey Hsu 	}
1093f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1094f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1095d0390e05SGarrett Wollman 	socantsendmore(so);
1096f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1097d0390e05SGarrett Wollman 	return 0;
1098d0390e05SGarrett Wollman }
1099d0390e05SGarrett Wollman 
1100f76fcf6dSJeffrey Hsu /*
1101f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1102f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1103f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1104f76fcf6dSJeffrey Hsu  */
1105f76fcf6dSJeffrey Hsu static int
1106f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1107f76fcf6dSJeffrey Hsu {
1108f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1109f76fcf6dSJeffrey Hsu }
1110f76fcf6dSJeffrey Hsu 
1111f76fcf6dSJeffrey Hsu /*
1112f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1113f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1114f76fcf6dSJeffrey Hsu  */
1115f76fcf6dSJeffrey Hsu static int
1116f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1117f76fcf6dSJeffrey Hsu {
1118f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1119f76fcf6dSJeffrey Hsu }
1120f76fcf6dSJeffrey Hsu 
1121d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1122117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1123117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1124f76fcf6dSJeffrey Hsu 	pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1125117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1126a557af22SRobert Watson 	udp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
1127d0390e05SGarrett Wollman };
1128