]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_ssmpingd.c
Merge pull request #10729 from scop/build/include-frr@-in-deb
[mirror_frr.git] / pimd / pim_ssmpingd.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2008 Everton da Silva Marques
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include <zebra.h>
21
22 #include "if.h"
23 #include "log.h"
24 #include "memory.h"
25 #include "sockopt.h"
26 #include "vrf.h"
27 #include "lib_errors.h"
28
29 #include "pimd.h"
30 #include "pim_ssmpingd.h"
31 #include "pim_time.h"
32 #include "pim_sock.h"
33
34 static const char *const PIM_SSMPINGD_REPLY_GROUP = "232.43.211.234";
35
36 enum { PIM_SSMPINGD_REQUEST = 'Q', PIM_SSMPINGD_REPLY = 'A' };
37
38 static void ssmpingd_read_on(struct ssmpingd_sock *ss);
39
40 void pim_ssmpingd_init(struct pim_instance *pim)
41 {
42 int result;
43
44 assert(!pim->ssmpingd_list);
45
46 result = inet_pton(AF_INET, PIM_SSMPINGD_REPLY_GROUP,
47 &pim->ssmpingd_group_addr);
48
49 assert(result > 0);
50 }
51
52 void pim_ssmpingd_destroy(struct pim_instance *pim)
53 {
54 if (pim->ssmpingd_list)
55 list_delete(&pim->ssmpingd_list);
56 }
57
58 static struct ssmpingd_sock *ssmpingd_find(struct pim_instance *pim,
59 struct in_addr source_addr)
60 {
61 struct listnode *node;
62 struct ssmpingd_sock *ss;
63
64 if (!pim->ssmpingd_list)
65 return 0;
66
67 for (ALL_LIST_ELEMENTS_RO(pim->ssmpingd_list, node, ss))
68 if (source_addr.s_addr == ss->source_addr.s_addr)
69 return ss;
70
71 return 0;
72 }
73
74 static void ssmpingd_free(struct ssmpingd_sock *ss)
75 {
76 XFREE(MTYPE_PIM_SSMPINGD, ss);
77 }
78
79 static int ssmpingd_socket(struct in_addr addr, int port, int mttl)
80 {
81 struct sockaddr_in sockaddr;
82 int fd;
83
84 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
85 if (fd < 0) {
86 flog_err_sys(EC_LIB_SOCKET,
87 "%s: could not create socket: errno=%d: %s",
88 __func__, errno, safe_strerror(errno));
89 return -1;
90 }
91
92 sockaddr.sin_family = AF_INET;
93 sockaddr.sin_addr = addr;
94 sockaddr.sin_port = htons(port);
95
96 if (bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
97 char addr_str[INET_ADDRSTRLEN];
98 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
99 zlog_warn(
100 "%s: bind(fd=%d,addr=%s,port=%d,len=%zu) failure: errno=%d: %s",
101 __func__, fd, addr_str, port, sizeof(sockaddr), errno,
102 safe_strerror(errno));
103 close(fd);
104 return -1;
105 }
106
107 /* Needed to obtain destination address from recvmsg() */
108 {
109 #if defined(HAVE_IP_PKTINFO)
110 /* Linux and Solaris IP_PKTINFO */
111 int opt = 1;
112 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt))) {
113 zlog_warn(
114 "%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
115 __func__, fd, errno, safe_strerror(errno));
116 }
117 #elif defined(HAVE_IP_RECVDSTADDR)
118 /* BSD IP_RECVDSTADDR */
119 int opt = 1;
120 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt,
121 sizeof(opt))) {
122 zlog_warn(
123 "%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
124 __func__, fd, errno, safe_strerror(errno));
125 }
126 #else
127 flog_err(
128 EC_LIB_DEVELOPMENT,
129 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
130 __FILE__, __func__);
131 close(fd);
132 return -1;
133 #endif
134 }
135
136 {
137 int reuse = 1;
138 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse,
139 sizeof(reuse))) {
140 zlog_warn(
141 "%s: could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
142 __func__, fd, errno, safe_strerror(errno));
143 close(fd);
144 return -1;
145 }
146 }
147
148 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&mttl,
149 sizeof(mttl))) {
150 zlog_warn(
151 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
152 __func__, mttl, fd, errno, safe_strerror(errno));
153 close(fd);
154 return -1;
155 }
156
157 if (setsockopt_ipv4_multicast_loop(fd, 0)) {
158 zlog_warn(
159 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
160 __func__, fd, errno, safe_strerror(errno));
161 close(fd);
162 return PIM_SOCK_ERR_LOOP;
163 }
164
165 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (void *)&addr,
166 sizeof(addr))) {
167 zlog_warn(
168 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
169 __func__, fd, errno, safe_strerror(errno));
170 close(fd);
171 return -1;
172 }
173
174 {
175 long flags;
176
177 flags = fcntl(fd, F_GETFL, 0);
178 if (flags < 0) {
179 zlog_warn(
180 "%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
181 __func__, fd, errno, safe_strerror(errno));
182 close(fd);
183 return -1;
184 }
185
186 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
187 zlog_warn(
188 "%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
189 __func__, fd, errno, safe_strerror(errno));
190 close(fd);
191 return -1;
192 }
193 }
194
195 return fd;
196 }
197
198 static void ssmpingd_delete(struct ssmpingd_sock *ss)
199 {
200 assert(ss);
201
202 THREAD_OFF(ss->t_sock_read);
203
204 if (close(ss->sock_fd)) {
205 char source_str[INET_ADDRSTRLEN];
206 pim_inet4_dump("<src?>", ss->source_addr, source_str,
207 sizeof(source_str));
208 zlog_warn(
209 "%s: failure closing ssmpingd sock_fd=%d for source %s: errno=%d: %s",
210 __func__, ss->sock_fd, source_str, errno,
211 safe_strerror(errno));
212 /* warning only */
213 }
214
215 listnode_delete(ss->pim->ssmpingd_list, ss);
216 ssmpingd_free(ss);
217 }
218
219 static void ssmpingd_sendto(struct ssmpingd_sock *ss, const uint8_t *buf,
220 int len, struct sockaddr_in to)
221 {
222 socklen_t tolen = sizeof(to);
223 int sent;
224
225 sent = sendto(ss->sock_fd, buf, len, MSG_DONTWAIT,
226 (struct sockaddr *)&to, tolen);
227 if (sent != len) {
228 char to_str[INET_ADDRSTRLEN];
229 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
230 if (sent < 0) {
231 zlog_warn(
232 "%s: sendto() failure to %s,%d: fd=%d len=%d: errno=%d: %s",
233 __func__, to_str, ntohs(to.sin_port),
234 ss->sock_fd, len, errno, safe_strerror(errno));
235 } else {
236 zlog_warn(
237 "%s: sendto() partial to %s,%d: fd=%d len=%d: sent=%d",
238 __func__, to_str, ntohs(to.sin_port),
239 ss->sock_fd, len, sent);
240 }
241 }
242 }
243
244 static int ssmpingd_read_msg(struct ssmpingd_sock *ss)
245 {
246 struct interface *ifp;
247 struct sockaddr_storage from;
248 struct sockaddr_storage to;
249 socklen_t fromlen = sizeof(from);
250 socklen_t tolen = sizeof(to);
251 ifindex_t ifindex = -1;
252 uint8_t buf[1000];
253 int len;
254
255 ++ss->requests;
256
257 len = pim_socket_recvfromto(ss->sock_fd, buf, sizeof(buf), &from,
258 &fromlen, &to, &tolen, &ifindex);
259
260 if (len < 0) {
261 zlog_warn(
262 "%s: failure receiving ssmping for source %pI4 on fd=%d: errno=%d: %s",
263 __func__, &ss->source_addr, ss->sock_fd, errno,
264 safe_strerror(errno));
265 return -1;
266 }
267
268 ifp = if_lookup_by_index(ifindex, ss->pim->vrf->vrf_id);
269
270 if (buf[0] != PIM_SSMPINGD_REQUEST) {
271 zlog_warn(
272 "%s: bad ssmping type=%d from %pSUp to %pSUp on interface %s ifindex=%d fd=%d src=%pI4",
273 __func__, buf[0], &from, &to,
274 ifp ? ifp->name : "<iface?>", ifindex, ss->sock_fd,
275 &ss->source_addr);
276 return 0;
277 }
278
279 if (PIM_DEBUG_SSMPINGD) {
280 zlog_debug(
281 "%s: recv ssmping from %pSUp, to %pSUp, on interface %s ifindex=%d fd=%d src=%pI4",
282 __func__, &from, &to, ifp ? ifp->name : "<iface?>",
283 ifindex, ss->sock_fd, &ss->source_addr);
284 }
285
286 buf[0] = PIM_SSMPINGD_REPLY;
287
288 struct sockaddr_in *from_addr = (struct sockaddr_in *)&from;
289
290 /* unicast reply */
291 ssmpingd_sendto(ss, buf, len, *from_addr);
292
293 /* multicast reply */
294 from_addr->sin_addr = ss->pim->ssmpingd_group_addr;
295 ssmpingd_sendto(ss, buf, len, *from_addr);
296
297 return 0;
298 }
299
300 static void ssmpingd_sock_read(struct thread *t)
301 {
302 struct ssmpingd_sock *ss;
303
304 ss = THREAD_ARG(t);
305
306 ssmpingd_read_msg(ss);
307
308 /* Keep reading */
309 ssmpingd_read_on(ss);
310 }
311
312 static void ssmpingd_read_on(struct ssmpingd_sock *ss)
313 {
314 thread_add_read(router->master, ssmpingd_sock_read, ss, ss->sock_fd,
315 &ss->t_sock_read);
316 }
317
318 static struct ssmpingd_sock *ssmpingd_new(struct pim_instance *pim,
319 struct in_addr source_addr)
320 {
321 struct ssmpingd_sock *ss;
322 int sock_fd;
323
324 if (!pim->ssmpingd_list) {
325 pim->ssmpingd_list = list_new();
326 pim->ssmpingd_list->del = (void (*)(void *))ssmpingd_free;
327 }
328
329 sock_fd =
330 ssmpingd_socket(source_addr, /* port: */ 4321, /* mTTL: */ 64);
331 if (sock_fd < 0) {
332 char source_str[INET_ADDRSTRLEN];
333 pim_inet4_dump("<src?>", source_addr, source_str,
334 sizeof(source_str));
335 zlog_warn("%s: ssmpingd_socket() failure for source %s",
336 __func__, source_str);
337 return 0;
338 }
339
340 ss = XCALLOC(MTYPE_PIM_SSMPINGD, sizeof(*ss));
341
342 ss->pim = pim;
343 ss->sock_fd = sock_fd;
344 ss->t_sock_read = NULL;
345 ss->source_addr = source_addr;
346 ss->creation = pim_time_monotonic_sec();
347 ss->requests = 0;
348
349 listnode_add(pim->ssmpingd_list, ss);
350
351 ssmpingd_read_on(ss);
352
353 return ss;
354 }
355
356 int pim_ssmpingd_start(struct pim_instance *pim, struct in_addr source_addr)
357 {
358 struct ssmpingd_sock *ss;
359
360 ss = ssmpingd_find(pim, source_addr);
361 if (ss) {
362 /* silently ignore request to recreate entry */
363 return 0;
364 }
365
366 {
367 char source_str[INET_ADDRSTRLEN];
368 pim_inet4_dump("<src?>", source_addr, source_str,
369 sizeof(source_str));
370 zlog_info("%s: starting ssmpingd for source %s", __func__,
371 source_str);
372 }
373
374 ss = ssmpingd_new(pim, source_addr);
375 if (!ss) {
376 char source_str[INET_ADDRSTRLEN];
377 pim_inet4_dump("<src?>", source_addr, source_str,
378 sizeof(source_str));
379 zlog_warn("%s: ssmpingd_new() failure for source %s", __func__,
380 source_str);
381 return -1;
382 }
383
384 return 0;
385 }
386
387 int pim_ssmpingd_stop(struct pim_instance *pim, struct in_addr source_addr)
388 {
389 struct ssmpingd_sock *ss;
390
391 ss = ssmpingd_find(pim, source_addr);
392 if (!ss) {
393 char source_str[INET_ADDRSTRLEN];
394 pim_inet4_dump("<src?>", source_addr, source_str,
395 sizeof(source_str));
396 zlog_warn("%s: could not find ssmpingd for source %s", __func__,
397 source_str);
398 return -1;
399 }
400
401 {
402 char source_str[INET_ADDRSTRLEN];
403 pim_inet4_dump("<src?>", source_addr, source_str,
404 sizeof(source_str));
405 zlog_info("%s: stopping ssmpingd for source %s", __func__,
406 source_str);
407 }
408
409 ssmpingd_delete(ss);
410
411 return 0;
412 }