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