xref: /freebsd/sys/netinet/udp_usrreq.c (revision bc725eafc7f7d0cb3d3746b71b3cad9b2bc9d499)
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>
41960ed29cSSeigo Tanimura #include <sys/jail.h>
42b110a8a2SGarrett Wollman #include <sys/kernel.h>
43960ed29cSSeigo Tanimura #include <sys/lock.h>
44bdb3fa18SRobert Watson #include <sys/mac.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47490d50b6SBrian Feldman #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49960ed29cSSeigo Tanimura #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52960ed29cSSeigo Tanimura #include <sys/sx.h>
53b5e8ce9fSBruce Evans #include <sys/sysctl.h>
54816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
558781d8e9SBruce Evans 
5669c2d429SJeff Roberson #include <vm/uma.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
63960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
64960ed29cSSeigo Tanimura #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip.h>
66cfa1ca9dSYoshinobu Inoue #ifdef INET6
67cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
68cfa1ca9dSYoshinobu Inoue #endif
69960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
70960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
72ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
73cfa1ca9dSYoshinobu Inoue #ifdef INET6
74cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
75cfa1ca9dSYoshinobu Inoue #endif
76df8bae1dSRodney W. Grimes #include <netinet/udp.h>
77df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
78df8bae1dSRodney W. Grimes 
79b9234fafSSam Leffler #ifdef FAST_IPSEC
80b9234fafSSam Leffler #include <netipsec/ipsec.h>
81b9234fafSSam Leffler #endif /*FAST_IPSEC*/
82b9234fafSSam Leffler 
83cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
84cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
85cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
86cfa1ca9dSYoshinobu Inoue 
87db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
88db4f9cc7SJonathan Lemon 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  * UDP protocol implementation.
91df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
92df8bae1dSRodney W. Grimes  */
93df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
940312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
95df8bae1dSRodney W. Grimes #else
960312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
97df8bae1dSRodney W. Grimes #endif
980312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
990312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
100df8bae1dSRodney W. Grimes 
101032dcc76SLuigi Rizzo int	log_in_vain = 0;
102816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1033d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
104816a3d83SPoul-Henning Kamp 
105032dcc76SLuigi Rizzo static int	blackhole = 0;
10616f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10716f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
10816f7f31fSGeoff Rehmet 
10983453a06SBruce M Simpson static int	strict_mcast_mship = 0;
11083453a06SBruce M Simpson SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
11183453a06SBruce M Simpson 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
11283453a06SBruce M Simpson 
11376429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
114cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1157a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
11615bd2b43SDavid Greenman 
11715bd2b43SDavid Greenman #ifndef UDBHASHSIZE
118c3229e05SDavid Greenman #define UDBHASHSIZE 16
11915bd2b43SDavid Greenman #endif
12015bd2b43SDavid Greenman 
12176429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
122c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1233d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
124f2ea20e6SGarrett Wollman 
125c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
126d4b509bdSRobert Watson 		int off, struct sockaddr_in *udp_in);
127cfa1ca9dSYoshinobu Inoue 
128bc725eafSRobert Watson static void udp_detach(struct socket *so);
1294d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1304d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes void
133032dcc76SLuigi Rizzo udp_init()
134df8bae1dSRodney W. Grimes {
135f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
13615bd2b43SDavid Greenman 	LIST_INIT(&udb);
13715bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
138ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1398781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1408781d8e9SBruce Evans 					&udbinfo.porthashmask);
14169c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
142420a2811SAndre Oppermann 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
14369c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
144df8bae1dSRodney W. Grimes }
145df8bae1dSRodney W. Grimes 
146df8bae1dSRodney W. Grimes void
147032dcc76SLuigi Rizzo udp_input(m, off)
148032dcc76SLuigi Rizzo 	register struct mbuf *m;
149032dcc76SLuigi Rizzo 	int off;
150df8bae1dSRodney W. Grimes {
151cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
152032dcc76SLuigi Rizzo 	register struct ip *ip;
153032dcc76SLuigi Rizzo 	register struct udphdr *uh;
154032dcc76SLuigi Rizzo 	register struct inpcb *inp;
155df8bae1dSRodney W. Grimes 	int len;
156df8bae1dSRodney W. Grimes 	struct ip save_ip;
157d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
1580b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
1590b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
1600b4ae859SGleb Smirnoff #endif
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes 	/*
165df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
166df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
167df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
168df8bae1dSRodney W. Grimes 	 * with options still present.
169df8bae1dSRodney W. Grimes 	 */
170df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
171df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
172df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
173df8bae1dSRodney W. Grimes 	}
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	/*
176df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
177df8bae1dSRodney W. Grimes 	 */
178df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
179df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
180df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
181df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
182df8bae1dSRodney W. Grimes 			return;
183df8bae1dSRodney W. Grimes 		}
184df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
185df8bae1dSRodney W. Grimes 	}
186df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
187df8bae1dSRodney W. Grimes 
188686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
189686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
190f76fcf6dSJeffrey Hsu 		goto badunlocked;
191686cdd19SJun-ichiro itojun Hagino 
192df8bae1dSRodney W. Grimes 	/*
193b9234fafSSam Leffler 	 * Construct sockaddr format source address.
194b9234fafSSam Leffler 	 * Stuff source address and datagram in user buffer.
195b9234fafSSam Leffler 	 */
196d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
197d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
198d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
199b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
200b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
201b9234fafSSam Leffler 
202b9234fafSSam Leffler 	/*
203df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
204df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
205df8bae1dSRodney W. Grimes 	 */
206df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
207df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2087eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
209df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
210f76fcf6dSJeffrey Hsu 			goto badunlocked;
211df8bae1dSRodney W. Grimes 		}
212df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
213df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
214df8bae1dSRodney W. Grimes 	}
215df8bae1dSRodney W. Grimes 	/*
216df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
217df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
218df8bae1dSRodney W. Grimes 	 */
21948cb400fSRuslan Ermilov 	if (!blackhole)
220df8bae1dSRodney W. Grimes 		save_ip = *ip;
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes 	/*
223df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
224df8bae1dSRodney W. Grimes 	 */
2256dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
226db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
227db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
228db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
229db4f9cc7SJonathan Lemon 			else
230db4f9cc7SJonathan Lemon 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
231506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
232db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
233db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
234db4f9cc7SJonathan Lemon 		} else {
235cb342100SHajimu UMEMOTO 			char b[9];
236cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2376effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
238df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
239623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
240cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
241db4f9cc7SJonathan Lemon 		}
242623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
243df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
244df8bae1dSRodney W. Grimes 			m_freem(m);
245df8bae1dSRodney W. Grimes 			return;
246df8bae1dSRodney W. Grimes 		}
247fb9aaba0SRuslan Ermilov 	} else
248fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
249df8bae1dSRodney W. Grimes 
2500b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
2510b4ae859SGleb Smirnoff 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
2520b4ae859SGleb Smirnoff 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
2530b4ae859SGleb Smirnoff 
2540b4ae859SGleb Smirnoff 	if (fwd_tag != NULL) {
2550b4ae859SGleb Smirnoff 		struct sockaddr_in *next_hop;
2560b4ae859SGleb Smirnoff 
2570b4ae859SGleb Smirnoff 		/* Do the hack. */
2580b4ae859SGleb Smirnoff 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
2590b4ae859SGleb Smirnoff 		ip->ip_dst = next_hop->sin_addr;
2600b4ae859SGleb Smirnoff 		uh->uh_dport = ntohs(next_hop->sin_port);
2610b4ae859SGleb Smirnoff 		/* Remove the tag from the packet.  We don't need it anymore. */
2620b4ae859SGleb Smirnoff 		m_tag_delete(m, fwd_tag);
2630b4ae859SGleb Smirnoff 	}
2640b4ae859SGleb Smirnoff #endif
2650b4ae859SGleb Smirnoff 
266f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
267f76fcf6dSJeffrey Hsu 
268df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
269df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
27082c23ebaSBill Fenner 		struct inpcb *last;
271df8bae1dSRodney W. Grimes 		/*
272df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
273df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
274df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
275df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
276df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
277df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
278df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
279df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
280df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
281df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
282df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
283df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
284df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
285df8bae1dSRodney W. Grimes 		 */
286df8bae1dSRodney W. Grimes 
287df8bae1dSRodney W. Grimes 		/*
288df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
289df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
290df8bae1dSRodney W. Grimes 		 */
291df8bae1dSRodney W. Grimes 		last = NULL;
292cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
2939c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
294f76fcf6dSJeffrey Hsu 				continue;
295cfa1ca9dSYoshinobu Inoue #ifdef INET6
296369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2979c1df695SRobert Watson 				continue;
298cfa1ca9dSYoshinobu Inoue #endif
299df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
300f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3019c1df695SRobert Watson 					continue;
302df8bae1dSRodney W. Grimes 			}
303df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
304df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
305df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
306df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
3079c1df695SRobert Watson 					continue;
308df8bae1dSRodney W. Grimes 			}
3099c1df695SRobert Watson 			INP_LOCK(inp);
310df8bae1dSRodney W. Grimes 
31183453a06SBruce M Simpson 			/*
31283453a06SBruce M Simpson 			 * Check multicast packets to make sure they are only
31383453a06SBruce M Simpson 			 * sent to sockets with multicast memberships for the
31483453a06SBruce M Simpson 			 * packet's destination address and arrival interface
31583453a06SBruce M Simpson 			 */
31683453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
31783453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
31883453a06SBruce M Simpson 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
31983453a06SBruce M Simpson 				int mship, foundmship = 0;
32083453a06SBruce M Simpson 
32183453a06SBruce M Simpson 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
32283453a06SBruce M Simpson 					if (MSHIP(inp, mship)->inm_addr.s_addr
32383453a06SBruce M Simpson 					    == ip->ip_dst.s_addr &&
32483453a06SBruce M Simpson 					    MSHIP(inp, mship)->inm_ifp
32583453a06SBruce M Simpson 					    == m->m_pkthdr.rcvif) {
32683453a06SBruce M Simpson 						foundmship = 1;
32783453a06SBruce M Simpson 						break;
32883453a06SBruce M Simpson 					}
32983453a06SBruce M Simpson 				}
3309c1df695SRobert Watson 				if (foundmship == 0) {
3319c1df695SRobert Watson 					INP_UNLOCK(inp);
3329c1df695SRobert Watson 					continue;
3339c1df695SRobert Watson 				}
33483453a06SBruce M Simpson 			}
33583453a06SBruce M Simpson #undef NMSHIPS
33683453a06SBruce M Simpson #undef MSHIP
337df8bae1dSRodney W. Grimes 			if (last != NULL) {
338df8bae1dSRodney W. Grimes 				struct mbuf *n;
339df8bae1dSRodney W. Grimes 
340032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
341365433d9SRobert Watson 				if (n != NULL)
342cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
343cfa1ca9dSYoshinobu Inoue 						   iphlen +
344d4b509bdSRobert Watson 						   sizeof(struct udphdr),
345d4b509bdSRobert Watson 						   &udp_in);
346f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
347df8bae1dSRodney W. Grimes 			}
34882c23ebaSBill Fenner 			last = inp;
349df8bae1dSRodney W. Grimes 			/*
350df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
351df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
352df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
353df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
354df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
355df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
356df8bae1dSRodney W. Grimes 			 */
3574cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
358df8bae1dSRodney W. Grimes 				break;
359df8bae1dSRodney W. Grimes 		}
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes 		if (last == NULL) {
362df8bae1dSRodney W. Grimes 			/*
363df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
364df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
365df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
366df8bae1dSRodney W. Grimes 			 */
367df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
36861ffc0b1SJeffrey Hsu 			goto badheadlocked;
369df8bae1dSRodney W. Grimes 		}
370d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
371d4b509bdSRobert Watson 		    &udp_in);
3729b657230SSam Leffler 		INP_UNLOCK(last);
3736b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
374df8bae1dSRodney W. Grimes 		return;
375df8bae1dSRodney W. Grimes 	}
376df8bae1dSRodney W. Grimes 	/*
3776d6a026bSDavid Greenman 	 * Locate pcb for datagram.
378df8bae1dSRodney W. Grimes 	 */
379c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
380cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
38115bd2b43SDavid Greenman 	if (inp == NULL) {
38275cfc95fSAndrey A. Chernov 		if (log_in_vain) {
383df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
38475cfc95fSAndrey A. Chernov 
38575cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
386592071e8SBruce Evans 			log(LOG_INFO,
387592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
388592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
389592071e8SBruce Evans 			    ntohs(uh->uh_sport));
39075cfc95fSAndrey A. Chernov 		}
391df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
392df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
393df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
39461ffc0b1SJeffrey Hsu 			goto badheadlocked;
395df8bae1dSRodney W. Grimes 		}
396582a7760SBruce Evans 		if (blackhole)
39761ffc0b1SJeffrey Hsu 			goto badheadlocked;
3981cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
3991cbd978eSLuigi Rizzo 			goto badheadlocked;
40004287599SRuslan Ermilov 		*ip = save_ip;
40104287599SRuslan Ermilov 		ip->ip_len += iphlen;
402582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
403f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
404df8bae1dSRodney W. Grimes 		return;
405df8bae1dSRodney W. Grimes 	}
40661ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
407936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
408936cd18dSAndre Oppermann 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
409936cd18dSAndre Oppermann 		goto badheadlocked;
410d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
411f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
4126b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
413df8bae1dSRodney W. Grimes 	return;
41461ffc0b1SJeffrey Hsu 
41561ffc0b1SJeffrey Hsu badheadlocked:
416f76fcf6dSJeffrey Hsu 	if (inp)
417f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
4186b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
419f76fcf6dSJeffrey Hsu badunlocked:
420df8bae1dSRodney W. Grimes 	m_freem(m);
421cfa1ca9dSYoshinobu Inoue 	return;
422cfa1ca9dSYoshinobu Inoue }
423cfa1ca9dSYoshinobu Inoue 
424cfa1ca9dSYoshinobu Inoue /*
425d4b509bdSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
426d4b509bdSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
427d4b509bdSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
428d4b509bdSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
429d4b509bdSRobert Watson  * into the socket code.
430cfa1ca9dSYoshinobu Inoue  */
431cfa1ca9dSYoshinobu Inoue static void
432d4b509bdSRobert Watson udp_append(last, ip, n, off, udp_in)
433032dcc76SLuigi Rizzo 	struct inpcb *last;
434032dcc76SLuigi Rizzo 	struct ip *ip;
435032dcc76SLuigi Rizzo 	struct mbuf *n;
436032dcc76SLuigi Rizzo 	int off;
437d4b509bdSRobert Watson 	struct sockaddr_in *udp_in;
438cfa1ca9dSYoshinobu Inoue {
439cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
4401e4d7da7SRobert Watson 	struct socket *so;
441cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
442c83c1318SPoul-Henning Kamp #ifdef INET6
443d4b509bdSRobert Watson 	struct sockaddr_in6 udp_in6;
444c83c1318SPoul-Henning Kamp #endif
445cfa1ca9dSYoshinobu Inoue 
44687f2bb8cSRobert Watson 	INP_LOCK_ASSERT(last);
44787f2bb8cSRobert Watson 
448da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
449da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
450da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
451b9234fafSSam Leffler #ifdef IPSEC
452b9234fafSSam Leffler 		ipsecstat.in_polvio++;
453b9234fafSSam Leffler #endif /*IPSEC*/
454b9234fafSSam Leffler 		m_freem(n);
455b9234fafSSam Leffler 		return;
456b9234fafSSam Leffler 	}
457da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
458b9234fafSSam Leffler #ifdef MAC
459a557af22SRobert Watson 	if (mac_check_inpcb_deliver(last, n) != 0) {
460b9234fafSSam Leffler 		m_freem(n);
461b9234fafSSam Leffler 		return;
462b9234fafSSam Leffler 	}
463b9234fafSSam Leffler #endif
464cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
465be8a62e8SPoul-Henning Kamp 	    last->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
466cfa1ca9dSYoshinobu Inoue #ifdef INET6
467cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
468cfa1ca9dSYoshinobu Inoue 			int savedflags;
469cfa1ca9dSYoshinobu Inoue 
470cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
471cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
47211de19f4SHajimu UMEMOTO 			ip6_savecontrol(last, n, &opts);
473cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
474cfa1ca9dSYoshinobu Inoue 		} else
475cfa1ca9dSYoshinobu Inoue #endif
476cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4774cc20ab1SSeigo Tanimura 	}
478cfa1ca9dSYoshinobu Inoue #ifdef INET6
479cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
480d4b509bdSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
481d4b509bdSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
482d4b509bdSRobert Watson 		udp_in6.sin6_family = AF_INET6;
483d4b509bdSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
484d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
485cfa1ca9dSYoshinobu Inoue 	} else
486cfa1ca9dSYoshinobu Inoue #endif
487d4b509bdSRobert Watson 	append_sa = (struct sockaddr *)udp_in;
488cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
4891e4d7da7SRobert Watson 
4901e4d7da7SRobert Watson 	so = last->inp_socket;
4911e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
4921e4d7da7SRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
493cfa1ca9dSYoshinobu Inoue 		m_freem(n);
494cfa1ca9dSYoshinobu Inoue 		if (opts)
495cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
496cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
4971e4d7da7SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
4984cc20ab1SSeigo Tanimura 	} else
4991e4d7da7SRobert Watson 		sorwakeup_locked(so);
500df8bae1dSRodney W. Grimes }
501df8bae1dSRodney W. Grimes 
502df8bae1dSRodney W. Grimes /*
503df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
504df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
505df8bae1dSRodney W. Grimes  */
5063ce144eaSJeffrey Hsu struct inpcb *
507032dcc76SLuigi Rizzo udp_notify(inp, errno)
508032dcc76SLuigi Rizzo 	register struct inpcb *inp;
509032dcc76SLuigi Rizzo 	int errno;
510df8bae1dSRodney W. Grimes {
511df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
512df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
513df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5143ce144eaSJeffrey Hsu 	return inp;
515df8bae1dSRodney W. Grimes }
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes void
518032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip)
519032dcc76SLuigi Rizzo 	int cmd;
520032dcc76SLuigi Rizzo 	struct sockaddr *sa;
521032dcc76SLuigi Rizzo 	void *vip;
522df8bae1dSRodney W. Grimes {
523c693a045SJonathan Lemon 	struct ip *ip = vip;
524c693a045SJonathan Lemon 	struct udphdr *uh;
5253ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
526c693a045SJonathan Lemon 	struct in_addr faddr;
527c693a045SJonathan Lemon 	struct inpcb *inp;
528c693a045SJonathan Lemon 
529c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
530c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
531c693a045SJonathan Lemon 		return;
532df8bae1dSRodney W. Grimes 
53397d8d152SAndre Oppermann 	/*
53497d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
53597d8d152SAndre Oppermann 	 */
53697d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
53797d8d152SAndre Oppermann 		return;
53897d8d152SAndre Oppermann 	/*
53997d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
54097d8d152SAndre Oppermann 	 * XXX: We never get this from ICMP, otherwise it makes an
54197d8d152SAndre Oppermann 	 * excellent DoS attack on machines with many connections.
54297d8d152SAndre Oppermann 	 */
54397d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
544d1c54148SJesper Skriver 		ip = 0;
545d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
546df8bae1dSRodney W. Grimes 		return;
547df8bae1dSRodney W. Grimes 	if (ip) {
548df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
549f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
550c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
551c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
552f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
553f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
554f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
555c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
556f76fcf6dSJeffrey Hsu 			}
557f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
558f76fcf6dSJeffrey Hsu 		}
559f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
560df8bae1dSRodney W. Grimes 	} else
561f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
562df8bae1dSRodney W. Grimes }
563df8bae1dSRodney W. Grimes 
5640312fbe9SPoul-Henning Kamp static int
56582d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
56698271db4SGarrett Wollman {
567277afaffSRobert Watson 	int error, i, n;
56898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
56998271db4SGarrett Wollman 	inp_gen_t gencnt;
57098271db4SGarrett Wollman 	struct xinpgen xig;
57198271db4SGarrett Wollman 
57298271db4SGarrett Wollman 	/*
57398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
57498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
57598271db4SGarrett Wollman 	 */
57698271db4SGarrett Wollman 	if (req->oldptr == 0) {
57798271db4SGarrett Wollman 		n = udbinfo.ipi_count;
57898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
57998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
58098271db4SGarrett Wollman 		return 0;
58198271db4SGarrett Wollman 	}
58298271db4SGarrett Wollman 
58398271db4SGarrett Wollman 	if (req->newptr != 0)
58498271db4SGarrett Wollman 		return EPERM;
58598271db4SGarrett Wollman 
58698271db4SGarrett Wollman 	/*
58798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
58898271db4SGarrett Wollman 	 */
5894b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
59098271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
59198271db4SGarrett Wollman 	n = udbinfo.ipi_count;
5924b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
59398271db4SGarrett Wollman 
59447934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
5955c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
59647934cefSDon Lewis 	if (error != 0)
59747934cefSDon Lewis 		return (error);
5985c38b6dbSDon Lewis 
59998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
60098271db4SGarrett Wollman 	xig.xig_count = n;
60198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
60298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
60398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
60498271db4SGarrett Wollman 	if (error)
60598271db4SGarrett Wollman 		return error;
60698271db4SGarrett Wollman 
607a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
60898271db4SGarrett Wollman 	if (inp_list == 0)
60998271db4SGarrett Wollman 		return ENOMEM;
61098271db4SGarrett Wollman 
611f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
612fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
613fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
614f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6152ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6162ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
61798271db4SGarrett Wollman 			inp_list[i++] = inp;
618f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6194787fd37SPaul Saab 	}
620f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
62198271db4SGarrett Wollman 	n = i;
62298271db4SGarrett Wollman 
62398271db4SGarrett Wollman 	error = 0;
62498271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
62598271db4SGarrett Wollman 		inp = inp_list[i];
62698271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
62798271db4SGarrett Wollman 			struct xinpcb xi;
628fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
62998271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
63098271db4SGarrett Wollman 			/* XXX should avoid extra copy */
63198271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
63298271db4SGarrett Wollman 			if (inp->inp_socket)
63398271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6344b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
63598271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
63698271db4SGarrett Wollman 		}
63798271db4SGarrett Wollman 	}
63898271db4SGarrett Wollman 	if (!error) {
63998271db4SGarrett Wollman 		/*
64098271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
64198271db4SGarrett Wollman 		 * If the generation differs from what we told
64298271db4SGarrett Wollman 		 * her before, she knows that something happened
64398271db4SGarrett Wollman 		 * while we were processing this request, and it
64498271db4SGarrett Wollman 		 * might be necessary to retry.
64598271db4SGarrett Wollman 		 */
646f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
64798271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
64898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
64998271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
650f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
65198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
65298271db4SGarrett Wollman 	}
65398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
65498271db4SGarrett Wollman 	return error;
65598271db4SGarrett Wollman }
65698271db4SGarrett Wollman 
65798271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
65898271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
65998271db4SGarrett Wollman 
66098271db4SGarrett Wollman static int
66182d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
662490d50b6SBrian Feldman {
663c0511d3bSBrian Feldman 	struct xucred xuc;
664490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
665490d50b6SBrian Feldman 	struct inpcb *inp;
666277afaffSRobert Watson 	int error;
667490d50b6SBrian Feldman 
66856f21b9dSColin Percival 	error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
669490d50b6SBrian Feldman 	if (error)
670490d50b6SBrian Feldman 		return (error);
671490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
672490d50b6SBrian Feldman 	if (error)
673490d50b6SBrian Feldman 		return (error);
674f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
675490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
676cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6772d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
678490d50b6SBrian Feldman 		error = ENOENT;
679490d50b6SBrian Feldman 		goto out;
680490d50b6SBrian Feldman 	}
68129dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
6827ce87f12SDavid Malone 	if (error)
6837ce87f12SDavid Malone 		goto out;
68476183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
685490d50b6SBrian Feldman out:
686f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
6870e1eebb8SDon Lewis 	if (error == 0)
6880e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
689490d50b6SBrian Feldman 	return (error);
690490d50b6SBrian Feldman }
691490d50b6SBrian Feldman 
6927ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
6937ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
6947ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
695490d50b6SBrian Feldman 
696490d50b6SBrian Feldman static int
697032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
698032dcc76SLuigi Rizzo 	register struct inpcb *inp;
699032dcc76SLuigi Rizzo 	struct mbuf *m;
700032dcc76SLuigi Rizzo 	struct sockaddr *addr;
701032dcc76SLuigi Rizzo 	struct mbuf *control;
702032dcc76SLuigi Rizzo 	struct thread *td;
703df8bae1dSRodney W. Grimes {
704032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
705032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
70690162a4eSIan Dowse 	struct in_addr faddr, laddr;
707c557ae16SIan Dowse 	struct cmsghdr *cm;
708c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
70990162a4eSIan Dowse 	int error = 0;
7108afa2304SBruce M Simpson 	int ipflags;
71190162a4eSIan Dowse 	u_short fport, lport;
7125c32ea65SRobert Watson 	int unlock_udbinfo;
713df8bae1dSRodney W. Grimes 
7145c32ea65SRobert Watson 	/*
7155c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
7165c32ea65SRobert Watson 	 * inpcb.  As such, we don't know up front what inpcb locks we will
7175c32ea65SRobert Watson 	 * need.  Do any work to decide what is needed up front before
7185c32ea65SRobert Watson 	 * acquiring locks.
7195c32ea65SRobert Watson 	 */
720430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
721c557ae16SIan Dowse 		if (control)
722c557ae16SIan Dowse 			m_freem(control);
7235c32ea65SRobert Watson 		m_freem(m);
7245c32ea65SRobert Watson 		return EMSGSIZE;
725430d30d8SBill Fenner 	}
726430d30d8SBill Fenner 
727c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
728c557ae16SIan Dowse 	if (control != NULL) {
729c557ae16SIan Dowse 		/*
730c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
731c557ae16SIan Dowse 		 * is stored in a single mbuf.
732c557ae16SIan Dowse 		 */
733c557ae16SIan Dowse 		if (control->m_next) {
734c557ae16SIan Dowse 			m_freem(control);
7355c32ea65SRobert Watson 			m_freem(m);
7365c32ea65SRobert Watson 			return EINVAL;
737c557ae16SIan Dowse 		}
738c557ae16SIan Dowse 		for (; control->m_len > 0;
739c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
740c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
741c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
742c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
743c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
744c557ae16SIan Dowse 				error = EINVAL;
745c557ae16SIan Dowse 				break;
746c557ae16SIan Dowse 			}
747c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
748c557ae16SIan Dowse 				continue;
749c557ae16SIan Dowse 
750c557ae16SIan Dowse 			switch (cm->cmsg_type) {
751c557ae16SIan Dowse 			case IP_SENDSRCADDR:
752c557ae16SIan Dowse 				if (cm->cmsg_len !=
753c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
754c557ae16SIan Dowse 					error = EINVAL;
755c557ae16SIan Dowse 					break;
756c557ae16SIan Dowse 				}
757c557ae16SIan Dowse 				bzero(&src, sizeof(src));
758c557ae16SIan Dowse 				src.sin_family = AF_INET;
759c557ae16SIan Dowse 				src.sin_len = sizeof(src);
760c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
761c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
762c557ae16SIan Dowse 				break;
763c557ae16SIan Dowse 			default:
764c557ae16SIan Dowse 				error = ENOPROTOOPT;
765c557ae16SIan Dowse 				break;
766c557ae16SIan Dowse 			}
767c557ae16SIan Dowse 			if (error)
768c557ae16SIan Dowse 				break;
769c557ae16SIan Dowse 		}
770c557ae16SIan Dowse 		m_freem(control);
771c557ae16SIan Dowse 	}
7725c32ea65SRobert Watson 	if (error) {
7735c32ea65SRobert Watson 		m_freem(m);
7745c32ea65SRobert Watson 		return error;
7755c32ea65SRobert Watson 	}
7765c32ea65SRobert Watson 
7775c32ea65SRobert Watson 	if (src.sin_addr.s_addr != INADDR_ANY ||
7785c32ea65SRobert Watson 	    addr != NULL) {
7795c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
7805c32ea65SRobert Watson 		unlock_udbinfo = 1;
7815c32ea65SRobert Watson 	} else
7825c32ea65SRobert Watson 		unlock_udbinfo = 0;
7835c32ea65SRobert Watson 	INP_LOCK(inp);
7845c32ea65SRobert Watson 
7855c32ea65SRobert Watson #ifdef MAC
7865c32ea65SRobert Watson 	mac_create_mbuf_from_inpcb(inp, m);
7875c32ea65SRobert Watson #endif
7885c32ea65SRobert Watson 
78990162a4eSIan Dowse 	laddr = inp->inp_laddr;
79090162a4eSIan Dowse 	lport = inp->inp_lport;
791c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
792c557ae16SIan Dowse 		if (lport == 0) {
793c557ae16SIan Dowse 			error = EINVAL;
794c557ae16SIan Dowse 			goto release;
795c557ae16SIan Dowse 		}
796c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
797b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
798c557ae16SIan Dowse 		if (error)
799c557ae16SIan Dowse 			goto release;
800c557ae16SIan Dowse 	}
801c557ae16SIan Dowse 
802df8bae1dSRodney W. Grimes 	if (addr) {
80375c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
804812d8653SSam Leffler 		if (jailed(td->td_ucred))
805a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
806df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
807df8bae1dSRodney W. Grimes 			error = EISCONN;
808df8bae1dSRodney W. Grimes 			goto release;
809df8bae1dSRodney W. Grimes 		}
81090162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
811b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
81290162a4eSIan Dowse 		if (error)
81390162a4eSIan Dowse 			goto release;
81490162a4eSIan Dowse 
81590162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
81690162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
81790162a4eSIan Dowse 		    inp->inp_lport == 0) {
8183a1757b9SGleb Smirnoff 			/*
8193a1757b9SGleb Smirnoff 			 * Remember addr if jailed, to prevent rebinding.
8203a1757b9SGleb Smirnoff 			 */
8213a1757b9SGleb Smirnoff 			if (jailed(td->td_ucred))
8223a1757b9SGleb Smirnoff 				inp->inp_laddr = laddr;
82390162a4eSIan Dowse 			inp->inp_lport = lport;
82490162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
82590162a4eSIan Dowse 				inp->inp_lport = 0;
82690162a4eSIan Dowse 				error = EAGAIN;
827df8bae1dSRodney W. Grimes 				goto release;
828df8bae1dSRodney W. Grimes 			}
82990162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
83090162a4eSIan Dowse 		}
831df8bae1dSRodney W. Grimes 	} else {
83290162a4eSIan Dowse 		faddr = inp->inp_faddr;
83390162a4eSIan Dowse 		fport = inp->inp_fport;
83490162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
835df8bae1dSRodney W. Grimes 			error = ENOTCONN;
836df8bae1dSRodney W. Grimes 			goto release;
837df8bae1dSRodney W. Grimes 		}
838df8bae1dSRodney W. Grimes 	}
839e6ccd709SRobert Watson 
840df8bae1dSRodney W. Grimes 	/*
841e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
842392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
843392e8407SRobert Watson 	 * since we won't use that space at this layer.
844df8bae1dSRodney W. Grimes 	 */
845e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
846e6ccd709SRobert Watson 	if (m == NULL) {
847df8bae1dSRodney W. Grimes 		error = ENOBUFS;
84849b19bfcSBruce M Simpson 		goto release;
849df8bae1dSRodney W. Grimes 	}
850e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
851e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
852392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
853df8bae1dSRodney W. Grimes 
854df8bae1dSRodney W. Grimes 	/*
855df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
856df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
857df8bae1dSRodney W. Grimes 	 */
858df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
859db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
860df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
86190162a4eSIan Dowse 	ui->ui_src = laddr;
86290162a4eSIan Dowse 	ui->ui_dst = faddr;
86390162a4eSIan Dowse 	ui->ui_sport = lport;
86490162a4eSIan Dowse 	ui->ui_dport = fport;
865db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
866df8bae1dSRodney W. Grimes 
867b2828ad2SAndre Oppermann 	/*
868b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
869b2828ad2SAndre Oppermann 	 */
870b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
871b2828ad2SAndre Oppermann 		struct ip *ip;
872b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
873b2828ad2SAndre Oppermann 		ip->ip_off |= IP_DF;
874b2828ad2SAndre Oppermann 	}
875b2828ad2SAndre Oppermann 
876b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
877b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
878b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
879b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
880b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
881d46ff6bdSMaxim Konovalov 	if (inp->inp_vflag & INP_ONESBCAST)
8828afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
8838afa2304SBruce M Simpson 
884df8bae1dSRodney W. Grimes 	/*
885db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
886df8bae1dSRodney W. Grimes 	 */
887df8bae1dSRodney W. Grimes 	if (udpcksum) {
888d46ff6bdSMaxim Konovalov 		if (inp->inp_vflag & INP_ONESBCAST)
8898a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
8908a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
891db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
892db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
893db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
894db4f9cc7SJonathan Lemon 	} else {
895db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
896df8bae1dSRodney W. Grimes 	}
897df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
898ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
899ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
900df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
901cfa1ca9dSYoshinobu Inoue 
9025c32ea65SRobert Watson 	if (unlock_udbinfo)
9035c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
90497d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
9055d846453SSam Leffler 	    inp->inp_moptions, inp);
9065c32ea65SRobert Watson 	INP_UNLOCK(inp);
907df8bae1dSRodney W. Grimes 	return (error);
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes release:
9105c32ea65SRobert Watson 	INP_UNLOCK(inp);
9115c32ea65SRobert Watson 	if (unlock_udbinfo)
9125c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
913df8bae1dSRodney W. Grimes 	m_freem(m);
914df8bae1dSRodney W. Grimes 	return (error);
915df8bae1dSRodney W. Grimes }
916df8bae1dSRodney W. Grimes 
91776429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
918032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
919e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
9203d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
9210312fbe9SPoul-Henning Kamp 
922cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
923cfa1ca9dSYoshinobu Inoue #ifdef INET6
924cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
925cfa1ca9dSYoshinobu Inoue #else
926cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
927cfa1ca9dSYoshinobu Inoue #endif
928cfa1ca9dSYoshinobu Inoue 				      );
929e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9300ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
931df8bae1dSRodney W. Grimes 
932ac45e92fSRobert Watson static void
933d0390e05SGarrett Wollman udp_abort(struct socket *so)
934df8bae1dSRodney W. Grimes {
935d0390e05SGarrett Wollman 	struct inpcb *inp;
936df8bae1dSRodney W. Grimes 
937f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
938d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
939f76fcf6dSJeffrey Hsu 	if (inp == 0) {
940f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
941ac45e92fSRobert Watson 		return;	/* ??? possible? panic instead? */
942f76fcf6dSJeffrey Hsu 	}
943f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
944d0390e05SGarrett Wollman 	soisdisconnected(so);
945d0390e05SGarrett Wollman 	in_pcbdetach(inp);
946f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
947df8bae1dSRodney W. Grimes }
948df8bae1dSRodney W. Grimes 
949d0390e05SGarrett Wollman static int
950b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
951d0390e05SGarrett Wollman {
952d0390e05SGarrett Wollman 	struct inpcb *inp;
953277afaffSRobert Watson 	int error;
954d0390e05SGarrett Wollman 
955f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
956d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
957f76fcf6dSJeffrey Hsu 	if (inp != 0) {
958f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
959d0390e05SGarrett Wollman 		return EINVAL;
960f76fcf6dSJeffrey Hsu 	}
961cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
962f76fcf6dSJeffrey Hsu 	if (error) {
963f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
964cfa1ca9dSYoshinobu Inoue 		return error;
965f76fcf6dSJeffrey Hsu 	}
9666823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &udbinfo, "udpinp");
96753b57cd1SSam Leffler 	if (error) {
96853b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
969d0390e05SGarrett Wollman 		return error;
97053b57cd1SSam Leffler 	}
971cfa1ca9dSYoshinobu Inoue 
972cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
973f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
974f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
975cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
976cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
977f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
978d0390e05SGarrett Wollman 	return 0;
979df8bae1dSRodney W. Grimes }
980d0390e05SGarrett Wollman 
981d0390e05SGarrett Wollman static int
982b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
983d0390e05SGarrett Wollman {
984d0390e05SGarrett Wollman 	struct inpcb *inp;
985277afaffSRobert Watson 	int error;
986d0390e05SGarrett Wollman 
987f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
988d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
989f76fcf6dSJeffrey Hsu 	if (inp == 0) {
990f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
991d0390e05SGarrett Wollman 		return EINVAL;
992f76fcf6dSJeffrey Hsu 	}
993f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
994b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
995f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
996f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
997d0390e05SGarrett Wollman 	return error;
998d0390e05SGarrett Wollman }
999d0390e05SGarrett Wollman 
1000d0390e05SGarrett Wollman static int
1001b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1002d0390e05SGarrett Wollman {
1003d0390e05SGarrett Wollman 	struct inpcb *inp;
1004277afaffSRobert Watson 	int error;
100575c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1006d0390e05SGarrett Wollman 
1007f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1008d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1009f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1010f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1011d0390e05SGarrett Wollman 		return EINVAL;
1012f76fcf6dSJeffrey Hsu 	}
1013f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1014f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1015f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1016f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1017d0390e05SGarrett Wollman 		return EISCONN;
1018f76fcf6dSJeffrey Hsu 	}
101975c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1020812d8653SSam Leffler 	if (jailed(td->td_ucred))
1021a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1022b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
10234cc20ab1SSeigo Tanimura 	if (error == 0)
1024df8bae1dSRodney W. Grimes 		soisconnected(so);
1025f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1026f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1027d0390e05SGarrett Wollman 	return error;
1028df8bae1dSRodney W. Grimes }
1029d0390e05SGarrett Wollman 
1030bc725eafSRobert Watson static void
1031d0390e05SGarrett Wollman udp_detach(struct socket *so)
1032d0390e05SGarrett Wollman {
1033d0390e05SGarrett Wollman 	struct inpcb *inp;
1034d0390e05SGarrett Wollman 
1035f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1036d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1037f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1038f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1039f76fcf6dSJeffrey Hsu 	}
1040f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1041d0390e05SGarrett Wollman 	in_pcbdetach(inp);
1042f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1043d0390e05SGarrett Wollman }
1044d0390e05SGarrett Wollman 
1045d0390e05SGarrett Wollman static int
1046d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1047d0390e05SGarrett Wollman {
1048d0390e05SGarrett Wollman 	struct inpcb *inp;
1049d0390e05SGarrett Wollman 
1050f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1051d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1052f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1053f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1054d0390e05SGarrett Wollman 		return EINVAL;
1055f76fcf6dSJeffrey Hsu 	}
1056f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1057f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1058f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1059f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1060d0390e05SGarrett Wollman 		return ENOTCONN;
1061f76fcf6dSJeffrey Hsu 	}
1062d0390e05SGarrett Wollman 
1063df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1064df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1065f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1066f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1067df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1068d0390e05SGarrett Wollman 	return 0;
1069df8bae1dSRodney W. Grimes }
1070df8bae1dSRodney W. Grimes 
1071d0390e05SGarrett Wollman static int
107257bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1073b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1074d0390e05SGarrett Wollman {
1075d0390e05SGarrett Wollman 	struct inpcb *inp;
1076d0390e05SGarrett Wollman 
1077d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
10785c32ea65SRobert Watson 	return udp_output(inp, m, addr, control, td);
1079d0390e05SGarrett Wollman }
1080d0390e05SGarrett Wollman 
108176429de4SYoshinobu Inoue int
1082d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1083d0390e05SGarrett Wollman {
1084d0390e05SGarrett Wollman 	struct inpcb *inp;
1085d0390e05SGarrett Wollman 
1086f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1087d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1088f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1089f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1090d0390e05SGarrett Wollman 		return EINVAL;
1091f76fcf6dSJeffrey Hsu 	}
1092f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1093f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1094d0390e05SGarrett Wollman 	socantsendmore(so);
1095f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1096d0390e05SGarrett Wollman 	return 0;
1097d0390e05SGarrett Wollman }
1098d0390e05SGarrett Wollman 
1099f76fcf6dSJeffrey Hsu /*
1100f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1101f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1102f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1103f76fcf6dSJeffrey Hsu  */
1104f76fcf6dSJeffrey Hsu static int
1105f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1106f76fcf6dSJeffrey Hsu {
1107f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1108f76fcf6dSJeffrey Hsu }
1109f76fcf6dSJeffrey Hsu 
1110f76fcf6dSJeffrey Hsu /*
1111f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1112f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1113f76fcf6dSJeffrey Hsu  */
1114f76fcf6dSJeffrey Hsu static int
1115f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1116f76fcf6dSJeffrey Hsu {
1117f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1118f76fcf6dSJeffrey Hsu }
1119f76fcf6dSJeffrey Hsu 
1120d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1121756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1122756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1123756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1124756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1125756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1126756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1127756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
1128756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		udp_peeraddr,
1129756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
1130756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
1131756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		udp_sockaddr,
1132756d52a1SPoul-Henning Kamp 	.pru_sosetlabel =	in_pcbsosetlabel
1133d0390e05SGarrett Wollman };
1134