xref: /freebsd/sys/netinet/udp_usrreq.c (revision 3329b236599c2765e7c57d82c718ff5d7c2e28b9)
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 
108032dcc76SLuigi Rizzo int	log_in_vain = 0;
109816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1103d177f46SBill Fumerola     &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 
11683453a06SBruce M Simpson static int	strict_mcast_mship = 0;
11783453a06SBruce M Simpson SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
11883453a06SBruce M Simpson     &strict_mcast_mship, 0, "Only send multicast to member sockets");
11983453a06SBruce M Simpson 
12076429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
1217a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
12215bd2b43SDavid Greenman 
12315bd2b43SDavid Greenman #ifndef UDBHASHSIZE
124c3229e05SDavid Greenman #define UDBHASHSIZE 16
12515bd2b43SDavid Greenman #endif
12615bd2b43SDavid Greenman 
12776429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
1283329b236SRobert Watson SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
1293329b236SRobert Watson     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
130f2ea20e6SGarrett Wollman 
131c1cd65baSBruce Evans static void	udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
132d4b509bdSRobert Watson 		    int off, struct sockaddr_in *udp_in);
133cfa1ca9dSYoshinobu Inoue 
134bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1354d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1364d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
137df8bae1dSRodney W. Grimes 
1384f590175SPaul Saab static void
1394f590175SPaul Saab udp_zone_change(void *tag)
1404f590175SPaul Saab {
1414f590175SPaul Saab 
1424f590175SPaul Saab 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1434f590175SPaul Saab }
1444f590175SPaul Saab 
145d915b280SStephan Uphoff static int
146d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
147d915b280SStephan Uphoff {
14808651e1fSJohn Baldwin 	struct inpcb *inp = mem;
14908651e1fSJohn Baldwin 
150d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
151d915b280SStephan Uphoff 	return (0);
152d915b280SStephan Uphoff }
153d915b280SStephan Uphoff 
154df8bae1dSRodney W. Grimes void
155032dcc76SLuigi Rizzo udp_init()
156df8bae1dSRodney W. Grimes {
157f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
15815bd2b43SDavid Greenman 	LIST_INIT(&udb);
15915bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
160ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1618781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1628781d8e9SBruce Evans 	    &udbinfo.porthashmask);
16369c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
164d915b280SStephan Uphoff 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
16569c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1664f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
1674f590175SPaul Saab 		EVENTHANDLER_PRI_ANY);
168df8bae1dSRodney W. Grimes }
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes void
1713329b236SRobert Watson udp_input(struct mbuf *m, int off)
172df8bae1dSRodney W. Grimes {
173cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
1743329b236SRobert Watson 	struct ip *ip;
1753329b236SRobert Watson 	struct udphdr *uh;
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 
184df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes 	/*
1873329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
1883329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
1893329b236SRobert Watson 	 * check the checksum with options still present.
190df8bae1dSRodney W. Grimes 	 */
191df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
192df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
193df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
194df8bae1dSRodney W. Grimes 	}
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes 	/*
197df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
198df8bae1dSRodney W. Grimes 	 */
199df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
200df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
201df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
202df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
203df8bae1dSRodney W. Grimes 			return;
204df8bae1dSRodney W. Grimes 		}
205df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
206df8bae1dSRodney W. Grimes 	}
207df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
208df8bae1dSRodney W. Grimes 
2093329b236SRobert Watson 	/*
2103329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
2113329b236SRobert Watson 	 */
212686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
213f76fcf6dSJeffrey Hsu 		goto badunlocked;
214686cdd19SJun-ichiro itojun Hagino 
215df8bae1dSRodney W. Grimes 	/*
2163329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
2173329b236SRobert Watson 	 * and datagram in user buffer.
218b9234fafSSam Leffler 	 */
219d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
220d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
221d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
222b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
223b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
224b9234fafSSam Leffler 
225b9234fafSSam Leffler 	/*
226df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
227df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
228df8bae1dSRodney W. Grimes 	 */
229df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
230df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2317eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
232df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
233f76fcf6dSJeffrey Hsu 			goto badunlocked;
234df8bae1dSRodney W. Grimes 		}
235df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
236df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
237df8bae1dSRodney W. Grimes 	}
2383329b236SRobert Watson 
239df8bae1dSRodney W. Grimes 	/*
2403329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
2413329b236SRobert Watson 	 * sending an ICMP error message in response.
242df8bae1dSRodney W. Grimes 	 */
24348cb400fSRuslan Ermilov 	if (!blackhole)
244df8bae1dSRodney W. Grimes 		save_ip = *ip;
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	/*
247df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
248df8bae1dSRodney W. Grimes 	 */
2496dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
250db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
251db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
252db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
253db4f9cc7SJonathan Lemon 			else
254db4f9cc7SJonathan Lemon 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
255506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
256db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
257db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
258db4f9cc7SJonathan Lemon 		} else {
259cb342100SHajimu UMEMOTO 			char b[9];
260cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2616effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
262df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
263623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
264cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
265db4f9cc7SJonathan Lemon 		}
266623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
267df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
268df8bae1dSRodney W. Grimes 			m_freem(m);
269df8bae1dSRodney W. Grimes 			return;
270df8bae1dSRodney W. Grimes 		}
271fb9aaba0SRuslan Ermilov 	} else
272fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
273df8bae1dSRodney W. Grimes 
2740b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
2753329b236SRobert Watson 	/*
2763329b236SRobert Watson 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
2773329b236SRobert Watson 	 */
2780b4ae859SGleb Smirnoff 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
2790b4ae859SGleb Smirnoff 	if (fwd_tag != NULL) {
2800b4ae859SGleb Smirnoff 		struct sockaddr_in *next_hop;
2810b4ae859SGleb Smirnoff 
2823329b236SRobert Watson 		/*
2833329b236SRobert Watson 		 * Do the hack.
2843329b236SRobert Watson 		 */
2850b4ae859SGleb Smirnoff 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
2860b4ae859SGleb Smirnoff 		ip->ip_dst = next_hop->sin_addr;
2870b4ae859SGleb Smirnoff 		uh->uh_dport = ntohs(next_hop->sin_port);
2883329b236SRobert Watson 
2893329b236SRobert Watson 		/*
2903329b236SRobert Watson 		 * Remove the tag from the packet.  We don't need it anymore.
2913329b236SRobert Watson 		 */
2920b4ae859SGleb Smirnoff 		m_tag_delete(m, fwd_tag);
2930b4ae859SGleb Smirnoff 	}
2940b4ae859SGleb Smirnoff #endif
2950b4ae859SGleb Smirnoff 
296f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
297f76fcf6dSJeffrey Hsu 
298df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
299df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
30082c23ebaSBill Fenner 		struct inpcb *last;
3013329b236SRobert Watson 
302df8bae1dSRodney W. Grimes 		/*
303df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
304df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
3053329b236SRobert Watson 		 * those of the incoming datagram.  This allows more than one
3063329b236SRobert Watson 		 * process to receive multi/broadcasts on the same port.
307df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
308df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
3093329b236SRobert Watson 		 * applications that open both address-specific sockets and a
3103329b236SRobert Watson 		 * wildcard socket listening to the same port -- they would
311df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
3123329b236SRobert Watson 		 * Those applications open the multiple sockets to overcome
3133329b236SRobert Watson 		 * an inadequacy of the UDP socket interface, but for
3143329b236SRobert Watson 		 * backwards compatibility we avoid the problem here rather
3153329b236SRobert Watson 		 * than fixing the interface.  Maybe 4.5BSD will remedy
3163329b236SRobert Watson 		 * this?)
317df8bae1dSRodney W. Grimes 		 */
318df8bae1dSRodney W. Grimes 		last = NULL;
319cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
3209c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
321f76fcf6dSJeffrey Hsu 				continue;
322cfa1ca9dSYoshinobu Inoue #ifdef INET6
323369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
3249c1df695SRobert Watson 				continue;
325cfa1ca9dSYoshinobu Inoue #endif
326df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
327f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3289c1df695SRobert Watson 					continue;
329df8bae1dSRodney W. Grimes 			}
330df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
331df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
332df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
333df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
3349c1df695SRobert Watson 					continue;
335df8bae1dSRodney W. Grimes 			}
336df8bae1dSRodney W. Grimes 
33783453a06SBruce M Simpson 			/*
33883453a06SBruce M Simpson 			 * Check multicast packets to make sure they are only
33983453a06SBruce M Simpson 			 * sent to sockets with multicast memberships for the
34083453a06SBruce M Simpson 			 * packet's destination address and arrival interface
34183453a06SBruce M Simpson 			 */
34283453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
34383453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
3443329b236SRobert Watson 			INP_LOCK(inp);
34583453a06SBruce M Simpson 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
34683453a06SBruce M Simpson 				int mship, foundmship = 0;
34783453a06SBruce M Simpson 
3483329b236SRobert Watson 				for (mship = 0; mship < NMSHIPS(inp);
3493329b236SRobert Watson 				    mship++) {
35083453a06SBruce M Simpson 					if (MSHIP(inp, mship)->inm_addr.s_addr
35183453a06SBruce M Simpson 					    == ip->ip_dst.s_addr &&
35283453a06SBruce M Simpson 					    MSHIP(inp, mship)->inm_ifp
35383453a06SBruce M Simpson 					    == m->m_pkthdr.rcvif) {
35483453a06SBruce M Simpson 						foundmship = 1;
35583453a06SBruce M Simpson 						break;
35683453a06SBruce M Simpson 					}
35783453a06SBruce M Simpson 				}
3589c1df695SRobert Watson 				if (foundmship == 0) {
3599c1df695SRobert Watson 					INP_UNLOCK(inp);
3609c1df695SRobert Watson 					continue;
3619c1df695SRobert Watson 				}
36283453a06SBruce M Simpson 			}
36383453a06SBruce M Simpson #undef NMSHIPS
36483453a06SBruce M Simpson #undef MSHIP
365df8bae1dSRodney W. Grimes 			if (last != NULL) {
366df8bae1dSRodney W. Grimes 				struct mbuf *n;
367df8bae1dSRodney W. Grimes 
368032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
369365433d9SRobert Watson 				if (n != NULL)
3703329b236SRobert Watson 					udp_append(last, ip, n, iphlen +
3713329b236SRobert Watson 					    sizeof(struct udphdr), &udp_in);
372f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
373df8bae1dSRodney W. Grimes 			}
37482c23ebaSBill Fenner 			last = inp;
375df8bae1dSRodney W. Grimes 			/*
376df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
377df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
3783329b236SRobert Watson 			 * socket options set.  This heuristic avoids
3793329b236SRobert Watson 			 * searching through all pcbs in the common case of a
3803329b236SRobert Watson 			 * non-shared port.  It assumes that an application
3813329b236SRobert Watson 			 * will never clear these options after setting them.
382df8bae1dSRodney W. Grimes 			 */
3833329b236SRobert Watson 			if ((last->inp_socket->so_options &
3843329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
385df8bae1dSRodney W. Grimes 				break;
386df8bae1dSRodney W. Grimes 		}
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes 		if (last == NULL) {
389df8bae1dSRodney W. Grimes 			/*
3903329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
3913329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
3923329b236SRobert Watson 			 * or multicast datgram.)
393df8bae1dSRodney W. Grimes 			 */
394df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
39561ffc0b1SJeffrey Hsu 			goto badheadlocked;
396df8bae1dSRodney W. Grimes 		}
397d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
398d4b509bdSRobert Watson 		    &udp_in);
3999b657230SSam Leffler 		INP_UNLOCK(last);
4006b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
401df8bae1dSRodney W. Grimes 		return;
402df8bae1dSRodney W. Grimes 	}
4033329b236SRobert Watson 
404df8bae1dSRodney W. Grimes 	/*
4056d6a026bSDavid Greenman 	 * Locate pcb for datagram.
406df8bae1dSRodney W. Grimes 	 */
407c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
408cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
40915bd2b43SDavid Greenman 	if (inp == NULL) {
41075cfc95fSAndrey A. Chernov 		if (log_in_vain) {
411df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
41275cfc95fSAndrey A. Chernov 
41375cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
414592071e8SBruce Evans 			log(LOG_INFO,
415592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
416592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
417592071e8SBruce Evans 			    ntohs(uh->uh_sport));
41875cfc95fSAndrey A. Chernov 		}
419df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
420df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
421df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
42261ffc0b1SJeffrey Hsu 			goto badheadlocked;
423df8bae1dSRodney W. Grimes 		}
424582a7760SBruce Evans 		if (blackhole)
42561ffc0b1SJeffrey Hsu 			goto badheadlocked;
4261cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
4271cbd978eSLuigi Rizzo 			goto badheadlocked;
42804287599SRuslan Ermilov 		*ip = save_ip;
42904287599SRuslan Ermilov 		ip->ip_len += iphlen;
430582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
431f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
432df8bae1dSRodney W. Grimes 		return;
433df8bae1dSRodney W. Grimes 	}
4343329b236SRobert Watson 
4353329b236SRobert Watson 	/*
4363329b236SRobert Watson 	 * Check the minimum TTL for socket.
4373329b236SRobert Watson 	 */
43861ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
439936cd18dSAndre Oppermann 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
440936cd18dSAndre Oppermann 		goto badheadlocked;
441d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
442f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
4436b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
444df8bae1dSRodney W. Grimes 	return;
44561ffc0b1SJeffrey Hsu 
44661ffc0b1SJeffrey Hsu badheadlocked:
447f76fcf6dSJeffrey Hsu 	if (inp)
448f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
4496b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
450f76fcf6dSJeffrey Hsu badunlocked:
451df8bae1dSRodney W. Grimes 	m_freem(m);
452cfa1ca9dSYoshinobu Inoue }
453cfa1ca9dSYoshinobu Inoue 
454cfa1ca9dSYoshinobu Inoue /*
455d4b509bdSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
456d4b509bdSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
457d4b509bdSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
458d4b509bdSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
459d4b509bdSRobert Watson  * into the socket code.
460cfa1ca9dSYoshinobu Inoue  */
461cfa1ca9dSYoshinobu Inoue static void
4623329b236SRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
4633329b236SRobert Watson     struct sockaddr_in *udp_in)
464cfa1ca9dSYoshinobu Inoue {
465cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
4661e4d7da7SRobert Watson 	struct socket *so;
467cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
468c83c1318SPoul-Henning Kamp #ifdef INET6
469d4b509bdSRobert Watson 	struct sockaddr_in6 udp_in6;
470c83c1318SPoul-Henning Kamp #endif
471cfa1ca9dSYoshinobu Inoue 
4728e3f3b16SRobert Watson 	INP_LOCK_ASSERT(inp);
47387f2bb8cSRobert Watson 
474da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
475da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
4768e3f3b16SRobert Watson 	if (ipsec4_in_reject(n, inp)) {
477b9234fafSSam Leffler #ifdef IPSEC
478b9234fafSSam Leffler 		ipsecstat.in_polvio++;
4793329b236SRobert Watson #endif
480b9234fafSSam Leffler 		m_freem(n);
481b9234fafSSam Leffler 		return;
482b9234fafSSam Leffler 	}
483da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
484b9234fafSSam Leffler #ifdef MAC
4858e3f3b16SRobert Watson 	if (mac_check_inpcb_deliver(inp, n) != 0) {
486b9234fafSSam Leffler 		m_freem(n);
487b9234fafSSam Leffler 		return;
488b9234fafSSam Leffler 	}
489b9234fafSSam Leffler #endif
4908e3f3b16SRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
4918e3f3b16SRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
492cfa1ca9dSYoshinobu Inoue #ifdef INET6
4938e3f3b16SRobert Watson 		if (inp->inp_vflag & INP_IPV6) {
494cfa1ca9dSYoshinobu Inoue 			int savedflags;
495cfa1ca9dSYoshinobu Inoue 
4968e3f3b16SRobert Watson 			savedflags = inp->inp_flags;
4978e3f3b16SRobert Watson 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
4988e3f3b16SRobert Watson 			ip6_savecontrol(inp, n, &opts);
4998e3f3b16SRobert Watson 			inp->inp_flags = savedflags;
500cfa1ca9dSYoshinobu Inoue 		} else
501cfa1ca9dSYoshinobu Inoue #endif
5028e3f3b16SRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
5034cc20ab1SSeigo Tanimura 	}
504cfa1ca9dSYoshinobu Inoue #ifdef INET6
5058e3f3b16SRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
506d4b509bdSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
507d4b509bdSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
508d4b509bdSRobert Watson 		udp_in6.sin6_family = AF_INET6;
509d4b509bdSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
510d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
511cfa1ca9dSYoshinobu Inoue 	} else
512cfa1ca9dSYoshinobu Inoue #endif
513d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
514cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
5151e4d7da7SRobert Watson 
5168e3f3b16SRobert Watson 	so = inp->inp_socket;
5171e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
5181e4d7da7SRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
519cfa1ca9dSYoshinobu Inoue 		m_freem(n);
520cfa1ca9dSYoshinobu Inoue 		if (opts)
521cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
522cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5231e4d7da7SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
5244cc20ab1SSeigo Tanimura 	} else
5251e4d7da7SRobert Watson 		sorwakeup_locked(so);
526df8bae1dSRodney W. Grimes }
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes /*
5293329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
5303329b236SRobert Watson  * collect error status.
531df8bae1dSRodney W. Grimes  */
5323ce144eaSJeffrey Hsu struct inpcb *
5333329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
534df8bae1dSRodney W. Grimes {
5353329b236SRobert Watson 
536df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
537df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
538df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5393329b236SRobert Watson 	return (inp);
540df8bae1dSRodney W. Grimes }
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes void
5433329b236SRobert Watson udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
544df8bae1dSRodney W. Grimes {
545c693a045SJonathan Lemon 	struct ip *ip = vip;
546c693a045SJonathan Lemon 	struct udphdr *uh;
5473ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
548c693a045SJonathan Lemon 	struct in_addr faddr;
549c693a045SJonathan Lemon 	struct inpcb *inp;
550c693a045SJonathan Lemon 
551c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
552c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
553c693a045SJonathan Lemon 		return;
554df8bae1dSRodney W. Grimes 
55597d8d152SAndre Oppermann 	/*
55697d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
55797d8d152SAndre Oppermann 	 */
55897d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
55997d8d152SAndre Oppermann 		return;
5603329b236SRobert Watson 
56197d8d152SAndre Oppermann 	/*
56297d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
5633329b236SRobert Watson 	 *
5643329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
5653329b236SRobert Watson 	 * DoS attack on machines with many connections.
56697d8d152SAndre Oppermann 	 */
56797d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
568d1c54148SJesper Skriver 		ip = 0;
569d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
570df8bae1dSRodney W. Grimes 		return;
571df8bae1dSRodney W. Grimes 	if (ip) {
572df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
573f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
574c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
575c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
576f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
577f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
578f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
579c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
580f76fcf6dSJeffrey Hsu 			}
581f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
582f76fcf6dSJeffrey Hsu 		}
583f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
584df8bae1dSRodney W. Grimes 	} else
585f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
586df8bae1dSRodney W. Grimes }
587df8bae1dSRodney W. Grimes 
5880312fbe9SPoul-Henning Kamp static int
58982d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
59098271db4SGarrett Wollman {
591277afaffSRobert Watson 	int error, i, n;
59298271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
59398271db4SGarrett Wollman 	inp_gen_t gencnt;
59498271db4SGarrett Wollman 	struct xinpgen xig;
59598271db4SGarrett Wollman 
59698271db4SGarrett Wollman 	/*
59798271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
59898271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
59998271db4SGarrett Wollman 	 */
60098271db4SGarrett Wollman 	if (req->oldptr == 0) {
60198271db4SGarrett Wollman 		n = udbinfo.ipi_count;
60298271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
60398271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
6043329b236SRobert Watson 		return (0);
60598271db4SGarrett Wollman 	}
60698271db4SGarrett Wollman 
60798271db4SGarrett Wollman 	if (req->newptr != 0)
6083329b236SRobert Watson 		return (EPERM);
60998271db4SGarrett Wollman 
61098271db4SGarrett Wollman 	/*
61198271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
61298271db4SGarrett Wollman 	 */
6134b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
61498271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
61598271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6164b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
61798271db4SGarrett Wollman 
61847934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6195c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
62047934cefSDon Lewis 	if (error != 0)
62147934cefSDon Lewis 		return (error);
6225c38b6dbSDon Lewis 
62398271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
62498271db4SGarrett Wollman 	xig.xig_count = n;
62598271db4SGarrett Wollman 	xig.xig_gen = gencnt;
62698271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
62798271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
62898271db4SGarrett Wollman 	if (error)
6293329b236SRobert Watson 		return (error);
63098271db4SGarrett Wollman 
631a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
63298271db4SGarrett Wollman 	if (inp_list == 0)
6333329b236SRobert Watson 		return (ENOMEM);
63498271db4SGarrett Wollman 
635f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
636fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
637fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
638f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6392ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6402ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
64198271db4SGarrett Wollman 			inp_list[i++] = inp;
642f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6434787fd37SPaul Saab 	}
644f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
64598271db4SGarrett Wollman 	n = i;
64698271db4SGarrett Wollman 
64798271db4SGarrett Wollman 	error = 0;
64898271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
64998271db4SGarrett Wollman 		inp = inp_list[i];
650d915b280SStephan Uphoff 		INP_LOCK(inp);
65198271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
65298271db4SGarrett Wollman 			struct xinpcb xi;
653fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
65498271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
65598271db4SGarrett Wollman 			/* XXX should avoid extra copy */
65698271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
65798271db4SGarrett Wollman 			if (inp->inp_socket)
65898271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6594b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
660d915b280SStephan Uphoff 			INP_UNLOCK(inp);
66198271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
662d915b280SStephan Uphoff 		} else
663d915b280SStephan Uphoff 			INP_UNLOCK(inp);
66498271db4SGarrett Wollman 	}
66598271db4SGarrett Wollman 	if (!error) {
66698271db4SGarrett Wollman 		/*
6673329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
6683329b236SRobert Watson 		 * generation differs from what we told her before, she knows
6693329b236SRobert Watson 		 * that something happened while we were processing this
6703329b236SRobert Watson 		 * request, and it might be necessary to retry.
67198271db4SGarrett Wollman 		 */
672f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
67398271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
67498271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
67598271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
676f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
67798271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
67898271db4SGarrett Wollman 	}
67998271db4SGarrett Wollman 	free(inp_list, M_TEMP);
6803329b236SRobert Watson 	return (error);
68198271db4SGarrett Wollman }
68298271db4SGarrett Wollman 
68398271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
68498271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
68598271db4SGarrett Wollman 
68698271db4SGarrett Wollman static int
68782d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
688490d50b6SBrian Feldman {
689c0511d3bSBrian Feldman 	struct xucred xuc;
690490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
691490d50b6SBrian Feldman 	struct inpcb *inp;
692277afaffSRobert Watson 	int error;
693490d50b6SBrian Feldman 
694acd3428bSRobert Watson 	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
695acd3428bSRobert Watson 	    SUSER_ALLOWJAIL);
696490d50b6SBrian Feldman 	if (error)
697490d50b6SBrian Feldman 		return (error);
698490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
699490d50b6SBrian Feldman 	if (error)
700490d50b6SBrian Feldman 		return (error);
701f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
702490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
703cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
7042d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
705490d50b6SBrian Feldman 		error = ENOENT;
706490d50b6SBrian Feldman 		goto out;
707490d50b6SBrian Feldman 	}
70829dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
7097ce87f12SDavid Malone 	if (error)
7107ce87f12SDavid Malone 		goto out;
71176183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
712490d50b6SBrian Feldman out:
713f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
7140e1eebb8SDon Lewis 	if (error == 0)
7150e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
716490d50b6SBrian Feldman 	return (error);
717490d50b6SBrian Feldman }
718490d50b6SBrian Feldman 
7197ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7207ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7217ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
722490d50b6SBrian Feldman 
723490d50b6SBrian Feldman static int
7243329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7253329b236SRobert Watson     struct mbuf *control, struct thread *td)
726df8bae1dSRodney W. Grimes {
7273329b236SRobert Watson 	struct udpiphdr *ui;
7283329b236SRobert Watson 	int len = m->m_pkthdr.len;
72990162a4eSIan Dowse 	struct in_addr faddr, laddr;
730c557ae16SIan Dowse 	struct cmsghdr *cm;
731c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
73290162a4eSIan Dowse 	int error = 0;
7338afa2304SBruce M Simpson 	int ipflags;
73490162a4eSIan Dowse 	u_short fport, lport;
7355c32ea65SRobert Watson 	int unlock_udbinfo;
736df8bae1dSRodney W. Grimes 
7375c32ea65SRobert Watson 	/*
7385c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
7395c32ea65SRobert Watson 	 * inpcb.  As such, we don't know up front what inpcb locks we will
7405c32ea65SRobert Watson 	 * need.  Do any work to decide what is needed up front before
7415c32ea65SRobert Watson 	 * acquiring locks.
7425c32ea65SRobert Watson 	 */
743430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
744c557ae16SIan Dowse 		if (control)
745c557ae16SIan Dowse 			m_freem(control);
7465c32ea65SRobert Watson 		m_freem(m);
7473329b236SRobert Watson 		return (EMSGSIZE);
748430d30d8SBill Fenner 	}
749430d30d8SBill Fenner 
750c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
751c557ae16SIan Dowse 	if (control != NULL) {
752c557ae16SIan Dowse 		/*
7533329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
7543329b236SRobert Watson 		 * stored in a single mbuf.
755c557ae16SIan Dowse 		 */
756c557ae16SIan Dowse 		if (control->m_next) {
757c557ae16SIan Dowse 			m_freem(control);
7585c32ea65SRobert Watson 			m_freem(m);
7593329b236SRobert Watson 			return (EINVAL);
760c557ae16SIan Dowse 		}
761c557ae16SIan Dowse 		for (; control->m_len > 0;
762c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
763c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
764c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
765c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
766c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
767c557ae16SIan Dowse 				error = EINVAL;
768c557ae16SIan Dowse 				break;
769c557ae16SIan Dowse 			}
770c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
771c557ae16SIan Dowse 				continue;
772c557ae16SIan Dowse 
773c557ae16SIan Dowse 			switch (cm->cmsg_type) {
774c557ae16SIan Dowse 			case IP_SENDSRCADDR:
775c557ae16SIan Dowse 				if (cm->cmsg_len !=
776c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
777c557ae16SIan Dowse 					error = EINVAL;
778c557ae16SIan Dowse 					break;
779c557ae16SIan Dowse 				}
780c557ae16SIan Dowse 				bzero(&src, sizeof(src));
781c557ae16SIan Dowse 				src.sin_family = AF_INET;
782c557ae16SIan Dowse 				src.sin_len = sizeof(src);
783c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
784c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
785c557ae16SIan Dowse 				break;
786c557ae16SIan Dowse 			default:
787c557ae16SIan Dowse 				error = ENOPROTOOPT;
788c557ae16SIan Dowse 				break;
789c557ae16SIan Dowse 			}
790c557ae16SIan Dowse 			if (error)
791c557ae16SIan Dowse 				break;
792c557ae16SIan Dowse 		}
793c557ae16SIan Dowse 		m_freem(control);
794c557ae16SIan Dowse 	}
7955c32ea65SRobert Watson 	if (error) {
7965c32ea65SRobert Watson 		m_freem(m);
7973329b236SRobert Watson 		return (error);
7985c32ea65SRobert Watson 	}
7995c32ea65SRobert Watson 
8003329b236SRobert Watson 	if (src.sin_addr.s_addr != INADDR_ANY || addr != NULL) {
8015c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
8025c32ea65SRobert Watson 		unlock_udbinfo = 1;
8035c32ea65SRobert Watson 	} else
8045c32ea65SRobert Watson 		unlock_udbinfo = 0;
8055c32ea65SRobert Watson 	INP_LOCK(inp);
8065c32ea65SRobert Watson 
8075c32ea65SRobert Watson #ifdef MAC
8085c32ea65SRobert Watson 	mac_create_mbuf_from_inpcb(inp, m);
8095c32ea65SRobert Watson #endif
8105c32ea65SRobert Watson 
81190162a4eSIan Dowse 	laddr = inp->inp_laddr;
81290162a4eSIan Dowse 	lport = inp->inp_lport;
813c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
814c557ae16SIan Dowse 		if (lport == 0) {
815c557ae16SIan Dowse 			error = EINVAL;
816c557ae16SIan Dowse 			goto release;
817c557ae16SIan Dowse 		}
818c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
819b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
820c557ae16SIan Dowse 		if (error)
821c557ae16SIan Dowse 			goto release;
822c557ae16SIan Dowse 	}
823c557ae16SIan Dowse 
824df8bae1dSRodney W. Grimes 	if (addr) {
82575c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
826812d8653SSam Leffler 		if (jailed(td->td_ucred))
8273329b236SRobert Watson 			prison_remote_ip(td->td_ucred, 0,
8283329b236SRobert Watson 			    &sin->sin_addr.s_addr);
829df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
830df8bae1dSRodney W. Grimes 			error = EISCONN;
831df8bae1dSRodney W. Grimes 			goto release;
832df8bae1dSRodney W. Grimes 		}
83390162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
834b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
83590162a4eSIan Dowse 		if (error)
83690162a4eSIan Dowse 			goto release;
83790162a4eSIan Dowse 
83890162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
83990162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
84090162a4eSIan Dowse 		    inp->inp_lport == 0) {
8413a1757b9SGleb Smirnoff 			/*
8423a1757b9SGleb Smirnoff 			 * Remember addr if jailed, to prevent rebinding.
8433a1757b9SGleb Smirnoff 			 */
8443a1757b9SGleb Smirnoff 			if (jailed(td->td_ucred))
8453a1757b9SGleb Smirnoff 				inp->inp_laddr = laddr;
84690162a4eSIan Dowse 			inp->inp_lport = lport;
84790162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
84890162a4eSIan Dowse 				inp->inp_lport = 0;
84990162a4eSIan Dowse 				error = EAGAIN;
850df8bae1dSRodney W. Grimes 				goto release;
851df8bae1dSRodney W. Grimes 			}
85290162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
85390162a4eSIan Dowse 		}
854df8bae1dSRodney W. Grimes 	} else {
85590162a4eSIan Dowse 		faddr = inp->inp_faddr;
85690162a4eSIan Dowse 		fport = inp->inp_fport;
85790162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
858df8bae1dSRodney W. Grimes 			error = ENOTCONN;
859df8bae1dSRodney W. Grimes 			goto release;
860df8bae1dSRodney W. Grimes 		}
861df8bae1dSRodney W. Grimes 	}
862e6ccd709SRobert Watson 
863df8bae1dSRodney W. Grimes 	/*
864e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
865392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
866392e8407SRobert Watson 	 * since we won't use that space at this layer.
867df8bae1dSRodney W. Grimes 	 */
868e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
869e6ccd709SRobert Watson 	if (m == NULL) {
870df8bae1dSRodney W. Grimes 		error = ENOBUFS;
87149b19bfcSBruce M Simpson 		goto release;
872df8bae1dSRodney W. Grimes 	}
873e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
874e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
875392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes 	/*
8783329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
8793329b236SRobert Watson 	 * into network format.
880df8bae1dSRodney W. Grimes 	 */
881df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
882db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
883df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
88490162a4eSIan Dowse 	ui->ui_src = laddr;
88590162a4eSIan Dowse 	ui->ui_dst = faddr;
88690162a4eSIan Dowse 	ui->ui_sport = lport;
88790162a4eSIan Dowse 	ui->ui_dport = fport;
888db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
889df8bae1dSRodney W. Grimes 
890b2828ad2SAndre Oppermann 	/*
891b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
892b2828ad2SAndre Oppermann 	 */
893b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
894b2828ad2SAndre Oppermann 		struct ip *ip;
8953329b236SRobert Watson 
896b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
897b2828ad2SAndre Oppermann 		ip->ip_off |= IP_DF;
898b2828ad2SAndre Oppermann 	}
899b2828ad2SAndre Oppermann 
900b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
901b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
902b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
903b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
904b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
9056fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
9068afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
9078afa2304SBruce M Simpson 
908df8bae1dSRodney W. Grimes 	/*
909db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
910df8bae1dSRodney W. Grimes 	 */
911df8bae1dSRodney W. Grimes 	if (udpcksum) {
9126fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
9138a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
9148a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
915db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
916db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
917db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
9183329b236SRobert Watson 	} else
919db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
920df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
921ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
922ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
923df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
924cfa1ca9dSYoshinobu Inoue 
9255c32ea65SRobert Watson 	if (unlock_udbinfo)
9265c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
92797d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
9285d846453SSam Leffler 	    inp->inp_moptions, inp);
9295c32ea65SRobert Watson 	INP_UNLOCK(inp);
930df8bae1dSRodney W. Grimes 	return (error);
931df8bae1dSRodney W. Grimes 
932df8bae1dSRodney W. Grimes release:
9335c32ea65SRobert Watson 	INP_UNLOCK(inp);
9345c32ea65SRobert Watson 	if (unlock_udbinfo)
9355c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
936df8bae1dSRodney W. Grimes 	m_freem(m);
937df8bae1dSRodney W. Grimes 	return (error);
938df8bae1dSRodney W. Grimes }
939df8bae1dSRodney W. Grimes 
94076429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
941032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
942e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
9433d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
9440312fbe9SPoul-Henning Kamp 
945cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
946cfa1ca9dSYoshinobu Inoue #ifdef INET6
947cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
948cfa1ca9dSYoshinobu Inoue #else
949cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
950cfa1ca9dSYoshinobu Inoue #endif
951cfa1ca9dSYoshinobu Inoue 				      );
952e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9530ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
954df8bae1dSRodney W. Grimes 
955ac45e92fSRobert Watson static void
956d0390e05SGarrett Wollman udp_abort(struct socket *so)
957df8bae1dSRodney W. Grimes {
958d0390e05SGarrett Wollman 	struct inpcb *inp;
959df8bae1dSRodney W. Grimes 
960d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
96114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
96214ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
963f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
964a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
965a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
966a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
967d0390e05SGarrett Wollman 		soisdisconnected(so);
968a152f8a3SRobert Watson 	}
969a152f8a3SRobert Watson 	INP_UNLOCK(inp);
970f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
971df8bae1dSRodney W. Grimes }
972df8bae1dSRodney W. Grimes 
973d0390e05SGarrett Wollman static int
974b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
975d0390e05SGarrett Wollman {
976d0390e05SGarrett Wollman 	struct inpcb *inp;
977277afaffSRobert Watson 	int error;
978d0390e05SGarrett Wollman 
979d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
98014ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
981cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
982f24618aaSRobert Watson 	if (error)
9833329b236SRobert Watson 		return (error);
984f24618aaSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
985d915b280SStephan Uphoff 	error = in_pcballoc(so, &udbinfo);
98653b57cd1SSam Leffler 	if (error) {
98753b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
9883329b236SRobert Watson 		return (error);
98953b57cd1SSam Leffler 	}
990cfa1ca9dSYoshinobu Inoue 
991cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
992f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
993cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
994cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
995f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
9963329b236SRobert Watson 	return (0);
997df8bae1dSRodney W. Grimes }
998d0390e05SGarrett Wollman 
999d0390e05SGarrett Wollman static int
1000b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1001d0390e05SGarrett Wollman {
1002d0390e05SGarrett Wollman 	struct inpcb *inp;
1003277afaffSRobert Watson 	int error;
1004d0390e05SGarrett Wollman 
1005d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
100614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
100714ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1008f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1009b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1010f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1011f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10123329b236SRobert Watson 	return (error);
1013d0390e05SGarrett Wollman }
1014d0390e05SGarrett Wollman 
1015a152f8a3SRobert Watson static void
1016a152f8a3SRobert Watson udp_close(struct socket *so)
1017a152f8a3SRobert Watson {
1018a152f8a3SRobert Watson 	struct inpcb *inp;
1019a152f8a3SRobert Watson 
1020a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1021a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1022a152f8a3SRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1023a152f8a3SRobert Watson 	INP_LOCK(inp);
1024a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1025a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1026a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1027a152f8a3SRobert Watson 		soisdisconnected(so);
1028a152f8a3SRobert Watson 	}
1029a152f8a3SRobert Watson 	INP_UNLOCK(inp);
1030a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&udbinfo);
1031a152f8a3SRobert Watson }
1032a152f8a3SRobert Watson 
1033d0390e05SGarrett Wollman static int
1034b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1035d0390e05SGarrett Wollman {
1036d0390e05SGarrett Wollman 	struct inpcb *inp;
1037277afaffSRobert Watson 	int error;
103875c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1039d0390e05SGarrett Wollman 
1040d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
104114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
104214ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1043f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1044f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1045f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1046f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
10473329b236SRobert Watson 		return (EISCONN);
1048f76fcf6dSJeffrey Hsu 	}
104975c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1050812d8653SSam Leffler 	if (jailed(td->td_ucred))
1051a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1052b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
10534cc20ab1SSeigo Tanimura 	if (error == 0)
1054df8bae1dSRodney W. Grimes 		soisconnected(so);
1055f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1056f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10573329b236SRobert Watson 	return (error);
1058df8bae1dSRodney W. Grimes }
1059d0390e05SGarrett Wollman 
1060bc725eafSRobert Watson static void
1061d0390e05SGarrett Wollman udp_detach(struct socket *so)
1062d0390e05SGarrett Wollman {
1063d0390e05SGarrett Wollman 	struct inpcb *inp;
1064d0390e05SGarrett Wollman 
1065d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
106614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1067a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1068a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
106914ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1070f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1071d0390e05SGarrett Wollman 	in_pcbdetach(inp);
107214ba8addSRobert Watson 	in_pcbfree(inp);
1073f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1074d0390e05SGarrett Wollman }
1075d0390e05SGarrett Wollman 
1076d0390e05SGarrett Wollman static int
1077d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1078d0390e05SGarrett Wollman {
1079d0390e05SGarrett Wollman 	struct inpcb *inp;
1080d0390e05SGarrett Wollman 
1081d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
108214ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
108314ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1084f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1085f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1086f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1087f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
10883329b236SRobert Watson 		return (ENOTCONN);
1089f76fcf6dSJeffrey Hsu 	}
1090d0390e05SGarrett Wollman 
1091df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1092df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1093d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1094d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1095d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
1096f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1097f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10983329b236SRobert Watson 	return (0);
1099df8bae1dSRodney W. Grimes }
1100df8bae1dSRodney W. Grimes 
1101d0390e05SGarrett Wollman static int
110257bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1103b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1104d0390e05SGarrett Wollman {
1105d0390e05SGarrett Wollman 	struct inpcb *inp;
1106d0390e05SGarrett Wollman 
1107d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
110814ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
11093329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1110d0390e05SGarrett Wollman }
1111d0390e05SGarrett Wollman 
111276429de4SYoshinobu Inoue int
1113d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1114d0390e05SGarrett Wollman {
1115d0390e05SGarrett Wollman 	struct inpcb *inp;
1116d0390e05SGarrett Wollman 
1117d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
111814ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1119f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1120d0390e05SGarrett Wollman 	socantsendmore(so);
1121f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
11223329b236SRobert Watson 	return (0);
1123d0390e05SGarrett Wollman }
1124d0390e05SGarrett Wollman 
1125f76fcf6dSJeffrey Hsu /*
11263329b236SRobert Watson  * This is the wrapper function for in_setsockaddr.  We just pass down the
11273329b236SRobert Watson  * pcbinfo for in_setsockaddr() to lock.  We don't want to do the locking
11283329b236SRobert Watson  * here because in_setsockaddr() will call malloc and might block.
1129f76fcf6dSJeffrey Hsu  */
1130f76fcf6dSJeffrey Hsu static int
1131f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1132f76fcf6dSJeffrey Hsu {
11333329b236SRobert Watson 
1134f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1135f76fcf6dSJeffrey Hsu }
1136f76fcf6dSJeffrey Hsu 
1137f76fcf6dSJeffrey Hsu /*
11383329b236SRobert Watson  * This is the wrapper function for in_setpeeraddr().  We just pass down the
11393329b236SRobert Watson  * pcbinfo for in_setpeeraddr() to lock.
1140f76fcf6dSJeffrey Hsu  */
1141f76fcf6dSJeffrey Hsu static int
1142f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1143f76fcf6dSJeffrey Hsu {
11443329b236SRobert Watson 
1145f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1146f76fcf6dSJeffrey Hsu }
1147f76fcf6dSJeffrey Hsu 
1148d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1149756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1150756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1151756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1152756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1153756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1154756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1155756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
1156756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		udp_peeraddr,
1157756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
115859b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1159756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
1160756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		udp_sockaddr,
1161a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1162a152f8a3SRobert Watson 	.pru_close =		udp_close,
1163d0390e05SGarrett Wollman };
1164