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