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