3 * Copyright (C) 2016 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "pim_igmpv2.h"
26 #include "pim_igmpv3.h"
32 static void on_trace(const char *label
, struct interface
*ifp
,
35 if (PIM_DEBUG_IGMP_TRACE
) {
36 char from_str
[INET_ADDRSTRLEN
];
37 pim_inet4_dump("<from?>", from
, from_str
, sizeof(from_str
));
38 zlog_debug("%s: from %s on %s", label
, from_str
, ifp
->name
);
42 void igmp_v2_send_query(struct igmp_group
*group
, int fd
, const char *ifname
,
43 char *query_buf
, struct in_addr dst_addr
,
44 struct in_addr group_addr
,
45 int query_max_response_time_dsec
)
48 uint8_t max_resp_code
;
50 struct sockaddr_in to
;
54 /* max_resp_code must be non-zero else this will look like an IGMP v1
56 max_resp_code
= igmp_msg_encode16to8(query_max_response_time_dsec
);
57 zassert(max_resp_code
> 0);
59 query_buf
[0] = PIM_IGMP_MEMBERSHIP_QUERY
;
60 query_buf
[1] = max_resp_code
;
61 *(uint16_t *)(query_buf
+ IGMP_CHECKSUM_OFFSET
) =
62 0; /* for computing checksum */
63 memcpy(query_buf
+ 4, &group_addr
, sizeof(struct in_addr
));
65 checksum
= in_cksum(query_buf
, msg_size
);
66 *(uint16_t *)(query_buf
+ IGMP_CHECKSUM_OFFSET
) = checksum
;
68 if (PIM_DEBUG_IGMP_PACKETS
) {
69 char dst_str
[INET_ADDRSTRLEN
];
70 char group_str
[INET_ADDRSTRLEN
];
71 pim_inet4_dump("<dst?>", dst_addr
, dst_str
, sizeof(dst_str
));
72 pim_inet4_dump("<group?>", group_addr
, group_str
,
74 zlog_debug("Send IGMPv2 QUERY to %s on %s for group %s",
75 dst_str
, ifname
, group_str
);
78 memset(&to
, 0, sizeof(to
));
79 to
.sin_family
= AF_INET
;
80 to
.sin_addr
= dst_addr
;
83 sent
= sendto(fd
, query_buf
, msg_size
, MSG_DONTWAIT
,
84 (struct sockaddr
*)&to
, tolen
);
85 if (sent
!= (ssize_t
)msg_size
) {
86 char dst_str
[INET_ADDRSTRLEN
];
87 char group_str
[INET_ADDRSTRLEN
];
88 pim_inet4_dump("<dst?>", dst_addr
, dst_str
, sizeof(dst_str
));
89 pim_inet4_dump("<group?>", group_addr
, group_str
,
93 "Send IGMPv2 QUERY failed due to %s on %s: group=%s msg_size=%zd: errno=%d: %s",
94 dst_str
, ifname
, group_str
, msg_size
, errno
,
95 safe_strerror(errno
));
98 "Send IGMPv2 QUERY failed due to %s on %s: group=%s msg_size=%zd: sent=%zd",
99 dst_str
, ifname
, group_str
, msg_size
, sent
);
105 int igmp_v2_recv_report(struct igmp_sock
*igmp
, struct in_addr from
,
106 const char *from_str
, char *igmp_msg
, int igmp_msg_len
)
108 struct interface
*ifp
= igmp
->interface
;
109 struct in_addr group_addr
;
110 char group_str
[INET_ADDRSTRLEN
];
112 on_trace(__PRETTY_FUNCTION__
, igmp
->interface
, from
);
114 if (igmp
->mtrace_only
)
117 if (igmp_msg_len
!= IGMP_V12_MSG_SIZE
) {
119 "Recv IGMPv2 REPORT from %s on %s: size=%d other than correct=%d",
120 from_str
, ifp
->name
, igmp_msg_len
, IGMP_V12_MSG_SIZE
);
124 /* Collecting IGMP Rx stats */
125 igmp
->rx_stats
.report_v2
++;
127 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
129 if (PIM_DEBUG_IGMP_PACKETS
) {
130 pim_inet4_dump("<dst?>", group_addr
, group_str
,
132 zlog_debug("Recv IGMPv2 REPORT from %s on %s for %s", from_str
,
133 ifp
->name
, group_str
);
138 * 7.3.2. In the Presence of Older Version Group Members
140 * When Group Compatibility Mode is IGMPv2, a router internally
141 * translates the following IGMPv2 messages for that group to their
142 * IGMPv3 equivalents:
144 * IGMPv2 Message IGMPv3 Equivalent
145 * -------------- -----------------
149 igmpv3_report_isex(igmp
, from
, group_addr
, 0, NULL
, 1);
154 int igmp_v2_recv_leave(struct igmp_sock
*igmp
, struct in_addr from
,
155 const char *from_str
, char *igmp_msg
, int igmp_msg_len
)
157 struct interface
*ifp
= igmp
->interface
;
158 struct in_addr group_addr
;
159 char group_str
[INET_ADDRSTRLEN
];
161 on_trace(__PRETTY_FUNCTION__
, igmp
->interface
, from
);
163 if (igmp
->mtrace_only
)
166 if (igmp_msg_len
!= IGMP_V12_MSG_SIZE
) {
168 "Recv IGMPv2 LEAVE from %s on %s: size=%d other than correct=%d",
169 from_str
, ifp
->name
, igmp_msg_len
, IGMP_V12_MSG_SIZE
);
173 /* Collecting IGMP Rx stats */
174 igmp
->rx_stats
.leave_v2
++;
176 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
178 if (PIM_DEBUG_IGMP_PACKETS
) {
179 pim_inet4_dump("<dst?>", group_addr
, group_str
,
181 zlog_debug("Recv IGMPv2 LEAVE from %s on %s for %s", from_str
,
182 ifp
->name
, group_str
);
187 * 7.3.2. In the Presence of Older Version Group Members
189 * When Group Compatibility Mode is IGMPv2, a router internally
190 * translates the following IGMPv2 messages for that group to their
191 * IGMPv3 equivalents:
193 * IGMPv2 Message IGMPv3 Equivalent
194 * -------------- -----------------
198 igmpv3_report_toin(igmp
, from
, group_addr
, 0, NULL
);