-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathip_ndp.c
More file actions
5348 lines (4919 loc) · 144 KB
/
Copy pathip_ndp.c
File metadata and controls
5348 lines (4919 loc) · 144 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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright (c) 2019, Joyent, Inc.
*/
#include <sys/types.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strsun.h>
#include <sys/sysmacros.h>
#include <sys/errno.h>
#include <sys/dlpi.h>
#include <sys/socket.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/vtrace.h>
#include <sys/kmem.h>
#include <sys/zone.h>
#include <sys/ethernet.h>
#include <sys/sdt.h>
#include <sys/mac.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <inet/common.h>
#include <inet/mi.h>
#include <inet/mib2.h>
#include <inet/nd.h>
#include <inet/ip.h>
#include <inet/ip_impl.h>
#include <inet/ipclassifier.h>
#include <inet/ip_if.h>
#include <inet/ip_ire.h>
#include <inet/ip_rts.h>
#include <inet/ip6.h>
#include <inet/ip_ndp.h>
#include <inet/sctp_ip.h>
#include <inet/ip_arp.h>
#include <inet/ip2mac_impl.h>
#define ANNOUNCE_INTERVAL(isv6) \
(isv6 ? ipst->ips_ip_ndp_unsolicit_interval : \
ipst->ips_ip_arp_publish_interval)
#define DEFENSE_INTERVAL(isv6) \
(isv6 ? ipst->ips_ndp_defend_interval : \
ipst->ips_arp_defend_interval)
/* Non-tunable probe interval, based on link capabilities */
#define ILL_PROBE_INTERVAL(ill) ((ill)->ill_note_link ? 150 : 1500)
/*
* The IPv4 Link Local address space is special; we do extra duplicate checking
* there, as the entire assignment mechanism rests on random numbers.
*/
#define IS_IPV4_LL_SPACE(ptr) (((uchar_t *)ptr)[0] == 169 && \
((uchar_t *)ptr)[1] == 254)
/*
* NCE_EXTERNAL_FLAGS_MASK defines the set of ncec_flags that may be passed
* in to the ncec*add* functions.
*
* NCE_F_AUTHORITY means that we ignore any incoming adverts for that
* mapping (though DAD is performed for the mapping). NCE_F_PUBLISH means
* that we will respond to requests for the protocol address.
*/
#define NCE_EXTERNAL_FLAGS_MASK \
(NCE_F_MYADDR | NCE_F_ISROUTER | NCE_F_NONUD | \
NCE_F_ANYCAST | NCE_F_UNSOL_ADV | NCE_F_BCAST | NCE_F_MCAST | \
NCE_F_AUTHORITY | NCE_F_PUBLISH | NCE_F_STATIC)
/*
* Lock ordering:
*
* ndp_g_lock -> ill_lock -> ncec_lock
*
* The ndp_g_lock protects the NCE hash (nce_hash_tbl, NCE_HASH_PTR) and
* ncec_next. ncec_lock protects the contents of the NCE (particularly
* ncec_refcnt).
*/
static void nce_cleanup_list(ncec_t *ncec);
static void nce_set_ll(ncec_t *ncec, uchar_t *ll_addr);
static ncec_t *ncec_lookup_illgrp(ill_t *, const in6_addr_t *,
ncec_t *);
static nce_t *nce_lookup_addr(ill_t *, const in6_addr_t *);
static int nce_set_multicast_v6(ill_t *ill, const in6_addr_t *addr,
uint16_t ncec_flags, nce_t **newnce);
static int nce_set_multicast_v4(ill_t *ill, const in_addr_t *dst,
uint16_t ncec_flags, nce_t **newnce);
static boolean_t ndp_xmit(ill_t *ill, uint32_t operation,
uint8_t *hwaddr, uint_t hwaddr_len, const in6_addr_t *sender,
const in6_addr_t *target, int flag);
static void ncec_refhold_locked(ncec_t *);
static boolean_t ill_defend_rate_limit(ill_t *, ncec_t *);
static void nce_queue_mp_common(ncec_t *, mblk_t *, boolean_t);
static int nce_add_common(ill_t *, uchar_t *, uint_t, const in6_addr_t *,
uint16_t, uint16_t, nce_t **);
static nce_t *nce_add_impl(ill_t *, ncec_t *, nce_t *, mblk_t *, list_t *);
static nce_t *nce_add(ill_t *, ncec_t *, list_t *);
static void nce_inactive(nce_t *);
extern nce_t *nce_lookup(ill_t *, const in6_addr_t *);
static nce_t *nce_ill_lookup_then_add(ill_t *, ncec_t *);
static int nce_add_v6(ill_t *, uchar_t *, uint_t, const in6_addr_t *,
uint16_t, uint16_t, nce_t **);
static int nce_add_v4(ill_t *, uchar_t *, uint_t, const in_addr_t *,
uint16_t, uint16_t, nce_t **);
static int nce_add_v6_postprocess(nce_t *);
static int nce_add_v4_postprocess(nce_t *);
static ill_t *nce_resolve_src(ncec_t *, in6_addr_t *);
static clock_t nce_fuzz_interval(clock_t, boolean_t);
static void nce_resolv_ipmp_ok(ncec_t *);
static void nce_walk_common(ill_t *, pfi_t, void *);
static void nce_start_timer(ncec_t *, uint_t);
static nce_t *nce_fastpath_create(ill_t *, ncec_t *);
static void nce_fastpath_trigger(nce_t *);
static nce_t *nce_fastpath(ncec_t *, boolean_t, nce_t *);
#ifdef DEBUG
static void ncec_trace_cleanup(const ncec_t *);
#endif
#define NCE_HASH_PTR_V4(ipst, addr) \
(&((ipst)->ips_ndp4->nce_hash_tbl[IRE_ADDR_HASH(addr, NCE_TABLE_SIZE)]))
#define NCE_HASH_PTR_V6(ipst, addr) \
(&((ipst)->ips_ndp6->nce_hash_tbl[NCE_ADDR_HASH_V6(addr, \
NCE_TABLE_SIZE)]))
extern kmem_cache_t *ncec_cache;
extern kmem_cache_t *nce_cache;
/*
* Send out a IPv6 (unicast) or IPv4 (broadcast) DAD probe
* If src_ill is not null, the ncec_addr is bound to src_ill. The
* src_ill is ignored by nce_dad for IPv4 Neighbor Cache entries where
* the probe is sent on the ncec_ill (in the non-IPMP case) or the
* IPMP cast_ill (in the IPMP case).
*
* Note that the probe interval is based on the src_ill for IPv6, and
* the ncec_xmit_interval for IPv4.
*/
static void
nce_dad(ncec_t *ncec, ill_t *src_ill, boolean_t send_probe)
{
boolean_t dropped;
uint32_t probe_interval;
ASSERT(!(ncec->ncec_flags & NCE_F_MCAST));
ASSERT(!(ncec->ncec_flags & NCE_F_BCAST));
if (ncec->ncec_ipversion == IPV6_VERSION) {
dropped = ndp_xmit(src_ill, ND_NEIGHBOR_SOLICIT,
ncec->ncec_lladdr, ncec->ncec_lladdr_length,
&ipv6_all_zeros, &ncec->ncec_addr, NDP_PROBE);
probe_interval = ILL_PROBE_INTERVAL(src_ill);
} else {
/* IPv4 DAD delay the initial probe. */
if (send_probe)
dropped = arp_probe(ncec);
else
dropped = B_TRUE;
probe_interval = nce_fuzz_interval(ncec->ncec_xmit_interval,
!send_probe);
}
if (!dropped) {
mutex_enter(&ncec->ncec_lock);
ncec->ncec_pcnt--;
mutex_exit(&ncec->ncec_lock);
}
nce_restart_timer(ncec, probe_interval);
}
/*
* Compute default flags to use for an advertisement of this ncec's address.
*/
static int
nce_advert_flags(const ncec_t *ncec)
{
int flag = 0;
if (ncec->ncec_flags & NCE_F_ISROUTER)
flag |= NDP_ISROUTER;
if (!(ncec->ncec_flags & NCE_F_ANYCAST))
flag |= NDP_ORIDE;
return (flag);
}
/*
* NDP Cache Entry creation routine.
* This routine must always be called with ndp6->ndp_g_lock held.
*/
int
nce_add_v6(ill_t *ill, uchar_t *hw_addr, uint_t hw_addr_len,
const in6_addr_t *addr, uint16_t flags, uint16_t state, nce_t **newnce)
{
int err;
nce_t *nce;
ASSERT(MUTEX_HELD(&ill->ill_ipst->ips_ndp6->ndp_g_lock));
ASSERT(ill != NULL && ill->ill_isv6);
err = nce_add_common(ill, hw_addr, hw_addr_len, addr, flags, state,
&nce);
if (err != 0)
return (err);
ASSERT(newnce != NULL);
*newnce = nce;
return (err);
}
/*
* Post-processing routine to be executed after nce_add_v6(). This function
* triggers fastpath (if appropriate) and DAD on the newly added nce entry
* and must be called without any locks held.
*/
int
nce_add_v6_postprocess(nce_t *nce)
{
ncec_t *ncec = nce->nce_common;
boolean_t dropped = B_FALSE;
uchar_t *hw_addr = ncec->ncec_lladdr;
uint_t hw_addr_len = ncec->ncec_lladdr_length;
ill_t *ill = ncec->ncec_ill;
int err = 0;
uint16_t flags = ncec->ncec_flags;
ip_stack_t *ipst = ill->ill_ipst;
boolean_t trigger_fastpath = B_TRUE;
/*
* If the hw_addr is NULL, typically for ND_INCOMPLETE nces, then
* we call nce_fastpath as soon as the ncec is resolved in nce_process.
* We call nce_fastpath from nce_update if the link layer address of
* the peer changes from nce_update
*/
if (NCE_PUBLISH(ncec) || !NCE_ISREACHABLE(ncec) ||
(hw_addr == NULL && ill->ill_net_type != IRE_IF_NORESOLVER))
trigger_fastpath = B_FALSE;
if (trigger_fastpath)
nce_fastpath_trigger(nce);
if (NCE_PUBLISH(ncec) && ncec->ncec_state == ND_PROBE) {
ill_t *hwaddr_ill;
/*
* Unicast entry that needs DAD.
*/
if (IS_IPMP(ill)) {
hwaddr_ill = ipmp_illgrp_find_ill(ill->ill_grp,
hw_addr, hw_addr_len);
} else {
hwaddr_ill = ill;
}
nce_dad(ncec, hwaddr_ill, B_TRUE);
err = EINPROGRESS;
} else if (flags & NCE_F_UNSOL_ADV) {
/*
* We account for the transmit below by assigning one
* less than the ndd variable. Subsequent decrements
* are done in nce_timer.
*/
mutex_enter(&ncec->ncec_lock);
ncec->ncec_unsolicit_count =
ipst->ips_ip_ndp_unsolicit_count - 1;
mutex_exit(&ncec->ncec_lock);
dropped = ndp_xmit(ill,
ND_NEIGHBOR_ADVERT,
hw_addr,
hw_addr_len,
&ncec->ncec_addr, /* Source and target of the adv */
&ipv6_all_hosts_mcast, /* Destination of the packet */
nce_advert_flags(ncec));
mutex_enter(&ncec->ncec_lock);
if (dropped)
ncec->ncec_unsolicit_count++;
else
ncec->ncec_last_time_defended = ddi_get_lbolt();
if (ncec->ncec_unsolicit_count != 0) {
nce_start_timer(ncec,
ipst->ips_ip_ndp_unsolicit_interval);
}
mutex_exit(&ncec->ncec_lock);
}
return (err);
}
/*
* Atomically lookup and add (if needed) Neighbor Cache information for
* an address.
*
* IPMP notes: the ncec for non-local (i.e., !NCE_MYADDR(ncec) addresses
* are always added pointing at the ipmp_ill. Thus, when the ill passed
* to nce_add_v6 is an under_ill (i.e., IS_UNDER_IPMP(ill)) two nce_t
* entries will be created, both pointing at the same ncec_t. The nce_t
* entries will have their nce_ill set to the ipmp_ill and the under_ill
* respectively, with the ncec_t having its ncec_ill pointing at the ipmp_ill.
* Local addresses are always created on the ill passed to nce_add_v6.
*/
int
nce_lookup_then_add_v6(ill_t *ill, uchar_t *hw_addr, uint_t hw_addr_len,
const in6_addr_t *addr, uint16_t flags, uint16_t state, nce_t **newnce)
{
int err = 0;
ip_stack_t *ipst = ill->ill_ipst;
nce_t *nce, *upper_nce = NULL;
ill_t *in_ill = ill;
boolean_t need_ill_refrele = B_FALSE;
if (flags & NCE_F_MCAST) {
/*
* hw_addr will be figured out in nce_set_multicast_v6;
* caller has to select the cast_ill
*/
ASSERT(hw_addr == NULL);
ASSERT(!IS_IPMP(ill));
err = nce_set_multicast_v6(ill, addr, flags, newnce);
return (err);
}
ASSERT(ill->ill_isv6);
if (IS_UNDER_IPMP(ill) && !(flags & NCE_F_MYADDR)) {
ill = ipmp_ill_hold_ipmp_ill(ill);
if (ill == NULL)
return (ENXIO);
need_ill_refrele = B_TRUE;
}
mutex_enter(&ipst->ips_ndp6->ndp_g_lock);
nce = nce_lookup_addr(ill, addr);
if (nce == NULL) {
err = nce_add_v6(ill, hw_addr, hw_addr_len, addr, flags, state,
&nce);
} else {
err = EEXIST;
}
mutex_exit(&ipst->ips_ndp6->ndp_g_lock);
if (err == 0)
err = nce_add_v6_postprocess(nce);
if (in_ill != ill && nce != NULL) {
nce_t *under_nce = NULL;
/*
* in_ill was the under_ill. Try to create the under_nce.
* Hold the ill_g_lock to prevent changes to group membership
* until we are done.
*/
rw_enter(&ipst->ips_ill_g_lock, RW_READER);
if (!IS_IN_SAME_ILLGRP(in_ill, ill)) {
DTRACE_PROBE2(ill__not__in__group, nce_t *, nce,
ill_t *, ill);
rw_exit(&ipst->ips_ill_g_lock);
err = ENXIO;
nce_refrele(nce);
nce = NULL;
goto bail;
}
under_nce = nce_fastpath_create(in_ill, nce->nce_common);
if (under_nce == NULL) {
rw_exit(&ipst->ips_ill_g_lock);
err = EINVAL;
nce_refrele(nce);
nce = NULL;
goto bail;
}
rw_exit(&ipst->ips_ill_g_lock);
upper_nce = nce;
nce = under_nce; /* will be returned to caller */
if (NCE_ISREACHABLE(nce->nce_common))
nce_fastpath_trigger(under_nce);
}
/* nce_refrele is deferred until the lock is dropped */
if (nce != NULL) {
if (newnce != NULL)
*newnce = nce;
else
nce_refrele(nce);
}
bail:
if (upper_nce != NULL)
nce_refrele(upper_nce);
if (need_ill_refrele)
ill_refrele(ill);
return (err);
}
/*
* Remove all the CONDEMNED nces from the appropriate hash table.
* We create a private list of NCEs, these may have ires pointing
* to them, so the list will be passed through to clean up dependent
* ires and only then we can do ncec_refrele() which can make NCE inactive.
*/
static void
nce_remove(ndp_g_t *ndp, ncec_t *ncec, ncec_t **free_nce_list)
{
ncec_t *ncec1;
ncec_t **ptpn;
ASSERT(MUTEX_HELD(&ndp->ndp_g_lock));
ASSERT(ndp->ndp_g_walker == 0);
for (; ncec; ncec = ncec1) {
ncec1 = ncec->ncec_next;
mutex_enter(&ncec->ncec_lock);
if (NCE_ISCONDEMNED(ncec)) {
ptpn = ncec->ncec_ptpn;
ncec1 = ncec->ncec_next;
if (ncec1 != NULL)
ncec1->ncec_ptpn = ptpn;
*ptpn = ncec1;
ncec->ncec_ptpn = NULL;
ncec->ncec_next = NULL;
ncec->ncec_next = *free_nce_list;
*free_nce_list = ncec;
}
mutex_exit(&ncec->ncec_lock);
}
}
/*
* 1. Mark the entry CONDEMNED. This ensures that no new nce_lookup()
* will return this NCE. Also no new timeouts will
* be started (See nce_restart_timer).
* 2. Cancel any currently running timeouts.
* 3. If there is an ndp walker, return. The walker will do the cleanup.
* This ensures that walkers see a consistent list of NCEs while walking.
* 4. Otherwise remove the NCE from the list of NCEs
*/
void
ncec_delete(ncec_t *ncec)
{
ncec_t **ptpn;
ncec_t *ncec1;
int ipversion = ncec->ncec_ipversion;
ndp_g_t *ndp;
ip_stack_t *ipst = ncec->ncec_ipst;
if (ipversion == IPV4_VERSION)
ndp = ipst->ips_ndp4;
else
ndp = ipst->ips_ndp6;
/* Serialize deletes */
mutex_enter(&ncec->ncec_lock);
if (NCE_ISCONDEMNED(ncec)) {
/* Some other thread is doing the delete */
mutex_exit(&ncec->ncec_lock);
return;
}
/*
* Caller has a refhold. Also 1 ref for being in the list. Thus
* refcnt has to be >= 2
*/
ASSERT(ncec->ncec_refcnt >= 2);
ncec->ncec_flags |= NCE_F_CONDEMNED;
mutex_exit(&ncec->ncec_lock);
/* Count how many condemned ires for kmem_cache callback */
atomic_inc_32(&ipst->ips_num_nce_condemned);
nce_fastpath_list_delete(ncec->ncec_ill, ncec, NULL);
/* Complete any waiting callbacks */
ncec_cb_dispatch(ncec);
/*
* Cancel any running timer. Timeout can't be restarted
* since CONDEMNED is set. Can't hold ncec_lock across untimeout.
* Passing invalid timeout id is fine.
*/
if (ncec->ncec_timeout_id != 0) {
(void) untimeout(ncec->ncec_timeout_id);
ncec->ncec_timeout_id = 0;
}
mutex_enter(&ndp->ndp_g_lock);
if (ncec->ncec_ptpn == NULL) {
/*
* The last ndp walker has already removed this ncec from
* the list after we marked the ncec CONDEMNED and before
* we grabbed the global lock.
*/
mutex_exit(&ndp->ndp_g_lock);
return;
}
if (ndp->ndp_g_walker > 0) {
/*
* Can't unlink. The walker will clean up
*/
ndp->ndp_g_walker_cleanup = B_TRUE;
mutex_exit(&ndp->ndp_g_lock);
return;
}
/*
* Now remove the ncec from the list. nce_restart_timer won't restart
* the timer since it is marked CONDEMNED.
*/
ptpn = ncec->ncec_ptpn;
ncec1 = ncec->ncec_next;
if (ncec1 != NULL)
ncec1->ncec_ptpn = ptpn;
*ptpn = ncec1;
ncec->ncec_ptpn = NULL;
ncec->ncec_next = NULL;
mutex_exit(&ndp->ndp_g_lock);
/* Removed from ncec_ptpn/ncec_next list */
ncec_refrele_notr(ncec);
}
void
ncec_inactive(ncec_t *ncec)
{
mblk_t **mpp;
ill_t *ill = ncec->ncec_ill;
ip_stack_t *ipst = ncec->ncec_ipst;
ASSERT(ncec->ncec_refcnt == 0);
ASSERT(MUTEX_HELD(&ncec->ncec_lock));
/* Count how many condemned nces for kmem_cache callback */
if (NCE_ISCONDEMNED(ncec))
atomic_add_32(&ipst->ips_num_nce_condemned, -1);
/* Free all allocated messages */
mpp = &ncec->ncec_qd_mp;
while (*mpp != NULL) {
mblk_t *mp;
mp = *mpp;
*mpp = mp->b_next;
inet_freemsg(mp);
}
/*
* must have been cleaned up in ncec_delete
*/
ASSERT(list_is_empty(&ncec->ncec_cb));
list_destroy(&ncec->ncec_cb);
/*
* free the ncec_lladdr if one was allocated in nce_add_common()
*/
if (ncec->ncec_lladdr_length > 0)
kmem_free(ncec->ncec_lladdr, ncec->ncec_lladdr_length);
#ifdef DEBUG
ncec_trace_cleanup(ncec);
#endif
mutex_enter(&ill->ill_lock);
DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
(char *), "ncec", (void *), ncec);
ill->ill_ncec_cnt--;
ncec->ncec_ill = NULL;
/*
* If the number of ncec's associated with this ill have dropped
* to zero, check whether we need to restart any operation that
* is waiting for this to happen.
*/
if (ILL_DOWN_OK(ill)) {
/* ipif_ill_refrele_tail drops the ill_lock */
ipif_ill_refrele_tail(ill);
} else {
mutex_exit(&ill->ill_lock);
}
mutex_destroy(&ncec->ncec_lock);
kmem_cache_free(ncec_cache, ncec);
}
/*
* ncec_walk routine. Delete the ncec if it is associated with the ill
* that is going away. Always called as a writer.
*/
void
ncec_delete_per_ill(ncec_t *ncec, void *arg)
{
if ((ncec != NULL) && ncec->ncec_ill == arg) {
ncec_delete(ncec);
}
}
/*
* Neighbor Cache cleanup logic for a list of ncec_t entries.
*/
static void
nce_cleanup_list(ncec_t *ncec)
{
ncec_t *ncec_next;
ASSERT(ncec != NULL);
while (ncec != NULL) {
ncec_next = ncec->ncec_next;
ncec->ncec_next = NULL;
/*
* It is possible for the last ndp walker (this thread)
* to come here after ncec_delete has marked the ncec CONDEMNED
* and before it has removed the ncec from the fastpath list
* or called untimeout. So we need to do it here. It is safe
* for both ncec_delete and this thread to do it twice or
* even simultaneously since each of the threads has a
* reference on the ncec.
*/
nce_fastpath_list_delete(ncec->ncec_ill, ncec, NULL);
/*
* Cancel any running timer. Timeout can't be restarted
* since CONDEMNED is set. The ncec_lock can't be
* held across untimeout though passing invalid timeout
* id is fine.
*/
if (ncec->ncec_timeout_id != 0) {
(void) untimeout(ncec->ncec_timeout_id);
ncec->ncec_timeout_id = 0;
}
/* Removed from ncec_ptpn/ncec_next list */
ncec_refrele_notr(ncec);
ncec = ncec_next;
}
}
/*
* Restart DAD on given NCE. Returns B_TRUE if DAD has been restarted.
*/
boolean_t
nce_restart_dad(ncec_t *ncec)
{
boolean_t started;
ill_t *ill, *hwaddr_ill;
if (ncec == NULL)
return (B_FALSE);
ill = ncec->ncec_ill;
mutex_enter(&ncec->ncec_lock);
if (ncec->ncec_state == ND_PROBE) {
mutex_exit(&ncec->ncec_lock);
started = B_TRUE;
} else if (ncec->ncec_state == ND_REACHABLE) {
ASSERT(ncec->ncec_lladdr != NULL);
ncec->ncec_state = ND_PROBE;
ncec->ncec_pcnt = ND_MAX_UNICAST_SOLICIT;
/*
* Slight cheat here: we don't use the initial probe delay
* for IPv4 in this obscure case.
*/
mutex_exit(&ncec->ncec_lock);
if (IS_IPMP(ill)) {
hwaddr_ill = ipmp_illgrp_find_ill(ill->ill_grp,
ncec->ncec_lladdr, ncec->ncec_lladdr_length);
} else {
hwaddr_ill = ill;
}
nce_dad(ncec, hwaddr_ill, B_TRUE);
started = B_TRUE;
} else {
mutex_exit(&ncec->ncec_lock);
started = B_FALSE;
}
return (started);
}
/*
* IPv6 Cache entry lookup. Try to find an ncec matching the parameters passed.
* If one is found, the refcnt on the ncec will be incremented.
*/
ncec_t *
ncec_lookup_illgrp_v6(ill_t *ill, const in6_addr_t *addr)
{
ncec_t *ncec;
ip_stack_t *ipst = ill->ill_ipst;
rw_enter(&ipst->ips_ill_g_lock, RW_READER);
mutex_enter(&ipst->ips_ndp6->ndp_g_lock);
/* Get head of v6 hash table */
ncec = *((ncec_t **)NCE_HASH_PTR_V6(ipst, *addr));
ncec = ncec_lookup_illgrp(ill, addr, ncec);
mutex_exit(&ipst->ips_ndp6->ndp_g_lock);
rw_exit(&ipst->ips_ill_g_lock);
return (ncec);
}
/*
* IPv4 Cache entry lookup. Try to find an ncec matching the parameters passed.
* If one is found, the refcnt on the ncec will be incremented.
*/
ncec_t *
ncec_lookup_illgrp_v4(ill_t *ill, const in_addr_t *addr)
{
ncec_t *ncec = NULL;
in6_addr_t addr6;
ip_stack_t *ipst = ill->ill_ipst;
rw_enter(&ipst->ips_ill_g_lock, RW_READER);
mutex_enter(&ipst->ips_ndp4->ndp_g_lock);
/* Get head of v4 hash table */
ncec = *((ncec_t **)NCE_HASH_PTR_V4(ipst, *addr));
IN6_IPADDR_TO_V4MAPPED(*addr, &addr6);
ncec = ncec_lookup_illgrp(ill, &addr6, ncec);
mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
rw_exit(&ipst->ips_ill_g_lock);
return (ncec);
}
/*
* Cache entry lookup. Try to find an ncec matching the parameters passed.
* If an ncec is found, increment the hold count on that ncec.
* The caller passes in the start of the appropriate hash table, and must
* be holding the appropriate global lock (ndp_g_lock). In addition, since
* this function matches ncec_t entries across the illgrp, the ips_ill_g_lock
* must be held as reader.
*
* This function always matches across the ipmp group.
*/
ncec_t *
ncec_lookup_illgrp(ill_t *ill, const in6_addr_t *addr, ncec_t *ncec)
{
ndp_g_t *ndp;
ip_stack_t *ipst = ill->ill_ipst;
if (ill->ill_isv6)
ndp = ipst->ips_ndp6;
else
ndp = ipst->ips_ndp4;
ASSERT(ill != NULL);
ASSERT(MUTEX_HELD(&ndp->ndp_g_lock));
if (IN6_IS_ADDR_UNSPECIFIED(addr))
return (NULL);
for (; ncec != NULL; ncec = ncec->ncec_next) {
if (ncec->ncec_ill == ill ||
IS_IN_SAME_ILLGRP(ill, ncec->ncec_ill)) {
if (IN6_ARE_ADDR_EQUAL(&ncec->ncec_addr, addr)) {
mutex_enter(&ncec->ncec_lock);
if (!NCE_ISCONDEMNED(ncec)) {
ncec_refhold_locked(ncec);
mutex_exit(&ncec->ncec_lock);
break;
}
mutex_exit(&ncec->ncec_lock);
}
}
}
return (ncec);
}
/*
* Find an nce_t on ill with nce_addr == addr. Lookup the nce_t
* entries for ill only, i.e., when ill is part of an ipmp group,
* nce_lookup_v4 will never try to match across the group.
*/
nce_t *
nce_lookup_v4(ill_t *ill, const in_addr_t *addr)
{
nce_t *nce;
in6_addr_t addr6;
ip_stack_t *ipst = ill->ill_ipst;
mutex_enter(&ipst->ips_ndp4->ndp_g_lock);
IN6_IPADDR_TO_V4MAPPED(*addr, &addr6);
nce = nce_lookup_addr(ill, &addr6);
mutex_exit(&ipst->ips_ndp4->ndp_g_lock);
return (nce);
}
/*
* Find an nce_t on ill with nce_addr == addr. Lookup the nce_t
* entries for ill only, i.e., when ill is part of an ipmp group,
* nce_lookup_v6 will never try to match across the group.
*/
nce_t *
nce_lookup_v6(ill_t *ill, const in6_addr_t *addr6)
{
nce_t *nce;
ip_stack_t *ipst = ill->ill_ipst;
mutex_enter(&ipst->ips_ndp6->ndp_g_lock);
nce = nce_lookup_addr(ill, addr6);
mutex_exit(&ipst->ips_ndp6->ndp_g_lock);
return (nce);
}
static nce_t *
nce_lookup_addr(ill_t *ill, const in6_addr_t *addr)
{
nce_t *nce;
ASSERT(ill != NULL);
#ifdef DEBUG
if (ill->ill_isv6)
ASSERT(MUTEX_HELD(&ill->ill_ipst->ips_ndp6->ndp_g_lock));
else
ASSERT(MUTEX_HELD(&ill->ill_ipst->ips_ndp4->ndp_g_lock));
#endif
mutex_enter(&ill->ill_lock);
nce = nce_lookup(ill, addr);
mutex_exit(&ill->ill_lock);
return (nce);
}
/*
* Router turned to host. We need to make sure that cached copies of the ncec
* are not used for forwarding packets if they were derived from the default
* route, and that the default route itself is removed, as required by
* section 7.2.5 of RFC 2461.
*
* Note that the ncec itself probably has valid link-layer information for the
* nexthop, so that there is no reason to delete the ncec, as long as the
* ISROUTER flag is turned off.
*/
static void
ncec_router_to_host(ncec_t *ncec)
{
ire_t *ire;
ip_stack_t *ipst = ncec->ncec_ipst;
mutex_enter(&ncec->ncec_lock);
ncec->ncec_flags &= ~NCE_F_ISROUTER;
mutex_exit(&ncec->ncec_lock);
ire = ire_ftable_lookup_v6(&ipv6_all_zeros, &ipv6_all_zeros,
&ncec->ncec_addr, IRE_DEFAULT, ncec->ncec_ill, ALL_ZONES, NULL,
MATCH_IRE_ILL | MATCH_IRE_TYPE | MATCH_IRE_GW, 0, ipst, NULL);
if (ire != NULL) {
ip_rts_rtmsg(RTM_DELETE, ire, 0, ipst);
ire_delete(ire);
ire_refrele(ire);
}
}
/*
* Process passed in parameters either from an incoming packet or via
* user ioctl.
*/
void
nce_process(ncec_t *ncec, uchar_t *hw_addr, uint32_t flag, boolean_t is_adv)
{
ill_t *ill = ncec->ncec_ill;
uint32_t hw_addr_len = ill->ill_phys_addr_length;
boolean_t ll_updated = B_FALSE;
boolean_t ll_changed;
nce_t *nce;
ASSERT(ncec->ncec_ipversion == IPV6_VERSION);
/*
* No updates of link layer address or the neighbor state is
* allowed, when the cache is in NONUD state. This still
* allows for responding to reachability solicitation.
*/
mutex_enter(&ncec->ncec_lock);
if (ncec->ncec_state == ND_INCOMPLETE) {
if (hw_addr == NULL) {
mutex_exit(&ncec->ncec_lock);
return;
}
nce_set_ll(ncec, hw_addr);
/*
* Update ncec state and send the queued packets
* back to ip this time ire will be added.
*/
if (flag & ND_NA_FLAG_SOLICITED) {
nce_update(ncec, ND_REACHABLE, NULL);
} else {
nce_update(ncec, ND_STALE, NULL);
}
mutex_exit(&ncec->ncec_lock);
nce = nce_fastpath(ncec, B_TRUE, NULL);
nce_resolv_ok(ncec);
if (nce != NULL)
nce_refrele(nce);
return;
}
ll_changed = nce_cmp_ll_addr(ncec, hw_addr, hw_addr_len);
if (!is_adv) {
/* If this is a SOLICITATION request only */
if (ll_changed)
nce_update(ncec, ND_STALE, hw_addr);
mutex_exit(&ncec->ncec_lock);
ncec_cb_dispatch(ncec);
return;
}
if (!(flag & ND_NA_FLAG_OVERRIDE) && ll_changed) {
/* If in any other state than REACHABLE, ignore */
if (ncec->ncec_state == ND_REACHABLE) {
nce_update(ncec, ND_STALE, NULL);
}
mutex_exit(&ncec->ncec_lock);
ncec_cb_dispatch(ncec);
return;
} else {
if (ll_changed) {
nce_update(ncec, ND_UNCHANGED, hw_addr);
ll_updated = B_TRUE;
}
if (flag & ND_NA_FLAG_SOLICITED) {
nce_update(ncec, ND_REACHABLE, NULL);
} else {
if (ll_updated) {
nce_update(ncec, ND_STALE, NULL);
}
}
mutex_exit(&ncec->ncec_lock);
if (!(flag & ND_NA_FLAG_ROUTER) && (ncec->ncec_flags &
NCE_F_ISROUTER)) {
ncec_router_to_host(ncec);
} else {
ncec_cb_dispatch(ncec);
}
}
}
/*
* Pass arg1 to the cbf supplied, along with each ncec in existence.
* ncec_walk() places a REFHOLD on the ncec and drops the lock when
* walking the hash list.
*/
void
ncec_walk_common(ndp_g_t *ndp, ill_t *ill, ncec_walk_cb_t cbf,
void *arg1, boolean_t trace)
{
ncec_t *ncec;
ncec_t *ncec1;
ncec_t **ncep;
ncec_t *free_nce_list = NULL;
mutex_enter(&ndp->ndp_g_lock);
/* Prevent ncec_delete from unlink and free of NCE */
ndp->ndp_g_walker++;
mutex_exit(&ndp->ndp_g_lock);
for (ncep = ndp->nce_hash_tbl;
ncep < A_END(ndp->nce_hash_tbl); ncep++) {
for (ncec = *ncep; ncec != NULL; ncec = ncec1) {
ncec1 = ncec->ncec_next;
if (ill == NULL || ncec->ncec_ill == ill) {
if (trace) {
ncec_refhold(ncec);
(*cbf)(ncec, arg1);
ncec_refrele(ncec);
} else {
ncec_refhold_notr(ncec);
(*cbf)(ncec, arg1);
ncec_refrele_notr(ncec);
}
}
}
}
mutex_enter(&ndp->ndp_g_lock);
ndp->ndp_g_walker--;
if (ndp->ndp_g_walker_cleanup && ndp->ndp_g_walker == 0) {
/* Time to delete condemned entries */
for (ncep = ndp->nce_hash_tbl;
ncep < A_END(ndp->nce_hash_tbl); ncep++) {
ncec = *ncep;
if (ncec != NULL) {
nce_remove(ndp, ncec, &free_nce_list);
}
}
ndp->ndp_g_walker_cleanup = B_FALSE;
}
mutex_exit(&ndp->ndp_g_lock);
if (free_nce_list != NULL) {
nce_cleanup_list(free_nce_list);
}
}
/*
* Walk everything.
* Note that ill can be NULL hence can't derive the ipst from it.
*/
void