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