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