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
17 * along with this program; see the file COPYING; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
26 #include "pim_igmpv2.h"
27 #include "pim_igmpv3.h"
33 static void on_trace(const char *label
, struct interface
*ifp
,
36 if (PIM_DEBUG_IGMP_TRACE
) {
37 char from_str
[INET_ADDRSTRLEN
];
38 pim_inet4_dump("<from?>", from
, from_str
, sizeof(from_str
));
39 zlog_debug("%s: from %s on %s", label
, from_str
, ifp
->name
);
43 void igmp_v2_send_query(struct igmp_group
*group
, int fd
, const char *ifname
,
44 char *query_buf
, struct in_addr dst_addr
,
45 struct in_addr group_addr
,
46 int query_max_response_time_dsec
)
49 uint8_t max_resp_code
;
51 struct sockaddr_in to
;
55 /* max_resp_code must be non-zero else this will look like an IGMP v1
57 max_resp_code
= igmp_msg_encode16to8(query_max_response_time_dsec
);
58 zassert(max_resp_code
> 0);
60 query_buf
[0] = PIM_IGMP_MEMBERSHIP_QUERY
;
61 query_buf
[1] = max_resp_code
;
62 *(uint16_t *)(query_buf
+ IGMP_CHECKSUM_OFFSET
) =
63 0; /* for computing checksum */
64 memcpy(query_buf
+ 4, &group_addr
, sizeof(struct in_addr
));
66 checksum
= in_cksum(query_buf
, msg_size
);
67 *(uint16_t *)(query_buf
+ IGMP_CHECKSUM_OFFSET
) = checksum
;
69 if (PIM_DEBUG_IGMP_PACKETS
) {
70 char dst_str
[INET_ADDRSTRLEN
];
71 char group_str
[INET_ADDRSTRLEN
];
72 pim_inet4_dump("<dst?>", dst_addr
, dst_str
, sizeof(dst_str
));
73 pim_inet4_dump("<group?>", group_addr
, group_str
,
75 zlog_debug("Send IGMPv2 QUERY to %s on %s for group %s",
76 dst_str
, ifname
, group_str
);
79 memset(&to
, 0, sizeof(to
));
80 to
.sin_family
= AF_INET
;
81 to
.sin_addr
= dst_addr
;
84 sent
= sendto(fd
, query_buf
, msg_size
, MSG_DONTWAIT
,
85 (struct sockaddr
*)&to
, tolen
);
86 if (sent
!= (ssize_t
)msg_size
) {
87 char dst_str
[INET_ADDRSTRLEN
];
88 char group_str
[INET_ADDRSTRLEN
];
89 pim_inet4_dump("<dst?>", dst_addr
, dst_str
, sizeof(dst_str
));
90 pim_inet4_dump("<group?>", group_addr
, group_str
,
94 "Send IGMPv2 QUERY failed due to %s on %s: group=%s msg_size=%zd: errno=%d: %s",
95 dst_str
, ifname
, group_str
, msg_size
, errno
,
96 safe_strerror(errno
));
99 "Send IGMPv2 QUERY failed due to %s on %s: group=%s msg_size=%zd: sent=%zd",
100 dst_str
, ifname
, group_str
, msg_size
, sent
);
106 int igmp_v2_recv_report(struct igmp_sock
*igmp
, struct in_addr from
,
107 const char *from_str
, char *igmp_msg
, int igmp_msg_len
)
109 struct interface
*ifp
= igmp
->interface
;
110 struct in_addr group_addr
;
111 char group_str
[INET_ADDRSTRLEN
];
113 on_trace(__PRETTY_FUNCTION__
, igmp
->interface
, from
);
115 if (igmp_msg_len
!= IGMP_V12_MSG_SIZE
) {
117 "Recv IGMPv2 REPORT from %s on %s: size=%d other than correct=%d",
118 from_str
, ifp
->name
, igmp_msg_len
, IGMP_V12_MSG_SIZE
);
122 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
124 if (PIM_DEBUG_IGMP_PACKETS
) {
125 pim_inet4_dump("<dst?>", group_addr
, group_str
,
127 zlog_debug("Recv IGMPv2 REPORT from %s on %s for %s", from_str
,
128 ifp
->name
, group_str
);
133 * 7.3.2. In the Presence of Older Version Group Members
135 * When Group Compatibility Mode is IGMPv2, a router internally
136 * translates the following IGMPv2 messages for that group to their
137 * IGMPv3 equivalents:
139 * IGMPv2 Message IGMPv3 Equivalent
140 * -------------- -----------------
144 igmpv3_report_isex(igmp
, from
, group_addr
, 0, NULL
, 1);
149 int igmp_v2_recv_leave(struct igmp_sock
*igmp
, struct in_addr from
,
150 const char *from_str
, char *igmp_msg
, int igmp_msg_len
)
152 struct interface
*ifp
= igmp
->interface
;
153 struct in_addr group_addr
;
154 char group_str
[INET_ADDRSTRLEN
];
156 on_trace(__PRETTY_FUNCTION__
, igmp
->interface
, from
);
158 if (igmp_msg_len
!= IGMP_V12_MSG_SIZE
) {
160 "Recv IGMPv2 LEAVE from %s on %s: size=%d other than correct=%d",
161 from_str
, ifp
->name
, igmp_msg_len
, IGMP_V12_MSG_SIZE
);
165 memcpy(&group_addr
, igmp_msg
+ 4, sizeof(struct in_addr
));
167 if (PIM_DEBUG_IGMP_PACKETS
) {
168 pim_inet4_dump("<dst?>", group_addr
, group_str
,
170 zlog_debug("Recv IGMPv2 LEAVE from %s on %s for %s", from_str
,
171 ifp
->name
, group_str
);
176 * 7.3.2. In the Presence of Older Version Group Members
178 * When Group Compatibility Mode is IGMPv2, a router internally
179 * translates the following IGMPv2 messages for that group to their
180 * IGMPv3 equivalents:
182 * IGMPv2 Message IGMPv3 Equivalent
183 * -------------- -----------------
187 igmpv3_report_toin(igmp
, from
, group_addr
, 0, NULL
);