-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathin_arp.c
1613 lines (1463 loc) · 46.5 KB
/
in_arp.c
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) 2004-2012 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, 1989, 1993
* 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.
*
*/
#include <kern/debug.h>
#include <netinet/in_arp.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/kernel_types.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/sysctl.h>
#include <sys/mcache.h>
#include <sys/protosw.h>
#include <string.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/dlil.h>
#include <net/if_types.h>
#include <net/if_llreach.h>
#include <net/route.h>
#include <netinet/if_ether.h>
#include <netinet/in_var.h>
#include <kern/zalloc.h>
#define CONST_LLADDR(s) ((const u_char*)((s)->sdl_data + (s)->sdl_nlen))
#define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
static const size_t MAX_HW_LEN = 10;
SYSCTL_DECL(_net_link_ether);
SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "");
/* timer values */
static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
static int arpt_down = 20; /* once declared down, don't send for 20 sec */
/* Apple Hardware SUM16 checksuming */
int apple_hwcksum_tx = 1;
int apple_hwcksum_rx = 1;
static int arp_llreach_base = (LL_BASE_REACHABLE / 1000); /* seconds */
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl,
CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_prune, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age,
CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_keep, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time,
CTLFLAG_RW | CTLFLAG_LOCKED, &arpt_down, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_tx,
CTLFLAG_RW | CTLFLAG_LOCKED, &apple_hwcksum_tx, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, apple_hwcksum_rx,
CTLFLAG_RW | CTLFLAG_LOCKED, &apple_hwcksum_rx, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, arp_llreach_base,
CTLFLAG_RW | CTLFLAG_LOCKED, &arp_llreach_base, LL_BASE_REACHABLE,
"default ARP link-layer reachability max lifetime (in seconds)");
struct llinfo_arp {
/*
* The following are protected by rnh_lock
*/
LIST_ENTRY(llinfo_arp) la_le;
struct rtentry *la_rt;
/*
* The following are protected by rt_lock
*/
struct mbuf *la_hold; /* last packet until resolved/timeout */
struct if_llreach *la_llreach; /* link-layer reachability record */
u_int64_t la_lastused; /* last used timestamp */
u_int32_t la_asked; /* # of requests sent */
u_int32_t la_persist; /* expirable, but stays around */
};
/*
* Synchronization notes:
*
* The global list of ARP entries are stored in llinfo_arp; an entry
* gets inserted into the list when the route is created and gets
* removed from the list when it is deleted; this is done as part
* of RTM_ADD/RTM_RESOLVE/RTM_DELETE in arp_rtrequest().
*
* Because rnh_lock and rt_lock for the entry are held during those
* operations, the same locks (and thus lock ordering) must be used
* elsewhere to access the relevant data structure fields:
*
* la_le.{le_next,le_prev}, la_rt
*
* - Routing lock (rnh_lock)
*
* la_hold, la_asked, la_llreach, la_lastused
*
* - Routing entry lock (rt_lock)
*
* Due to the dependency on rt_lock, llinfo_arp has the same lifetime
* as the route entry itself. When a route is deleted (RTM_DELETE),
* it is simply removed from the global list but the memory is not
* freed until the route itself is freed.
*/
static LIST_HEAD(, llinfo_arp) llinfo_arp;
static int arp_inuse, arp_allocated;
static u_int32_t arp_maxtries = 5;
static int useloopback = 1; /* use loopback interface for local traffic */
static int arp_proxyall = 0;
static int arp_sendllconflict = 0;
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW | CTLFLAG_LOCKED,
&arp_maxtries, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW | CTLFLAG_LOCKED,
&useloopback, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW | CTLFLAG_LOCKED,
&arp_proxyall, 0, "");
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, sendllconflict, CTLFLAG_RW | CTLFLAG_LOCKED,
&arp_sendllconflict, 0, "");
static int log_arp_warnings = 0; /* Thread safe: no accumulated state */
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_warnings,
CTLFLAG_RW | CTLFLAG_LOCKED,
&log_arp_warnings, 0,
"log arp warning messages");
static int keep_announcements = 1; /* Thread safe: no aging of state */
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, keep_announcements,
CTLFLAG_RW | CTLFLAG_LOCKED,
&keep_announcements, 0,
"keep arp announcements");
static int send_conflicting_probes = 1; /* Thread safe: no accumulated state */
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, send_conflicting_probes,
CTLFLAG_RW | CTLFLAG_LOCKED,
&send_conflicting_probes, 0,
"send conflicting link-local arp probes");
static errno_t arp_lookup_route(const struct in_addr *, int,
int, route_t *, unsigned int);
static void arptimer(void *);
static struct llinfo_arp *arp_llinfo_alloc(void);
static void arp_llinfo_free(void *);
static void arp_llinfo_purge(struct rtentry *);
static void arp_llinfo_get_ri(struct rtentry *, struct rt_reach_info *);
static void arp_llinfo_get_iflri(struct rtentry *, struct ifnet_llreach_info *);
static __inline void arp_llreach_use(struct llinfo_arp *);
static __inline int arp_llreach_reachable(struct llinfo_arp *);
static void arp_llreach_alloc(struct rtentry *, struct ifnet *, void *,
unsigned int, boolean_t);
extern u_int32_t ipv4_ll_arp_aware;
static int arpinit_done;
static struct zone *llinfo_arp_zone;
#define LLINFO_ARP_ZONE_MAX 256 /* maximum elements in zone */
#define LLINFO_ARP_ZONE_NAME "llinfo_arp" /* name for zone */
void
arp_init(void)
{
if (arpinit_done) {
log(LOG_NOTICE, "arp_init called more than once (ignored)\n");
return;
}
LIST_INIT(&llinfo_arp);
llinfo_arp_zone = zinit(sizeof (struct llinfo_arp),
LLINFO_ARP_ZONE_MAX * sizeof (struct llinfo_arp), 0,
LLINFO_ARP_ZONE_NAME);
if (llinfo_arp_zone == NULL)
panic("%s: failed allocating llinfo_arp_zone", __func__);
zone_change(llinfo_arp_zone, Z_EXPAND, TRUE);
zone_change(llinfo_arp_zone, Z_CALLERACCT, FALSE);
arpinit_done = 1;
/* start timer */
timeout(arptimer, (caddr_t)0, hz);
}
static struct llinfo_arp *
arp_llinfo_alloc(void)
{
return (zalloc(llinfo_arp_zone));
}
static void
arp_llinfo_free(void *arg)
{
struct llinfo_arp *la = arg;
if (la->la_le.le_next != NULL || la->la_le.le_prev != NULL) {
panic("%s: trying to free %p when it is in use", __func__, la);
/* NOTREACHED */
}
/* Just in case there's anything there, free it */
if (la->la_hold != NULL) {
m_freem(la->la_hold);
la->la_hold = NULL;
}
/* Purge any link-layer info caching */
VERIFY(la->la_rt->rt_llinfo == la);
if (la->la_rt->rt_llinfo_purge != NULL)
la->la_rt->rt_llinfo_purge(la->la_rt);
zfree(llinfo_arp_zone, la);
}
static void
arp_llinfo_purge(struct rtentry *rt)
{
struct llinfo_arp *la = rt->rt_llinfo;
RT_LOCK_ASSERT_HELD(rt);
VERIFY(rt->rt_llinfo_purge == arp_llinfo_purge && la != NULL);
if (la->la_llreach != NULL) {
RT_CONVERT_LOCK(rt);
ifnet_llreach_free(la->la_llreach);
la->la_llreach = NULL;
}
la->la_lastused = 0;
}
static void
arp_llinfo_get_ri(struct rtentry *rt, struct rt_reach_info *ri)
{
struct llinfo_arp *la = rt->rt_llinfo;
struct if_llreach *lr = la->la_llreach;
if (lr == NULL) {
bzero(ri, sizeof (*ri));
ri->ri_rssi = IFNET_RSSI_UNKNOWN;
ri->ri_lqm = IFNET_LQM_THRESH_OFF;
ri->ri_npm = IFNET_NPM_THRESH_UNKNOWN;
} else {
IFLR_LOCK(lr);
/* Export to rt_reach_info structure */
ifnet_lr2ri(lr, ri);
/* Export ARP send expiration (calendar) time */
ri->ri_snd_expire =
ifnet_llreach_up2calexp(lr, la->la_lastused);
IFLR_UNLOCK(lr);
}
}
static void
arp_llinfo_get_iflri(struct rtentry *rt, struct ifnet_llreach_info *iflri)
{
struct llinfo_arp *la = rt->rt_llinfo;
struct if_llreach *lr = la->la_llreach;
if (lr == NULL) {
bzero(iflri, sizeof (*iflri));
iflri->iflri_rssi = IFNET_RSSI_UNKNOWN;
iflri->iflri_lqm = IFNET_LQM_THRESH_OFF;
iflri->iflri_npm = IFNET_NPM_THRESH_UNKNOWN;
} else {
IFLR_LOCK(lr);
/* Export to ifnet_llreach_info structure */
ifnet_lr2iflri(lr, iflri);
/* Export ARP send expiration (uptime) time */
iflri->iflri_snd_expire =
ifnet_llreach_up2upexp(lr, la->la_lastused);
IFLR_UNLOCK(lr);
}
}
void
arp_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen)
{
/* Nothing more to do if it's disabled */
if (arp_llreach_base == 0)
return;
ifnet_llreach_set_reachable(ifp, ETHERTYPE_IP, addr, alen);
}
static __inline void
arp_llreach_use(struct llinfo_arp *la)
{
if (la->la_llreach != NULL)
la->la_lastused = net_uptime();
}
static __inline int
arp_llreach_reachable(struct llinfo_arp *la)
{
struct if_llreach *lr;
const char *why = NULL;
/* Nothing more to do if it's disabled; pretend it's reachable */
if (arp_llreach_base == 0)
return (1);
if ((lr = la->la_llreach) == NULL) {
/*
* Link-layer reachability record isn't present for this
* ARP entry; pretend it's reachable and use it as is.
*/
return (1);
} else if (ifnet_llreach_reachable(lr)) {
/*
* Record is present, it's not shared with other ARP
* entries and a packet has recently been received
* from the remote host; consider it reachable.
*/
if (lr->lr_reqcnt == 1)
return (1);
/* Prime it up, if this is the first time */
if (la->la_lastused == 0) {
VERIFY(la->la_llreach != NULL);
arp_llreach_use(la);
}
/*
* Record is present and shared with one or more ARP
* entries, and a packet has recently been received
* from the remote host. Since it's shared by more
* than one IP addresses, we can't rely on the link-
* layer reachability alone; consider it reachable if
* this ARP entry has been used "recently."
*/
if (ifnet_llreach_reachable_delta(lr, la->la_lastused))
return (1);
why = "has alias(es) and hasn't been used in a while";
} else {
why = "haven't heard from it in a while";
}
if (log_arp_warnings) {
char tmp[MAX_IPv4_STR_LEN];
u_int64_t now = net_uptime();
log(LOG_DEBUG, "%s%d: ARP probe(s) needed for %s; "
"%s [lastused %lld, lastrcvd %lld] secs ago\n",
lr->lr_ifp->if_name, lr->lr_ifp->if_unit, inet_ntop(AF_INET,
&SIN(rt_key(la->la_rt))->sin_addr, tmp, sizeof (tmp)), why,
(la->la_lastused ? (int64_t)(now - la->la_lastused) : -1),
(lr->lr_lastrcvd ? (int64_t)(now - lr->lr_lastrcvd) : -1));
}
return (0);
}
/*
* Obtain a link-layer source cache entry for the sender.
*
* NOTE: This is currently only for ARP/Ethernet.
*/
static void
arp_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr,
unsigned int alen, boolean_t solicited)
{
VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
if (arp_llreach_base != 0 &&
rt->rt_expire != 0 && rt->rt_ifp != lo_ifp &&
ifp->if_addrlen == IF_LLREACH_MAXLEN && /* Ethernet */
alen == ifp->if_addrlen) {
struct llinfo_arp *la = rt->rt_llinfo;
struct if_llreach *lr;
const char *why = NULL, *type = "";
/* Become a regular mutex, just in case */
RT_CONVERT_LOCK(rt);
if ((lr = la->la_llreach) != NULL) {
type = (solicited ? "ARP reply" : "ARP announcement");
/*
* If target has changed, create a new record;
* otherwise keep existing record.
*/
IFLR_LOCK(lr);
if (bcmp(addr, lr->lr_key.addr, alen) != 0) {
IFLR_UNLOCK(lr);
/* Purge any link-layer info caching */
VERIFY(rt->rt_llinfo_purge != NULL);
rt->rt_llinfo_purge(rt);
lr = NULL;
why = " for different target HW address; "
"using new llreach record";
} else {
lr->lr_probes = 0; /* reset probe count */
IFLR_UNLOCK(lr);
if (solicited) {
why = " for same target HW address; "
"keeping existing llreach record";
}
}
}
if (lr == NULL) {
lr = la->la_llreach = ifnet_llreach_alloc(ifp,
ETHERTYPE_IP, addr, alen, arp_llreach_base);
if (lr != NULL) {
lr->lr_probes = 0; /* reset probe count */
if (why == NULL)
why = "creating new llreach record";
}
}
if (log_arp_warnings && lr != NULL && why != NULL) {
char tmp[MAX_IPv4_STR_LEN];
log(LOG_DEBUG, "%s%d: %s%s for %s\n", ifp->if_name,
ifp->if_unit, type, why, inet_ntop(AF_INET,
&SIN(rt_key(rt))->sin_addr, tmp, sizeof (tmp)));
}
}
}
/*
* Free an arp entry.
*/
static void
arptfree(struct llinfo_arp *la)
{
struct rtentry *rt = la->la_rt;
struct sockaddr_dl *sdl;
lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
RT_LOCK_ASSERT_HELD(rt);
if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
sdl->sdl_family == AF_LINK) {
sdl->sdl_alen = 0;
la->la_asked = 0;
rt->rt_flags &= ~RTF_REJECT;
RT_UNLOCK(rt);
} else if (la->la_persist) {
/*
* Instead of issuing RTM_DELETE, stop this route entry
* from holding an interface idle reference count; if
* the route is later reused, arp_validate() will revert
* this action.
*/
if (rt->rt_refcnt == 0)
rt_clear_idleref(rt);
RT_UNLOCK(rt);
} else {
/*
* Safe to drop rt_lock and use rt_key, since holding
* rnh_lock here prevents another thread from calling
* rt_setgate() on this route.
*/
RT_UNLOCK(rt);
rtrequest_locked(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
0, NULL);
}
}
void
in_arpdrain(void *ignored_arg)
{
#pragma unused (ignored_arg)
struct llinfo_arp *la, *ola;
uint64_t timenow;
lck_mtx_lock(rnh_lock);
la = llinfo_arp.lh_first;
timenow = net_uptime();
while ((ola = la) != 0) {
struct rtentry *rt = la->la_rt;
la = la->la_le.le_next;
RT_LOCK(rt);
VERIFY(rt->rt_expire == 0 || rt->rt_rmx.rmx_expire != 0);
VERIFY(rt->rt_expire != 0 || rt->rt_rmx.rmx_expire == 0);
if (rt->rt_expire && rt->rt_expire <= timenow)
arptfree(ola); /* timer has expired, clear */
else
RT_UNLOCK(rt);
}
lck_mtx_unlock(rnh_lock);
}
void
arp_validate(struct rtentry *rt)
{
struct llinfo_arp *la = rt->rt_llinfo;
RT_LOCK_ASSERT_HELD(rt);
/*
* If this is a persistent ARP entry, make it count towards the
* interface idleness just like before arptfree() was called.
*/
if (la->la_persist)
rt_set_idleref(rt);
}
/*
* Timeout routine. Age arp_tab entries periodically.
*/
/* ARGSUSED */
static void
arptimer(void *ignored_arg)
{
#pragma unused (ignored_arg)
in_arpdrain(NULL);
timeout(arptimer, (caddr_t)0, arpt_prune * hz);
}
/*
* Parallel to llc_rtrequest.
*/
static void
arp_rtrequest(
int req,
struct rtentry *rt,
__unused struct sockaddr *sa)
{
struct sockaddr *gate = rt->rt_gateway;
struct llinfo_arp *la = rt->rt_llinfo;
static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK, 0, 0, 0, 0, 0, {0}};
uint64_t timenow;
if (!arpinit_done) {
panic("%s: ARP has not been initialized", __func__);
/* NOTREACHED */
}
lck_mtx_assert(rnh_lock, LCK_MTX_ASSERT_OWNED);
RT_LOCK_ASSERT_HELD(rt);
if (rt->rt_flags & RTF_GATEWAY)
return;
timenow = net_uptime();
switch (req) {
case RTM_ADD:
/*
* XXX: If this is a manually added route to interface
* such as older version of routed or gated might provide,
* restore cloning bit.
*/
if ((rt->rt_flags & RTF_HOST) == 0 && rt_mask(rt) != NULL &&
SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
rt->rt_flags |= RTF_CLONING;
if (rt->rt_flags & RTF_CLONING) {
/*
* Case 1: This route should come from a route to iface.
*/
if (rt_setgate(rt, rt_key(rt),
(struct sockaddr *)&null_sdl) == 0) {
gate = rt->rt_gateway;
SDL(gate)->sdl_type = rt->rt_ifp->if_type;
SDL(gate)->sdl_index = rt->rt_ifp->if_index;
/*
* In case we're called before 1.0 sec.
* has elapsed.
*/
rt_setexpire(rt, MAX(timenow, 1));
}
break;
}
/* Announce a new entry if requested. */
if (rt->rt_flags & RTF_ANNOUNCE) {
if (la != NULL)
arp_llreach_use(la); /* Mark use timestamp */
RT_UNLOCK(rt);
dlil_send_arp(rt->rt_ifp, ARPOP_REQUEST,
SDL(gate), rt_key(rt), NULL, rt_key(rt), 0);
RT_LOCK(rt);
}
/*FALLTHROUGH*/
case RTM_RESOLVE:
if (gate->sa_family != AF_LINK ||
gate->sa_len < sizeof(null_sdl)) {
if (log_arp_warnings)
log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
break;
}
SDL(gate)->sdl_type = rt->rt_ifp->if_type;
SDL(gate)->sdl_index = rt->rt_ifp->if_index;
if (la != 0)
break; /* This happens on a route change */
/*
* Case 2: This route may come from cloning, or a manual route
* add with a LL address.
*/
rt->rt_llinfo = la = arp_llinfo_alloc();
if (la == NULL) {
if (log_arp_warnings)
log(LOG_DEBUG, "%s: malloc failed\n", __func__);
break;
}
rt->rt_llinfo_get_ri = arp_llinfo_get_ri;
rt->rt_llinfo_get_iflri = arp_llinfo_get_iflri;
rt->rt_llinfo_purge = arp_llinfo_purge;
rt->rt_llinfo_free = arp_llinfo_free;
arp_inuse++, arp_allocated++;
Bzero(la, sizeof(*la));
la->la_rt = rt;
rt->rt_flags |= RTF_LLINFO;
LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
/*
* This keeps the multicast addresses from showing up
* in `arp -a' listings as unresolved. It's not actually
* functional. Then the same for broadcast. For IPv4
* link-local address, keep the entry around even after
* it has expired.
*/
if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
RT_UNLOCK(rt);
dlil_resolve_multi(rt->rt_ifp, rt_key(rt), gate,
sizeof(struct sockaddr_dl));
RT_LOCK(rt);
rt_setexpire(rt, 0);
}
else if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
struct sockaddr_dl *gate_ll = SDL(gate);
size_t broadcast_len;
ifnet_llbroadcast_copy_bytes(rt->rt_ifp,
LLADDR(gate_ll), sizeof(gate_ll->sdl_data),
&broadcast_len);
gate_ll->sdl_alen = broadcast_len;
gate_ll->sdl_family = AF_LINK;
gate_ll->sdl_len = sizeof(struct sockaddr_dl);
/* In case we're called before 1.0 sec. has elapsed */
rt_setexpire(rt, MAX(timenow, 1));
} else if (IN_LINKLOCAL(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
/*
* The persistent bit implies that once the ARP
* entry has reached it expiration time, the idle
* reference count to the interface will be released,
* but the ARP entry itself stays in the routing table
* until it is explicitly removed.
*/
la->la_persist = 1;
rt->rt_flags |= RTF_STATIC;
}
/* Become a regular mutex, just in case */
RT_CONVERT_LOCK(rt);
IFA_LOCK_SPIN(rt->rt_ifa);
if (SIN(rt_key(rt))->sin_addr.s_addr ==
(IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
IFA_UNLOCK(rt->rt_ifa);
/*
* This test used to be
* if (loif.if_flags & IFF_UP)
* It allowed local traffic to be forced through the
* hardware by configuring the loopback down. However,
* it causes problems during network configuration
* for boards that can't receive packets they send.
* It is now necessary to clear "useloopback" and
* remove the route to force traffic out to the
* hardware.
*/
rt_setexpire(rt, 0);
ifnet_lladdr_copy_bytes(rt->rt_ifp, LLADDR(SDL(gate)),
SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
if (useloopback) {
if (rt->rt_ifp != lo_ifp) {
/*
* Purge any link-layer info caching.
*/
if (rt->rt_llinfo_purge != NULL)
rt->rt_llinfo_purge(rt);
/*
* Adjust route ref count for the
* interfaces.
*/
if (rt->rt_if_ref_fn != NULL) {
rt->rt_if_ref_fn(lo_ifp, 1);
rt->rt_if_ref_fn(rt->rt_ifp, -1);
}
}
rt->rt_ifp = lo_ifp;
}
} else {
IFA_UNLOCK(rt->rt_ifa);
}
break;
case RTM_DELETE:
if (la == 0)
break;
arp_inuse--;
/*
* Unchain it but defer the actual freeing until the route
* itself is to be freed. rt->rt_llinfo still points to
* llinfo_arp, and likewise, la->la_rt still points to this
* route entry, except that RTF_LLINFO is now cleared.
*/
LIST_REMOVE(la, la_le);
la->la_le.le_next = NULL;
la->la_le.le_prev = NULL;
/*
* Purge any link-layer info caching.
*/
if (rt->rt_llinfo_purge != NULL)
rt->rt_llinfo_purge(rt);
rt->rt_flags &= ~RTF_LLINFO;
if (la->la_hold != NULL) {
m_freem(la->la_hold);
la->la_hold = NULL;
}
}
}
/*
* convert hardware address to hex string for logging errors.
*/
static const char *
sdl_addr_to_hex(const struct sockaddr_dl *sdl, char * orig_buf, int buflen)
{
char * buf = orig_buf;
int i;
const u_char * lladdr = (u_char *)(size_t)sdl->sdl_data;
int maxbytes = buflen / 3;
if (maxbytes > sdl->sdl_alen) {
maxbytes = sdl->sdl_alen;
}
*buf = '\0';
for (i = 0; i < maxbytes; i++) {
snprintf(buf, 3, "%02x", lladdr[i]);
buf += 2;
*buf = (i == maxbytes - 1) ? '\0' : ':';
buf++;
}
return (orig_buf);
}
/*
* arp_lookup_route will lookup the route for a given address.
*
* The address must be for a host on a local network on this interface.
* If the returned route is non-NULL, the route is locked and the caller
* is responsible for unlocking it and releasing its reference.
*/
static errno_t
arp_lookup_route(const struct in_addr *addr, int create, int proxy,
route_t *route, unsigned int ifscope)
{
struct sockaddr_inarp sin = {sizeof(sin), AF_INET, 0, {0}, {0}, 0, 0};
const char *why = NULL;
errno_t error = 0;
route_t rt;
*route = NULL;
sin.sin_addr.s_addr = addr->s_addr;
sin.sin_other = proxy ? SIN_PROXY : 0;
/*
* If the destination is a link-local address, don't
* constrain the lookup (don't scope it).
*/
if (IN_LINKLOCAL(ntohl(addr->s_addr)))
ifscope = IFSCOPE_NONE;
rt = rtalloc1_scoped((struct sockaddr*)&sin, create, 0, ifscope);
if (rt == NULL)
return (ENETUNREACH);
RT_LOCK(rt);
if (rt->rt_flags & RTF_GATEWAY) {
why = "host is not on local network";
error = ENETUNREACH;
} else if (!(rt->rt_flags & RTF_LLINFO)) {
why = "could not allocate llinfo";
error = ENOMEM;
} else if (rt->rt_gateway->sa_family != AF_LINK) {
why = "gateway route is not ours";
error = EPROTONOSUPPORT;
}
if (error != 0) {
if (create && log_arp_warnings) {
char tmp[MAX_IPv4_STR_LEN];
log(LOG_DEBUG, "arplookup link#%d %s failed: %s\n",
ifscope, inet_ntop(AF_INET, addr, tmp,
sizeof (tmp)), why);
}
/*
* If there are no references to this route, and it is
* a cloned route, and not static, and ARP had created
* the route, then purge it from the routing table as
* it is probably bogus.
*/
if (rt->rt_refcnt == 1 &&
(rt->rt_flags & (RTF_WASCLONED | RTF_STATIC)) ==
RTF_WASCLONED) {
/*
* Prevent another thread from modiying rt_key,
* rt_gateway via rt_setgate() after rt_lock is
* dropped by marking the route as defunct.
*/
rt->rt_flags |= RTF_CONDEMNED;
RT_UNLOCK(rt);
rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
rt_mask(rt), rt->rt_flags, 0);
rtfree(rt);
} else {
RT_REMREF_LOCKED(rt);
RT_UNLOCK(rt);
}
return (error);
}
/*
* Caller releases reference and does RT_UNLOCK(rt).
*/
*route = rt;
return (0);
}
/*
* This is the ARP pre-output routine; care must be taken to ensure that
* the "hint" route never gets freed via rtfree(), since the caller may
* have stored it inside a struct route with a reference held for that
* placeholder.
*/
errno_t
arp_lookup_ip(ifnet_t ifp, const struct sockaddr_in *net_dest,
struct sockaddr_dl *ll_dest, size_t ll_dest_len, route_t hint,
mbuf_t packet)
{
route_t route = NULL; /* output route */
errno_t result = 0;
struct sockaddr_dl *gateway;
struct llinfo_arp *llinfo = NULL;
uint64_t timenow;
int unreachable = 0;
if (net_dest->sin_family != AF_INET)
return (EAFNOSUPPORT);
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
return (ENETDOWN);
/*
* If we were given a route, verify the route and grab the gateway
*/
if (hint != NULL) {
/*
* Callee holds a reference on the route and returns
* with the route entry locked, upon success.
*/
result = route_to_gwroute((const struct sockaddr *)
net_dest, hint, &route);
if (result != 0)
return (result);
if (route != NULL)
RT_LOCK_ASSERT_HELD(route);
}
if (packet->m_flags & M_BCAST) {
size_t broadcast_len;
bzero(ll_dest, ll_dest_len);
result = ifnet_llbroadcast_copy_bytes(ifp, LLADDR(ll_dest),
ll_dest_len - offsetof(struct sockaddr_dl, sdl_data),
&broadcast_len);
if (result == 0) {
ll_dest->sdl_alen = broadcast_len;
ll_dest->sdl_family = AF_LINK;
ll_dest->sdl_len = sizeof(struct sockaddr_dl);
}
goto release;
}
if (packet->m_flags & M_MCAST) {
if (route != NULL)
RT_UNLOCK(route);
result = dlil_resolve_multi(ifp,
(const struct sockaddr*)net_dest,
(struct sockaddr*)ll_dest, ll_dest_len);
if (route != NULL)
RT_LOCK(route);
goto release;
}
/*
* If we didn't find a route, or the route doesn't have
* link layer information, trigger the creation of the
* route and link layer information.
*/
if (route == NULL || route->rt_llinfo == NULL) {
/* Clean up now while we can */
if (route != NULL) {
if (route == hint) {
RT_REMREF_LOCKED(route);
RT_UNLOCK(route);
} else {
RT_UNLOCK(route);
rtfree(route);
}
}
/*
* Callee holds a reference on the route and returns
* with the route entry locked, upon success.
*/
result = arp_lookup_route(&net_dest->sin_addr, 1, 0, &route,
ifp->if_index);
if (result == 0)
RT_LOCK_ASSERT_HELD(route);
}
if (result || route == NULL || (llinfo = route->rt_llinfo) == NULL) {
char tmp[MAX_IPv4_STR_LEN];
/* In case result is 0 but no route, return an error */
if (result == 0)
result = EHOSTUNREACH;
if (log_arp_warnings &&
route != NULL && route->rt_llinfo == NULL)
log(LOG_DEBUG, "arpresolve: can't allocate llinfo "
"for %s\n", inet_ntop(AF_INET, &net_dest->sin_addr,
tmp, sizeof(tmp)));
goto release;
}
/*
* Now that we have the right route, is it filled in?
*/
gateway = SDL(route->rt_gateway);
timenow = net_uptime();
VERIFY(route->rt_expire == 0 || route->rt_rmx.rmx_expire != 0);
VERIFY(route->rt_expire != 0 || route->rt_rmx.rmx_expire == 0);
if ((route->rt_expire == 0 ||