xref: /freebsd/sys/netinet/udp_usrreq.c (revision d45e4f9945b038cd1dbb7447021f0253dbc6fc95)
1c398230bSWarner Losh /*-
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
296dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
330b4ae859SGleb Smirnoff #include "opt_ipfw.h"
346a800098SYoshinobu Inoue #include "opt_ipsec.h"
35cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
36bdb3fa18SRobert Watson #include "opt_mac.h"
37cfa1ca9dSYoshinobu Inoue 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
3926f9a767SRodney W. Grimes #include <sys/systm.h>
40960ed29cSSeigo Tanimura #include <sys/domain.h>
414f590175SPaul Saab #include <sys/eventhandler.h>
42960ed29cSSeigo Tanimura #include <sys/jail.h>
43b110a8a2SGarrett Wollman #include <sys/kernel.h>
44960ed29cSSeigo Tanimura #include <sys/lock.h>
45bdb3fa18SRobert Watson #include <sys/mac.h>
46df8bae1dSRodney W. Grimes #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48490d50b6SBrian Feldman #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50960ed29cSSeigo Tanimura #include <sys/signalvar.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53960ed29cSSeigo Tanimura #include <sys/sx.h>
54b5e8ce9fSBruce Evans #include <sys/sysctl.h>
55816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
568781d8e9SBruce Evans 
5769c2d429SJeff Roberson #include <vm/uma.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
65960ed29cSSeigo Tanimura #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip.h>
67cfa1ca9dSYoshinobu Inoue #ifdef INET6
68cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
69cfa1ca9dSYoshinobu Inoue #endif
70960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
71960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
74cfa1ca9dSYoshinobu Inoue #ifdef INET6
75cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
76cfa1ca9dSYoshinobu Inoue #endif
77df8bae1dSRodney W. Grimes #include <netinet/udp.h>
78df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
79df8bae1dSRodney W. Grimes 
80b9234fafSSam Leffler #ifdef FAST_IPSEC
81b9234fafSSam Leffler #include <netipsec/ipsec.h>
82b9234fafSSam Leffler #endif /*FAST_IPSEC*/
83b9234fafSSam Leffler 
84cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
85cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
86cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
87cfa1ca9dSYoshinobu Inoue 
88db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
89db4f9cc7SJonathan Lemon 
90df8bae1dSRodney W. Grimes /*
91df8bae1dSRodney W. Grimes  * UDP protocol implementation.
92df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
93df8bae1dSRodney W. Grimes  */
94df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
950312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
96df8bae1dSRodney W. Grimes #else
970312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
98df8bae1dSRodney W. Grimes #endif
990312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
1000312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
101df8bae1dSRodney W. Grimes 
102032dcc76SLuigi Rizzo int	log_in_vain = 0;
103816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1043d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
105816a3d83SPoul-Henning Kamp 
106032dcc76SLuigi Rizzo static int	blackhole = 0;
10716f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10816f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
10916f7f31fSGeoff Rehmet 
11083453a06SBruce M Simpson static int	strict_mcast_mship = 0;
11183453a06SBruce M Simpson SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
11283453a06SBruce M Simpson 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
11383453a06SBruce M Simpson 
11476429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
115cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1167a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
11715bd2b43SDavid Greenman 
11815bd2b43SDavid Greenman #ifndef UDBHASHSIZE
119c3229e05SDavid Greenman #define UDBHASHSIZE 16
12015bd2b43SDavid Greenman #endif
12115bd2b43SDavid Greenman 
12276429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
123c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1243d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
125f2ea20e6SGarrett Wollman 
126c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
127d4b509bdSRobert Watson 		int off, struct sockaddr_in *udp_in);
128cfa1ca9dSYoshinobu Inoue 
129bc725eafSRobert Watson static void udp_detach(struct socket *so);
1304d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1314d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
132df8bae1dSRodney W. Grimes 
1334f590175SPaul Saab static void
1344f590175SPaul Saab udp_zone_change(void *tag)
1354f590175SPaul Saab {
1364f590175SPaul Saab 
1374f590175SPaul Saab 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1384f590175SPaul Saab }
1394f590175SPaul Saab 
140df8bae1dSRodney W. Grimes void
141032dcc76SLuigi Rizzo udp_init()
142df8bae1dSRodney W. Grimes {
143f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
14415bd2b43SDavid Greenman 	LIST_INIT(&udb);
14515bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
146ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1478781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1488781d8e9SBruce Evans 					&udbinfo.porthashmask);
14969c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
150420a2811SAndre Oppermann 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
15169c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1524f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
1534f590175SPaul Saab 		EVENTHANDLER_PRI_ANY);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes void
157032dcc76SLuigi Rizzo udp_input(m, off)
158032dcc76SLuigi Rizzo 	register struct mbuf *m;
159032dcc76SLuigi Rizzo 	int off;
160df8bae1dSRodney W. Grimes {
161cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
162032dcc76SLuigi Rizzo 	register struct ip *ip;
163032dcc76SLuigi Rizzo 	register struct udphdr *uh;
164032dcc76SLuigi Rizzo 	register struct inpcb *inp;
165df8bae1dSRodney W. Grimes 	int len;
166df8bae1dSRodney W. Grimes 	struct ip save_ip;
167d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
1680b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
1690b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
1700b4ae859SGleb Smirnoff #endif
171df8bae1dSRodney W. Grimes 
172df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	/*
175df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
176df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
177df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
178df8bae1dSRodney W. Grimes 	 * with options still present.
179df8bae1dSRodney W. Grimes 	 */
180df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
181df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
182df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
183df8bae1dSRodney W. Grimes 	}
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes 	/*
186df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
187df8bae1dSRodney W. Grimes 	 */
188df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
189df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
190df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
191df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
192df8bae1dSRodney W. Grimes 			return;
193df8bae1dSRodney W. Grimes 		}
194df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
195df8bae1dSRodney W. Grimes 	}
196df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
197df8bae1dSRodney W. Grimes 
198686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
199686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
200f76fcf6dSJeffrey Hsu 		goto badunlocked;
201686cdd19SJun-ichiro itojun Hagino 
202df8bae1dSRodney W. Grimes 	/*
203b9234fafSSam Leffler 	 * Construct sockaddr format source address.
204b9234fafSSam Leffler 	 * Stuff source address and datagram in user buffer.
205b9234fafSSam Leffler 	 */
206d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
207d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
208d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
209b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
210b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
211b9234fafSSam Leffler 
212b9234fafSSam Leffler 	/*
213df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
214df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
215df8bae1dSRodney W. Grimes 	 */
216df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
217df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2187eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
219df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
220f76fcf6dSJeffrey Hsu 			goto badunlocked;
221df8bae1dSRodney W. Grimes 		}
222df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
223df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
224df8bae1dSRodney W. Grimes 	}
225df8bae1dSRodney W. Grimes 	/*
226df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
227df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
228df8bae1dSRodney W. Grimes 	 */
22948cb400fSRuslan Ermilov 	if (!blackhole)
230df8bae1dSRodney W. Grimes 		save_ip = *ip;
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes 	/*
233df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
234df8bae1dSRodney W. Grimes 	 */
2356dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
236db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
237db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
238db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
239db4f9cc7SJonathan Lemon 			else
240db4f9cc7SJonathan Lemon 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
241506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
242db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
243db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
244db4f9cc7SJonathan Lemon 		} else {
245cb342100SHajimu UMEMOTO 			char b[9];
246cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2476effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
248df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
249623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
250cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
251db4f9cc7SJonathan Lemon 		}
252623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
253df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
254df8bae1dSRodney W. Grimes 			m_freem(m);
255df8bae1dSRodney W. Grimes 			return;
256df8bae1dSRodney W. Grimes 		}
257fb9aaba0SRuslan Ermilov 	} else
258fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
259df8bae1dSRodney W. Grimes 
2600b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
2610b4ae859SGleb Smirnoff 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
2620b4ae859SGleb Smirnoff 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
2630b4ae859SGleb Smirnoff 
2640b4ae859SGleb Smirnoff 	if (fwd_tag != NULL) {
2650b4ae859SGleb Smirnoff 		struct sockaddr_in *next_hop;
2660b4ae859SGleb Smirnoff 
2670b4ae859SGleb Smirnoff 		/* Do the hack. */
2680b4ae859SGleb Smirnoff 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
2690b4ae859SGleb Smirnoff 		ip->ip_dst = next_hop->sin_addr;
2700b4ae859SGleb Smirnoff 		uh->uh_dport = ntohs(next_hop->sin_port);
2710b4ae859SGleb Smirnoff 		/* Remove the tag from the packet.  We don't need it anymore. */
2720b4ae859SGleb Smirnoff 		m_tag_delete(m, fwd_tag);
2730b4ae859SGleb Smirnoff 	}
2740b4ae859SGleb Smirnoff #endif
2750b4ae859SGleb Smirnoff 
276f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
277f76fcf6dSJeffrey Hsu 
278df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
279df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
28082c23ebaSBill Fenner 		struct inpcb *last;
281df8bae1dSRodney W. Grimes 		/*
282df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
283df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
284df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
285df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
286df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
287df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
288df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
289df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
290df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
291df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
292df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
293df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
294df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
295df8bae1dSRodney W. Grimes 		 */
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 		/*
298df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
299df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
300df8bae1dSRodney W. Grimes 		 */
301df8bae1dSRodney W. Grimes 		last = NULL;
302cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
3039c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
304f76fcf6dSJeffrey Hsu 				continue;
305cfa1ca9dSYoshinobu Inoue #ifdef INET6
306369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
3079c1df695SRobert Watson 				continue;
308cfa1ca9dSYoshinobu Inoue #endif
309df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
310f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3119c1df695SRobert Watson 					continue;
312df8bae1dSRodney W. Grimes 			}
313df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
314df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
315df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
316df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
3179c1df695SRobert Watson 					continue;
318df8bae1dSRodney W. Grimes 			}
3199c1df695SRobert Watson 			INP_LOCK(inp);
320df8bae1dSRodney W. Grimes 
32183453a06SBruce M Simpson 			/*
32283453a06SBruce M Simpson 			 * Check multicast packets to make sure they are only
32383453a06SBruce M Simpson 			 * sent to sockets with multicast memberships for the
32483453a06SBruce M Simpson 			 * packet's destination address and arrival interface
32583453a06SBruce M Simpson 			 */
32683453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
32783453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
32883453a06SBruce M Simpson 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
32983453a06SBruce M Simpson 				int mship, foundmship = 0;
33083453a06SBruce M Simpson 
33183453a06SBruce M Simpson 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
33283453a06SBruce M Simpson 					if (MSHIP(inp, mship)->inm_addr.s_addr
33383453a06SBruce M Simpson 					    == ip->ip_dst.s_addr &&
33483453a06SBruce M Simpson 					    MSHIP(inp, mship)->inm_ifp
33583453a06SBruce M Simpson 					    == m->m_pkthdr.rcvif) {
33683453a06SBruce M Simpson 						foundmship = 1;
33783453a06SBruce M Simpson 						break;
33883453a06SBruce M Simpson 					}
33983453a06SBruce M Simpson 				}
3409c1df695SRobert Watson 				if (foundmship == 0) {
3419c1df695SRobert Watson 					INP_UNLOCK(inp);
3429c1df695SRobert Watson 					continue;
3439c1df695SRobert Watson 				}
34483453a06SBruce M Simpson 			}
34583453a06SBruce M Simpson #undef NMSHIPS
34683453a06SBruce M Simpson #undef MSHIP
347df8bae1dSRodney W. Grimes 			if (last != NULL) {
348df8bae1dSRodney W. Grimes 				struct mbuf *n;
349df8bae1dSRodney W. Grimes 
350032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
351365433d9SRobert Watson 				if (n != NULL)
352cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
353cfa1ca9dSYoshinobu Inoue 						   iphlen +
354d4b509bdSRobert Watson 						   sizeof(struct udphdr),
355d4b509bdSRobert Watson 						   &udp_in);
356f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
357df8bae1dSRodney W. Grimes 			}
35882c23ebaSBill Fenner 			last = inp;
359df8bae1dSRodney W. Grimes 			/*
360df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
361df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
362df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
363df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
364df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
365df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
366df8bae1dSRodney W. Grimes 			 */
3674cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
368df8bae1dSRodney W. Grimes 				break;
369df8bae1dSRodney W. Grimes 		}
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes 		if (last == NULL) {
372df8bae1dSRodney W. Grimes 			/*
373df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
374df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
375df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
376df8bae1dSRodney W. Grimes 			 */
377df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
37861ffc0b1SJeffrey Hsu 			goto badheadlocked;
379df8bae1dSRodney W. Grimes 		}
380d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
381d4b509bdSRobert Watson 		    &udp_in);
3829b657230SSam Leffler 		INP_UNLOCK(last);
3836b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
384df8bae1dSRodney W. Grimes 		return;
385df8bae1dSRodney W. Grimes 	}
386df8bae1dSRodney W. Grimes 	/*
3876d6a026bSDavid Greenman 	 * Locate pcb for datagram.
388df8bae1dSRodney W. Grimes 	 */
389c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
390cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
39115bd2b43SDavid Greenman 	if (inp == NULL) {
39275cfc95fSAndrey A. Chernov 		if (log_in_vain) {
393df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
39475cfc95fSAndrey A. Chernov 
39575cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
396592071e8SBruce Evans 			log(LOG_INFO,
397592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
398592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
399592071e8SBruce Evans 			    ntohs(uh->uh_sport));
40075cfc95fSAndrey A. Chernov 		}
401df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
402df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
403df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
40461ffc0b1SJeffrey Hsu 			goto badheadlocked;
405df8bae1dSRodney W. Grimes 		}
406582a7760SBruce Evans 		if (blackhole)
40761ffc0b1SJeffrey Hsu 			goto badheadlocked;
4081cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
4091cbd978eSLuigi Rizzo 			goto badheadlocked;
41004287599SRuslan Ermilov 		*ip = save_ip;
41104287599SRuslan Ermilov 		ip->ip_len += iphlen;
412582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
413f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
414df8bae1dSRodney W. Grimes 		return;
415df8bae1dSRodney W. Grimes 	}
41661ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
417936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
418936cd18dSAndre Oppermann 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
419936cd18dSAndre Oppermann 		goto badheadlocked;
420d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
421f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
4226b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
423df8bae1dSRodney W. Grimes 	return;
42461ffc0b1SJeffrey Hsu 
42561ffc0b1SJeffrey Hsu badheadlocked:
426f76fcf6dSJeffrey Hsu 	if (inp)
427f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
4286b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
429f76fcf6dSJeffrey Hsu badunlocked:
430df8bae1dSRodney W. Grimes 	m_freem(m);
431cfa1ca9dSYoshinobu Inoue 	return;
432cfa1ca9dSYoshinobu Inoue }
433cfa1ca9dSYoshinobu Inoue 
434cfa1ca9dSYoshinobu Inoue /*
435d4b509bdSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
436d4b509bdSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
437d4b509bdSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
438d4b509bdSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
439d4b509bdSRobert Watson  * into the socket code.
440cfa1ca9dSYoshinobu Inoue  */
441cfa1ca9dSYoshinobu Inoue static void
4428e3f3b16SRobert Watson udp_append(inp, ip, n, off, udp_in)
4438e3f3b16SRobert Watson 	struct inpcb *inp;
444032dcc76SLuigi Rizzo 	struct ip *ip;
445032dcc76SLuigi Rizzo 	struct mbuf *n;
446032dcc76SLuigi Rizzo 	int off;
447d4b509bdSRobert Watson 	struct sockaddr_in *udp_in;
448cfa1ca9dSYoshinobu Inoue {
449cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
4501e4d7da7SRobert Watson 	struct socket *so;
451cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
452c83c1318SPoul-Henning Kamp #ifdef INET6
453d4b509bdSRobert Watson 	struct sockaddr_in6 udp_in6;
454c83c1318SPoul-Henning Kamp #endif
455cfa1ca9dSYoshinobu Inoue 
4568e3f3b16SRobert Watson 	INP_LOCK_ASSERT(inp);
45787f2bb8cSRobert Watson 
458da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
459da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
4608e3f3b16SRobert Watson 	if (ipsec4_in_reject(n, inp)) {
461b9234fafSSam Leffler #ifdef IPSEC
462b9234fafSSam Leffler 		ipsecstat.in_polvio++;
463b9234fafSSam Leffler #endif /*IPSEC*/
464b9234fafSSam Leffler 		m_freem(n);
465b9234fafSSam Leffler 		return;
466b9234fafSSam Leffler 	}
467da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
468b9234fafSSam Leffler #ifdef MAC
4698e3f3b16SRobert Watson 	if (mac_check_inpcb_deliver(inp, n) != 0) {
470b9234fafSSam Leffler 		m_freem(n);
471b9234fafSSam Leffler 		return;
472b9234fafSSam Leffler 	}
473b9234fafSSam Leffler #endif
4748e3f3b16SRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
4758e3f3b16SRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
476cfa1ca9dSYoshinobu Inoue #ifdef INET6
4778e3f3b16SRobert Watson 		if (inp->inp_vflag & INP_IPV6) {
478cfa1ca9dSYoshinobu Inoue 			int savedflags;
479cfa1ca9dSYoshinobu Inoue 
4808e3f3b16SRobert Watson 			savedflags = inp->inp_flags;
4818e3f3b16SRobert Watson 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
4828e3f3b16SRobert Watson 			ip6_savecontrol(inp, n, &opts);
4838e3f3b16SRobert Watson 			inp->inp_flags = savedflags;
484cfa1ca9dSYoshinobu Inoue 		} else
485cfa1ca9dSYoshinobu Inoue #endif
4868e3f3b16SRobert Watson 		ip_savecontrol(inp, &opts, ip, n);
4874cc20ab1SSeigo Tanimura 	}
488cfa1ca9dSYoshinobu Inoue #ifdef INET6
4898e3f3b16SRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
490d4b509bdSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
491d4b509bdSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
492d4b509bdSRobert Watson 		udp_in6.sin6_family = AF_INET6;
493d4b509bdSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
494d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
495cfa1ca9dSYoshinobu Inoue 	} else
496cfa1ca9dSYoshinobu Inoue #endif
497d4b509bdSRobert Watson 	append_sa = (struct sockaddr *)udp_in;
498cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
4991e4d7da7SRobert Watson 
5008e3f3b16SRobert Watson 	so = inp->inp_socket;
5011e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
5021e4d7da7SRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
503cfa1ca9dSYoshinobu Inoue 		m_freem(n);
504cfa1ca9dSYoshinobu Inoue 		if (opts)
505cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
506cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
5071e4d7da7SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
5084cc20ab1SSeigo Tanimura 	} else
5091e4d7da7SRobert Watson 		sorwakeup_locked(so);
510df8bae1dSRodney W. Grimes }
511df8bae1dSRodney W. Grimes 
512df8bae1dSRodney W. Grimes /*
513df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
514df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
515df8bae1dSRodney W. Grimes  */
5163ce144eaSJeffrey Hsu struct inpcb *
517032dcc76SLuigi Rizzo udp_notify(inp, errno)
518032dcc76SLuigi Rizzo 	register struct inpcb *inp;
519032dcc76SLuigi Rizzo 	int errno;
520df8bae1dSRodney W. Grimes {
521df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
522df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
523df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5243ce144eaSJeffrey Hsu 	return inp;
525df8bae1dSRodney W. Grimes }
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes void
528032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip)
529032dcc76SLuigi Rizzo 	int cmd;
530032dcc76SLuigi Rizzo 	struct sockaddr *sa;
531032dcc76SLuigi Rizzo 	void *vip;
532df8bae1dSRodney W. Grimes {
533c693a045SJonathan Lemon 	struct ip *ip = vip;
534c693a045SJonathan Lemon 	struct udphdr *uh;
5353ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
536c693a045SJonathan Lemon 	struct in_addr faddr;
537c693a045SJonathan Lemon 	struct inpcb *inp;
538c693a045SJonathan Lemon 
539c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
540c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
541c693a045SJonathan Lemon 		return;
542df8bae1dSRodney W. Grimes 
54397d8d152SAndre Oppermann 	/*
54497d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
54597d8d152SAndre Oppermann 	 */
54697d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
54797d8d152SAndre Oppermann 		return;
54897d8d152SAndre Oppermann 	/*
54997d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
55097d8d152SAndre Oppermann 	 * XXX: We never get this from ICMP, otherwise it makes an
55197d8d152SAndre Oppermann 	 * excellent DoS attack on machines with many connections.
55297d8d152SAndre Oppermann 	 */
55397d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
554d1c54148SJesper Skriver 		ip = 0;
555d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
556df8bae1dSRodney W. Grimes 		return;
557df8bae1dSRodney W. Grimes 	if (ip) {
558df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
559f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
560c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
561c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
562f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
563f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
564f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
565c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
566f76fcf6dSJeffrey Hsu 			}
567f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
568f76fcf6dSJeffrey Hsu 		}
569f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
570df8bae1dSRodney W. Grimes 	} else
571f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
572df8bae1dSRodney W. Grimes }
573df8bae1dSRodney W. Grimes 
5740312fbe9SPoul-Henning Kamp static int
57582d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
57698271db4SGarrett Wollman {
577277afaffSRobert Watson 	int error, i, n;
57898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
57998271db4SGarrett Wollman 	inp_gen_t gencnt;
58098271db4SGarrett Wollman 	struct xinpgen xig;
58198271db4SGarrett Wollman 
58298271db4SGarrett Wollman 	/*
58398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
58498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
58598271db4SGarrett Wollman 	 */
58698271db4SGarrett Wollman 	if (req->oldptr == 0) {
58798271db4SGarrett Wollman 		n = udbinfo.ipi_count;
58898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
58998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
59098271db4SGarrett Wollman 		return 0;
59198271db4SGarrett Wollman 	}
59298271db4SGarrett Wollman 
59398271db4SGarrett Wollman 	if (req->newptr != 0)
59498271db4SGarrett Wollman 		return EPERM;
59598271db4SGarrett Wollman 
59698271db4SGarrett Wollman 	/*
59798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
59898271db4SGarrett Wollman 	 */
5994b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
60098271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
60198271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6024b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
60398271db4SGarrett Wollman 
60447934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6055c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
60647934cefSDon Lewis 	if (error != 0)
60747934cefSDon Lewis 		return (error);
6085c38b6dbSDon Lewis 
60998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
61098271db4SGarrett Wollman 	xig.xig_count = n;
61198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
61298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
61398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
61498271db4SGarrett Wollman 	if (error)
61598271db4SGarrett Wollman 		return error;
61698271db4SGarrett Wollman 
617a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
61898271db4SGarrett Wollman 	if (inp_list == 0)
61998271db4SGarrett Wollman 		return ENOMEM;
62098271db4SGarrett Wollman 
621f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
622fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
623fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
624f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6252ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6262ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
62798271db4SGarrett Wollman 			inp_list[i++] = inp;
628f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6294787fd37SPaul Saab 	}
630f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
63198271db4SGarrett Wollman 	n = i;
63298271db4SGarrett Wollman 
63398271db4SGarrett Wollman 	error = 0;
63498271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
63598271db4SGarrett Wollman 		inp = inp_list[i];
63698271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
63798271db4SGarrett Wollman 			struct xinpcb xi;
638fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
63998271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
64098271db4SGarrett Wollman 			/* XXX should avoid extra copy */
64198271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
64298271db4SGarrett Wollman 			if (inp->inp_socket)
64398271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6444b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
64598271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
64698271db4SGarrett Wollman 		}
64798271db4SGarrett Wollman 	}
64898271db4SGarrett Wollman 	if (!error) {
64998271db4SGarrett Wollman 		/*
65098271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
65198271db4SGarrett Wollman 		 * If the generation differs from what we told
65298271db4SGarrett Wollman 		 * her before, she knows that something happened
65398271db4SGarrett Wollman 		 * while we were processing this request, and it
65498271db4SGarrett Wollman 		 * might be necessary to retry.
65598271db4SGarrett Wollman 		 */
656f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
65798271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
65898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
65998271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
660f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
66198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
66298271db4SGarrett Wollman 	}
66398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
66498271db4SGarrett Wollman 	return error;
66598271db4SGarrett Wollman }
66698271db4SGarrett Wollman 
66798271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
66898271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
66998271db4SGarrett Wollman 
67098271db4SGarrett Wollman static int
67182d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
672490d50b6SBrian Feldman {
673c0511d3bSBrian Feldman 	struct xucred xuc;
674490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
675490d50b6SBrian Feldman 	struct inpcb *inp;
676277afaffSRobert Watson 	int error;
677490d50b6SBrian Feldman 
67856f21b9dSColin Percival 	error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
679490d50b6SBrian Feldman 	if (error)
680490d50b6SBrian Feldman 		return (error);
681490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
682490d50b6SBrian Feldman 	if (error)
683490d50b6SBrian Feldman 		return (error);
684f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
685490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
686cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6872d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
688490d50b6SBrian Feldman 		error = ENOENT;
689490d50b6SBrian Feldman 		goto out;
690490d50b6SBrian Feldman 	}
69129dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
6927ce87f12SDavid Malone 	if (error)
6937ce87f12SDavid Malone 		goto out;
69476183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
695490d50b6SBrian Feldman out:
696f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
6970e1eebb8SDon Lewis 	if (error == 0)
6980e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
699490d50b6SBrian Feldman 	return (error);
700490d50b6SBrian Feldman }
701490d50b6SBrian Feldman 
7027ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7037ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7047ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
705490d50b6SBrian Feldman 
706490d50b6SBrian Feldman static int
707032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
708032dcc76SLuigi Rizzo 	register struct inpcb *inp;
709032dcc76SLuigi Rizzo 	struct mbuf *m;
710032dcc76SLuigi Rizzo 	struct sockaddr *addr;
711032dcc76SLuigi Rizzo 	struct mbuf *control;
712032dcc76SLuigi Rizzo 	struct thread *td;
713df8bae1dSRodney W. Grimes {
714032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
715032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
71690162a4eSIan Dowse 	struct in_addr faddr, laddr;
717c557ae16SIan Dowse 	struct cmsghdr *cm;
718c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
71990162a4eSIan Dowse 	int error = 0;
7208afa2304SBruce M Simpson 	int ipflags;
72190162a4eSIan Dowse 	u_short fport, lport;
7225c32ea65SRobert Watson 	int unlock_udbinfo;
723df8bae1dSRodney W. Grimes 
7245c32ea65SRobert Watson 	/*
7255c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
7265c32ea65SRobert Watson 	 * inpcb.  As such, we don't know up front what inpcb locks we will
7275c32ea65SRobert Watson 	 * need.  Do any work to decide what is needed up front before
7285c32ea65SRobert Watson 	 * acquiring locks.
7295c32ea65SRobert Watson 	 */
730430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
731c557ae16SIan Dowse 		if (control)
732c557ae16SIan Dowse 			m_freem(control);
7335c32ea65SRobert Watson 		m_freem(m);
7345c32ea65SRobert Watson 		return EMSGSIZE;
735430d30d8SBill Fenner 	}
736430d30d8SBill Fenner 
737c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
738c557ae16SIan Dowse 	if (control != NULL) {
739c557ae16SIan Dowse 		/*
740c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
741c557ae16SIan Dowse 		 * is stored in a single mbuf.
742c557ae16SIan Dowse 		 */
743c557ae16SIan Dowse 		if (control->m_next) {
744c557ae16SIan Dowse 			m_freem(control);
7455c32ea65SRobert Watson 			m_freem(m);
7465c32ea65SRobert Watson 			return EINVAL;
747c557ae16SIan Dowse 		}
748c557ae16SIan Dowse 		for (; control->m_len > 0;
749c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
750c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
751c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
752c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
753c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
754c557ae16SIan Dowse 				error = EINVAL;
755c557ae16SIan Dowse 				break;
756c557ae16SIan Dowse 			}
757c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
758c557ae16SIan Dowse 				continue;
759c557ae16SIan Dowse 
760c557ae16SIan Dowse 			switch (cm->cmsg_type) {
761c557ae16SIan Dowse 			case IP_SENDSRCADDR:
762c557ae16SIan Dowse 				if (cm->cmsg_len !=
763c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
764c557ae16SIan Dowse 					error = EINVAL;
765c557ae16SIan Dowse 					break;
766c557ae16SIan Dowse 				}
767c557ae16SIan Dowse 				bzero(&src, sizeof(src));
768c557ae16SIan Dowse 				src.sin_family = AF_INET;
769c557ae16SIan Dowse 				src.sin_len = sizeof(src);
770c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
771c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
772c557ae16SIan Dowse 				break;
773c557ae16SIan Dowse 			default:
774c557ae16SIan Dowse 				error = ENOPROTOOPT;
775c557ae16SIan Dowse 				break;
776c557ae16SIan Dowse 			}
777c557ae16SIan Dowse 			if (error)
778c557ae16SIan Dowse 				break;
779c557ae16SIan Dowse 		}
780c557ae16SIan Dowse 		m_freem(control);
781c557ae16SIan Dowse 	}
7825c32ea65SRobert Watson 	if (error) {
7835c32ea65SRobert Watson 		m_freem(m);
7845c32ea65SRobert Watson 		return error;
7855c32ea65SRobert Watson 	}
7865c32ea65SRobert Watson 
7875c32ea65SRobert Watson 	if (src.sin_addr.s_addr != INADDR_ANY ||
7885c32ea65SRobert Watson 	    addr != NULL) {
7895c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
7905c32ea65SRobert Watson 		unlock_udbinfo = 1;
7915c32ea65SRobert Watson 	} else
7925c32ea65SRobert Watson 		unlock_udbinfo = 0;
7935c32ea65SRobert Watson 	INP_LOCK(inp);
7945c32ea65SRobert Watson 
7955c32ea65SRobert Watson #ifdef MAC
7965c32ea65SRobert Watson 	mac_create_mbuf_from_inpcb(inp, m);
7975c32ea65SRobert Watson #endif
7985c32ea65SRobert Watson 
79990162a4eSIan Dowse 	laddr = inp->inp_laddr;
80090162a4eSIan Dowse 	lport = inp->inp_lport;
801c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
802c557ae16SIan Dowse 		if (lport == 0) {
803c557ae16SIan Dowse 			error = EINVAL;
804c557ae16SIan Dowse 			goto release;
805c557ae16SIan Dowse 		}
806c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
807b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
808c557ae16SIan Dowse 		if (error)
809c557ae16SIan Dowse 			goto release;
810c557ae16SIan Dowse 	}
811c557ae16SIan Dowse 
812df8bae1dSRodney W. Grimes 	if (addr) {
81375c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
814812d8653SSam Leffler 		if (jailed(td->td_ucred))
815a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
816df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
817df8bae1dSRodney W. Grimes 			error = EISCONN;
818df8bae1dSRodney W. Grimes 			goto release;
819df8bae1dSRodney W. Grimes 		}
82090162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
821b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
82290162a4eSIan Dowse 		if (error)
82390162a4eSIan Dowse 			goto release;
82490162a4eSIan Dowse 
82590162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
82690162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
82790162a4eSIan Dowse 		    inp->inp_lport == 0) {
8283a1757b9SGleb Smirnoff 			/*
8293a1757b9SGleb Smirnoff 			 * Remember addr if jailed, to prevent rebinding.
8303a1757b9SGleb Smirnoff 			 */
8313a1757b9SGleb Smirnoff 			if (jailed(td->td_ucred))
8323a1757b9SGleb Smirnoff 				inp->inp_laddr = laddr;
83390162a4eSIan Dowse 			inp->inp_lport = lport;
83490162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
83590162a4eSIan Dowse 				inp->inp_lport = 0;
83690162a4eSIan Dowse 				error = EAGAIN;
837df8bae1dSRodney W. Grimes 				goto release;
838df8bae1dSRodney W. Grimes 			}
83990162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
84090162a4eSIan Dowse 		}
841df8bae1dSRodney W. Grimes 	} else {
84290162a4eSIan Dowse 		faddr = inp->inp_faddr;
84390162a4eSIan Dowse 		fport = inp->inp_fport;
84490162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
845df8bae1dSRodney W. Grimes 			error = ENOTCONN;
846df8bae1dSRodney W. Grimes 			goto release;
847df8bae1dSRodney W. Grimes 		}
848df8bae1dSRodney W. Grimes 	}
849e6ccd709SRobert Watson 
850df8bae1dSRodney W. Grimes 	/*
851e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
852392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
853392e8407SRobert Watson 	 * since we won't use that space at this layer.
854df8bae1dSRodney W. Grimes 	 */
855e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
856e6ccd709SRobert Watson 	if (m == NULL) {
857df8bae1dSRodney W. Grimes 		error = ENOBUFS;
85849b19bfcSBruce M Simpson 		goto release;
859df8bae1dSRodney W. Grimes 	}
860e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
861e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
862392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
863df8bae1dSRodney W. Grimes 
864df8bae1dSRodney W. Grimes 	/*
865df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
866df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
867df8bae1dSRodney W. Grimes 	 */
868df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
869db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
870df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
87190162a4eSIan Dowse 	ui->ui_src = laddr;
87290162a4eSIan Dowse 	ui->ui_dst = faddr;
87390162a4eSIan Dowse 	ui->ui_sport = lport;
87490162a4eSIan Dowse 	ui->ui_dport = fport;
875db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
876df8bae1dSRodney W. Grimes 
877b2828ad2SAndre Oppermann 	/*
878b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
879b2828ad2SAndre Oppermann 	 */
880b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
881b2828ad2SAndre Oppermann 		struct ip *ip;
882b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
883b2828ad2SAndre Oppermann 		ip->ip_off |= IP_DF;
884b2828ad2SAndre Oppermann 	}
885b2828ad2SAndre Oppermann 
886b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
887b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
888b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
889b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
890b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
891d46ff6bdSMaxim Konovalov 	if (inp->inp_vflag & INP_ONESBCAST)
8928afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
8938afa2304SBruce M Simpson 
894df8bae1dSRodney W. Grimes 	/*
895db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
896df8bae1dSRodney W. Grimes 	 */
897df8bae1dSRodney W. Grimes 	if (udpcksum) {
898d46ff6bdSMaxim Konovalov 		if (inp->inp_vflag & INP_ONESBCAST)
8998a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
9008a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
901db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
902db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
903db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
904db4f9cc7SJonathan Lemon 	} else {
905db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
906df8bae1dSRodney W. Grimes 	}
907df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
908ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
909ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
910df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
911cfa1ca9dSYoshinobu Inoue 
9125c32ea65SRobert Watson 	if (unlock_udbinfo)
9135c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
91497d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
9155d846453SSam Leffler 	    inp->inp_moptions, inp);
9165c32ea65SRobert Watson 	INP_UNLOCK(inp);
917df8bae1dSRodney W. Grimes 	return (error);
918df8bae1dSRodney W. Grimes 
919df8bae1dSRodney W. Grimes release:
9205c32ea65SRobert Watson 	INP_UNLOCK(inp);
9215c32ea65SRobert Watson 	if (unlock_udbinfo)
9225c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
923df8bae1dSRodney W. Grimes 	m_freem(m);
924df8bae1dSRodney W. Grimes 	return (error);
925df8bae1dSRodney W. Grimes }
926df8bae1dSRodney W. Grimes 
92776429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
928032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
929e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
9303d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
9310312fbe9SPoul-Henning Kamp 
932cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
933cfa1ca9dSYoshinobu Inoue #ifdef INET6
934cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
935cfa1ca9dSYoshinobu Inoue #else
936cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
937cfa1ca9dSYoshinobu Inoue #endif
938cfa1ca9dSYoshinobu Inoue 				      );
939e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9400ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
941df8bae1dSRodney W. Grimes 
942ac45e92fSRobert Watson static void
943d0390e05SGarrett Wollman udp_abort(struct socket *so)
944df8bae1dSRodney W. Grimes {
945d0390e05SGarrett Wollman 	struct inpcb *inp;
946df8bae1dSRodney W. Grimes 
947d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
94814ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
94914ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
950f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
951d0390e05SGarrett Wollman 	soisdisconnected(so);
952d0390e05SGarrett Wollman 	in_pcbdetach(inp);
95314ba8addSRobert Watson 	in_pcbfree(inp);
954f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
955df8bae1dSRodney W. Grimes }
956df8bae1dSRodney W. Grimes 
957d0390e05SGarrett Wollman static int
958b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
959d0390e05SGarrett Wollman {
960d0390e05SGarrett Wollman 	struct inpcb *inp;
961277afaffSRobert Watson 	int error;
962d0390e05SGarrett Wollman 
963d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
96414ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
96514ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
966cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
967f76fcf6dSJeffrey Hsu 	if (error) {
968f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
969cfa1ca9dSYoshinobu Inoue 		return error;
970f76fcf6dSJeffrey Hsu 	}
9716823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &udbinfo, "udpinp");
97253b57cd1SSam Leffler 	if (error) {
97353b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
974d0390e05SGarrett Wollman 		return error;
97553b57cd1SSam Leffler 	}
976cfa1ca9dSYoshinobu Inoue 
977cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
978f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
979f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
980cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
981cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
982f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
983d0390e05SGarrett Wollman 	return 0;
984df8bae1dSRodney W. Grimes }
985d0390e05SGarrett Wollman 
986d0390e05SGarrett Wollman static int
987b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
988d0390e05SGarrett Wollman {
989d0390e05SGarrett Wollman 	struct inpcb *inp;
990277afaffSRobert Watson 	int error;
991d0390e05SGarrett Wollman 
992d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
99314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
99414ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
995f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
996b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
997f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
998f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
999d0390e05SGarrett Wollman 	return error;
1000d0390e05SGarrett Wollman }
1001d0390e05SGarrett Wollman 
1002d0390e05SGarrett Wollman static int
1003b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1004d0390e05SGarrett Wollman {
1005d0390e05SGarrett Wollman 	struct inpcb *inp;
1006277afaffSRobert Watson 	int error;
100775c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1008d0390e05SGarrett Wollman 
1009d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
101014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
101114ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1012f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1013f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1014f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1015f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1016d0390e05SGarrett Wollman 		return EISCONN;
1017f76fcf6dSJeffrey Hsu 	}
101875c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1019812d8653SSam Leffler 	if (jailed(td->td_ucred))
1020a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1021b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
10224cc20ab1SSeigo Tanimura 	if (error == 0)
1023df8bae1dSRodney W. Grimes 		soisconnected(so);
1024f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1025f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1026d0390e05SGarrett Wollman 	return error;
1027df8bae1dSRodney W. Grimes }
1028d0390e05SGarrett Wollman 
1029bc725eafSRobert Watson static void
1030d0390e05SGarrett Wollman udp_detach(struct socket *so)
1031d0390e05SGarrett Wollman {
1032d0390e05SGarrett Wollman 	struct inpcb *inp;
1033d0390e05SGarrett Wollman 
1034d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
103514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
103614ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1037f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1038d0390e05SGarrett Wollman 	in_pcbdetach(inp);
103914ba8addSRobert Watson 	in_pcbfree(inp);
1040f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1041d0390e05SGarrett Wollman }
1042d0390e05SGarrett Wollman 
1043d0390e05SGarrett Wollman static int
1044d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1045d0390e05SGarrett Wollman {
1046d0390e05SGarrett Wollman 	struct inpcb *inp;
1047d0390e05SGarrett Wollman 
1048d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
104914ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
105014ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1051f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1052f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1053f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1054f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1055d0390e05SGarrett Wollman 		return ENOTCONN;
1056f76fcf6dSJeffrey Hsu 	}
1057d0390e05SGarrett Wollman 
1058df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1059df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1060d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1061d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1062d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
1063f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1064f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1065d0390e05SGarrett Wollman 	return 0;
1066df8bae1dSRodney W. Grimes }
1067df8bae1dSRodney W. Grimes 
1068d0390e05SGarrett Wollman static int
106957bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1070b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1071d0390e05SGarrett Wollman {
1072d0390e05SGarrett Wollman 	struct inpcb *inp;
1073d0390e05SGarrett Wollman 
1074d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
107514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
10765c32ea65SRobert Watson 	return udp_output(inp, m, addr, control, td);
1077d0390e05SGarrett Wollman }
1078d0390e05SGarrett Wollman 
107976429de4SYoshinobu Inoue int
1080d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1081d0390e05SGarrett Wollman {
1082d0390e05SGarrett Wollman 	struct inpcb *inp;
1083d0390e05SGarrett Wollman 
1084d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
108514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1086f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1087d0390e05SGarrett Wollman 	socantsendmore(so);
1088f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1089d0390e05SGarrett Wollman 	return 0;
1090d0390e05SGarrett Wollman }
1091d0390e05SGarrett Wollman 
1092f76fcf6dSJeffrey Hsu /*
1093f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1094f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1095f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1096f76fcf6dSJeffrey Hsu  */
1097f76fcf6dSJeffrey Hsu static int
1098f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1099f76fcf6dSJeffrey Hsu {
1100f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1101f76fcf6dSJeffrey Hsu }
1102f76fcf6dSJeffrey Hsu 
1103f76fcf6dSJeffrey Hsu /*
1104f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1105f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1106f76fcf6dSJeffrey Hsu  */
1107f76fcf6dSJeffrey Hsu static int
1108f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1109f76fcf6dSJeffrey Hsu {
1110f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1111f76fcf6dSJeffrey Hsu }
1112f76fcf6dSJeffrey Hsu 
1113d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1114756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1115756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1116756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1117756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1118756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1119756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1120756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
1121756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		udp_peeraddr,
1122756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
112359b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1124756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
1125756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		udp_sockaddr,
1126756d52a1SPoul-Henning Kamp 	.pru_sosetlabel =	in_pcbsosetlabel
1127d0390e05SGarrett Wollman };
1128