xref: /xnu-8796.141.3/bsd/netinet6/udp6_usrreq.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1 /*
2  * Copyright (c) 2000-2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*	$FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $	*/
30 /*	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $	*/
31 
32 /*
33  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the project nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 /*
62  * Copyright (c) 1982, 1986, 1989, 1993
63  *	The Regents of the University of California.  All rights reserved.
64  *
65  * Redistribution and use in source and binary forms, with or without
66  * modification, are permitted provided that the following conditions
67  * are met:
68  * 1. Redistributions of source code must retain the above copyright
69  *    notice, this list of conditions and the following disclaimer.
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in the
72  *    documentation and/or other materials provided with the distribution.
73  * 3. All advertising materials mentioning features or use of this software
74  *    must display the following acknowledgement:
75  *	This product includes software developed by the University of
76  *	California, Berkeley and its contributors.
77  * 4. Neither the name of the University nor the names of its contributors
78  *    may be used to endorse or promote products derived from this software
79  *    without specific prior written permission.
80  *
81  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91  * SUCH DAMAGE.
92  *
93  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
94  */
95 #include <sys/kernel.h>
96 #include <sys/malloc.h>
97 #include <sys/mbuf.h>
98 #include <sys/param.h>
99 #include <sys/protosw.h>
100 #include <sys/socket.h>
101 #include <sys/socketvar.h>
102 #include <sys/sysctl.h>
103 #include <sys/errno.h>
104 #include <sys/stat.h>
105 #include <sys/systm.h>
106 #include <sys/syslog.h>
107 #include <sys/proc.h>
108 #include <sys/kauth.h>
109 
110 #include <net/if.h>
111 #include <net/route.h>
112 #include <net/if_types.h>
113 #include <net/ntstat.h>
114 #include <net/dlil.h>
115 #include <net/net_api_stats.h>
116 
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/ip.h>
120 #include <netinet/in_pcb.h>
121 #include <netinet/in_var.h>
122 #include <netinet/ip_var.h>
123 #include <netinet/udp.h>
124 #include <netinet/udp_var.h>
125 #include <netinet/ip6.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet6/in6_pcb.h>
128 #include <netinet/icmp6.h>
129 #include <netinet6/udp6_var.h>
130 #include <netinet6/ip6protosw.h>
131 
132 #if IPSEC
133 #include <netinet6/ipsec.h>
134 #include <netinet6/ipsec6.h>
135 #include <netinet6/esp6.h>
136 #include <netkey/key.h>
137 extern int ipsec_bypass;
138 extern int esp_udp_encap_port;
139 #endif /* IPSEC */
140 
141 #if NECP
142 #include <net/necp.h>
143 #endif /* NECP */
144 
145 #if FLOW_DIVERT
146 #include <netinet/flow_divert.h>
147 #endif /* FLOW_DIVERT */
148 
149 #if CONTENT_FILTER
150 #include <net/content_filter.h>
151 #endif /* CONTENT_FILTER */
152 
153 #if SKYWALK
154 #include <skywalk/core/skywalk_var.h>
155 #endif /* SKYWALK */
156 
157 /*
158  * UDP protocol inplementation.
159  * Per RFC 768, August, 1980.
160  */
161 
162 static int udp6_abort(struct socket *);
163 static int udp6_attach(struct socket *, int, struct proc *);
164 static int udp6_bind(struct socket *, struct sockaddr *, struct proc *);
165 static int udp6_connectx(struct socket *, struct sockaddr *,
166     struct sockaddr *, struct proc *, uint32_t, sae_associd_t,
167     sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *);
168 static  int udp6_detach(struct socket *);
169 static int udp6_disconnect(struct socket *);
170 static int udp6_disconnectx(struct socket *, sae_associd_t, sae_connid_t);
171 static int udp6_send(struct socket *, int, struct mbuf *, struct sockaddr *,
172     struct mbuf *, struct proc *);
173 static void udp6_append(struct inpcb *, struct ip6_hdr *,
174     struct sockaddr_in6 *, struct mbuf *, int, struct ifnet *);
175 static int udp6_input_checksum(struct mbuf *, struct udphdr *, int, int);
176 static int udp6_defunct(struct socket *);
177 
178 struct pr_usrreqs udp6_usrreqs = {
179 	.pru_abort =            udp6_abort,
180 	.pru_attach =           udp6_attach,
181 	.pru_bind =             udp6_bind,
182 	.pru_connect =          udp6_connect,
183 	.pru_connectx =         udp6_connectx,
184 	.pru_control =          in6_control,
185 	.pru_detach =           udp6_detach,
186 	.pru_disconnect =       udp6_disconnect,
187 	.pru_disconnectx =      udp6_disconnectx,
188 	.pru_peeraddr =         in6_mapped_peeraddr,
189 	.pru_send =             udp6_send,
190 	.pru_shutdown =         udp_shutdown,
191 	.pru_sockaddr =         in6_mapped_sockaddr,
192 	.pru_sosend =           sosend,
193 	.pru_soreceive =        soreceive,
194 	.pru_soreceive_list =   soreceive_list,
195 	.pru_defunct =          udp6_defunct,
196 };
197 
198 /*
199  * subroutine of udp6_input(), mainly for source code readability.
200  */
201 static void
udp6_append(struct inpcb * last,struct ip6_hdr * ip6,struct sockaddr_in6 * udp_in6,struct mbuf * n,int off,struct ifnet * ifp)202 udp6_append(struct inpcb *last, struct ip6_hdr *ip6,
203     struct sockaddr_in6 *udp_in6, struct mbuf *n, int off, struct ifnet *ifp)
204 {
205 #pragma unused(ip6)
206 	struct  mbuf *opts = NULL;
207 	int ret = 0;
208 	boolean_t cell = IFNET_IS_CELLULAR(ifp);
209 	boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
210 	boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
211 
212 	if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
213 	    SOFLOW_ENABLED(last->in6p_socket) ||
214 	    SO_RECV_CONTROL_OPTS(last->in6p_socket)) {
215 		ret = ip6_savecontrol(last, n, &opts);
216 		if (ret != 0) {
217 			m_freem(n);
218 			m_freem(opts);
219 			return;
220 		}
221 	}
222 	m_adj(n, off);
223 	if (nstat_collect) {
224 		INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1);
225 		INP_ADD_STAT(last, cell, wifi, wired, rxbytes, n->m_pkthdr.len);
226 		inp_set_activity_bitmap(last);
227 	}
228 	so_recv_data_stat(last->in6p_socket, n, 0);
229 	if (sbappendaddr(&last->in6p_socket->so_rcv,
230 	    (struct sockaddr *)udp_in6, n, opts, NULL) == 0) {
231 		udpstat.udps_fullsock++;
232 	} else {
233 		sorwakeup(last->in6p_socket);
234 	}
235 }
236 
237 int
udp6_input(struct mbuf ** mp,int * offp,int proto)238 udp6_input(struct mbuf **mp, int *offp, int proto)
239 {
240 #pragma unused(proto)
241 	struct mbuf *m = *mp;
242 	struct ifnet *ifp;
243 	struct ip6_hdr *ip6;
244 	struct udphdr *uh;
245 	struct inpcb *in6p;
246 	struct  mbuf *opts = NULL;
247 	int off = *offp;
248 	int plen, ulen, ret = 0;
249 	boolean_t cell, wifi, wired;
250 	struct sockaddr_in6 udp_in6;
251 	struct inpcbinfo *pcbinfo = &udbinfo;
252 	struct sockaddr_in6 fromsa;
253 	u_int16_t pf_tag = 0;
254 	boolean_t is_wake_pkt = false;
255 
256 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), return IPPROTO_DONE);
257 
258 	/* Expect 32-bit aligned data pointer on strict-align platforms */
259 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
260 
261 	ifp = m->m_pkthdr.rcvif;
262 	ip6 = mtod(m, struct ip6_hdr *);
263 	cell = IFNET_IS_CELLULAR(ifp);
264 	wifi = (!cell && IFNET_IS_WIFI(ifp));
265 	wired = (!wifi && IFNET_IS_WIRED(ifp));
266 
267 	if (m->m_flags & M_PKTHDR) {
268 		pf_tag = m_pftag(m)->pftag_tag;
269 		if (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) {
270 			is_wake_pkt = true;
271 		}
272 	}
273 
274 	udpstat.udps_ipackets++;
275 
276 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
277 	uh = (struct udphdr *)(void *)((caddr_t)ip6 + off);
278 	ulen = ntohs((u_short)uh->uh_ulen);
279 
280 	if (plen != ulen) {
281 		udpstat.udps_badlen++;
282 		IF_UDP_STATINC(ifp, badlength);
283 		goto bad;
284 	}
285 
286 	/* destination port of 0 is illegal, based on RFC768. */
287 	if (uh->uh_dport == 0) {
288 		IF_UDP_STATINC(ifp, port0);
289 		goto bad;
290 	}
291 
292 	/*
293 	 * Checksum extended UDP header and data.
294 	 */
295 	if (udp6_input_checksum(m, uh, off, ulen)) {
296 		goto bad;
297 	}
298 
299 	/*
300 	 * Construct sockaddr format source address.
301 	 */
302 	init_sin6(&fromsa, m);
303 	fromsa.sin6_port = uh->uh_sport;
304 
305 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
306 		int reuse_sock = 0, mcast_delivered = 0;
307 		struct ip6_moptions *imo;
308 
309 		/*
310 		 * Deliver a multicast datagram to all sockets
311 		 * for which the local and remote addresses and ports match
312 		 * those of the incoming datagram.  This allows more than
313 		 * one process to receive multicasts on the same port.
314 		 * (This really ought to be done for unicast datagrams as
315 		 * well, but that would cause problems with existing
316 		 * applications that open both address-specific sockets and
317 		 * a wildcard socket listening to the same port -- they would
318 		 * end up receiving duplicates of every unicast datagram.
319 		 * Those applications open the multiple sockets to overcome an
320 		 * inadequacy of the UDP socket interface, but for backwards
321 		 * compatibility we avoid the problem here rather than
322 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
323 		 */
324 
325 		/*
326 		 * In a case that laddr should be set to the link-local
327 		 * address (this happens in RIPng), the multicast address
328 		 * specified in the received packet does not match with
329 		 * laddr. To cure this situation, the matching is relaxed
330 		 * if the receiving interface is the same as one specified
331 		 * in the socket and if the destination multicast address
332 		 * matches one of the multicast groups specified in the socket.
333 		 */
334 
335 		/*
336 		 * Construct sockaddr format source address.
337 		 */
338 		init_sin6(&udp_in6, m); /* general init */
339 		udp_in6.sin6_port = uh->uh_sport;
340 		/*
341 		 * KAME note: usually we drop udphdr from mbuf here.
342 		 * We need udphdr for IPsec processing so we do that later.
343 		 */
344 
345 		/*
346 		 * Locate pcb(s) for datagram.
347 		 * (Algorithm copied from raw_intr().)
348 		 */
349 		lck_rw_lock_shared(&pcbinfo->ipi_lock);
350 
351 		LIST_FOREACH(in6p, &udb, inp_list) {
352 #if IPSEC
353 			int skipit;
354 #endif /* IPSEC */
355 
356 			if ((in6p->inp_vflag & INP_IPV6) == 0) {
357 				continue;
358 			}
359 
360 			if (inp_restricted_recv(in6p, ifp)) {
361 				continue;
362 			}
363 			/*
364 			 * Skip unbound sockets before taking the lock on the socket as
365 			 * the test with the destination port in the header will fail
366 			 */
367 			if (in6p->in6p_lport == 0) {
368 				continue;
369 			}
370 
371 			if (in_pcb_checkstate(in6p, WNT_ACQUIRE, 0) ==
372 			    WNT_STOPUSING) {
373 				continue;
374 			}
375 
376 			udp_lock(in6p->in6p_socket, 1, 0);
377 
378 			if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) ==
379 			    WNT_STOPUSING) {
380 				udp_unlock(in6p->in6p_socket, 1, 0);
381 				continue;
382 			}
383 			if (in6p->in6p_lport != uh->uh_dport) {
384 				udp_unlock(in6p->in6p_socket, 1, 0);
385 				continue;
386 			}
387 
388 			/*
389 			 * Handle socket delivery policy for any-source
390 			 * and source-specific multicast. [RFC3678]
391 			 */
392 			imo = in6p->in6p_moptions;
393 			if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
394 				struct sockaddr_in6 mcaddr;
395 				int blocked;
396 
397 				IM6O_LOCK(imo);
398 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
399 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
400 				mcaddr.sin6_family = AF_INET6;
401 				mcaddr.sin6_addr = ip6->ip6_dst;
402 
403 				blocked = im6o_mc_filter(imo, ifp,
404 				    &mcaddr, &fromsa);
405 				IM6O_UNLOCK(imo);
406 				if (blocked != MCAST_PASS) {
407 					udp_unlock(in6p->in6p_socket, 1, 0);
408 					if (blocked == MCAST_NOTSMEMBER ||
409 					    blocked == MCAST_MUTED) {
410 						udpstat.udps_filtermcast++;
411 					}
412 					continue;
413 				}
414 			}
415 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
416 			    (!in6_are_addr_equal_scoped(&in6p->in6p_faddr,
417 			    &ip6->ip6_src, in6p->inp_fifscope, ifp->if_index) ||
418 			    in6p->in6p_fport != uh->uh_sport)) {
419 				udp_unlock(in6p->in6p_socket, 1, 0);
420 				continue;
421 			}
422 
423 			reuse_sock = in6p->inp_socket->so_options &
424 			    (SO_REUSEPORT | SO_REUSEADDR);
425 
426 #if NECP
427 			skipit = 0;
428 			if (!necp_socket_is_allowed_to_send_recv_v6(in6p,
429 			    uh->uh_dport, uh->uh_sport, &ip6->ip6_dst,
430 			    &ip6->ip6_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
431 				/* do not inject data to pcb */
432 				skipit = 1;
433 			}
434 			if (skipit == 0)
435 #endif /* NECP */
436 			{
437 				struct mbuf *n = NULL;
438 				/*
439 				 * KAME NOTE: do not
440 				 * m_copy(m, offset, ...) below.
441 				 * sbappendaddr() expects M_PKTHDR,
442 				 * and m_copy() will copy M_PKTHDR
443 				 * only if offset is 0.
444 				 */
445 				if (reuse_sock) {
446 					n = m_copy(m, 0, M_COPYALL);
447 				}
448 				udp6_append(in6p, ip6, &udp_in6, m,
449 				    off + sizeof(struct udphdr), ifp);
450 				mcast_delivered++;
451 				m = n;
452 			}
453 			if (is_wake_pkt) {
454 				soevent(in6p->in6p_socket,
455 				    SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
456 			}
457 			udp_unlock(in6p->in6p_socket, 1, 0);
458 
459 			/*
460 			 * Don't look for additional matches if this one does
461 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
462 			 * socket options set.  This heuristic avoids searching
463 			 * through all pcbs in the common case of a non-shared
464 			 * port.  It assumes that an application will never
465 			 * clear these options after setting them.
466 			 */
467 			if (reuse_sock == 0 || m == NULL) {
468 				break;
469 			}
470 
471 			/*
472 			 * Expect 32-bit aligned data pointer on strict-align
473 			 * platforms.
474 			 */
475 			MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
476 
477 			/*
478 			 * Recompute IP and UDP header pointers for new mbuf
479 			 */
480 			ip6 = mtod(m, struct ip6_hdr *);
481 			uh = (struct udphdr *)(void *)((caddr_t)ip6 + off);
482 		}
483 		lck_rw_done(&pcbinfo->ipi_lock);
484 
485 		if (mcast_delivered == 0) {
486 			/*
487 			 * No matching pcb found; discard datagram.
488 			 * (No need to send an ICMP Port Unreachable
489 			 * for a broadcast or multicast datgram.)
490 			 */
491 			udpstat.udps_noport++;
492 			udpstat.udps_noportmcast++;
493 			IF_UDP_STATINC(ifp, port_unreach);
494 			goto bad;
495 		}
496 
497 		/* free the extra copy of mbuf or skipped by NECP */
498 		if (m != NULL) {
499 			m_freem(m);
500 		}
501 		return IPPROTO_DONE;
502 	}
503 
504 #if IPSEC
505 	/*
506 	 * UDP to port 4500 with a payload where the first four bytes are
507 	 * not zero is a UDP encapsulated IPsec packet. Packets where
508 	 * the payload is one byte and that byte is 0xFF are NAT keepalive
509 	 * packets. Decapsulate the ESP packet and carry on with IPsec input
510 	 * or discard the NAT keep-alive.
511 	 */
512 	if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 &&
513 	    (uh->uh_dport == ntohs((u_short)esp_udp_encap_port) ||
514 	    uh->uh_sport == ntohs((u_short)esp_udp_encap_port))) {
515 		/*
516 		 * Check if ESP or keepalive:
517 		 *      1. If the destination port of the incoming packet is 4500.
518 		 *      2. If the source port of the incoming packet is 4500,
519 		 *         then check the SADB to match IP address and port.
520 		 */
521 		bool check_esp = true;
522 		if (uh->uh_dport != ntohs((u_short)esp_udp_encap_port)) {
523 			check_esp = key_checksa_present(AF_INET6, (caddr_t)&ip6->ip6_dst,
524 			    (caddr_t)&ip6->ip6_src, uh->uh_dport,
525 			    uh->uh_sport, ip6_input_getdstifscope(m), ip6_input_getsrcifscope(m));
526 		}
527 
528 		if (check_esp) {
529 			int payload_len = ulen - sizeof(struct udphdr) > 4 ? 4 :
530 			    ulen - sizeof(struct udphdr);
531 
532 			if (m->m_len < off + sizeof(struct udphdr) + payload_len) {
533 				if ((m = m_pullup(m, off + sizeof(struct udphdr) +
534 				    payload_len)) == NULL) {
535 					udpstat.udps_hdrops++;
536 					goto bad;
537 				}
538 				/*
539 				 * Expect 32-bit aligned data pointer on strict-align
540 				 * platforms.
541 				 */
542 				MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
543 
544 				ip6 = mtod(m, struct ip6_hdr *);
545 				uh = (struct udphdr *)(void *)((caddr_t)ip6 + off);
546 			}
547 			/* Check for NAT keepalive packet */
548 			if (payload_len == 1 && *(u_int8_t*)
549 			    ((caddr_t)uh + sizeof(struct udphdr)) == 0xFF) {
550 				goto bad;
551 			} else if (payload_len == 4 && *(u_int32_t*)(void *)
552 			    ((caddr_t)uh + sizeof(struct udphdr)) != 0) {
553 				/* UDP encapsulated IPsec packet to pass through NAT */
554 				/* preserve the udp header */
555 				*offp = off + sizeof(struct udphdr);
556 				return esp6_input(mp, offp, IPPROTO_UDP);
557 			}
558 		}
559 	}
560 #endif /* IPSEC */
561 
562 	/*
563 	 * Locate pcb for datagram.
564 	 */
565 	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, ip6_input_getsrcifscope(m),
566 	    &ip6->ip6_dst, uh->uh_dport, ip6_input_getdstifscope(m), 1, m->m_pkthdr.rcvif);
567 	if (in6p == NULL) {
568 		IF_UDP_STATINC(ifp, port_unreach);
569 
570 		if (udp_log_in_vain) {
571 			char buf[INET6_ADDRSTRLEN];
572 
573 			strlcpy(buf, ip6_sprintf(&ip6->ip6_dst), sizeof(buf));
574 			if (udp_log_in_vain < 3) {
575 				log(LOG_INFO, "Connection attempt to UDP "
576 				    "%s:%d from %s:%d\n", buf,
577 				    ntohs(uh->uh_dport),
578 				    ip6_sprintf(&ip6->ip6_src),
579 				    ntohs(uh->uh_sport));
580 			} else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
581 			    !in6_are_addr_equal_scoped(&ip6->ip6_dst, &ip6->ip6_src, ip6_input_getdstifscope(m), ip6_input_getsrcifscope(m))) {
582 				log(LOG_INFO, "Connection attempt "
583 				    "to UDP %s:%d from %s:%d\n", buf,
584 				    ntohs(uh->uh_dport),
585 				    ip6_sprintf(&ip6->ip6_src),
586 				    ntohs(uh->uh_sport));
587 			}
588 		}
589 		udpstat.udps_noport++;
590 		if (m->m_flags & M_MCAST) {
591 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
592 			udpstat.udps_noportmcast++;
593 			IF_UDP_STATINC(ifp, badmcast);
594 			goto bad;
595 		}
596 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
597 		return IPPROTO_DONE;
598 	}
599 
600 	/*
601 	 * Construct sockaddr format source address.
602 	 * Stuff source address and datagram in user buffer.
603 	 */
604 	udp_lock(in6p->in6p_socket, 1, 0);
605 
606 #if NECP
607 	if (!necp_socket_is_allowed_to_send_recv_v6(in6p, uh->uh_dport,
608 	    uh->uh_sport, &ip6->ip6_dst, &ip6->ip6_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
609 		in_pcb_checkstate(in6p, WNT_RELEASE, 1);
610 		udp_unlock(in6p->in6p_socket, 1, 0);
611 		IF_UDP_STATINC(ifp, badipsec);
612 		goto bad;
613 	}
614 #endif /* NECP */
615 
616 	if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) {
617 		udp_unlock(in6p->in6p_socket, 1, 0);
618 		IF_UDP_STATINC(ifp, cleanup);
619 		goto bad;
620 	}
621 
622 	init_sin6(&udp_in6, m); /* general init */
623 	udp_in6.sin6_port = uh->uh_sport;
624 	if ((in6p->in6p_flags & INP_CONTROLOPTS) != 0 ||
625 	    SOFLOW_ENABLED(in6p->in6p_socket) ||
626 	    SO_RECV_CONTROL_OPTS(in6p->in6p_socket)) {
627 		ret = ip6_savecontrol(in6p, m, &opts);
628 		if (ret != 0) {
629 			udp_unlock(in6p->in6p_socket, 1, 0);
630 			goto bad;
631 		}
632 	}
633 	m_adj(m, off + sizeof(struct udphdr));
634 	if (nstat_collect) {
635 		INP_ADD_STAT(in6p, cell, wifi, wired, rxpackets, 1);
636 		INP_ADD_STAT(in6p, cell, wifi, wired, rxbytes, m->m_pkthdr.len);
637 		inp_set_activity_bitmap(in6p);
638 	}
639 	so_recv_data_stat(in6p->in6p_socket, m, 0);
640 	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
641 	    (struct sockaddr *)&udp_in6, m, opts, NULL) == 0) {
642 		m = NULL;
643 		opts = NULL;
644 		udpstat.udps_fullsock++;
645 		udp_unlock(in6p->in6p_socket, 1, 0);
646 		goto bad;
647 	}
648 	if (is_wake_pkt) {
649 		soevent(in6p->in6p_socket, SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
650 	}
651 	sorwakeup(in6p->in6p_socket);
652 	udp_unlock(in6p->in6p_socket, 1, 0);
653 	return IPPROTO_DONE;
654 bad:
655 	if (m != NULL) {
656 		m_freem(m);
657 	}
658 	if (opts != NULL) {
659 		m_freem(opts);
660 	}
661 	return IPPROTO_DONE;
662 }
663 
664 void
udp6_ctlinput(int cmd,struct sockaddr * sa,void * d,__unused struct ifnet * ifp)665 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d, __unused struct ifnet *ifp)
666 {
667 	struct udphdr uh;
668 	struct ip6_hdr *ip6;
669 	struct mbuf *m;
670 	int off = 0;
671 	struct ip6ctlparam *ip6cp = NULL;
672 	struct icmp6_hdr *icmp6 = NULL;
673 	const struct sockaddr_in6 *sa6_src = NULL;
674 	void *cmdarg = NULL;
675 	void (*notify)(struct inpcb *, int) = udp_notify;
676 	struct inpcb *in6p;
677 	struct udp_portonly {
678 		u_int16_t uh_sport;
679 		u_int16_t uh_dport;
680 	} *uhp;
681 
682 	if (sa->sa_family != AF_INET6 ||
683 	    sa->sa_len != sizeof(struct sockaddr_in6)) {
684 		return;
685 	}
686 
687 	if ((unsigned)cmd >= PRC_NCMDS) {
688 		return;
689 	}
690 	if (PRC_IS_REDIRECT(cmd)) {
691 		notify = in6_rtchange;
692 		d = NULL;
693 	} else if (cmd == PRC_HOSTDEAD) {
694 		d = NULL;
695 	} else if (inet6ctlerrmap[cmd] == 0) {
696 		return;
697 	}
698 
699 	/* if the parameter is from icmp6, decode it. */
700 	if (d != NULL) {
701 		ip6cp = (struct ip6ctlparam *)d;
702 		icmp6 = ip6cp->ip6c_icmp6;
703 		m = ip6cp->ip6c_m;
704 		ip6 = ip6cp->ip6c_ip6;
705 		off = ip6cp->ip6c_off;
706 		cmdarg = ip6cp->ip6c_cmdarg;
707 		sa6_src = ip6cp->ip6c_src;
708 	} else {
709 		m = NULL;
710 		ip6 = NULL;
711 		cmdarg = NULL;
712 		sa6_src = &sa6_any;
713 	}
714 
715 	if (ip6 != NULL) {
716 #if SKYWALK
717 		union sockaddr_in_4_6 sock_laddr;
718 		struct protoctl_ev_val prctl_ev_val;
719 #endif /* SKYWALK */
720 		/*
721 		 * XXX: We assume that when IPV6 is non NULL,
722 		 * M and OFF are valid.
723 		 */
724 		/* check if we can safely examine src and dst ports */
725 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
726 			return;
727 		}
728 
729 		bzero(&uh, sizeof(uh));
730 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
731 
732 		in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_dst, uh.uh_dport, ip6_input_getdstifscope(m),
733 		    &ip6->ip6_src, uh.uh_sport, ip6_input_getsrcifscope(m), 0, NULL);
734 		if (cmd == PRC_MSGSIZE && in6p != NULL && !uuid_is_null(in6p->necp_client_uuid)) {
735 			uuid_t null_uuid;
736 			uuid_clear(null_uuid);
737 			necp_update_flow_protoctl_event(null_uuid, in6p->necp_client_uuid,
738 			    PRC_MSGSIZE, ntohl(icmp6->icmp6_mtu), 0);
739 			/*
740 			 * Avoid setting so_error when using Network.framework
741 			 * since the notification of PRC_MSGSIZE has been delivered
742 			 * through NECP.
743 			 */
744 			in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
745 			    (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport,
746 			    cmd, cmdarg, NULL);
747 		} else {
748 			in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
749 			    (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport,
750 			    cmd, cmdarg, notify);
751 		}
752 #if SKYWALK
753 		bzero(&prctl_ev_val, sizeof(prctl_ev_val));
754 		bzero(&sock_laddr, sizeof(sock_laddr));
755 
756 		if (cmd == PRC_MSGSIZE && icmp6 != NULL) {
757 			prctl_ev_val.val = ntohl(icmp6->icmp6_mtu);
758 		}
759 		sock_laddr.sin6.sin6_family = AF_INET6;
760 		sock_laddr.sin6.sin6_len = sizeof(sock_laddr.sin6);
761 		sock_laddr.sin6.sin6_addr = ip6->ip6_src;
762 
763 		protoctl_event_enqueue_nwk_wq_entry(ifp,
764 		    (struct sockaddr *)&sock_laddr, sa,
765 		    uh.uh_sport, uh.uh_dport, IPPROTO_UDP,
766 		    cmd, &prctl_ev_val);
767 #endif /* SKYWALK */
768 	}
769 	/*
770 	 * XXX The else condition here was broken for a long time.
771 	 * Fixing it made us deliver notification correctly but broke
772 	 * some frameworks that didn't handle it well.
773 	 * For now we have removed it and will revisit it later.
774 	 */
775 }
776 
777 static int
udp6_abort(struct socket * so)778 udp6_abort(struct socket *so)
779 {
780 	struct inpcb *inp;
781 
782 	inp = sotoinpcb(so);
783 	if (inp == NULL) {
784 		panic("%s: so=%p null inp", __func__, so);
785 		/* NOTREACHED */
786 	}
787 	soisdisconnected(so);
788 	in6_pcbdetach(inp);
789 	return 0;
790 }
791 
792 static int
udp6_attach(struct socket * so,int proto,struct proc * p)793 udp6_attach(struct socket *so, int proto, struct proc *p)
794 {
795 #pragma unused(proto)
796 	struct inpcb *inp;
797 	int error;
798 
799 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
800 		error = soreserve(so, udp_sendspace, udp_recvspace);
801 		if (error) {
802 			return error;
803 		}
804 	}
805 
806 	inp = sotoinpcb(so);
807 	if (inp != NULL) {
808 		return EINVAL;
809 	}
810 
811 	error = in_pcballoc(so, &udbinfo, p);
812 	if (error) {
813 		return error;
814 	}
815 
816 	inp = (struct inpcb *)so->so_pcb;
817 	inp->inp_vflag |= INP_IPV6;
818 	if (ip6_mapped_addr_on) {
819 		inp->inp_vflag |= INP_IPV4;
820 	}
821 	inp->in6p_hops = -1;    /* use kernel default */
822 	inp->in6p_cksum = -1;   /* just to be sure */
823 	/*
824 	 * XXX: ugly!!
825 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
826 	 * because the socket may be bound to an IPv6 wildcard address,
827 	 * which may match an IPv4-mapped IPv6 address.
828 	 */
829 	inp->inp_ip_ttl = (u_char)ip_defttl;
830 	if (nstat_collect) {
831 		nstat_udp_new_pcb(inp);
832 	}
833 	return 0;
834 }
835 
836 static int
udp6_bind(struct socket * so,struct sockaddr * nam,struct proc * p)837 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
838 {
839 	struct inpcb *inp;
840 	int error;
841 
842 	inp = sotoinpcb(so);
843 	if (inp == NULL) {
844 		return EINVAL;
845 	}
846 
847 	const uint8_t old_flags = inp->inp_vflag;
848 	inp->inp_vflag &= ~INP_IPV4;
849 	inp->inp_vflag |= INP_IPV6;
850 
851 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
852 		struct sockaddr_in6 *sin6_p;
853 
854 		sin6_p = (struct sockaddr_in6 *)(void *)nam;
855 
856 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
857 			inp->inp_vflag |= INP_IPV4;
858 			inp->inp_vflag &= ~INP_V4MAPPEDV6;
859 		} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
860 			struct sockaddr_in sin;
861 
862 			in6_sin6_2_sin(&sin, sin6_p);
863 			inp->inp_vflag |= INP_IPV4;
864 			inp->inp_vflag &= ~INP_IPV6;
865 			inp->inp_vflag |= INP_V4MAPPEDV6;
866 
867 			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
868 			if (error != 0) {
869 				inp->inp_vflag = old_flags;
870 			}
871 			return error;
872 		}
873 	}
874 
875 	error = in6_pcbbind(inp, nam, p);
876 	if (error != 0) {
877 		inp->inp_vflag = old_flags;
878 	}
879 	return error;
880 }
881 
882 int
udp6_connect(struct socket * so,struct sockaddr * nam,struct proc * p)883 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
884 {
885 	struct inpcb *inp;
886 	int error;
887 	struct sockaddr_in6 *sin6_p = (struct sockaddr_in6 *)(void *)nam;
888 
889 #if defined(NECP) && defined(FLOW_DIVERT)
890 	int should_use_flow_divert = 0;
891 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
892 
893 	inp = sotoinpcb(so);
894 	if (inp == NULL) {
895 		return EINVAL;
896 	}
897 
898 #if defined(NECP) && defined(FLOW_DIVERT)
899 	should_use_flow_divert = necp_socket_should_use_flow_divert(inp);
900 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
901 
902 	/*
903 	 * It is possible that the socket is bound to v4 mapped v6 address.
904 	 * Post that do not allow connect to a v6 endpoint.
905 	 */
906 	if (inp->inp_vflag & INP_V4MAPPEDV6 &&
907 	    !IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
908 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
909 			sin6_p->sin6_addr.s6_addr[10] = 0xff;
910 			sin6_p->sin6_addr.s6_addr[11] = 0xff;
911 		} else {
912 			return EINVAL;
913 		}
914 	}
915 
916 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
917 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
918 			struct sockaddr_in sin;
919 			const uint8_t old_flags = inp->inp_vflag;
920 
921 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
922 				return EISCONN;
923 			}
924 
925 			if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
926 				so->so_flags1 |= SOF1_CONNECT_COUNTED;
927 				INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected);
928 			}
929 
930 			in6_sin6_2_sin(&sin, sin6_p);
931 #if defined(NECP) && defined(FLOW_DIVERT)
932 			if (should_use_flow_divert) {
933 				goto do_flow_divert;
934 			}
935 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
936 			inp->inp_vflag |= INP_IPV4;
937 			inp->inp_vflag &= ~INP_IPV6;
938 			inp->inp_vflag |= INP_V4MAPPEDV6;
939 
940 			error = in_pcbconnect(inp, (struct sockaddr *)&sin,
941 			    p, IFSCOPE_NONE, NULL);
942 			if (error == 0) {
943 #if NECP
944 				/* Update NECP client with connected five-tuple */
945 				if (!uuid_is_null(inp->necp_client_uuid)) {
946 					socket_unlock(so, 0);
947 					necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
948 					socket_lock(so, 0);
949 				}
950 #endif /* NECP */
951 				soisconnected(so);
952 			} else {
953 				inp->inp_vflag = old_flags;
954 			}
955 			return error;
956 		}
957 	}
958 
959 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
960 		return EISCONN;
961 	}
962 
963 	if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
964 		so->so_flags1 |= SOF1_CONNECT_COUNTED;
965 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
966 	}
967 
968 #if defined(NECP) && defined(FLOW_DIVERT)
969 do_flow_divert:
970 	if (should_use_flow_divert) {
971 		error = flow_divert_pcb_init(so);
972 		if (error == 0) {
973 			error = flow_divert_connect_out(so, nam, p);
974 		}
975 		return error;
976 	}
977 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
978 
979 	error = in6_pcbconnect(inp, nam, p);
980 	if (error == 0) {
981 		/* should be non mapped addr */
982 		if (ip6_mapped_addr_on ||
983 		    (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
984 			inp->inp_vflag &= ~INP_IPV4;
985 			inp->inp_vflag |= INP_IPV6;
986 		}
987 #if NECP
988 		/* Update NECP client with connected five-tuple */
989 		if (!uuid_is_null(inp->necp_client_uuid)) {
990 			socket_unlock(so, 0);
991 			necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
992 			socket_lock(so, 0);
993 		}
994 #endif /* NECP */
995 		soisconnected(so);
996 		if (inp->inp_flowhash == 0) {
997 			inp_calc_flowhash(inp);
998 			ASSERT(inp->inp_flowhash != 0);
999 		}
1000 		/* update flowinfo - RFC 6437 */
1001 		if (inp->inp_flow == 0 &&
1002 		    inp->in6p_flags & IN6P_AUTOFLOWLABEL) {
1003 			inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1004 			inp->inp_flow |=
1005 			    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1006 		}
1007 	}
1008 	return error;
1009 }
1010 
1011 static int
udp6_connectx(struct socket * so,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * uio,user_ssize_t * bytes_written)1012 udp6_connectx(struct socket *so, struct sockaddr *src,
1013     struct sockaddr *dst, struct proc *p, uint32_t ifscope,
1014     sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
1015     uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
1016 {
1017 	return udp_connectx_common(so, AF_INET6, src, dst,
1018 	           p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written);
1019 }
1020 
1021 static int
udp6_detach(struct socket * so)1022 udp6_detach(struct socket *so)
1023 {
1024 	struct inpcb *inp;
1025 
1026 	inp = sotoinpcb(so);
1027 	if (inp == NULL) {
1028 		return EINVAL;
1029 	}
1030 	in6_pcbdetach(inp);
1031 	return 0;
1032 }
1033 
1034 static int
udp6_disconnect(struct socket * so)1035 udp6_disconnect(struct socket *so)
1036 {
1037 	struct inpcb *inp;
1038 
1039 	inp = sotoinpcb(so);
1040 	if (inp == NULL
1041 #if NECP
1042 	    || (necp_socket_should_use_flow_divert(inp))
1043 #endif /* NECP */
1044 	    ) {
1045 		return inp == NULL ? EINVAL : EPROTOTYPE;
1046 	}
1047 
1048 	if (inp->inp_vflag & INP_IPV4) {
1049 		struct pr_usrreqs *pru;
1050 
1051 		pru = ip_protox[IPPROTO_UDP]->pr_usrreqs;
1052 		return (*pru->pru_disconnect)(so);
1053 	}
1054 
1055 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1056 		return ENOTCONN;
1057 	}
1058 
1059 	in6_pcbdisconnect(inp);
1060 
1061 	/* reset flow-controlled state, just in case */
1062 	inp_reset_fc_state(inp);
1063 
1064 	inp->in6p_laddr = in6addr_any;
1065 	inp->inp_lifscope = IFSCOPE_NONE;
1066 	inp->in6p_last_outifp = NULL;
1067 #if SKYWALK
1068 	if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1069 		netns_set_ifnet(&inp->inp_netns_token, NULL);
1070 	}
1071 #endif /* SKYWALK */
1072 
1073 	so->so_state &= ~SS_ISCONNECTED;                /* XXX */
1074 	return 0;
1075 }
1076 
1077 static int
udp6_disconnectx(struct socket * so,sae_associd_t aid,sae_connid_t cid)1078 udp6_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
1079 {
1080 #pragma unused(cid)
1081 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
1082 		return EINVAL;
1083 	}
1084 
1085 	return udp6_disconnect(so);
1086 }
1087 
1088 static int
udp6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)1089 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1090     struct mbuf *control, struct proc *p)
1091 {
1092 	struct inpcb *inp;
1093 	int error = 0;
1094 #if defined(NECP) && defined(FLOW_DIVERT)
1095 	int should_use_flow_divert = 0;
1096 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1097 #if CONTENT_FILTER
1098 	struct m_tag *cfil_tag = NULL;
1099 	struct sockaddr *cfil_faddr = NULL;
1100 #endif
1101 
1102 	inp = sotoinpcb(so);
1103 	if (inp == NULL) {
1104 		error = EINVAL;
1105 		goto bad;
1106 	}
1107 
1108 #if CONTENT_FILTER
1109 	//If socket is subject to UDP Content Filter and unconnected, get addr from tag.
1110 	if (CFIL_DGRAM_FILTERED(so) && !addr && IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1111 		cfil_tag = cfil_dgram_get_socket_state(m, NULL, NULL, &cfil_faddr, NULL);
1112 		if (cfil_tag) {
1113 			addr = (struct sockaddr *)cfil_faddr;
1114 		}
1115 	}
1116 #endif
1117 
1118 #if defined(NECP) && defined(FLOW_DIVERT)
1119 	should_use_flow_divert = necp_socket_should_use_flow_divert(inp);
1120 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1121 
1122 	if (addr != NULL) {
1123 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1124 			error = EINVAL;
1125 			goto bad;
1126 		}
1127 		if (addr->sa_family != AF_INET6) {
1128 			error = EAFNOSUPPORT;
1129 			goto bad;
1130 		}
1131 	}
1132 
1133 	if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1134 		int hasv4addr;
1135 		struct sockaddr_in6 *sin6 = NULL;
1136 
1137 		if (addr == NULL) {
1138 			hasv4addr = (inp->inp_vflag & INP_IPV4);
1139 		} else {
1140 			sin6 = (struct sockaddr_in6 *)(void *)addr;
1141 			hasv4addr =
1142 			    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 1 : 0;
1143 		}
1144 		if (hasv4addr) {
1145 			struct pr_usrreqs *pru;
1146 
1147 			if (sin6 != NULL) {
1148 				in6_sin6_2_sin_in_sock(addr);
1149 			}
1150 #if defined(NECP) && defined(FLOW_DIVERT)
1151 			if (should_use_flow_divert) {
1152 				goto do_flow_divert;
1153 			}
1154 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1155 			pru = ip_protox[IPPROTO_UDP]->pr_usrreqs;
1156 			error = ((*pru->pru_send)(so, flags, m, addr,
1157 			    control, p));
1158 #if CONTENT_FILTER
1159 			if (cfil_tag) {
1160 				m_tag_free(cfil_tag);
1161 			}
1162 #endif
1163 			/* addr will just be freed in sendit(). */
1164 			return error;
1165 		}
1166 	}
1167 
1168 #if defined(NECP) && defined(FLOW_DIVERT)
1169 do_flow_divert:
1170 	if (should_use_flow_divert) {
1171 		/* Implicit connect */
1172 		error = flow_divert_implicit_data_out(so, flags, m, addr, control, p);
1173 #if CONTENT_FILTER
1174 		if (cfil_tag) {
1175 			m_tag_free(cfil_tag);
1176 		}
1177 #endif
1178 		return error;
1179 	}
1180 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1181 
1182 #if SKYWALK
1183 	sk_protect_t protect = sk_async_transmit_protect();
1184 #endif /* SKYWALK */
1185 	error = udp6_output(inp, m, addr, control, p);
1186 #if SKYWALK
1187 	sk_async_transmit_unprotect(protect);
1188 #endif /* SKYWALK */
1189 
1190 #if CONTENT_FILTER
1191 	if (cfil_tag) {
1192 		m_tag_free(cfil_tag);
1193 	}
1194 #endif
1195 	return error;
1196 
1197 bad:
1198 	VERIFY(error != 0);
1199 
1200 	if (m != NULL) {
1201 		m_freem(m);
1202 	}
1203 	if (control != NULL) {
1204 		m_freem(control);
1205 	}
1206 #if CONTENT_FILTER
1207 	if (cfil_tag) {
1208 		m_tag_free(cfil_tag);
1209 	}
1210 #endif
1211 	return error;
1212 }
1213 
1214 /*
1215  * Checksum extended UDP header and data.
1216  */
1217 static int
udp6_input_checksum(struct mbuf * m,struct udphdr * uh,int off,int ulen)1218 udp6_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
1219 {
1220 	struct ifnet *ifp = m->m_pkthdr.rcvif;
1221 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1222 
1223 	if (!(m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
1224 	    uh->uh_sum == 0) {
1225 		/* UDP/IPv6 checksum is mandatory (RFC2460) */
1226 
1227 		/*
1228 		 * If checksum was already validated, ignore this check.
1229 		 * This is necessary for transport-mode ESP, which may be
1230 		 * getting UDP payloads without checksums when the network
1231 		 * has a NAT64.
1232 		 */
1233 		udpstat.udps_nosum++;
1234 		goto badsum;
1235 	}
1236 
1237 	if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
1238 	    (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
1239 	    (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
1240 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
1241 			uh->uh_sum = m->m_pkthdr.csum_rx_val;
1242 		} else {
1243 			uint32_t sum = m->m_pkthdr.csum_rx_val;
1244 			uint32_t start = m->m_pkthdr.csum_rx_start;
1245 			int32_t trailer = (m_pktlen(m) - (off + ulen));
1246 
1247 			/*
1248 			 * Perform 1's complement adjustment of octets
1249 			 * that got included/excluded in the hardware-
1250 			 * calculated checksum value.  Also take care
1251 			 * of any trailing bytes and subtract out
1252 			 * their partial sum.
1253 			 */
1254 			ASSERT(trailer >= 0);
1255 			if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
1256 			    (start != off || trailer != 0)) {
1257 				uint32_t swbytes = (uint32_t)trailer;
1258 				uint16_t s = 0, d = 0;
1259 
1260 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
1261 					s = ip6->ip6_src.s6_addr16[1];
1262 					ip6->ip6_src.s6_addr16[1] = 0;
1263 				}
1264 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1265 					d = ip6->ip6_dst.s6_addr16[1];
1266 					ip6->ip6_dst.s6_addr16[1] = 0;
1267 				}
1268 
1269 				/* callee folds in sum */
1270 				sum = m_adj_sum16(m, start, off, ulen, sum);
1271 				if (off > start) {
1272 					swbytes += (off - start);
1273 				} else {
1274 					swbytes += (start - off);
1275 				}
1276 
1277 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
1278 					ip6->ip6_src.s6_addr16[1] = s;
1279 				}
1280 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1281 					ip6->ip6_dst.s6_addr16[1] = d;
1282 				}
1283 
1284 				if (swbytes != 0) {
1285 					udp_in_cksum_stats(swbytes);
1286 				}
1287 				if (trailer != 0) {
1288 					m_adj(m, -trailer);
1289 				}
1290 			}
1291 
1292 			uh->uh_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst,
1293 			    sum + htonl(ulen + IPPROTO_UDP));
1294 		}
1295 		uh->uh_sum ^= 0xffff;
1296 	} else {
1297 		udp_in6_cksum_stats(ulen);
1298 		uh->uh_sum = in6_cksum(m, IPPROTO_UDP, off, ulen);
1299 	}
1300 
1301 	if (uh->uh_sum != 0) {
1302 badsum:
1303 		udpstat.udps_badsum++;
1304 		IF_UDP_STATINC(ifp, badchksum);
1305 		return -1;
1306 	}
1307 
1308 	return 0;
1309 }
1310 
1311 int
udp6_defunct(struct socket * so)1312 udp6_defunct(struct socket *so)
1313 {
1314 	struct ip_moptions *imo;
1315 	struct ip6_moptions *im6o;
1316 	struct inpcb *inp;
1317 
1318 	inp = sotoinpcb(so);
1319 	if (inp == NULL) {
1320 		return EINVAL;
1321 	}
1322 
1323 	im6o = inp->in6p_moptions;
1324 	inp->in6p_moptions = NULL;
1325 	if (im6o != NULL) {
1326 		struct proc *p = current_proc();
1327 
1328 		SODEFUNCTLOG("%s[%d, %s]: defuncting so 0x%llu drop ipv6 multicast memberships",
1329 		    __func__, proc_pid(p), proc_best_name(p),
1330 		    so->so_gencnt);
1331 		IM6O_REMREF(im6o);
1332 	}
1333 	imo = inp->inp_moptions;
1334 	if (imo != NULL) {
1335 		struct proc *p = current_proc();
1336 
1337 		SODEFUNCTLOG("%s[%d, %s]: defuncting so 0x%llu drop ipv4 multicast memberships",
1338 		    __func__, proc_pid(p), proc_best_name(p),
1339 		    so->so_gencnt);
1340 
1341 		inp->inp_moptions = NULL;
1342 
1343 		IMO_REMREF(imo);
1344 	}
1345 
1346 	return 0;
1347 }
1348