xref: /freebsd/sys/netinet/udp_usrreq.c (revision 71498f308b2324dbd94e94fd8c4ae41bf4bd663b)
1c398230bSWarner Losh /*-
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
33329b236SRobert Watson  *	The Regents of the University of California.
43329b236SRobert Watson  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
306dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
31c3aac50fSPeter Wemm  * $FreeBSD$
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
340b4ae859SGleb Smirnoff #include "opt_ipfw.h"
356a800098SYoshinobu Inoue #include "opt_ipsec.h"
36cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
37bdb3fa18SRobert Watson #include "opt_mac.h"
38cfa1ca9dSYoshinobu Inoue 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
4026f9a767SRodney W. Grimes #include <sys/systm.h>
41960ed29cSSeigo Tanimura #include <sys/domain.h>
424f590175SPaul Saab #include <sys/eventhandler.h>
43960ed29cSSeigo Tanimura #include <sys/jail.h>
44b110a8a2SGarrett Wollman #include <sys/kernel.h>
45960ed29cSSeigo Tanimura #include <sys/lock.h>
46df8bae1dSRodney W. Grimes #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48acd3428bSRobert Watson #include <sys/priv.h>
49490d50b6SBrian Feldman #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51960ed29cSSeigo Tanimura #include <sys/signalvar.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
53df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
54960ed29cSSeigo Tanimura #include <sys/sx.h>
55b5e8ce9fSBruce Evans #include <sys/sysctl.h>
56816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
578781d8e9SBruce Evans 
5869c2d429SJeff Roberson #include <vm/uma.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
65960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
66960ed29cSSeigo Tanimura #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip.h>
68cfa1ca9dSYoshinobu Inoue #ifdef INET6
69cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
70cfa1ca9dSYoshinobu Inoue #endif
71960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
72960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
74ef39adf0SAndre Oppermann #include <netinet/ip_options.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>
833329b236SRobert Watson #endif
84b9234fafSSam Leffler 
85cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
86cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
873329b236SRobert Watson #endif
88cfa1ca9dSYoshinobu Inoue 
89db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
90db4f9cc7SJonathan Lemon 
91aed55708SRobert Watson #include <security/mac/mac_framework.h>
92aed55708SRobert Watson 
93df8bae1dSRodney W. Grimes /*
94df8bae1dSRodney W. Grimes  * UDP protocol implementation.
95df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
96df8bae1dSRodney W. Grimes  */
9774eb3236SWarner Losh 
9874eb3236SWarner Losh /*
993329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
1003329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
1013329b236SRobert Watson  * packets that would otherwise be discarded by bad checksums may cause
1023329b236SRobert Watson  * problems (especially for NFS data blocks).
10374eb3236SWarner Losh  */
1040312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
1053329b236SRobert Watson SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udpcksum,
1063329b236SRobert Watson     0, "");
107df8bae1dSRodney W. Grimes 
108afdb4274SRobert Watson int	udp_log_in_vain = 0;
109816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
110afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
111816a3d83SPoul-Henning Kamp 
112032dcc76SLuigi Rizzo static int	blackhole = 0;
1133329b236SRobert Watson SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &blackhole, 0,
1143329b236SRobert Watson     "Do not send port unreachables for refused connects");
11516f7f31fSGeoff Rehmet 
11676429de4SYoshinobu Inoue struct inpcbhead	udb;		/* from udp_var.h */
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 */
1243329b236SRobert Watson SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
1253329b236SRobert Watson     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
126f2ea20e6SGarrett Wollman 
127c1cd65baSBruce Evans static void	udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
128d4b509bdSRobert Watson 		    int off, struct sockaddr_in *udp_in);
129cfa1ca9dSYoshinobu Inoue 
130bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1314d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1324d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
133df8bae1dSRodney W. Grimes 
1344f590175SPaul Saab static void
1354f590175SPaul Saab udp_zone_change(void *tag)
1364f590175SPaul Saab {
1374f590175SPaul Saab 
1384f590175SPaul Saab 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1394f590175SPaul Saab }
1404f590175SPaul Saab 
141d915b280SStephan Uphoff static int
142d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
143d915b280SStephan Uphoff {
144af1ee11dSRobert Watson 	struct inpcb *inp;
14508651e1fSJohn Baldwin 
146af1ee11dSRobert Watson 	inp = mem;
147d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
148d915b280SStephan Uphoff 	return (0);
149d915b280SStephan Uphoff }
150d915b280SStephan Uphoff 
151df8bae1dSRodney W. Grimes void
152af1ee11dSRobert Watson udp_init(void)
153df8bae1dSRodney W. Grimes {
154af1ee11dSRobert Watson 
155f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
15615bd2b43SDavid Greenman 	LIST_INIT(&udb);
157712fc218SRobert Watson 	udbinfo.ipi_listhead = &udb;
158712fc218SRobert Watson 	udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
159712fc218SRobert Watson 	    &udbinfo.ipi_hashmask);
160712fc218SRobert Watson 	udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
161712fc218SRobert Watson 	    &udbinfo.ipi_porthashmask);
16269c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
163d915b280SStephan Uphoff 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
16469c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1654f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
1664f590175SPaul Saab 	    EVENTHANDLER_PRI_ANY);
167df8bae1dSRodney W. Grimes }
168df8bae1dSRodney W. Grimes 
169df8bae1dSRodney W. Grimes void
1703329b236SRobert Watson udp_input(struct mbuf *m, int off)
171df8bae1dSRodney W. Grimes {
172cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
1733329b236SRobert Watson 	struct ip *ip;
1743329b236SRobert Watson 	struct udphdr *uh;
17571498f30SBruce M Simpson 	struct ifnet *ifp;
1763329b236SRobert Watson 	struct inpcb *inp;
177df8bae1dSRodney W. Grimes 	int len;
178df8bae1dSRodney W. Grimes 	struct ip save_ip;
179d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
1800b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
1810b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
1820b4ae859SGleb Smirnoff #endif
183df8bae1dSRodney W. Grimes 
18471498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
185df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes 	/*
1883329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
1893329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
1903329b236SRobert Watson 	 * check the checksum with options still present.
191df8bae1dSRodney W. Grimes 	 */
192df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
193df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
194df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
195df8bae1dSRodney W. Grimes 	}
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes 	/*
198df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
199df8bae1dSRodney W. Grimes 	 */
200df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
201df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
202df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
203df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
204df8bae1dSRodney W. Grimes 			return;
205df8bae1dSRodney W. Grimes 		}
206df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
207df8bae1dSRodney W. Grimes 	}
208df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
209df8bae1dSRodney W. Grimes 
2103329b236SRobert Watson 	/*
2113329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
2123329b236SRobert Watson 	 */
213686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
214f76fcf6dSJeffrey Hsu 		goto badunlocked;
215686cdd19SJun-ichiro itojun Hagino 
216df8bae1dSRodney W. Grimes 	/*
2173329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
2183329b236SRobert Watson 	 * and datagram in user buffer.
219b9234fafSSam Leffler 	 */
220d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
221d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
222d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
223b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
224b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
225b9234fafSSam Leffler 
226b9234fafSSam Leffler 	/*
227af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
228af1ee11dSRobert Watson 	 * reflect UDP length, drop.
229df8bae1dSRodney W. Grimes 	 */
230df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
231df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2327eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
233df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
234f76fcf6dSJeffrey Hsu 			goto badunlocked;
235df8bae1dSRodney W. Grimes 		}
236df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
237df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
238df8bae1dSRodney W. Grimes 	}
2393329b236SRobert Watson 
240df8bae1dSRodney W. Grimes 	/*
2413329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
2423329b236SRobert Watson 	 * sending an ICMP error message in response.
243df8bae1dSRodney W. Grimes 	 */
24448cb400fSRuslan Ermilov 	if (!blackhole)
245df8bae1dSRodney W. Grimes 		save_ip = *ip;
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes 	/*
248df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
249df8bae1dSRodney W. Grimes 	 */
2506dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
25139629c92SDavid Malone 		u_short uh_sum;
25239629c92SDavid Malone 
253db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
254db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
25539629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
256db4f9cc7SJonathan Lemon 			else
25739629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
258506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
259db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
26039629c92SDavid Malone 			uh_sum ^= 0xffff;
261db4f9cc7SJonathan Lemon 		} else {
262cb342100SHajimu UMEMOTO 			char b[9];
263af1ee11dSRobert Watson 
264cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2656effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
266df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
26739629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
268cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
269db4f9cc7SJonathan Lemon 		}
27039629c92SDavid Malone 		if (uh_sum) {
271df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
272df8bae1dSRodney W. Grimes 			m_freem(m);
273df8bae1dSRodney W. Grimes 			return;
274df8bae1dSRodney W. Grimes 		}
275fb9aaba0SRuslan Ermilov 	} else
276fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
277df8bae1dSRodney W. Grimes 
2780b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
2793329b236SRobert Watson 	/*
2803329b236SRobert Watson 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
2813329b236SRobert Watson 	 */
2820b4ae859SGleb Smirnoff 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
2830b4ae859SGleb Smirnoff 	if (fwd_tag != NULL) {
2840b4ae859SGleb Smirnoff 		struct sockaddr_in *next_hop;
2850b4ae859SGleb Smirnoff 
2863329b236SRobert Watson 		/*
2873329b236SRobert Watson 		 * Do the hack.
2883329b236SRobert Watson 		 */
2890b4ae859SGleb Smirnoff 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
2900b4ae859SGleb Smirnoff 		ip->ip_dst = next_hop->sin_addr;
2910b4ae859SGleb Smirnoff 		uh->uh_dport = ntohs(next_hop->sin_port);
2923329b236SRobert Watson 
2933329b236SRobert Watson 		/*
2943329b236SRobert Watson 		 * Remove the tag from the packet.  We don't need it anymore.
2953329b236SRobert Watson 		 */
2960b4ae859SGleb Smirnoff 		m_tag_delete(m, fwd_tag);
2970b4ae859SGleb Smirnoff 	}
2980b4ae859SGleb Smirnoff #endif
2990b4ae859SGleb Smirnoff 
300f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
301df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
30271498f30SBruce M Simpson 	    in_broadcast(ip->ip_dst, ifp)) {
30382c23ebaSBill Fenner 		struct inpcb *last;
30471498f30SBruce M Simpson 		struct ip_moptions *imo;
3053329b236SRobert Watson 
306df8bae1dSRodney W. Grimes 		last = NULL;
307cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
3089c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
309f76fcf6dSJeffrey Hsu 				continue;
310cfa1ca9dSYoshinobu Inoue #ifdef INET6
311369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
3129c1df695SRobert Watson 				continue;
313cfa1ca9dSYoshinobu Inoue #endif
31471498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
31571498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3169c1df695SRobert Watson 					continue;
31771498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
31871498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
31971498f30SBruce M Simpson 					continue;
32071498f30SBruce M Simpson 			/*
32171498f30SBruce M Simpson 			 * XXX: Do not check source port of incoming datagram
32271498f30SBruce M Simpson 			 * unless inp_connect() has been called to bind the
32371498f30SBruce M Simpson 			 * fport part of the 4-tuple; the source could be
32471498f30SBruce M Simpson 			 * trying to talk to us with an ephemeral port.
32571498f30SBruce M Simpson 			 */
32671498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
327df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
3289c1df695SRobert Watson 					continue;
32971498f30SBruce M Simpson 
33071498f30SBruce M Simpson 			INP_LOCK(inp);
331df8bae1dSRodney W. Grimes 
33283453a06SBruce M Simpson 			/*
33371498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
33471498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
33583453a06SBruce M Simpson 			 */
33671498f30SBruce M Simpson 			imo = inp->inp_moptions;
33771498f30SBruce M Simpson 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
33871498f30SBruce M Simpson 			    imo != NULL) {
33971498f30SBruce M Simpson 				struct sockaddr_in	 sin;
34071498f30SBruce M Simpson 				struct in_msource	*ims;
34171498f30SBruce M Simpson 				int			 blocked, mode;
34271498f30SBruce M Simpson 				size_t			 idx;
34383453a06SBruce M Simpson 
34471498f30SBruce M Simpson 				bzero(&sin, sizeof(struct sockaddr_in));
34571498f30SBruce M Simpson 				sin.sin_len = sizeof(struct sockaddr_in);
34671498f30SBruce M Simpson 				sin.sin_family = AF_INET;
34771498f30SBruce M Simpson 				sin.sin_addr = ip->ip_dst;
34871498f30SBruce M Simpson 
34971498f30SBruce M Simpson 				blocked = 0;
35071498f30SBruce M Simpson 				idx = imo_match_group(imo, ifp,
35171498f30SBruce M Simpson 				    (struct sockaddr *)&sin);
35271498f30SBruce M Simpson 				if (idx == -1) {
35371498f30SBruce M Simpson 					/*
35471498f30SBruce M Simpson 					 * No group membership for this socket.
35571498f30SBruce M Simpson 					 * Do not bump udps_noportbcast, as
35671498f30SBruce M Simpson 					 * this will happen further down.
35771498f30SBruce M Simpson 					 */
35871498f30SBruce M Simpson 					blocked++;
35971498f30SBruce M Simpson 				} else {
36071498f30SBruce M Simpson 					/*
36171498f30SBruce M Simpson 					 * Check for a multicast source filter
36271498f30SBruce M Simpson 					 * entry on this socket for this group.
36371498f30SBruce M Simpson 					 * MCAST_EXCLUDE is the default
36471498f30SBruce M Simpson 					 * behaviour. It means default accept;
36571498f30SBruce M Simpson 					 * entries, if present, denote sources
36671498f30SBruce M Simpson 					 * to be excluded from delivery.
36771498f30SBruce M Simpson 					 */
36871498f30SBruce M Simpson 					ims = imo_match_source(imo, idx,
36971498f30SBruce M Simpson 					    (struct sockaddr *)&udp_in);
37071498f30SBruce M Simpson 					mode = imo->imo_mfilters[idx].imf_fmode;
37171498f30SBruce M Simpson 					if ((ims != NULL &&
37271498f30SBruce M Simpson 					     mode == MCAST_EXCLUDE) ||
37371498f30SBruce M Simpson 					    (ims == NULL &&
37471498f30SBruce M Simpson 					     mode == MCAST_INCLUDE)) {
37571498f30SBruce M Simpson #ifdef DIAGNOSTIC
37671498f30SBruce M Simpson 						if (bootverbose) {
37771498f30SBruce M Simpson 							printf("%s: blocked by"
37871498f30SBruce M Simpson 							    " source filter\n",
37971498f30SBruce M Simpson 							    __func__);
38071498f30SBruce M Simpson 						}
38171498f30SBruce M Simpson #endif
38271498f30SBruce M Simpson 						udpstat.udps_filtermcast++;
38371498f30SBruce M Simpson 						blocked++;
38483453a06SBruce M Simpson 					}
38583453a06SBruce M Simpson 				}
38671498f30SBruce M Simpson 				if (blocked != 0) {
3879c1df695SRobert Watson 					INP_UNLOCK(inp);
3889c1df695SRobert Watson 					continue;
3899c1df695SRobert Watson 				}
39083453a06SBruce M Simpson 			}
391df8bae1dSRodney W. Grimes 			if (last != NULL) {
392df8bae1dSRodney W. Grimes 				struct mbuf *n;
393df8bae1dSRodney W. Grimes 
394032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
395365433d9SRobert Watson 				if (n != NULL)
3963329b236SRobert Watson 					udp_append(last, ip, n, iphlen +
3973329b236SRobert Watson 					    sizeof(struct udphdr), &udp_in);
398f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
399df8bae1dSRodney W. Grimes 			}
40082c23ebaSBill Fenner 			last = inp;
401df8bae1dSRodney W. Grimes 			/*
402df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
403df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
4043329b236SRobert Watson 			 * socket options set.  This heuristic avoids
4053329b236SRobert Watson 			 * searching through all pcbs in the common case of a
4063329b236SRobert Watson 			 * non-shared port.  It assumes that an application
4073329b236SRobert Watson 			 * will never clear these options after setting them.
408df8bae1dSRodney W. Grimes 			 */
4093329b236SRobert Watson 			if ((last->inp_socket->so_options &
4103329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
411df8bae1dSRodney W. Grimes 				break;
412df8bae1dSRodney W. Grimes 		}
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes 		if (last == NULL) {
415df8bae1dSRodney W. Grimes 			/*
4163329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
4173329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
4183329b236SRobert Watson 			 * or multicast datgram.)
419df8bae1dSRodney W. Grimes 			 */
420df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
42161ffc0b1SJeffrey Hsu 			goto badheadlocked;
422df8bae1dSRodney W. Grimes 		}
423d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
424d4b509bdSRobert Watson 		    &udp_in);
4259b657230SSam Leffler 		INP_UNLOCK(last);
4266b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
427df8bae1dSRodney W. Grimes 		return;
428df8bae1dSRodney W. Grimes 	}
4293329b236SRobert Watson 
430df8bae1dSRodney W. Grimes 	/*
4316d6a026bSDavid Greenman 	 * Locate pcb for datagram.
432df8bae1dSRodney W. Grimes 	 */
433c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
43471498f30SBruce M Simpson 	    ip->ip_dst, uh->uh_dport, 1, ifp);
43515bd2b43SDavid Greenman 	if (inp == NULL) {
436afdb4274SRobert Watson 		if (udp_log_in_vain) {
437df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
43875cfc95fSAndrey A. Chernov 
43975cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
440592071e8SBruce Evans 			log(LOG_INFO,
441592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
442592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
443592071e8SBruce Evans 			    ntohs(uh->uh_sport));
44475cfc95fSAndrey A. Chernov 		}
445df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
446df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
447df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
44861ffc0b1SJeffrey Hsu 			goto badheadlocked;
449df8bae1dSRodney W. Grimes 		}
450582a7760SBruce Evans 		if (blackhole)
45161ffc0b1SJeffrey Hsu 			goto badheadlocked;
4521cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
4531cbd978eSLuigi Rizzo 			goto badheadlocked;
45404287599SRuslan Ermilov 		*ip = save_ip;
45504287599SRuslan Ermilov 		ip->ip_len += iphlen;
456582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
457f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
458df8bae1dSRodney W. Grimes 		return;
459df8bae1dSRodney W. Grimes 	}
4603329b236SRobert Watson 
4613329b236SRobert Watson 	/*
4623329b236SRobert Watson 	 * Check the minimum TTL for socket.
4633329b236SRobert Watson 	 */
46461ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
465936cd18dSAndre Oppermann 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
466936cd18dSAndre Oppermann 		goto badheadlocked;
467d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
468f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
4696b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
470df8bae1dSRodney W. Grimes 	return;
47161ffc0b1SJeffrey Hsu 
47261ffc0b1SJeffrey Hsu badheadlocked:
473f76fcf6dSJeffrey Hsu 	if (inp)
474f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
4756b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
476f76fcf6dSJeffrey Hsu badunlocked:
477df8bae1dSRodney W. Grimes 	m_freem(m);
478cfa1ca9dSYoshinobu Inoue }
479cfa1ca9dSYoshinobu Inoue 
480cfa1ca9dSYoshinobu Inoue /*
481d4b509bdSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
482d4b509bdSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
483d4b509bdSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
484d4b509bdSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
485d4b509bdSRobert Watson  * into the socket code.
486cfa1ca9dSYoshinobu Inoue  */
487cfa1ca9dSYoshinobu Inoue static void
4883329b236SRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
4893329b236SRobert Watson     struct sockaddr_in *udp_in)
490cfa1ca9dSYoshinobu Inoue {
491cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
4921e4d7da7SRobert Watson 	struct socket *so;
493cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
494c83c1318SPoul-Henning Kamp #ifdef INET6
495d4b509bdSRobert Watson 	struct sockaddr_in6 udp_in6;
496c83c1318SPoul-Henning Kamp #endif
497cfa1ca9dSYoshinobu Inoue 
4988e3f3b16SRobert Watson 	INP_LOCK_ASSERT(inp);
49987f2bb8cSRobert Watson 
500da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
501da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
5028e3f3b16SRobert Watson 	if (ipsec4_in_reject(n, inp)) {
503b9234fafSSam Leffler #ifdef IPSEC
504b9234fafSSam Leffler 		ipsecstat.in_polvio++;
5053329b236SRobert Watson #endif
506b9234fafSSam Leffler 		m_freem(n);
507b9234fafSSam Leffler 		return;
508b9234fafSSam Leffler 	}
509da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
510b9234fafSSam Leffler #ifdef MAC
5118e3f3b16SRobert Watson 	if (mac_check_inpcb_deliver(inp, n) != 0) {
512b9234fafSSam Leffler 		m_freem(n);
513b9234fafSSam Leffler 		return;
514b9234fafSSam Leffler 	}
515b9234fafSSam Leffler #endif
5168e3f3b16SRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
5178e3f3b16SRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
518cfa1ca9dSYoshinobu Inoue #ifdef INET6
5198e3f3b16SRobert Watson 		if (inp->inp_vflag & INP_IPV6) {
520cfa1ca9dSYoshinobu Inoue 			int savedflags;
521cfa1ca9dSYoshinobu Inoue 
5228e3f3b16SRobert Watson 			savedflags = inp->inp_flags;
5238e3f3b16SRobert Watson 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
5248e3f3b16SRobert Watson 			ip6_savecontrol(inp, n, &opts);
5258e3f3b16SRobert Watson 			inp->inp_flags = savedflags;
526cfa1ca9dSYoshinobu Inoue 		} else
527cfa1ca9dSYoshinobu Inoue #endif
5288e3f3b16SRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
5294cc20ab1SSeigo Tanimura 	}
530cfa1ca9dSYoshinobu Inoue #ifdef INET6
5318e3f3b16SRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
532d4b509bdSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
533d4b509bdSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
534d4b509bdSRobert Watson 		udp_in6.sin6_family = AF_INET6;
535d4b509bdSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
536d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
537cfa1ca9dSYoshinobu Inoue 	} else
538cfa1ca9dSYoshinobu Inoue #endif
539d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
540cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
5411e4d7da7SRobert Watson 
5428e3f3b16SRobert Watson 	so = inp->inp_socket;
5431e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
5441e4d7da7SRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
545cfa1ca9dSYoshinobu Inoue 		m_freem(n);
546cfa1ca9dSYoshinobu Inoue 		if (opts)
547cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
548cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5491e4d7da7SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
5504cc20ab1SSeigo Tanimura 	} else
5511e4d7da7SRobert Watson 		sorwakeup_locked(so);
552df8bae1dSRodney W. Grimes }
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes /*
5553329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
5563329b236SRobert Watson  * collect error status.
557df8bae1dSRodney W. Grimes  */
5583ce144eaSJeffrey Hsu struct inpcb *
5593329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
560df8bae1dSRodney W. Grimes {
5613329b236SRobert Watson 
562df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
563df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
564df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5653329b236SRobert Watson 	return (inp);
566df8bae1dSRodney W. Grimes }
567df8bae1dSRodney W. Grimes 
568df8bae1dSRodney W. Grimes void
5693329b236SRobert Watson udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
570df8bae1dSRodney W. Grimes {
571c693a045SJonathan Lemon 	struct ip *ip = vip;
572c693a045SJonathan Lemon 	struct udphdr *uh;
5733ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
574c693a045SJonathan Lemon 	struct in_addr faddr;
575c693a045SJonathan Lemon 	struct inpcb *inp;
576c693a045SJonathan Lemon 
577c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
578c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
579c693a045SJonathan Lemon 		return;
580df8bae1dSRodney W. Grimes 
58197d8d152SAndre Oppermann 	/*
58297d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
58397d8d152SAndre Oppermann 	 */
58497d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
58597d8d152SAndre Oppermann 		return;
5863329b236SRobert Watson 
58797d8d152SAndre Oppermann 	/*
58897d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
5893329b236SRobert Watson 	 *
5903329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
5913329b236SRobert Watson 	 * DoS attack on machines with many connections.
59297d8d152SAndre Oppermann 	 */
59397d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
594af1ee11dSRobert Watson 		ip = NULL;
595d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
596df8bae1dSRodney W. Grimes 		return;
597af1ee11dSRobert Watson 	if (ip != NULL) {
598df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
599f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
600c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
601c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
602f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
603f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
604f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
605c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
606f76fcf6dSJeffrey Hsu 			}
607f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
608f76fcf6dSJeffrey Hsu 		}
609f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
610df8bae1dSRodney W. Grimes 	} else
611f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
612df8bae1dSRodney W. Grimes }
613df8bae1dSRodney W. Grimes 
6140312fbe9SPoul-Henning Kamp static int
61582d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
61698271db4SGarrett Wollman {
617277afaffSRobert Watson 	int error, i, n;
61898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
61998271db4SGarrett Wollman 	inp_gen_t gencnt;
62098271db4SGarrett Wollman 	struct xinpgen xig;
62198271db4SGarrett Wollman 
62298271db4SGarrett Wollman 	/*
62398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
62498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
62598271db4SGarrett Wollman 	 */
62698271db4SGarrett Wollman 	if (req->oldptr == 0) {
62798271db4SGarrett Wollman 		n = udbinfo.ipi_count;
62898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
62998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
6303329b236SRobert Watson 		return (0);
63198271db4SGarrett Wollman 	}
63298271db4SGarrett Wollman 
63398271db4SGarrett Wollman 	if (req->newptr != 0)
6343329b236SRobert Watson 		return (EPERM);
63598271db4SGarrett Wollman 
63698271db4SGarrett Wollman 	/*
63798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
63898271db4SGarrett Wollman 	 */
6394b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
64098271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
64198271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6424b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
64398271db4SGarrett Wollman 
64447934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6455c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
64647934cefSDon Lewis 	if (error != 0)
64747934cefSDon Lewis 		return (error);
6485c38b6dbSDon Lewis 
64998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
65098271db4SGarrett Wollman 	xig.xig_count = n;
65198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
65298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
65398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
65498271db4SGarrett Wollman 	if (error)
6553329b236SRobert Watson 		return (error);
65698271db4SGarrett Wollman 
657a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
65898271db4SGarrett Wollman 	if (inp_list == 0)
6593329b236SRobert Watson 		return (ENOMEM);
66098271db4SGarrett Wollman 
661f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
662712fc218SRobert Watson 	for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
663fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
664f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6652ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6662ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
66798271db4SGarrett Wollman 			inp_list[i++] = inp;
668f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6694787fd37SPaul Saab 	}
670f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
67198271db4SGarrett Wollman 	n = i;
67298271db4SGarrett Wollman 
67398271db4SGarrett Wollman 	error = 0;
67498271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
67598271db4SGarrett Wollman 		inp = inp_list[i];
676d915b280SStephan Uphoff 		INP_LOCK(inp);
67798271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
67898271db4SGarrett Wollman 			struct xinpcb xi;
679fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
68098271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
68198271db4SGarrett Wollman 			/* XXX should avoid extra copy */
68298271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
68398271db4SGarrett Wollman 			if (inp->inp_socket)
68498271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6854b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
686d915b280SStephan Uphoff 			INP_UNLOCK(inp);
68798271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
688d915b280SStephan Uphoff 		} else
689d915b280SStephan Uphoff 			INP_UNLOCK(inp);
69098271db4SGarrett Wollman 	}
69198271db4SGarrett Wollman 	if (!error) {
69298271db4SGarrett Wollman 		/*
6933329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
6943329b236SRobert Watson 		 * generation differs from what we told her before, she knows
6953329b236SRobert Watson 		 * that something happened while we were processing this
6963329b236SRobert Watson 		 * request, and it might be necessary to retry.
69798271db4SGarrett Wollman 		 */
698f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
69998271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
70098271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
70198271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
702f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
70398271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
70498271db4SGarrett Wollman 	}
70598271db4SGarrett Wollman 	free(inp_list, M_TEMP);
7063329b236SRobert Watson 	return (error);
70798271db4SGarrett Wollman }
70898271db4SGarrett Wollman 
70998271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
71098271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
71198271db4SGarrett Wollman 
71298271db4SGarrett Wollman static int
71382d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
714490d50b6SBrian Feldman {
715c0511d3bSBrian Feldman 	struct xucred xuc;
716490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
717490d50b6SBrian Feldman 	struct inpcb *inp;
718277afaffSRobert Watson 	int error;
719490d50b6SBrian Feldman 
72032f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
721490d50b6SBrian Feldman 	if (error)
722490d50b6SBrian Feldman 		return (error);
723490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
724490d50b6SBrian Feldman 	if (error)
725490d50b6SBrian Feldman 		return (error);
726f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
727490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
728cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
7292d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
730490d50b6SBrian Feldman 		error = ENOENT;
731490d50b6SBrian Feldman 		goto out;
732490d50b6SBrian Feldman 	}
73329dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
7347ce87f12SDavid Malone 	if (error)
7357ce87f12SDavid Malone 		goto out;
73676183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
737490d50b6SBrian Feldman out:
738f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
7390e1eebb8SDon Lewis 	if (error == 0)
7400e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
741490d50b6SBrian Feldman 	return (error);
742490d50b6SBrian Feldman }
743490d50b6SBrian Feldman 
7447ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7457ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7467ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
747490d50b6SBrian Feldman 
748490d50b6SBrian Feldman static int
7493329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7503329b236SRobert Watson     struct mbuf *control, struct thread *td)
751df8bae1dSRodney W. Grimes {
7523329b236SRobert Watson 	struct udpiphdr *ui;
7533329b236SRobert Watson 	int len = m->m_pkthdr.len;
75490162a4eSIan Dowse 	struct in_addr faddr, laddr;
755c557ae16SIan Dowse 	struct cmsghdr *cm;
756c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
75790162a4eSIan Dowse 	int error = 0;
7588afa2304SBruce M Simpson 	int ipflags;
75990162a4eSIan Dowse 	u_short fport, lport;
7605c32ea65SRobert Watson 	int unlock_udbinfo;
761df8bae1dSRodney W. Grimes 
7625c32ea65SRobert Watson 	/*
7635c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
7645c32ea65SRobert Watson 	 * inpcb.  As such, we don't know up front what inpcb locks we will
7655c32ea65SRobert Watson 	 * need.  Do any work to decide what is needed up front before
7665c32ea65SRobert Watson 	 * acquiring locks.
7675c32ea65SRobert Watson 	 */
768430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
769c557ae16SIan Dowse 		if (control)
770c557ae16SIan Dowse 			m_freem(control);
7715c32ea65SRobert Watson 		m_freem(m);
7723329b236SRobert Watson 		return (EMSGSIZE);
773430d30d8SBill Fenner 	}
774430d30d8SBill Fenner 
7751b7f0384SBruce M Simpson 	src.sin_family = 0;
776c557ae16SIan Dowse 	if (control != NULL) {
777c557ae16SIan Dowse 		/*
7783329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
7793329b236SRobert Watson 		 * stored in a single mbuf.
780c557ae16SIan Dowse 		 */
781c557ae16SIan Dowse 		if (control->m_next) {
782c557ae16SIan Dowse 			m_freem(control);
7835c32ea65SRobert Watson 			m_freem(m);
7843329b236SRobert Watson 			return (EINVAL);
785c557ae16SIan Dowse 		}
786c557ae16SIan Dowse 		for (; control->m_len > 0;
787c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
788c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
789c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
790af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
791af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
792c557ae16SIan Dowse 				error = EINVAL;
793c557ae16SIan Dowse 				break;
794c557ae16SIan Dowse 			}
795c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
796c557ae16SIan Dowse 				continue;
797c557ae16SIan Dowse 
798c557ae16SIan Dowse 			switch (cm->cmsg_type) {
799c557ae16SIan Dowse 			case IP_SENDSRCADDR:
800c557ae16SIan Dowse 				if (cm->cmsg_len !=
801c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
802c557ae16SIan Dowse 					error = EINVAL;
803c557ae16SIan Dowse 					break;
804c557ae16SIan Dowse 				}
805c557ae16SIan Dowse 				bzero(&src, sizeof(src));
806c557ae16SIan Dowse 				src.sin_family = AF_INET;
807c557ae16SIan Dowse 				src.sin_len = sizeof(src);
808c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
809af1ee11dSRobert Watson 				src.sin_addr =
810af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
811c557ae16SIan Dowse 				break;
812af1ee11dSRobert Watson 
813c557ae16SIan Dowse 			default:
814c557ae16SIan Dowse 				error = ENOPROTOOPT;
815c557ae16SIan Dowse 				break;
816c557ae16SIan Dowse 			}
817c557ae16SIan Dowse 			if (error)
818c557ae16SIan Dowse 				break;
819c557ae16SIan Dowse 		}
820c557ae16SIan Dowse 		m_freem(control);
821c557ae16SIan Dowse 	}
8225c32ea65SRobert Watson 	if (error) {
8235c32ea65SRobert Watson 		m_freem(m);
8243329b236SRobert Watson 		return (error);
8255c32ea65SRobert Watson 	}
8265c32ea65SRobert Watson 
8271b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET || addr != NULL) {
8285c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
8295c32ea65SRobert Watson 		unlock_udbinfo = 1;
8305c32ea65SRobert Watson 	} else
8315c32ea65SRobert Watson 		unlock_udbinfo = 0;
8325c32ea65SRobert Watson 	INP_LOCK(inp);
8335c32ea65SRobert Watson 
8345c32ea65SRobert Watson #ifdef MAC
8355c32ea65SRobert Watson 	mac_create_mbuf_from_inpcb(inp, m);
8365c32ea65SRobert Watson #endif
8375c32ea65SRobert Watson 
8381b7f0384SBruce M Simpson 	/*
8391b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
8401b7f0384SBruce M Simpson 	 * source address for this datagram. Its use is invalidated if the
8411b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
8421b7f0384SBruce M Simpson 	 */
84390162a4eSIan Dowse 	laddr = inp->inp_laddr;
84490162a4eSIan Dowse 	lport = inp->inp_lport;
8451b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
8461b7f0384SBruce M Simpson 		if ((lport == 0) ||
8471b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
8481b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
849c557ae16SIan Dowse 			error = EINVAL;
850c557ae16SIan Dowse 			goto release;
851c557ae16SIan Dowse 		}
852c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
853b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
854c557ae16SIan Dowse 		if (error)
855c557ae16SIan Dowse 			goto release;
856c557ae16SIan Dowse 	}
857c557ae16SIan Dowse 
858df8bae1dSRodney W. Grimes 	if (addr) {
85975c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
860812d8653SSam Leffler 		if (jailed(td->td_ucred))
8613329b236SRobert Watson 			prison_remote_ip(td->td_ucred, 0,
8623329b236SRobert Watson 			    &sin->sin_addr.s_addr);
863df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
864df8bae1dSRodney W. Grimes 			error = EISCONN;
865df8bae1dSRodney W. Grimes 			goto release;
866df8bae1dSRodney W. Grimes 		}
86790162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
868b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
86990162a4eSIan Dowse 		if (error)
87090162a4eSIan Dowse 			goto release;
87190162a4eSIan Dowse 
87290162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
87390162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
87490162a4eSIan Dowse 		    inp->inp_lport == 0) {
8753a1757b9SGleb Smirnoff 			/*
8763a1757b9SGleb Smirnoff 			 * Remember addr if jailed, to prevent rebinding.
8773a1757b9SGleb Smirnoff 			 */
8783a1757b9SGleb Smirnoff 			if (jailed(td->td_ucred))
8793a1757b9SGleb Smirnoff 				inp->inp_laddr = laddr;
88090162a4eSIan Dowse 			inp->inp_lport = lport;
88190162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
88290162a4eSIan Dowse 				inp->inp_lport = 0;
88390162a4eSIan Dowse 				error = EAGAIN;
884df8bae1dSRodney W. Grimes 				goto release;
885df8bae1dSRodney W. Grimes 			}
88690162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
88790162a4eSIan Dowse 		}
888df8bae1dSRodney W. Grimes 	} else {
88990162a4eSIan Dowse 		faddr = inp->inp_faddr;
89090162a4eSIan Dowse 		fport = inp->inp_fport;
89190162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
892df8bae1dSRodney W. Grimes 			error = ENOTCONN;
893df8bae1dSRodney W. Grimes 			goto release;
894df8bae1dSRodney W. Grimes 		}
895df8bae1dSRodney W. Grimes 	}
896e6ccd709SRobert Watson 
897df8bae1dSRodney W. Grimes 	/*
898e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
899392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
900392e8407SRobert Watson 	 * since we won't use that space at this layer.
901df8bae1dSRodney W. Grimes 	 */
902e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
903e6ccd709SRobert Watson 	if (m == NULL) {
904df8bae1dSRodney W. Grimes 		error = ENOBUFS;
90549b19bfcSBruce M Simpson 		goto release;
906df8bae1dSRodney W. Grimes 	}
907e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
908e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
909392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes 	/*
9123329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
9133329b236SRobert Watson 	 * into network format.
914df8bae1dSRodney W. Grimes 	 */
915df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
916db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
917df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
91890162a4eSIan Dowse 	ui->ui_src = laddr;
91990162a4eSIan Dowse 	ui->ui_dst = faddr;
92090162a4eSIan Dowse 	ui->ui_sport = lport;
92190162a4eSIan Dowse 	ui->ui_dport = fport;
922db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
923df8bae1dSRodney W. Grimes 
924b2828ad2SAndre Oppermann 	/*
925b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
926b2828ad2SAndre Oppermann 	 */
927b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
928b2828ad2SAndre Oppermann 		struct ip *ip;
9293329b236SRobert Watson 
930b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
931b2828ad2SAndre Oppermann 		ip->ip_off |= IP_DF;
932b2828ad2SAndre Oppermann 	}
933b2828ad2SAndre Oppermann 
934b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
935b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
936b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
937b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
938b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
9396fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
9408afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
9418afa2304SBruce M Simpson 
942df8bae1dSRodney W. Grimes 	/*
943db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
944df8bae1dSRodney W. Grimes 	 */
945df8bae1dSRodney W. Grimes 	if (udpcksum) {
9466fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
9478a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
9488a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
949db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
950db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
951db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
9523329b236SRobert Watson 	} else
953db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
954df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
955ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
956ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
957df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
958cfa1ca9dSYoshinobu Inoue 
9595c32ea65SRobert Watson 	if (unlock_udbinfo)
9605c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
96197d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
9625d846453SSam Leffler 	    inp->inp_moptions, inp);
9635c32ea65SRobert Watson 	INP_UNLOCK(inp);
964df8bae1dSRodney W. Grimes 	return (error);
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes release:
9675c32ea65SRobert Watson 	INP_UNLOCK(inp);
9685c32ea65SRobert Watson 	if (unlock_udbinfo)
9695c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
970df8bae1dSRodney W. Grimes 	m_freem(m);
971df8bae1dSRodney W. Grimes 	return (error);
972df8bae1dSRodney W. Grimes }
973df8bae1dSRodney W. Grimes 
97476429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
975032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
976e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
9773d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
9780312fbe9SPoul-Henning Kamp 
979cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
980cfa1ca9dSYoshinobu Inoue #ifdef INET6
981cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
982cfa1ca9dSYoshinobu Inoue #else
983cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
984cfa1ca9dSYoshinobu Inoue #endif
985cfa1ca9dSYoshinobu Inoue 				      );
986af1ee11dSRobert Watson 
987e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9880ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
989df8bae1dSRodney W. Grimes 
990ac45e92fSRobert Watson static void
991d0390e05SGarrett Wollman udp_abort(struct socket *so)
992df8bae1dSRodney W. Grimes {
993d0390e05SGarrett Wollman 	struct inpcb *inp;
994df8bae1dSRodney W. Grimes 
995d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
99614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
99714ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
998f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
999a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1000a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1001a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1002d0390e05SGarrett Wollman 		soisdisconnected(so);
1003a152f8a3SRobert Watson 	}
1004a152f8a3SRobert Watson 	INP_UNLOCK(inp);
1005f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1006df8bae1dSRodney W. Grimes }
1007df8bae1dSRodney W. Grimes 
1008d0390e05SGarrett Wollman static int
1009b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1010d0390e05SGarrett Wollman {
1011d0390e05SGarrett Wollman 	struct inpcb *inp;
1012277afaffSRobert Watson 	int error;
1013d0390e05SGarrett Wollman 
1014d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
101514ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1016cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1017f24618aaSRobert Watson 	if (error)
10183329b236SRobert Watson 		return (error);
1019f24618aaSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1020d915b280SStephan Uphoff 	error = in_pcballoc(so, &udbinfo);
102153b57cd1SSam Leffler 	if (error) {
102253b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
10233329b236SRobert Watson 		return (error);
102453b57cd1SSam Leffler 	}
1025cfa1ca9dSYoshinobu Inoue 
1026cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
1027f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1028cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1029cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
1030f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
10313329b236SRobert Watson 	return (0);
1032df8bae1dSRodney W. Grimes }
1033d0390e05SGarrett Wollman 
1034d0390e05SGarrett Wollman static int
1035b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1036d0390e05SGarrett Wollman {
1037d0390e05SGarrett Wollman 	struct inpcb *inp;
1038277afaffSRobert Watson 	int error;
1039d0390e05SGarrett Wollman 
1040d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
104114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
104214ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1043f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1044b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1045f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1046f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10473329b236SRobert Watson 	return (error);
1048d0390e05SGarrett Wollman }
1049d0390e05SGarrett Wollman 
1050a152f8a3SRobert Watson static void
1051a152f8a3SRobert Watson udp_close(struct socket *so)
1052a152f8a3SRobert Watson {
1053a152f8a3SRobert Watson 	struct inpcb *inp;
1054a152f8a3SRobert Watson 
1055a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1056a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1057a152f8a3SRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1058a152f8a3SRobert Watson 	INP_LOCK(inp);
1059a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1060a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1061a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1062a152f8a3SRobert Watson 		soisdisconnected(so);
1063a152f8a3SRobert Watson 	}
1064a152f8a3SRobert Watson 	INP_UNLOCK(inp);
1065a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&udbinfo);
1066a152f8a3SRobert Watson }
1067a152f8a3SRobert Watson 
1068d0390e05SGarrett Wollman static int
1069b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1070d0390e05SGarrett Wollman {
1071d0390e05SGarrett Wollman 	struct inpcb *inp;
1072277afaffSRobert Watson 	int error;
107375c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1074d0390e05SGarrett Wollman 
1075d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
107614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
107714ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1078f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1079f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1080f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1081f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
10823329b236SRobert Watson 		return (EISCONN);
1083f76fcf6dSJeffrey Hsu 	}
108475c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1085812d8653SSam Leffler 	if (jailed(td->td_ucred))
1086a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1087b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
10884cc20ab1SSeigo Tanimura 	if (error == 0)
1089df8bae1dSRodney W. Grimes 		soisconnected(so);
1090f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1091f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10923329b236SRobert Watson 	return (error);
1093df8bae1dSRodney W. Grimes }
1094d0390e05SGarrett Wollman 
1095bc725eafSRobert Watson static void
1096d0390e05SGarrett Wollman udp_detach(struct socket *so)
1097d0390e05SGarrett Wollman {
1098d0390e05SGarrett Wollman 	struct inpcb *inp;
1099d0390e05SGarrett Wollman 
1100d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
110114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1102a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1103a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
110414ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1105f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1106d0390e05SGarrett Wollman 	in_pcbdetach(inp);
110714ba8addSRobert Watson 	in_pcbfree(inp);
1108f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1109d0390e05SGarrett Wollman }
1110d0390e05SGarrett Wollman 
1111d0390e05SGarrett Wollman static int
1112d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1113d0390e05SGarrett Wollman {
1114d0390e05SGarrett Wollman 	struct inpcb *inp;
1115d0390e05SGarrett Wollman 
1116d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
111714ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
111814ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1119f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1120f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1121f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1122f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
11233329b236SRobert Watson 		return (ENOTCONN);
1124f76fcf6dSJeffrey Hsu 	}
1125d0390e05SGarrett Wollman 
1126df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1127df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1128d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1129d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1130d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
1131f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1132f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
11333329b236SRobert Watson 	return (0);
1134df8bae1dSRodney W. Grimes }
1135df8bae1dSRodney W. Grimes 
1136d0390e05SGarrett Wollman static int
113757bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1138b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1139d0390e05SGarrett Wollman {
1140d0390e05SGarrett Wollman 	struct inpcb *inp;
1141d0390e05SGarrett Wollman 
1142d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
114314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
11443329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1145d0390e05SGarrett Wollman }
1146d0390e05SGarrett Wollman 
114776429de4SYoshinobu Inoue int
1148d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1149d0390e05SGarrett Wollman {
1150d0390e05SGarrett Wollman 	struct inpcb *inp;
1151d0390e05SGarrett Wollman 
1152d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
115314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1154f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1155d0390e05SGarrett Wollman 	socantsendmore(so);
1156f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
11573329b236SRobert Watson 	return (0);
1158d0390e05SGarrett Wollman }
1159d0390e05SGarrett Wollman 
1160d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1161756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1162756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1163756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1164756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1165756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1166756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1167756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
116854d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1169756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
117059b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1171756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
117254d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1173a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1174a152f8a3SRobert Watson 	.pru_close =		udp_close,
1175d0390e05SGarrett Wollman };
1176