3 * Copyright (C) 2008 Everton da Silva Marques
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "lib_errors.h"
31 #include "pim_igmpv2.h"
32 #include "pim_igmpv3.h"
33 #include "pim_igmp_mtrace.h"
34 #include "pim_iface.h"
36 #include "pim_mroute.h"
40 #include "pim_zebra.h"
42 static void group_timer_off(struct igmp_group
*group
);
43 static int pim_igmp_general_query(struct thread
*t
);
45 /* This socket is used for TXing IGMP packets only, IGMP RX happens
48 static int igmp_sock_open(struct in_addr ifaddr
, struct interface
*ifp
,
55 fd
= pim_socket_mcast(IPPROTO_IGMP
, ifaddr
, ifp
, 1);
60 if (PIM_IF_TEST_IGMP_LISTEN_ALLROUTERS(pim_options
)) {
61 if (inet_aton(PIM_ALL_ROUTERS
, &group
)) {
62 if (!pim_socket_join(fd
, group
, ifaddr
, ifp
->ifindex
))
66 "%s %s: IGMP socket fd=%d interface %s: could not solve %s to group address: errno=%d: %s",
67 __FILE__
, __func__
, fd
, inet_ntoa(ifaddr
),
68 PIM_ALL_ROUTERS
, errno
, safe_strerror(errno
));
73 IGMP routers periodically send IGMP general queries to
75 IGMP routers must receive general queries for querier election.
77 if (inet_aton(PIM_ALL_SYSTEMS
, &group
)) {
78 if (!pim_socket_join(fd
, group
, ifaddr
, ifp
->ifindex
))
82 "%s %s: IGMP socket fd=%d interface %s: could not solve %s to group address: errno=%d: %s",
83 __FILE__
, __func__
, fd
, inet_ntoa(ifaddr
),
84 PIM_ALL_SYSTEMS
, errno
, safe_strerror(errno
));
87 if (inet_aton(PIM_ALL_IGMP_ROUTERS
, &group
)) {
88 if (!pim_socket_join(fd
, group
, ifaddr
, ifp
->ifindex
)) {
93 "%s %s: IGMP socket fd=%d interface %s: could not solve %s to group address: errno=%d: %s",
94 __FILE__
, __func__
, fd
, inet_ntoa(ifaddr
),
95 PIM_ALL_IGMP_ROUTERS
, errno
, safe_strerror(errno
));
101 "IGMP socket fd=%d could not join any group on interface address %s",
102 fd
, inet_ntoa(ifaddr
));
110 #undef IGMP_SOCK_DUMP
112 #ifdef IGMP_SOCK_DUMP
113 static void igmp_sock_dump(array_t
*igmp_sock_array
)
115 int size
= array_size(igmp_sock_array
);
116 for (int i
= 0; i
< size
; ++i
) {
118 struct igmp_sock
*igmp
= array_get(igmp_sock_array
, i
);
120 zlog_debug("%s %s: [%d/%d] igmp_addr=%s fd=%d", __FILE__
,
121 __func__
, i
, size
, inet_ntoa(igmp
->ifaddr
),
127 struct igmp_sock
*pim_igmp_sock_lookup_ifaddr(struct list
*igmp_sock_list
,
128 struct in_addr ifaddr
)
130 struct listnode
*sock_node
;
131 struct igmp_sock
*igmp
;
133 #ifdef IGMP_SOCK_DUMP
134 igmp_sock_dump(igmp_sock_list
);
137 for (ALL_LIST_ELEMENTS_RO(igmp_sock_list
, sock_node
, igmp
))
138 if (ifaddr
.s_addr
== igmp
->ifaddr
.s_addr
)
144 struct igmp_sock
*igmp_sock_lookup_by_fd(struct list
*igmp_sock_list
, int fd
)
146 struct listnode
*sock_node
;
147 struct igmp_sock
*igmp
;
149 for (ALL_LIST_ELEMENTS_RO(igmp_sock_list
, sock_node
, igmp
))
156 static int pim_igmp_other_querier_expire(struct thread
*t
)
158 struct igmp_sock
*igmp
;
160 igmp
= THREAD_ARG(t
);
162 zassert(!igmp
->t_igmp_query_timer
);
164 if (PIM_DEBUG_IGMP_TRACE
) {
165 char ifaddr_str
[INET_ADDRSTRLEN
];
166 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
168 zlog_debug("%s: Querier %s resuming", __func__
, ifaddr_str
);
172 We are the current querier, then
173 re-start sending general queries.
174 RFC 2236 - sec 7 Other Querier
175 present timer expired (Send General
176 Query, Set Gen. Query. timer)
178 pim_igmp_general_query(t
);
183 void pim_igmp_other_querier_timer_on(struct igmp_sock
*igmp
)
185 long other_querier_present_interval_msec
;
186 struct pim_interface
*pim_ifp
;
189 zassert(igmp
->interface
);
190 zassert(igmp
->interface
->info
);
192 pim_ifp
= igmp
->interface
->info
;
194 if (igmp
->t_other_querier_timer
) {
196 There is other querier present already,
197 then reset the other-querier-present timer.
200 if (PIM_DEBUG_IGMP_TRACE
) {
201 char ifaddr_str
[INET_ADDRSTRLEN
];
202 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
205 "Querier %s resetting TIMER event for Other-Querier-Present",
208 THREAD_OFF(igmp
->t_other_querier_timer
);
211 We are the current querier, then stop sending general queries:
212 igmp->t_igmp_query_timer = NULL;
214 pim_igmp_general_query_off(igmp
);
218 Since this socket is starting the other-querier-present timer,
219 there should not be periodic query timer for this socket.
221 zassert(!igmp
->t_igmp_query_timer
);
224 RFC 3376: 8.5. Other Querier Present Interval
226 The Other Querier Present Interval is the length of time that must
227 pass before a multicast router decides that there is no longer
228 another multicast router which should be the querier. This value
229 MUST be ((the Robustness Variable) times (the Query Interval)) plus
230 (one half of one Query Response Interval).
232 other_querier_present_interval_msec = \
233 igmp->querier_robustness_variable * \
234 1000 * igmp->querier_query_interval + \
235 100 * (pim_ifp->query_max_response_time_dsec >> 1);
237 other_querier_present_interval_msec
= PIM_IGMP_OQPI_MSEC(
238 igmp
->querier_robustness_variable
, igmp
->querier_query_interval
,
239 pim_ifp
->igmp_query_max_response_time_dsec
);
241 if (PIM_DEBUG_IGMP_TRACE
) {
242 char ifaddr_str
[INET_ADDRSTRLEN
];
243 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
246 "Querier %s scheduling %ld.%03ld sec TIMER event for Other-Querier-Present",
247 ifaddr_str
, other_querier_present_interval_msec
/ 1000,
248 other_querier_present_interval_msec
% 1000);
251 thread_add_timer_msec(router
->master
, pim_igmp_other_querier_expire
,
252 igmp
, other_querier_present_interval_msec
,
253 &igmp
->t_other_querier_timer
);
256 void pim_igmp_other_querier_timer_off(struct igmp_sock
*igmp
)
260 if (PIM_DEBUG_IGMP_TRACE
) {
261 if (igmp
->t_other_querier_timer
) {
262 char ifaddr_str
[INET_ADDRSTRLEN
];
263 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
266 "IGMP querier %s fd=%d cancelling other-querier-present TIMER event on %s",
267 ifaddr_str
, igmp
->fd
, igmp
->interface
->name
);
270 THREAD_OFF(igmp
->t_other_querier_timer
);
273 static int igmp_recv_query(struct igmp_sock
*igmp
, int query_version
,
274 int max_resp_code
, struct in_addr from
,
275 const char *from_str
, char *igmp_msg
,
278 struct interface
*ifp
;
279 struct pim_interface
*pim_ifp
;
280 struct in_addr group_addr
;
281 uint16_t recv_checksum
;
284 if (igmp
->mtrace_only
)
287 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
289 ifp
= igmp
->interface
;
292 recv_checksum
= *(uint16_t *)(igmp_msg
+ IGMP_CHECKSUM_OFFSET
);
294 /* for computing checksum */
295 *(uint16_t *)(igmp_msg
+ IGMP_CHECKSUM_OFFSET
) = 0;
297 checksum
= in_cksum(igmp_msg
, igmp_msg_len
);
298 if (checksum
!= recv_checksum
) {
300 "Recv IGMP query v%d from %s on %s: checksum mismatch: received=%x computed=%x",
301 query_version
, from_str
, ifp
->name
, recv_checksum
,
306 if (!pim_if_connected_to_source(ifp
, from
)) {
307 if (PIM_DEBUG_IGMP_PACKETS
)
308 zlog_debug("Recv IGMP query on interface: %s from a non-connected source: %s",
309 ifp
->name
, from_str
);
313 if (if_lookup_exact_address(&from
, AF_INET
, ifp
->vrf_id
)) {
314 if (PIM_DEBUG_IGMP_PACKETS
)
315 zlog_debug("Recv IGMP query on interface: %s from ourself %s",
316 ifp
->name
, from_str
);
320 /* Collecting IGMP Rx stats */
321 switch (query_version
) {
323 igmp
->rx_stats
.query_v1
++;
326 igmp
->rx_stats
.query_v2
++;
329 igmp
->rx_stats
.query_v3
++;
332 igmp
->rx_stats
.unsupported
++;
336 * RFC 3376 defines some guidelines on operating in backwards
337 * compatibility with older versions of IGMP but there are some gaps in
340 * - once we drop from say version 3 to version 2 we will never go back
341 * to version 3 even if the node that TXed an IGMP v2 query upgrades
344 * - The node with the lowest IP is the querier so we will only know to
345 * drop from v3 to v2 if the node that is the querier is also the one
346 * that is running igmp v2. If a non-querier only supports igmp v2
347 * we will have no way of knowing.
349 * For now we will simplify things and inform the user that they need to
350 * configure all PIM routers to use the same version of IGMP.
352 if (query_version
!= pim_ifp
->igmp_version
) {
354 "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",
355 query_version
, from_str
, ifp
->name
,
356 pim_ifp
->igmp_version
);
360 if (PIM_DEBUG_IGMP_PACKETS
) {
361 char group_str
[INET_ADDRSTRLEN
];
362 pim_inet4_dump("<group?>", group_addr
, group_str
,
364 zlog_debug("Recv IGMP query v%d from %s on %s for group %s",
365 query_version
, from_str
, ifp
->name
, group_str
);
369 RFC 3376: 6.6.2. Querier Election
371 When a router receives a query with a lower IP address, it sets
372 the Other-Querier-Present timer to Other Querier Present Interval
373 and ceases to send queries on the network if it was the previously
376 if (ntohl(from
.s_addr
) < ntohl(igmp
->ifaddr
.s_addr
)) {
378 if (PIM_DEBUG_IGMP_TRACE
) {
379 char ifaddr_str
[INET_ADDRSTRLEN
];
380 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
383 "%s: local address %s (%u) lost querier election to %s (%u)",
384 ifp
->name
, ifaddr_str
,
385 ntohl(igmp
->ifaddr
.s_addr
), from_str
,
389 pim_igmp_other_querier_timer_on(igmp
);
392 /* IGMP version 3 is the only one where we process the RXed query */
393 if (query_version
== 3) {
394 igmp_v3_recv_query(igmp
, from_str
, igmp_msg
);
400 static void on_trace(const char *label
, struct interface
*ifp
,
403 if (PIM_DEBUG_IGMP_TRACE
) {
404 char from_str
[INET_ADDRSTRLEN
];
405 pim_inet4_dump("<from?>", from
, from_str
, sizeof(from_str
));
406 zlog_debug("%s: from %s on %s", label
, from_str
, ifp
->name
);
410 static int igmp_v1_recv_report(struct igmp_sock
*igmp
, struct in_addr from
,
411 const char *from_str
, char *igmp_msg
,
414 struct interface
*ifp
= igmp
->interface
;
415 struct igmp_group
*group
;
416 struct in_addr group_addr
;
418 on_trace(__func__
, igmp
->interface
, from
);
420 if (igmp
->mtrace_only
)
423 if (igmp_msg_len
!= IGMP_V12_MSG_SIZE
) {
425 "Recv IGMP report v1 from %s on %s: size=%d other than correct=%d",
426 from_str
, ifp
->name
, igmp_msg_len
, IGMP_V12_MSG_SIZE
);
430 /* Collecting IGMP Rx stats */
431 igmp
->rx_stats
.report_v1
++;
433 if (PIM_DEBUG_IGMP_TRACE
) {
434 zlog_warn("%s %s: FIXME WRITEME", __FILE__
, __func__
);
437 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
439 if (pim_is_group_filtered(ifp
->info
, &group_addr
))
442 /* non-existant group is created as INCLUDE {empty} */
443 group
= igmp_add_group_by_addr(igmp
, group_addr
);
448 group
->last_igmp_v1_report_dsec
= pim_time_monotonic_dsec();
453 int pim_igmp_packet(struct igmp_sock
*igmp
, char *buf
, size_t len
)
456 size_t ip_hlen
; /* ip header length in bytes */
460 char from_str
[INET_ADDRSTRLEN
];
461 char to_str
[INET_ADDRSTRLEN
];
463 if (len
< sizeof(*ip_hdr
)) {
464 zlog_warn("IGMP packet size=%zu shorter than minimum=%zu", len
,
469 ip_hdr
= (struct ip
*)buf
;
471 pim_inet4_dump("<src?>", ip_hdr
->ip_src
, from_str
, sizeof(from_str
));
472 pim_inet4_dump("<dst?>", ip_hdr
->ip_dst
, to_str
, sizeof(to_str
));
474 ip_hlen
= ip_hdr
->ip_hl
<< 2; /* ip_hl gives length in 4-byte words */
478 "IGMP packet header claims size %zu, but we only have %zu bytes",
483 igmp_msg
= buf
+ ip_hlen
;
484 igmp_msg_len
= len
- ip_hlen
;
486 if (igmp_msg_len
< PIM_IGMP_MIN_LEN
) {
487 zlog_warn("IGMP message size=%d shorter than minimum=%d",
488 igmp_msg_len
, PIM_IGMP_MIN_LEN
);
492 msg_type
= *igmp_msg
;
494 if (PIM_DEBUG_IGMP_PACKETS
) {
496 "Recv IGMP packet from %s to %s on %s: size=%zu ttl=%d msg_type=%d msg_size=%d",
497 from_str
, to_str
, igmp
->interface
->name
, len
, ip_hdr
->ip_ttl
,
498 msg_type
, igmp_msg_len
);
502 case PIM_IGMP_MEMBERSHIP_QUERY
: {
503 int max_resp_code
= igmp_msg
[1];
507 RFC 3376: 7.1. Query Version Distinctions
508 IGMPv1 Query: length = 8 octets AND Max Resp Code field is
510 IGMPv2 Query: length = 8 octets AND Max Resp Code field is
512 IGMPv3 Query: length >= 12 octets
515 if (igmp_msg_len
== 8) {
516 query_version
= max_resp_code
? 2 : 1;
517 } else if (igmp_msg_len
>= 12) {
520 zlog_warn("Unknown IGMP query version");
524 return igmp_recv_query(igmp
, query_version
, max_resp_code
,
525 ip_hdr
->ip_src
, from_str
, igmp_msg
,
529 case PIM_IGMP_V3_MEMBERSHIP_REPORT
:
530 return igmp_v3_recv_report(igmp
, ip_hdr
->ip_src
, from_str
,
531 igmp_msg
, igmp_msg_len
);
533 case PIM_IGMP_V2_MEMBERSHIP_REPORT
:
534 return igmp_v2_recv_report(igmp
, ip_hdr
->ip_src
, from_str
,
535 igmp_msg
, igmp_msg_len
);
537 case PIM_IGMP_V1_MEMBERSHIP_REPORT
:
538 return igmp_v1_recv_report(igmp
, ip_hdr
->ip_src
, from_str
,
539 igmp_msg
, igmp_msg_len
);
541 case PIM_IGMP_V2_LEAVE_GROUP
:
542 return igmp_v2_recv_leave(igmp
, ip_hdr
->ip_src
, from_str
,
543 igmp_msg
, igmp_msg_len
);
545 case PIM_IGMP_MTRACE_RESPONSE
:
546 return igmp_mtrace_recv_response(igmp
, ip_hdr
, ip_hdr
->ip_src
,
549 case PIM_IGMP_MTRACE_QUERY_REQUEST
:
550 return igmp_mtrace_recv_qry_req(igmp
, ip_hdr
, ip_hdr
->ip_src
,
555 zlog_warn("Ignoring unsupported IGMP message type: %d", msg_type
);
557 /* Collecting IGMP Rx stats */
558 igmp
->rx_stats
.unsupported
++;
563 void pim_igmp_general_query_on(struct igmp_sock
*igmp
)
565 struct pim_interface
*pim_ifp
;
570 Since this socket is starting as querier,
571 there should not exist a timer for other-querier-present.
573 zassert(!igmp
->t_other_querier_timer
);
574 pim_ifp
= igmp
->interface
->info
;
578 RFC 3376: 8.6. Startup Query Interval
580 The Startup Query Interval is the interval between General Queries
581 sent by a Querier on startup. Default: 1/4 the Query Interval.
582 The first one should be sent out immediately instead of 125/4
585 startup_mode
= igmp
->startup_query_count
> 0;
588 * If this is the first time we are sending a query on a
589 * newly configured igmp interface send it out in 1 second
590 * just to give the entire world a tiny bit of time to settle
591 * else the query interval is:
592 * query_interval = pim_ifp->igmp_default_query_interval >> 2;
594 if (igmp
->startup_query_count
595 == igmp
->querier_robustness_variable
)
598 query_interval
= PIM_IGMP_SQI(
599 pim_ifp
->igmp_default_query_interval
);
601 --igmp
->startup_query_count
;
603 query_interval
= igmp
->querier_query_interval
;
606 if (PIM_DEBUG_IGMP_TRACE
) {
607 char ifaddr_str
[INET_ADDRSTRLEN
];
608 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
611 "Querier %s scheduling %d-second (%s) TIMER event for IGMP query on fd=%d",
612 ifaddr_str
, query_interval
,
613 startup_mode
? "startup" : "non-startup", igmp
->fd
);
615 igmp
->t_igmp_query_timer
= NULL
;
616 thread_add_timer(router
->master
, pim_igmp_general_query
, igmp
,
617 query_interval
, &igmp
->t_igmp_query_timer
);
620 void pim_igmp_general_query_off(struct igmp_sock
*igmp
)
624 if (PIM_DEBUG_IGMP_TRACE
) {
625 if (igmp
->t_igmp_query_timer
) {
626 char ifaddr_str
[INET_ADDRSTRLEN
];
627 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
630 "IGMP querier %s fd=%d cancelling query TIMER event on %s",
631 ifaddr_str
, igmp
->fd
, igmp
->interface
->name
);
634 THREAD_OFF(igmp
->t_igmp_query_timer
);
637 /* Issue IGMP general query */
638 static int pim_igmp_general_query(struct thread
*t
)
640 struct igmp_sock
*igmp
;
641 struct in_addr dst_addr
;
642 struct in_addr group_addr
;
643 struct pim_interface
*pim_ifp
;
646 igmp
= THREAD_ARG(t
);
648 zassert(igmp
->interface
);
649 zassert(igmp
->interface
->info
);
651 pim_ifp
= igmp
->interface
->info
;
653 if (pim_ifp
->igmp_version
== 3) {
654 query_buf_size
= PIM_IGMP_BUFSIZE_WRITE
;
656 query_buf_size
= IGMP_V12_MSG_SIZE
;
659 char query_buf
[query_buf_size
];
662 RFC3376: 4.1.12. IP Destination Addresses for Queries
664 In IGMPv3, General Queries are sent with an IP destination address
665 of 224.0.0.1, the all-systems multicast address. Group-Specific
666 and Group-and-Source-Specific Queries are sent with an IP
667 destination address equal to the multicast address of interest.
670 dst_addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
671 group_addr
.s_addr
= PIM_NET_INADDR_ANY
;
673 if (PIM_DEBUG_IGMP_TRACE
) {
674 char querier_str
[INET_ADDRSTRLEN
];
675 char dst_str
[INET_ADDRSTRLEN
];
676 pim_inet4_dump("<querier?>", igmp
->ifaddr
, querier_str
,
677 sizeof(querier_str
));
678 pim_inet4_dump("<dst?>", dst_addr
, dst_str
, sizeof(dst_str
));
679 zlog_debug("Querier %s issuing IGMP general query to %s on %s",
680 querier_str
, dst_str
, igmp
->interface
->name
);
683 igmp_send_query(pim_ifp
->igmp_version
, 0 /* igmp_group */, igmp
->fd
,
684 igmp
->interface
->name
, query_buf
, sizeof(query_buf
),
685 0 /* num_sources */, dst_addr
, group_addr
,
686 pim_ifp
->igmp_query_max_response_time_dsec
,
687 1 /* s_flag: always set for general queries */,
688 igmp
->querier_robustness_variable
,
689 igmp
->querier_query_interval
);
691 pim_igmp_general_query_on(igmp
);
696 static void sock_close(struct igmp_sock
*igmp
)
698 pim_igmp_other_querier_timer_off(igmp
);
699 pim_igmp_general_query_off(igmp
);
701 if (PIM_DEBUG_IGMP_TRACE_DETAIL
) {
702 if (igmp
->t_igmp_read
) {
704 "Cancelling READ event on IGMP socket %s fd=%d on interface %s",
705 inet_ntoa(igmp
->ifaddr
), igmp
->fd
,
706 igmp
->interface
->name
);
709 THREAD_OFF(igmp
->t_igmp_read
);
711 if (close(igmp
->fd
)) {
714 "Failure closing IGMP socket %s fd=%d on interface %s: errno=%d: %s",
715 inet_ntoa(igmp
->ifaddr
), igmp
->fd
,
716 igmp
->interface
->name
, errno
, safe_strerror(errno
));
719 if (PIM_DEBUG_IGMP_TRACE_DETAIL
) {
720 zlog_debug("Deleted IGMP socket %s fd=%d on interface %s",
721 inet_ntoa(igmp
->ifaddr
), igmp
->fd
,
722 igmp
->interface
->name
);
726 void igmp_startup_mode_on(struct igmp_sock
*igmp
)
728 struct pim_interface
*pim_ifp
;
730 pim_ifp
= igmp
->interface
->info
;
733 RFC 3376: 8.7. Startup Query Count
735 The Startup Query Count is the number of Queries sent out on
736 startup, separated by the Startup Query Interval. Default: the
739 igmp
->startup_query_count
= igmp
->querier_robustness_variable
;
742 Since we're (re)starting, reset QQI to default Query Interval
744 igmp
->querier_query_interval
= pim_ifp
->igmp_default_query_interval
;
747 static void igmp_group_free(struct igmp_group
*group
)
749 list_delete(&group
->group_source_list
);
751 XFREE(MTYPE_PIM_IGMP_GROUP
, group
);
754 static void igmp_group_count_incr(struct igmp_sock
*igmp
)
756 struct pim_interface
*pim_ifp
= igmp
->interface
->info
;
761 ++pim_ifp
->pim
->igmp_group_count
;
762 if (pim_ifp
->pim
->igmp_group_count
763 == pim_ifp
->pim
->igmp_watermark_limit
) {
765 "IGMP group count reached watermark limit: %u(vrf: %s)",
766 pim_ifp
->pim
->igmp_group_count
,
767 VRF_LOGNAME(pim_ifp
->pim
->vrf
));
771 static void igmp_group_count_decr(struct igmp_sock
*igmp
)
773 struct pim_interface
*pim_ifp
= igmp
->interface
->info
;
778 if (pim_ifp
->pim
->igmp_group_count
== 0) {
779 zlog_warn("Cannot decrement igmp group count below 0(vrf: %s)",
780 VRF_LOGNAME(pim_ifp
->pim
->vrf
));
784 --pim_ifp
->pim
->igmp_group_count
;
787 void igmp_group_delete(struct igmp_group
*group
)
789 struct listnode
*src_node
;
790 struct listnode
*src_nextnode
;
791 struct igmp_source
*src
;
793 if (PIM_DEBUG_IGMP_TRACE
) {
794 char group_str
[INET_ADDRSTRLEN
];
795 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
797 zlog_debug("Deleting IGMP group %s from socket %d interface %s",
798 group_str
, group
->group_igmp_sock
->fd
,
799 group
->group_igmp_sock
->interface
->name
);
802 for (ALL_LIST_ELEMENTS(group
->group_source_list
, src_node
, src_nextnode
,
804 igmp_source_delete(src
);
807 if (group
->t_group_query_retransmit_timer
) {
808 THREAD_OFF(group
->t_group_query_retransmit_timer
);
811 group_timer_off(group
);
812 igmp_group_count_decr(group
->group_igmp_sock
);
813 listnode_delete(group
->group_igmp_sock
->igmp_group_list
, group
);
814 hash_release(group
->group_igmp_sock
->igmp_group_hash
, group
);
816 igmp_group_free(group
);
819 void igmp_group_delete_empty_include(struct igmp_group
*group
)
821 zassert(!group
->group_filtermode_isexcl
);
822 zassert(!listcount(group
->group_source_list
));
824 igmp_group_delete(group
);
827 void igmp_sock_free(struct igmp_sock
*igmp
)
829 zassert(!igmp
->t_igmp_read
);
830 zassert(!igmp
->t_igmp_query_timer
);
831 zassert(!igmp
->t_other_querier_timer
);
832 zassert(igmp
->igmp_group_list
);
833 zassert(!listcount(igmp
->igmp_group_list
));
835 list_delete(&igmp
->igmp_group_list
);
836 hash_free(igmp
->igmp_group_hash
);
838 XFREE(MTYPE_PIM_IGMP_SOCKET
, igmp
);
841 void igmp_sock_delete(struct igmp_sock
*igmp
)
843 struct pim_interface
*pim_ifp
;
844 struct listnode
*grp_node
;
845 struct listnode
*grp_nextnode
;
846 struct igmp_group
*grp
;
848 for (ALL_LIST_ELEMENTS(igmp
->igmp_group_list
, grp_node
, grp_nextnode
,
850 igmp_group_delete(grp
);
855 pim_ifp
= igmp
->interface
->info
;
857 listnode_delete(pim_ifp
->igmp_socket_list
, igmp
);
859 igmp_sock_free(igmp
);
862 void igmp_sock_delete_all(struct interface
*ifp
)
864 struct pim_interface
*pim_ifp
;
865 struct listnode
*igmp_node
, *igmp_nextnode
;
866 struct igmp_sock
*igmp
;
870 for (ALL_LIST_ELEMENTS(pim_ifp
->igmp_socket_list
, igmp_node
,
871 igmp_nextnode
, igmp
)) {
872 igmp_sock_delete(igmp
);
876 static unsigned int igmp_group_hash_key(const void *arg
)
878 const struct igmp_group
*group
= arg
;
880 return jhash_1word(group
->group_addr
.s_addr
, 0);
883 static bool igmp_group_hash_equal(const void *arg1
, const void *arg2
)
885 const struct igmp_group
*g1
= (const struct igmp_group
*)arg1
;
886 const struct igmp_group
*g2
= (const struct igmp_group
*)arg2
;
888 if (g1
->group_addr
.s_addr
== g2
->group_addr
.s_addr
)
894 static struct igmp_sock
*igmp_sock_new(int fd
, struct in_addr ifaddr
,
895 struct interface
*ifp
, int mtrace_only
)
897 struct pim_interface
*pim_ifp
;
898 struct igmp_sock
*igmp
;
903 if (PIM_DEBUG_IGMP_TRACE
) {
905 "Creating IGMP socket fd=%d for address %s on interface %s",
906 fd
, inet_ntoa(ifaddr
), ifp
->name
);
909 igmp
= XCALLOC(MTYPE_PIM_IGMP_SOCKET
, sizeof(*igmp
));
911 igmp
->igmp_group_list
= list_new();
912 igmp
->igmp_group_list
->del
= (void (*)(void *))igmp_group_free
;
914 snprintf(hash_name
, sizeof(hash_name
), "IGMP %s hash", ifp
->name
);
915 igmp
->igmp_group_hash
= hash_create(igmp_group_hash_key
,
916 igmp_group_hash_equal
, hash_name
);
919 igmp
->interface
= ifp
;
920 igmp
->ifaddr
= ifaddr
;
921 igmp
->t_igmp_read
= NULL
;
922 igmp
->t_igmp_query_timer
= NULL
;
923 igmp
->t_other_querier_timer
= NULL
; /* no other querier present */
924 igmp
->querier_robustness_variable
=
925 pim_ifp
->igmp_default_robustness_variable
;
926 igmp
->sock_creation
= pim_time_monotonic_sec();
928 igmp_stats_init(&igmp
->rx_stats
);
931 igmp
->mtrace_only
= mtrace_only
;
935 igmp
->mtrace_only
= false;
938 igmp_startup_mode_on() will reset QQI:
940 igmp->querier_query_interval = pim_ifp->igmp_default_query_interval;
942 igmp_startup_mode_on(igmp
);
943 pim_igmp_general_query_on(igmp
);
948 static void igmp_read_on(struct igmp_sock
*igmp
);
950 static int pim_igmp_read(struct thread
*t
)
953 struct igmp_sock
*igmp
= (struct igmp_sock
*)THREAD_ARG(t
);
954 struct sockaddr_in from
;
955 struct sockaddr_in to
;
956 socklen_t fromlen
= sizeof(from
);
957 socklen_t tolen
= sizeof(to
);
958 ifindex_t ifindex
= -1;
962 len
= pim_socket_recvfromto(igmp
->fd
, buf
, sizeof(buf
), &from
,
963 &fromlen
, &to
, &tolen
, &ifindex
);
967 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
979 static void igmp_read_on(struct igmp_sock
*igmp
)
982 if (PIM_DEBUG_IGMP_TRACE_DETAIL
) {
983 zlog_debug("Scheduling READ event on IGMP socket fd=%d",
986 igmp
->t_igmp_read
= NULL
;
987 thread_add_read(router
->master
, pim_igmp_read
, igmp
, igmp
->fd
,
991 struct igmp_sock
*pim_igmp_sock_add(struct list
*igmp_sock_list
,
992 struct in_addr ifaddr
,
993 struct interface
*ifp
,
996 struct pim_interface
*pim_ifp
;
997 struct igmp_sock
*igmp
;
998 struct sockaddr_in sin
;
1001 pim_ifp
= ifp
->info
;
1003 fd
= igmp_sock_open(ifaddr
, ifp
, pim_ifp
->options
);
1005 zlog_warn("Could not open IGMP socket for %s on %s",
1006 inet_ntoa(ifaddr
), ifp
->name
);
1010 sin
.sin_family
= AF_INET
;
1011 sin
.sin_addr
= ifaddr
;
1013 if (bind(fd
, (struct sockaddr
*) &sin
, sizeof(sin
)) != 0) {
1014 zlog_warn("Could not bind IGMP socket for %s on %s",
1015 inet_ntoa(ifaddr
), ifp
->name
);
1019 igmp
= igmp_sock_new(fd
, ifaddr
, ifp
, mtrace_only
);
1023 listnode_add(igmp_sock_list
, igmp
);
1025 #ifdef IGMP_SOCK_DUMP
1026 igmp_sock_dump(igmp_sock_array
);
1033 RFC 3376: 6.5. Switching Router Filter-Modes
1035 When a router's filter-mode for a group is EXCLUDE and the group
1036 timer expires, the router filter-mode for the group transitions to
1039 A router uses source records with running source timers as its state
1040 for the switch to a filter-mode of INCLUDE. If there are any source
1041 records with source timers greater than zero (i.e., requested to be
1042 forwarded), a router switches to filter-mode of INCLUDE using those
1043 source records. Source records whose timers are zero (from the
1044 previous EXCLUDE mode) are deleted.
1046 static int igmp_group_timer(struct thread
*t
)
1048 struct igmp_group
*group
;
1050 group
= THREAD_ARG(t
);
1052 if (PIM_DEBUG_IGMP_TRACE
) {
1053 char group_str
[INET_ADDRSTRLEN
];
1054 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1056 zlog_debug("%s: Timer for group %s on interface %s", __func__
,
1057 group_str
, group
->group_igmp_sock
->interface
->name
);
1060 zassert(group
->group_filtermode_isexcl
);
1062 group
->group_filtermode_isexcl
= 0;
1064 /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
1065 igmp_anysource_forward_stop(group
);
1067 igmp_source_delete_expired(group
->group_source_list
);
1069 zassert(!group
->group_filtermode_isexcl
);
1072 RFC 3376: 6.2.2. Definition of Group Timers
1074 If there are no more source records for the group, delete group
1077 if (listcount(group
->group_source_list
) < 1) {
1078 igmp_group_delete_empty_include(group
);
1084 static void group_timer_off(struct igmp_group
*group
)
1086 if (!group
->t_group_timer
)
1089 if (PIM_DEBUG_IGMP_TRACE
) {
1090 char group_str
[INET_ADDRSTRLEN
];
1091 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1093 zlog_debug("Cancelling TIMER event for group %s on %s",
1094 group_str
, group
->group_igmp_sock
->interface
->name
);
1096 THREAD_OFF(group
->t_group_timer
);
1099 void igmp_group_timer_on(struct igmp_group
*group
, long interval_msec
,
1102 group_timer_off(group
);
1104 if (PIM_DEBUG_IGMP_EVENTS
) {
1105 char group_str
[INET_ADDRSTRLEN
];
1106 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1109 "Scheduling %ld.%03ld sec TIMER event for group %s on %s",
1110 interval_msec
/ 1000, interval_msec
% 1000, group_str
,
1115 RFC 3376: 6.2.2. Definition of Group Timers
1117 The group timer is only used when a group is in EXCLUDE mode and
1118 it represents the time for the *filter-mode* of the group to
1119 expire and switch to INCLUDE mode.
1121 zassert(group
->group_filtermode_isexcl
);
1123 thread_add_timer_msec(router
->master
, igmp_group_timer
, group
,
1124 interval_msec
, &group
->t_group_timer
);
1127 struct igmp_group
*find_group_by_addr(struct igmp_sock
*igmp
,
1128 struct in_addr group_addr
)
1130 struct igmp_group lookup
;
1132 lookup
.group_addr
.s_addr
= group_addr
.s_addr
;
1134 return hash_lookup(igmp
->igmp_group_hash
, &lookup
);
1137 struct igmp_group
*igmp_add_group_by_addr(struct igmp_sock
*igmp
,
1138 struct in_addr group_addr
)
1140 struct igmp_group
*group
;
1142 group
= find_group_by_addr(igmp
, group_addr
);
1147 if (!pim_is_group_224_4(group_addr
)) {
1148 zlog_warn("%s: Group Specified is not part of 224.0.0.0/4",
1153 if (pim_is_group_224_0_0_0_24(group_addr
)) {
1154 if (PIM_DEBUG_IGMP_TRACE
)
1156 "%s: Group specified %s is part of 224.0.0.0/24",
1157 __func__
, inet_ntoa(group_addr
));
1161 Non-existant group is created as INCLUDE {empty}:
1163 RFC 3376 - 5.1. Action on Change of Interface State
1165 If no interface state existed for that multicast address before
1166 the change (i.e., the change consisted of creating a new
1167 per-interface record), or if no state exists after the change
1168 (i.e., the change consisted of deleting a per-interface record),
1169 then the "non-existent" state is considered to have a filter mode
1170 of INCLUDE and an empty source list.
1173 group
= XCALLOC(MTYPE_PIM_IGMP_GROUP
, sizeof(*group
));
1175 group
->group_source_list
= list_new();
1176 group
->group_source_list
->del
= (void (*)(void *))igmp_source_free
;
1178 group
->t_group_timer
= NULL
;
1179 group
->t_group_query_retransmit_timer
= NULL
;
1180 group
->group_specific_query_retransmit_count
= 0;
1181 group
->group_addr
= group_addr
;
1182 group
->group_igmp_sock
= igmp
;
1183 group
->last_igmp_v1_report_dsec
= -1;
1184 group
->last_igmp_v2_report_dsec
= -1;
1185 group
->group_creation
= pim_time_monotonic_sec();
1186 group
->igmp_version
= IGMP_DEFAULT_VERSION
;
1188 /* initialize new group as INCLUDE {empty} */
1189 group
->group_filtermode_isexcl
= 0; /* 0=INCLUDE, 1=EXCLUDE */
1191 listnode_add(igmp
->igmp_group_list
, group
);
1192 group
= hash_get(igmp
->igmp_group_hash
, group
, hash_alloc_intern
);
1194 if (PIM_DEBUG_IGMP_TRACE
) {
1195 char group_str
[INET_ADDRSTRLEN
];
1196 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1199 "Creating new IGMP group %s on socket %d interface %s",
1200 group_str
, igmp
->fd
, igmp
->interface
->name
);
1203 igmp_group_count_incr(igmp
);
1206 RFC 3376: 6.2.2. Definition of Group Timers
1208 The group timer is only used when a group is in EXCLUDE mode and
1209 it represents the time for the *filter-mode* of the group to
1210 expire and switch to INCLUDE mode.
1212 zassert(!group
->group_filtermode_isexcl
); /* INCLUDE mode */
1213 zassert(!group
->t_group_timer
); /* group timer == 0 */
1215 /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
1216 igmp_anysource_forward_stop(group
);
1221 void igmp_send_query(int igmp_version
, struct igmp_group
*group
, int fd
,
1222 const char *ifname
, char *query_buf
, int query_buf_size
,
1223 int num_sources
, struct in_addr dst_addr
,
1224 struct in_addr group_addr
,
1225 int query_max_response_time_dsec
, uint8_t s_flag
,
1226 uint8_t querier_robustness_variable
,
1227 uint16_t querier_query_interval
)
1229 if (igmp_version
== 3) {
1230 igmp_v3_send_query(group
, fd
, ifname
, query_buf
, query_buf_size
,
1231 num_sources
, dst_addr
, group_addr
,
1232 query_max_response_time_dsec
, s_flag
,
1233 querier_robustness_variable
,
1234 querier_query_interval
);
1235 } else if (igmp_version
== 2) {
1236 igmp_v2_send_query(group
, fd
, ifname
, query_buf
, dst_addr
,
1237 group_addr
, query_max_response_time_dsec
);
1241 void igmp_send_query_on_intf(struct interface
*ifp
, int igmp_ver
)
1243 struct pim_interface
*pim_ifp
= ifp
->info
;
1244 struct listnode
*sock_node
= NULL
;
1245 struct igmp_sock
*igmp
= NULL
;
1246 struct in_addr dst_addr
;
1247 struct in_addr group_addr
;
1254 query_buf_size
= PIM_IGMP_BUFSIZE_WRITE
;
1256 query_buf_size
= IGMP_V12_MSG_SIZE
;
1258 dst_addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
1259 group_addr
.s_addr
= PIM_NET_INADDR_ANY
;
1261 if (PIM_DEBUG_IGMP_TRACE
)
1262 zlog_debug("Issuing general query on request on %s",
1265 for (ALL_LIST_ELEMENTS_RO(pim_ifp
->igmp_socket_list
, sock_node
, igmp
)) {
1267 char query_buf
[query_buf_size
];
1269 igmp_send_query(igmp_ver
, 0 /* igmp_group */, igmp
->fd
,
1270 igmp
->interface
->name
, query_buf
,
1271 sizeof(query_buf
), 0 /* num_sources */,
1272 dst_addr
, group_addr
,
1273 pim_ifp
->igmp_query_max_response_time_dsec
,
1274 1 /* s_flag: always set for general queries */,
1275 igmp
->querier_robustness_variable
,
1276 igmp
->querier_query_interval
);