forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpim6_mld.c
3233 lines (2698 loc) · 84.8 KB
/
pim6_mld.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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* PIMv6 MLD querier
* Copyright (C) 2021-2022 David Lamparter for NetDEF, Inc.
*/
/*
* keep pim6_mld.h open when working on this code. Most data structures are
* commented in the header.
*
* IPv4 support is pre-planned but hasn't been tackled yet. It is intended
* that this code will replace the old IGMP querier at some point.
*/
#include <zebra.h>
#include <netinet/icmp6.h>
#include <netinet/ip6.h>
#include "lib/memory.h"
#include "lib/jhash.h"
#include "lib/prefix.h"
#include "lib/checksum.h"
#include "lib/frrevent.h"
#include "termtable.h"
#include "pimd/pim6_mld.h"
#include "pimd/pim6_mld_protocol.h"
#include "pimd/pim_memory.h"
#include "pimd/pim_instance.h"
#include "pimd/pim_iface.h"
#include "pimd/pim6_cmd.h"
#include "pimd/pim_cmd_common.h"
#include "pimd/pim_util.h"
#include "pimd/pim_tib.h"
#include "pimd/pimd.h"
#ifndef IPV6_MULTICAST_ALL
#define IPV6_MULTICAST_ALL 29
#endif
DEFINE_MTYPE_STATIC(PIMD, GM_IFACE, "MLD interface");
DEFINE_MTYPE_STATIC(PIMD, GM_PACKET, "MLD packet");
DEFINE_MTYPE_STATIC(PIMD, GM_SUBSCRIBER, "MLD subscriber");
DEFINE_MTYPE_STATIC(PIMD, GM_STATE, "MLD subscription state");
DEFINE_MTYPE_STATIC(PIMD, GM_SG, "MLD (S,G)");
DEFINE_MTYPE_STATIC(PIMD, GM_GRP_PENDING, "MLD group query state");
DEFINE_MTYPE_STATIC(PIMD, GM_GSQ_PENDING, "MLD group/source query aggregate");
static void gm_t_query(struct event *t);
static void gm_trigger_specific(struct gm_sg *sg);
static void gm_sg_timer_start(struct gm_if *gm_ifp, struct gm_sg *sg,
struct timeval expire_wait);
/* shorthand for log messages */
#define log_ifp(msg) \
"[MLD %s:%s] " msg, gm_ifp->ifp->vrf->name, gm_ifp->ifp->name
#define log_pkt_src(msg) \
"[MLD %s:%s %pI6] " msg, gm_ifp->ifp->vrf->name, gm_ifp->ifp->name, \
&pkt_src->sin6_addr
#define log_sg(sg, msg) \
"[MLD %s:%s %pSG] " msg, sg->iface->ifp->vrf->name, \
sg->iface->ifp->name, &sg->sgaddr
/* clang-format off */
#if PIM_IPV == 6
static const pim_addr gm_all_hosts = {
.s6_addr = {
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
},
};
static const pim_addr gm_all_routers = {
.s6_addr = {
0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
},
};
/* MLDv1 does not allow subscriber tracking due to report suppression
* hence, the source address is replaced with ffff:...:ffff
*/
static const pim_addr gm_dummy_untracked = {
.s6_addr = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
},
};
#else
/* 224.0.0.1 */
static const pim_addr gm_all_hosts = { .s_addr = htonl(0xe0000001), };
/* 224.0.0.22 */
static const pim_addr gm_all_routers = { .s_addr = htonl(0xe0000016), };
static const pim_addr gm_dummy_untracked = { .s_addr = 0xffffffff, };
#endif
/* clang-format on */
#define IPV6_MULTICAST_SCOPE_LINK 2
static inline uint8_t in6_multicast_scope(const pim_addr *addr)
{
return addr->s6_addr[1] & 0xf;
}
bool in6_multicast_nofwd(const pim_addr *addr)
{
return in6_multicast_scope(addr) <= IPV6_MULTICAST_SCOPE_LINK;
}
/*
* (S,G) -> subscriber,(S,G)
*/
static int gm_packet_sg_cmp(const struct gm_packet_sg *a,
const struct gm_packet_sg *b)
{
const struct gm_packet_state *s_a, *s_b;
s_a = gm_packet_sg2state(a);
s_b = gm_packet_sg2state(b);
return IPV6_ADDR_CMP(&s_a->subscriber->addr, &s_b->subscriber->addr);
}
DECLARE_RBTREE_UNIQ(gm_packet_sg_subs, struct gm_packet_sg, subs_itm,
gm_packet_sg_cmp);
static struct gm_packet_sg *gm_packet_sg_find(struct gm_sg *sg,
enum gm_sub_sense sense,
struct gm_subscriber *sub)
{
struct {
struct gm_packet_state hdr;
struct gm_packet_sg item;
} ref = {
/* clang-format off */
.hdr = {
.subscriber = sub,
},
.item = {
.offset = 0,
},
/* clang-format on */
};
return gm_packet_sg_subs_find(&sg->subs[sense], &ref.item);
}
/*
* interface -> (*,G),pending
*/
static int gm_grp_pending_cmp(const struct gm_grp_pending *a,
const struct gm_grp_pending *b)
{
return IPV6_ADDR_CMP(&a->grp, &b->grp);
}
DECLARE_RBTREE_UNIQ(gm_grp_pends, struct gm_grp_pending, itm,
gm_grp_pending_cmp);
/*
* interface -> ([S1,S2,...],G),pending
*/
static int gm_gsq_pending_cmp(const struct gm_gsq_pending *a,
const struct gm_gsq_pending *b)
{
if (a->s_bit != b->s_bit)
return numcmp(a->s_bit, b->s_bit);
return IPV6_ADDR_CMP(&a->grp, &b->grp);
}
static uint32_t gm_gsq_pending_hash(const struct gm_gsq_pending *a)
{
uint32_t seed = a->s_bit ? 0x68f0eb5e : 0x156b7f19;
return jhash(&a->grp, sizeof(a->grp), seed);
}
DECLARE_HASH(gm_gsq_pends, struct gm_gsq_pending, itm, gm_gsq_pending_cmp,
gm_gsq_pending_hash);
/*
* interface -> (S,G)
*/
int gm_sg_cmp(const struct gm_sg *a, const struct gm_sg *b)
{
return pim_sgaddr_cmp(a->sgaddr, b->sgaddr);
}
static struct gm_sg *gm_sg_find(struct gm_if *gm_ifp, pim_addr grp,
pim_addr src)
{
struct gm_sg ref = {};
ref.sgaddr.grp = grp;
ref.sgaddr.src = src;
return gm_sgs_find(gm_ifp->sgs, &ref);
}
static struct gm_sg *gm_sg_make(struct gm_if *gm_ifp, pim_addr grp,
pim_addr src)
{
struct gm_sg *ret, *prev;
ret = XCALLOC(MTYPE_GM_SG, sizeof(*ret));
ret->sgaddr.grp = grp;
ret->sgaddr.src = src;
ret->iface = gm_ifp;
prev = gm_sgs_add(gm_ifp->sgs, ret);
if (prev) {
XFREE(MTYPE_GM_SG, ret);
ret = prev;
} else {
monotime(&ret->created);
gm_packet_sg_subs_init(ret->subs_positive);
gm_packet_sg_subs_init(ret->subs_negative);
}
return ret;
}
/*
* interface -> packets, sorted by expiry (because add_tail insert order)
*/
DECLARE_DLIST(gm_packet_expires, struct gm_packet_state, exp_itm);
/*
* subscriber -> packets
*/
DECLARE_DLIST(gm_packets, struct gm_packet_state, pkt_itm);
/*
* interface -> subscriber
*/
static int gm_subscriber_cmp(const struct gm_subscriber *a,
const struct gm_subscriber *b)
{
return IPV6_ADDR_CMP(&a->addr, &b->addr);
}
static uint32_t gm_subscriber_hash(const struct gm_subscriber *a)
{
return jhash(&a->addr, sizeof(a->addr), 0xd0e94ad4);
}
DECLARE_HASH(gm_subscribers, struct gm_subscriber, itm, gm_subscriber_cmp,
gm_subscriber_hash);
static struct gm_subscriber *gm_subscriber_findref(struct gm_if *gm_ifp,
pim_addr addr)
{
struct gm_subscriber ref = {}, *ret;
ref.addr = addr;
ret = gm_subscribers_find(gm_ifp->subscribers, &ref);
if (ret)
ret->refcount++;
return ret;
}
static struct gm_subscriber *gm_subscriber_get(struct gm_if *gm_ifp,
pim_addr addr)
{
struct gm_subscriber ref = {}, *ret;
ref.addr = addr;
ret = gm_subscribers_find(gm_ifp->subscribers, &ref);
if (!ret) {
ret = XCALLOC(MTYPE_GM_SUBSCRIBER, sizeof(*ret));
ret->iface = gm_ifp;
ret->addr = addr;
ret->refcount = 1;
monotime(&ret->created);
gm_packets_init(ret->packets);
gm_subscribers_add(gm_ifp->subscribers, ret);
}
return ret;
}
static void gm_subscriber_drop(struct gm_subscriber **subp)
{
struct gm_subscriber *sub = *subp;
struct gm_if *gm_ifp;
if (!sub)
return;
gm_ifp = sub->iface;
*subp = NULL;
sub->refcount--;
if (sub->refcount)
return;
gm_subscribers_del(gm_ifp->subscribers, sub);
XFREE(MTYPE_GM_SUBSCRIBER, sub);
}
/****************************************************************************/
/* bundle query timer values for combined v1/v2 handling */
struct gm_query_timers {
unsigned int qrv;
unsigned int max_resp_ms;
unsigned int qqic_ms;
struct timeval fuzz;
struct timeval expire_wait;
};
static void gm_expiry_calc(struct gm_query_timers *timers)
{
unsigned int expire =
(timers->qrv - 1) * timers->qqic_ms + timers->max_resp_ms;
ldiv_t exp_div = ldiv(expire, 1000);
timers->expire_wait.tv_sec = exp_div.quot;
timers->expire_wait.tv_usec = exp_div.rem * 1000;
timeradd(&timers->expire_wait, &timers->fuzz, &timers->expire_wait);
}
static void gm_sg_free(struct gm_sg *sg)
{
/* t_sg_expiry is handled before this is reached */
EVENT_OFF(sg->t_sg_query);
gm_packet_sg_subs_fini(sg->subs_negative);
gm_packet_sg_subs_fini(sg->subs_positive);
XFREE(MTYPE_GM_SG, sg);
}
/* clang-format off */
static const char *const gm_states[] = {
[GM_SG_NOINFO] = "NOINFO",
[GM_SG_JOIN] = "JOIN",
[GM_SG_JOIN_EXPIRING] = "JOIN_EXPIRING",
[GM_SG_PRUNE] = "PRUNE",
[GM_SG_NOPRUNE] = "NOPRUNE",
[GM_SG_NOPRUNE_EXPIRING] = "NOPRUNE_EXPIRING",
};
/* clang-format on */
/* TODO: S,G entries in EXCLUDE (i.e. prune) unsupported" */
/* tib_sg_gm_prune() below is an "un-join", it doesn't prune S,G when *,G is
* joined. Whether we actually want/need to support this is a separate
* question - it is almost never used. In fact this is exactly what RFC5790
* ("lightweight" MLDv2) does: it removes S,G EXCLUDE support.
*/
static void gm_sg_update(struct gm_sg *sg, bool has_expired)
{
struct gm_if *gm_ifp = sg->iface;
struct pim_interface *pim_ifp = gm_ifp->ifp->info;
enum gm_sg_state prev, desired;
bool new_join;
struct gm_sg *grp = NULL;
if (!pim_addr_is_any(sg->sgaddr.src))
grp = gm_sg_find(gm_ifp, sg->sgaddr.grp, PIMADDR_ANY);
else
assert(sg->state != GM_SG_PRUNE);
if (gm_packet_sg_subs_count(sg->subs_positive)) {
desired = GM_SG_JOIN;
assert(!sg->t_sg_expire);
} else if ((sg->state == GM_SG_JOIN ||
sg->state == GM_SG_JOIN_EXPIRING) &&
!has_expired)
desired = GM_SG_JOIN_EXPIRING;
else if (!grp || !gm_packet_sg_subs_count(grp->subs_positive))
desired = GM_SG_NOINFO;
else if (gm_packet_sg_subs_count(grp->subs_positive) ==
gm_packet_sg_subs_count(sg->subs_negative)) {
if ((sg->state == GM_SG_NOPRUNE ||
sg->state == GM_SG_NOPRUNE_EXPIRING) &&
!has_expired)
desired = GM_SG_NOPRUNE_EXPIRING;
else
desired = GM_SG_PRUNE;
} else if (gm_packet_sg_subs_count(sg->subs_negative))
desired = GM_SG_NOPRUNE;
else
desired = GM_SG_NOINFO;
if (desired != sg->state && !gm_ifp->stopping) {
if (PIM_DEBUG_GM_EVENTS)
zlog_debug(log_sg(sg, "%s => %s"), gm_states[sg->state],
gm_states[desired]);
if (desired == GM_SG_JOIN_EXPIRING ||
desired == GM_SG_NOPRUNE_EXPIRING) {
struct gm_query_timers timers;
timers.qrv = gm_ifp->cur_qrv;
timers.max_resp_ms = gm_ifp->cur_max_resp;
timers.qqic_ms = gm_ifp->cur_query_intv_trig;
timers.fuzz = gm_ifp->cfg_timing_fuzz;
gm_expiry_calc(&timers);
gm_sg_timer_start(gm_ifp, sg, timers.expire_wait);
EVENT_OFF(sg->t_sg_query);
sg->query_sbit = false;
/* Trigger the specific queries only for querier. */
if (IPV6_ADDR_SAME(&gm_ifp->querier, &pim_ifp->ll_lowest)) {
sg->n_query = gm_ifp->cur_lmqc;
gm_trigger_specific(sg);
}
}
}
prev = sg->state;
sg->state = desired;
if (in6_multicast_nofwd(&sg->sgaddr.grp) || gm_ifp->stopping)
new_join = false;
else
new_join = gm_sg_state_want_join(desired);
if (new_join && !sg->tib_joined) {
/* this will retry if join previously failed */
sg->tib_joined = tib_sg_gm_join(gm_ifp->pim, sg->sgaddr,
gm_ifp->ifp, &sg->oil);
if (!sg->tib_joined)
zlog_warn(
"MLD join for %pSG%%%s not propagated into TIB",
&sg->sgaddr, gm_ifp->ifp->name);
else
zlog_info(log_ifp("%pSG%%%s TIB joined"), &sg->sgaddr,
gm_ifp->ifp->name);
} else if (sg->tib_joined && !new_join) {
tib_sg_gm_prune(gm_ifp->pim, sg->sgaddr, gm_ifp->ifp, &sg->oil);
sg->oil = NULL;
sg->tib_joined = false;
}
if (desired == GM_SG_NOINFO) {
/* multiple paths can lead to the last state going away;
* t_sg_expire can still be running if we're arriving from
* another path.
*/
if (has_expired)
EVENT_OFF(sg->t_sg_expire);
assertf((!sg->t_sg_expire &&
!gm_packet_sg_subs_count(sg->subs_positive) &&
!gm_packet_sg_subs_count(sg->subs_negative)),
"%pSG%%%s hx=%u exp=%pTHD state=%s->%s pos=%zu neg=%zu grp=%p",
&sg->sgaddr, gm_ifp->ifp->name, has_expired,
sg->t_sg_expire, gm_states[prev], gm_states[desired],
gm_packet_sg_subs_count(sg->subs_positive),
gm_packet_sg_subs_count(sg->subs_negative), grp);
if (PIM_DEBUG_GM_TRACE)
zlog_debug(log_sg(sg, "dropping"));
gm_sgs_del(gm_ifp->sgs, sg);
gm_sg_free(sg);
}
}
/****************************************************************************/
/* the following bunch of functions deals with transferring state from
* received packets into gm_packet_state. As a reminder, the querier is
* structured to keep all items received in one packet together, since they
* will share expiry timers and thus allows efficient handling.
*/
static void gm_packet_free(struct gm_packet_state *pkt)
{
gm_packet_expires_del(pkt->iface->expires, pkt);
gm_packets_del(pkt->subscriber->packets, pkt);
gm_subscriber_drop(&pkt->subscriber);
XFREE(MTYPE_GM_STATE, pkt);
}
static struct gm_packet_sg *gm_packet_sg_setup(struct gm_packet_state *pkt,
struct gm_sg *sg, bool is_excl,
bool is_src)
{
struct gm_packet_sg *item;
assert(pkt->n_active < pkt->n_sg);
item = &pkt->items[pkt->n_active];
item->sg = sg;
item->is_excl = is_excl;
item->is_src = is_src;
item->offset = pkt->n_active;
pkt->n_active++;
return item;
}
static bool gm_packet_sg_drop(struct gm_packet_sg *item)
{
struct gm_packet_state *pkt;
size_t i;
assert(item->sg);
pkt = gm_packet_sg2state(item);
if (item->sg->most_recent == item)
item->sg->most_recent = NULL;
for (i = 0; i < item->n_exclude; i++) {
struct gm_packet_sg *excl_item;
excl_item = item + 1 + i;
if (!excl_item->sg)
continue;
gm_packet_sg_subs_del(excl_item->sg->subs_negative, excl_item);
excl_item->sg = NULL;
pkt->n_active--;
assert(pkt->n_active > 0);
}
if (item->is_excl && item->is_src)
gm_packet_sg_subs_del(item->sg->subs_negative, item);
else
gm_packet_sg_subs_del(item->sg->subs_positive, item);
item->sg = NULL;
pkt->n_active--;
if (!pkt->n_active) {
gm_packet_free(pkt);
return true;
}
return false;
}
static void gm_packet_drop(struct gm_packet_state *pkt, bool trace)
{
for (size_t i = 0; i < pkt->n_sg; i++) {
struct gm_sg *sg = pkt->items[i].sg;
bool deleted;
if (!sg)
continue;
if (trace && PIM_DEBUG_GM_TRACE)
zlog_debug(log_sg(sg, "general-dropping from %pPA"),
&pkt->subscriber->addr);
deleted = gm_packet_sg_drop(&pkt->items[i]);
gm_sg_update(sg, true);
if (deleted)
break;
}
}
static void gm_packet_sg_remove_sources(struct gm_if *gm_ifp,
struct gm_subscriber *subscriber,
pim_addr grp, pim_addr *srcs,
size_t n_src, enum gm_sub_sense sense)
{
struct gm_sg *sg;
struct gm_packet_sg *old_src;
size_t i;
for (i = 0; i < n_src; i++) {
sg = gm_sg_find(gm_ifp, grp, srcs[i]);
if (!sg)
continue;
old_src = gm_packet_sg_find(sg, sense, subscriber);
if (!old_src)
continue;
gm_packet_sg_drop(old_src);
gm_sg_update(sg, false);
}
}
static void gm_sg_expiry_cancel(struct gm_sg *sg)
{
if (sg->t_sg_expire && PIM_DEBUG_GM_TRACE)
zlog_debug(log_sg(sg, "alive, cancelling expiry timer"));
EVENT_OFF(sg->t_sg_expire);
sg->query_sbit = true;
}
/* first pass: process all changes resulting in removal of state:
* - {TO,IS}_INCLUDE removes *,G EXCLUDE state (and S,G)
* - ALLOW_NEW_SOURCES, if *,G in EXCLUDE removes S,G state
* - BLOCK_OLD_SOURCES, if *,G in INCLUDE removes S,G state
* - {TO,IS}_EXCLUDE, if *,G in INCLUDE removes S,G state
* note *replacing* state is NOT considered *removing* state here
*
* everything else is thrown into pkt for creation of state in pass 2
*/
static void gm_handle_v2_pass1(struct gm_packet_state *pkt,
struct mld_v2_rec_hdr *rechdr, size_t n_src)
{
/* NB: pkt->subscriber can be NULL here if the subscriber was not
* previously seen!
*/
struct gm_subscriber *subscriber = pkt->subscriber;
struct gm_sg *grp;
struct gm_packet_sg *old_grp = NULL;
struct gm_packet_sg *item;
size_t j;
bool is_excl = false;
grp = gm_sg_find(pkt->iface, rechdr->grp, PIMADDR_ANY);
if (grp && subscriber)
old_grp = gm_packet_sg_find(grp, GM_SUB_POS, subscriber);
assert(old_grp == NULL || old_grp->is_excl);
switch (rechdr->type) {
case MLD_RECTYPE_IS_EXCLUDE:
case MLD_RECTYPE_CHANGE_TO_EXCLUDE:
/* this always replaces or creates state */
is_excl = true;
if (!grp)
grp = gm_sg_make(pkt->iface, rechdr->grp, PIMADDR_ANY);
item = gm_packet_sg_setup(pkt, grp, is_excl, false);
item->n_exclude = n_src;
/* [EXCL_INCL_SG_NOTE] referenced below
*
* in theory, we should drop any S,G that the host may have
* previously added in INCLUDE mode. In practice, this is both
* incredibly rare and entirely irrelevant. It only makes any
* difference if an S,G that the host previously had on the
* INCLUDE list is now on the blocked list for EXCLUDE, which
* we can cover in processing the S,G list in pass2_excl().
*
* Other S,G from the host are simply left to expire
* "naturally" through general expiry.
*/
break;
case MLD_RECTYPE_IS_INCLUDE:
case MLD_RECTYPE_CHANGE_TO_INCLUDE:
if (old_grp) {
/* INCLUDE has no *,G state, so old_grp here refers to
* previous EXCLUDE => delete it
*/
gm_packet_sg_drop(old_grp);
gm_sg_update(grp, false);
/* TODO "need S,G PRUNE => NO_INFO transition here" */
}
break;
case MLD_RECTYPE_ALLOW_NEW_SOURCES:
if (old_grp) {
/* remove S,Gs from EXCLUDE, and then we're done */
gm_packet_sg_remove_sources(pkt->iface, subscriber,
rechdr->grp, rechdr->srcs,
n_src, GM_SUB_NEG);
return;
}
/* in INCLUDE mode => ALLOW_NEW_SOURCES is functionally
* idential to IS_INCLUDE (because the list of sources in
* IS_INCLUDE is not exhaustive)
*/
break;
case MLD_RECTYPE_BLOCK_OLD_SOURCES:
if (old_grp) {
/* this is intentionally not implemented because it
* would be complicated as hell. we only take the list
* of blocked sources from full group state records
*/
return;
}
if (subscriber)
gm_packet_sg_remove_sources(pkt->iface, subscriber,
rechdr->grp, rechdr->srcs,
n_src, GM_SUB_POS);
return;
}
for (j = 0; j < n_src; j++) {
struct gm_sg *sg;
sg = gm_sg_find(pkt->iface, rechdr->grp, rechdr->srcs[j]);
if (!sg)
sg = gm_sg_make(pkt->iface, rechdr->grp,
rechdr->srcs[j]);
gm_packet_sg_setup(pkt, sg, is_excl, true);
}
}
/* second pass: creating/updating/refreshing state. All the items from the
* received packet have already been thrown into gm_packet_state.
*/
static void gm_handle_v2_pass2_incl(struct gm_packet_state *pkt, size_t i)
{
struct gm_packet_sg *item = &pkt->items[i];
struct gm_packet_sg *old = NULL;
struct gm_sg *sg = item->sg;
/* EXCLUDE state was already dropped in pass1 */
assert(!gm_packet_sg_find(sg, GM_SUB_NEG, pkt->subscriber));
old = gm_packet_sg_find(sg, GM_SUB_POS, pkt->subscriber);
if (old)
gm_packet_sg_drop(old);
pkt->n_active++;
gm_packet_sg_subs_add(sg->subs_positive, item);
sg->most_recent = item;
gm_sg_expiry_cancel(sg);
gm_sg_update(sg, false);
}
static void gm_handle_v2_pass2_excl(struct gm_packet_state *pkt, size_t offs)
{
struct gm_packet_sg *item = &pkt->items[offs];
struct gm_packet_sg *old_grp, *item_dup;
struct gm_sg *sg_grp = item->sg;
size_t i;
old_grp = gm_packet_sg_find(sg_grp, GM_SUB_POS, pkt->subscriber);
if (old_grp) {
for (i = 0; i < item->n_exclude; i++) {
struct gm_packet_sg *item_src, *old_src;
item_src = &pkt->items[offs + 1 + i];
old_src = gm_packet_sg_find(item_src->sg, GM_SUB_NEG,
pkt->subscriber);
if (old_src)
gm_packet_sg_drop(old_src);
/* See [EXCL_INCL_SG_NOTE] above - we can have old S,G
* items left over if the host previously had INCLUDE
* mode going. Remove them here if we find any.
*/
old_src = gm_packet_sg_find(item_src->sg, GM_SUB_POS,
pkt->subscriber);
if (old_src)
gm_packet_sg_drop(old_src);
}
/* the previous loop has removed the S,G entries which are
* still excluded after this update. So anything left on the
* old item was previously excluded but is now included
* => need to trigger update on S,G
*/
for (i = 0; i < old_grp->n_exclude; i++) {
struct gm_packet_sg *old_src;
struct gm_sg *old_sg_src;
old_src = old_grp + 1 + i;
old_sg_src = old_src->sg;
if (!old_sg_src)
continue;
gm_packet_sg_drop(old_src);
gm_sg_update(old_sg_src, false);
}
gm_packet_sg_drop(old_grp);
}
item_dup = gm_packet_sg_subs_add(sg_grp->subs_positive, item);
assert(!item_dup);
pkt->n_active++;
sg_grp->most_recent = item;
gm_sg_expiry_cancel(sg_grp);
for (i = 0; i < item->n_exclude; i++) {
struct gm_packet_sg *item_src;
item_src = &pkt->items[offs + 1 + i];
item_dup = gm_packet_sg_subs_add(item_src->sg->subs_negative,
item_src);
if (item_dup)
item_src->sg = NULL;
else {
pkt->n_active++;
gm_sg_update(item_src->sg, false);
}
}
/* TODO: determine best ordering between gm_sg_update(S,G) and (*,G)
* to get lower PIM churn/flapping
*/
gm_sg_update(sg_grp, false);
}
/* TODO: QRV/QQIC are not copied from queries to local state" */
/* on receiving a query, we need to update our robustness/query interval to
* match, so we correctly process group/source specific queries after last
* member leaves
*/
static void gm_handle_v2_report(struct gm_if *gm_ifp,
const struct sockaddr_in6 *pkt_src, char *data,
size_t len)
{
struct mld_v2_report_hdr *hdr;
size_t i, n_records, max_entries;
struct gm_packet_state *pkt;
if (len < sizeof(*hdr)) {
if (PIM_DEBUG_GM_PACKETS)
zlog_debug(log_pkt_src(
"malformed MLDv2 report (truncated header)"));
gm_ifp->stats.rx_drop_malformed++;
return;
}
hdr = (struct mld_v2_report_hdr *)data;
data += sizeof(*hdr);
len -= sizeof(*hdr);
n_records = ntohs(hdr->n_records);
if (n_records > len / sizeof(struct mld_v2_rec_hdr)) {
/* note this is only an upper bound, records with source lists
* are larger. This is mostly here to make coverity happy.
*/
zlog_warn(log_pkt_src(
"malformed MLDv2 report (infeasible record count)"));
gm_ifp->stats.rx_drop_malformed++;
return;
}
/* errors after this may at least partially process the packet */
gm_ifp->stats.rx_new_report++;
/* can't have more *,G and S,G items than there is space for ipv6
* addresses, so just use this to allocate temporary buffer
*/
max_entries = len / sizeof(pim_addr);
pkt = XCALLOC(MTYPE_GM_STATE,
offsetof(struct gm_packet_state, items[max_entries]));
pkt->n_sg = max_entries;
pkt->iface = gm_ifp;
pkt->subscriber = gm_subscriber_findref(gm_ifp, pkt_src->sin6_addr);
/* validate & remove state in v2_pass1() */
for (i = 0; i < n_records; i++) {
struct mld_v2_rec_hdr *rechdr;
size_t n_src, record_size;
if (len < sizeof(*rechdr)) {
zlog_warn(log_pkt_src(
"malformed MLDv2 report (truncated record header)"));
gm_ifp->stats.rx_trunc_report++;
break;
}
rechdr = (struct mld_v2_rec_hdr *)data;
data += sizeof(*rechdr);
len -= sizeof(*rechdr);
n_src = ntohs(rechdr->n_src);
record_size = n_src * sizeof(pim_addr) + rechdr->aux_len * 4;
if (len < record_size) {
zlog_warn(log_pkt_src(
"malformed MLDv2 report (truncated source list)"));
gm_ifp->stats.rx_trunc_report++;
break;
}
if (!IN6_IS_ADDR_MULTICAST(&rechdr->grp)) {
zlog_warn(
log_pkt_src(
"malformed MLDv2 report (invalid group %pI6)"),
&rechdr->grp);
gm_ifp->stats.rx_trunc_report++;
break;
}
data += record_size;
len -= record_size;
gm_handle_v2_pass1(pkt, rechdr, n_src);
}
if (!pkt->n_active) {
gm_subscriber_drop(&pkt->subscriber);
XFREE(MTYPE_GM_STATE, pkt);
return;
}
pkt = XREALLOC(MTYPE_GM_STATE, pkt,
offsetof(struct gm_packet_state, items[pkt->n_active]));
pkt->n_sg = pkt->n_active;
pkt->n_active = 0;
monotime(&pkt->received);
if (!pkt->subscriber)
pkt->subscriber = gm_subscriber_get(gm_ifp, pkt_src->sin6_addr);
gm_packets_add_tail(pkt->subscriber->packets, pkt);
gm_packet_expires_add_tail(gm_ifp->expires, pkt);
for (i = 0; i < pkt->n_sg; i++)
if (!pkt->items[i].is_excl)
gm_handle_v2_pass2_incl(pkt, i);
else {
gm_handle_v2_pass2_excl(pkt, i);
i += pkt->items[i].n_exclude;
}
if (pkt->n_active == 0)
gm_packet_free(pkt);
}
static void gm_handle_v1_report(struct gm_if *gm_ifp,
const struct sockaddr_in6 *pkt_src, char *data,
size_t len)
{
struct mld_v1_pkt *hdr;
struct gm_packet_state *pkt;
struct gm_sg *grp;
struct gm_packet_sg *item;
size_t max_entries;
if (len < sizeof(*hdr)) {
if (PIM_DEBUG_GM_PACKETS)
zlog_debug(log_pkt_src(
"malformed MLDv1 report (truncated)"));
gm_ifp->stats.rx_drop_malformed++;
return;
}
gm_ifp->stats.rx_old_report++;
hdr = (struct mld_v1_pkt *)data;
max_entries = 1;
pkt = XCALLOC(MTYPE_GM_STATE,
offsetof(struct gm_packet_state, items[max_entries]));
pkt->n_sg = max_entries;
pkt->iface = gm_ifp;
pkt->subscriber = gm_subscriber_findref(gm_ifp, gm_dummy_untracked);
/* { equivalent of gm_handle_v2_pass1() with IS_EXCLUDE */
grp = gm_sg_find(pkt->iface, hdr->grp, PIMADDR_ANY);
if (!grp)
grp = gm_sg_make(pkt->iface, hdr->grp, PIMADDR_ANY);
item = gm_packet_sg_setup(pkt, grp, true, false);
item->n_exclude = 0;
/* TODO "set v1-seen timer on grp here" */
/* } */
/* pass2 will count n_active back up to 1. Also since a v1 report
* has exactly 1 group, we can skip the realloc() that v2 needs here.
*/
assert(pkt->n_active == 1);
pkt->n_sg = pkt->n_active;
pkt->n_active = 0;
monotime(&pkt->received);
if (!pkt->subscriber)
pkt->subscriber = gm_subscriber_get(gm_ifp, gm_dummy_untracked);
gm_packets_add_tail(pkt->subscriber->packets, pkt);
gm_packet_expires_add_tail(gm_ifp->expires, pkt);
/* pass2 covers installing state & removing old state; all the v1
* compat is handled at this point.
*
* Note that "old state" may be v2; subscribers will switch from v2
* reports to v1 reports when the querier changes from v2 to v1. So,
* limiting this to v1 would be wrong.
*/
gm_handle_v2_pass2_excl(pkt, 0);
if (pkt->n_active == 0)
gm_packet_free(pkt);
}
static void gm_handle_v1_leave(struct gm_if *gm_ifp,
const struct sockaddr_in6 *pkt_src, char *data,
size_t len)
{
struct mld_v1_pkt *hdr;
struct gm_subscriber *subscriber;
struct gm_sg *grp;
struct gm_packet_sg *old_grp;
if (len < sizeof(*hdr)) {
if (PIM_DEBUG_GM_PACKETS)