-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathudp_usrreq.c
More file actions
3231 lines (2877 loc) · 84.3 KB
/
Copy pathudp_usrreq.c
File metadata and controls
3231 lines (2877 loc) · 84.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2024 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/*
* Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/mcache.h>
#include <net/ntstat.h>
#include <kern/zalloc.h>
#include <mach/boolean.h>
#include <pexpert/pexpert.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/dlil.h>
#include <net/droptap.h>
#include <net/net_api_stats.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_tclass.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/in_pcb.h>
#include <netinet/in_var.h>
#include <netinet/ip_var.h>
#include <netinet6/in6_pcb.h>
#include <netinet6/ip6_var.h>
#include <netinet6/udp6_var.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/udp_log.h>
#include <sys/kdebug.h>
#if IPSEC
#include <netinet6/ipsec.h>
#include <netinet6/esp.h>
#include <netkey/key.h>
extern int ipsec_bypass;
extern int esp_udp_encap_port;
#endif /* IPSEC */
#if NECP
#include <net/necp.h>
#endif /* NECP */
#if FLOW_DIVERT
#include <netinet/flow_divert.h>
#endif /* FLOW_DIVERT */
#if CONTENT_FILTER
#include <net/content_filter.h>
#endif /* CONTENT_FILTER */
#if SKYWALK
#include <skywalk/core/skywalk_var.h>
#endif /* SKYWALK */
#include <net/sockaddr_utils.h>
#define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
#define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
#define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
#define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
#define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
#define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
/*
* UDP protocol implementation.
* Per RFC 768, August, 1980.
*/
#ifndef COMPAT_42
static int udpcksum = 1;
#else
static int udpcksum = 0; /* XXX */
#endif
SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum,
CTLFLAG_RW | CTLFLAG_LOCKED, &udpcksum, 0, "");
int udp_log_in_vain = 0;
SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW | CTLFLAG_LOCKED,
&udp_log_in_vain, 0, "Log all incoming UDP packets");
static int blackhole = 0;
SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW | CTLFLAG_LOCKED,
&blackhole, 0, "Do not send port unreachables for refused connects");
static KALLOC_TYPE_DEFINE(inpcbzone, struct inpcb, NET_KT_DEFAULT);
struct inpcbhead udb; /* from udp_var.h */
#define udb6 udb /* for KAME src sync over BSD*'s */
struct inpcbinfo udbinfo;
#ifndef UDBHASHSIZE
#define UDBHASHSIZE 16
#endif
/* Garbage collection performed during most recent udp_gc() run */
static boolean_t udp_gc_done = FALSE;
#define log_in_vain_log(a) { log a; }
static int udp_getstat SYSCTL_HANDLER_ARGS;
struct udpstat udpstat; /* from udp_var.h */
SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats,
CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
0, 0, udp_getstat, "S,udpstat",
"UDP statistics (struct udpstat, netinet/udp_var.h)");
SYSCTL_UINT(_net_inet_udp, OID_AUTO, pcbcount,
CTLFLAG_RD | CTLFLAG_LOCKED, &udbinfo.ipi_count, 0,
"Number of active PCBs");
__private_extern__ int udp_use_randomport = 1;
SYSCTL_INT(_net_inet_udp, OID_AUTO, randomize_ports,
CTLFLAG_RW | CTLFLAG_LOCKED, &udp_use_randomport, 0,
"Randomize UDP port numbers");
struct udp_in6 {
struct sockaddr_in6 uin6_sin;
u_char uin6_init_done : 1;
};
struct udp_ip6 {
struct ip6_hdr uip6_ip6;
u_char uip6_init_done : 1;
};
int udp_abort(struct socket *);
int udp_attach(struct socket *, int, struct proc *);
int udp_bind(struct socket *, struct sockaddr *, struct proc *);
int udp_connect(struct socket *, struct sockaddr *, struct proc *);
int udp_connectx(struct socket *, struct sockaddr *,
struct sockaddr *, struct proc *, uint32_t, sae_associd_t,
sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *);
int udp_detach(struct socket *);
int udp_disconnect(struct socket *);
int udp_disconnectx(struct socket *, sae_associd_t, sae_connid_t);
int udp_send(struct socket *, int, struct mbuf *, struct sockaddr *,
struct mbuf *, struct proc *);
static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int,
struct sockaddr_in *, struct udp_in6 *, struct udp_ip6 *, struct ifnet *);
static int udp_input_checksum(struct mbuf *, struct udphdr *, int, int);
int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
struct mbuf *, struct proc *);
static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
static void udp_gc(struct inpcbinfo *);
static int udp_defunct(struct socket *);
struct pr_usrreqs udp_usrreqs = {
.pru_abort = udp_abort,
.pru_attach = udp_attach,
.pru_bind = udp_bind,
.pru_connect = udp_connect,
.pru_connectx = udp_connectx,
.pru_control = in_control,
.pru_detach = udp_detach,
.pru_disconnect = udp_disconnect,
.pru_disconnectx = udp_disconnectx,
.pru_peeraddr = in_getpeeraddr,
.pru_send = udp_send,
.pru_shutdown = udp_shutdown,
.pru_sockaddr = in_getsockaddr,
.pru_sosend = sosend,
.pru_soreceive = soreceive,
.pru_defunct = udp_defunct,
};
void
udp_init(struct protosw *pp, struct domain *dp)
{
#pragma unused(dp)
static int udp_initialized = 0;
struct inpcbinfo *pcbinfo;
VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
if (udp_initialized) {
return;
}
udp_initialized = 1;
uint32_t pool_size = (nmbclusters << MCLSHIFT) >> MBSHIFT;
if (pool_size >= 96) {
/* Improves 10GbE UDP performance. */
udp_recvspace = 786896;
}
if (PE_parse_boot_argn("udp_log", &udp_log_enable_flags, sizeof(udp_log_enable_flags))) {
os_log(OS_LOG_DEFAULT, "udp_init: set udp_log_enable_flags to 0x%x", udp_log_enable_flags);
}
LIST_INIT(&udb);
udbinfo.ipi_listhead = &udb;
hashinit_counted_by(UDBHASHSIZE, udbinfo.ipi_hashbase,
udbinfo.ipi_hashbase_count);
udbinfo.ipi_hashmask = udbinfo.ipi_hashbase_count - 1;
hashinit_counted_by(UDBHASHSIZE, udbinfo.ipi_porthashbase,
udbinfo.ipi_porthashbase_count);
udbinfo.ipi_porthashmask = udbinfo.ipi_porthashbase_count - 1;
udbinfo.ipi_zone = inpcbzone;
pcbinfo = &udbinfo;
/*
* allocate lock group and attribute for udp pcb mutexes
*/
pcbinfo->ipi_lock_grp = lck_grp_alloc_init("udppcb",
LCK_GRP_ATTR_NULL);
lck_attr_setdefault(&pcbinfo->ipi_lock_attr);
lck_rw_init(&pcbinfo->ipi_lock, pcbinfo->ipi_lock_grp,
&pcbinfo->ipi_lock_attr);
udbinfo.ipi_gc = udp_gc;
in_pcbinfo_attach(&udbinfo);
}
void
udp_input(struct mbuf *m, int iphlen)
{
struct ip *ip;
struct udphdr *uh;
struct inpcb *inp;
mbuf_ref_t opts = NULL;
int len, isbroadcast;
struct ip save_ip;
struct sockaddr *append_sa = NULL;
struct sockaddr *append_da = NULL;
struct inpcbinfo *pcbinfo = &udbinfo;
struct sockaddr_in udp_in;
struct sockaddr_in udp_dst;
struct ip_moptions *imo = NULL;
int foundmembership = 0, ret = 0;
struct udp_in6 udp_in6;
struct udp_in6 udp_dst6;
struct udp_ip6 udp_ip6;
struct ifnet *ifp = m->m_pkthdr.rcvif;
u_int16_t pf_tag = 0;
boolean_t is_wake_pkt = false;
boolean_t check_cfil = cfil_filter_present();
drop_reason_t drop_reason = DROP_REASON_UNSPECIFIED;
SOCKADDR_ZERO(&udp_in, sizeof(udp_in));
udp_in.sin_len = sizeof(struct sockaddr_in);
udp_in.sin_family = AF_INET;
bzero(&udp_in6, sizeof(udp_in6));
udp_in6.uin6_sin.sin6_len = sizeof(struct sockaddr_in6);
udp_in6.uin6_sin.sin6_family = AF_INET6;
if (m->m_flags & M_PKTHDR) {
pf_tag = m_pftag(m)->pftag_tag;
if (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) {
is_wake_pkt = true;
}
}
udpstat.udps_ipackets++;
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
/* Expect 32-bit aligned data pointer on strict-align platforms */
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
m_add_crumb(m, PKT_CRUMB_UDP_INPUT);
/*
* Strip IP options, if any; should skip this,
* make available to user, and use on returned packets,
* but we don't yet have a way to check the checksum
* with options still present.
*/
if (iphlen > sizeof(struct ip)) {
ip_stripoptions(m);
iphlen = sizeof(struct ip);
}
/*
* Get IP and UDP header together in first mbuf.
*/
ip = mtod(m, struct ip *);
if (m->m_len < iphlen + sizeof(struct udphdr)) {
m = m_pullup(m, iphlen + sizeof(struct udphdr));
if (m == NULL) {
udpstat.udps_hdrops++;
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
0, 0, 0, 0, 0);
return;
}
ip = mtod(m, struct ip *);
}
uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
/* destination port of 0 is illegal, based on RFC768. */
if (uh->uh_dport == 0) {
drop_reason = DROP_REASON_UDP_DST_PORT_ZERO;
IF_UDP_STATINC(ifp, port0);
goto bad;
}
KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport,
ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen);
/*
* Make mbuf data length reflect UDP length.
* If not enough data to reflect UDP length, drop.
*/
len = ntohs((u_short)uh->uh_ulen);
if (ip->ip_len != len) {
if (len > ip->ip_len || len < sizeof(struct udphdr)) {
udpstat.udps_badlen++;
IF_UDP_STATINC(ifp, badlength);
drop_reason = DROP_REASON_UDP_BAD_LENGTH;
goto bad;
}
m_adj(m, len - ip->ip_len);
/* ip->ip_len = len; */
}
/*
* Save a copy of the IP header in case we want restore it
* for sending an ICMP error message in response.
*/
save_ip = *ip;
/*
* Checksum extended UDP header and data.
*/
if (udp_input_checksum(m, uh, iphlen, len)) {
drop_reason = DROP_REASON_UDP_BAD_CHECKSUM;
goto bad;
}
isbroadcast = in_broadcast(ip->ip_dst, ifp);
if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || isbroadcast) {
int reuse_sock = 0, mcast_delivered = 0;
lck_rw_lock_shared(&pcbinfo->ipi_lock);
/*
* Deliver a multicast or broadcast datagram to *all* sockets
* for which the local and remote addresses and ports match
* those of the incoming datagram. This allows more than
* one process to receive multi/broadcasts on the same port.
* (This really ought to be done for unicast datagrams as
* well, but that would cause problems with existing
* applications that open both address-specific sockets and
* a wildcard socket listening to the same port -- they would
* end up receiving duplicates of every unicast datagram.
* Those applications open the multiple sockets to overcome an
* inadequacy of the UDP socket interface, but for backwards
* compatibility we avoid the problem here rather than
* fixing the interface. Maybe 4.5BSD will remedy this?)
*/
/*
* Construct sockaddr format source address.
*/
udp_in.sin_port = uh->uh_sport;
udp_in.sin_addr = ip->ip_src;
/*
* Locate pcb(s) for datagram.
* (Algorithm copied from raw_intr().)
*/
udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
LIST_FOREACH(inp, &udb, inp_list) {
#if IPSEC
int skipit;
#endif /* IPSEC */
if (inp->inp_socket == NULL) {
continue;
}
if (inp != sotoinpcb(inp->inp_socket)) {
panic("%s: bad so back ptr inp=%p",
__func__, inp);
/* NOTREACHED */
}
if ((inp->inp_vflag & INP_IPV4) == 0) {
continue;
}
if (inp_restricted_recv(inp, ifp)) {
continue;
}
if ((inp->inp_moptions == NULL) &&
(ntohl(ip->ip_dst.s_addr) !=
INADDR_ALLHOSTS_GROUP) && (isbroadcast == 0)) {
continue;
}
/*
* Skip unbound sockets before taking the lock on the socket as
* the test with the destination port in the header will fail
*/
if (inp->inp_lport == 0) {
continue;
}
if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
WNT_STOPUSING) {
continue;
}
udp_lock(inp->inp_socket, 1, 0);
if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
WNT_STOPUSING) {
udp_unlock(inp->inp_socket, 1, 0);
continue;
}
if (inp->inp_lport != uh->uh_dport) {
udp_unlock(inp->inp_socket, 1, 0);
continue;
}
if (inp->inp_laddr.s_addr != INADDR_ANY) {
if (inp->inp_laddr.s_addr !=
ip->ip_dst.s_addr) {
udp_unlock(inp->inp_socket, 1, 0);
continue;
}
}
if (inp->inp_faddr.s_addr != INADDR_ANY) {
if (inp->inp_faddr.s_addr !=
ip->ip_src.s_addr ||
inp->inp_fport != uh->uh_sport) {
udp_unlock(inp->inp_socket, 1, 0);
continue;
}
}
if (isbroadcast == 0 && (ntohl(ip->ip_dst.s_addr) !=
INADDR_ALLHOSTS_GROUP)) {
struct sockaddr_in group;
int blocked;
if ((imo = inp->inp_moptions) == NULL) {
udp_unlock(inp->inp_socket, 1, 0);
continue;
}
IMO_LOCK(imo);
SOCKADDR_ZERO(&group, sizeof(struct sockaddr_in));
group.sin_len = sizeof(struct sockaddr_in);
group.sin_family = AF_INET;
group.sin_addr = ip->ip_dst;
blocked = imo_multi_filter(imo, ifp,
&group, &udp_in);
if (blocked == MCAST_PASS) {
foundmembership = 1;
}
IMO_UNLOCK(imo);
if (!foundmembership) {
udp_unlock(inp->inp_socket, 1, 0);
if (blocked == MCAST_NOTSMEMBER ||
blocked == MCAST_MUTED) {
udpstat.udps_filtermcast++;
}
continue;
}
foundmembership = 0;
}
reuse_sock = (inp->inp_socket->so_options &
(SO_REUSEPORT | SO_REUSEADDR));
#if NECP
skipit = 0;
if (!necp_socket_is_allowed_to_send_recv_v4(inp,
uh->uh_dport, uh->uh_sport, &ip->ip_dst,
&ip->ip_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
/* do not inject data to pcb */
skipit = 1;
UDP_LOG_DROP_NECP(ip, uh, inp, false);
}
if (skipit == 0)
#endif /* NECP */
{
mbuf_ref_t n = NULL;
if (reuse_sock) {
n = m_copy(m, 0, M_COPYALL);
}
udp_append(inp, ip, m,
iphlen + sizeof(struct udphdr),
&udp_in, &udp_in6, &udp_ip6, ifp);
mcast_delivered++;
m = n;
}
if (is_wake_pkt) {
soevent(inp->inp_socket, SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
}
udp_unlock(inp->inp_socket, 1, 0);
/*
* Don't look for additional matches if this one does
* not have either the SO_REUSEPORT or SO_REUSEADDR
* socket options set. This heuristic avoids searching
* through all pcbs in the common case of a non-shared
* port. It assumes that an application will never
* clear these options after setting them.
*/
if (reuse_sock == 0 || m == NULL) {
break;
}
/*
* Expect 32-bit aligned data pointer on strict-align
* platforms.
*/
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
/*
* Recompute IP and UDP header pointers for new mbuf
*/
ip = mtod(m, struct ip *);
uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
}
lck_rw_done(&pcbinfo->ipi_lock);
if (mcast_delivered == 0) {
/*
* No matching pcb found; discard datagram.
* (No need to send an ICMP Port Unreachable
* for a broadcast or multicast datgram.)
*/
udpstat.udps_noportbcast++;
IF_UDP_STATINC(ifp, port_unreach);
drop_reason = DROP_REASON_UDP_PORT_UNREACHEABLE;
goto bad;
}
/* free the extra copy of mbuf or skipped by IPsec */
if (m != NULL) {
m_freem(m);
}
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
return;
}
#if IPSEC
/*
* UDP to port 4500 with a payload where the first four bytes are
* not zero is a UDP encapsulated IPsec packet. Packets where
* the payload is one byte and that byte is 0xFF are NAT keepalive
* packets. Decapsulate the ESP packet and carry on with IPsec input
* or discard the NAT keep-alive.
*/
if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 &&
(uh->uh_dport == ntohs((u_short)esp_udp_encap_port) ||
uh->uh_sport == ntohs((u_short)esp_udp_encap_port))) {
/*
* Check if ESP or keepalive:
* 1. If the destination port of the incoming packet is 4500.
* 2. If the source port of the incoming packet is 4500,
* then check the SADB to match IP address and port.
*/
bool check_esp = true;
if (uh->uh_dport != ntohs((u_short)esp_udp_encap_port)) {
union sockaddr_in_4_6 src = {};
union sockaddr_in_4_6 dst = {};
ipsec_fill_ip_sockaddr_4_6(&src, ip->ip_src, uh->uh_sport);
ipsec_fill_ip_sockaddr_4_6(&dst, ip->ip_dst, uh->uh_dport);
check_esp = key_checksa_present(&dst, &src);
}
if (check_esp) {
int payload_len = len - sizeof(struct udphdr) > 4 ? 4 :
len - sizeof(struct udphdr);
if (m->m_len < iphlen + sizeof(struct udphdr) + payload_len) {
if ((m = m_pullup(m, iphlen + sizeof(struct udphdr) +
payload_len)) == NULL) {
udpstat.udps_hdrops++;
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
0, 0, 0, 0, 0);
return;
}
/*
* Expect 32-bit aligned data pointer on strict-align
* platforms.
*/
MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
ip = mtod(m, struct ip *);
uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
}
/* Check for NAT keepalive packet */
if (payload_len == 1 && *(u_int8_t *)
((caddr_t)uh + sizeof(struct udphdr)) == 0xFF) {
m_freem(m);
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
0, 0, 0, 0, 0);
return;
} else if (payload_len == 4 && *(u_int32_t *)(void *)
((caddr_t)uh + sizeof(struct udphdr)) != 0) {
/* UDP encapsulated IPsec packet to pass through NAT */
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
0, 0, 0, 0, 0);
/* preserve the udp header */
esp4_input(m, iphlen + sizeof(struct udphdr));
return;
}
}
}
#endif /* IPSEC */
/*
* Locate pcb for datagram.
*/
inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
ip->ip_dst, uh->uh_dport, 1, ifp);
if (inp == NULL) {
IF_UDP_STATINC(ifp, port_unreach);
if (udp_log_in_vain) {
char buf[MAX_IPv4_STR_LEN];
char buf2[MAX_IPv4_STR_LEN];
/* check src and dst address */
if (udp_log_in_vain < 3) {
log(LOG_INFO, "Connection attempt to "
"UDP %s:%d from %s:%d\n", inet_ntop(AF_INET,
&ip->ip_dst, buf, sizeof(buf)),
ntohs(uh->uh_dport), inet_ntop(AF_INET,
&ip->ip_src, buf2, sizeof(buf2)),
ntohs(uh->uh_sport));
} else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
ip->ip_dst.s_addr != ip->ip_src.s_addr) {
log_in_vain_log((LOG_INFO,
"Stealth Mode connection attempt to "
"UDP %s:%d from %s:%d\n", inet_ntop(AF_INET,
&ip->ip_dst, buf, sizeof(buf)),
ntohs(uh->uh_dport), inet_ntop(AF_INET,
&ip->ip_src, buf2, sizeof(buf2)),
ntohs(uh->uh_sport)))
}
}
udpstat.udps_noport++;
if (m->m_flags & (M_BCAST | M_MCAST)) {
udpstat.udps_noportbcast++;
drop_reason = DROP_REASON_UDP_PORT_UNREACHEABLE;
goto bad;
}
if (blackhole) {
if (ifp && ifp->if_type != IFT_LOOP) {
drop_reason = DROP_REASON_UDP_PORT_UNREACHEABLE;
goto bad;
}
}
if (if_link_heuristics_enabled(ifp)) {
drop_reason = DROP_REASON_UDP_PORT_UNREACHEABLE;
IF_UDP_STATINC(ifp, linkheur_stealthdrop);
goto bad;
}
*ip = save_ip;
ip->ip_len += iphlen;
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
return;
}
udp_lock(inp->inp_socket, 1, 0);
if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
udp_unlock(inp->inp_socket, 1, 0);
IF_UDP_STATINC(ifp, cleanup);
drop_reason = DROP_REASON_UDP_SOCKET_CLOSING;
goto bad;
}
#if NECP
if (!necp_socket_is_allowed_to_send_recv_v4(inp, uh->uh_dport,
uh->uh_sport, &ip->ip_dst, &ip->ip_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
udp_unlock(inp->inp_socket, 1, 0);
IF_UDP_STATINC(ifp, badipsec);
drop_reason = DROP_REASON_UDP_NECP;
goto bad;
}
#endif /* NECP */
/*
* Construct sockaddr format source address.
* Stuff source address and datagram in user buffer.
*/
udp_in.sin_port = uh->uh_sport;
udp_in.sin_addr = ip->ip_src;
if ((inp->inp_flags & INP_CONTROLOPTS) != 0 ||
SOFLOW_ENABLED(inp->inp_socket) ||
SO_RECV_CONTROL_OPTS(inp->inp_socket)) {
if (inp->inp_vflag & INP_IPV6 || inp->inp_vflag & INP_V4MAPPEDV6) {
int savedflags;
ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
savedflags = inp->inp_flags;
inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
ret = ip6_savecontrol(inp, m, &opts);
inp->inp_flags = savedflags;
} else {
ret = ip_savecontrol(inp, &opts, ip, m);
}
if (ret != 0) {
udp_unlock(inp->inp_socket, 1, 0);
drop_reason = DROP_REASON_UDP_CANNOT_SAVE_CONTROL;
goto bad;
}
}
m_adj(m, iphlen + sizeof(struct udphdr));
KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
if (inp->inp_vflag & INP_IPV6) {
in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
append_sa = SA(&udp_in6.uin6_sin);
} else {
append_sa = SA(&udp_in);
}
if (nstat_collect) {
stats_functional_type ifnet_count_type = IFNET_COUNT_TYPE(ifp);
INP_ADD_STAT(inp, ifnet_count_type, rxpackets, 1);
INP_ADD_STAT(inp, ifnet_count_type, rxbytes, m->m_pkthdr.len);
inp_set_activity_bitmap(inp);
}
#if CONTENT_FILTER && NECP
if (check_cfil && inp != NULL && inp->inp_policyresult.results.filter_control_unit == 0) {
if (inp->inp_vflag & INP_IPV6) {
bzero(&udp_dst6, sizeof(udp_dst6));
udp_dst6.uin6_sin.sin6_len = sizeof(struct sockaddr_in6);
udp_dst6.uin6_sin.sin6_family = AF_INET6;
in6_sin_2_v4mapsin6(&udp_dst, &udp_dst6.uin6_sin);
append_da = SA(&udp_dst6.uin6_sin);
} else {
SOCKADDR_ZERO(&udp_dst, sizeof(udp_dst));
udp_dst.sin_len = sizeof(struct sockaddr_in);
udp_dst.sin_family = AF_INET;
udp_dst.sin_port = uh->uh_dport;
udp_dst.sin_addr = ip->ip_dst;
append_da = SA(&udp_dst);
}
// Override the dst input here so NECP can pick up the policy
// and CFIL can find an existing control socket.
necp_socket_find_policy_match(inp, append_da, append_sa, 0);
}
#endif /* CONTENT_FILTER and NECP */
so_recv_data_stat(inp->inp_socket, m, 0);
if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa,
m, opts, NULL) == 0) {
udpstat.udps_fullsock++;
} else {
sorwakeup(inp->inp_socket);
}
if (is_wake_pkt) {
soevent(inp->inp_socket, SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
}
udp_unlock(inp->inp_socket, 1, 0);
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
return;
bad:
m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, drop_reason, NULL, 0);
if (opts) {
m_freem(opts);
}
KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
}
static void
ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
{
bzero(ip6, sizeof(*ip6));
ip6->ip6_vfc = IPV6_VERSION;
ip6->ip6_plen = ip->ip_len;
ip6->ip6_nxt = ip->ip_p;
ip6->ip6_hlim = ip->ip_ttl;
if (ip->ip_src.s_addr) {
ip6->ip6_src.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
}
if (ip->ip_dst.s_addr) {
ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
}
}
/*
* subroutine of udp_input(), mainly for source code readability.
*/
static void
udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6,
struct udp_ip6 *pudp_ip6, struct ifnet *ifp)
{
struct sockaddr *append_sa;
mbuf_ref_t opts = NULL;
int ret = 0;
if ((last->inp_flags & INP_CONTROLOPTS) != 0 ||
SOFLOW_ENABLED(last->inp_socket) ||
SO_RECV_CONTROL_OPTS(last->inp_socket)) {
if (last->inp_vflag & INP_IPV6 || last->inp_vflag & INP_V4MAPPEDV6) {
int savedflags;
if (pudp_ip6->uip6_init_done == 0) {
ip_2_ip6_hdr(&pudp_ip6->uip6_ip6, ip);
pudp_ip6->uip6_init_done = 1;
}
savedflags = last->inp_flags;
last->inp_flags &= ~INP_UNMAPPABLEOPTS;
ret = ip6_savecontrol(last, n, &opts);
if (ret != 0) {
last->inp_flags = savedflags;
UDP_LOG(last, "ip6_savecontrol error %d", ret);
goto error;
}
last->inp_flags = savedflags;
} else {
ret = ip_savecontrol(last, &opts, ip, n);
if (ret != 0) {
UDP_LOG(last, "ip_savecontrol error %d", ret);
goto error;
}
}
}
if (last->inp_vflag & INP_IPV6) {
if (pudp_in6->uin6_init_done == 0) {
in6_sin_2_v4mapsin6(pudp_in, &pudp_in6->uin6_sin);
pudp_in6->uin6_init_done = 1;
}
append_sa = SA(&pudp_in6->uin6_sin);
} else {
append_sa = SA(pudp_in);
}
if (nstat_collect) {
stats_functional_type ifnet_count_type = IFNET_COUNT_TYPE(ifp);
INP_ADD_STAT(last, ifnet_count_type, rxpackets, 1);
INP_ADD_STAT(last, ifnet_count_type, rxbytes,
n->m_pkthdr.len);
inp_set_activity_bitmap(last);
}
so_recv_data_stat(last->inp_socket, n, 0);
m_adj(n, off);
if (sbappendaddr(&last->inp_socket->so_rcv, append_sa,
n, opts, NULL) == 0) {
udpstat.udps_fullsock++;
UDP_LOG(last, "sbappendaddr full receive socket buffer");
} else {
sorwakeup(last->inp_socket);
}
return;
error:
m_freem(n);
m_freem(opts);
}
/*
* Notify a udp user of an asynchronous error;
* just wake up so that he can collect error status.
*/
void
udp_notify(struct inpcb *inp, int errno)
{
inp->inp_socket->so_error = (u_short)errno;
sorwakeup(inp->inp_socket);
sowwakeup(inp->inp_socket);
}
void
udp_ctlinput(int cmd, struct sockaddr *sa, void *vip, __unused struct ifnet * ifp)
{
struct ipctlparam *__single ctl_param = vip;
struct ip *ip = NULL;
mbuf_ref_t m = NULL;
void (*notify)(struct inpcb *, int) = udp_notify;
struct in_addr faddr;
struct inpcb *inp = NULL;
struct icmp *icp = NULL;
size_t off;
if (ctl_param != NULL) {
ip = ctl_param->ipc_icmp_ip;
icp = ctl_param->ipc_icmp;
m = ctl_param->ipc_m;
off = ctl_param->ipc_off;
} else {
ip = NULL;
icp = NULL;
m = NULL;
off = 0;
}
faddr = SIN(sa)->sin_addr;
if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) {
return;
}
if (PRC_IS_REDIRECT(cmd)) {
ip = 0;
notify = in_rtchange;
} else if (cmd == PRC_HOSTDEAD) {
ip = 0;
} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
return;
}
if (ip) {
struct udphdr uh;
/* Check if we can safely get the ports from the UDP header */
if (m == NULL ||
(m->m_len < off + sizeof(uh))) {
/* Insufficient length */
return;
}
bcopy(m_mtod_current(m) + off, &uh, sizeof(uh));
inp = in_pcblookup_hash(&udbinfo, faddr, uh.uh_dport,
ip->ip_src, uh.uh_sport, 0, NULL);
if (inp != NULL && inp->inp_socket != NULL) {
udp_lock(inp->inp_socket, 1, 0);
if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
WNT_STOPUSING) {
udp_unlock(inp->inp_socket, 1, 0);
return;
}
if (cmd == PRC_MSGSIZE && !uuid_is_null(inp->necp_client_uuid)) {
uuid_t null_uuid;
uuid_clear(null_uuid);
necp_update_flow_protoctl_event(null_uuid, inp->necp_client_uuid,
PRC_MSGSIZE, ntohs(icp->icmp_nextmtu), 0);