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