xref: /freebsd/sys/netinet/udp_usrreq.c (revision e6ccd70936186495ada83c2ee0e4cf74efa19811)
1df8bae1dSRodney W. Grimes /*
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 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 
336a800098SYoshinobu Inoue #include "opt_ipsec.h"
34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
35bdb3fa18SRobert Watson #include "opt_mac.h"
36cfa1ca9dSYoshinobu Inoue 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
3826f9a767SRodney W. Grimes #include <sys/systm.h>
39960ed29cSSeigo Tanimura #include <sys/domain.h>
40960ed29cSSeigo Tanimura #include <sys/jail.h>
41b110a8a2SGarrett Wollman #include <sys/kernel.h>
42960ed29cSSeigo Tanimura #include <sys/lock.h>
43bdb3fa18SRobert Watson #include <sys/mac.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46490d50b6SBrian Feldman #include <sys/proc.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48960ed29cSSeigo Tanimura #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51960ed29cSSeigo Tanimura #include <sys/sx.h>
52b5e8ce9fSBruce Evans #include <sys/sysctl.h>
53816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
548781d8e9SBruce Evans 
5569c2d429SJeff Roberson #include <vm/uma.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <net/if.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <netinet/in.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
62960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
63960ed29cSSeigo Tanimura #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip.h>
65cfa1ca9dSYoshinobu Inoue #ifdef INET6
66cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
67cfa1ca9dSYoshinobu Inoue #endif
68960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
69960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
71cfa1ca9dSYoshinobu Inoue #ifdef INET6
72cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
73cfa1ca9dSYoshinobu Inoue #endif
74df8bae1dSRodney W. Grimes #include <netinet/udp.h>
75df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
76df8bae1dSRodney W. Grimes 
77b9234fafSSam Leffler #ifdef FAST_IPSEC
78b9234fafSSam Leffler #include <netipsec/ipsec.h>
79b9234fafSSam Leffler #endif /*FAST_IPSEC*/
80b9234fafSSam Leffler 
81cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
82cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
83cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
84cfa1ca9dSYoshinobu Inoue 
85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
86db4f9cc7SJonathan Lemon 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * UDP protocol implementation.
89df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
90df8bae1dSRodney W. Grimes  */
91df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
920312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
93df8bae1dSRodney W. Grimes #else
940312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
95df8bae1dSRodney W. Grimes #endif
960312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
970312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
98df8bae1dSRodney W. Grimes 
99032dcc76SLuigi Rizzo int	log_in_vain = 0;
100816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1013d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
102816a3d83SPoul-Henning Kamp 
103032dcc76SLuigi Rizzo static int	blackhole = 0;
10416f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
10516f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
10616f7f31fSGeoff Rehmet 
10783453a06SBruce M Simpson static int	strict_mcast_mship = 0;
10883453a06SBruce M Simpson SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
10983453a06SBruce M Simpson 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
11083453a06SBruce M Simpson 
11176429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
112cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1137a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
11415bd2b43SDavid Greenman 
11515bd2b43SDavid Greenman #ifndef UDBHASHSIZE
116c3229e05SDavid Greenman #define UDBHASHSIZE 16
11715bd2b43SDavid Greenman #endif
11815bd2b43SDavid Greenman 
11976429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
120c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
1213d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
122f2ea20e6SGarrett Wollman 
1230312fbe9SPoul-Henning Kamp static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
124cfa1ca9dSYoshinobu Inoue #ifdef INET6
125cfa1ca9dSYoshinobu Inoue struct udp_in6 {
126cfa1ca9dSYoshinobu Inoue 	struct sockaddr_in6	uin6_sin;
127cfa1ca9dSYoshinobu Inoue 	u_char			uin6_init_done : 1;
128cfa1ca9dSYoshinobu Inoue } udp_in6 = {
129cfa1ca9dSYoshinobu Inoue 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
130cfa1ca9dSYoshinobu Inoue 	0
131cfa1ca9dSYoshinobu Inoue };
132cfa1ca9dSYoshinobu Inoue struct udp_ip6 {
133cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr		uip6_ip6;
134cfa1ca9dSYoshinobu Inoue 	u_char			uip6_init_done : 1;
135cfa1ca9dSYoshinobu Inoue } udp_ip6;
136cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
137df8bae1dSRodney W. Grimes 
138c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
139c1cd65baSBruce Evans 		int off);
140cfa1ca9dSYoshinobu Inoue #ifdef INET6
1414d77a549SAlfred Perlstein static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
142cfa1ca9dSYoshinobu Inoue #endif
143cfa1ca9dSYoshinobu Inoue 
1444d77a549SAlfred Perlstein static int udp_detach(struct socket *so);
1454d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1464d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes void
149032dcc76SLuigi Rizzo udp_init()
150df8bae1dSRodney W. Grimes {
151f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
15215bd2b43SDavid Greenman 	LIST_INIT(&udb);
15315bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
154ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1558781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1568781d8e9SBruce Evans 					&udbinfo.porthashmask);
15769c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
158420a2811SAndre Oppermann 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
15969c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes void
163032dcc76SLuigi Rizzo udp_input(m, off)
164032dcc76SLuigi Rizzo 	register struct mbuf *m;
165032dcc76SLuigi Rizzo 	int off;
166df8bae1dSRodney W. Grimes {
167cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
168032dcc76SLuigi Rizzo 	register struct ip *ip;
169032dcc76SLuigi Rizzo 	register struct udphdr *uh;
170032dcc76SLuigi Rizzo 	register struct inpcb *inp;
171df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
172df8bae1dSRodney W. Grimes 	int len;
173df8bae1dSRodney W. Grimes 	struct ip save_ip;
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
176df8bae1dSRodney W. Grimes 
177df8bae1dSRodney W. Grimes 	/*
178df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
179df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
180df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
181df8bae1dSRodney W. Grimes 	 * with options still present.
182df8bae1dSRodney W. Grimes 	 */
183df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
184df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
185df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
186df8bae1dSRodney W. Grimes 	}
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes 	/*
189df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
190df8bae1dSRodney W. Grimes 	 */
191df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
192df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
193df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
194df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
195df8bae1dSRodney W. Grimes 			return;
196df8bae1dSRodney W. Grimes 		}
197df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
198df8bae1dSRodney W. Grimes 	}
199df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
200df8bae1dSRodney W. Grimes 
201686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
202686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
203f76fcf6dSJeffrey Hsu 		goto badunlocked;
204686cdd19SJun-ichiro itojun Hagino 
205df8bae1dSRodney W. Grimes 	/*
206b9234fafSSam Leffler 	 * Construct sockaddr format source address.
207b9234fafSSam Leffler 	 * Stuff source address and datagram in user buffer.
208b9234fafSSam Leffler 	 */
209b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
210b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
211b9234fafSSam Leffler #ifdef INET6
212b9234fafSSam Leffler 	udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
213b9234fafSSam Leffler #endif
214b9234fafSSam Leffler 
215b9234fafSSam Leffler 	/*
216df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
217df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
218df8bae1dSRodney W. Grimes 	 */
219df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
220df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2217eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
222df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
223f76fcf6dSJeffrey Hsu 			goto badunlocked;
224df8bae1dSRodney W. Grimes 		}
225df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
226df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
227df8bae1dSRodney W. Grimes 	}
228df8bae1dSRodney W. Grimes 	/*
229df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
230df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
231df8bae1dSRodney W. Grimes 	 */
23248cb400fSRuslan Ermilov 	if (!blackhole)
233df8bae1dSRodney W. Grimes 		save_ip = *ip;
234df8bae1dSRodney W. Grimes 
235df8bae1dSRodney W. Grimes 	/*
236df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
237df8bae1dSRodney W. Grimes 	 */
2386dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
239db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
240db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
241db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
242db4f9cc7SJonathan Lemon 			else
243db4f9cc7SJonathan Lemon 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
244506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
245db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
246db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
247db4f9cc7SJonathan Lemon 		} else {
248cb342100SHajimu UMEMOTO 			char b[9];
249cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2506effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
251df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
252623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
253cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
254db4f9cc7SJonathan Lemon 		}
255623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
256df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
257df8bae1dSRodney W. Grimes 			m_freem(m);
258df8bae1dSRodney W. Grimes 			return;
259df8bae1dSRodney W. Grimes 		}
260fb9aaba0SRuslan Ermilov 	} else
261fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
262df8bae1dSRodney W. Grimes 
263f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
264f76fcf6dSJeffrey Hsu 
265df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
266df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
26782c23ebaSBill Fenner 		struct inpcb *last;
268df8bae1dSRodney W. Grimes 		/*
269df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
270df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
271df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
272df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
273df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
274df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
275df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
276df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
277df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
278df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
279df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
280df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
281df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
282df8bae1dSRodney W. Grimes 		 */
283df8bae1dSRodney W. Grimes 
284df8bae1dSRodney W. Grimes 		/*
285df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
286df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
287df8bae1dSRodney W. Grimes 		 */
288df8bae1dSRodney W. Grimes 		last = NULL;
289cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
2909c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
291f76fcf6dSJeffrey Hsu 				continue;
292cfa1ca9dSYoshinobu Inoue #ifdef INET6
293369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2949c1df695SRobert Watson 				continue;
295cfa1ca9dSYoshinobu Inoue #endif
296df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
297f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
2989c1df695SRobert Watson 					continue;
299df8bae1dSRodney W. Grimes 			}
300df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
301df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
302df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
303df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
3049c1df695SRobert Watson 					continue;
305df8bae1dSRodney W. Grimes 			}
3069c1df695SRobert Watson 			INP_LOCK(inp);
307df8bae1dSRodney W. Grimes 
30883453a06SBruce M Simpson 			/*
30983453a06SBruce M Simpson 			 * Check multicast packets to make sure they are only
31083453a06SBruce M Simpson 			 * sent to sockets with multicast memberships for the
31183453a06SBruce M Simpson 			 * packet's destination address and arrival interface
31283453a06SBruce M Simpson 			 */
31383453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
31483453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
31583453a06SBruce M Simpson 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
31683453a06SBruce M Simpson 				int mship, foundmship = 0;
31783453a06SBruce M Simpson 
31883453a06SBruce M Simpson 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
31983453a06SBruce M Simpson 					if (MSHIP(inp, mship)->inm_addr.s_addr
32083453a06SBruce M Simpson 					    == ip->ip_dst.s_addr &&
32183453a06SBruce M Simpson 					    MSHIP(inp, mship)->inm_ifp
32283453a06SBruce M Simpson 					    == m->m_pkthdr.rcvif) {
32383453a06SBruce M Simpson 						foundmship = 1;
32483453a06SBruce M Simpson 						break;
32583453a06SBruce M Simpson 					}
32683453a06SBruce M Simpson 				}
3279c1df695SRobert Watson 				if (foundmship == 0) {
3289c1df695SRobert Watson 					INP_UNLOCK(inp);
3299c1df695SRobert Watson 					continue;
3309c1df695SRobert Watson 				}
33183453a06SBruce M Simpson 			}
33283453a06SBruce M Simpson #undef NMSHIPS
33383453a06SBruce M Simpson #undef MSHIP
334df8bae1dSRodney W. Grimes 			if (last != NULL) {
335df8bae1dSRodney W. Grimes 				struct mbuf *n;
336df8bae1dSRodney W. Grimes 
337032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
338365433d9SRobert Watson 				if (n != NULL)
339cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
340cfa1ca9dSYoshinobu Inoue 						   iphlen +
341cfa1ca9dSYoshinobu Inoue 						   sizeof(struct udphdr));
342f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
343df8bae1dSRodney W. Grimes 			}
34482c23ebaSBill Fenner 			last = inp;
345df8bae1dSRodney W. Grimes 			/*
346df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
347df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
348df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
349df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
350df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
351df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
352df8bae1dSRodney W. Grimes 			 */
3534cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
354df8bae1dSRodney W. Grimes 				break;
355df8bae1dSRodney W. Grimes 		}
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 		if (last == NULL) {
358df8bae1dSRodney W. Grimes 			/*
359df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
360df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
361df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
362df8bae1dSRodney W. Grimes 			 */
363df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
36461ffc0b1SJeffrey Hsu 			goto badheadlocked;
365df8bae1dSRodney W. Grimes 		}
366f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
367cfa1ca9dSYoshinobu Inoue 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
3689b657230SSam Leffler 		INP_UNLOCK(last);
369df8bae1dSRodney W. Grimes 		return;
370df8bae1dSRodney W. Grimes 	}
371df8bae1dSRodney W. Grimes 	/*
3726d6a026bSDavid Greenman 	 * Locate pcb for datagram.
373df8bae1dSRodney W. Grimes 	 */
374c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
375cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
37615bd2b43SDavid Greenman 	if (inp == NULL) {
37775cfc95fSAndrey A. Chernov 		if (log_in_vain) {
378df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
37975cfc95fSAndrey A. Chernov 
38075cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
381592071e8SBruce Evans 			log(LOG_INFO,
382592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
383592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
384592071e8SBruce Evans 			    ntohs(uh->uh_sport));
38575cfc95fSAndrey A. Chernov 		}
386df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
387df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
388df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
38961ffc0b1SJeffrey Hsu 			goto badheadlocked;
390df8bae1dSRodney W. Grimes 		}
391582a7760SBruce Evans 		if (blackhole)
39261ffc0b1SJeffrey Hsu 			goto badheadlocked;
3931cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
3941cbd978eSLuigi Rizzo 			goto badheadlocked;
39504287599SRuslan Ermilov 		*ip = save_ip;
39604287599SRuslan Ermilov 		ip->ip_len += iphlen;
397582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
398f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
399df8bae1dSRodney W. Grimes 		return;
400df8bae1dSRodney W. Grimes 	}
40161ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
40261ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
403b9234fafSSam Leffler 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
404f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
405df8bae1dSRodney W. Grimes 	return;
40661ffc0b1SJeffrey Hsu 
40761ffc0b1SJeffrey Hsu badheadlocked:
40861ffc0b1SJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
409f76fcf6dSJeffrey Hsu 	if (inp)
410f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
411f76fcf6dSJeffrey Hsu badunlocked:
412df8bae1dSRodney W. Grimes 	m_freem(m);
413df8bae1dSRodney W. Grimes 	if (opts)
414df8bae1dSRodney W. Grimes 		m_freem(opts);
415cfa1ca9dSYoshinobu Inoue 	return;
416cfa1ca9dSYoshinobu Inoue }
417cfa1ca9dSYoshinobu Inoue 
418686cdd19SJun-ichiro itojun Hagino #ifdef INET6
419cfa1ca9dSYoshinobu Inoue static void
420032dcc76SLuigi Rizzo ip_2_ip6_hdr(ip6, ip)
421032dcc76SLuigi Rizzo 	struct ip6_hdr *ip6;
422032dcc76SLuigi Rizzo 	struct ip *ip;
423cfa1ca9dSYoshinobu Inoue {
424cfa1ca9dSYoshinobu Inoue 	bzero(ip6, sizeof(*ip6));
425cfa1ca9dSYoshinobu Inoue 
426cfa1ca9dSYoshinobu Inoue 	ip6->ip6_vfc = IPV6_VERSION;
427cfa1ca9dSYoshinobu Inoue 	ip6->ip6_plen = ip->ip_len;
428cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt = ip->ip_p;
429cfa1ca9dSYoshinobu Inoue 	ip6->ip6_hlim = ip->ip_ttl;
430cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
431cfa1ca9dSYoshinobu Inoue 		IPV6_ADDR_INT32_SMP;
432cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
433cfa1ca9dSYoshinobu Inoue 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
434cfa1ca9dSYoshinobu Inoue }
435cfa1ca9dSYoshinobu Inoue #endif
436cfa1ca9dSYoshinobu Inoue 
437cfa1ca9dSYoshinobu Inoue /*
438cfa1ca9dSYoshinobu Inoue  * subroutine of udp_input(), mainly for source code readability.
439cfa1ca9dSYoshinobu Inoue  * caller must properly init udp_ip6 and udp_in6 beforehand.
440cfa1ca9dSYoshinobu Inoue  */
441cfa1ca9dSYoshinobu Inoue static void
442032dcc76SLuigi Rizzo udp_append(last, ip, n, off)
443032dcc76SLuigi Rizzo 	struct inpcb *last;
444032dcc76SLuigi Rizzo 	struct ip *ip;
445032dcc76SLuigi Rizzo 	struct mbuf *n;
446032dcc76SLuigi Rizzo 	int off;
447cfa1ca9dSYoshinobu Inoue {
448cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
4491e4d7da7SRobert Watson 	struct socket *so;
450cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
451cfa1ca9dSYoshinobu Inoue 
45287f2bb8cSRobert Watson 	INP_LOCK_ASSERT(last);
45387f2bb8cSRobert Watson 
454da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
455da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
456da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
457b9234fafSSam Leffler #ifdef IPSEC
458b9234fafSSam Leffler 		ipsecstat.in_polvio++;
459b9234fafSSam Leffler #endif /*IPSEC*/
460b9234fafSSam Leffler 		m_freem(n);
461b9234fafSSam Leffler 		return;
462b9234fafSSam Leffler 	}
463da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
464b9234fafSSam Leffler #ifdef MAC
465a557af22SRobert Watson 	if (mac_check_inpcb_deliver(last, n) != 0) {
466b9234fafSSam Leffler 		m_freem(n);
467b9234fafSSam Leffler 		return;
468b9234fafSSam Leffler 	}
469b9234fafSSam Leffler #endif
470cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
471be8a62e8SPoul-Henning Kamp 	    last->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
472cfa1ca9dSYoshinobu Inoue #ifdef INET6
473cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
474cfa1ca9dSYoshinobu Inoue 			int savedflags;
475cfa1ca9dSYoshinobu Inoue 
476cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
477cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
478cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
479cfa1ca9dSYoshinobu Inoue 			}
480cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
481cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
48211de19f4SHajimu UMEMOTO 			ip6_savecontrol(last, n, &opts);
483cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
484cfa1ca9dSYoshinobu Inoue 		} else
485cfa1ca9dSYoshinobu Inoue #endif
486cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4874cc20ab1SSeigo Tanimura 	}
488cfa1ca9dSYoshinobu Inoue #ifdef INET6
489cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
490cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
491cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
492cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
493cfa1ca9dSYoshinobu Inoue 		}
494cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
495cfa1ca9dSYoshinobu Inoue 	} else
496cfa1ca9dSYoshinobu Inoue #endif
497cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
498cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
4991e4d7da7SRobert Watson 
5001e4d7da7SRobert Watson 	so = last->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 	int s;
539c693a045SJonathan Lemon 
540c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
541c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
542c693a045SJonathan Lemon 		return;
543df8bae1dSRodney W. Grimes 
54497d8d152SAndre Oppermann 	/*
54597d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
54697d8d152SAndre Oppermann 	 */
54797d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
54897d8d152SAndre Oppermann 		return;
54997d8d152SAndre Oppermann 	/*
55097d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
55197d8d152SAndre Oppermann 	 * XXX: We never get this from ICMP, otherwise it makes an
55297d8d152SAndre Oppermann 	 * excellent DoS attack on machines with many connections.
55397d8d152SAndre Oppermann 	 */
55497d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
555d1c54148SJesper Skriver 		ip = 0;
556d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
557df8bae1dSRodney W. Grimes 		return;
558df8bae1dSRodney W. Grimes 	if (ip) {
559c693a045SJonathan Lemon 		s = splnet();
560df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
561f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
562c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
563c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
564f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
565f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
566f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
567c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
568f76fcf6dSJeffrey Hsu 			}
569f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
570f76fcf6dSJeffrey Hsu 		}
571f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
572c693a045SJonathan Lemon 		splx(s);
573df8bae1dSRodney W. Grimes 	} else
574f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
575df8bae1dSRodney W. Grimes }
576df8bae1dSRodney W. Grimes 
5770312fbe9SPoul-Henning Kamp static int
57882d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
57998271db4SGarrett Wollman {
58098271db4SGarrett Wollman 	int error, i, n, s;
58198271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
58298271db4SGarrett Wollman 	inp_gen_t gencnt;
58398271db4SGarrett Wollman 	struct xinpgen xig;
58498271db4SGarrett Wollman 
58598271db4SGarrett Wollman 	/*
58698271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
58798271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
58898271db4SGarrett Wollman 	 */
58998271db4SGarrett Wollman 	if (req->oldptr == 0) {
59098271db4SGarrett Wollman 		n = udbinfo.ipi_count;
59198271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
59298271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
59398271db4SGarrett Wollman 		return 0;
59498271db4SGarrett Wollman 	}
59598271db4SGarrett Wollman 
59698271db4SGarrett Wollman 	if (req->newptr != 0)
59798271db4SGarrett Wollman 		return EPERM;
59898271db4SGarrett Wollman 
59998271db4SGarrett Wollman 	/*
60098271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
60198271db4SGarrett Wollman 	 */
60298271db4SGarrett Wollman 	s = splnet();
6034b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
60498271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
60598271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6064b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
60798271db4SGarrett Wollman 	splx(s);
60898271db4SGarrett Wollman 
60947934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6105c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
61147934cefSDon Lewis 	if (error != 0)
61247934cefSDon Lewis 		return (error);
6135c38b6dbSDon Lewis 
61498271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
61598271db4SGarrett Wollman 	xig.xig_count = n;
61698271db4SGarrett Wollman 	xig.xig_gen = gencnt;
61798271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
61898271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
61998271db4SGarrett Wollman 	if (error)
62098271db4SGarrett Wollman 		return error;
62198271db4SGarrett Wollman 
622a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
62398271db4SGarrett Wollman 	if (inp_list == 0)
62498271db4SGarrett Wollman 		return ENOMEM;
62598271db4SGarrett Wollman 
62698271db4SGarrett Wollman 	s = splnet();
627f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
628fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
629fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
630f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
6312ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6322ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
63398271db4SGarrett Wollman 			inp_list[i++] = inp;
634f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
6354787fd37SPaul Saab 	}
636f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
63798271db4SGarrett Wollman 	splx(s);
63898271db4SGarrett Wollman 	n = i;
63998271db4SGarrett Wollman 
64098271db4SGarrett Wollman 	error = 0;
64198271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
64298271db4SGarrett Wollman 		inp = inp_list[i];
64398271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
64498271db4SGarrett Wollman 			struct xinpcb xi;
64598271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
64698271db4SGarrett Wollman 			/* XXX should avoid extra copy */
64798271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
64898271db4SGarrett Wollman 			if (inp->inp_socket)
64998271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6504b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
65198271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
65298271db4SGarrett Wollman 		}
65398271db4SGarrett Wollman 	}
65498271db4SGarrett Wollman 	if (!error) {
65598271db4SGarrett Wollman 		/*
65698271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
65798271db4SGarrett Wollman 		 * If the generation differs from what we told
65898271db4SGarrett Wollman 		 * her before, she knows that something happened
65998271db4SGarrett Wollman 		 * while we were processing this request, and it
66098271db4SGarrett Wollman 		 * might be necessary to retry.
66198271db4SGarrett Wollman 		 */
66298271db4SGarrett Wollman 		s = splnet();
663f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
66498271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
66598271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
66698271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
667f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
66898271db4SGarrett Wollman 		splx(s);
66998271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
67098271db4SGarrett Wollman 	}
67198271db4SGarrett Wollman 	free(inp_list, M_TEMP);
67298271db4SGarrett Wollman 	return error;
67398271db4SGarrett Wollman }
67498271db4SGarrett Wollman 
67598271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
67698271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
67798271db4SGarrett Wollman 
67898271db4SGarrett Wollman static int
67982d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
680490d50b6SBrian Feldman {
681c0511d3bSBrian Feldman 	struct xucred xuc;
682490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
683490d50b6SBrian Feldman 	struct inpcb *inp;
684490d50b6SBrian Feldman 	int error, s;
685490d50b6SBrian Feldman 
68656f21b9dSColin Percival 	error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
687490d50b6SBrian Feldman 	if (error)
688490d50b6SBrian Feldman 		return (error);
689490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
690490d50b6SBrian Feldman 	if (error)
691490d50b6SBrian Feldman 		return (error);
692490d50b6SBrian Feldman 	s = splnet();
693f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
694490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
695cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6962d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
697490d50b6SBrian Feldman 		error = ENOENT;
698490d50b6SBrian Feldman 		goto out;
699490d50b6SBrian Feldman 	}
70029dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
7017ce87f12SDavid Malone 	if (error)
7027ce87f12SDavid Malone 		goto out;
70376183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
704490d50b6SBrian Feldman out:
705f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
706490d50b6SBrian Feldman 	splx(s);
7070e1eebb8SDon Lewis 	if (error == 0)
7080e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
709490d50b6SBrian Feldman 	return (error);
710490d50b6SBrian Feldman }
711490d50b6SBrian Feldman 
7127ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7137ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7147ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
715490d50b6SBrian Feldman 
716490d50b6SBrian Feldman static int
717032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
718032dcc76SLuigi Rizzo 	register struct inpcb *inp;
719032dcc76SLuigi Rizzo 	struct mbuf *m;
720032dcc76SLuigi Rizzo 	struct sockaddr *addr;
721032dcc76SLuigi Rizzo 	struct mbuf *control;
722032dcc76SLuigi Rizzo 	struct thread *td;
723df8bae1dSRodney W. Grimes {
724032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
725032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
72690162a4eSIan Dowse 	struct in_addr faddr, laddr;
727c557ae16SIan Dowse 	struct cmsghdr *cm;
728c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
72990162a4eSIan Dowse 	int error = 0;
7308afa2304SBruce M Simpson 	int ipflags;
73190162a4eSIan Dowse 	u_short fport, lport;
7325c32ea65SRobert Watson 	int unlock_udbinfo;
733df8bae1dSRodney W. Grimes 
7345c32ea65SRobert Watson 	/*
7355c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
7365c32ea65SRobert Watson 	 * inpcb.  As such, we don't know up front what inpcb locks we will
7375c32ea65SRobert Watson 	 * need.  Do any work to decide what is needed up front before
7385c32ea65SRobert Watson 	 * acquiring locks.
7395c32ea65SRobert Watson 	 */
740430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
741c557ae16SIan Dowse 		if (control)
742c557ae16SIan Dowse 			m_freem(control);
7435c32ea65SRobert Watson 		m_freem(m);
7445c32ea65SRobert Watson 		return EMSGSIZE;
745430d30d8SBill Fenner 	}
746430d30d8SBill Fenner 
747c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
748c557ae16SIan Dowse 	if (control != NULL) {
749c557ae16SIan Dowse 		/*
750c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
751c557ae16SIan Dowse 		 * is stored in a single mbuf.
752c557ae16SIan Dowse 		 */
753c557ae16SIan Dowse 		if (control->m_next) {
754c557ae16SIan Dowse 			m_freem(control);
7555c32ea65SRobert Watson 			m_freem(m);
7565c32ea65SRobert Watson 			return EINVAL;
757c557ae16SIan Dowse 		}
758c557ae16SIan Dowse 		for (; control->m_len > 0;
759c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
760c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
761c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
762c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
763c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
764c557ae16SIan Dowse 				error = EINVAL;
765c557ae16SIan Dowse 				break;
766c557ae16SIan Dowse 			}
767c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
768c557ae16SIan Dowse 				continue;
769c557ae16SIan Dowse 
770c557ae16SIan Dowse 			switch (cm->cmsg_type) {
771c557ae16SIan Dowse 			case IP_SENDSRCADDR:
772c557ae16SIan Dowse 				if (cm->cmsg_len !=
773c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
774c557ae16SIan Dowse 					error = EINVAL;
775c557ae16SIan Dowse 					break;
776c557ae16SIan Dowse 				}
777c557ae16SIan Dowse 				bzero(&src, sizeof(src));
778c557ae16SIan Dowse 				src.sin_family = AF_INET;
779c557ae16SIan Dowse 				src.sin_len = sizeof(src);
780c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
781c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
782c557ae16SIan Dowse 				break;
783c557ae16SIan Dowse 			default:
784c557ae16SIan Dowse 				error = ENOPROTOOPT;
785c557ae16SIan Dowse 				break;
786c557ae16SIan Dowse 			}
787c557ae16SIan Dowse 			if (error)
788c557ae16SIan Dowse 				break;
789c557ae16SIan Dowse 		}
790c557ae16SIan Dowse 		m_freem(control);
791c557ae16SIan Dowse 	}
7925c32ea65SRobert Watson 	if (error) {
7935c32ea65SRobert Watson 		m_freem(m);
7945c32ea65SRobert Watson 		return error;
7955c32ea65SRobert Watson 	}
7965c32ea65SRobert Watson 
7975c32ea65SRobert Watson 	if (src.sin_addr.s_addr != INADDR_ANY ||
7985c32ea65SRobert Watson 	    addr != NULL) {
7995c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
8005c32ea65SRobert Watson 		unlock_udbinfo = 1;
8015c32ea65SRobert Watson 	} else
8025c32ea65SRobert Watson 		unlock_udbinfo = 0;
8035c32ea65SRobert Watson 	INP_LOCK(inp);
8045c32ea65SRobert Watson 
8055c32ea65SRobert Watson #ifdef MAC
8065c32ea65SRobert Watson 	mac_create_mbuf_from_inpcb(inp, m);
8075c32ea65SRobert Watson #endif
8085c32ea65SRobert Watson 
80990162a4eSIan Dowse 	laddr = inp->inp_laddr;
81090162a4eSIan Dowse 	lport = inp->inp_lport;
811c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
812c557ae16SIan Dowse 		if (lport == 0) {
813c557ae16SIan Dowse 			error = EINVAL;
814c557ae16SIan Dowse 			goto release;
815c557ae16SIan Dowse 		}
816c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
817b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
818c557ae16SIan Dowse 		if (error)
819c557ae16SIan Dowse 			goto release;
820c557ae16SIan Dowse 	}
821c557ae16SIan Dowse 
822df8bae1dSRodney W. Grimes 	if (addr) {
82375c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
824a854ed98SJohn Baldwin 		if (td && jailed(td->td_ucred))
825a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
826df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
827df8bae1dSRodney W. Grimes 			error = EISCONN;
828df8bae1dSRodney W. Grimes 			goto release;
829df8bae1dSRodney W. Grimes 		}
83090162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
831b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
83290162a4eSIan Dowse 		if (error)
83390162a4eSIan Dowse 			goto release;
83490162a4eSIan Dowse 
83590162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
83690162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
83790162a4eSIan Dowse 		    inp->inp_lport == 0) {
83890162a4eSIan Dowse 			inp->inp_lport = lport;
83990162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
84090162a4eSIan Dowse 				inp->inp_lport = 0;
84190162a4eSIan Dowse 				error = EAGAIN;
842df8bae1dSRodney W. Grimes 				goto release;
843df8bae1dSRodney W. Grimes 			}
84490162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
84590162a4eSIan Dowse 		}
846df8bae1dSRodney W. Grimes 	} else {
84790162a4eSIan Dowse 		faddr = inp->inp_faddr;
84890162a4eSIan Dowse 		fport = inp->inp_fport;
84990162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
850df8bae1dSRodney W. Grimes 			error = ENOTCONN;
851df8bae1dSRodney W. Grimes 			goto release;
852df8bae1dSRodney W. Grimes 		}
853df8bae1dSRodney W. Grimes 	}
854e6ccd709SRobert Watson 
855df8bae1dSRodney W. Grimes 	/*
856e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
857e6ccd709SRobert Watson 	 * link-layer headers.
858df8bae1dSRodney W. Grimes 	 */
859e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
860e6ccd709SRobert Watson 	if (m == NULL) {
861df8bae1dSRodney W. Grimes 		error = ENOBUFS;
86249b19bfcSBruce M Simpson 		goto release;
863df8bae1dSRodney W. Grimes 	}
864e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
865e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes 	/*
868df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
869df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
870df8bae1dSRodney W. Grimes 	 */
871df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
872db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
873df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
87490162a4eSIan Dowse 	ui->ui_src = laddr;
87590162a4eSIan Dowse 	ui->ui_dst = faddr;
87690162a4eSIan Dowse 	ui->ui_sport = lport;
87790162a4eSIan Dowse 	ui->ui_dport = fport;
878db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
879df8bae1dSRodney W. Grimes 
8808afa2304SBruce M Simpson 	ipflags = inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST);
8818afa2304SBruce M Simpson 	if (inp->inp_flags & 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) {
8888a538743SBruce M Simpson 		if (inp->inp_flags & 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 */
9190312fbe9SPoul-Henning Kamp SYSCTL_INT(_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 				      );
9290312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9300ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
931df8bae1dSRodney W. Grimes 
932d0390e05SGarrett Wollman static int
933d0390e05SGarrett Wollman udp_abort(struct socket *so)
934df8bae1dSRodney W. Grimes {
935d0390e05SGarrett Wollman 	struct inpcb *inp;
936df8bae1dSRodney W. Grimes 	int s;
937df8bae1dSRodney W. Grimes 
938f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
939d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
940f76fcf6dSJeffrey Hsu 	if (inp == 0) {
941f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
942d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
943f76fcf6dSJeffrey Hsu 	}
944f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
945d0390e05SGarrett Wollman 	soisdisconnected(so);
946d0390e05SGarrett Wollman 	s = splnet();
947d0390e05SGarrett Wollman 	in_pcbdetach(inp);
948f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
949d0390e05SGarrett Wollman 	splx(s);
950d0390e05SGarrett Wollman 	return 0;
951df8bae1dSRodney W. Grimes }
952df8bae1dSRodney W. Grimes 
953d0390e05SGarrett Wollman static int
954b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
955d0390e05SGarrett Wollman {
956d0390e05SGarrett Wollman 	struct inpcb *inp;
957d0390e05SGarrett Wollman 	int s, error;
958d0390e05SGarrett Wollman 
959f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
960d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
961f76fcf6dSJeffrey Hsu 	if (inp != 0) {
962f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
963d0390e05SGarrett Wollman 		return EINVAL;
964f76fcf6dSJeffrey Hsu 	}
965cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
966f76fcf6dSJeffrey Hsu 	if (error) {
967f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
968cfa1ca9dSYoshinobu Inoue 		return error;
969f76fcf6dSJeffrey Hsu 	}
970df8bae1dSRodney W. Grimes 	s = splnet();
9716823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &udbinfo, "udpinp");
972df8bae1dSRodney W. Grimes 	splx(s);
97353b57cd1SSam Leffler 	if (error) {
97453b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
975d0390e05SGarrett Wollman 		return error;
97653b57cd1SSam Leffler 	}
977cfa1ca9dSYoshinobu Inoue 
978cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
979f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
980f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
981cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
982cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
983f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
984d0390e05SGarrett Wollman 	return 0;
985df8bae1dSRodney W. Grimes }
986d0390e05SGarrett Wollman 
987d0390e05SGarrett Wollman static int
988b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
989d0390e05SGarrett Wollman {
990d0390e05SGarrett Wollman 	struct inpcb *inp;
991d0390e05SGarrett Wollman 	int s, error;
992d0390e05SGarrett Wollman 
993f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
994d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
995f76fcf6dSJeffrey Hsu 	if (inp == 0) {
996f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
997d0390e05SGarrett Wollman 		return EINVAL;
998f76fcf6dSJeffrey Hsu 	}
999f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1000df8bae1dSRodney W. Grimes 	s = splnet();
1001b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
1002d0390e05SGarrett Wollman 	splx(s);
1003f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1004f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1005d0390e05SGarrett Wollman 	return error;
1006d0390e05SGarrett Wollman }
1007d0390e05SGarrett Wollman 
1008d0390e05SGarrett Wollman static int
1009b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1010d0390e05SGarrett Wollman {
1011d0390e05SGarrett Wollman 	struct inpcb *inp;
1012d0390e05SGarrett Wollman 	int s, error;
101375c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1014d0390e05SGarrett Wollman 
1015f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1016d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1017f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1018f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1019d0390e05SGarrett Wollman 		return EINVAL;
1020f76fcf6dSJeffrey Hsu 	}
1021f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1022f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1023f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1024f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1025d0390e05SGarrett Wollman 		return EISCONN;
1026f76fcf6dSJeffrey Hsu 	}
1027d0390e05SGarrett Wollman 	s = splnet();
102875c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1029a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
1030a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1031b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
1032df8bae1dSRodney W. Grimes 	splx(s);
10334cc20ab1SSeigo Tanimura 	if (error == 0)
1034df8bae1dSRodney W. Grimes 		soisconnected(so);
1035f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1036f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1037d0390e05SGarrett Wollman 	return error;
1038df8bae1dSRodney W. Grimes }
1039d0390e05SGarrett Wollman 
1040d0390e05SGarrett Wollman static int
1041d0390e05SGarrett Wollman udp_detach(struct socket *so)
1042d0390e05SGarrett Wollman {
1043d0390e05SGarrett Wollman 	struct inpcb *inp;
1044d0390e05SGarrett Wollman 	int s;
1045d0390e05SGarrett Wollman 
1046f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1047d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1048f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1049f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1050d0390e05SGarrett Wollman 		return EINVAL;
1051f76fcf6dSJeffrey Hsu 	}
1052f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1053d0390e05SGarrett Wollman 	s = splnet();
1054d0390e05SGarrett Wollman 	in_pcbdetach(inp);
1055f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1056d0390e05SGarrett Wollman 	splx(s);
1057d0390e05SGarrett Wollman 	return 0;
1058d0390e05SGarrett Wollman }
1059d0390e05SGarrett Wollman 
1060d0390e05SGarrett Wollman static int
1061d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1062d0390e05SGarrett Wollman {
1063d0390e05SGarrett Wollman 	struct inpcb *inp;
1064d0390e05SGarrett Wollman 	int s;
1065d0390e05SGarrett Wollman 
1066f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1067d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1068f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1069f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1070d0390e05SGarrett Wollman 		return EINVAL;
1071f76fcf6dSJeffrey Hsu 	}
1072f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1073f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1074f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1075f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1076d0390e05SGarrett Wollman 		return ENOTCONN;
1077f76fcf6dSJeffrey Hsu 	}
1078d0390e05SGarrett Wollman 
1079df8bae1dSRodney W. Grimes 	s = splnet();
1080df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1081df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1082f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1083f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1084df8bae1dSRodney W. Grimes 	splx(s);
1085df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1086d0390e05SGarrett Wollman 	return 0;
1087df8bae1dSRodney W. Grimes }
1088df8bae1dSRodney W. Grimes 
1089d0390e05SGarrett Wollman static int
109057bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1091b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1092d0390e05SGarrett Wollman {
1093d0390e05SGarrett Wollman 	struct inpcb *inp;
1094d0390e05SGarrett Wollman 
1095d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
10965c32ea65SRobert Watson 	return udp_output(inp, m, addr, control, td);
1097d0390e05SGarrett Wollman }
1098d0390e05SGarrett Wollman 
109976429de4SYoshinobu Inoue int
1100d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1101d0390e05SGarrett Wollman {
1102d0390e05SGarrett Wollman 	struct inpcb *inp;
1103d0390e05SGarrett Wollman 
1104f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1105d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1106f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1107f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1108d0390e05SGarrett Wollman 		return EINVAL;
1109f76fcf6dSJeffrey Hsu 	}
1110f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1111f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1112d0390e05SGarrett Wollman 	socantsendmore(so);
1113f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1114d0390e05SGarrett Wollman 	return 0;
1115d0390e05SGarrett Wollman }
1116d0390e05SGarrett Wollman 
1117f76fcf6dSJeffrey Hsu /*
1118f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1119f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1120f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1121f76fcf6dSJeffrey Hsu  */
1122f76fcf6dSJeffrey Hsu static int
1123f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1124f76fcf6dSJeffrey Hsu {
1125f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1126f76fcf6dSJeffrey Hsu }
1127f76fcf6dSJeffrey Hsu 
1128f76fcf6dSJeffrey Hsu /*
1129f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1130f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1131f76fcf6dSJeffrey Hsu  */
1132f76fcf6dSJeffrey Hsu static int
1133f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1134f76fcf6dSJeffrey Hsu {
1135f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1136f76fcf6dSJeffrey Hsu }
1137f76fcf6dSJeffrey Hsu 
1138d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1139117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1140117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1141f76fcf6dSJeffrey Hsu 	pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1142117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1143a557af22SRobert Watson 	udp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
1144d0390e05SGarrett Wollman };
1145