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