xref: /freebsd/sys/netinet/udp_usrreq.c (revision 9fc7a59f2a9baa13d62a86a10d97652ca06caa5f)
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  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * Copyright (c) 2014 Kevin Lo
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Robert N. M. Watson under
10  * contract to Juniper Networks, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_ipsec.h"
45 #include "opt_rss.h"
46 
47 #include <sys/param.h>
48 #include <sys/domain.h>
49 #include <sys/eventhandler.h>
50 #include <sys/jail.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/priv.h>
56 #include <sys/proc.h>
57 #include <sys/protosw.h>
58 #include <sys/sdt.h>
59 #include <sys/signalvar.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sx.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65 #include <sys/systm.h>
66 
67 #include <vm/uma.h>
68 
69 #include <net/if.h>
70 #include <net/if_var.h>
71 #include <net/route.h>
72 #include <net/rss_config.h>
73 
74 #include <netinet/in.h>
75 #include <netinet/in_kdtrace.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip.h>
80 #ifdef INET6
81 #include <netinet/ip6.h>
82 #endif
83 #include <netinet/ip_icmp.h>
84 #include <netinet/icmp_var.h>
85 #include <netinet/ip_var.h>
86 #include <netinet/ip_options.h>
87 #ifdef INET6
88 #include <netinet6/ip6_var.h>
89 #endif
90 #include <netinet/udp.h>
91 #include <netinet/udp_var.h>
92 #include <netinet/udplite.h>
93 #include <netinet/in_rss.h>
94 
95 #include <netipsec/ipsec_support.h>
96 
97 #include <machine/in_cksum.h>
98 
99 #include <security/mac/mac_framework.h>
100 
101 /*
102  * UDP and UDP-Lite protocols implementation.
103  * Per RFC 768, August, 1980.
104  * Per RFC 3828, July, 2004.
105  */
106 
107 /*
108  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
109  * removes the only data integrity mechanism for packets and malformed
110  * packets that would otherwise be discarded due to bad checksums, and may
111  * cause problems (especially for NFS data blocks).
112  */
113 VNET_DEFINE(int, udp_cksum) = 1;
114 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
115     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
116 
117 int	udp_log_in_vain = 0;
118 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
119     &udp_log_in_vain, 0, "Log all incoming UDP packets");
120 
121 VNET_DEFINE(int, udp_blackhole) = 0;
122 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
123     &VNET_NAME(udp_blackhole), 0,
124     "Do not send port unreachables for refused connects");
125 
126 static VNET_DEFINE(int, udp_require_l2_bcast) = 0;
127 #define	V_udp_require_l2_bcast		VNET(udp_require_l2_bcast)
128 SYSCTL_INT(_net_inet_udp, OID_AUTO, require_l2_bcast, CTLFLAG_VNET | CTLFLAG_RW,
129     &VNET_NAME(udp_require_l2_bcast), 0,
130     "Only treat packets sent to an L2 broadcast address as broadcast packets");
131 
132 u_long	udp_sendspace = 9216;		/* really max datagram size */
133 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
134     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
135 
136 u_long	udp_recvspace = 40 * (1024 +
137 #ifdef INET6
138 				      sizeof(struct sockaddr_in6)
139 #else
140 				      sizeof(struct sockaddr_in)
141 #endif
142 				      );	/* 40 1K datagrams */
143 
144 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
145     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
146 
147 VNET_DEFINE(struct inpcbhead, udb);		/* from udp_var.h */
148 VNET_DEFINE(struct inpcbinfo, udbinfo);
149 VNET_DEFINE(struct inpcbhead, ulitecb);
150 VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
151 static VNET_DEFINE(uma_zone_t, udpcb_zone);
152 #define	V_udpcb_zone			VNET(udpcb_zone)
153 
154 #ifndef UDBHASHSIZE
155 #define	UDBHASHSIZE	128
156 #endif
157 
158 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);		/* from udp_var.h */
159 VNET_PCPUSTAT_SYSINIT(udpstat);
160 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
161     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
162 
163 #ifdef VIMAGE
164 VNET_PCPUSTAT_SYSUNINIT(udpstat);
165 #endif /* VIMAGE */
166 #ifdef INET
167 static void	udp_detach(struct socket *so);
168 static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
169 		    struct mbuf *, struct thread *);
170 #endif
171 
172 static void
173 udp_zone_change(void *tag)
174 {
175 
176 	uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
177 	uma_zone_set_max(V_udpcb_zone, maxsockets);
178 }
179 
180 static int
181 udp_inpcb_init(void *mem, int size, int flags)
182 {
183 	struct inpcb *inp;
184 
185 	inp = mem;
186 	INP_LOCK_INIT(inp, "inp", "udpinp");
187 	return (0);
188 }
189 
190 static int
191 udplite_inpcb_init(void *mem, int size, int flags)
192 {
193 	struct inpcb *inp;
194 
195 	inp = mem;
196 	INP_LOCK_INIT(inp, "inp", "udpliteinp");
197 	return (0);
198 }
199 
200 void
201 udp_init(void)
202 {
203 
204 	/*
205 	 * For now default to 2-tuple UDP hashing - until the fragment
206 	 * reassembly code can also update the flowid.
207 	 *
208 	 * Once we can calculate the flowid that way and re-establish
209 	 * a 4-tuple, flip this to 4-tuple.
210 	 */
211 	in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
212 	    "udp_inpcb", udp_inpcb_init, NULL, 0,
213 	    IPI_HASHFIELDS_2TUPLE);
214 	V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
215 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
216 	uma_zone_set_max(V_udpcb_zone, maxsockets);
217 	uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached");
218 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
219 	    EVENTHANDLER_PRI_ANY);
220 }
221 
222 void
223 udplite_init(void)
224 {
225 
226 	in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE,
227 	    UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, NULL,
228 	    0, IPI_HASHFIELDS_2TUPLE);
229 }
230 
231 /*
232  * Kernel module interface for updating udpstat.  The argument is an index
233  * into udpstat treated as an array of u_long.  While this encodes the
234  * general layout of udpstat into the caller, it doesn't encode its location,
235  * so that future changes to add, for example, per-CPU stats support won't
236  * cause binary compatibility problems for kernel modules.
237  */
238 void
239 kmod_udpstat_inc(int statnum)
240 {
241 
242 	counter_u64_add(VNET(udpstat)[statnum], 1);
243 }
244 
245 int
246 udp_newudpcb(struct inpcb *inp)
247 {
248 	struct udpcb *up;
249 
250 	up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
251 	if (up == NULL)
252 		return (ENOBUFS);
253 	inp->inp_ppcb = up;
254 	return (0);
255 }
256 
257 void
258 udp_discardcb(struct udpcb *up)
259 {
260 
261 	uma_zfree(V_udpcb_zone, up);
262 }
263 
264 #ifdef VIMAGE
265 static void
266 udp_destroy(void *unused __unused)
267 {
268 
269 	in_pcbinfo_destroy(&V_udbinfo);
270 	uma_zdestroy(V_udpcb_zone);
271 }
272 VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL);
273 
274 static void
275 udplite_destroy(void *unused __unused)
276 {
277 
278 	in_pcbinfo_destroy(&V_ulitecbinfo);
279 }
280 VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy,
281     NULL);
282 #endif
283 
284 #ifdef INET
285 /*
286  * Subroutine of udp_input(), which appends the provided mbuf chain to the
287  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
288  * contains the source address.  If the socket ends up being an IPv6 socket,
289  * udp_append() will convert to a sockaddr_in6 before passing the address
290  * into the socket code.
291  *
292  * In the normal case udp_append() will return 0, indicating that you
293  * must unlock the inp. However if a tunneling protocol is in place we increment
294  * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
295  * then decrement the reference count. If the inp_rele returns 1, indicating the
296  * inp is gone, we return that to the caller to tell them *not* to unlock
297  * the inp. In the case of multi-cast this will cause the distribution
298  * to stop (though most tunneling protocols known currently do *not* use
299  * multicast).
300  */
301 static int
302 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
303     struct sockaddr_in *udp_in)
304 {
305 	struct sockaddr *append_sa;
306 	struct socket *so;
307 	struct mbuf *tmpopts, *opts = NULL;
308 #ifdef INET6
309 	struct sockaddr_in6 udp_in6;
310 #endif
311 	struct udpcb *up;
312 
313 	INP_LOCK_ASSERT(inp);
314 
315 	/*
316 	 * Engage the tunneling protocol.
317 	 */
318 	up = intoudpcb(inp);
319 	if (up->u_tun_func != NULL) {
320 		in_pcbref(inp);
321 		INP_RUNLOCK(inp);
322 		(*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0],
323 		    up->u_tun_ctx);
324 		INP_RLOCK(inp);
325 		return (in_pcbrele_rlocked(inp));
326 	}
327 
328 	off += sizeof(struct udphdr);
329 
330 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
331 	/* Check AH/ESP integrity. */
332 	if (IPSEC_ENABLED(ipv4) &&
333 	    IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) {
334 		m_freem(n);
335 		return (0);
336 	}
337 	if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */
338 		if (IPSEC_ENABLED(ipv4) &&
339 		    UDPENCAP_INPUT(n, off, AF_INET) != 0)
340 			return (0);	/* Consumed. */
341 	}
342 #endif /* IPSEC */
343 #ifdef MAC
344 	if (mac_inpcb_check_deliver(inp, n) != 0) {
345 		m_freem(n);
346 		return (0);
347 	}
348 #endif /* MAC */
349 	if (inp->inp_flags & INP_CONTROLOPTS ||
350 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
351 #ifdef INET6
352 		if (inp->inp_vflag & INP_IPV6)
353 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
354 		else
355 #endif /* INET6 */
356 			ip_savecontrol(inp, &opts, ip, n);
357 	}
358 	if (inp->inp_vflag & INP_IPV4 && inp->inp_flags2 & INP_ORIGDSTADDR) {
359 		tmpopts = sbcreatecontrol((caddr_t)&udp_in[1],
360 			sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP);
361 		if (tmpopts) {
362 			if (opts) {
363 				tmpopts->m_next = opts;
364 				opts = tmpopts;
365 			} else
366 				opts = tmpopts;
367 		}
368 	}
369 #ifdef INET6
370 	if (inp->inp_vflag & INP_IPV6) {
371 		bzero(&udp_in6, sizeof(udp_in6));
372 		udp_in6.sin6_len = sizeof(udp_in6);
373 		udp_in6.sin6_family = AF_INET6;
374 		in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6);
375 		append_sa = (struct sockaddr *)&udp_in6;
376 	} else
377 #endif /* INET6 */
378 		append_sa = (struct sockaddr *)&udp_in[0];
379 	m_adj(n, off);
380 
381 	so = inp->inp_socket;
382 	SOCKBUF_LOCK(&so->so_rcv);
383 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
384 		SOCKBUF_UNLOCK(&so->so_rcv);
385 		m_freem(n);
386 		if (opts)
387 			m_freem(opts);
388 		UDPSTAT_INC(udps_fullsock);
389 	} else
390 		sorwakeup_locked(so);
391 	return (0);
392 }
393 
394 int
395 udp_input(struct mbuf **mp, int *offp, int proto)
396 {
397 	struct ip *ip;
398 	struct udphdr *uh;
399 	struct ifnet *ifp;
400 	struct inpcb *inp;
401 	uint16_t len, ip_len;
402 	struct inpcbinfo *pcbinfo;
403 	struct ip save_ip;
404 	struct sockaddr_in udpin[2];
405 	struct mbuf *m;
406 	struct m_tag *fwd_tag;
407 	int cscov_partial, iphlen;
408 
409 	m = *mp;
410 	iphlen = *offp;
411 	ifp = m->m_pkthdr.rcvif;
412 	*mp = NULL;
413 	UDPSTAT_INC(udps_ipackets);
414 
415 	/*
416 	 * Strip IP options, if any; should skip this, make available to
417 	 * user, and use on returned packets, but we don't yet have a way to
418 	 * check the checksum with options still present.
419 	 */
420 	if (iphlen > sizeof (struct ip)) {
421 		ip_stripoptions(m);
422 		iphlen = sizeof(struct ip);
423 	}
424 
425 	/*
426 	 * Get IP and UDP header together in first mbuf.
427 	 */
428 	ip = mtod(m, struct ip *);
429 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
430 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
431 			UDPSTAT_INC(udps_hdrops);
432 			return (IPPROTO_DONE);
433 		}
434 		ip = mtod(m, struct ip *);
435 	}
436 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
437 	cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
438 
439 	/*
440 	 * Destination port of 0 is illegal, based on RFC768.
441 	 */
442 	if (uh->uh_dport == 0)
443 		goto badunlocked;
444 
445 	/*
446 	 * Construct sockaddr format source address.  Stuff source address
447 	 * and datagram in user buffer.
448 	 */
449 	bzero(&udpin[0], sizeof(struct sockaddr_in));
450 	udpin[0].sin_len = sizeof(struct sockaddr_in);
451 	udpin[0].sin_family = AF_INET;
452 	udpin[0].sin_port = uh->uh_sport;
453 	udpin[0].sin_addr = ip->ip_src;
454 	bzero(&udpin[1], sizeof(struct sockaddr_in));
455 	udpin[1].sin_len = sizeof(struct sockaddr_in);
456 	udpin[1].sin_family = AF_INET;
457 	udpin[1].sin_port = uh->uh_dport;
458 	udpin[1].sin_addr = ip->ip_dst;
459 
460 	/*
461 	 * Make mbuf data length reflect UDP length.  If not enough data to
462 	 * reflect UDP length, drop.
463 	 */
464 	len = ntohs((u_short)uh->uh_ulen);
465 	ip_len = ntohs(ip->ip_len) - iphlen;
466 	if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
467 		/* Zero means checksum over the complete packet. */
468 		if (len == 0)
469 			len = ip_len;
470 		cscov_partial = 0;
471 	}
472 	if (ip_len != len) {
473 		if (len > ip_len || len < sizeof(struct udphdr)) {
474 			UDPSTAT_INC(udps_badlen);
475 			goto badunlocked;
476 		}
477 		if (proto == IPPROTO_UDP)
478 			m_adj(m, len - ip_len);
479 	}
480 
481 	/*
482 	 * Save a copy of the IP header in case we want restore it for
483 	 * sending an ICMP error message in response.
484 	 */
485 	if (!V_udp_blackhole)
486 		save_ip = *ip;
487 	else
488 		memset(&save_ip, 0, sizeof(save_ip));
489 
490 	/*
491 	 * Checksum extended UDP header and data.
492 	 */
493 	if (uh->uh_sum) {
494 		u_short uh_sum;
495 
496 		if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
497 		    !cscov_partial) {
498 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
499 				uh_sum = m->m_pkthdr.csum_data;
500 			else
501 				uh_sum = in_pseudo(ip->ip_src.s_addr,
502 				    ip->ip_dst.s_addr, htonl((u_short)len +
503 				    m->m_pkthdr.csum_data + proto));
504 			uh_sum ^= 0xffff;
505 		} else {
506 			char b[9];
507 
508 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
509 			bzero(((struct ipovly *)ip)->ih_x1, 9);
510 			((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
511 			    uh->uh_ulen : htons(ip_len);
512 			uh_sum = in_cksum(m, len + sizeof (struct ip));
513 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
514 		}
515 		if (uh_sum) {
516 			UDPSTAT_INC(udps_badsum);
517 			m_freem(m);
518 			return (IPPROTO_DONE);
519 		}
520 	} else {
521 		if (proto == IPPROTO_UDP) {
522 			UDPSTAT_INC(udps_nosum);
523 		} else {
524 			/* UDPLite requires a checksum */
525 			/* XXX: What is the right UDPLite MIB counter here? */
526 			m_freem(m);
527 			return (IPPROTO_DONE);
528 		}
529 	}
530 
531 	pcbinfo = udp_get_inpcbinfo(proto);
532 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
533 	    ((!V_udp_require_l2_bcast || m->m_flags & M_BCAST) &&
534 	    in_broadcast(ip->ip_dst, ifp))) {
535 		struct inpcb *last;
536 		struct inpcbhead *pcblist;
537 		struct ip_moptions *imo;
538 
539 		INP_INFO_RLOCK(pcbinfo);
540 		pcblist = udp_get_pcblist(proto);
541 		last = NULL;
542 		LIST_FOREACH(inp, pcblist, inp_list) {
543 			if (inp->inp_lport != uh->uh_dport)
544 				continue;
545 #ifdef INET6
546 			if ((inp->inp_vflag & INP_IPV4) == 0)
547 				continue;
548 #endif
549 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
550 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
551 				continue;
552 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
553 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
554 				continue;
555 			if (inp->inp_fport != 0 &&
556 			    inp->inp_fport != uh->uh_sport)
557 				continue;
558 
559 			INP_RLOCK(inp);
560 
561 			/*
562 			 * XXXRW: Because we weren't holding either the inpcb
563 			 * or the hash lock when we checked for a match
564 			 * before, we should probably recheck now that the
565 			 * inpcb lock is held.
566 			 */
567 
568 			/*
569 			 * Handle socket delivery policy for any-source
570 			 * and source-specific multicast. [RFC3678]
571 			 */
572 			imo = inp->inp_moptions;
573 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
574 				struct sockaddr_in	 group;
575 				int			 blocked;
576 				if (imo == NULL) {
577 					INP_RUNLOCK(inp);
578 					continue;
579 				}
580 				bzero(&group, sizeof(struct sockaddr_in));
581 				group.sin_len = sizeof(struct sockaddr_in);
582 				group.sin_family = AF_INET;
583 				group.sin_addr = ip->ip_dst;
584 
585 				blocked = imo_multi_filter(imo, ifp,
586 					(struct sockaddr *)&group,
587 					(struct sockaddr *)&udpin[0]);
588 				if (blocked != MCAST_PASS) {
589 					if (blocked == MCAST_NOTGMEMBER)
590 						IPSTAT_INC(ips_notmember);
591 					if (blocked == MCAST_NOTSMEMBER ||
592 					    blocked == MCAST_MUTED)
593 						UDPSTAT_INC(udps_filtermcast);
594 					INP_RUNLOCK(inp);
595 					continue;
596 				}
597 			}
598 			if (last != NULL) {
599 				struct mbuf *n;
600 
601 				if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
602 				    NULL) {
603 					UDP_PROBE(receive, NULL, last, ip,
604 					    last, uh);
605 					if (udp_append(last, ip, n, iphlen,
606 						udpin)) {
607 						goto inp_lost;
608 					}
609 				}
610 				INP_RUNLOCK(last);
611 			}
612 			last = inp;
613 			/*
614 			 * Don't look for additional matches if this one does
615 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
616 			 * socket options set.  This heuristic avoids
617 			 * searching through all pcbs in the common case of a
618 			 * non-shared port.  It assumes that an application
619 			 * will never clear these options after setting them.
620 			 */
621 			if ((last->inp_socket->so_options &
622 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
623 				break;
624 		}
625 
626 		if (last == NULL) {
627 			/*
628 			 * No matching pcb found; discard datagram.  (No need
629 			 * to send an ICMP Port Unreachable for a broadcast
630 			 * or multicast datgram.)
631 			 */
632 			UDPSTAT_INC(udps_noportbcast);
633 			if (inp)
634 				INP_RUNLOCK(inp);
635 			INP_INFO_RUNLOCK(pcbinfo);
636 			goto badunlocked;
637 		}
638 		UDP_PROBE(receive, NULL, last, ip, last, uh);
639 		if (udp_append(last, ip, m, iphlen, udpin) == 0)
640 			INP_RUNLOCK(last);
641 	inp_lost:
642 		INP_INFO_RUNLOCK(pcbinfo);
643 		return (IPPROTO_DONE);
644 	}
645 
646 	/*
647 	 * Locate pcb for datagram.
648 	 */
649 
650 	/*
651 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
652 	 */
653 	if ((m->m_flags & M_IP_NEXTHOP) &&
654 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
655 		struct sockaddr_in *next_hop;
656 
657 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
658 
659 		/*
660 		 * Transparently forwarded. Pretend to be the destination.
661 		 * Already got one like this?
662 		 */
663 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
664 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
665 		if (!inp) {
666 			/*
667 			 * It's new.  Try to find the ambushing socket.
668 			 * Because we've rewritten the destination address,
669 			 * any hardware-generated hash is ignored.
670 			 */
671 			inp = in_pcblookup(pcbinfo, ip->ip_src,
672 			    uh->uh_sport, next_hop->sin_addr,
673 			    next_hop->sin_port ? htons(next_hop->sin_port) :
674 			    uh->uh_dport, INPLOOKUP_WILDCARD |
675 			    INPLOOKUP_RLOCKPCB, ifp);
676 		}
677 		/* Remove the tag from the packet. We don't need it anymore. */
678 		m_tag_delete(m, fwd_tag);
679 		m->m_flags &= ~M_IP_NEXTHOP;
680 	} else
681 		inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
682 		    ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
683 		    INPLOOKUP_RLOCKPCB, ifp, m);
684 	if (inp == NULL) {
685 		if (udp_log_in_vain) {
686 			char src[INET_ADDRSTRLEN];
687 			char dst[INET_ADDRSTRLEN];
688 
689 			log(LOG_INFO,
690 			    "Connection attempt to UDP %s:%d from %s:%d\n",
691 			    inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport),
692 			    inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport));
693 		}
694 		UDPSTAT_INC(udps_noport);
695 		if (m->m_flags & (M_BCAST | M_MCAST)) {
696 			UDPSTAT_INC(udps_noportbcast);
697 			goto badunlocked;
698 		}
699 		if (V_udp_blackhole)
700 			goto badunlocked;
701 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
702 			goto badunlocked;
703 		*ip = save_ip;
704 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
705 		return (IPPROTO_DONE);
706 	}
707 
708 	/*
709 	 * Check the minimum TTL for socket.
710 	 */
711 	INP_RLOCK_ASSERT(inp);
712 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
713 		INP_RUNLOCK(inp);
714 		m_freem(m);
715 		return (IPPROTO_DONE);
716 	}
717 	if (cscov_partial) {
718 		struct udpcb *up;
719 
720 		up = intoudpcb(inp);
721 		if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
722 			INP_RUNLOCK(inp);
723 			m_freem(m);
724 			return (IPPROTO_DONE);
725 		}
726 	}
727 
728 	UDP_PROBE(receive, NULL, inp, ip, inp, uh);
729 	if (udp_append(inp, ip, m, iphlen, udpin) == 0)
730 		INP_RUNLOCK(inp);
731 	return (IPPROTO_DONE);
732 
733 badunlocked:
734 	m_freem(m);
735 	return (IPPROTO_DONE);
736 }
737 #endif /* INET */
738 
739 /*
740  * Notify a udp user of an asynchronous error; just wake up so that they can
741  * collect error status.
742  */
743 struct inpcb *
744 udp_notify(struct inpcb *inp, int errno)
745 {
746 
747 	/*
748 	 * While udp_ctlinput() always calls udp_notify() with a read lock
749 	 * when invoking it directly, in_pcbnotifyall() currently uses write
750 	 * locks due to sharing code with TCP.  For now, accept either a read
751 	 * or a write lock, but a read lock is sufficient.
752 	 */
753 	INP_LOCK_ASSERT(inp);
754 	if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
755 	     errno == EHOSTDOWN) && inp->inp_route.ro_rt) {
756 		RTFREE(inp->inp_route.ro_rt);
757 		inp->inp_route.ro_rt = (struct rtentry *)NULL;
758 	}
759 
760 	inp->inp_socket->so_error = errno;
761 	sorwakeup(inp->inp_socket);
762 	sowwakeup(inp->inp_socket);
763 	return (inp);
764 }
765 
766 #ifdef INET
767 static void
768 udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
769     struct inpcbinfo *pcbinfo)
770 {
771 	struct ip *ip = vip;
772 	struct udphdr *uh;
773 	struct in_addr faddr;
774 	struct inpcb *inp;
775 
776 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
777 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
778 		return;
779 
780 	if (PRC_IS_REDIRECT(cmd)) {
781 		/* signal EHOSTDOWN, as it flushes the cached route */
782 		in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify);
783 		return;
784 	}
785 
786 	/*
787 	 * Hostdead is ugly because it goes linearly through all PCBs.
788 	 *
789 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
790 	 * DoS attack on machines with many connections.
791 	 */
792 	if (cmd == PRC_HOSTDEAD)
793 		ip = NULL;
794 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
795 		return;
796 	if (ip != NULL) {
797 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
798 		inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
799 		    ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL);
800 		if (inp != NULL) {
801 			INP_RLOCK_ASSERT(inp);
802 			if (inp->inp_socket != NULL) {
803 				udp_notify(inp, inetctlerrmap[cmd]);
804 			}
805 			INP_RUNLOCK(inp);
806 		} else {
807 			inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
808 					   ip->ip_src, uh->uh_sport,
809 					   INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
810 			if (inp != NULL) {
811 				struct udpcb *up;
812 
813 				up = intoudpcb(inp);
814 				if (up->u_icmp_func != NULL) {
815 					INP_RUNLOCK(inp);
816 					(*up->u_icmp_func)(cmd, sa, vip, up->u_tun_ctx);
817 				} else {
818 					INP_RUNLOCK(inp);
819 				}
820 			}
821 		}
822 	} else
823 		in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
824 		    udp_notify);
825 }
826 void
827 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
828 {
829 
830 	return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
831 }
832 
833 void
834 udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
835 {
836 
837 	return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
838 }
839 #endif /* INET */
840 
841 static int
842 udp_pcblist(SYSCTL_HANDLER_ARGS)
843 {
844 	int error, i, n;
845 	struct inpcb *inp, **inp_list;
846 	inp_gen_t gencnt;
847 	struct xinpgen xig;
848 
849 	/*
850 	 * The process of preparing the PCB list is too time-consuming and
851 	 * resource-intensive to repeat twice on every request.
852 	 */
853 	if (req->oldptr == 0) {
854 		n = V_udbinfo.ipi_count;
855 		n += imax(n / 8, 10);
856 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
857 		return (0);
858 	}
859 
860 	if (req->newptr != 0)
861 		return (EPERM);
862 
863 	/*
864 	 * OK, now we're committed to doing something.
865 	 */
866 	INP_INFO_RLOCK(&V_udbinfo);
867 	gencnt = V_udbinfo.ipi_gencnt;
868 	n = V_udbinfo.ipi_count;
869 	INP_INFO_RUNLOCK(&V_udbinfo);
870 
871 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
872 		+ n * sizeof(struct xinpcb));
873 	if (error != 0)
874 		return (error);
875 
876 	xig.xig_len = sizeof xig;
877 	xig.xig_count = n;
878 	xig.xig_gen = gencnt;
879 	xig.xig_sogen = so_gencnt;
880 	error = SYSCTL_OUT(req, &xig, sizeof xig);
881 	if (error)
882 		return (error);
883 
884 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
885 	if (inp_list == NULL)
886 		return (ENOMEM);
887 
888 	INP_INFO_RLOCK(&V_udbinfo);
889 	for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
890 	     inp = LIST_NEXT(inp, inp_list)) {
891 		INP_WLOCK(inp);
892 		if (inp->inp_gencnt <= gencnt &&
893 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
894 			in_pcbref(inp);
895 			inp_list[i++] = inp;
896 		}
897 		INP_WUNLOCK(inp);
898 	}
899 	INP_INFO_RUNLOCK(&V_udbinfo);
900 	n = i;
901 
902 	error = 0;
903 	for (i = 0; i < n; i++) {
904 		inp = inp_list[i];
905 		INP_RLOCK(inp);
906 		if (inp->inp_gencnt <= gencnt) {
907 			struct xinpcb xi;
908 
909 			bzero(&xi, sizeof(xi));
910 			xi.xi_len = sizeof xi;
911 			/* XXX should avoid extra copy */
912 			bcopy(inp, &xi.xi_inp, sizeof *inp);
913 			if (inp->inp_socket)
914 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
915 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
916 			INP_RUNLOCK(inp);
917 			error = SYSCTL_OUT(req, &xi, sizeof xi);
918 		} else
919 			INP_RUNLOCK(inp);
920 	}
921 	INP_INFO_WLOCK(&V_udbinfo);
922 	for (i = 0; i < n; i++) {
923 		inp = inp_list[i];
924 		INP_RLOCK(inp);
925 		if (!in_pcbrele_rlocked(inp))
926 			INP_RUNLOCK(inp);
927 	}
928 	INP_INFO_WUNLOCK(&V_udbinfo);
929 
930 	if (!error) {
931 		/*
932 		 * Give the user an updated idea of our state.  If the
933 		 * generation differs from what we told her before, she knows
934 		 * that something happened while we were processing this
935 		 * request, and it might be necessary to retry.
936 		 */
937 		INP_INFO_RLOCK(&V_udbinfo);
938 		xig.xig_gen = V_udbinfo.ipi_gencnt;
939 		xig.xig_sogen = so_gencnt;
940 		xig.xig_count = V_udbinfo.ipi_count;
941 		INP_INFO_RUNLOCK(&V_udbinfo);
942 		error = SYSCTL_OUT(req, &xig, sizeof xig);
943 	}
944 	free(inp_list, M_TEMP);
945 	return (error);
946 }
947 
948 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
949     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
950     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
951 
952 #ifdef INET
953 static int
954 udp_getcred(SYSCTL_HANDLER_ARGS)
955 {
956 	struct xucred xuc;
957 	struct sockaddr_in addrs[2];
958 	struct inpcb *inp;
959 	int error;
960 
961 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
962 	if (error)
963 		return (error);
964 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
965 	if (error)
966 		return (error);
967 	inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
968 	    addrs[0].sin_addr, addrs[0].sin_port,
969 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
970 	if (inp != NULL) {
971 		INP_RLOCK_ASSERT(inp);
972 		if (inp->inp_socket == NULL)
973 			error = ENOENT;
974 		if (error == 0)
975 			error = cr_canseeinpcb(req->td->td_ucred, inp);
976 		if (error == 0)
977 			cru2x(inp->inp_cred, &xuc);
978 		INP_RUNLOCK(inp);
979 	} else
980 		error = ENOENT;
981 	if (error == 0)
982 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
983 	return (error);
984 }
985 
986 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
987     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
988     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
989 #endif /* INET */
990 
991 int
992 udp_ctloutput(struct socket *so, struct sockopt *sopt)
993 {
994 	struct inpcb *inp;
995 	struct udpcb *up;
996 	int isudplite, error, optval;
997 
998 	error = 0;
999 	isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
1000 	inp = sotoinpcb(so);
1001 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1002 	INP_WLOCK(inp);
1003 	if (sopt->sopt_level != so->so_proto->pr_protocol) {
1004 #ifdef INET6
1005 		if (INP_CHECK_SOCKAF(so, AF_INET6)) {
1006 			INP_WUNLOCK(inp);
1007 			error = ip6_ctloutput(so, sopt);
1008 		}
1009 #endif
1010 #if defined(INET) && defined(INET6)
1011 		else
1012 #endif
1013 #ifdef INET
1014 		{
1015 			INP_WUNLOCK(inp);
1016 			error = ip_ctloutput(so, sopt);
1017 		}
1018 #endif
1019 		return (error);
1020 	}
1021 
1022 	switch (sopt->sopt_dir) {
1023 	case SOPT_SET:
1024 		switch (sopt->sopt_name) {
1025 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1026 #ifdef INET
1027 		case UDP_ENCAP:
1028 			if (!IPSEC_ENABLED(ipv4)) {
1029 				INP_WUNLOCK(inp);
1030 				return (ENOPROTOOPT);
1031 			}
1032 			error = UDPENCAP_PCBCTL(inp, sopt);
1033 			break;
1034 #endif /* INET */
1035 #endif /* IPSEC */
1036 		case UDPLITE_SEND_CSCOV:
1037 		case UDPLITE_RECV_CSCOV:
1038 			if (!isudplite) {
1039 				INP_WUNLOCK(inp);
1040 				error = ENOPROTOOPT;
1041 				break;
1042 			}
1043 			INP_WUNLOCK(inp);
1044 			error = sooptcopyin(sopt, &optval, sizeof(optval),
1045 			    sizeof(optval));
1046 			if (error != 0)
1047 				break;
1048 			inp = sotoinpcb(so);
1049 			KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1050 			INP_WLOCK(inp);
1051 			up = intoudpcb(inp);
1052 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1053 			if ((optval != 0 && optval < 8) || (optval > 65535)) {
1054 				INP_WUNLOCK(inp);
1055 				error = EINVAL;
1056 				break;
1057 			}
1058 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1059 				up->u_txcslen = optval;
1060 			else
1061 				up->u_rxcslen = optval;
1062 			INP_WUNLOCK(inp);
1063 			break;
1064 		default:
1065 			INP_WUNLOCK(inp);
1066 			error = ENOPROTOOPT;
1067 			break;
1068 		}
1069 		break;
1070 	case SOPT_GET:
1071 		switch (sopt->sopt_name) {
1072 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1073 #ifdef INET
1074 		case UDP_ENCAP:
1075 			if (!IPSEC_ENABLED(ipv4)) {
1076 				INP_WUNLOCK(inp);
1077 				return (ENOPROTOOPT);
1078 			}
1079 			error = UDPENCAP_PCBCTL(inp, sopt);
1080 			break;
1081 #endif /* INET */
1082 #endif /* IPSEC */
1083 		case UDPLITE_SEND_CSCOV:
1084 		case UDPLITE_RECV_CSCOV:
1085 			if (!isudplite) {
1086 				INP_WUNLOCK(inp);
1087 				error = ENOPROTOOPT;
1088 				break;
1089 			}
1090 			up = intoudpcb(inp);
1091 			KASSERT(up != NULL, ("%s: up == NULL", __func__));
1092 			if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1093 				optval = up->u_txcslen;
1094 			else
1095 				optval = up->u_rxcslen;
1096 			INP_WUNLOCK(inp);
1097 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1098 			break;
1099 		default:
1100 			INP_WUNLOCK(inp);
1101 			error = ENOPROTOOPT;
1102 			break;
1103 		}
1104 		break;
1105 	}
1106 	return (error);
1107 }
1108 
1109 #ifdef INET
1110 #define	UH_WLOCKED	2
1111 #define	UH_RLOCKED	1
1112 #define	UH_UNLOCKED	0
1113 static int
1114 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1115     struct mbuf *control, struct thread *td)
1116 {
1117 	struct udpiphdr *ui;
1118 	int len = m->m_pkthdr.len;
1119 	struct in_addr faddr, laddr;
1120 	struct cmsghdr *cm;
1121 	struct inpcbinfo *pcbinfo;
1122 	struct sockaddr_in *sin, src;
1123 	int cscov_partial = 0;
1124 	int error = 0;
1125 	int ipflags;
1126 	u_short fport, lport;
1127 	int unlock_udbinfo, unlock_inp;
1128 	u_char tos;
1129 	uint8_t pr;
1130 	uint16_t cscov = 0;
1131 	uint32_t flowid = 0;
1132 	uint8_t flowtype = M_HASHTYPE_NONE;
1133 
1134 	/*
1135 	 * udp_output() may need to temporarily bind or connect the current
1136 	 * inpcb.  As such, we don't know up front whether we will need the
1137 	 * pcbinfo lock or not.  Do any work to decide what is needed up
1138 	 * front before acquiring any locks.
1139 	 */
1140 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1141 		if (control)
1142 			m_freem(control);
1143 		m_freem(m);
1144 		return (EMSGSIZE);
1145 	}
1146 
1147 	src.sin_family = 0;
1148 	sin = (struct sockaddr_in *)addr;
1149 	if (sin == NULL ||
1150 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1151 		INP_WLOCK(inp);
1152 		unlock_inp = UH_WLOCKED;
1153 	} else {
1154 		INP_RLOCK(inp);
1155 		unlock_inp = UH_RLOCKED;
1156 	}
1157 	tos = inp->inp_ip_tos;
1158 	if (control != NULL) {
1159 		/*
1160 		 * XXX: Currently, we assume all the optional information is
1161 		 * stored in a single mbuf.
1162 		 */
1163 		if (control->m_next) {
1164 			if (unlock_inp == UH_WLOCKED)
1165 				INP_WUNLOCK(inp);
1166 			else
1167 				INP_RUNLOCK(inp);
1168 			m_freem(control);
1169 			m_freem(m);
1170 			return (EINVAL);
1171 		}
1172 		for (; control->m_len > 0;
1173 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1174 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1175 			cm = mtod(control, struct cmsghdr *);
1176 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1177 			    || cm->cmsg_len > control->m_len) {
1178 				error = EINVAL;
1179 				break;
1180 			}
1181 			if (cm->cmsg_level != IPPROTO_IP)
1182 				continue;
1183 
1184 			switch (cm->cmsg_type) {
1185 			case IP_SENDSRCADDR:
1186 				if (cm->cmsg_len !=
1187 				    CMSG_LEN(sizeof(struct in_addr))) {
1188 					error = EINVAL;
1189 					break;
1190 				}
1191 				bzero(&src, sizeof(src));
1192 				src.sin_family = AF_INET;
1193 				src.sin_len = sizeof(src);
1194 				src.sin_port = inp->inp_lport;
1195 				src.sin_addr =
1196 				    *(struct in_addr *)CMSG_DATA(cm);
1197 				break;
1198 
1199 			case IP_TOS:
1200 				if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1201 					error = EINVAL;
1202 					break;
1203 				}
1204 				tos = *(u_char *)CMSG_DATA(cm);
1205 				break;
1206 
1207 			case IP_FLOWID:
1208 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1209 					error = EINVAL;
1210 					break;
1211 				}
1212 				flowid = *(uint32_t *) CMSG_DATA(cm);
1213 				break;
1214 
1215 			case IP_FLOWTYPE:
1216 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1217 					error = EINVAL;
1218 					break;
1219 				}
1220 				flowtype = *(uint32_t *) CMSG_DATA(cm);
1221 				break;
1222 
1223 #ifdef	RSS
1224 			case IP_RSSBUCKETID:
1225 				if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1226 					error = EINVAL;
1227 					break;
1228 				}
1229 				/* This is just a placeholder for now */
1230 				break;
1231 #endif	/* RSS */
1232 			default:
1233 				error = ENOPROTOOPT;
1234 				break;
1235 			}
1236 			if (error)
1237 				break;
1238 		}
1239 		m_freem(control);
1240 	}
1241 	if (error) {
1242 		if (unlock_inp == UH_WLOCKED)
1243 			INP_WUNLOCK(inp);
1244 		else
1245 			INP_RUNLOCK(inp);
1246 		m_freem(m);
1247 		return (error);
1248 	}
1249 
1250 	/*
1251 	 * Depending on whether or not the application has bound or connected
1252 	 * the socket, we may have to do varying levels of work.  The optimal
1253 	 * case is for a connected UDP socket, as a global lock isn't
1254 	 * required at all.
1255 	 *
1256 	 * In order to decide which we need, we require stability of the
1257 	 * inpcb binding, which we ensure by acquiring a read lock on the
1258 	 * inpcb.  This doesn't strictly follow the lock order, so we play
1259 	 * the trylock and retry game; note that we may end up with more
1260 	 * conservative locks than required the second time around, so later
1261 	 * assertions have to accept that.  Further analysis of the number of
1262 	 * misses under contention is required.
1263 	 *
1264 	 * XXXRW: Check that hash locking update here is correct.
1265 	 */
1266 	pr = inp->inp_socket->so_proto->pr_protocol;
1267 	pcbinfo = udp_get_inpcbinfo(pr);
1268 	sin = (struct sockaddr_in *)addr;
1269 	if (sin != NULL &&
1270 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1271 		INP_HASH_WLOCK(pcbinfo);
1272 		unlock_udbinfo = UH_WLOCKED;
1273 	} else if ((sin != NULL && (
1274 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
1275 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
1276 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
1277 	    (inp->inp_lport == 0))) ||
1278 	    (src.sin_family == AF_INET)) {
1279 		INP_HASH_RLOCK(pcbinfo);
1280 		unlock_udbinfo = UH_RLOCKED;
1281 	} else
1282 		unlock_udbinfo = UH_UNLOCKED;
1283 
1284 	/*
1285 	 * If the IP_SENDSRCADDR control message was specified, override the
1286 	 * source address for this datagram.  Its use is invalidated if the
1287 	 * address thus specified is incomplete or clobbers other inpcbs.
1288 	 */
1289 	laddr = inp->inp_laddr;
1290 	lport = inp->inp_lport;
1291 	if (src.sin_family == AF_INET) {
1292 		INP_HASH_LOCK_ASSERT(pcbinfo);
1293 		if ((lport == 0) ||
1294 		    (laddr.s_addr == INADDR_ANY &&
1295 		     src.sin_addr.s_addr == INADDR_ANY)) {
1296 			error = EINVAL;
1297 			goto release;
1298 		}
1299 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1300 		    &laddr.s_addr, &lport, td->td_ucred);
1301 		if (error)
1302 			goto release;
1303 	}
1304 
1305 	/*
1306 	 * If a UDP socket has been connected, then a local address/port will
1307 	 * have been selected and bound.
1308 	 *
1309 	 * If a UDP socket has not been connected to, then an explicit
1310 	 * destination address must be used, in which case a local
1311 	 * address/port may not have been selected and bound.
1312 	 */
1313 	if (sin != NULL) {
1314 		INP_LOCK_ASSERT(inp);
1315 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1316 			error = EISCONN;
1317 			goto release;
1318 		}
1319 
1320 		/*
1321 		 * Jail may rewrite the destination address, so let it do
1322 		 * that before we use it.
1323 		 */
1324 		error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1325 		if (error)
1326 			goto release;
1327 
1328 		/*
1329 		 * If a local address or port hasn't yet been selected, or if
1330 		 * the destination address needs to be rewritten due to using
1331 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
1332 		 * to do the heavy lifting.  Once a port is selected, we
1333 		 * commit the binding back to the socket; we also commit the
1334 		 * binding of the address if in jail.
1335 		 *
1336 		 * If we already have a valid binding and we're not
1337 		 * requesting a destination address rewrite, use a fast path.
1338 		 */
1339 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
1340 		    inp->inp_lport == 0 ||
1341 		    sin->sin_addr.s_addr == INADDR_ANY ||
1342 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
1343 			INP_HASH_LOCK_ASSERT(pcbinfo);
1344 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
1345 			    &lport, &faddr.s_addr, &fport, NULL,
1346 			    td->td_ucred);
1347 			if (error)
1348 				goto release;
1349 
1350 			/*
1351 			 * XXXRW: Why not commit the port if the address is
1352 			 * !INADDR_ANY?
1353 			 */
1354 			/* Commit the local port if newly assigned. */
1355 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
1356 			    inp->inp_lport == 0) {
1357 				INP_WLOCK_ASSERT(inp);
1358 				INP_HASH_WLOCK_ASSERT(pcbinfo);
1359 				/*
1360 				 * Remember addr if jailed, to prevent
1361 				 * rebinding.
1362 				 */
1363 				if (prison_flag(td->td_ucred, PR_IP4))
1364 					inp->inp_laddr = laddr;
1365 				inp->inp_lport = lport;
1366 				if (in_pcbinshash(inp) != 0) {
1367 					inp->inp_lport = 0;
1368 					error = EAGAIN;
1369 					goto release;
1370 				}
1371 				inp->inp_flags |= INP_ANONPORT;
1372 			}
1373 		} else {
1374 			faddr = sin->sin_addr;
1375 			fport = sin->sin_port;
1376 		}
1377 	} else {
1378 		INP_LOCK_ASSERT(inp);
1379 		faddr = inp->inp_faddr;
1380 		fport = inp->inp_fport;
1381 		if (faddr.s_addr == INADDR_ANY) {
1382 			error = ENOTCONN;
1383 			goto release;
1384 		}
1385 	}
1386 
1387 	/*
1388 	 * Calculate data length and get a mbuf for UDP, IP, and possible
1389 	 * link-layer headers.  Immediate slide the data pointer back forward
1390 	 * since we won't use that space at this layer.
1391 	 */
1392 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1393 	if (m == NULL) {
1394 		error = ENOBUFS;
1395 		goto release;
1396 	}
1397 	m->m_data += max_linkhdr;
1398 	m->m_len -= max_linkhdr;
1399 	m->m_pkthdr.len -= max_linkhdr;
1400 
1401 	/*
1402 	 * Fill in mbuf with extended UDP header and addresses and length put
1403 	 * into network format.
1404 	 */
1405 	ui = mtod(m, struct udpiphdr *);
1406 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1407 	ui->ui_pr = pr;
1408 	ui->ui_src = laddr;
1409 	ui->ui_dst = faddr;
1410 	ui->ui_sport = lport;
1411 	ui->ui_dport = fport;
1412 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1413 	if (pr == IPPROTO_UDPLITE) {
1414 		struct udpcb *up;
1415 		uint16_t plen;
1416 
1417 		up = intoudpcb(inp);
1418 		cscov = up->u_txcslen;
1419 		plen = (u_short)len + sizeof(struct udphdr);
1420 		if (cscov >= plen)
1421 			cscov = 0;
1422 		ui->ui_len = htons(plen);
1423 		ui->ui_ulen = htons(cscov);
1424 		/*
1425 		 * For UDP-Lite, checksum coverage length of zero means
1426 		 * the entire UDPLite packet is covered by the checksum.
1427 		 */
1428 		cscov_partial = (cscov == 0) ? 0 : 1;
1429 	} else
1430 		ui->ui_v = IPVERSION << 4;
1431 
1432 	/*
1433 	 * Set the Don't Fragment bit in the IP header.
1434 	 */
1435 	if (inp->inp_flags & INP_DONTFRAG) {
1436 		struct ip *ip;
1437 
1438 		ip = (struct ip *)&ui->ui_i;
1439 		ip->ip_off |= htons(IP_DF);
1440 	}
1441 
1442 	ipflags = 0;
1443 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1444 		ipflags |= IP_ROUTETOIF;
1445 	if (inp->inp_socket->so_options & SO_BROADCAST)
1446 		ipflags |= IP_ALLOWBROADCAST;
1447 	if (inp->inp_flags & INP_ONESBCAST)
1448 		ipflags |= IP_SENDONES;
1449 
1450 #ifdef MAC
1451 	mac_inpcb_create_mbuf(inp, m);
1452 #endif
1453 
1454 	/*
1455 	 * Set up checksum and output datagram.
1456 	 */
1457 	ui->ui_sum = 0;
1458 	if (pr == IPPROTO_UDPLITE) {
1459 		if (inp->inp_flags & INP_ONESBCAST)
1460 			faddr.s_addr = INADDR_BROADCAST;
1461 		if (cscov_partial) {
1462 			if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1463 				ui->ui_sum = 0xffff;
1464 		} else {
1465 			if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1466 				ui->ui_sum = 0xffff;
1467 		}
1468 	} else if (V_udp_cksum) {
1469 		if (inp->inp_flags & INP_ONESBCAST)
1470 			faddr.s_addr = INADDR_BROADCAST;
1471 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1472 		    htons((u_short)len + sizeof(struct udphdr) + pr));
1473 		m->m_pkthdr.csum_flags = CSUM_UDP;
1474 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1475 	}
1476 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1477 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1478 	((struct ip *)ui)->ip_tos = tos;		/* XXX */
1479 	UDPSTAT_INC(udps_opackets);
1480 
1481 	/*
1482 	 * Setup flowid / RSS information for outbound socket.
1483 	 *
1484 	 * Once the UDP code decides to set a flowid some other way,
1485 	 * this allows the flowid to be overridden by userland.
1486 	 */
1487 	if (flowtype != M_HASHTYPE_NONE) {
1488 		m->m_pkthdr.flowid = flowid;
1489 		M_HASHTYPE_SET(m, flowtype);
1490 #ifdef	RSS
1491 	} else {
1492 		uint32_t hash_val, hash_type;
1493 		/*
1494 		 * Calculate an appropriate RSS hash for UDP and
1495 		 * UDP Lite.
1496 		 *
1497 		 * The called function will take care of figuring out
1498 		 * whether a 2-tuple or 4-tuple hash is required based
1499 		 * on the currently configured scheme.
1500 		 *
1501 		 * Later later on connected socket values should be
1502 		 * cached in the inpcb and reused, rather than constantly
1503 		 * re-calculating it.
1504 		 *
1505 		 * UDP Lite is a different protocol number and will
1506 		 * likely end up being hashed as a 2-tuple until
1507 		 * RSS / NICs grow UDP Lite protocol awareness.
1508 		 */
1509 		if (rss_proto_software_hash_v4(faddr, laddr, fport, lport,
1510 		    pr, &hash_val, &hash_type) == 0) {
1511 			m->m_pkthdr.flowid = hash_val;
1512 			M_HASHTYPE_SET(m, hash_type);
1513 		}
1514 #endif
1515 	}
1516 
1517 #ifdef	RSS
1518 	/*
1519 	 * Don't override with the inp cached flowid value.
1520 	 *
1521 	 * Depending upon the kind of send being done, the inp
1522 	 * flowid/flowtype values may actually not be appropriate
1523 	 * for this particular socket send.
1524 	 *
1525 	 * We should either leave the flowid at zero (which is what is
1526 	 * currently done) or set it to some software generated
1527 	 * hash value based on the packet contents.
1528 	 */
1529 	ipflags |= IP_NODEFAULTFLOWID;
1530 #endif	/* RSS */
1531 
1532 	if (unlock_udbinfo == UH_WLOCKED)
1533 		INP_HASH_WUNLOCK(pcbinfo);
1534 	else if (unlock_udbinfo == UH_RLOCKED)
1535 		INP_HASH_RUNLOCK(pcbinfo);
1536 	UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1537 	error = ip_output(m, inp->inp_options,
1538 	    (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags,
1539 	    inp->inp_moptions, inp);
1540 	if (unlock_inp == UH_WLOCKED)
1541 		INP_WUNLOCK(inp);
1542 	else
1543 		INP_RUNLOCK(inp);
1544 	return (error);
1545 
1546 release:
1547 	if (unlock_udbinfo == UH_WLOCKED) {
1548 		KASSERT(unlock_inp == UH_WLOCKED,
1549 		    ("%s: excl udbinfo lock, shared inp lock", __func__));
1550 		INP_HASH_WUNLOCK(pcbinfo);
1551 		INP_WUNLOCK(inp);
1552 	} else if (unlock_udbinfo == UH_RLOCKED) {
1553 		KASSERT(unlock_inp == UH_RLOCKED,
1554 		    ("%s: shared udbinfo lock, excl inp lock", __func__));
1555 		INP_HASH_RUNLOCK(pcbinfo);
1556 		INP_RUNLOCK(inp);
1557 	} else if (unlock_inp == UH_WLOCKED)
1558 		INP_WUNLOCK(inp);
1559 	else
1560 		INP_RUNLOCK(inp);
1561 	m_freem(m);
1562 	return (error);
1563 }
1564 
1565 static void
1566 udp_abort(struct socket *so)
1567 {
1568 	struct inpcb *inp;
1569 	struct inpcbinfo *pcbinfo;
1570 
1571 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1572 	inp = sotoinpcb(so);
1573 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1574 	INP_WLOCK(inp);
1575 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1576 		INP_HASH_WLOCK(pcbinfo);
1577 		in_pcbdisconnect(inp);
1578 		inp->inp_laddr.s_addr = INADDR_ANY;
1579 		INP_HASH_WUNLOCK(pcbinfo);
1580 		soisdisconnected(so);
1581 	}
1582 	INP_WUNLOCK(inp);
1583 }
1584 
1585 static int
1586 udp_attach(struct socket *so, int proto, struct thread *td)
1587 {
1588 	struct inpcb *inp;
1589 	struct inpcbinfo *pcbinfo;
1590 	int error;
1591 
1592 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1593 	inp = sotoinpcb(so);
1594 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1595 	error = soreserve(so, udp_sendspace, udp_recvspace);
1596 	if (error)
1597 		return (error);
1598 	INP_INFO_WLOCK(pcbinfo);
1599 	error = in_pcballoc(so, pcbinfo);
1600 	if (error) {
1601 		INP_INFO_WUNLOCK(pcbinfo);
1602 		return (error);
1603 	}
1604 
1605 	inp = sotoinpcb(so);
1606 	inp->inp_vflag |= INP_IPV4;
1607 	inp->inp_ip_ttl = V_ip_defttl;
1608 
1609 	error = udp_newudpcb(inp);
1610 	if (error) {
1611 		in_pcbdetach(inp);
1612 		in_pcbfree(inp);
1613 		INP_INFO_WUNLOCK(pcbinfo);
1614 		return (error);
1615 	}
1616 
1617 	INP_WUNLOCK(inp);
1618 	INP_INFO_WUNLOCK(pcbinfo);
1619 	return (0);
1620 }
1621 #endif /* INET */
1622 
1623 int
1624 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1625 {
1626 	struct inpcb *inp;
1627 	struct udpcb *up;
1628 
1629 	KASSERT(so->so_type == SOCK_DGRAM,
1630 	    ("udp_set_kernel_tunneling: !dgram"));
1631 	inp = sotoinpcb(so);
1632 	KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1633 	INP_WLOCK(inp);
1634 	up = intoudpcb(inp);
1635 	if ((up->u_tun_func != NULL) ||
1636 	    (up->u_icmp_func != NULL)) {
1637 		INP_WUNLOCK(inp);
1638 		return (EBUSY);
1639 	}
1640 	up->u_tun_func = f;
1641 	up->u_icmp_func = i;
1642 	up->u_tun_ctx = ctx;
1643 	INP_WUNLOCK(inp);
1644 	return (0);
1645 }
1646 
1647 #ifdef INET
1648 static int
1649 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1650 {
1651 	struct inpcb *inp;
1652 	struct inpcbinfo *pcbinfo;
1653 	int error;
1654 
1655 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1656 	inp = sotoinpcb(so);
1657 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1658 	INP_WLOCK(inp);
1659 	INP_HASH_WLOCK(pcbinfo);
1660 	error = in_pcbbind(inp, nam, td->td_ucred);
1661 	INP_HASH_WUNLOCK(pcbinfo);
1662 	INP_WUNLOCK(inp);
1663 	return (error);
1664 }
1665 
1666 static void
1667 udp_close(struct socket *so)
1668 {
1669 	struct inpcb *inp;
1670 	struct inpcbinfo *pcbinfo;
1671 
1672 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1673 	inp = sotoinpcb(so);
1674 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1675 	INP_WLOCK(inp);
1676 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1677 		INP_HASH_WLOCK(pcbinfo);
1678 		in_pcbdisconnect(inp);
1679 		inp->inp_laddr.s_addr = INADDR_ANY;
1680 		INP_HASH_WUNLOCK(pcbinfo);
1681 		soisdisconnected(so);
1682 	}
1683 	INP_WUNLOCK(inp);
1684 }
1685 
1686 static int
1687 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1688 {
1689 	struct inpcb *inp;
1690 	struct inpcbinfo *pcbinfo;
1691 	struct sockaddr_in *sin;
1692 	int error;
1693 
1694 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1695 	inp = sotoinpcb(so);
1696 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1697 	INP_WLOCK(inp);
1698 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1699 		INP_WUNLOCK(inp);
1700 		return (EISCONN);
1701 	}
1702 	sin = (struct sockaddr_in *)nam;
1703 	error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1704 	if (error != 0) {
1705 		INP_WUNLOCK(inp);
1706 		return (error);
1707 	}
1708 	INP_HASH_WLOCK(pcbinfo);
1709 	error = in_pcbconnect(inp, nam, td->td_ucred);
1710 	INP_HASH_WUNLOCK(pcbinfo);
1711 	if (error == 0)
1712 		soisconnected(so);
1713 	INP_WUNLOCK(inp);
1714 	return (error);
1715 }
1716 
1717 static void
1718 udp_detach(struct socket *so)
1719 {
1720 	struct inpcb *inp;
1721 	struct inpcbinfo *pcbinfo;
1722 	struct udpcb *up;
1723 
1724 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1725 	inp = sotoinpcb(so);
1726 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1727 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1728 	    ("udp_detach: not disconnected"));
1729 	INP_INFO_WLOCK(pcbinfo);
1730 	INP_WLOCK(inp);
1731 	up = intoudpcb(inp);
1732 	KASSERT(up != NULL, ("%s: up == NULL", __func__));
1733 	inp->inp_ppcb = NULL;
1734 	in_pcbdetach(inp);
1735 	in_pcbfree(inp);
1736 	INP_INFO_WUNLOCK(pcbinfo);
1737 	udp_discardcb(up);
1738 }
1739 
1740 static int
1741 udp_disconnect(struct socket *so)
1742 {
1743 	struct inpcb *inp;
1744 	struct inpcbinfo *pcbinfo;
1745 
1746 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1747 	inp = sotoinpcb(so);
1748 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1749 	INP_WLOCK(inp);
1750 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1751 		INP_WUNLOCK(inp);
1752 		return (ENOTCONN);
1753 	}
1754 	INP_HASH_WLOCK(pcbinfo);
1755 	in_pcbdisconnect(inp);
1756 	inp->inp_laddr.s_addr = INADDR_ANY;
1757 	INP_HASH_WUNLOCK(pcbinfo);
1758 	SOCK_LOCK(so);
1759 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1760 	SOCK_UNLOCK(so);
1761 	INP_WUNLOCK(inp);
1762 	return (0);
1763 }
1764 
1765 static int
1766 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1767     struct mbuf *control, struct thread *td)
1768 {
1769 	struct inpcb *inp;
1770 
1771 	inp = sotoinpcb(so);
1772 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1773 	return (udp_output(inp, m, addr, control, td));
1774 }
1775 #endif /* INET */
1776 
1777 int
1778 udp_shutdown(struct socket *so)
1779 {
1780 	struct inpcb *inp;
1781 
1782 	inp = sotoinpcb(so);
1783 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1784 	INP_WLOCK(inp);
1785 	socantsendmore(so);
1786 	INP_WUNLOCK(inp);
1787 	return (0);
1788 }
1789 
1790 #ifdef INET
1791 struct pr_usrreqs udp_usrreqs = {
1792 	.pru_abort =		udp_abort,
1793 	.pru_attach =		udp_attach,
1794 	.pru_bind =		udp_bind,
1795 	.pru_connect =		udp_connect,
1796 	.pru_control =		in_control,
1797 	.pru_detach =		udp_detach,
1798 	.pru_disconnect =	udp_disconnect,
1799 	.pru_peeraddr =		in_getpeeraddr,
1800 	.pru_send =		udp_send,
1801 	.pru_soreceive =	soreceive_dgram,
1802 	.pru_sosend =		sosend_dgram,
1803 	.pru_shutdown =		udp_shutdown,
1804 	.pru_sockaddr =		in_getsockaddr,
1805 	.pru_sosetlabel =	in_pcbsosetlabel,
1806 	.pru_close =		udp_close,
1807 };
1808 #endif /* INET */
1809