xref: /freebsd/sys/netinet/udp_usrreq.c (revision f4f8f02054f3abb6ceb84aefcdecc78d5c8b462f)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.
4  * Copyright (c) 2008 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include "opt_ipfw.h"
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_mac.h"
41 
42 #include <sys/param.h>
43 #include <sys/domain.h>
44 #include <sys/eventhandler.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/protosw.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sx.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/systm.h>
60 #include <sys/vimage.h>
61 
62 #include <vm/uma.h>
63 
64 #include <net/if.h>
65 #include <net/route.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #ifdef INET6
73 #include <netinet/ip6.h>
74 #endif
75 #include <netinet/ip_icmp.h>
76 #include <netinet/icmp_var.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_options.h>
79 #ifdef INET6
80 #include <netinet6/ip6_var.h>
81 #endif
82 #include <netinet/udp.h>
83 #include <netinet/udp_var.h>
84 #include <netinet/vinet.h>
85 
86 #ifdef IPSEC
87 #include <netipsec/ipsec.h>
88 #endif
89 
90 #include <machine/in_cksum.h>
91 
92 #include <security/mac/mac_framework.h>
93 
94 /*
95  * UDP protocol implementation.
96  * Per RFC 768, August, 1980.
97  */
98 
99 #ifdef VIMAGE_GLOBALS
100 int	udp_blackhole;
101 #endif
102 
103 /*
104  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
105  * removes the only data integrity mechanism for packets and malformed
106  * packets that would otherwise be discarded due to bad checksums, and may
107  * cause problems (especially for NFS data blocks).
108  */
109 static int	udp_cksum = 1;
110 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum,
111     0, "compute udp checksum");
112 
113 int	udp_log_in_vain = 0;
114 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
115     &udp_log_in_vain, 0, "Log all incoming UDP packets");
116 
117 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_udp, OID_AUTO, blackhole,
118     CTLFLAG_RW, udp_blackhole, 0,
119     "Do not send port unreachables for refused connects");
120 
121 u_long	udp_sendspace = 9216;		/* really max datagram size */
122 					/* 40 1K datagrams */
123 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
124     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
125 
126 u_long	udp_recvspace = 40 * (1024 +
127 #ifdef INET6
128 				      sizeof(struct sockaddr_in6)
129 #else
130 				      sizeof(struct sockaddr_in)
131 #endif
132 				      );
133 
134 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
135     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
136 
137 #ifdef VIMAGE_GLOBALS
138 struct inpcbhead	udb;		/* from udp_var.h */
139 struct inpcbinfo	udbinfo;
140 struct udpstat		udpstat;	/* from udp_var.h */
141 #endif
142 
143 #ifndef UDBHASHSIZE
144 #define	UDBHASHSIZE	128
145 #endif
146 
147 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_udp, UDPCTL_STATS, stats,
148     CTLFLAG_RW, udpstat, udpstat,
149     "UDP statistics (struct udpstat, netinet/udp_var.h)");
150 
151 static void	udp_detach(struct socket *so);
152 static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
153 		    struct mbuf *, struct thread *);
154 
155 static void
156 udp_zone_change(void *tag)
157 {
158 	INIT_VNET_INET(curvnet);
159 
160 	uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
161 }
162 
163 static int
164 udp_inpcb_init(void *mem, int size, int flags)
165 {
166 	struct inpcb *inp;
167 
168 	inp = mem;
169 	INP_LOCK_INIT(inp, "inp", "udpinp");
170 	return (0);
171 }
172 
173 void
174 udp_init(void)
175 {
176 	INIT_VNET_INET(curvnet);
177 
178 	V_udp_blackhole = 0;
179 
180 	INP_INFO_LOCK_INIT(&V_udbinfo, "udp");
181 	LIST_INIT(&V_udb);
182 #ifdef VIMAGE
183 	V_udbinfo.ipi_vnet = curvnet;
184 #endif
185 	V_udbinfo.ipi_listhead = &V_udb;
186 	V_udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
187 	    &V_udbinfo.ipi_hashmask);
188 	V_udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
189 	    &V_udbinfo.ipi_porthashmask);
190 	V_udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
191 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
192 	uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
193 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
194 	    EVENTHANDLER_PRI_ANY);
195 }
196 
197 /*
198  * Subroutine of udp_input(), which appends the provided mbuf chain to the
199  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
200  * contains the source address.  If the socket ends up being an IPv6 socket,
201  * udp_append() will convert to a sockaddr_in6 before passing the address
202  * into the socket code.
203  */
204 static void
205 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
206     struct sockaddr_in *udp_in)
207 {
208 	struct sockaddr *append_sa;
209 	struct socket *so;
210 	struct mbuf *opts = 0;
211 #ifdef INET6
212 	struct sockaddr_in6 udp_in6;
213 #endif
214 
215 	INP_RLOCK_ASSERT(inp);
216 
217 #ifdef IPSEC
218 	/* Check AH/ESP integrity. */
219 	if (ipsec4_in_reject(n, inp)) {
220 		INIT_VNET_IPSEC(curvnet);
221 		m_freem(n);
222 		V_ipsec4stat.in_polvio++;
223 		return;
224 	}
225 #endif /* IPSEC */
226 #ifdef MAC
227 	if (mac_inpcb_check_deliver(inp, n) != 0) {
228 		m_freem(n);
229 		return;
230 	}
231 #endif
232 	if (inp->inp_flags & INP_CONTROLOPTS ||
233 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
234 #ifdef INET6
235 		if (inp->inp_vflag & INP_IPV6)
236 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
237 		else
238 #endif
239 			ip_savecontrol(inp, &opts, ip, n);
240 	}
241 #ifdef INET6
242 	if (inp->inp_vflag & INP_IPV6) {
243 		bzero(&udp_in6, sizeof(udp_in6));
244 		udp_in6.sin6_len = sizeof(udp_in6);
245 		udp_in6.sin6_family = AF_INET6;
246 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
247 		append_sa = (struct sockaddr *)&udp_in6;
248 	} else
249 #endif
250 		append_sa = (struct sockaddr *)udp_in;
251 	m_adj(n, off);
252 
253 	so = inp->inp_socket;
254 	SOCKBUF_LOCK(&so->so_rcv);
255 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
256 		INIT_VNET_INET(so->so_vnet);
257 		SOCKBUF_UNLOCK(&so->so_rcv);
258 		m_freem(n);
259 		if (opts)
260 			m_freem(opts);
261 		UDPSTAT_INC(udps_fullsock);
262 	} else
263 		sorwakeup_locked(so);
264 }
265 
266 void
267 udp_input(struct mbuf *m, int off)
268 {
269 	INIT_VNET_INET(curvnet);
270 	int iphlen = off;
271 	struct ip *ip;
272 	struct udphdr *uh;
273 	struct ifnet *ifp;
274 	struct inpcb *inp;
275 	int len;
276 	struct ip save_ip;
277 	struct sockaddr_in udp_in;
278 #ifdef IPFIREWALL_FORWARD
279 	struct m_tag *fwd_tag;
280 #endif
281 
282 	ifp = m->m_pkthdr.rcvif;
283 	UDPSTAT_INC(udps_ipackets);
284 
285 	/*
286 	 * Strip IP options, if any; should skip this, make available to
287 	 * user, and use on returned packets, but we don't yet have a way to
288 	 * check the checksum with options still present.
289 	 */
290 	if (iphlen > sizeof (struct ip)) {
291 		ip_stripoptions(m, (struct mbuf *)0);
292 		iphlen = sizeof(struct ip);
293 	}
294 
295 	/*
296 	 * Get IP and UDP header together in first mbuf.
297 	 */
298 	ip = mtod(m, struct ip *);
299 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
300 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
301 			UDPSTAT_INC(udps_hdrops);
302 			return;
303 		}
304 		ip = mtod(m, struct ip *);
305 	}
306 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
307 
308 	/*
309 	 * Destination port of 0 is illegal, based on RFC768.
310 	 */
311 	if (uh->uh_dport == 0)
312 		goto badunlocked;
313 
314 	/*
315 	 * Construct sockaddr format source address.  Stuff source address
316 	 * and datagram in user buffer.
317 	 */
318 	bzero(&udp_in, sizeof(udp_in));
319 	udp_in.sin_len = sizeof(udp_in);
320 	udp_in.sin_family = AF_INET;
321 	udp_in.sin_port = uh->uh_sport;
322 	udp_in.sin_addr = ip->ip_src;
323 
324 	/*
325 	 * Make mbuf data length reflect UDP length.  If not enough data to
326 	 * reflect UDP length, drop.
327 	 */
328 	len = ntohs((u_short)uh->uh_ulen);
329 	if (ip->ip_len != len) {
330 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
331 			UDPSTAT_INC(udps_badlen);
332 			goto badunlocked;
333 		}
334 		m_adj(m, len - ip->ip_len);
335 		/* ip->ip_len = len; */
336 	}
337 
338 	/*
339 	 * Save a copy of the IP header in case we want restore it for
340 	 * sending an ICMP error message in response.
341 	 */
342 	if (!V_udp_blackhole)
343 		save_ip = *ip;
344 	else
345 		memset(&save_ip, 0, sizeof(save_ip));
346 
347 	/*
348 	 * Checksum extended UDP header and data.
349 	 */
350 	if (uh->uh_sum) {
351 		u_short uh_sum;
352 
353 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
354 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
355 				uh_sum = m->m_pkthdr.csum_data;
356 			else
357 				uh_sum = in_pseudo(ip->ip_src.s_addr,
358 				    ip->ip_dst.s_addr, htonl((u_short)len +
359 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
360 			uh_sum ^= 0xffff;
361 		} else {
362 			char b[9];
363 
364 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
365 			bzero(((struct ipovly *)ip)->ih_x1, 9);
366 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
367 			uh_sum = in_cksum(m, len + sizeof (struct ip));
368 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
369 		}
370 		if (uh_sum) {
371 			UDPSTAT_INC(udps_badsum);
372 			m_freem(m);
373 			return;
374 		}
375 	} else
376 		UDPSTAT_INC(udps_nosum);
377 
378 #ifdef IPFIREWALL_FORWARD
379 	/*
380 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
381 	 */
382 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
383 	if (fwd_tag != NULL) {
384 		struct sockaddr_in *next_hop;
385 
386 		/*
387 		 * Do the hack.
388 		 */
389 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
390 		ip->ip_dst = next_hop->sin_addr;
391 		uh->uh_dport = ntohs(next_hop->sin_port);
392 
393 		/*
394 		 * Remove the tag from the packet.  We don't need it anymore.
395 		 */
396 		m_tag_delete(m, fwd_tag);
397 	}
398 #endif
399 
400 	INP_INFO_RLOCK(&V_udbinfo);
401 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
402 	    in_broadcast(ip->ip_dst, ifp)) {
403 		struct inpcb *last;
404 		struct ip_moptions *imo;
405 
406 		last = NULL;
407 		LIST_FOREACH(inp, &V_udb, inp_list) {
408 			if (inp->inp_lport != uh->uh_dport)
409 				continue;
410 #ifdef INET6
411 			if ((inp->inp_vflag & INP_IPV4) == 0)
412 				continue;
413 #endif
414 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
415 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
416 				continue;
417 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
418 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
419 				continue;
420 			if (inp->inp_fport != 0 &&
421 			    inp->inp_fport != uh->uh_sport)
422 				continue;
423 
424 			INP_RLOCK(inp);
425 
426 			/*
427 			 * Handle socket delivery policy for any-source
428 			 * and source-specific multicast. [RFC3678]
429 			 */
430 			imo = inp->inp_moptions;
431 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
432 			    imo != NULL) {
433 				struct sockaddr_in	 group;
434 				int			 blocked;
435 
436 				bzero(&group, sizeof(struct sockaddr_in));
437 				group.sin_len = sizeof(struct sockaddr_in);
438 				group.sin_family = AF_INET;
439 				group.sin_addr = ip->ip_dst;
440 
441 				blocked = imo_multi_filter(imo, ifp,
442 					(struct sockaddr *)&group,
443 					(struct sockaddr *)&udp_in);
444 				if (blocked != MCAST_PASS) {
445 					if (blocked == MCAST_NOTGMEMBER)
446 						IPSTAT_INC(ips_notmember);
447 					if (blocked == MCAST_NOTSMEMBER ||
448 					    blocked == MCAST_MUTED)
449 						UDPSTAT_INC(udps_filtermcast);
450 					INP_RUNLOCK(inp);
451 					continue;
452 				}
453 			}
454 			if (last != NULL) {
455 				struct mbuf *n;
456 
457 				n = m_copy(m, 0, M_COPYALL);
458 				if (last->inp_ppcb == NULL) {
459 					if (n != NULL)
460 						udp_append(last,
461 						    ip, n,
462 						    iphlen +
463 						    sizeof(struct udphdr),
464 						    &udp_in);
465 					INP_RUNLOCK(last);
466 				} else {
467 					/*
468 					 * Engage the tunneling protocol we
469 					 * will have to leave the info_lock
470 					 * up, since we are hunting through
471 					 * multiple UDP's.
472 					 *
473 					 */
474 					udp_tun_func_t tunnel_func;
475 
476 					tunnel_func = (udp_tun_func_t)last->inp_ppcb;
477 					tunnel_func(n, iphlen, last);
478 					INP_RUNLOCK(last);
479 				}
480 			}
481 			last = inp;
482 			/*
483 			 * Don't look for additional matches if this one does
484 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
485 			 * socket options set.  This heuristic avoids
486 			 * searching through all pcbs in the common case of a
487 			 * non-shared port.  It assumes that an application
488 			 * will never clear these options after setting them.
489 			 */
490 			if ((last->inp_socket->so_options &
491 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
492 				break;
493 		}
494 
495 		if (last == NULL) {
496 			/*
497 			 * No matching pcb found; discard datagram.  (No need
498 			 * to send an ICMP Port Unreachable for a broadcast
499 			 * or multicast datgram.)
500 			 */
501 			UDPSTAT_INC(udps_noportbcast);
502 			goto badheadlocked;
503 		}
504 		if (last->inp_ppcb == NULL) {
505 			udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
506 			    &udp_in);
507 			INP_RUNLOCK(last);
508 			INP_INFO_RUNLOCK(&V_udbinfo);
509 		} else {
510 			/*
511 			 * Engage the tunneling protocol.
512 			 */
513 			udp_tun_func_t tunnel_func;
514 
515 			tunnel_func = (udp_tun_func_t)last->inp_ppcb;
516 			tunnel_func(m, iphlen, last);
517 			INP_RUNLOCK(last);
518 			INP_INFO_RUNLOCK(&V_udbinfo);
519 		}
520 		return;
521 	}
522 
523 	/*
524 	 * Locate pcb for datagram.
525 	 */
526 	inp = in_pcblookup_hash(&V_udbinfo, ip->ip_src, uh->uh_sport,
527 	    ip->ip_dst, uh->uh_dport, 1, ifp);
528 	if (inp == NULL) {
529 		if (udp_log_in_vain) {
530 			char buf[4*sizeof "123"];
531 
532 			strcpy(buf, inet_ntoa(ip->ip_dst));
533 			log(LOG_INFO,
534 			    "Connection attempt to UDP %s:%d from %s:%d\n",
535 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
536 			    ntohs(uh->uh_sport));
537 		}
538 		UDPSTAT_INC(udps_noport);
539 		if (m->m_flags & (M_BCAST | M_MCAST)) {
540 			UDPSTAT_INC(udps_noportbcast);
541 			goto badheadlocked;
542 		}
543 		if (V_udp_blackhole)
544 			goto badheadlocked;
545 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
546 			goto badheadlocked;
547 		*ip = save_ip;
548 		ip->ip_len += iphlen;
549 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
550 		INP_INFO_RUNLOCK(&V_udbinfo);
551 		return;
552 	}
553 
554 	/*
555 	 * Check the minimum TTL for socket.
556 	 */
557 	INP_RLOCK(inp);
558 	INP_INFO_RUNLOCK(&V_udbinfo);
559 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
560 		INP_RUNLOCK(inp);
561 		goto badunlocked;
562 	}
563 	if (inp->inp_ppcb != NULL) {
564 		/*
565 		 * Engage the tunneling protocol.
566 		 */
567 		udp_tun_func_t tunnel_func;
568 
569 		tunnel_func = (udp_tun_func_t)inp->inp_ppcb;
570 		tunnel_func(m, iphlen, inp);
571 		INP_RUNLOCK(inp);
572 		return;
573 	}
574 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
575 	INP_RUNLOCK(inp);
576 	return;
577 
578 badheadlocked:
579 	if (inp)
580 		INP_RUNLOCK(inp);
581 	INP_INFO_RUNLOCK(&V_udbinfo);
582 badunlocked:
583 	m_freem(m);
584 }
585 
586 /*
587  * Notify a udp user of an asynchronous error; just wake up so that they can
588  * collect error status.
589  */
590 struct inpcb *
591 udp_notify(struct inpcb *inp, int errno)
592 {
593 
594 	/*
595 	 * While udp_ctlinput() always calls udp_notify() with a read lock
596 	 * when invoking it directly, in_pcbnotifyall() currently uses write
597 	 * locks due to sharing code with TCP.  For now, accept either a read
598 	 * or a write lock, but a read lock is sufficient.
599 	 */
600 	INP_LOCK_ASSERT(inp);
601 
602 	inp->inp_socket->so_error = errno;
603 	sorwakeup(inp->inp_socket);
604 	sowwakeup(inp->inp_socket);
605 	return (inp);
606 }
607 
608 void
609 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
610 {
611 	INIT_VNET_INET(curvnet);
612 	struct ip *ip = vip;
613 	struct udphdr *uh;
614 	struct in_addr faddr;
615 	struct inpcb *inp;
616 
617 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
618 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
619 		return;
620 
621 	/*
622 	 * Redirects don't need to be handled up here.
623 	 */
624 	if (PRC_IS_REDIRECT(cmd))
625 		return;
626 
627 	/*
628 	 * Hostdead is ugly because it goes linearly through all PCBs.
629 	 *
630 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
631 	 * DoS attack on machines with many connections.
632 	 */
633 	if (cmd == PRC_HOSTDEAD)
634 		ip = NULL;
635 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
636 		return;
637 	if (ip != NULL) {
638 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
639 		INP_INFO_RLOCK(&V_udbinfo);
640 		inp = in_pcblookup_hash(&V_udbinfo, faddr, uh->uh_dport,
641 		    ip->ip_src, uh->uh_sport, 0, NULL);
642 		if (inp != NULL) {
643 			INP_RLOCK(inp);
644 			if (inp->inp_socket != NULL) {
645 				udp_notify(inp, inetctlerrmap[cmd]);
646 			}
647 			INP_RUNLOCK(inp);
648 		}
649 		INP_INFO_RUNLOCK(&V_udbinfo);
650 	} else
651 		in_pcbnotifyall(&V_udbinfo, faddr, inetctlerrmap[cmd],
652 		    udp_notify);
653 }
654 
655 static int
656 udp_pcblist(SYSCTL_HANDLER_ARGS)
657 {
658 	INIT_VNET_INET(curvnet);
659 	int error, i, n;
660 	struct inpcb *inp, **inp_list;
661 	inp_gen_t gencnt;
662 	struct xinpgen xig;
663 
664 	/*
665 	 * The process of preparing the PCB list is too time-consuming and
666 	 * resource-intensive to repeat twice on every request.
667 	 */
668 	if (req->oldptr == 0) {
669 		n = V_udbinfo.ipi_count;
670 		req->oldidx = 2 * (sizeof xig)
671 			+ (n + n/8) * sizeof(struct xinpcb);
672 		return (0);
673 	}
674 
675 	if (req->newptr != 0)
676 		return (EPERM);
677 
678 	/*
679 	 * OK, now we're committed to doing something.
680 	 */
681 	INP_INFO_RLOCK(&V_udbinfo);
682 	gencnt = V_udbinfo.ipi_gencnt;
683 	n = V_udbinfo.ipi_count;
684 	INP_INFO_RUNLOCK(&V_udbinfo);
685 
686 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
687 		+ n * sizeof(struct xinpcb));
688 	if (error != 0)
689 		return (error);
690 
691 	xig.xig_len = sizeof xig;
692 	xig.xig_count = n;
693 	xig.xig_gen = gencnt;
694 	xig.xig_sogen = so_gencnt;
695 	error = SYSCTL_OUT(req, &xig, sizeof xig);
696 	if (error)
697 		return (error);
698 
699 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
700 	if (inp_list == 0)
701 		return (ENOMEM);
702 
703 	INP_INFO_RLOCK(&V_udbinfo);
704 	for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
705 	     inp = LIST_NEXT(inp, inp_list)) {
706 		INP_RLOCK(inp);
707 		if (inp->inp_gencnt <= gencnt &&
708 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0)
709 			inp_list[i++] = inp;
710 		INP_RUNLOCK(inp);
711 	}
712 	INP_INFO_RUNLOCK(&V_udbinfo);
713 	n = i;
714 
715 	error = 0;
716 	for (i = 0; i < n; i++) {
717 		inp = inp_list[i];
718 		INP_RLOCK(inp);
719 		if (inp->inp_gencnt <= gencnt) {
720 			struct xinpcb xi;
721 			bzero(&xi, sizeof(xi));
722 			xi.xi_len = sizeof xi;
723 			/* XXX should avoid extra copy */
724 			bcopy(inp, &xi.xi_inp, sizeof *inp);
725 			if (inp->inp_socket)
726 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
727 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
728 			INP_RUNLOCK(inp);
729 			error = SYSCTL_OUT(req, &xi, sizeof xi);
730 		} else
731 			INP_RUNLOCK(inp);
732 	}
733 	if (!error) {
734 		/*
735 		 * Give the user an updated idea of our state.  If the
736 		 * generation differs from what we told her before, she knows
737 		 * that something happened while we were processing this
738 		 * request, and it might be necessary to retry.
739 		 */
740 		INP_INFO_RLOCK(&V_udbinfo);
741 		xig.xig_gen = V_udbinfo.ipi_gencnt;
742 		xig.xig_sogen = so_gencnt;
743 		xig.xig_count = V_udbinfo.ipi_count;
744 		INP_INFO_RUNLOCK(&V_udbinfo);
745 		error = SYSCTL_OUT(req, &xig, sizeof xig);
746 	}
747 	free(inp_list, M_TEMP);
748 	return (error);
749 }
750 
751 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
752     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
753 
754 static int
755 udp_getcred(SYSCTL_HANDLER_ARGS)
756 {
757 	INIT_VNET_INET(curvnet);
758 	struct xucred xuc;
759 	struct sockaddr_in addrs[2];
760 	struct inpcb *inp;
761 	int error;
762 
763 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
764 	if (error)
765 		return (error);
766 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
767 	if (error)
768 		return (error);
769 	INP_INFO_RLOCK(&V_udbinfo);
770 	inp = in_pcblookup_hash(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
771 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
772 	if (inp != NULL) {
773 		INP_RLOCK(inp);
774 		INP_INFO_RUNLOCK(&V_udbinfo);
775 		if (inp->inp_socket == NULL)
776 			error = ENOENT;
777 		if (error == 0)
778 			error = cr_canseeinpcb(req->td->td_ucred, inp);
779 		if (error == 0)
780 			cru2x(inp->inp_cred, &xuc);
781 		INP_RUNLOCK(inp);
782 	} else {
783 		INP_INFO_RUNLOCK(&V_udbinfo);
784 		error = ENOENT;
785 	}
786 	if (error == 0)
787 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
788 	return (error);
789 }
790 
791 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
792     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
793     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
794 
795 static int
796 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
797     struct mbuf *control, struct thread *td)
798 {
799 	INIT_VNET_INET(inp->inp_vnet);
800 	struct udpiphdr *ui;
801 	int len = m->m_pkthdr.len;
802 	struct in_addr faddr, laddr;
803 	struct cmsghdr *cm;
804 	struct sockaddr_in *sin, src;
805 	int error = 0;
806 	int ipflags;
807 	u_short fport, lport;
808 	int unlock_udbinfo;
809 
810 	/*
811 	 * udp_output() may need to temporarily bind or connect the current
812 	 * inpcb.  As such, we don't know up front whether we will need the
813 	 * pcbinfo lock or not.  Do any work to decide what is needed up
814 	 * front before acquiring any locks.
815 	 */
816 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
817 		if (control)
818 			m_freem(control);
819 		m_freem(m);
820 		return (EMSGSIZE);
821 	}
822 
823 	src.sin_family = 0;
824 	if (control != NULL) {
825 		/*
826 		 * XXX: Currently, we assume all the optional information is
827 		 * stored in a single mbuf.
828 		 */
829 		if (control->m_next) {
830 			m_freem(control);
831 			m_freem(m);
832 			return (EINVAL);
833 		}
834 		for (; control->m_len > 0;
835 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
836 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
837 			cm = mtod(control, struct cmsghdr *);
838 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
839 			    || cm->cmsg_len > control->m_len) {
840 				error = EINVAL;
841 				break;
842 			}
843 			if (cm->cmsg_level != IPPROTO_IP)
844 				continue;
845 
846 			switch (cm->cmsg_type) {
847 			case IP_SENDSRCADDR:
848 				if (cm->cmsg_len !=
849 				    CMSG_LEN(sizeof(struct in_addr))) {
850 					error = EINVAL;
851 					break;
852 				}
853 				bzero(&src, sizeof(src));
854 				src.sin_family = AF_INET;
855 				src.sin_len = sizeof(src);
856 				src.sin_port = inp->inp_lport;
857 				src.sin_addr =
858 				    *(struct in_addr *)CMSG_DATA(cm);
859 				break;
860 
861 			default:
862 				error = ENOPROTOOPT;
863 				break;
864 			}
865 			if (error)
866 				break;
867 		}
868 		m_freem(control);
869 	}
870 	if (error) {
871 		m_freem(m);
872 		return (error);
873 	}
874 
875 	/*
876 	 * Depending on whether or not the application has bound or connected
877 	 * the socket, we may have to do varying levels of work.  The optimal
878 	 * case is for a connected UDP socket, as a global lock isn't
879 	 * required at all.
880 	 *
881 	 * In order to decide which we need, we require stability of the
882 	 * inpcb binding, which we ensure by acquiring a read lock on the
883 	 * inpcb.  This doesn't strictly follow the lock order, so we play
884 	 * the trylock and retry game; note that we may end up with more
885 	 * conservative locks than required the second time around, so later
886 	 * assertions have to accept that.  Further analysis of the number of
887 	 * misses under contention is required.
888 	 */
889 	sin = (struct sockaddr_in *)addr;
890 	INP_RLOCK(inp);
891 	if (sin != NULL &&
892 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
893 		INP_RUNLOCK(inp);
894 		INP_INFO_WLOCK(&V_udbinfo);
895 		INP_WLOCK(inp);
896 		unlock_udbinfo = 2;
897 	} else if ((sin != NULL && (
898 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
899 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
900 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
901 	    (inp->inp_lport == 0))) ||
902 	    (src.sin_family == AF_INET)) {
903 		if (!INP_INFO_TRY_RLOCK(&V_udbinfo)) {
904 			INP_RUNLOCK(inp);
905 			INP_INFO_RLOCK(&V_udbinfo);
906 			INP_RLOCK(inp);
907 		}
908 		unlock_udbinfo = 1;
909 	} else
910 		unlock_udbinfo = 0;
911 
912 	/*
913 	 * If the IP_SENDSRCADDR control message was specified, override the
914 	 * source address for this datagram.  Its use is invalidated if the
915 	 * address thus specified is incomplete or clobbers other inpcbs.
916 	 */
917 	laddr = inp->inp_laddr;
918 	lport = inp->inp_lport;
919 	if (src.sin_family == AF_INET) {
920 		INP_INFO_LOCK_ASSERT(&V_udbinfo);
921 		if ((lport == 0) ||
922 		    (laddr.s_addr == INADDR_ANY &&
923 		     src.sin_addr.s_addr == INADDR_ANY)) {
924 			error = EINVAL;
925 			goto release;
926 		}
927 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
928 		    &laddr.s_addr, &lport, td->td_ucred);
929 		if (error)
930 			goto release;
931 	}
932 
933 	/*
934 	 * If a UDP socket has been connected, then a local address/port will
935 	 * have been selected and bound.
936 	 *
937 	 * If a UDP socket has not been connected to, then an explicit
938 	 * destination address must be used, in which case a local
939 	 * address/port may not have been selected and bound.
940 	 */
941 	if (sin != NULL) {
942 		INP_LOCK_ASSERT(inp);
943 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
944 			error = EISCONN;
945 			goto release;
946 		}
947 
948 		/*
949 		 * Jail may rewrite the destination address, so let it do
950 		 * that before we use it.
951 		 */
952 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
953 		if (error)
954 			goto release;
955 
956 		/*
957 		 * If a local address or port hasn't yet been selected, or if
958 		 * the destination address needs to be rewritten due to using
959 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
960 		 * to do the heavy lifting.  Once a port is selected, we
961 		 * commit the binding back to the socket; we also commit the
962 		 * binding of the address if in jail.
963 		 *
964 		 * If we already have a valid binding and we're not
965 		 * requesting a destination address rewrite, use a fast path.
966 		 */
967 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
968 		    inp->inp_lport == 0 ||
969 		    sin->sin_addr.s_addr == INADDR_ANY ||
970 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
971 			INP_INFO_LOCK_ASSERT(&V_udbinfo);
972 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
973 			    &lport, &faddr.s_addr, &fport, NULL,
974 			    td->td_ucred);
975 			if (error)
976 				goto release;
977 
978 			/*
979 			 * XXXRW: Why not commit the port if the address is
980 			 * !INADDR_ANY?
981 			 */
982 			/* Commit the local port if newly assigned. */
983 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
984 			    inp->inp_lport == 0) {
985 				INP_INFO_WLOCK_ASSERT(&V_udbinfo);
986 				INP_WLOCK_ASSERT(inp);
987 				/*
988 				 * Remember addr if jailed, to prevent
989 				 * rebinding.
990 				 */
991 				if (jailed(td->td_ucred))
992 					inp->inp_laddr = laddr;
993 				inp->inp_lport = lport;
994 				if (in_pcbinshash(inp) != 0) {
995 					inp->inp_lport = 0;
996 					error = EAGAIN;
997 					goto release;
998 				}
999 				inp->inp_flags |= INP_ANONPORT;
1000 			}
1001 		} else {
1002 			faddr = sin->sin_addr;
1003 			fport = sin->sin_port;
1004 		}
1005 	} else {
1006 		INP_LOCK_ASSERT(inp);
1007 		faddr = inp->inp_faddr;
1008 		fport = inp->inp_fport;
1009 		if (faddr.s_addr == INADDR_ANY) {
1010 			error = ENOTCONN;
1011 			goto release;
1012 		}
1013 	}
1014 
1015 	/*
1016 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1017 	 * link-layer headers.  Immediate slide the data pointer back forward
1018 	 * since we won't use that space at this layer.
1019 	 */
1020 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
1021 	if (m == NULL) {
1022 		error = ENOBUFS;
1023 		goto release;
1024 	}
1025 	m->m_data += max_linkhdr;
1026 	m->m_len -= max_linkhdr;
1027 	m->m_pkthdr.len -= max_linkhdr;
1028 
1029 	/*
1030 	 * Fill in mbuf with extended UDP header and addresses and length put
1031 	 * into network format.
1032 	 */
1033 	ui = mtod(m, struct udpiphdr *);
1034 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1035 	ui->ui_pr = IPPROTO_UDP;
1036 	ui->ui_src = laddr;
1037 	ui->ui_dst = faddr;
1038 	ui->ui_sport = lport;
1039 	ui->ui_dport = fport;
1040 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1041 
1042 	/*
1043 	 * Set the Don't Fragment bit in the IP header.
1044 	 */
1045 	if (inp->inp_flags & INP_DONTFRAG) {
1046 		struct ip *ip;
1047 
1048 		ip = (struct ip *)&ui->ui_i;
1049 		ip->ip_off |= IP_DF;
1050 	}
1051 
1052 	ipflags = 0;
1053 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1054 		ipflags |= IP_ROUTETOIF;
1055 	if (inp->inp_socket->so_options & SO_BROADCAST)
1056 		ipflags |= IP_ALLOWBROADCAST;
1057 	if (inp->inp_flags & INP_ONESBCAST)
1058 		ipflags |= IP_SENDONES;
1059 
1060 #ifdef MAC
1061 	mac_inpcb_create_mbuf(inp, m);
1062 #endif
1063 
1064 	/*
1065 	 * Set up checksum and output datagram.
1066 	 */
1067 	if (udp_cksum) {
1068 		if (inp->inp_flags & INP_ONESBCAST)
1069 			faddr.s_addr = INADDR_BROADCAST;
1070 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1071 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1072 		m->m_pkthdr.csum_flags = CSUM_UDP;
1073 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1074 	} else
1075 		ui->ui_sum = 0;
1076 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1077 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1078 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
1079 	UDPSTAT_INC(udps_opackets);
1080 
1081 	if (unlock_udbinfo == 2)
1082 		INP_INFO_WUNLOCK(&V_udbinfo);
1083 	else if (unlock_udbinfo == 1)
1084 		INP_INFO_RUNLOCK(&V_udbinfo);
1085 	error = ip_output(m, inp->inp_options, NULL, ipflags,
1086 	    inp->inp_moptions, inp);
1087 	if (unlock_udbinfo == 2)
1088 		INP_WUNLOCK(inp);
1089 	else
1090 		INP_RUNLOCK(inp);
1091 	return (error);
1092 
1093 release:
1094 	if (unlock_udbinfo == 2) {
1095 		INP_WUNLOCK(inp);
1096 		INP_INFO_WUNLOCK(&V_udbinfo);
1097 	} else if (unlock_udbinfo == 1) {
1098 		INP_RUNLOCK(inp);
1099 		INP_INFO_RUNLOCK(&V_udbinfo);
1100 	} else
1101 		INP_RUNLOCK(inp);
1102 	m_freem(m);
1103 	return (error);
1104 }
1105 
1106 static void
1107 udp_abort(struct socket *so)
1108 {
1109 	INIT_VNET_INET(so->so_vnet);
1110 	struct inpcb *inp;
1111 
1112 	inp = sotoinpcb(so);
1113 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1114 	INP_INFO_WLOCK(&V_udbinfo);
1115 	INP_WLOCK(inp);
1116 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1117 		in_pcbdisconnect(inp);
1118 		inp->inp_laddr.s_addr = INADDR_ANY;
1119 		soisdisconnected(so);
1120 	}
1121 	INP_WUNLOCK(inp);
1122 	INP_INFO_WUNLOCK(&V_udbinfo);
1123 }
1124 
1125 static int
1126 udp_attach(struct socket *so, int proto, struct thread *td)
1127 {
1128 	INIT_VNET_INET(so->so_vnet);
1129 	struct inpcb *inp;
1130 	int error;
1131 
1132 	inp = sotoinpcb(so);
1133 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1134 	error = soreserve(so, udp_sendspace, udp_recvspace);
1135 	if (error)
1136 		return (error);
1137 	INP_INFO_WLOCK(&V_udbinfo);
1138 	error = in_pcballoc(so, &V_udbinfo);
1139 	if (error) {
1140 		INP_INFO_WUNLOCK(&V_udbinfo);
1141 		return (error);
1142 	}
1143 
1144 	inp = (struct inpcb *)so->so_pcb;
1145 	INP_INFO_WUNLOCK(&V_udbinfo);
1146 	inp->inp_vflag |= INP_IPV4;
1147 	inp->inp_ip_ttl = V_ip_defttl;
1148 	/*
1149 	 * UDP does not have a per-protocol pcb (inp->inp_ppcb).
1150 	 * We use this pointer for kernel tunneling pointer.
1151 	 * If we ever need to have a protocol block we will
1152 	 * need to move this function pointer there. Null
1153 	 * in this pointer means "do the normal thing".
1154 	 */
1155 	inp->inp_ppcb = NULL;
1156 	INP_WUNLOCK(inp);
1157 	return (0);
1158 }
1159 
1160 int
1161 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f)
1162 {
1163 	struct inpcb *inp;
1164 
1165 	inp = (struct inpcb *)so->so_pcb;
1166 	KASSERT(so->so_type == SOCK_DGRAM, ("udp_set_kernel_tunneling: !dgram"));
1167 	KASSERT(so->so_pcb != NULL, ("udp_set_kernel_tunneling: NULL inp"));
1168 	if (so->so_type != SOCK_DGRAM) {
1169 		/* Not UDP socket... sorry! */
1170 		return (ENOTSUP);
1171 	}
1172 	if (inp == NULL) {
1173 		/* NULL INP? */
1174 		return (EINVAL);
1175 	}
1176 	INP_WLOCK(inp);
1177 	if (inp->inp_ppcb != NULL) {
1178 		INP_WUNLOCK(inp);
1179 		return (EBUSY);
1180 	}
1181 	inp->inp_ppcb = f;
1182 	INP_WUNLOCK(inp);
1183 	return (0);
1184 }
1185 
1186 static int
1187 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1188 {
1189 	INIT_VNET_INET(so->so_vnet);
1190 	struct inpcb *inp;
1191 	int error;
1192 
1193 	inp = sotoinpcb(so);
1194 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1195 	INP_INFO_WLOCK(&V_udbinfo);
1196 	INP_WLOCK(inp);
1197 	error = in_pcbbind(inp, nam, td->td_ucred);
1198 	INP_WUNLOCK(inp);
1199 	INP_INFO_WUNLOCK(&V_udbinfo);
1200 	return (error);
1201 }
1202 
1203 static void
1204 udp_close(struct socket *so)
1205 {
1206 	INIT_VNET_INET(so->so_vnet);
1207 	struct inpcb *inp;
1208 
1209 	inp = sotoinpcb(so);
1210 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1211 	INP_INFO_WLOCK(&V_udbinfo);
1212 	INP_WLOCK(inp);
1213 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1214 		in_pcbdisconnect(inp);
1215 		inp->inp_laddr.s_addr = INADDR_ANY;
1216 		soisdisconnected(so);
1217 	}
1218 	INP_WUNLOCK(inp);
1219 	INP_INFO_WUNLOCK(&V_udbinfo);
1220 }
1221 
1222 static int
1223 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1224 {
1225 	INIT_VNET_INET(so->so_vnet);
1226 	struct inpcb *inp;
1227 	int error;
1228 	struct sockaddr_in *sin;
1229 
1230 	inp = sotoinpcb(so);
1231 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1232 	INP_INFO_WLOCK(&V_udbinfo);
1233 	INP_WLOCK(inp);
1234 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1235 		INP_WUNLOCK(inp);
1236 		INP_INFO_WUNLOCK(&V_udbinfo);
1237 		return (EISCONN);
1238 	}
1239 	sin = (struct sockaddr_in *)nam;
1240 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1241 	if (error != 0) {
1242 		INP_WUNLOCK(inp);
1243 		INP_INFO_WUNLOCK(&V_udbinfo);
1244 		return (error);
1245 	}
1246 	error = in_pcbconnect(inp, nam, td->td_ucred);
1247 	if (error == 0)
1248 		soisconnected(so);
1249 	INP_WUNLOCK(inp);
1250 	INP_INFO_WUNLOCK(&V_udbinfo);
1251 	return (error);
1252 }
1253 
1254 static void
1255 udp_detach(struct socket *so)
1256 {
1257 	INIT_VNET_INET(so->so_vnet);
1258 	struct inpcb *inp;
1259 
1260 	inp = sotoinpcb(so);
1261 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1262 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1263 	    ("udp_detach: not disconnected"));
1264 	INP_INFO_WLOCK(&V_udbinfo);
1265 	INP_WLOCK(inp);
1266 	in_pcbdetach(inp);
1267 	in_pcbfree(inp);
1268 	INP_INFO_WUNLOCK(&V_udbinfo);
1269 }
1270 
1271 static int
1272 udp_disconnect(struct socket *so)
1273 {
1274 	INIT_VNET_INET(so->so_vnet);
1275 	struct inpcb *inp;
1276 
1277 	inp = sotoinpcb(so);
1278 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1279 	INP_INFO_WLOCK(&V_udbinfo);
1280 	INP_WLOCK(inp);
1281 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1282 		INP_WUNLOCK(inp);
1283 		INP_INFO_WUNLOCK(&V_udbinfo);
1284 		return (ENOTCONN);
1285 	}
1286 
1287 	in_pcbdisconnect(inp);
1288 	inp->inp_laddr.s_addr = INADDR_ANY;
1289 	SOCK_LOCK(so);
1290 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1291 	SOCK_UNLOCK(so);
1292 	INP_WUNLOCK(inp);
1293 	INP_INFO_WUNLOCK(&V_udbinfo);
1294 	return (0);
1295 }
1296 
1297 static int
1298 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1299     struct mbuf *control, struct thread *td)
1300 {
1301 	struct inpcb *inp;
1302 
1303 	inp = sotoinpcb(so);
1304 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1305 	return (udp_output(inp, m, addr, control, td));
1306 }
1307 
1308 int
1309 udp_shutdown(struct socket *so)
1310 {
1311 	struct inpcb *inp;
1312 
1313 	inp = sotoinpcb(so);
1314 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1315 	INP_WLOCK(inp);
1316 	socantsendmore(so);
1317 	INP_WUNLOCK(inp);
1318 	return (0);
1319 }
1320 
1321 struct pr_usrreqs udp_usrreqs = {
1322 	.pru_abort =		udp_abort,
1323 	.pru_attach =		udp_attach,
1324 	.pru_bind =		udp_bind,
1325 	.pru_connect =		udp_connect,
1326 	.pru_control =		in_control,
1327 	.pru_detach =		udp_detach,
1328 	.pru_disconnect =	udp_disconnect,
1329 	.pru_peeraddr =		in_getpeeraddr,
1330 	.pru_send =		udp_send,
1331 	.pru_soreceive =	soreceive_dgram,
1332 	.pru_sosend =		sosend_dgram,
1333 	.pru_shutdown =		udp_shutdown,
1334 	.pru_sockaddr =		in_getsockaddr,
1335 	.pru_sosetlabel =	in_pcbsosetlabel,
1336 	.pru_close =		udp_close,
1337 };
1338