xref: /freebsd/sys/netinet/udp_usrreq.c (revision 277afaff667dbe7d85e7b926ac313aa304f83276)
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 
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 
123c1cd65baSBruce Evans static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
124d4b509bdSRobert Watson 		int off, struct sockaddr_in *udp_in);
125cfa1ca9dSYoshinobu Inoue 
1264d77a549SAlfred Perlstein static int udp_detach(struct socket *so);
1274d77a549SAlfred Perlstein static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1284d77a549SAlfred Perlstein 		struct mbuf *, struct thread *);
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes void
131032dcc76SLuigi Rizzo udp_init()
132df8bae1dSRodney W. Grimes {
133f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
13415bd2b43SDavid Greenman 	LIST_INIT(&udb);
13515bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
136ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1378781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1388781d8e9SBruce Evans 					&udbinfo.porthashmask);
13969c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
140420a2811SAndre Oppermann 	    NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
14169c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
142df8bae1dSRodney W. Grimes }
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes void
145032dcc76SLuigi Rizzo udp_input(m, off)
146032dcc76SLuigi Rizzo 	register struct mbuf *m;
147032dcc76SLuigi Rizzo 	int off;
148df8bae1dSRodney W. Grimes {
149cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
150032dcc76SLuigi Rizzo 	register struct ip *ip;
151032dcc76SLuigi Rizzo 	register struct udphdr *uh;
152032dcc76SLuigi Rizzo 	register struct inpcb *inp;
153df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
154df8bae1dSRodney W. Grimes 	int len;
155df8bae1dSRodney W. Grimes 	struct ip save_ip;
156d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes 	/*
161df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
162df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
163df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
164df8bae1dSRodney W. Grimes 	 * with options still present.
165df8bae1dSRodney W. Grimes 	 */
166df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
167df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
168df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
169df8bae1dSRodney W. Grimes 	}
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes 	/*
172df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
173df8bae1dSRodney W. Grimes 	 */
174df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
175df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
176df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
177df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
178df8bae1dSRodney W. Grimes 			return;
179df8bae1dSRodney W. Grimes 		}
180df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
181df8bae1dSRodney W. Grimes 	}
182df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
183df8bae1dSRodney W. Grimes 
184686cdd19SJun-ichiro itojun Hagino 	/* destination port of 0 is illegal, based on RFC768. */
185686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
186f76fcf6dSJeffrey Hsu 		goto badunlocked;
187686cdd19SJun-ichiro itojun Hagino 
188df8bae1dSRodney W. Grimes 	/*
189b9234fafSSam Leffler 	 * Construct sockaddr format source address.
190b9234fafSSam Leffler 	 * Stuff source address and datagram in user buffer.
191b9234fafSSam Leffler 	 */
192d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
193d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
194d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
195b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
196b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
197b9234fafSSam Leffler 
198b9234fafSSam Leffler 	/*
199df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
200df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
201df8bae1dSRodney W. Grimes 	 */
202df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
203df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
2047eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
205df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
206f76fcf6dSJeffrey Hsu 			goto badunlocked;
207df8bae1dSRodney W. Grimes 		}
208df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
209df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
210df8bae1dSRodney W. Grimes 	}
211df8bae1dSRodney W. Grimes 	/*
212df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
213df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
214df8bae1dSRodney W. Grimes 	 */
21548cb400fSRuslan Ermilov 	if (!blackhole)
216df8bae1dSRodney W. Grimes 		save_ip = *ip;
217df8bae1dSRodney W. Grimes 
218df8bae1dSRodney W. Grimes 	/*
219df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
220df8bae1dSRodney W. Grimes 	 */
2216dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
222db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
223db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
224db4f9cc7SJonathan Lemon 				uh->uh_sum = m->m_pkthdr.csum_data;
225db4f9cc7SJonathan Lemon 			else
226db4f9cc7SJonathan Lemon 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
227506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
228db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
229db4f9cc7SJonathan Lemon 			uh->uh_sum ^= 0xffff;
230db4f9cc7SJonathan Lemon 		} else {
231cb342100SHajimu UMEMOTO 			char b[9];
232cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
2336effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
234df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
235623ae52eSPoul-Henning Kamp 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
236cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
237db4f9cc7SJonathan Lemon 		}
238623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
239df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
240df8bae1dSRodney W. Grimes 			m_freem(m);
241df8bae1dSRodney W. Grimes 			return;
242df8bae1dSRodney W. Grimes 		}
243fb9aaba0SRuslan Ermilov 	} else
244fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
245df8bae1dSRodney W. Grimes 
246f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
247f76fcf6dSJeffrey Hsu 
248df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
249df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
25082c23ebaSBill Fenner 		struct inpcb *last;
251df8bae1dSRodney W. Grimes 		/*
252df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
253df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
254df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
255df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
256df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
257df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
258df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
259df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
260df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
261df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
262df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
263df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
264df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
265df8bae1dSRodney W. Grimes 		 */
266df8bae1dSRodney W. Grimes 
267df8bae1dSRodney W. Grimes 		/*
268df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
269df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
270df8bae1dSRodney W. Grimes 		 */
271df8bae1dSRodney W. Grimes 		last = NULL;
272cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
2739c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
274f76fcf6dSJeffrey Hsu 				continue;
275cfa1ca9dSYoshinobu Inoue #ifdef INET6
276369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
2779c1df695SRobert Watson 				continue;
278cfa1ca9dSYoshinobu Inoue #endif
279df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
280f76fcf6dSJeffrey Hsu 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
2819c1df695SRobert Watson 					continue;
282df8bae1dSRodney W. Grimes 			}
283df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
284df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
285df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
286df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
2879c1df695SRobert Watson 					continue;
288df8bae1dSRodney W. Grimes 			}
2899c1df695SRobert Watson 			INP_LOCK(inp);
290df8bae1dSRodney W. Grimes 
29183453a06SBruce M Simpson 			/*
29283453a06SBruce M Simpson 			 * Check multicast packets to make sure they are only
29383453a06SBruce M Simpson 			 * sent to sockets with multicast memberships for the
29483453a06SBruce M Simpson 			 * packet's destination address and arrival interface
29583453a06SBruce M Simpson 			 */
29683453a06SBruce M Simpson #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
29783453a06SBruce M Simpson #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
29883453a06SBruce M Simpson 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
29983453a06SBruce M Simpson 				int mship, foundmship = 0;
30083453a06SBruce M Simpson 
30183453a06SBruce M Simpson 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
30283453a06SBruce M Simpson 					if (MSHIP(inp, mship)->inm_addr.s_addr
30383453a06SBruce M Simpson 					    == ip->ip_dst.s_addr &&
30483453a06SBruce M Simpson 					    MSHIP(inp, mship)->inm_ifp
30583453a06SBruce M Simpson 					    == m->m_pkthdr.rcvif) {
30683453a06SBruce M Simpson 						foundmship = 1;
30783453a06SBruce M Simpson 						break;
30883453a06SBruce M Simpson 					}
30983453a06SBruce M Simpson 				}
3109c1df695SRobert Watson 				if (foundmship == 0) {
3119c1df695SRobert Watson 					INP_UNLOCK(inp);
3129c1df695SRobert Watson 					continue;
3139c1df695SRobert Watson 				}
31483453a06SBruce M Simpson 			}
31583453a06SBruce M Simpson #undef NMSHIPS
31683453a06SBruce M Simpson #undef MSHIP
317df8bae1dSRodney W. Grimes 			if (last != NULL) {
318df8bae1dSRodney W. Grimes 				struct mbuf *n;
319df8bae1dSRodney W. Grimes 
320032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
321365433d9SRobert Watson 				if (n != NULL)
322cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
323cfa1ca9dSYoshinobu Inoue 						   iphlen +
324d4b509bdSRobert Watson 						   sizeof(struct udphdr),
325d4b509bdSRobert Watson 						   &udp_in);
326f76fcf6dSJeffrey Hsu 				INP_UNLOCK(last);
327df8bae1dSRodney W. Grimes 			}
32882c23ebaSBill Fenner 			last = inp;
329df8bae1dSRodney W. Grimes 			/*
330df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
331df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
332df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
333df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
334df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
335df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
336df8bae1dSRodney W. Grimes 			 */
3374cc20ab1SSeigo Tanimura 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
338df8bae1dSRodney W. Grimes 				break;
339df8bae1dSRodney W. Grimes 		}
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes 		if (last == NULL) {
342df8bae1dSRodney W. Grimes 			/*
343df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
344df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
345df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
346df8bae1dSRodney W. Grimes 			 */
347df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
34861ffc0b1SJeffrey Hsu 			goto badheadlocked;
349df8bae1dSRodney W. Grimes 		}
350d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
351d4b509bdSRobert Watson 		    &udp_in);
3529b657230SSam Leffler 		INP_UNLOCK(last);
3536b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
354df8bae1dSRodney W. Grimes 		return;
355df8bae1dSRodney W. Grimes 	}
356df8bae1dSRodney W. Grimes 	/*
3576d6a026bSDavid Greenman 	 * Locate pcb for datagram.
358df8bae1dSRodney W. Grimes 	 */
359c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
360cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
36115bd2b43SDavid Greenman 	if (inp == NULL) {
36275cfc95fSAndrey A. Chernov 		if (log_in_vain) {
363df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
36475cfc95fSAndrey A. Chernov 
36575cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
366592071e8SBruce Evans 			log(LOG_INFO,
367592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
368592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
369592071e8SBruce Evans 			    ntohs(uh->uh_sport));
37075cfc95fSAndrey A. Chernov 		}
371df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
372df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
373df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
37461ffc0b1SJeffrey Hsu 			goto badheadlocked;
375df8bae1dSRodney W. Grimes 		}
376582a7760SBruce Evans 		if (blackhole)
37761ffc0b1SJeffrey Hsu 			goto badheadlocked;
3781cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
3791cbd978eSLuigi Rizzo 			goto badheadlocked;
38004287599SRuslan Ermilov 		*ip = save_ip;
38104287599SRuslan Ermilov 		ip->ip_len += iphlen;
382582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
383f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
384df8bae1dSRodney W. Grimes 		return;
385df8bae1dSRodney W. Grimes 	}
38661ffc0b1SJeffrey Hsu 	INP_LOCK(inp);
387d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
388f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
3896b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
390df8bae1dSRodney W. Grimes 	return;
39161ffc0b1SJeffrey Hsu 
39261ffc0b1SJeffrey Hsu badheadlocked:
393f76fcf6dSJeffrey Hsu 	if (inp)
394f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
3956b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
396f76fcf6dSJeffrey Hsu badunlocked:
397df8bae1dSRodney W. Grimes 	m_freem(m);
398df8bae1dSRodney W. Grimes 	if (opts)
399df8bae1dSRodney W. Grimes 		m_freem(opts);
400cfa1ca9dSYoshinobu Inoue 	return;
401cfa1ca9dSYoshinobu Inoue }
402cfa1ca9dSYoshinobu Inoue 
403cfa1ca9dSYoshinobu Inoue /*
404d4b509bdSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
405d4b509bdSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
406d4b509bdSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
407d4b509bdSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
408d4b509bdSRobert Watson  * into the socket code.
409cfa1ca9dSYoshinobu Inoue  */
410cfa1ca9dSYoshinobu Inoue static void
411d4b509bdSRobert Watson udp_append(last, ip, n, off, udp_in)
412032dcc76SLuigi Rizzo 	struct inpcb *last;
413032dcc76SLuigi Rizzo 	struct ip *ip;
414032dcc76SLuigi Rizzo 	struct mbuf *n;
415032dcc76SLuigi Rizzo 	int off;
416d4b509bdSRobert Watson 	struct sockaddr_in *udp_in;
417cfa1ca9dSYoshinobu Inoue {
418cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
4191e4d7da7SRobert Watson 	struct socket *so;
420cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
421c83c1318SPoul-Henning Kamp #ifdef INET6
422d4b509bdSRobert Watson 	struct sockaddr_in6 udp_in6;
423c83c1318SPoul-Henning Kamp #endif
424cfa1ca9dSYoshinobu Inoue 
42587f2bb8cSRobert Watson 	INP_LOCK_ASSERT(last);
42687f2bb8cSRobert Watson 
427da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
428da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
429da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
430b9234fafSSam Leffler #ifdef IPSEC
431b9234fafSSam Leffler 		ipsecstat.in_polvio++;
432b9234fafSSam Leffler #endif /*IPSEC*/
433b9234fafSSam Leffler 		m_freem(n);
434b9234fafSSam Leffler 		return;
435b9234fafSSam Leffler 	}
436da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
437b9234fafSSam Leffler #ifdef MAC
438a557af22SRobert Watson 	if (mac_check_inpcb_deliver(last, n) != 0) {
439b9234fafSSam Leffler 		m_freem(n);
440b9234fafSSam Leffler 		return;
441b9234fafSSam Leffler 	}
442b9234fafSSam Leffler #endif
443cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
444be8a62e8SPoul-Henning Kamp 	    last->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
445cfa1ca9dSYoshinobu Inoue #ifdef INET6
446cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
447cfa1ca9dSYoshinobu Inoue 			int savedflags;
448cfa1ca9dSYoshinobu Inoue 
449cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
450cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
45111de19f4SHajimu UMEMOTO 			ip6_savecontrol(last, n, &opts);
452cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
453cfa1ca9dSYoshinobu Inoue 		} else
454cfa1ca9dSYoshinobu Inoue #endif
455cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
4564cc20ab1SSeigo Tanimura 	}
457cfa1ca9dSYoshinobu Inoue #ifdef INET6
458cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
459d4b509bdSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
460d4b509bdSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
461d4b509bdSRobert Watson 		udp_in6.sin6_family = AF_INET6;
462d4b509bdSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
463d4b509bdSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
464cfa1ca9dSYoshinobu Inoue 	} else
465cfa1ca9dSYoshinobu Inoue #endif
466d4b509bdSRobert Watson 	append_sa = (struct sockaddr *)udp_in;
467cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
4681e4d7da7SRobert Watson 
4691e4d7da7SRobert Watson 	so = last->inp_socket;
4701e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
4711e4d7da7SRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
472cfa1ca9dSYoshinobu Inoue 		m_freem(n);
473cfa1ca9dSYoshinobu Inoue 		if (opts)
474cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
475cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
4761e4d7da7SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
4774cc20ab1SSeigo Tanimura 	} else
4781e4d7da7SRobert Watson 		sorwakeup_locked(so);
479df8bae1dSRodney W. Grimes }
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes /*
482df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
483df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
484df8bae1dSRodney W. Grimes  */
4853ce144eaSJeffrey Hsu struct inpcb *
486032dcc76SLuigi Rizzo udp_notify(inp, errno)
487032dcc76SLuigi Rizzo 	register struct inpcb *inp;
488032dcc76SLuigi Rizzo 	int errno;
489df8bae1dSRodney W. Grimes {
490df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
491df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
492df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
4933ce144eaSJeffrey Hsu 	return inp;
494df8bae1dSRodney W. Grimes }
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes void
497032dcc76SLuigi Rizzo udp_ctlinput(cmd, sa, vip)
498032dcc76SLuigi Rizzo 	int cmd;
499032dcc76SLuigi Rizzo 	struct sockaddr *sa;
500032dcc76SLuigi Rizzo 	void *vip;
501df8bae1dSRodney W. Grimes {
502c693a045SJonathan Lemon 	struct ip *ip = vip;
503c693a045SJonathan Lemon 	struct udphdr *uh;
5043ce144eaSJeffrey Hsu 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
505c693a045SJonathan Lemon 	struct in_addr faddr;
506c693a045SJonathan Lemon 	struct inpcb *inp;
507c693a045SJonathan Lemon 
508c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
509c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
510c693a045SJonathan Lemon 		return;
511df8bae1dSRodney W. Grimes 
51297d8d152SAndre Oppermann 	/*
51397d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
51497d8d152SAndre Oppermann 	 */
51597d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
51697d8d152SAndre Oppermann 		return;
51797d8d152SAndre Oppermann 	/*
51897d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
51997d8d152SAndre Oppermann 	 * XXX: We never get this from ICMP, otherwise it makes an
52097d8d152SAndre Oppermann 	 * excellent DoS attack on machines with many connections.
52197d8d152SAndre Oppermann 	 */
52297d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
523d1c54148SJesper Skriver 		ip = 0;
524d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
525df8bae1dSRodney W. Grimes 		return;
526df8bae1dSRodney W. Grimes 	if (ip) {
527df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
528f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
529c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
530c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
531f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
532f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
533f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
534c693a045SJonathan Lemon 				(*notify)(inp, inetctlerrmap[cmd]);
535f76fcf6dSJeffrey Hsu 			}
536f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
537f76fcf6dSJeffrey Hsu 		}
538f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
539df8bae1dSRodney W. Grimes 	} else
540f76fcf6dSJeffrey Hsu 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
541df8bae1dSRodney W. Grimes }
542df8bae1dSRodney W. Grimes 
5430312fbe9SPoul-Henning Kamp static int
54482d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
54598271db4SGarrett Wollman {
546277afaffSRobert Watson 	int error, i, n;
54798271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
54898271db4SGarrett Wollman 	inp_gen_t gencnt;
54998271db4SGarrett Wollman 	struct xinpgen xig;
55098271db4SGarrett Wollman 
55198271db4SGarrett Wollman 	/*
55298271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
55398271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
55498271db4SGarrett Wollman 	 */
55598271db4SGarrett Wollman 	if (req->oldptr == 0) {
55698271db4SGarrett Wollman 		n = udbinfo.ipi_count;
55798271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
55898271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
55998271db4SGarrett Wollman 		return 0;
56098271db4SGarrett Wollman 	}
56198271db4SGarrett Wollman 
56298271db4SGarrett Wollman 	if (req->newptr != 0)
56398271db4SGarrett Wollman 		return EPERM;
56498271db4SGarrett Wollman 
56598271db4SGarrett Wollman 	/*
56698271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
56798271db4SGarrett Wollman 	 */
5684b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
56998271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
57098271db4SGarrett Wollman 	n = udbinfo.ipi_count;
5714b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
57298271db4SGarrett Wollman 
57347934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
5745c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
57547934cefSDon Lewis 	if (error != 0)
57647934cefSDon Lewis 		return (error);
5775c38b6dbSDon Lewis 
57898271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
57998271db4SGarrett Wollman 	xig.xig_count = n;
58098271db4SGarrett Wollman 	xig.xig_gen = gencnt;
58198271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
58298271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
58398271db4SGarrett Wollman 	if (error)
58498271db4SGarrett Wollman 		return error;
58598271db4SGarrett Wollman 
586a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
58798271db4SGarrett Wollman 	if (inp_list == 0)
58898271db4SGarrett Wollman 		return ENOMEM;
58998271db4SGarrett Wollman 
590f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
591fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
592fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
593f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);
5942ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
5952ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
59698271db4SGarrett Wollman 			inp_list[i++] = inp;
597f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
5984787fd37SPaul Saab 	}
599f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
60098271db4SGarrett Wollman 	n = i;
60198271db4SGarrett Wollman 
60298271db4SGarrett Wollman 	error = 0;
60398271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
60498271db4SGarrett Wollman 		inp = inp_list[i];
60598271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
60698271db4SGarrett Wollman 			struct xinpcb xi;
607fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
60898271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
60998271db4SGarrett Wollman 			/* XXX should avoid extra copy */
61098271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
61198271db4SGarrett Wollman 			if (inp->inp_socket)
61298271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6134b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
61498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
61598271db4SGarrett Wollman 		}
61698271db4SGarrett Wollman 	}
61798271db4SGarrett Wollman 	if (!error) {
61898271db4SGarrett Wollman 		/*
61998271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
62098271db4SGarrett Wollman 		 * If the generation differs from what we told
62198271db4SGarrett Wollman 		 * her before, she knows that something happened
62298271db4SGarrett Wollman 		 * while we were processing this request, and it
62398271db4SGarrett Wollman 		 * might be necessary to retry.
62498271db4SGarrett Wollman 		 */
625f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
62698271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
62798271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
62898271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
629f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
63098271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
63198271db4SGarrett Wollman 	}
63298271db4SGarrett Wollman 	free(inp_list, M_TEMP);
63398271db4SGarrett Wollman 	return error;
63498271db4SGarrett Wollman }
63598271db4SGarrett Wollman 
63698271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
63798271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
63898271db4SGarrett Wollman 
63998271db4SGarrett Wollman static int
64082d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
641490d50b6SBrian Feldman {
642c0511d3bSBrian Feldman 	struct xucred xuc;
643490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
644490d50b6SBrian Feldman 	struct inpcb *inp;
645277afaffSRobert Watson 	int error;
646490d50b6SBrian Feldman 
64756f21b9dSColin Percival 	error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
648490d50b6SBrian Feldman 	if (error)
649490d50b6SBrian Feldman 		return (error);
650490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
651490d50b6SBrian Feldman 	if (error)
652490d50b6SBrian Feldman 		return (error);
653f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
654490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
655cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6562d20c83fSDon Lewis 	if (inp == NULL || inp->inp_socket == NULL) {
657490d50b6SBrian Feldman 		error = ENOENT;
658490d50b6SBrian Feldman 		goto out;
659490d50b6SBrian Feldman 	}
66029dc1288SRobert Watson 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
6617ce87f12SDavid Malone 	if (error)
6627ce87f12SDavid Malone 		goto out;
66376183f34SDima Dorfman 	cru2x(inp->inp_socket->so_cred, &xuc);
664490d50b6SBrian Feldman out:
665f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
6660e1eebb8SDon Lewis 	if (error == 0)
6670e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
668490d50b6SBrian Feldman 	return (error);
669490d50b6SBrian Feldman }
670490d50b6SBrian Feldman 
6717ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
6727ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
6737ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
674490d50b6SBrian Feldman 
675490d50b6SBrian Feldman static int
676032dcc76SLuigi Rizzo udp_output(inp, m, addr, control, td)
677032dcc76SLuigi Rizzo 	register struct inpcb *inp;
678032dcc76SLuigi Rizzo 	struct mbuf *m;
679032dcc76SLuigi Rizzo 	struct sockaddr *addr;
680032dcc76SLuigi Rizzo 	struct mbuf *control;
681032dcc76SLuigi Rizzo 	struct thread *td;
682df8bae1dSRodney W. Grimes {
683032dcc76SLuigi Rizzo 	register struct udpiphdr *ui;
684032dcc76SLuigi Rizzo 	register int len = m->m_pkthdr.len;
68590162a4eSIan Dowse 	struct in_addr faddr, laddr;
686c557ae16SIan Dowse 	struct cmsghdr *cm;
687c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
68890162a4eSIan Dowse 	int error = 0;
6898afa2304SBruce M Simpson 	int ipflags;
69090162a4eSIan Dowse 	u_short fport, lport;
6915c32ea65SRobert Watson 	int unlock_udbinfo;
692df8bae1dSRodney W. Grimes 
6935c32ea65SRobert Watson 	/*
6945c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
6955c32ea65SRobert Watson 	 * inpcb.  As such, we don't know up front what inpcb locks we will
6965c32ea65SRobert Watson 	 * need.  Do any work to decide what is needed up front before
6975c32ea65SRobert Watson 	 * acquiring locks.
6985c32ea65SRobert Watson 	 */
699430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
700c557ae16SIan Dowse 		if (control)
701c557ae16SIan Dowse 			m_freem(control);
7025c32ea65SRobert Watson 		m_freem(m);
7035c32ea65SRobert Watson 		return EMSGSIZE;
704430d30d8SBill Fenner 	}
705430d30d8SBill Fenner 
706c557ae16SIan Dowse 	src.sin_addr.s_addr = INADDR_ANY;
707c557ae16SIan Dowse 	if (control != NULL) {
708c557ae16SIan Dowse 		/*
709c557ae16SIan Dowse 		 * XXX: Currently, we assume all the optional information
710c557ae16SIan Dowse 		 * is stored in a single mbuf.
711c557ae16SIan Dowse 		 */
712c557ae16SIan Dowse 		if (control->m_next) {
713c557ae16SIan Dowse 			m_freem(control);
7145c32ea65SRobert Watson 			m_freem(m);
7155c32ea65SRobert Watson 			return EINVAL;
716c557ae16SIan Dowse 		}
717c557ae16SIan Dowse 		for (; control->m_len > 0;
718c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
719c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
720c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
721c557ae16SIan Dowse 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
722c557ae16SIan Dowse 			    cm->cmsg_len > control->m_len) {
723c557ae16SIan Dowse 				error = EINVAL;
724c557ae16SIan Dowse 				break;
725c557ae16SIan Dowse 			}
726c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
727c557ae16SIan Dowse 				continue;
728c557ae16SIan Dowse 
729c557ae16SIan Dowse 			switch (cm->cmsg_type) {
730c557ae16SIan Dowse 			case IP_SENDSRCADDR:
731c557ae16SIan Dowse 				if (cm->cmsg_len !=
732c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
733c557ae16SIan Dowse 					error = EINVAL;
734c557ae16SIan Dowse 					break;
735c557ae16SIan Dowse 				}
736c557ae16SIan Dowse 				bzero(&src, sizeof(src));
737c557ae16SIan Dowse 				src.sin_family = AF_INET;
738c557ae16SIan Dowse 				src.sin_len = sizeof(src);
739c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
740c557ae16SIan Dowse 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
741c557ae16SIan Dowse 				break;
742c557ae16SIan Dowse 			default:
743c557ae16SIan Dowse 				error = ENOPROTOOPT;
744c557ae16SIan Dowse 				break;
745c557ae16SIan Dowse 			}
746c557ae16SIan Dowse 			if (error)
747c557ae16SIan Dowse 				break;
748c557ae16SIan Dowse 		}
749c557ae16SIan Dowse 		m_freem(control);
750c557ae16SIan Dowse 	}
7515c32ea65SRobert Watson 	if (error) {
7525c32ea65SRobert Watson 		m_freem(m);
7535c32ea65SRobert Watson 		return error;
7545c32ea65SRobert Watson 	}
7555c32ea65SRobert Watson 
7565c32ea65SRobert Watson 	if (src.sin_addr.s_addr != INADDR_ANY ||
7575c32ea65SRobert Watson 	    addr != NULL) {
7585c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
7595c32ea65SRobert Watson 		unlock_udbinfo = 1;
7605c32ea65SRobert Watson 	} else
7615c32ea65SRobert Watson 		unlock_udbinfo = 0;
7625c32ea65SRobert Watson 	INP_LOCK(inp);
7635c32ea65SRobert Watson 
7645c32ea65SRobert Watson #ifdef MAC
7655c32ea65SRobert Watson 	mac_create_mbuf_from_inpcb(inp, m);
7665c32ea65SRobert Watson #endif
7675c32ea65SRobert Watson 
76890162a4eSIan Dowse 	laddr = inp->inp_laddr;
76990162a4eSIan Dowse 	lport = inp->inp_lport;
770c557ae16SIan Dowse 	if (src.sin_addr.s_addr != INADDR_ANY) {
771c557ae16SIan Dowse 		if (lport == 0) {
772c557ae16SIan Dowse 			error = EINVAL;
773c557ae16SIan Dowse 			goto release;
774c557ae16SIan Dowse 		}
775c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
776b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
777c557ae16SIan Dowse 		if (error)
778c557ae16SIan Dowse 			goto release;
779c557ae16SIan Dowse 	}
780c557ae16SIan Dowse 
781df8bae1dSRodney W. Grimes 	if (addr) {
78275c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
783812d8653SSam Leffler 		if (jailed(td->td_ucred))
784a854ed98SJohn Baldwin 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
785df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
786df8bae1dSRodney W. Grimes 			error = EISCONN;
787df8bae1dSRodney W. Grimes 			goto release;
788df8bae1dSRodney W. Grimes 		}
78990162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
790b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
79190162a4eSIan Dowse 		if (error)
79290162a4eSIan Dowse 			goto release;
79390162a4eSIan Dowse 
79490162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
79590162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
79690162a4eSIan Dowse 		    inp->inp_lport == 0) {
7973a1757b9SGleb Smirnoff 			/*
7983a1757b9SGleb Smirnoff 			 * Remember addr if jailed, to prevent rebinding.
7993a1757b9SGleb Smirnoff 			 */
8003a1757b9SGleb Smirnoff 			if (jailed(td->td_ucred))
8013a1757b9SGleb Smirnoff 				inp->inp_laddr = laddr;
80290162a4eSIan Dowse 			inp->inp_lport = lport;
80390162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
80490162a4eSIan Dowse 				inp->inp_lport = 0;
80590162a4eSIan Dowse 				error = EAGAIN;
806df8bae1dSRodney W. Grimes 				goto release;
807df8bae1dSRodney W. Grimes 			}
80890162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
80990162a4eSIan Dowse 		}
810df8bae1dSRodney W. Grimes 	} else {
81190162a4eSIan Dowse 		faddr = inp->inp_faddr;
81290162a4eSIan Dowse 		fport = inp->inp_fport;
81390162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
814df8bae1dSRodney W. Grimes 			error = ENOTCONN;
815df8bae1dSRodney W. Grimes 			goto release;
816df8bae1dSRodney W. Grimes 		}
817df8bae1dSRodney W. Grimes 	}
818e6ccd709SRobert Watson 
819df8bae1dSRodney W. Grimes 	/*
820e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
821392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
822392e8407SRobert Watson 	 * since we won't use that space at this layer.
823df8bae1dSRodney W. Grimes 	 */
824e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
825e6ccd709SRobert Watson 	if (m == NULL) {
826df8bae1dSRodney W. Grimes 		error = ENOBUFS;
82749b19bfcSBruce M Simpson 		goto release;
828df8bae1dSRodney W. Grimes 	}
829e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
830e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
831392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
832df8bae1dSRodney W. Grimes 
833df8bae1dSRodney W. Grimes 	/*
834df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
835df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
836df8bae1dSRodney W. Grimes 	 */
837df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
838db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
839df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
84090162a4eSIan Dowse 	ui->ui_src = laddr;
84190162a4eSIan Dowse 	ui->ui_dst = faddr;
84290162a4eSIan Dowse 	ui->ui_sport = lport;
84390162a4eSIan Dowse 	ui->ui_dport = fport;
844db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
845df8bae1dSRodney W. Grimes 
846b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
847b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
848b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
849b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
850b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
8518afa2304SBruce M Simpson 	if (inp->inp_flags & INP_ONESBCAST)
8528afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
8538afa2304SBruce M Simpson 
854df8bae1dSRodney W. Grimes 	/*
855db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
856df8bae1dSRodney W. Grimes 	 */
857df8bae1dSRodney W. Grimes 	if (udpcksum) {
8588a538743SBruce M Simpson 		if (inp->inp_flags & INP_ONESBCAST)
8598a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
8608a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
861db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
862db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
863db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
864db4f9cc7SJonathan Lemon 	} else {
865db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
866df8bae1dSRodney W. Grimes 	}
867df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
868ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
869ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
870df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
871cfa1ca9dSYoshinobu Inoue 
8725c32ea65SRobert Watson 	if (unlock_udbinfo)
8735c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
87497d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
8755d846453SSam Leffler 	    inp->inp_moptions, inp);
8765c32ea65SRobert Watson 	INP_UNLOCK(inp);
877df8bae1dSRodney W. Grimes 	return (error);
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes release:
8805c32ea65SRobert Watson 	INP_UNLOCK(inp);
8815c32ea65SRobert Watson 	if (unlock_udbinfo)
8825c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
883df8bae1dSRodney W. Grimes 	m_freem(m);
884df8bae1dSRodney W. Grimes 	return (error);
885df8bae1dSRodney W. Grimes }
886df8bae1dSRodney W. Grimes 
88776429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
888032dcc76SLuigi Rizzo 					/* 40 1K datagrams */
8890312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
8903d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
8910312fbe9SPoul-Henning Kamp 
892cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
893cfa1ca9dSYoshinobu Inoue #ifdef INET6
894cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
895cfa1ca9dSYoshinobu Inoue #else
896cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
897cfa1ca9dSYoshinobu Inoue #endif
898cfa1ca9dSYoshinobu Inoue 				      );
8990312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9000ca2861fSRuslan Ermilov     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
901df8bae1dSRodney W. Grimes 
902d0390e05SGarrett Wollman static int
903d0390e05SGarrett Wollman udp_abort(struct socket *so)
904df8bae1dSRodney W. Grimes {
905d0390e05SGarrett Wollman 	struct inpcb *inp;
906df8bae1dSRodney W. Grimes 
907f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
908d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
909f76fcf6dSJeffrey Hsu 	if (inp == 0) {
910f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
911d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
912f76fcf6dSJeffrey Hsu 	}
913f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
914d0390e05SGarrett Wollman 	soisdisconnected(so);
915d0390e05SGarrett Wollman 	in_pcbdetach(inp);
916f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
917d0390e05SGarrett Wollman 	return 0;
918df8bae1dSRodney W. Grimes }
919df8bae1dSRodney W. Grimes 
920d0390e05SGarrett Wollman static int
921b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
922d0390e05SGarrett Wollman {
923d0390e05SGarrett Wollman 	struct inpcb *inp;
924277afaffSRobert Watson 	int error;
925d0390e05SGarrett Wollman 
926f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
927d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
928f76fcf6dSJeffrey Hsu 	if (inp != 0) {
929f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
930d0390e05SGarrett Wollman 		return EINVAL;
931f76fcf6dSJeffrey Hsu 	}
932cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
933f76fcf6dSJeffrey Hsu 	if (error) {
934f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
935cfa1ca9dSYoshinobu Inoue 		return error;
936f76fcf6dSJeffrey Hsu 	}
9376823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &udbinfo, "udpinp");
93853b57cd1SSam Leffler 	if (error) {
93953b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
940d0390e05SGarrett Wollman 		return error;
94153b57cd1SSam Leffler 	}
942cfa1ca9dSYoshinobu Inoue 
943cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
944f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
945f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
946cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
947cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
948f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
949d0390e05SGarrett Wollman 	return 0;
950df8bae1dSRodney W. Grimes }
951d0390e05SGarrett Wollman 
952d0390e05SGarrett Wollman static int
953b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
954d0390e05SGarrett Wollman {
955d0390e05SGarrett Wollman 	struct inpcb *inp;
956277afaffSRobert Watson 	int error;
957d0390e05SGarrett Wollman 
958f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
959d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
960f76fcf6dSJeffrey Hsu 	if (inp == 0) {
961f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
962d0390e05SGarrett Wollman 		return EINVAL;
963f76fcf6dSJeffrey Hsu 	}
964f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
965b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
966f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
967f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
968d0390e05SGarrett Wollman 	return error;
969d0390e05SGarrett Wollman }
970d0390e05SGarrett Wollman 
971d0390e05SGarrett Wollman static int
972b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
973d0390e05SGarrett Wollman {
974d0390e05SGarrett Wollman 	struct inpcb *inp;
975277afaffSRobert Watson 	int error;
97675c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
977d0390e05SGarrett Wollman 
978f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
979d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
980f76fcf6dSJeffrey Hsu 	if (inp == 0) {
981f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
982d0390e05SGarrett Wollman 		return EINVAL;
983f76fcf6dSJeffrey Hsu 	}
984f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
985f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
986f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
987f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
988d0390e05SGarrett Wollman 		return EISCONN;
989f76fcf6dSJeffrey Hsu 	}
99075c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
991812d8653SSam Leffler 	if (jailed(td->td_ucred))
992a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
993b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
9944cc20ab1SSeigo Tanimura 	if (error == 0)
995df8bae1dSRodney W. Grimes 		soisconnected(so);
996f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
997f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
998d0390e05SGarrett Wollman 	return error;
999df8bae1dSRodney W. Grimes }
1000d0390e05SGarrett Wollman 
1001d0390e05SGarrett Wollman static int
1002d0390e05SGarrett Wollman udp_detach(struct socket *so)
1003d0390e05SGarrett Wollman {
1004d0390e05SGarrett Wollman 	struct inpcb *inp;
1005d0390e05SGarrett Wollman 
1006f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1007d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1008f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1009f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1010d0390e05SGarrett Wollman 		return EINVAL;
1011f76fcf6dSJeffrey Hsu 	}
1012f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1013d0390e05SGarrett Wollman 	in_pcbdetach(inp);
1014f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1015d0390e05SGarrett Wollman 	return 0;
1016d0390e05SGarrett Wollman }
1017d0390e05SGarrett Wollman 
1018d0390e05SGarrett Wollman static int
1019d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1020d0390e05SGarrett Wollman {
1021d0390e05SGarrett Wollman 	struct inpcb *inp;
1022d0390e05SGarrett Wollman 
1023f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&udbinfo);
1024d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1025f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1026f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1027d0390e05SGarrett Wollman 		return EINVAL;
1028f76fcf6dSJeffrey Hsu 	}
1029f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1030f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1031f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
1032f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1033d0390e05SGarrett Wollman 		return ENOTCONN;
1034f76fcf6dSJeffrey Hsu 	}
1035d0390e05SGarrett Wollman 
1036df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1037df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1038f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1039f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1040df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1041d0390e05SGarrett Wollman 	return 0;
1042df8bae1dSRodney W. Grimes }
1043df8bae1dSRodney W. Grimes 
1044d0390e05SGarrett Wollman static int
104557bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1046b40ce416SJulian Elischer 	    struct mbuf *control, struct thread *td)
1047d0390e05SGarrett Wollman {
1048d0390e05SGarrett Wollman 	struct inpcb *inp;
1049d0390e05SGarrett Wollman 
1050d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
10515c32ea65SRobert Watson 	return udp_output(inp, m, addr, control, td);
1052d0390e05SGarrett Wollman }
1053d0390e05SGarrett Wollman 
105476429de4SYoshinobu Inoue int
1055d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1056d0390e05SGarrett Wollman {
1057d0390e05SGarrett Wollman 	struct inpcb *inp;
1058d0390e05SGarrett Wollman 
1059f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
1060d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
1061f76fcf6dSJeffrey Hsu 	if (inp == 0) {
1062f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
1063d0390e05SGarrett Wollman 		return EINVAL;
1064f76fcf6dSJeffrey Hsu 	}
1065f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1066f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
1067d0390e05SGarrett Wollman 	socantsendmore(so);
1068f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1069d0390e05SGarrett Wollman 	return 0;
1070d0390e05SGarrett Wollman }
1071d0390e05SGarrett Wollman 
1072f76fcf6dSJeffrey Hsu /*
1073f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
1074f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1075f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and might block.
1076f76fcf6dSJeffrey Hsu  */
1077f76fcf6dSJeffrey Hsu static int
1078f76fcf6dSJeffrey Hsu udp_sockaddr(struct socket *so, struct sockaddr **nam)
1079f76fcf6dSJeffrey Hsu {
1080f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &udbinfo));
1081f76fcf6dSJeffrey Hsu }
1082f76fcf6dSJeffrey Hsu 
1083f76fcf6dSJeffrey Hsu /*
1084f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
1085f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
1086f76fcf6dSJeffrey Hsu  */
1087f76fcf6dSJeffrey Hsu static int
1088f76fcf6dSJeffrey Hsu udp_peeraddr(struct socket *so, struct sockaddr **nam)
1089f76fcf6dSJeffrey Hsu {
1090f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &udbinfo));
1091f76fcf6dSJeffrey Hsu }
1092f76fcf6dSJeffrey Hsu 
1093d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1094756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1095756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1096756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1097756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1098756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1099756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1100756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
1101756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		udp_peeraddr,
1102756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
1103756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
1104756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		udp_sockaddr,
1105756d52a1SPoul-Henning Kamp 	.pru_sosetlabel =	in_pcbsosetlabel
1106d0390e05SGarrett Wollman };
1107