forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpim_igmp.c
1537 lines (1265 loc) · 40.4 KB
/
pim_igmp.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
/*
* PIM for Quagga
* Copyright (C) 2008 Everton da Silva Marques
*/
#include <zebra.h>
#include "memory.h"
#include "prefix.h"
#include "if.h"
#include "hash.h"
#include "jhash.h"
#include "lib_errors.h"
#include "pimd.h"
#include "pim_instance.h"
#include "pim_igmp.h"
#include "pim_igmpv2.h"
#include "pim_igmpv3.h"
#include "pim_igmp_mtrace.h"
#include "pim_iface.h"
#include "pim_sock.h"
#include "pim_mroute.h"
#include "pim_str.h"
#include "pim_util.h"
#include "pim_time.h"
#include "pim_ssm.h"
#include "pim_tib.h"
static void group_timer_off(struct gm_group *group);
static void pim_igmp_general_query(struct event *t);
void igmp_anysource_forward_start(struct pim_instance *pim,
struct gm_group *group)
{
struct gm_source *source;
struct in_addr src_addr = {.s_addr = 0};
/* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
assert(group->group_filtermode_isexcl);
assert(listcount(group->group_source_list) < 1);
source = igmp_get_source_by_addr(group, src_addr, NULL);
if (!source) {
zlog_warn("%s: Failure to create * source", __func__);
return;
}
igmp_source_forward_start(pim, source);
}
void igmp_anysource_forward_stop(struct gm_group *group)
{
struct gm_source *source;
struct in_addr star = {.s_addr = 0};
source = igmp_find_source_by_addr(group, star);
if (source)
igmp_source_forward_stop(source);
}
static void igmp_source_forward_reevaluate_one(struct pim_instance *pim,
struct gm_source *source,
int is_grp_ssm)
{
pim_sgaddr sg;
struct gm_group *group = source->source_group;
memset(&sg, 0, sizeof(sg));
sg.src = source->source_addr;
sg.grp = group->group_addr;
/** if there is no PIM state **/
if (IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
if (pim_addr_is_any(source->source_addr)) {
if (is_grp_ssm) {
if (PIM_DEBUG_PIM_EVENTS)
zlog_debug(
"local membership del for %pSG as G is now SSM",
&sg);
igmp_source_forward_stop(source);
}
} else {
if (!is_grp_ssm) {
if (PIM_DEBUG_PIM_EVENTS)
zlog_debug(
"local membership del for %pSG as G is now ASM",
&sg);
igmp_source_forward_stop(source);
}
}
} else {
if (!pim_addr_is_any(source->source_addr) && (is_grp_ssm)) {
if (PIM_DEBUG_PIM_EVENTS)
zlog_debug(
"local membership add for %pSG as G is now SSM",
&sg);
igmp_source_forward_start(pim, source);
}
}
}
void igmp_source_forward_reevaluate_all(struct pim_instance *pim)
{
struct interface *ifp;
FOR_ALL_INTERFACES (pim->vrf, ifp) {
struct pim_interface *pim_ifp = ifp->info;
struct listnode *grpnode, *grp_nextnode;
struct gm_group *grp;
struct pim_ifchannel *ch, *ch_temp;
if (!pim_ifp)
continue;
/* scan igmp groups */
for (ALL_LIST_ELEMENTS(pim_ifp->gm_group_list, grpnode,
grp_nextnode, grp)) {
struct listnode *srcnode;
struct gm_source *src;
int is_grp_ssm;
/*
* RFC 4604
* section 2.2.1
* EXCLUDE mode does not apply to SSM addresses,
* and an SSM-aware router will ignore
* MODE_IS_EXCLUDE and CHANGE_TO_EXCLUDE_MODE
* requests in the SSM range.
*/
is_grp_ssm = pim_is_grp_ssm(pim, grp->group_addr);
if (is_grp_ssm && grp->group_filtermode_isexcl) {
igmp_group_delete(grp);
} else {
/* scan group sources */
for (ALL_LIST_ELEMENTS_RO(
grp->group_source_list, srcnode,
src)) {
igmp_source_forward_reevaluate_one(
pim, src, is_grp_ssm);
} /* scan group sources */
}
} /* scan igmp groups */
RB_FOREACH_SAFE (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb,
ch_temp) {
if (pim_is_grp_ssm(pim, ch->sg.grp)) {
if (pim_addr_is_any(ch->sg.src))
pim_ifchannel_delete(ch);
}
}
} /* scan interfaces */
}
void igmp_source_forward_start(struct pim_instance *pim,
struct gm_source *source)
{
struct gm_group *group;
pim_sgaddr sg;
memset(&sg, 0, sizeof(sg));
sg.src = source->source_addr;
sg.grp = source->source_group->group_addr;
if (PIM_DEBUG_GM_TRACE) {
zlog_debug("%s: (S,G)=%pSG oif=%s fwd=%d", __func__, &sg,
source->source_group->interface->name,
IGMP_SOURCE_TEST_FORWARDING(source->source_flags));
}
/*
* PIM state should not be allowed for ASM group with valid source
* address.
*/
if ((!pim_is_grp_ssm(pim, source->source_group->group_addr)) &&
!pim_addr_is_any(source->source_addr)) {
zlog_warn(
"%s: (S,G)=%pSG ASM range having source address, not allowed to create PIM state",
__func__, &sg);
return;
}
/* Prevent IGMP interface from installing multicast route multiple
times */
if (IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
return;
}
group = source->source_group;
if (tib_sg_gm_join(pim, sg, group->interface,
&source->source_channel_oil))
IGMP_SOURCE_DO_FORWARDING(source->source_flags);
}
/*
igmp_source_forward_stop: stop forwarding, but keep the source
igmp_source_delete: stop forwarding, and delete the source
*/
void igmp_source_forward_stop(struct gm_source *source)
{
struct pim_interface *pim_oif;
struct gm_group *group;
pim_sgaddr sg;
memset(&sg, 0, sizeof(sg));
sg.src = source->source_addr;
sg.grp = source->source_group->group_addr;
if (PIM_DEBUG_GM_TRACE) {
zlog_debug("%s: (S,G)=%pSG oif=%s fwd=%d", __func__, &sg,
source->source_group->interface->name,
IGMP_SOURCE_TEST_FORWARDING(source->source_flags));
}
/* Prevent IGMP interface from removing multicast route multiple
times */
if (!IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
return;
}
group = source->source_group;
pim_oif = group->interface->info;
tib_sg_gm_prune(pim_oif->pim, sg, group->interface,
&source->source_channel_oil);
IGMP_SOURCE_DONT_FORWARDING(source->source_flags);
}
/* This socket is used for TXing IGMP packets only, IGMP RX happens
* in pim_mroute_msg()
*/
static int igmp_sock_open(struct in_addr ifaddr, struct interface *ifp)
{
int fd;
int join = 0;
struct in_addr group;
struct pim_interface *pim_ifp = ifp->info;
fd = pim_socket_mcast(IPPROTO_IGMP, ifaddr, ifp, 1);
if (fd < 0)
return -1;
if (inet_aton(PIM_ALL_ROUTERS, &group)) {
if (!pim_socket_join(fd, group, ifaddr, ifp->ifindex, pim_ifp))
++join;
} else {
zlog_warn(
"%s %s: IGMP socket fd=%d interface %pI4: could not solve %s to group address: errno=%d: %s",
__FILE__, __func__, fd, &ifaddr, PIM_ALL_ROUTERS, errno,
safe_strerror(errno));
}
/*
IGMP routers periodically send IGMP general queries to
AllSystems=224.0.0.1
IGMP routers must receive general queries for querier election.
*/
if (inet_aton(PIM_ALL_SYSTEMS, &group)) {
if (!pim_socket_join(fd, group, ifaddr, ifp->ifindex, pim_ifp))
++join;
} else {
zlog_warn(
"%s %s: IGMP socket fd=%d interface %pI4: could not solve %s to group address: errno=%d: %s",
__FILE__, __func__, fd, &ifaddr,
PIM_ALL_SYSTEMS, errno, safe_strerror(errno));
}
if (inet_aton(PIM_ALL_IGMP_ROUTERS, &group)) {
if (!pim_socket_join(fd, group, ifaddr, ifp->ifindex,
pim_ifp)) {
++join;
}
} else {
zlog_warn(
"%s %s: IGMP socket fd=%d interface %pI4: could not solve %s to group address: errno=%d: %s",
__FILE__, __func__, fd, &ifaddr,
PIM_ALL_IGMP_ROUTERS, errno, safe_strerror(errno));
}
if (!join) {
flog_err_sys(
EC_LIB_SOCKET,
"IGMP socket fd=%d could not join any group on interface address %pI4",
fd, &ifaddr);
close(fd);
fd = -1;
}
return fd;
}
#undef IGMP_SOCK_DUMP
#ifdef IGMP_SOCK_DUMP
static void igmp_sock_dump(array_t *igmp_sock_array)
{
int size = array_size(igmp_sock_array);
for (int i = 0; i < size; ++i) {
struct gm_sock *igmp = array_get(igmp_sock_array, i);
zlog_debug("%s %s: [%d/%d] igmp_addr=%pI4 fd=%d", __FILE__,
__func__, i, size, &igmp->ifaddr,
igmp->fd);
}
}
#endif
struct gm_sock *pim_igmp_sock_lookup_ifaddr(struct list *igmp_sock_list,
struct in_addr ifaddr)
{
struct listnode *sock_node;
struct gm_sock *igmp;
#ifdef IGMP_SOCK_DUMP
igmp_sock_dump(igmp_sock_list);
#endif
for (ALL_LIST_ELEMENTS_RO(igmp_sock_list, sock_node, igmp))
if (ifaddr.s_addr == igmp->ifaddr.s_addr)
return igmp;
return NULL;
}
static void pim_igmp_other_querier_expire(struct event *t)
{
struct gm_sock *igmp;
igmp = EVENT_ARG(t);
assert(!igmp->t_igmp_query_timer);
if (PIM_DEBUG_GM_TRACE) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug("%s: Querier %s resuming", __func__, ifaddr_str);
}
/* Mark the interface address as querier address */
igmp->querier_addr = igmp->ifaddr;
/*
We are the current querier, then
re-start sending general queries.
RFC 2236 - sec 7 Other Querier
present timer expired (Send General
Query, Set Gen. Query. timer)
*/
pim_igmp_general_query(t);
}
void pim_igmp_other_querier_timer_on(struct gm_sock *igmp)
{
long other_querier_present_interval_msec;
struct pim_interface *pim_ifp;
assert(igmp);
assert(igmp->interface);
assert(igmp->interface->info);
pim_ifp = igmp->interface->info;
if (igmp->t_other_querier_timer) {
/*
There is other querier present already,
then reset the other-querier-present timer.
*/
if (PIM_DEBUG_GM_TRACE) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug(
"Querier %s resetting TIMER event for Other-Querier-Present",
ifaddr_str);
}
EVENT_OFF(igmp->t_other_querier_timer);
} else {
/*
We are the current querier, then stop sending general queries:
igmp->t_igmp_query_timer = NULL;
*/
pim_igmp_general_query_off(igmp);
}
/*
Since this socket is starting the other-querier-present timer,
there should not be periodic query timer for this socket.
*/
assert(!igmp->t_igmp_query_timer);
/*
RFC 3376: 8.5. Other Querier Present Interval
The Other Querier Present Interval is the length of time that must
pass before a multicast router decides that there is no longer
another multicast router which should be the querier. This value
MUST be ((the Robustness Variable) times (the Query Interval)) plus
(one half of one Query Response Interval).
other_querier_present_interval_msec = \
igmp->querier_robustness_variable * \
1000 * igmp->querier_query_interval + \
100 * (pim_ifp->query_max_response_time_dsec >> 1);
*/
other_querier_present_interval_msec = PIM_IGMP_OQPI_MSEC(
igmp->querier_robustness_variable, igmp->querier_query_interval,
pim_ifp->gm_query_max_response_time_dsec);
if (PIM_DEBUG_GM_TRACE) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug(
"Querier %s scheduling %ld.%03ld sec TIMER event for Other-Querier-Present",
ifaddr_str, other_querier_present_interval_msec / 1000,
other_querier_present_interval_msec % 1000);
}
event_add_timer_msec(router->master, pim_igmp_other_querier_expire,
igmp, other_querier_present_interval_msec,
&igmp->t_other_querier_timer);
}
void pim_igmp_other_querier_timer_off(struct gm_sock *igmp)
{
assert(igmp);
if (PIM_DEBUG_GM_TRACE) {
if (igmp->t_other_querier_timer) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug(
"IGMP querier %s fd=%d cancelling other-querier-present TIMER event on %s",
ifaddr_str, igmp->fd, igmp->interface->name);
}
}
EVENT_OFF(igmp->t_other_querier_timer);
}
int igmp_validate_checksum(char *igmp_msg, int igmp_msg_len)
{
uint16_t recv_checksum;
uint16_t checksum;
IGMP_GET_INT16((unsigned char *)(igmp_msg + IGMP_CHECKSUM_OFFSET),
recv_checksum);
/* Clear the checksum field */
memset(igmp_msg + IGMP_CHECKSUM_OFFSET, 0, 2);
checksum = in_cksum(igmp_msg, igmp_msg_len);
if (ntohs(checksum) != recv_checksum) {
zlog_warn("Invalid checksum received %x, calculated %x",
recv_checksum, ntohs(checksum));
return -1;
}
return 0;
}
static int igmp_recv_query(struct gm_sock *igmp, int query_version,
int max_resp_code, struct in_addr from,
const char *from_str, char *igmp_msg,
int igmp_msg_len)
{
struct interface *ifp;
struct pim_interface *pim_ifp;
struct in_addr group_addr;
if (igmp->mtrace_only)
return 0;
memcpy(&group_addr, igmp_msg + 4, sizeof(struct in_addr));
ifp = igmp->interface;
pim_ifp = ifp->info;
if (igmp_validate_checksum(igmp_msg, igmp_msg_len) == -1) {
zlog_warn(
"Recv IGMP query v%d from %s on %s with invalid checksum",
query_version, from_str, ifp->name);
return -1;
}
if (!pim_if_connected_to_source(ifp, from)) {
if (PIM_DEBUG_GM_PACKETS)
zlog_debug("Recv IGMP query on interface: %s from a non-connected source: %s",
ifp->name, from_str);
return 0;
}
if (if_address_is_local(&from, AF_INET, ifp->vrf->vrf_id)) {
if (PIM_DEBUG_GM_PACKETS)
zlog_debug("Recv IGMP query on interface: %s from ourself %s",
ifp->name, from_str);
return 0;
}
/* Collecting IGMP Rx stats */
switch (query_version) {
case 1:
igmp->igmp_stats.query_v1++;
break;
case 2:
igmp->igmp_stats.query_v2++;
break;
case 3:
igmp->igmp_stats.query_v3++;
break;
default:
igmp->igmp_stats.unsupported++;
}
/*
* RFC 3376 defines some guidelines on operating in backwards
* compatibility with older versions of IGMP but there are some gaps in
* the logic:
*
* - once we drop from say version 3 to version 2 we will never go back
* to version 3 even if the node that TXed an IGMP v2 query upgrades
* to v3
*
* - The node with the lowest IP is the querier so we will only know to
* drop from v3 to v2 if the node that is the querier is also the one
* that is running igmp v2. If a non-querier only supports igmp v2
* we will have no way of knowing.
*
* For now we will simplify things and inform the user that they need to
* configure all PIM routers to use the same version of IGMP.
*/
if (query_version != pim_ifp->igmp_version) {
zlog_warn(
"Recv IGMP query v%d from %s on %s but we are using v%d, please configure all PIM routers on this subnet to use the same IGMP version",
query_version, from_str, ifp->name,
pim_ifp->igmp_version);
return 0;
}
if (PIM_DEBUG_GM_PACKETS) {
char group_str[INET_ADDRSTRLEN];
pim_inet4_dump("<group?>", group_addr, group_str,
sizeof(group_str));
zlog_debug("Recv IGMP query v%d from %s on %s for group %s",
query_version, from_str, ifp->name, group_str);
}
/*
RFC 3376: 6.6.2. Querier Election
When a router receives a query with a lower IP address, it sets
the Other-Querier-Present timer to Other Querier Present Interval
and ceases to send queries on the network if it was the previously
elected querier.
*/
if (ntohl(from.s_addr) < ntohl(igmp->ifaddr.s_addr)) {
/* As per RFC 2236 section 3:
* When a Querier receives a Leave Group message for a group
* that has group members on the reception interface, it sends
* [Last Member Query Count] Group-Specific Queries every [Last
* Member Query Interval] to the group being left. These
* Group-Specific Queries have their Max Response time set to
* [Last Member Query Interval]. If no Reports are received
* after the response time of the last query expires, the
* routers assume that the group has no local members, as above.
* Any Querier to non-Querier transition is ignored during this
* time; the same router keeps sending the Group-Specific
* Queries.
*/
const struct gm_group *group;
const struct listnode *grpnode;
for (ALL_LIST_ELEMENTS_RO(pim_ifp->gm_group_list, grpnode,
group)) {
if (!group->t_group_query_retransmit_timer)
continue;
if (PIM_DEBUG_GM_TRACE)
zlog_debug(
"%s: lower address query packet from %s is ignored when last member query interval timer is running",
ifp->name, from_str);
return 0;
}
if (PIM_DEBUG_GM_TRACE) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug(
"%s: local address %s (%u) lost querier election to %s (%u)",
ifp->name, ifaddr_str,
ntohl(igmp->ifaddr.s_addr), from_str,
ntohl(from.s_addr));
}
/* Reset the other querier timer only if query is received from
* the previously elected querier or a better new querier
* This will make sure that non-querier elects the new querier
* whose ip address is higher than the old querier
* in case the old querier goes down via other querier present
* timer expiry
*/
if (ntohl(from.s_addr) <= ntohl(igmp->querier_addr.s_addr)) {
igmp->querier_addr.s_addr = from.s_addr;
pim_igmp_other_querier_timer_on(igmp);
}
}
/* IGMP version 3 is the only one where we process the RXed query */
if (query_version == 3) {
igmp_v3_recv_query(igmp, from_str, igmp_msg);
}
return 0;
}
static void on_trace(const char *label, struct interface *ifp,
struct in_addr from)
{
if (PIM_DEBUG_GM_TRACE) {
char from_str[INET_ADDRSTRLEN];
pim_inet4_dump("<from?>", from, from_str, sizeof(from_str));
zlog_debug("%s: from %s on %s", label, from_str, ifp->name);
}
}
static int igmp_v1_recv_report(struct gm_sock *igmp, struct in_addr from,
const char *from_str, char *igmp_msg,
int igmp_msg_len)
{
struct interface *ifp = igmp->interface;
struct gm_group *group;
struct in_addr group_addr;
on_trace(__func__, igmp->interface, from);
if (igmp->mtrace_only)
return 0;
if (igmp_msg_len != IGMP_V12_MSG_SIZE) {
zlog_warn(
"Recv IGMP report v1 from %s on %s: size=%d other than correct=%d",
from_str, ifp->name, igmp_msg_len, IGMP_V12_MSG_SIZE);
return -1;
}
if (igmp_validate_checksum(igmp_msg, igmp_msg_len) == -1) {
zlog_warn(
"Recv IGMP report v1 from %s on %s with invalid checksum",
from_str, ifp->name);
return -1;
}
/* Collecting IGMP Rx stats */
igmp->igmp_stats.report_v1++;
if (PIM_DEBUG_GM_TRACE) {
zlog_warn("%s %s: FIXME WRITEME", __FILE__, __func__);
}
memcpy(&group_addr, igmp_msg + 4, sizeof(struct in_addr));
if (pim_is_group_filtered(ifp->info, &group_addr))
return -1;
/* non-existent group is created as INCLUDE {empty} */
group = igmp_add_group_by_addr(igmp, group_addr);
if (!group) {
return -1;
}
group->last_igmp_v1_report_dsec = pim_time_monotonic_dsec();
return 0;
}
bool pim_igmp_verify_header(struct ip *ip_hdr, size_t len, size_t *hlen)
{
char *igmp_msg;
int igmp_msg_len;
int msg_type;
size_t ip_hlen; /* ip header length in bytes */
if (len < sizeof(*ip_hdr)) {
zlog_warn("IGMP packet size=%zu shorter than minimum=%zu", len,
sizeof(*ip_hdr));
return false;
}
ip_hlen = ip_hdr->ip_hl << 2; /* ip_hl gives length in 4-byte words */
*hlen = ip_hlen;
if (ip_hlen > len) {
zlog_warn(
"IGMP packet header claims size %zu, but we only have %zu bytes",
ip_hlen, len);
return false;
}
igmp_msg = (char *)ip_hdr + ip_hlen;
igmp_msg_len = len - ip_hlen;
msg_type = *igmp_msg;
if (igmp_msg_len < PIM_IGMP_MIN_LEN) {
zlog_warn("IGMP message size=%d shorter than minimum=%d",
igmp_msg_len, PIM_IGMP_MIN_LEN);
return false;
}
if ((msg_type != PIM_IGMP_MTRACE_RESPONSE)
&& (msg_type != PIM_IGMP_MTRACE_QUERY_REQUEST)) {
if (ip_hdr->ip_ttl != 1) {
zlog_warn(
"Recv IGMP packet with invalid ttl=%u, discarding the packet",
ip_hdr->ip_ttl);
return false;
}
}
return true;
}
int pim_igmp_packet(struct gm_sock *igmp, char *buf, size_t len)
{
struct ip *ip_hdr = (struct ip *)buf;
size_t ip_hlen; /* ip header length in bytes */
char *igmp_msg;
int igmp_msg_len;
int msg_type;
char from_str[INET_ADDRSTRLEN];
char to_str[INET_ADDRSTRLEN];
if (!pim_igmp_verify_header(ip_hdr, len, &ip_hlen))
return -1;
igmp_msg = buf + ip_hlen;
igmp_msg_len = len - ip_hlen;
msg_type = *igmp_msg;
pim_inet4_dump("<src?>", ip_hdr->ip_src, from_str, sizeof(from_str));
pim_inet4_dump("<dst?>", ip_hdr->ip_dst, to_str, sizeof(to_str));
if (PIM_DEBUG_GM_PACKETS) {
zlog_debug(
"Recv IGMP packet from %s to %s on %s: size=%zu ttl=%d msg_type=%d msg_size=%d",
from_str, to_str, igmp->interface->name, len, ip_hdr->ip_ttl,
msg_type, igmp_msg_len);
}
switch (msg_type) {
case PIM_IGMP_MEMBERSHIP_QUERY: {
int max_resp_code = igmp_msg[1];
int query_version;
/*
RFC 3376: 7.1. Query Version Distinctions
IGMPv1 Query: length = 8 octets AND Max Resp Code field is
zero
IGMPv2 Query: length = 8 octets AND Max Resp Code field is
non-zero
IGMPv3 Query: length >= 12 octets
*/
if (igmp_msg_len == 8) {
query_version = max_resp_code ? 2 : 1;
} else if (igmp_msg_len >= 12) {
query_version = 3;
} else {
zlog_warn("Unknown IGMP query version");
return -1;
}
return igmp_recv_query(igmp, query_version, max_resp_code,
ip_hdr->ip_src, from_str, igmp_msg,
igmp_msg_len);
}
case PIM_IGMP_V3_MEMBERSHIP_REPORT:
return igmp_v3_recv_report(igmp, ip_hdr->ip_src, from_str,
igmp_msg, igmp_msg_len);
case PIM_IGMP_V2_MEMBERSHIP_REPORT:
return igmp_v2_recv_report(igmp, ip_hdr->ip_src, from_str,
igmp_msg, igmp_msg_len);
case PIM_IGMP_V1_MEMBERSHIP_REPORT:
return igmp_v1_recv_report(igmp, ip_hdr->ip_src, from_str,
igmp_msg, igmp_msg_len);
case PIM_IGMP_V2_LEAVE_GROUP:
return igmp_v2_recv_leave(igmp, ip_hdr, from_str, igmp_msg,
igmp_msg_len);
case PIM_IGMP_MTRACE_RESPONSE:
return igmp_mtrace_recv_response(igmp, ip_hdr, ip_hdr->ip_src,
from_str, igmp_msg,
igmp_msg_len);
case PIM_IGMP_MTRACE_QUERY_REQUEST:
return igmp_mtrace_recv_qry_req(igmp, ip_hdr, ip_hdr->ip_src,
from_str, igmp_msg,
igmp_msg_len);
}
zlog_warn("Ignoring unsupported IGMP message type: %d", msg_type);
/* Collecting IGMP Rx stats */
igmp->igmp_stats.unsupported++;
return -1;
}
void pim_igmp_general_query_on(struct gm_sock *igmp)
{
struct pim_interface *pim_ifp;
int startup_mode;
int query_interval;
/*
Since this socket is starting as querier,
there should not exist a timer for other-querier-present.
*/
assert(!igmp->t_other_querier_timer);
pim_ifp = igmp->interface->info;
assert(pim_ifp);
/*
RFC 3376: 8.6. Startup Query Interval
The Startup Query Interval is the interval between General Queries
sent by a Querier on startup. Default: 1/4 the Query Interval.
The first one should be sent out immediately instead of 125/4
seconds from now.
*/
startup_mode = igmp->startup_query_count > 0;
if (startup_mode) {
/*
* If this is the first time we are sending a query on a
* newly configured igmp interface send it out in 1 second
* just to give the entire world a tiny bit of time to settle
* else the query interval is:
* query_interval = pim_ifp->gm_default_query_interval >> 2;
*/
if (igmp->startup_query_count ==
igmp->querier_robustness_variable)
query_interval = 1;
else
query_interval = PIM_IGMP_SQI(
pim_ifp->gm_default_query_interval);
--igmp->startup_query_count;
} else {
query_interval = igmp->querier_query_interval;
}
if (PIM_DEBUG_GM_TRACE) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug(
"Querier %s scheduling %d-second (%s) TIMER event for IGMP query on fd=%d",
ifaddr_str, query_interval,
startup_mode ? "startup" : "non-startup", igmp->fd);
}
event_add_timer(router->master, pim_igmp_general_query, igmp,
query_interval, &igmp->t_igmp_query_timer);
}
void pim_igmp_general_query_off(struct gm_sock *igmp)
{
assert(igmp);
if (PIM_DEBUG_GM_TRACE) {
if (igmp->t_igmp_query_timer) {
char ifaddr_str[INET_ADDRSTRLEN];
pim_inet4_dump("<ifaddr?>", igmp->ifaddr, ifaddr_str,
sizeof(ifaddr_str));
zlog_debug(
"IGMP querier %s fd=%d cancelling query TIMER event on %s",
ifaddr_str, igmp->fd, igmp->interface->name);
}
}
EVENT_OFF(igmp->t_igmp_query_timer);
}
/* Issue IGMP general query */
static void pim_igmp_general_query(struct event *t)
{
struct gm_sock *igmp;
struct in_addr dst_addr;
struct in_addr group_addr;
struct pim_interface *pim_ifp;
int query_buf_size;
igmp = EVENT_ARG(t);
assert(igmp->interface);
assert(igmp->interface->info);
pim_ifp = igmp->interface->info;
if (pim_ifp->igmp_version == 3) {
query_buf_size = PIM_IGMP_BUFSIZE_WRITE;
} else {
query_buf_size = IGMP_V12_MSG_SIZE;
}
char query_buf[query_buf_size];
/*
RFC3376: 4.1.12. IP Destination Addresses for Queries
In IGMPv3, General Queries are sent with an IP destination address
of 224.0.0.1, the all-systems multicast address. Group-Specific
and Group-and-Source-Specific Queries are sent with an IP
destination address equal to the multicast address of interest.
*/
dst_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
group_addr.s_addr = PIM_NET_INADDR_ANY;
if (PIM_DEBUG_GM_TRACE) {
char querier_str[INET_ADDRSTRLEN];
char dst_str[INET_ADDRSTRLEN];
pim_inet4_dump("<querier?>", igmp->ifaddr, querier_str,
sizeof(querier_str));
pim_inet4_dump("<dst?>", dst_addr, dst_str, sizeof(dst_str));
zlog_debug("Querier %s issuing IGMP general query to %s on %s",
querier_str, dst_str, igmp->interface->name);
}
igmp_send_query(pim_ifp->igmp_version, 0 /* igmp_group */, query_buf,
sizeof(query_buf), 0 /* num_sources */, dst_addr,
group_addr, pim_ifp->gm_query_max_response_time_dsec,
1 /* s_flag: always set for general queries */, igmp);
pim_igmp_general_query_on(igmp);
}
static void sock_close(struct gm_sock *igmp)
{
pim_igmp_other_querier_timer_off(igmp);
pim_igmp_general_query_off(igmp);
if (PIM_DEBUG_GM_TRACE_DETAIL) {
if (igmp->t_igmp_read) {
zlog_debug(
"Cancelling READ event on IGMP socket %pI4 fd=%d on interface %s",
&igmp->ifaddr, igmp->fd,
igmp->interface->name);
}
}
EVENT_OFF(igmp->t_igmp_read);
if (close(igmp->fd)) {
flog_err(
EC_LIB_SOCKET,
"Failure closing IGMP socket %pI4 fd=%d on interface %s: errno=%d: %s",
&igmp->ifaddr, igmp->fd,
igmp->interface->name, errno, safe_strerror(errno));
}
if (PIM_DEBUG_GM_TRACE_DETAIL) {
zlog_debug("Deleted IGMP socket %pI4 fd=%d on interface %s",
&igmp->ifaddr, igmp->fd,
igmp->interface->name);
}
}
void igmp_startup_mode_on(struct gm_sock *igmp)
{
struct pim_interface *pim_ifp;
pim_ifp = igmp->interface->info;
/*
RFC 3376: 8.7. Startup Query Count
The Startup Query Count is the number of Queries sent out on
startup, separated by the Startup Query Interval. Default: the
Robustness Variable.
*/
igmp->startup_query_count = igmp->querier_robustness_variable;
/*
Since we're (re)starting, reset QQI to default Query Interval
*/
igmp->querier_query_interval = pim_ifp->gm_default_query_interval;
}
static void igmp_group_free(struct gm_group *group)
{
list_delete(&group->group_source_list);
XFREE(MTYPE_PIM_IGMP_GROUP, group);
}