]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_igmpv2.c
pimd: add support for IGMPv2
[mirror_frr.git] / pimd / pim_igmpv2.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2016 Cumulus Networks, Inc.
4 * Daniel Walton
5 *
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.
10 *
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.
15 *
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,
19 * MA 02110-1301 USA
20 */
21
22 #include "pimd.h"
23 #include "pim_igmp.h"
24 #include "pim_igmpv2.h"
25 #include "pim_igmpv3.h"
26 #include "pim_str.h"
27 #include "pim_time.h"
28 #include "pim_util.h"
29
30
31 static void
32 on_trace (const char *label,
33 struct interface *ifp, struct in_addr from)
34 {
35 if (PIM_DEBUG_IGMP_TRACE) {
36 char from_str[100];
37 pim_inet4_dump("<from?>", from, from_str, sizeof(from_str));
38 zlog_debug("%s: from %s on %s",
39 label, from_str, ifp->name);
40 }
41 }
42
43 void
44 igmp_v2_send_query (struct igmp_group *group,
45 int fd,
46 const char *ifname,
47 char *query_buf,
48 struct in_addr dst_addr,
49 struct in_addr group_addr,
50 int query_max_response_time_dsec)
51 {
52 ssize_t msg_size = 8;
53 uint8_t max_resp_code;
54 ssize_t sent;
55 struct sockaddr_in to;
56 socklen_t tolen;
57 uint16_t checksum;
58
59 /* max_resp_code must be non-zero else this will look like an IGMP v1 query */
60 max_resp_code = igmp_msg_encode16to8(query_max_response_time_dsec);
61 zassert(max_resp_code > 0);
62
63 query_buf[0] = PIM_IGMP_MEMBERSHIP_QUERY;
64 query_buf[1] = max_resp_code;
65 *(uint16_t *)(query_buf + IGMP_CHECKSUM_OFFSET) = 0; /* for computing checksum */
66 memcpy(query_buf+4, &group_addr, sizeof(struct in_addr));
67
68 checksum = in_cksum(query_buf, msg_size);
69 *(uint16_t *)(query_buf + IGMP_CHECKSUM_OFFSET) = checksum;
70
71 if (PIM_DEBUG_IGMP_PACKETS) {
72 char dst_str[100];
73 char group_str[100];
74 pim_inet4_dump("<dst?>", dst_addr, dst_str, sizeof(dst_str));
75 pim_inet4_dump("<group?>", group_addr, group_str, sizeof(group_str));
76 zlog_debug("Send IGMPv2 QUERY to %s on %s for group %s",
77 dst_str, ifname, group_str);
78 }
79
80 memset(&to, 0, sizeof(to));
81 to.sin_family = AF_INET;
82 to.sin_addr = dst_addr;
83 tolen = sizeof(to);
84
85 sent = sendto(fd, query_buf, msg_size, MSG_DONTWAIT,
86 (struct sockaddr *)&to, tolen);
87 if (sent != (ssize_t) msg_size) {
88 char dst_str[100];
89 char group_str[100];
90 pim_inet4_dump("<dst?>", dst_addr, dst_str, sizeof(dst_str));
91 pim_inet4_dump("<group?>", group_addr, group_str, sizeof(group_str));
92 if (sent < 0) {
93 zlog_warn("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, safe_strerror(errno));
95 }
96 else {
97 zlog_warn("Send IGMPv2 QUERY failed due to %s on %s: group=%s msg_size=%zd: sent=%zd",
98 dst_str, ifname, group_str, msg_size, sent);
99 }
100 return;
101 }
102 }
103
104 int
105 igmp_v2_recv_report (struct igmp_sock *igmp,
106 struct in_addr from, const char *from_str,
107 char *igmp_msg, int igmp_msg_len)
108 {
109 struct interface *ifp = igmp->interface;
110 struct in_addr group_addr;
111 char group_str[100];
112
113 on_trace(__PRETTY_FUNCTION__, igmp->interface, from);
114
115 if (igmp_msg_len != IGMP_V12_MSG_SIZE) {
116 zlog_warn("Recv IGMPv2 REPORT from %s on %s: size=%d other than correct=%d",
117 from_str, ifp->name, igmp_msg_len, IGMP_V12_MSG_SIZE);
118 return -1;
119 }
120
121 memcpy(&group_addr, igmp_msg + 4, sizeof(struct in_addr));
122
123 if (PIM_DEBUG_IGMP_PACKETS) {
124 pim_inet4_dump("<dst?>", group_addr, group_str, sizeof(group_str));
125 zlog_debug("Recv IGMPv2 REPORT from %s on %s for %s",
126 from_str, ifp->name, group_str);
127 }
128
129 /*
130 * RFC 3376
131 * 7.3.2. In the Presence of Older Version Group Members
132 *
133 * When Group Compatibility Mode is IGMPv2, a router internally
134 * translates the following IGMPv2 messages for that group to their
135 * IGMPv3 equivalents:
136 *
137 * IGMPv2 Message IGMPv3 Equivalent
138 * -------------- -----------------
139 * Report IS_EX( {} )
140 * Leave TO_IN( {} )
141 */
142 igmpv3_report_isex (igmp, from, group_addr, 0, NULL, 1);
143
144 return 0;
145 }
146
147 int
148 igmp_v2_recv_leave (struct igmp_sock *igmp,
149 struct in_addr from, const char *from_str,
150 char *igmp_msg, int igmp_msg_len)
151 {
152 struct interface *ifp = igmp->interface;
153 struct in_addr group_addr;
154 char group_str[100];
155
156 on_trace(__PRETTY_FUNCTION__, igmp->interface, from);
157
158 if (igmp_msg_len != IGMP_V12_MSG_SIZE) {
159 zlog_warn("Recv IGMPv2 LEAVE from %s on %s: size=%d other than correct=%d",
160 from_str, ifp->name, igmp_msg_len, IGMP_V12_MSG_SIZE);
161 return -1;
162 }
163
164 memcpy(&group_addr, igmp_msg + 4, sizeof(struct in_addr));
165
166 if (PIM_DEBUG_IGMP_PACKETS) {
167 pim_inet4_dump("<dst?>", group_addr, group_str, sizeof(group_str));
168 zlog_debug("Recv IGMPv2 LEAVE from %s on %s for %s",
169 from_str, ifp->name, group_str);
170 }
171
172 memcpy(&group_addr, igmp_msg + 4, sizeof(struct in_addr));
173
174 /*
175 * RFC 3376
176 * 7.3.2. In the Presence of Older Version Group Members
177 *
178 * When Group Compatibility Mode is IGMPv2, a router internally
179 * translates the following IGMPv2 messages for that group to their
180 * IGMPv3 equivalents:
181 *
182 * IGMPv2 Message IGMPv3 Equivalent
183 * -------------- -----------------
184 * Report IS_EX( {} )
185 * Leave TO_IN( {} )
186 */
187 igmpv3_report_toin (igmp, from, group_addr, 0, NULL);
188
189 return 0;
190 }