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__
, __PRETTY_FUNCTION__
, fd
,
68 inet_ntoa(ifaddr
), PIM_ALL_ROUTERS
, errno
,
69 safe_strerror(errno
));
74 IGMP routers periodically send IGMP general queries to
76 IGMP routers must receive general queries for querier election.
78 if (inet_aton(PIM_ALL_SYSTEMS
, &group
)) {
79 if (!pim_socket_join(fd
, group
, ifaddr
, ifp
->ifindex
))
83 "%s %s: IGMP socket fd=%d interface %s: could not solve %s to group address: errno=%d: %s",
84 __FILE__
, __PRETTY_FUNCTION__
, fd
, inet_ntoa(ifaddr
),
85 PIM_ALL_SYSTEMS
, errno
, safe_strerror(errno
));
88 if (inet_aton(PIM_ALL_IGMP_ROUTERS
, &group
)) {
89 if (!pim_socket_join(fd
, group
, ifaddr
, ifp
->ifindex
)) {
94 "%s %s: IGMP socket fd=%d interface %s: could not solve %s to group address: errno=%d: %s",
95 __FILE__
, __PRETTY_FUNCTION__
, fd
, inet_ntoa(ifaddr
),
96 PIM_ALL_IGMP_ROUTERS
, errno
, safe_strerror(errno
));
100 zlog_ferr(LIB_ERR_SOCKET
,
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 __PRETTY_FUNCTION__
, i
, size
,
122 inet_ntoa(igmp
->ifaddr
), igmp
->fd
);
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", __PRETTY_FUNCTION__
,
173 We are the current querier, then
174 re-start sending general queries.
175 RFC 2236 - sec 7 Other Querier
176 present timer expired (Send General
177 Query, Set Gen. Query. timer)
179 pim_igmp_general_query(t
);
184 void pim_igmp_other_querier_timer_on(struct igmp_sock
*igmp
)
186 long other_querier_present_interval_msec
;
187 struct pim_interface
*pim_ifp
;
190 zassert(igmp
->interface
);
191 zassert(igmp
->interface
->info
);
193 pim_ifp
= igmp
->interface
->info
;
195 if (igmp
->t_other_querier_timer
) {
197 There is other querier present already,
198 then reset the other-querier-present timer.
201 if (PIM_DEBUG_IGMP_TRACE
) {
202 char ifaddr_str
[INET_ADDRSTRLEN
];
203 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
206 "Querier %s resetting TIMER event for Other-Querier-Present",
209 THREAD_OFF(igmp
->t_other_querier_timer
);
212 We are the current querier, then stop sending general queries:
213 igmp->t_igmp_query_timer = NULL;
215 pim_igmp_general_query_off(igmp
);
219 Since this socket is starting the other-querier-present timer,
220 there should not be periodic query timer for this socket.
222 zassert(!igmp
->t_igmp_query_timer
);
225 RFC 3376: 8.5. Other Querier Present Interval
227 The Other Querier Present Interval is the length of time that must
228 pass before a multicast router decides that there is no longer
229 another multicast router which should be the querier. This value
230 MUST be ((the Robustness Variable) times (the Query Interval)) plus
231 (one half of one Query Response Interval).
233 other_querier_present_interval_msec = \
234 igmp->querier_robustness_variable * \
235 1000 * igmp->querier_query_interval + \
236 100 * (pim_ifp->query_max_response_time_dsec >> 1);
238 other_querier_present_interval_msec
= PIM_IGMP_OQPI_MSEC(
239 igmp
->querier_robustness_variable
, igmp
->querier_query_interval
,
240 pim_ifp
->igmp_query_max_response_time_dsec
);
242 if (PIM_DEBUG_IGMP_TRACE
) {
243 char ifaddr_str
[INET_ADDRSTRLEN
];
244 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
247 "Querier %s scheduling %ld.%03ld sec TIMER event for Other-Querier-Present",
248 ifaddr_str
, other_querier_present_interval_msec
/ 1000,
249 other_querier_present_interval_msec
% 1000);
252 thread_add_timer_msec(master
, pim_igmp_other_querier_expire
, igmp
,
253 other_querier_present_interval_msec
,
254 &igmp
->t_other_querier_timer
);
257 void pim_igmp_other_querier_timer_off(struct igmp_sock
*igmp
)
261 if (PIM_DEBUG_IGMP_TRACE
) {
262 if (igmp
->t_other_querier_timer
) {
263 char ifaddr_str
[INET_ADDRSTRLEN
];
264 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
267 "IGMP querier %s fd=%d cancelling other-querier-present TIMER event on %s",
268 ifaddr_str
, igmp
->fd
, igmp
->interface
->name
);
271 THREAD_OFF(igmp
->t_other_querier_timer
);
274 static int igmp_recv_query(struct igmp_sock
*igmp
, int query_version
,
275 int max_resp_code
, struct in_addr from
,
276 const char *from_str
, char *igmp_msg
,
279 struct interface
*ifp
;
280 struct pim_interface
*pim_ifp
;
281 struct in_addr group_addr
;
282 uint16_t recv_checksum
;
285 if (igmp
->mtrace_only
)
288 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
290 ifp
= igmp
->interface
;
293 recv_checksum
= *(uint16_t *)(igmp_msg
+ IGMP_CHECKSUM_OFFSET
);
295 /* for computing checksum */
296 *(uint16_t *)(igmp_msg
+ IGMP_CHECKSUM_OFFSET
) = 0;
298 checksum
= in_cksum(igmp_msg
, igmp_msg_len
);
299 if (checksum
!= recv_checksum
) {
301 "Recv IGMP query v%d from %s on %s: checksum mismatch: received=%x computed=%x",
302 query_version
, from_str
, ifp
->name
, recv_checksum
,
307 /* Collecting IGMP Rx stats */
308 switch (query_version
) {
310 igmp
->rx_stats
.query_v1
++;
313 igmp
->rx_stats
.query_v2
++;
316 igmp
->rx_stats
.query_v3
++;
319 igmp
->rx_stats
.unsupported
++;
323 * RFC 3376 defines some guidelines on operating in backwards
324 * compatibility with older versions of IGMP but there are some gaps in
327 * - once we drop from say version 3 to version 2 we will never go back
328 * to version 3 even if the node that TXed an IGMP v2 query upgrades
331 * - The node with the lowest IP is the querier so we will only know to
332 * drop from v3 to v2 if the node that is the querier is also the one
333 * that is running igmp v2. If a non-querier only supports igmp v2
334 * we will have no way of knowing.
336 * For now we will simplify things and inform the user that they need to
337 * configure all PIM routers to use the same version of IGMP.
339 if (query_version
!= pim_ifp
->igmp_version
) {
341 "Recv IGMP query v%d from %s on %s but we are using v%d, please "
342 "configure all PIM routers on this subnet to use the same "
344 query_version
, from_str
, ifp
->name
,
345 pim_ifp
->igmp_version
);
349 if (PIM_DEBUG_IGMP_PACKETS
) {
350 char group_str
[INET_ADDRSTRLEN
];
351 pim_inet4_dump("<group?>", group_addr
, group_str
,
353 zlog_debug("Recv IGMP query v%d from %s on %s for group %s",
354 query_version
, from_str
, ifp
->name
, group_str
);
358 RFC 3376: 6.6.2. Querier Election
360 When a router receives a query with a lower IP address, it sets
361 the Other-Querier-Present timer to Other Querier Present Interval
362 and ceases to send queries on the network if it was the previously
365 if (ntohl(from
.s_addr
) < ntohl(igmp
->ifaddr
.s_addr
)) {
367 if (PIM_DEBUG_IGMP_TRACE
) {
368 char ifaddr_str
[INET_ADDRSTRLEN
];
369 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
372 "%s: local address %s (%u) lost querier election to %s (%u)",
373 ifp
->name
, ifaddr_str
,
374 ntohl(igmp
->ifaddr
.s_addr
), from_str
,
378 pim_igmp_other_querier_timer_on(igmp
);
381 /* IGMP version 3 is the only one where we process the RXed query */
382 if (query_version
== 3) {
383 igmp_v3_recv_query(igmp
, from_str
, igmp_msg
);
389 static void on_trace(const char *label
, struct interface
*ifp
,
392 if (PIM_DEBUG_IGMP_TRACE
) {
393 char from_str
[INET_ADDRSTRLEN
];
394 pim_inet4_dump("<from?>", from
, from_str
, sizeof(from_str
));
395 zlog_debug("%s: from %s on %s", label
, from_str
, ifp
->name
);
399 static int igmp_v1_recv_report(struct igmp_sock
*igmp
, struct in_addr from
,
400 const char *from_str
, char *igmp_msg
,
403 struct interface
*ifp
= igmp
->interface
;
404 struct igmp_group
*group
;
405 struct in_addr group_addr
;
407 on_trace(__PRETTY_FUNCTION__
, igmp
->interface
, from
);
409 if (igmp
->mtrace_only
)
412 if (igmp_msg_len
!= IGMP_V12_MSG_SIZE
) {
414 "Recv IGMP report v1 from %s on %s: size=%d other than correct=%d",
415 from_str
, ifp
->name
, igmp_msg_len
, IGMP_V12_MSG_SIZE
);
419 /* Collecting IGMP Rx stats */
420 igmp
->rx_stats
.report_v1
++;
422 if (PIM_DEBUG_IGMP_TRACE
) {
423 zlog_warn("%s %s: FIXME WRITEME", __FILE__
,
424 __PRETTY_FUNCTION__
);
427 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
429 if (pim_is_group_filtered(ifp
->info
, &group_addr
))
432 /* non-existant group is created as INCLUDE {empty} */
433 group
= igmp_add_group_by_addr(igmp
, group_addr
);
438 group
->last_igmp_v1_report_dsec
= pim_time_monotonic_dsec();
443 int pim_igmp_packet(struct igmp_sock
*igmp
, char *buf
, size_t len
)
446 size_t ip_hlen
; /* ip header length in bytes */
450 char from_str
[INET_ADDRSTRLEN
];
451 char to_str
[INET_ADDRSTRLEN
];
453 if (len
< sizeof(*ip_hdr
)) {
454 zlog_warn("IGMP packet size=%zu shorter than minimum=%zu", len
,
459 ip_hdr
= (struct ip
*)buf
;
461 pim_inet4_dump("<src?>", ip_hdr
->ip_src
, from_str
, sizeof(from_str
));
462 pim_inet4_dump("<dst?>", ip_hdr
->ip_dst
, to_str
, sizeof(to_str
));
464 ip_hlen
= ip_hdr
->ip_hl
<< 2; /* ip_hl gives length in 4-byte words */
466 if (PIM_DEBUG_IGMP_PACKETS
) {
468 "Recv IP packet from %s to %s on %s: size=%zu ip_header_size=%zu ip_proto=%d",
469 from_str
, to_str
, igmp
->interface
->name
, len
, ip_hlen
,
473 igmp_msg
= buf
+ ip_hlen
;
474 msg_type
= *igmp_msg
;
475 igmp_msg_len
= len
- ip_hlen
;
477 if (PIM_DEBUG_IGMP_PACKETS
) {
479 "Recv IGMP packet from %s to %s on %s: ttl=%d msg_type=%d msg_size=%d",
480 from_str
, to_str
, igmp
->interface
->name
, ip_hdr
->ip_ttl
,
481 msg_type
, igmp_msg_len
);
484 if (igmp_msg_len
< PIM_IGMP_MIN_LEN
) {
485 zlog_warn("IGMP message size=%d shorter than minimum=%d",
486 igmp_msg_len
, PIM_IGMP_MIN_LEN
);
491 case PIM_IGMP_MEMBERSHIP_QUERY
: {
492 int max_resp_code
= igmp_msg
[1];
496 RFC 3376: 7.1. Query Version Distinctions
497 IGMPv1 Query: length = 8 octets AND Max Resp Code field is
499 IGMPv2 Query: length = 8 octets AND Max Resp Code field is
501 IGMPv3 Query: length >= 12 octets
504 if (igmp_msg_len
== 8) {
505 query_version
= max_resp_code
? 2 : 1;
506 } else if (igmp_msg_len
>= 12) {
509 zlog_warn("Unknown IGMP query version");
513 return igmp_recv_query(igmp
, query_version
, max_resp_code
,
514 ip_hdr
->ip_src
, from_str
, igmp_msg
,
518 case PIM_IGMP_V3_MEMBERSHIP_REPORT
:
519 return igmp_v3_recv_report(igmp
, ip_hdr
->ip_src
, from_str
,
520 igmp_msg
, igmp_msg_len
);
522 case PIM_IGMP_V2_MEMBERSHIP_REPORT
:
523 return igmp_v2_recv_report(igmp
, ip_hdr
->ip_src
, from_str
,
524 igmp_msg
, igmp_msg_len
);
526 case PIM_IGMP_V1_MEMBERSHIP_REPORT
:
527 return igmp_v1_recv_report(igmp
, ip_hdr
->ip_src
, from_str
,
528 igmp_msg
, igmp_msg_len
);
530 case PIM_IGMP_V2_LEAVE_GROUP
:
531 return igmp_v2_recv_leave(igmp
, ip_hdr
->ip_src
, from_str
,
532 igmp_msg
, igmp_msg_len
);
534 case PIM_IGMP_MTRACE_RESPONSE
:
535 return igmp_mtrace_recv_response(igmp
, ip_hdr
, ip_hdr
->ip_src
,
538 case PIM_IGMP_MTRACE_QUERY_REQUEST
:
539 return igmp_mtrace_recv_qry_req(igmp
, ip_hdr
, ip_hdr
->ip_src
,
544 zlog_warn("Ignoring unsupported IGMP message type: %d", msg_type
);
546 /* Collecting IGMP Rx stats */
547 igmp
->rx_stats
.unsupported
++;
552 void pim_igmp_general_query_on(struct igmp_sock
*igmp
)
554 struct pim_interface
*pim_ifp
;
559 Since this socket is starting as querier,
560 there should not exist a timer for other-querier-present.
562 zassert(!igmp
->t_other_querier_timer
);
563 pim_ifp
= igmp
->interface
->info
;
567 RFC 3376: 8.6. Startup Query Interval
569 The Startup Query Interval is the interval between General Queries
570 sent by a Querier on startup. Default: 1/4 the Query Interval.
571 The first one should be sent out immediately instead of 125/4
574 startup_mode
= igmp
->startup_query_count
> 0;
577 * If this is the first time we are sending a query on a
578 * newly configured igmp interface send it out in 1 second
579 * just to give the entire world a tiny bit of time to settle
580 * else the query interval is:
581 * query_interval = pim_ifp->igmp_default_query_interval >> 2;
583 if (igmp
->startup_query_count
584 == igmp
->querier_robustness_variable
)
587 query_interval
= PIM_IGMP_SQI(
588 pim_ifp
->igmp_default_query_interval
);
590 --igmp
->startup_query_count
;
592 query_interval
= igmp
->querier_query_interval
;
595 if (PIM_DEBUG_IGMP_TRACE
) {
596 char ifaddr_str
[INET_ADDRSTRLEN
];
597 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
600 "Querier %s scheduling %d-second (%s) TIMER event for IGMP query on fd=%d",
601 ifaddr_str
, query_interval
,
602 startup_mode
? "startup" : "non-startup", igmp
->fd
);
604 igmp
->t_igmp_query_timer
= NULL
;
605 thread_add_timer(master
, pim_igmp_general_query
, igmp
, query_interval
,
606 &igmp
->t_igmp_query_timer
);
609 void pim_igmp_general_query_off(struct igmp_sock
*igmp
)
613 if (PIM_DEBUG_IGMP_TRACE
) {
614 if (igmp
->t_igmp_query_timer
) {
615 char ifaddr_str
[INET_ADDRSTRLEN
];
616 pim_inet4_dump("<ifaddr?>", igmp
->ifaddr
, ifaddr_str
,
619 "IGMP querier %s fd=%d cancelling query TIMER event on %s",
620 ifaddr_str
, igmp
->fd
, igmp
->interface
->name
);
623 THREAD_OFF(igmp
->t_igmp_query_timer
);
626 /* Issue IGMP general query */
627 static int pim_igmp_general_query(struct thread
*t
)
629 struct igmp_sock
*igmp
;
630 struct in_addr dst_addr
;
631 struct in_addr group_addr
;
632 struct pim_interface
*pim_ifp
;
635 igmp
= THREAD_ARG(t
);
637 zassert(igmp
->interface
);
638 zassert(igmp
->interface
->info
);
640 pim_ifp
= igmp
->interface
->info
;
642 if (pim_ifp
->igmp_version
== 3) {
643 query_buf_size
= PIM_IGMP_BUFSIZE_WRITE
;
645 query_buf_size
= IGMP_V12_MSG_SIZE
;
648 char query_buf
[query_buf_size
];
651 RFC3376: 4.1.12. IP Destination Addresses for Queries
653 In IGMPv3, General Queries are sent with an IP destination address
654 of 224.0.0.1, the all-systems multicast address. Group-Specific
655 and Group-and-Source-Specific Queries are sent with an IP
656 destination address equal to the multicast address of interest.
659 dst_addr
.s_addr
= htonl(INADDR_ALLHOSTS_GROUP
);
660 group_addr
.s_addr
= PIM_NET_INADDR_ANY
;
662 if (PIM_DEBUG_IGMP_TRACE
) {
663 char querier_str
[INET_ADDRSTRLEN
];
664 char dst_str
[INET_ADDRSTRLEN
];
665 pim_inet4_dump("<querier?>", igmp
->ifaddr
, querier_str
,
666 sizeof(querier_str
));
667 pim_inet4_dump("<dst?>", dst_addr
, dst_str
, sizeof(dst_str
));
668 zlog_debug("Querier %s issuing IGMP general query to %s on %s",
669 querier_str
, dst_str
, igmp
->interface
->name
);
672 igmp_send_query(pim_ifp
->igmp_version
, 0 /* igmp_group */, igmp
->fd
,
673 igmp
->interface
->name
, query_buf
, sizeof(query_buf
),
674 0 /* num_sources */, dst_addr
, group_addr
,
675 pim_ifp
->igmp_query_max_response_time_dsec
,
676 1 /* s_flag: always set for general queries */,
677 igmp
->querier_robustness_variable
,
678 igmp
->querier_query_interval
);
680 pim_igmp_general_query_on(igmp
);
685 static void sock_close(struct igmp_sock
*igmp
)
687 pim_igmp_other_querier_timer_off(igmp
);
688 pim_igmp_general_query_off(igmp
);
690 if (PIM_DEBUG_IGMP_TRACE_DETAIL
) {
691 if (igmp
->t_igmp_read
) {
693 "Cancelling READ event on IGMP socket %s fd=%d on interface %s",
694 inet_ntoa(igmp
->ifaddr
), igmp
->fd
,
695 igmp
->interface
->name
);
698 THREAD_OFF(igmp
->t_igmp_read
);
700 if (close(igmp
->fd
)) {
703 "Failure closing IGMP socket %s fd=%d on interface %s: errno=%d: %s",
704 inet_ntoa(igmp
->ifaddr
), igmp
->fd
,
705 igmp
->interface
->name
, errno
, safe_strerror(errno
));
708 if (PIM_DEBUG_IGMP_TRACE_DETAIL
) {
709 zlog_debug("Deleted IGMP socket %s fd=%d on interface %s",
710 inet_ntoa(igmp
->ifaddr
), igmp
->fd
,
711 igmp
->interface
->name
);
715 void igmp_startup_mode_on(struct igmp_sock
*igmp
)
717 struct pim_interface
*pim_ifp
;
719 pim_ifp
= igmp
->interface
->info
;
722 RFC 3376: 8.7. Startup Query Count
724 The Startup Query Count is the number of Queries sent out on
725 startup, separated by the Startup Query Interval. Default: the
728 igmp
->startup_query_count
= igmp
->querier_robustness_variable
;
731 Since we're (re)starting, reset QQI to default Query Interval
733 igmp
->querier_query_interval
= pim_ifp
->igmp_default_query_interval
;
736 static void igmp_group_free(struct igmp_group
*group
)
738 list_delete_and_null(&group
->group_source_list
);
740 XFREE(MTYPE_PIM_IGMP_GROUP
, group
);
743 static void igmp_group_delete(struct igmp_group
*group
)
745 struct listnode
*src_node
;
746 struct listnode
*src_nextnode
;
747 struct igmp_source
*src
;
749 if (PIM_DEBUG_IGMP_TRACE
) {
750 char group_str
[INET_ADDRSTRLEN
];
751 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
753 zlog_debug("Deleting IGMP group %s from socket %d interface %s",
754 group_str
, group
->group_igmp_sock
->fd
,
755 group
->group_igmp_sock
->interface
->name
);
758 for (ALL_LIST_ELEMENTS(group
->group_source_list
, src_node
, src_nextnode
,
760 igmp_source_delete(src
);
763 if (group
->t_group_query_retransmit_timer
) {
764 THREAD_OFF(group
->t_group_query_retransmit_timer
);
767 group_timer_off(group
);
768 listnode_delete(group
->group_igmp_sock
->igmp_group_list
, group
);
769 hash_release(group
->group_igmp_sock
->igmp_group_hash
, group
);
771 igmp_group_free(group
);
774 void igmp_group_delete_empty_include(struct igmp_group
*group
)
776 zassert(!group
->group_filtermode_isexcl
);
777 zassert(!listcount(group
->group_source_list
));
779 igmp_group_delete(group
);
782 void igmp_sock_free(struct igmp_sock
*igmp
)
784 zassert(!igmp
->t_igmp_read
);
785 zassert(!igmp
->t_igmp_query_timer
);
786 zassert(!igmp
->t_other_querier_timer
);
787 zassert(igmp
->igmp_group_list
);
788 zassert(!listcount(igmp
->igmp_group_list
));
790 list_delete_and_null(&igmp
->igmp_group_list
);
791 hash_free(igmp
->igmp_group_hash
);
793 XFREE(MTYPE_PIM_IGMP_SOCKET
, igmp
);
796 void igmp_sock_delete(struct igmp_sock
*igmp
)
798 struct pim_interface
*pim_ifp
;
799 struct listnode
*grp_node
;
800 struct listnode
*grp_nextnode
;
801 struct igmp_group
*grp
;
803 for (ALL_LIST_ELEMENTS(igmp
->igmp_group_list
, grp_node
, grp_nextnode
,
805 igmp_group_delete(grp
);
810 pim_ifp
= igmp
->interface
->info
;
812 listnode_delete(pim_ifp
->igmp_socket_list
, igmp
);
814 igmp_sock_free(igmp
);
817 void igmp_sock_delete_all(struct interface
*ifp
)
819 struct pim_interface
*pim_ifp
;
820 struct listnode
*igmp_node
, *igmp_nextnode
;
821 struct igmp_sock
*igmp
;
825 for (ALL_LIST_ELEMENTS(pim_ifp
->igmp_socket_list
, igmp_node
,
826 igmp_nextnode
, igmp
)) {
827 igmp_sock_delete(igmp
);
831 static unsigned int igmp_group_hash_key(void *arg
)
833 struct igmp_group
*group
= (struct igmp_group
*)arg
;
835 return jhash_1word(group
->group_addr
.s_addr
, 0);
838 static int igmp_group_hash_equal(const void *arg1
, const void *arg2
)
840 const struct igmp_group
*g1
= (const struct igmp_group
*)arg1
;
841 const struct igmp_group
*g2
= (const struct igmp_group
*)arg2
;
843 if (g1
->group_addr
.s_addr
== g2
->group_addr
.s_addr
)
849 static struct igmp_sock
*igmp_sock_new(int fd
, struct in_addr ifaddr
,
850 struct interface
*ifp
, int mtrace_only
)
852 struct pim_interface
*pim_ifp
;
853 struct igmp_sock
*igmp
;
858 if (PIM_DEBUG_IGMP_TRACE
) {
860 "Creating IGMP socket fd=%d for address %s on interface %s",
861 fd
, inet_ntoa(ifaddr
), ifp
->name
);
864 igmp
= XCALLOC(MTYPE_PIM_IGMP_SOCKET
, sizeof(*igmp
));
866 igmp
->igmp_group_list
= list_new();
867 igmp
->igmp_group_list
->del
= (void (*)(void *))igmp_group_free
;
869 snprintf(hash_name
, 64, "IGMP %s hash", ifp
->name
);
870 igmp
->igmp_group_hash
= hash_create(igmp_group_hash_key
,
871 igmp_group_hash_equal
, hash_name
);
874 igmp
->interface
= ifp
;
875 igmp
->ifaddr
= ifaddr
;
876 igmp
->t_igmp_read
= NULL
;
877 igmp
->t_igmp_query_timer
= NULL
;
878 igmp
->t_other_querier_timer
= NULL
; /* no other querier present */
879 igmp
->querier_robustness_variable
=
880 pim_ifp
->igmp_default_robustness_variable
;
881 igmp
->sock_creation
= pim_time_monotonic_sec();
883 igmp_stats_init(&igmp
->rx_stats
);
886 igmp
->mtrace_only
= mtrace_only
;
890 igmp
->mtrace_only
= false;
893 igmp_startup_mode_on() will reset QQI:
895 igmp->querier_query_interval = pim_ifp->igmp_default_query_interval;
897 igmp_startup_mode_on(igmp
);
898 pim_igmp_general_query_on(igmp
);
903 static void igmp_read_on(struct igmp_sock
*igmp
);
905 static int pim_igmp_read(struct thread
*t
)
908 struct igmp_sock
*igmp
= (struct igmp_sock
*)THREAD_ARG(t
);
909 struct sockaddr_in from
;
910 struct sockaddr_in to
;
911 socklen_t fromlen
= sizeof(from
);
912 socklen_t tolen
= sizeof(to
);
913 ifindex_t ifindex
= -1;
917 len
= pim_socket_recvfromto(igmp
->fd
, buf
, sizeof(buf
), &from
,
918 &fromlen
, &to
, &tolen
, &ifindex
);
922 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
934 static void igmp_read_on(struct igmp_sock
*igmp
)
937 if (PIM_DEBUG_IGMP_TRACE_DETAIL
) {
938 zlog_debug("Scheduling READ event on IGMP socket fd=%d",
941 igmp
->t_igmp_read
= NULL
;
942 thread_add_read(master
, pim_igmp_read
, igmp
, igmp
->fd
,
946 struct igmp_sock
*pim_igmp_sock_add(struct list
*igmp_sock_list
,
947 struct in_addr ifaddr
,
948 struct interface
*ifp
,
951 struct pim_interface
*pim_ifp
;
952 struct igmp_sock
*igmp
;
957 fd
= igmp_sock_open(ifaddr
, ifp
, pim_ifp
->options
);
959 zlog_warn("Could not open IGMP socket for %s on %s",
960 inet_ntoa(ifaddr
), ifp
->name
);
964 igmp
= igmp_sock_new(fd
, ifaddr
, ifp
, mtrace_only
);
968 listnode_add(igmp_sock_list
, igmp
);
970 #ifdef IGMP_SOCK_DUMP
971 igmp_sock_dump(igmp_sock_array
);
978 RFC 3376: 6.5. Switching Router Filter-Modes
980 When a router's filter-mode for a group is EXCLUDE and the group
981 timer expires, the router filter-mode for the group transitions to
984 A router uses source records with running source timers as its state
985 for the switch to a filter-mode of INCLUDE. If there are any source
986 records with source timers greater than zero (i.e., requested to be
987 forwarded), a router switches to filter-mode of INCLUDE using those
988 source records. Source records whose timers are zero (from the
989 previous EXCLUDE mode) are deleted.
991 static int igmp_group_timer(struct thread
*t
)
993 struct igmp_group
*group
;
995 group
= THREAD_ARG(t
);
997 if (PIM_DEBUG_IGMP_TRACE
) {
998 char group_str
[INET_ADDRSTRLEN
];
999 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1001 zlog_debug("%s: Timer for group %s on interface %s",
1002 __PRETTY_FUNCTION__
, group_str
,
1003 group
->group_igmp_sock
->interface
->name
);
1006 zassert(group
->group_filtermode_isexcl
);
1008 group
->group_filtermode_isexcl
= 0;
1010 /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
1011 igmp_anysource_forward_stop(group
);
1013 igmp_source_delete_expired(group
->group_source_list
);
1015 zassert(!group
->group_filtermode_isexcl
);
1018 RFC 3376: 6.2.2. Definition of Group Timers
1020 If there are no more source records for the group, delete group
1023 if (listcount(group
->group_source_list
) < 1) {
1024 igmp_group_delete_empty_include(group
);
1030 static void group_timer_off(struct igmp_group
*group
)
1032 if (!group
->t_group_timer
)
1035 if (PIM_DEBUG_IGMP_TRACE
) {
1036 char group_str
[INET_ADDRSTRLEN
];
1037 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1039 zlog_debug("Cancelling TIMER event for group %s on %s",
1040 group_str
, group
->group_igmp_sock
->interface
->name
);
1042 THREAD_OFF(group
->t_group_timer
);
1045 void igmp_group_timer_on(struct igmp_group
*group
, long interval_msec
,
1048 group_timer_off(group
);
1050 if (PIM_DEBUG_IGMP_EVENTS
) {
1051 char group_str
[INET_ADDRSTRLEN
];
1052 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1055 "Scheduling %ld.%03ld sec TIMER event for group %s on %s",
1056 interval_msec
/ 1000, interval_msec
% 1000, group_str
,
1061 RFC 3376: 6.2.2. Definition of Group Timers
1063 The group timer is only used when a group is in EXCLUDE mode and
1064 it represents the time for the *filter-mode* of the group to
1065 expire and switch to INCLUDE mode.
1067 zassert(group
->group_filtermode_isexcl
);
1069 thread_add_timer_msec(master
, igmp_group_timer
, group
, interval_msec
,
1070 &group
->t_group_timer
);
1073 struct igmp_group
*find_group_by_addr(struct igmp_sock
*igmp
,
1074 struct in_addr group_addr
)
1076 struct igmp_group lookup
;
1078 lookup
.group_addr
.s_addr
= group_addr
.s_addr
;
1080 return hash_lookup(igmp
->igmp_group_hash
, &lookup
);
1083 struct igmp_group
*igmp_add_group_by_addr(struct igmp_sock
*igmp
,
1084 struct in_addr group_addr
)
1086 struct igmp_group
*group
;
1088 group
= find_group_by_addr(igmp
, group_addr
);
1093 if (!pim_is_group_224_4(group_addr
)) {
1094 zlog_warn("%s: Group Specified is not part of 224.0.0.0/4",
1095 __PRETTY_FUNCTION__
);
1099 if (pim_is_group_224_0_0_0_24(group_addr
)) {
1100 zlog_warn("%s: Group specified is part of 224.0.0.0/24",
1101 __PRETTY_FUNCTION__
);
1105 Non-existant group is created as INCLUDE {empty}:
1107 RFC 3376 - 5.1. Action on Change of Interface State
1109 If no interface state existed for that multicast address before
1110 the change (i.e., the change consisted of creating a new
1111 per-interface record), or if no state exists after the change
1112 (i.e., the change consisted of deleting a per-interface record),
1113 then the "non-existent" state is considered to have a filter mode
1114 of INCLUDE and an empty source list.
1117 group
= XCALLOC(MTYPE_PIM_IGMP_GROUP
, sizeof(*group
));
1119 group
->group_source_list
= list_new();
1120 group
->group_source_list
->del
= (void (*)(void *))igmp_source_free
;
1122 group
->t_group_timer
= NULL
;
1123 group
->t_group_query_retransmit_timer
= NULL
;
1124 group
->group_specific_query_retransmit_count
= 0;
1125 group
->group_addr
= group_addr
;
1126 group
->group_igmp_sock
= igmp
;
1127 group
->last_igmp_v1_report_dsec
= -1;
1128 group
->last_igmp_v2_report_dsec
= -1;
1129 group
->group_creation
= pim_time_monotonic_sec();
1130 group
->igmp_version
= IGMP_DEFAULT_VERSION
;
1132 /* initialize new group as INCLUDE {empty} */
1133 group
->group_filtermode_isexcl
= 0; /* 0=INCLUDE, 1=EXCLUDE */
1135 listnode_add(igmp
->igmp_group_list
, group
);
1136 group
= hash_get(igmp
->igmp_group_hash
, group
, hash_alloc_intern
);
1138 if (PIM_DEBUG_IGMP_TRACE
) {
1139 char group_str
[INET_ADDRSTRLEN
];
1140 pim_inet4_dump("<group?>", group
->group_addr
, group_str
,
1143 "Creating new IGMP group %s on socket %d interface %s",
1144 group_str
, igmp
->fd
, igmp
->interface
->name
);
1148 RFC 3376: 6.2.2. Definition of Group Timers
1150 The group timer is only used when a group is in EXCLUDE mode and
1151 it represents the time for the *filter-mode* of the group to
1152 expire and switch to INCLUDE mode.
1154 zassert(!group
->group_filtermode_isexcl
); /* INCLUDE mode */
1155 zassert(!group
->t_group_timer
); /* group timer == 0 */
1157 /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
1158 igmp_anysource_forward_stop(group
);
1163 void igmp_send_query(int igmp_version
, struct igmp_group
*group
, int fd
,
1164 const char *ifname
, char *query_buf
, int query_buf_size
,
1165 int num_sources
, struct in_addr dst_addr
,
1166 struct in_addr group_addr
,
1167 int query_max_response_time_dsec
, uint8_t s_flag
,
1168 uint8_t querier_robustness_variable
,
1169 uint16_t querier_query_interval
)
1171 if (igmp_version
== 3) {
1172 igmp_v3_send_query(group
, fd
, ifname
, query_buf
, query_buf_size
,
1173 num_sources
, dst_addr
, group_addr
,
1174 query_max_response_time_dsec
, s_flag
,
1175 querier_robustness_variable
,
1176 querier_query_interval
);
1177 } else if (igmp_version
== 2) {
1178 igmp_v2_send_query(group
, fd
, ifname
, query_buf
, dst_addr
,
1179 group_addr
, query_max_response_time_dsec
);