xref: /xnu-8792.41.9/bsd/netinet6/udp6_usrreq.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
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 
177 struct pr_usrreqs udp6_usrreqs = {
178 	.pru_abort =            udp6_abort,
179 	.pru_attach =           udp6_attach,
180 	.pru_bind =             udp6_bind,
181 	.pru_connect =          udp6_connect,
182 	.pru_connectx =         udp6_connectx,
183 	.pru_control =          in6_control,
184 	.pru_detach =           udp6_detach,
185 	.pru_disconnect =       udp6_disconnect,
186 	.pru_disconnectx =      udp6_disconnectx,
187 	.pru_peeraddr =         in6_mapped_peeraddr,
188 	.pru_send =             udp6_send,
189 	.pru_shutdown =         udp_shutdown,
190 	.pru_sockaddr =         in6_mapped_sockaddr,
191 	.pru_sosend =           sosend,
192 	.pru_soreceive =        soreceive,
193 	.pru_soreceive_list =   soreceive_list,
194 };
195 
196 /*
197  * subroutine of udp6_input(), mainly for source code readability.
198  */
199 static void
udp6_append(struct inpcb * last,struct ip6_hdr * ip6,struct sockaddr_in6 * udp_in6,struct mbuf * n,int off,struct ifnet * ifp)200 udp6_append(struct inpcb *last, struct ip6_hdr *ip6,
201     struct sockaddr_in6 *udp_in6, struct mbuf *n, int off, struct ifnet *ifp)
202 {
203 #pragma unused(ip6)
204 	struct  mbuf *opts = NULL;
205 	int ret = 0;
206 	boolean_t cell = IFNET_IS_CELLULAR(ifp);
207 	boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
208 	boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
209 
210 	if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
211 	    SOFLOW_ENABLED(last->in6p_socket) ||
212 	    SO_RECV_CONTROL_OPTS(last->in6p_socket)) {
213 		ret = ip6_savecontrol(last, n, &opts);
214 		if (ret != 0) {
215 			m_freem(n);
216 			m_freem(opts);
217 			return;
218 		}
219 	}
220 	m_adj(n, off);
221 	if (nstat_collect) {
222 		INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1);
223 		INP_ADD_STAT(last, cell, wifi, wired, rxbytes, n->m_pkthdr.len);
224 		inp_set_activity_bitmap(last);
225 	}
226 	so_recv_data_stat(last->in6p_socket, n, 0);
227 	if (sbappendaddr(&last->in6p_socket->so_rcv,
228 	    (struct sockaddr *)udp_in6, n, opts, NULL) == 0) {
229 		udpstat.udps_fullsock++;
230 	} else {
231 		sorwakeup(last->in6p_socket);
232 	}
233 }
234 
235 int
udp6_input(struct mbuf ** mp,int * offp,int proto)236 udp6_input(struct mbuf **mp, int *offp, int proto)
237 {
238 #pragma unused(proto)
239 	struct mbuf *m = *mp;
240 	struct ifnet *ifp;
241 	struct ip6_hdr *ip6;
242 	struct udphdr *uh;
243 	struct inpcb *in6p;
244 	struct  mbuf *opts = NULL;
245 	int off = *offp;
246 	int plen, ulen, ret = 0;
247 	boolean_t cell, wifi, wired;
248 	struct sockaddr_in6 udp_in6;
249 	struct inpcbinfo *pcbinfo = &udbinfo;
250 	struct sockaddr_in6 fromsa;
251 	u_int16_t pf_tag = 0;
252 	boolean_t is_wake_pkt = false;
253 
254 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), return IPPROTO_DONE);
255 
256 	/* Expect 32-bit aligned data pointer on strict-align platforms */
257 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
258 
259 	ifp = m->m_pkthdr.rcvif;
260 	ip6 = mtod(m, struct ip6_hdr *);
261 	cell = IFNET_IS_CELLULAR(ifp);
262 	wifi = (!cell && IFNET_IS_WIFI(ifp));
263 	wired = (!wifi && IFNET_IS_WIRED(ifp));
264 
265 	if (m->m_flags & M_PKTHDR) {
266 		pf_tag = m_pftag(m)->pftag_tag;
267 		if (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) {
268 			is_wake_pkt = true;
269 		}
270 	}
271 
272 	udpstat.udps_ipackets++;
273 
274 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
275 	uh = (struct udphdr *)(void *)((caddr_t)ip6 + off);
276 	ulen = ntohs((u_short)uh->uh_ulen);
277 
278 	if (plen != ulen) {
279 		udpstat.udps_badlen++;
280 		IF_UDP_STATINC(ifp, badlength);
281 		goto bad;
282 	}
283 
284 	/* destination port of 0 is illegal, based on RFC768. */
285 	if (uh->uh_dport == 0) {
286 		IF_UDP_STATINC(ifp, port0);
287 		goto bad;
288 	}
289 
290 	/*
291 	 * Checksum extended UDP header and data.
292 	 */
293 	if (udp6_input_checksum(m, uh, off, ulen)) {
294 		goto bad;
295 	}
296 
297 	/*
298 	 * Construct sockaddr format source address.
299 	 */
300 	init_sin6(&fromsa, m);
301 	fromsa.sin6_port = uh->uh_sport;
302 
303 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
304 		int reuse_sock = 0, mcast_delivered = 0;
305 		struct ip6_moptions *imo;
306 
307 		/*
308 		 * Deliver a multicast datagram to all sockets
309 		 * for which the local and remote addresses and ports match
310 		 * those of the incoming datagram.  This allows more than
311 		 * one process to receive multicasts on the same port.
312 		 * (This really ought to be done for unicast datagrams as
313 		 * well, but that would cause problems with existing
314 		 * applications that open both address-specific sockets and
315 		 * a wildcard socket listening to the same port -- they would
316 		 * end up receiving duplicates of every unicast datagram.
317 		 * Those applications open the multiple sockets to overcome an
318 		 * inadequacy of the UDP socket interface, but for backwards
319 		 * compatibility we avoid the problem here rather than
320 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
321 		 */
322 
323 		/*
324 		 * In a case that laddr should be set to the link-local
325 		 * address (this happens in RIPng), the multicast address
326 		 * specified in the received packet does not match with
327 		 * laddr. To cure this situation, the matching is relaxed
328 		 * if the receiving interface is the same as one specified
329 		 * in the socket and if the destination multicast address
330 		 * matches one of the multicast groups specified in the socket.
331 		 */
332 
333 		/*
334 		 * Construct sockaddr format source address.
335 		 */
336 		init_sin6(&udp_in6, m); /* general init */
337 		udp_in6.sin6_port = uh->uh_sport;
338 		/*
339 		 * KAME note: usually we drop udphdr from mbuf here.
340 		 * We need udphdr for IPsec processing so we do that later.
341 		 */
342 
343 		/*
344 		 * Locate pcb(s) for datagram.
345 		 * (Algorithm copied from raw_intr().)
346 		 */
347 		lck_rw_lock_shared(&pcbinfo->ipi_lock);
348 
349 		LIST_FOREACH(in6p, &udb, inp_list) {
350 #if IPSEC
351 			int skipit;
352 #endif /* IPSEC */
353 
354 			if ((in6p->inp_vflag & INP_IPV6) == 0) {
355 				continue;
356 			}
357 
358 			if (inp_restricted_recv(in6p, ifp)) {
359 				continue;
360 			}
361 			/*
362 			 * Skip unbound sockets before taking the lock on the socket as
363 			 * the test with the destination port in the header will fail
364 			 */
365 			if (in6p->in6p_lport == 0) {
366 				continue;
367 			}
368 
369 			if (in_pcb_checkstate(in6p, WNT_ACQUIRE, 0) ==
370 			    WNT_STOPUSING) {
371 				continue;
372 			}
373 
374 			udp_lock(in6p->in6p_socket, 1, 0);
375 
376 			if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) ==
377 			    WNT_STOPUSING) {
378 				udp_unlock(in6p->in6p_socket, 1, 0);
379 				continue;
380 			}
381 			if (in6p->in6p_lport != uh->uh_dport) {
382 				udp_unlock(in6p->in6p_socket, 1, 0);
383 				continue;
384 			}
385 
386 			/*
387 			 * Handle socket delivery policy for any-source
388 			 * and source-specific multicast. [RFC3678]
389 			 */
390 			imo = in6p->in6p_moptions;
391 			if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
392 				struct sockaddr_in6 mcaddr;
393 				int blocked;
394 
395 				IM6O_LOCK(imo);
396 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
397 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
398 				mcaddr.sin6_family = AF_INET6;
399 				mcaddr.sin6_addr = ip6->ip6_dst;
400 
401 				blocked = im6o_mc_filter(imo, ifp,
402 				    &mcaddr, &fromsa);
403 				IM6O_UNLOCK(imo);
404 				if (blocked != MCAST_PASS) {
405 					udp_unlock(in6p->in6p_socket, 1, 0);
406 					if (blocked == MCAST_NOTSMEMBER ||
407 					    blocked == MCAST_MUTED) {
408 						udpstat.udps_filtermcast++;
409 					}
410 					continue;
411 				}
412 			}
413 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
414 			    (!in6_are_addr_equal_scoped(&in6p->in6p_faddr,
415 			    &ip6->ip6_src, in6p->inp_fifscope, ifp->if_index) ||
416 			    in6p->in6p_fport != uh->uh_sport)) {
417 				udp_unlock(in6p->in6p_socket, 1, 0);
418 				continue;
419 			}
420 
421 			reuse_sock = in6p->inp_socket->so_options &
422 			    (SO_REUSEPORT | SO_REUSEADDR);
423 
424 #if NECP
425 			skipit = 0;
426 			if (!necp_socket_is_allowed_to_send_recv_v6(in6p,
427 			    uh->uh_dport, uh->uh_sport, &ip6->ip6_dst,
428 			    &ip6->ip6_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
429 				/* do not inject data to pcb */
430 				skipit = 1;
431 			}
432 			if (skipit == 0)
433 #endif /* NECP */
434 			{
435 				struct mbuf *n = NULL;
436 				/*
437 				 * KAME NOTE: do not
438 				 * m_copy(m, offset, ...) below.
439 				 * sbappendaddr() expects M_PKTHDR,
440 				 * and m_copy() will copy M_PKTHDR
441 				 * only if offset is 0.
442 				 */
443 				if (reuse_sock) {
444 					n = m_copy(m, 0, M_COPYALL);
445 				}
446 				udp6_append(in6p, ip6, &udp_in6, m,
447 				    off + sizeof(struct udphdr), ifp);
448 				mcast_delivered++;
449 				m = n;
450 			}
451 			if (is_wake_pkt) {
452 				soevent(in6p->in6p_socket,
453 				    SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
454 			}
455 			udp_unlock(in6p->in6p_socket, 1, 0);
456 
457 			/*
458 			 * Don't look for additional matches if this one does
459 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
460 			 * socket options set.  This heuristic avoids searching
461 			 * through all pcbs in the common case of a non-shared
462 			 * port.  It assumes that an application will never
463 			 * clear these options after setting them.
464 			 */
465 			if (reuse_sock == 0 || m == NULL) {
466 				break;
467 			}
468 
469 			/*
470 			 * Expect 32-bit aligned data pointer on strict-align
471 			 * platforms.
472 			 */
473 			MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
474 
475 			/*
476 			 * Recompute IP and UDP header pointers for new mbuf
477 			 */
478 			ip6 = mtod(m, struct ip6_hdr *);
479 			uh = (struct udphdr *)(void *)((caddr_t)ip6 + off);
480 		}
481 		lck_rw_done(&pcbinfo->ipi_lock);
482 
483 		if (mcast_delivered == 0) {
484 			/*
485 			 * No matching pcb found; discard datagram.
486 			 * (No need to send an ICMP Port Unreachable
487 			 * for a broadcast or multicast datgram.)
488 			 */
489 			udpstat.udps_noport++;
490 			udpstat.udps_noportmcast++;
491 			IF_UDP_STATINC(ifp, port_unreach);
492 			goto bad;
493 		}
494 
495 		/* free the extra copy of mbuf or skipped by NECP */
496 		if (m != NULL) {
497 			m_freem(m);
498 		}
499 		return IPPROTO_DONE;
500 	}
501 
502 #if IPSEC
503 	/*
504 	 * UDP to port 4500 with a payload where the first four bytes are
505 	 * not zero is a UDP encapsulated IPsec packet. Packets where
506 	 * the payload is one byte and that byte is 0xFF are NAT keepalive
507 	 * packets. Decapsulate the ESP packet and carry on with IPsec input
508 	 * or discard the NAT keep-alive.
509 	 */
510 	if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 &&
511 	    (uh->uh_dport == ntohs((u_short)esp_udp_encap_port) ||
512 	    uh->uh_sport == ntohs((u_short)esp_udp_encap_port))) {
513 		/*
514 		 * Check if ESP or keepalive:
515 		 *      1. If the destination port of the incoming packet is 4500.
516 		 *      2. If the source port of the incoming packet is 4500,
517 		 *         then check the SADB to match IP address and port.
518 		 */
519 		bool check_esp = true;
520 		if (uh->uh_dport != ntohs((u_short)esp_udp_encap_port)) {
521 			check_esp = key_checksa_present(AF_INET6, (caddr_t)&ip6->ip6_dst,
522 			    (caddr_t)&ip6->ip6_src, uh->uh_dport,
523 			    uh->uh_sport, ip6_input_getdstifscope(m), ip6_input_getsrcifscope(m));
524 		}
525 
526 		if (check_esp) {
527 			int payload_len = ulen - sizeof(struct udphdr) > 4 ? 4 :
528 			    ulen - sizeof(struct udphdr);
529 
530 			if (m->m_len < off + sizeof(struct udphdr) + payload_len) {
531 				if ((m = m_pullup(m, off + sizeof(struct udphdr) +
532 				    payload_len)) == NULL) {
533 					udpstat.udps_hdrops++;
534 					goto bad;
535 				}
536 				/*
537 				 * Expect 32-bit aligned data pointer on strict-align
538 				 * platforms.
539 				 */
540 				MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
541 
542 				ip6 = mtod(m, struct ip6_hdr *);
543 				uh = (struct udphdr *)(void *)((caddr_t)ip6 + off);
544 			}
545 			/* Check for NAT keepalive packet */
546 			if (payload_len == 1 && *(u_int8_t*)
547 			    ((caddr_t)uh + sizeof(struct udphdr)) == 0xFF) {
548 				goto bad;
549 			} else if (payload_len == 4 && *(u_int32_t*)(void *)
550 			    ((caddr_t)uh + sizeof(struct udphdr)) != 0) {
551 				/* UDP encapsulated IPsec packet to pass through NAT */
552 				/* preserve the udp header */
553 				*offp = off + sizeof(struct udphdr);
554 				return esp6_input(mp, offp, IPPROTO_UDP);
555 			}
556 		}
557 	}
558 #endif /* IPSEC */
559 
560 	/*
561 	 * Locate pcb for datagram.
562 	 */
563 	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, ip6_input_getsrcifscope(m),
564 	    &ip6->ip6_dst, uh->uh_dport, ip6_input_getdstifscope(m), 1, m->m_pkthdr.rcvif);
565 	if (in6p == NULL) {
566 		IF_UDP_STATINC(ifp, port_unreach);
567 
568 		if (udp_log_in_vain) {
569 			char buf[INET6_ADDRSTRLEN];
570 
571 			strlcpy(buf, ip6_sprintf(&ip6->ip6_dst), sizeof(buf));
572 			if (udp_log_in_vain < 3) {
573 				log(LOG_INFO, "Connection attempt to UDP "
574 				    "%s:%d from %s:%d\n", buf,
575 				    ntohs(uh->uh_dport),
576 				    ip6_sprintf(&ip6->ip6_src),
577 				    ntohs(uh->uh_sport));
578 			} else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
579 			    !in6_are_addr_equal_scoped(&ip6->ip6_dst, &ip6->ip6_src, ip6_input_getdstifscope(m), ip6_input_getsrcifscope(m))) {
580 				log(LOG_INFO, "Connection attempt "
581 				    "to UDP %s:%d from %s:%d\n", buf,
582 				    ntohs(uh->uh_dport),
583 				    ip6_sprintf(&ip6->ip6_src),
584 				    ntohs(uh->uh_sport));
585 			}
586 		}
587 		udpstat.udps_noport++;
588 		if (m->m_flags & M_MCAST) {
589 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
590 			udpstat.udps_noportmcast++;
591 			IF_UDP_STATINC(ifp, badmcast);
592 			goto bad;
593 		}
594 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
595 		return IPPROTO_DONE;
596 	}
597 #if NECP
598 	if (!necp_socket_is_allowed_to_send_recv_v6(in6p, uh->uh_dport,
599 	    uh->uh_sport, &ip6->ip6_dst, &ip6->ip6_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
600 		in_pcb_checkstate(in6p, WNT_RELEASE, 0);
601 		IF_UDP_STATINC(ifp, badipsec);
602 		goto bad;
603 	}
604 #endif /* NECP */
605 
606 	/*
607 	 * Construct sockaddr format source address.
608 	 * Stuff source address and datagram in user buffer.
609 	 */
610 	udp_lock(in6p->in6p_socket, 1, 0);
611 
612 	if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) {
613 		udp_unlock(in6p->in6p_socket, 1, 0);
614 		IF_UDP_STATINC(ifp, cleanup);
615 		goto bad;
616 	}
617 
618 	init_sin6(&udp_in6, m); /* general init */
619 	udp_in6.sin6_port = uh->uh_sport;
620 	if ((in6p->in6p_flags & INP_CONTROLOPTS) != 0 ||
621 	    SOFLOW_ENABLED(in6p->in6p_socket) ||
622 	    SO_RECV_CONTROL_OPTS(in6p->in6p_socket)) {
623 		ret = ip6_savecontrol(in6p, m, &opts);
624 		if (ret != 0) {
625 			udp_unlock(in6p->in6p_socket, 1, 0);
626 			goto bad;
627 		}
628 	}
629 	m_adj(m, off + sizeof(struct udphdr));
630 	if (nstat_collect) {
631 		INP_ADD_STAT(in6p, cell, wifi, wired, rxpackets, 1);
632 		INP_ADD_STAT(in6p, cell, wifi, wired, rxbytes, m->m_pkthdr.len);
633 		inp_set_activity_bitmap(in6p);
634 	}
635 	so_recv_data_stat(in6p->in6p_socket, m, 0);
636 	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
637 	    (struct sockaddr *)&udp_in6, m, opts, NULL) == 0) {
638 		m = NULL;
639 		opts = NULL;
640 		udpstat.udps_fullsock++;
641 		udp_unlock(in6p->in6p_socket, 1, 0);
642 		goto bad;
643 	}
644 	if (is_wake_pkt) {
645 		soevent(in6p->in6p_socket, SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
646 	}
647 	sorwakeup(in6p->in6p_socket);
648 	udp_unlock(in6p->in6p_socket, 1, 0);
649 	return IPPROTO_DONE;
650 bad:
651 	if (m != NULL) {
652 		m_freem(m);
653 	}
654 	if (opts != NULL) {
655 		m_freem(opts);
656 	}
657 	return IPPROTO_DONE;
658 }
659 
660 void
udp6_ctlinput(int cmd,struct sockaddr * sa,void * d,__unused struct ifnet * ifp)661 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d, __unused struct ifnet *ifp)
662 {
663 	struct udphdr uh;
664 	struct ip6_hdr *ip6;
665 	struct mbuf *m;
666 	int off = 0;
667 	struct ip6ctlparam *ip6cp = NULL;
668 	struct icmp6_hdr *icmp6 = NULL;
669 	const struct sockaddr_in6 *sa6_src = NULL;
670 	void *cmdarg = NULL;
671 	void (*notify)(struct inpcb *, int) = udp_notify;
672 	struct inpcb *in6p;
673 	struct udp_portonly {
674 		u_int16_t uh_sport;
675 		u_int16_t uh_dport;
676 	} *uhp;
677 
678 	if (sa->sa_family != AF_INET6 ||
679 	    sa->sa_len != sizeof(struct sockaddr_in6)) {
680 		return;
681 	}
682 
683 	if ((unsigned)cmd >= PRC_NCMDS) {
684 		return;
685 	}
686 	if (PRC_IS_REDIRECT(cmd)) {
687 		notify = in6_rtchange;
688 		d = NULL;
689 	} else if (cmd == PRC_HOSTDEAD) {
690 		d = NULL;
691 	} else if (inet6ctlerrmap[cmd] == 0) {
692 		return;
693 	}
694 
695 	/* if the parameter is from icmp6, decode it. */
696 	if (d != NULL) {
697 		ip6cp = (struct ip6ctlparam *)d;
698 		icmp6 = ip6cp->ip6c_icmp6;
699 		m = ip6cp->ip6c_m;
700 		ip6 = ip6cp->ip6c_ip6;
701 		off = ip6cp->ip6c_off;
702 		cmdarg = ip6cp->ip6c_cmdarg;
703 		sa6_src = ip6cp->ip6c_src;
704 	} else {
705 		m = NULL;
706 		ip6 = NULL;
707 		cmdarg = NULL;
708 		sa6_src = &sa6_any;
709 	}
710 
711 	if (ip6 != NULL) {
712 #if SKYWALK
713 		union sockaddr_in_4_6 sock_laddr;
714 		struct protoctl_ev_val prctl_ev_val;
715 #endif /* SKYWALK */
716 		/*
717 		 * XXX: We assume that when IPV6 is non NULL,
718 		 * M and OFF are valid.
719 		 */
720 		/* check if we can safely examine src and dst ports */
721 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
722 			return;
723 		}
724 
725 		bzero(&uh, sizeof(uh));
726 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
727 
728 		in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_dst, uh.uh_dport, ip6_input_getdstifscope(m),
729 		    &ip6->ip6_src, uh.uh_sport, ip6_input_getsrcifscope(m), 0, NULL);
730 		if (cmd == PRC_MSGSIZE && in6p != NULL && !uuid_is_null(in6p->necp_client_uuid)) {
731 			uuid_t null_uuid;
732 			uuid_clear(null_uuid);
733 			necp_update_flow_protoctl_event(null_uuid, in6p->necp_client_uuid,
734 			    PRC_MSGSIZE, ntohl(icmp6->icmp6_mtu), 0);
735 			/*
736 			 * Avoid setting so_error when using Network.framework
737 			 * since the notification of PRC_MSGSIZE has been delivered
738 			 * through NECP.
739 			 */
740 			in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
741 			    (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport,
742 			    cmd, cmdarg, NULL);
743 		} else {
744 			in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
745 			    (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport,
746 			    cmd, cmdarg, notify);
747 		}
748 #if SKYWALK
749 		bzero(&prctl_ev_val, sizeof(prctl_ev_val));
750 		bzero(&sock_laddr, sizeof(sock_laddr));
751 
752 		if (cmd == PRC_MSGSIZE && icmp6 != NULL) {
753 			prctl_ev_val.val = ntohl(icmp6->icmp6_mtu);
754 		}
755 		sock_laddr.sin6.sin6_family = AF_INET6;
756 		sock_laddr.sin6.sin6_len = sizeof(sock_laddr.sin6);
757 		sock_laddr.sin6.sin6_addr = ip6->ip6_src;
758 
759 		protoctl_event_enqueue_nwk_wq_entry(ifp,
760 		    (struct sockaddr *)&sock_laddr, sa,
761 		    uh.uh_sport, uh.uh_dport, IPPROTO_UDP,
762 		    cmd, &prctl_ev_val);
763 #endif /* SKYWALK */
764 	}
765 	/*
766 	 * XXX The else condition here was broken for a long time.
767 	 * Fixing it made us deliver notification correctly but broke
768 	 * some frameworks that didn't handle it well.
769 	 * For now we have removed it and will revisit it later.
770 	 */
771 }
772 
773 static int
udp6_abort(struct socket * so)774 udp6_abort(struct socket *so)
775 {
776 	struct inpcb *inp;
777 
778 	inp = sotoinpcb(so);
779 	if (inp == NULL) {
780 		panic("%s: so=%p null inp", __func__, so);
781 		/* NOTREACHED */
782 	}
783 	soisdisconnected(so);
784 	in6_pcbdetach(inp);
785 	return 0;
786 }
787 
788 static int
udp6_attach(struct socket * so,int proto,struct proc * p)789 udp6_attach(struct socket *so, int proto, struct proc *p)
790 {
791 #pragma unused(proto)
792 	struct inpcb *inp;
793 	int error;
794 
795 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
796 		error = soreserve(so, udp_sendspace, udp_recvspace);
797 		if (error) {
798 			return error;
799 		}
800 	}
801 
802 	inp = sotoinpcb(so);
803 	if (inp != NULL) {
804 		return EINVAL;
805 	}
806 
807 	error = in_pcballoc(so, &udbinfo, p);
808 	if (error) {
809 		return error;
810 	}
811 
812 	inp = (struct inpcb *)so->so_pcb;
813 	inp->inp_vflag |= INP_IPV6;
814 	if (ip6_mapped_addr_on) {
815 		inp->inp_vflag |= INP_IPV4;
816 	}
817 	inp->in6p_hops = -1;    /* use kernel default */
818 	inp->in6p_cksum = -1;   /* just to be sure */
819 	/*
820 	 * XXX: ugly!!
821 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
822 	 * because the socket may be bound to an IPv6 wildcard address,
823 	 * which may match an IPv4-mapped IPv6 address.
824 	 */
825 	inp->inp_ip_ttl = (u_char)ip_defttl;
826 	if (nstat_collect) {
827 		nstat_udp_new_pcb(inp);
828 	}
829 	return 0;
830 }
831 
832 static int
udp6_bind(struct socket * so,struct sockaddr * nam,struct proc * p)833 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
834 {
835 	struct inpcb *inp;
836 	int error;
837 
838 	inp = sotoinpcb(so);
839 	if (inp == NULL) {
840 		return EINVAL;
841 	}
842 
843 	const uint8_t old_flags = inp->inp_vflag;
844 	inp->inp_vflag &= ~INP_IPV4;
845 	inp->inp_vflag |= INP_IPV6;
846 
847 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
848 		struct sockaddr_in6 *sin6_p;
849 
850 		sin6_p = (struct sockaddr_in6 *)(void *)nam;
851 
852 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
853 			inp->inp_vflag |= INP_IPV4;
854 			inp->inp_vflag &= ~INP_V4MAPPEDV6;
855 		} else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
856 			struct sockaddr_in sin;
857 
858 			in6_sin6_2_sin(&sin, sin6_p);
859 			inp->inp_vflag |= INP_IPV4;
860 			inp->inp_vflag &= ~INP_IPV6;
861 			inp->inp_vflag |= INP_V4MAPPEDV6;
862 
863 			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
864 			if (error != 0) {
865 				inp->inp_vflag = old_flags;
866 			}
867 			return error;
868 		}
869 	}
870 
871 	error = in6_pcbbind(inp, nam, p);
872 	if (error != 0) {
873 		inp->inp_vflag = old_flags;
874 	}
875 	return error;
876 }
877 
878 int
udp6_connect(struct socket * so,struct sockaddr * nam,struct proc * p)879 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
880 {
881 	struct inpcb *inp;
882 	int error;
883 	struct sockaddr_in6 *sin6_p = (struct sockaddr_in6 *)(void *)nam;
884 
885 #if defined(NECP) && defined(FLOW_DIVERT)
886 	int should_use_flow_divert = 0;
887 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
888 
889 	inp = sotoinpcb(so);
890 	if (inp == NULL) {
891 		return EINVAL;
892 	}
893 
894 #if defined(NECP) && defined(FLOW_DIVERT)
895 	should_use_flow_divert = necp_socket_should_use_flow_divert(inp);
896 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
897 
898 	/*
899 	 * It is possible that the socket is bound to v4 mapped v6 address.
900 	 * Post that do not allow connect to a v6 endpoint.
901 	 */
902 	if (inp->inp_vflag & INP_V4MAPPEDV6 &&
903 	    !IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
904 		return EINVAL;
905 	}
906 
907 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
908 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
909 			struct sockaddr_in sin;
910 			const uint8_t old_flags = inp->inp_vflag;
911 
912 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
913 				return EISCONN;
914 			}
915 
916 			if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
917 				so->so_flags1 |= SOF1_CONNECT_COUNTED;
918 				INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected);
919 			}
920 
921 			in6_sin6_2_sin(&sin, sin6_p);
922 #if defined(NECP) && defined(FLOW_DIVERT)
923 			if (should_use_flow_divert) {
924 				goto do_flow_divert;
925 			}
926 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
927 			inp->inp_vflag |= INP_IPV4;
928 			inp->inp_vflag &= ~INP_IPV6;
929 			inp->inp_vflag |= INP_V4MAPPEDV6;
930 
931 			error = in_pcbconnect(inp, (struct sockaddr *)&sin,
932 			    p, IFSCOPE_NONE, NULL);
933 			if (error == 0) {
934 #if NECP
935 				/* Update NECP client with connected five-tuple */
936 				if (!uuid_is_null(inp->necp_client_uuid)) {
937 					socket_unlock(so, 0);
938 					necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
939 					socket_lock(so, 0);
940 				}
941 #endif /* NECP */
942 				soisconnected(so);
943 			} else {
944 				inp->inp_vflag = old_flags;
945 			}
946 			return error;
947 		}
948 	}
949 
950 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
951 		return EISCONN;
952 	}
953 
954 	if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
955 		so->so_flags1 |= SOF1_CONNECT_COUNTED;
956 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
957 	}
958 
959 #if defined(NECP) && defined(FLOW_DIVERT)
960 do_flow_divert:
961 	if (should_use_flow_divert) {
962 		error = flow_divert_pcb_init(so);
963 		if (error == 0) {
964 			error = flow_divert_connect_out(so, nam, p);
965 		}
966 		return error;
967 	}
968 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
969 
970 	error = in6_pcbconnect(inp, nam, p);
971 	if (error == 0) {
972 		/* should be non mapped addr */
973 		if (ip6_mapped_addr_on ||
974 		    (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
975 			inp->inp_vflag &= ~INP_IPV4;
976 			inp->inp_vflag |= INP_IPV6;
977 		}
978 #if NECP
979 		/* Update NECP client with connected five-tuple */
980 		if (!uuid_is_null(inp->necp_client_uuid)) {
981 			socket_unlock(so, 0);
982 			necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
983 			socket_lock(so, 0);
984 		}
985 #endif /* NECP */
986 		soisconnected(so);
987 		if (inp->inp_flowhash == 0) {
988 			inp_calc_flowhash(inp);
989 			ASSERT(inp->inp_flowhash != 0);
990 		}
991 		/* update flowinfo - RFC 6437 */
992 		if (inp->inp_flow == 0 &&
993 		    inp->in6p_flags & IN6P_AUTOFLOWLABEL) {
994 			inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
995 			inp->inp_flow |=
996 			    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
997 		}
998 	}
999 	return error;
1000 }
1001 
1002 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)1003 udp6_connectx(struct socket *so, struct sockaddr *src,
1004     struct sockaddr *dst, struct proc *p, uint32_t ifscope,
1005     sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
1006     uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
1007 {
1008 	return udp_connectx_common(so, AF_INET6, src, dst,
1009 	           p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written);
1010 }
1011 
1012 static int
udp6_detach(struct socket * so)1013 udp6_detach(struct socket *so)
1014 {
1015 	struct inpcb *inp;
1016 
1017 	inp = sotoinpcb(so);
1018 	if (inp == NULL) {
1019 		return EINVAL;
1020 	}
1021 	in6_pcbdetach(inp);
1022 	return 0;
1023 }
1024 
1025 static int
udp6_disconnect(struct socket * so)1026 udp6_disconnect(struct socket *so)
1027 {
1028 	struct inpcb *inp;
1029 
1030 	inp = sotoinpcb(so);
1031 	if (inp == NULL
1032 #if NECP
1033 	    || (necp_socket_should_use_flow_divert(inp))
1034 #endif /* NECP */
1035 	    ) {
1036 		return inp == NULL ? EINVAL : EPROTOTYPE;
1037 	}
1038 
1039 	if (inp->inp_vflag & INP_IPV4) {
1040 		struct pr_usrreqs *pru;
1041 
1042 		pru = ip_protox[IPPROTO_UDP]->pr_usrreqs;
1043 		return (*pru->pru_disconnect)(so);
1044 	}
1045 
1046 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1047 		return ENOTCONN;
1048 	}
1049 
1050 	in6_pcbdisconnect(inp);
1051 
1052 	/* reset flow-controlled state, just in case */
1053 	inp_reset_fc_state(inp);
1054 
1055 	inp->in6p_laddr = in6addr_any;
1056 	inp->inp_lifscope = IFSCOPE_NONE;
1057 	inp->in6p_last_outifp = NULL;
1058 #if SKYWALK
1059 	if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1060 		netns_set_ifnet(&inp->inp_netns_token, NULL);
1061 	}
1062 #endif /* SKYWALK */
1063 
1064 	so->so_state &= ~SS_ISCONNECTED;                /* XXX */
1065 	return 0;
1066 }
1067 
1068 static int
udp6_disconnectx(struct socket * so,sae_associd_t aid,sae_connid_t cid)1069 udp6_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
1070 {
1071 #pragma unused(cid)
1072 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
1073 		return EINVAL;
1074 	}
1075 
1076 	return udp6_disconnect(so);
1077 }
1078 
1079 static int
udp6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)1080 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1081     struct mbuf *control, struct proc *p)
1082 {
1083 	struct inpcb *inp;
1084 	int error = 0;
1085 #if defined(NECP) && defined(FLOW_DIVERT)
1086 	int should_use_flow_divert = 0;
1087 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1088 #if CONTENT_FILTER
1089 	struct m_tag *cfil_tag = NULL;
1090 	struct sockaddr *cfil_faddr = NULL;
1091 #endif
1092 
1093 	inp = sotoinpcb(so);
1094 	if (inp == NULL) {
1095 		error = EINVAL;
1096 		goto bad;
1097 	}
1098 
1099 #if CONTENT_FILTER
1100 	//If socket is subject to UDP Content Filter and unconnected, get addr from tag.
1101 	if (CFIL_DGRAM_FILTERED(so) && !addr && IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1102 		cfil_tag = cfil_dgram_get_socket_state(m, NULL, NULL, &cfil_faddr, NULL);
1103 		if (cfil_tag) {
1104 			addr = (struct sockaddr *)cfil_faddr;
1105 		}
1106 	}
1107 #endif
1108 
1109 #if defined(NECP) && defined(FLOW_DIVERT)
1110 	should_use_flow_divert = necp_socket_should_use_flow_divert(inp);
1111 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1112 
1113 	if (addr != NULL) {
1114 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1115 			error = EINVAL;
1116 			goto bad;
1117 		}
1118 		if (addr->sa_family != AF_INET6) {
1119 			error = EAFNOSUPPORT;
1120 			goto bad;
1121 		}
1122 	}
1123 
1124 	if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1125 		int hasv4addr;
1126 		struct sockaddr_in6 *sin6 = NULL;
1127 
1128 		if (addr == NULL) {
1129 			hasv4addr = (inp->inp_vflag & INP_IPV4);
1130 		} else {
1131 			sin6 = (struct sockaddr_in6 *)(void *)addr;
1132 			hasv4addr =
1133 			    IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 1 : 0;
1134 		}
1135 		if (hasv4addr) {
1136 			struct pr_usrreqs *pru;
1137 
1138 			if (sin6 != NULL) {
1139 				in6_sin6_2_sin_in_sock(addr);
1140 			}
1141 #if defined(NECP) && defined(FLOW_DIVERT)
1142 			if (should_use_flow_divert) {
1143 				goto do_flow_divert;
1144 			}
1145 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1146 			pru = ip_protox[IPPROTO_UDP]->pr_usrreqs;
1147 			error = ((*pru->pru_send)(so, flags, m, addr,
1148 			    control, p));
1149 #if CONTENT_FILTER
1150 			if (cfil_tag) {
1151 				m_tag_free(cfil_tag);
1152 			}
1153 #endif
1154 			/* addr will just be freed in sendit(). */
1155 			return error;
1156 		}
1157 	}
1158 
1159 #if defined(NECP) && defined(FLOW_DIVERT)
1160 do_flow_divert:
1161 	if (should_use_flow_divert) {
1162 		/* Implicit connect */
1163 		error = flow_divert_implicit_data_out(so, flags, m, addr, control, p);
1164 #if CONTENT_FILTER
1165 		if (cfil_tag) {
1166 			m_tag_free(cfil_tag);
1167 		}
1168 #endif
1169 		return error;
1170 	}
1171 #endif /* defined(NECP) && defined(FLOW_DIVERT) */
1172 
1173 #if SKYWALK
1174 	sk_protect_t protect = sk_async_transmit_protect();
1175 #endif /* SKYWALK */
1176 	error = udp6_output(inp, m, addr, control, p);
1177 #if SKYWALK
1178 	sk_async_transmit_unprotect(protect);
1179 #endif /* SKYWALK */
1180 
1181 #if CONTENT_FILTER
1182 	if (cfil_tag) {
1183 		m_tag_free(cfil_tag);
1184 	}
1185 #endif
1186 	return error;
1187 
1188 bad:
1189 	VERIFY(error != 0);
1190 
1191 	if (m != NULL) {
1192 		m_freem(m);
1193 	}
1194 	if (control != NULL) {
1195 		m_freem(control);
1196 	}
1197 #if CONTENT_FILTER
1198 	if (cfil_tag) {
1199 		m_tag_free(cfil_tag);
1200 	}
1201 #endif
1202 	return error;
1203 }
1204 
1205 /*
1206  * Checksum extended UDP header and data.
1207  */
1208 static int
udp6_input_checksum(struct mbuf * m,struct udphdr * uh,int off,int ulen)1209 udp6_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
1210 {
1211 	struct ifnet *ifp = m->m_pkthdr.rcvif;
1212 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1213 
1214 	if (!(m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
1215 	    uh->uh_sum == 0) {
1216 		/* UDP/IPv6 checksum is mandatory (RFC2460) */
1217 
1218 		/*
1219 		 * If checksum was already validated, ignore this check.
1220 		 * This is necessary for transport-mode ESP, which may be
1221 		 * getting UDP payloads without checksums when the network
1222 		 * has a NAT64.
1223 		 */
1224 		udpstat.udps_nosum++;
1225 		goto badsum;
1226 	}
1227 
1228 	if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
1229 	    (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
1230 	    (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
1231 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
1232 			uh->uh_sum = m->m_pkthdr.csum_rx_val;
1233 		} else {
1234 			uint32_t sum = m->m_pkthdr.csum_rx_val;
1235 			uint32_t start = m->m_pkthdr.csum_rx_start;
1236 			int32_t trailer = (m_pktlen(m) - (off + ulen));
1237 
1238 			/*
1239 			 * Perform 1's complement adjustment of octets
1240 			 * that got included/excluded in the hardware-
1241 			 * calculated checksum value.  Also take care
1242 			 * of any trailing bytes and subtract out
1243 			 * their partial sum.
1244 			 */
1245 			ASSERT(trailer >= 0);
1246 			if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
1247 			    (start != off || trailer != 0)) {
1248 				uint32_t swbytes = (uint32_t)trailer;
1249 				uint16_t s = 0, d = 0;
1250 
1251 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
1252 					s = ip6->ip6_src.s6_addr16[1];
1253 					ip6->ip6_src.s6_addr16[1] = 0;
1254 				}
1255 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1256 					d = ip6->ip6_dst.s6_addr16[1];
1257 					ip6->ip6_dst.s6_addr16[1] = 0;
1258 				}
1259 
1260 				/* callee folds in sum */
1261 				sum = m_adj_sum16(m, start, off, ulen, sum);
1262 				if (off > start) {
1263 					swbytes += (off - start);
1264 				} else {
1265 					swbytes += (start - off);
1266 				}
1267 
1268 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
1269 					ip6->ip6_src.s6_addr16[1] = s;
1270 				}
1271 				if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
1272 					ip6->ip6_dst.s6_addr16[1] = d;
1273 				}
1274 
1275 				if (swbytes != 0) {
1276 					udp_in_cksum_stats(swbytes);
1277 				}
1278 				if (trailer != 0) {
1279 					m_adj(m, -trailer);
1280 				}
1281 			}
1282 
1283 			uh->uh_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst,
1284 			    sum + htonl(ulen + IPPROTO_UDP));
1285 		}
1286 		uh->uh_sum ^= 0xffff;
1287 	} else {
1288 		udp_in6_cksum_stats(ulen);
1289 		uh->uh_sum = in6_cksum(m, IPPROTO_UDP, off, ulen);
1290 	}
1291 
1292 	if (uh->uh_sum != 0) {
1293 badsum:
1294 		udpstat.udps_badsum++;
1295 		IF_UDP_STATINC(ifp, badchksum);
1296 		return -1;
1297 	}
1298 
1299 	return 0;
1300 }
1301