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