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