]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_ssmpingd.c
pimd: Move pim_ifchannel_list and vif array into 'struct pim_instance *'
[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
28 #include "pimd.h"
29 #include "pim_ssmpingd.h"
30 #include "pim_time.h"
31 #include "pim_sock.h"
32
33 static const char *const PIM_SSMPINGD_REPLY_GROUP = "232.43.211.234";
34
35 enum { PIM_SSMPINGD_REQUEST = 'Q', PIM_SSMPINGD_REPLY = 'A' };
36
37 static void ssmpingd_read_on(struct ssmpingd_sock *ss);
38
39 void pim_ssmpingd_init()
40 {
41 int result;
42
43 zassert(!qpim_ssmpingd_list);
44
45 result = inet_pton(AF_INET, PIM_SSMPINGD_REPLY_GROUP,
46 &qpim_ssmpingd_group_addr);
47
48 zassert(result > 0);
49 }
50
51 void pim_ssmpingd_destroy()
52 {
53 if (qpim_ssmpingd_list) {
54 list_free(qpim_ssmpingd_list);
55 qpim_ssmpingd_list = 0;
56 }
57 }
58
59 static struct ssmpingd_sock *ssmpingd_find(struct in_addr source_addr)
60 {
61 struct listnode *node;
62 struct ssmpingd_sock *ss;
63
64 if (!qpim_ssmpingd_list)
65 return 0;
66
67 for (ALL_LIST_ELEMENTS_RO(qpim_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 zlog_err("%s: could not create socket: errno=%d: %s",
87 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
88 return -1;
89 }
90
91 sockaddr.sin_family = AF_INET;
92 sockaddr.sin_addr = addr;
93 sockaddr.sin_port = htons(port);
94
95 if (bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
96 char addr_str[INET_ADDRSTRLEN];
97 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
98 zlog_warn(
99 "%s: bind(fd=%d,addr=%s,port=%d,len=%zu) failure: errno=%d: %s",
100 __PRETTY_FUNCTION__, fd, addr_str, port,
101 sizeof(sockaddr), errno, safe_strerror(errno));
102 close(fd);
103 return -1;
104 }
105
106 /* Needed to obtain destination address from recvmsg() */
107 {
108 #if defined(HAVE_IP_PKTINFO)
109 /* Linux and Solaris IP_PKTINFO */
110 int opt = 1;
111 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt))) {
112 zlog_warn(
113 "%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
114 __PRETTY_FUNCTION__, fd, errno,
115 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 __PRETTY_FUNCTION__, fd, errno,
125 safe_strerror(errno));
126 }
127 #else
128 zlog_err(
129 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
130 __FILE__, __PRETTY_FUNCTION__);
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 __PRETTY_FUNCTION__, fd, errno,
143 safe_strerror(errno));
144 close(fd);
145 return -1;
146 }
147 }
148
149 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&mttl,
150 sizeof(mttl))) {
151 zlog_warn(
152 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
153 __PRETTY_FUNCTION__, mttl, fd, errno,
154 safe_strerror(errno));
155 close(fd);
156 return -1;
157 }
158
159 if (setsockopt_ipv4_multicast_loop(fd, 0)) {
160 zlog_warn(
161 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
162 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
163 close(fd);
164 return PIM_SOCK_ERR_LOOP;
165 }
166
167 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (void *)&addr,
168 sizeof(addr))) {
169 zlog_warn(
170 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
171 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
172 close(fd);
173 return -1;
174 }
175
176 {
177 long flags;
178
179 flags = fcntl(fd, F_GETFL, 0);
180 if (flags < 0) {
181 zlog_warn(
182 "%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
183 __PRETTY_FUNCTION__, fd, errno,
184 safe_strerror(errno));
185 close(fd);
186 return -1;
187 }
188
189 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
190 zlog_warn(
191 "%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
192 __PRETTY_FUNCTION__, fd, errno,
193 safe_strerror(errno));
194 close(fd);
195 return -1;
196 }
197 }
198
199 return fd;
200 }
201
202 static void ssmpingd_delete(struct ssmpingd_sock *ss)
203 {
204 zassert(ss);
205 zassert(qpim_ssmpingd_list);
206
207 THREAD_OFF(ss->t_sock_read);
208
209 if (close(ss->sock_fd)) {
210 char source_str[INET_ADDRSTRLEN];
211 pim_inet4_dump("<src?>", ss->source_addr, source_str,
212 sizeof(source_str));
213 zlog_warn(
214 "%s: failure closing ssmpingd sock_fd=%d for source %s: errno=%d: %s",
215 __PRETTY_FUNCTION__, ss->sock_fd, source_str, errno,
216 safe_strerror(errno));
217 /* warning only */
218 }
219
220 listnode_delete(qpim_ssmpingd_list, ss);
221 ssmpingd_free(ss);
222 }
223
224 static void ssmpingd_sendto(struct ssmpingd_sock *ss, const uint8_t *buf,
225 int len, struct sockaddr_in to)
226 {
227 socklen_t tolen = sizeof(to);
228 int sent;
229
230 sent = sendto(ss->sock_fd, buf, len, MSG_DONTWAIT,
231 (struct sockaddr *)&to, tolen);
232 if (sent != len) {
233 char to_str[INET_ADDRSTRLEN];
234 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
235 if (sent < 0) {
236 zlog_warn(
237 "%s: sendto() failure to %s,%d: fd=%d len=%d: errno=%d: %s",
238 __PRETTY_FUNCTION__, to_str, ntohs(to.sin_port),
239 ss->sock_fd, len, errno, safe_strerror(errno));
240 } else {
241 zlog_warn(
242 "%s: sendto() partial to %s,%d: fd=%d len=%d: sent=%d",
243 __PRETTY_FUNCTION__, to_str, ntohs(to.sin_port),
244 ss->sock_fd, len, sent);
245 }
246 }
247 }
248
249 static int ssmpingd_read_msg(struct ssmpingd_sock *ss)
250 {
251 struct interface *ifp;
252 struct sockaddr_in from;
253 struct sockaddr_in to;
254 socklen_t fromlen = sizeof(from);
255 socklen_t tolen = sizeof(to);
256 ifindex_t ifindex = -1;
257 uint8_t buf[1000];
258 int len;
259
260 ++ss->requests;
261
262 len = pim_socket_recvfromto(ss->sock_fd, buf, sizeof(buf), &from,
263 &fromlen, &to, &tolen, &ifindex);
264 if (len < 0) {
265 char source_str[INET_ADDRSTRLEN];
266 pim_inet4_dump("<src?>", ss->source_addr, source_str,
267 sizeof(source_str));
268 zlog_warn(
269 "%s: failure receiving ssmping for source %s on fd=%d: errno=%d: %s",
270 __PRETTY_FUNCTION__, source_str, ss->sock_fd, errno,
271 safe_strerror(errno));
272 return -1;
273 }
274
275 ifp = if_lookup_by_index(ifindex, pimg->vrf_id);
276
277 if (buf[0] != PIM_SSMPINGD_REQUEST) {
278 char source_str[INET_ADDRSTRLEN];
279 char from_str[INET_ADDRSTRLEN];
280 char to_str[INET_ADDRSTRLEN];
281 pim_inet4_dump("<src?>", ss->source_addr, source_str,
282 sizeof(source_str));
283 pim_inet4_dump("<from?>", from.sin_addr, from_str,
284 sizeof(from_str));
285 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
286 zlog_warn(
287 "%s: bad ssmping type=%d from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
288 __PRETTY_FUNCTION__, buf[0], from_str,
289 ntohs(from.sin_port), to_str, ntohs(to.sin_port),
290 ifp ? ifp->name : "<iface?>", ifindex, ss->sock_fd,
291 source_str);
292 return 0;
293 }
294
295 if (PIM_DEBUG_SSMPINGD) {
296 char source_str[INET_ADDRSTRLEN];
297 char from_str[INET_ADDRSTRLEN];
298 char to_str[INET_ADDRSTRLEN];
299 pim_inet4_dump("<src?>", ss->source_addr, source_str,
300 sizeof(source_str));
301 pim_inet4_dump("<from?>", from.sin_addr, from_str,
302 sizeof(from_str));
303 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
304 zlog_debug(
305 "%s: recv ssmping from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
306 __PRETTY_FUNCTION__, from_str, ntohs(from.sin_port),
307 to_str, ntohs(to.sin_port),
308 ifp ? ifp->name : "<iface?>", ifindex, ss->sock_fd,
309 source_str);
310 }
311
312 buf[0] = PIM_SSMPINGD_REPLY;
313
314 /* unicast reply */
315 ssmpingd_sendto(ss, buf, len, from);
316
317 /* multicast reply */
318 from.sin_addr = qpim_ssmpingd_group_addr;
319 ssmpingd_sendto(ss, buf, len, from);
320
321 return 0;
322 }
323
324 static int ssmpingd_sock_read(struct thread *t)
325 {
326 struct ssmpingd_sock *ss;
327 int result;
328
329 ss = THREAD_ARG(t);
330
331 result = ssmpingd_read_msg(ss);
332
333 /* Keep reading */
334 ssmpingd_read_on(ss);
335
336 return result;
337 }
338
339 static void ssmpingd_read_on(struct ssmpingd_sock *ss)
340 {
341 thread_add_read(master, ssmpingd_sock_read, ss, ss->sock_fd,
342 &ss->t_sock_read);
343 }
344
345 static struct ssmpingd_sock *ssmpingd_new(struct in_addr source_addr)
346 {
347 struct ssmpingd_sock *ss;
348 int sock_fd;
349
350 if (!qpim_ssmpingd_list) {
351 qpim_ssmpingd_list = list_new();
352 if (!qpim_ssmpingd_list) {
353 zlog_err(
354 "%s %s: failure: qpim_ssmpingd_list=list_new()",
355 __FILE__, __PRETTY_FUNCTION__);
356 return 0;
357 }
358 qpim_ssmpingd_list->del = (void (*)(void *))ssmpingd_free;
359 }
360
361 sock_fd =
362 ssmpingd_socket(source_addr, /* port: */ 4321, /* mTTL: */ 64);
363 if (sock_fd < 0) {
364 char source_str[INET_ADDRSTRLEN];
365 pim_inet4_dump("<src?>", source_addr, source_str,
366 sizeof(source_str));
367 zlog_warn("%s: ssmpingd_socket() failure for source %s",
368 __PRETTY_FUNCTION__, source_str);
369 return 0;
370 }
371
372 ss = XCALLOC(MTYPE_PIM_SSMPINGD, sizeof(*ss));
373 if (!ss) {
374 char source_str[INET_ADDRSTRLEN];
375 pim_inet4_dump("<src?>", source_addr, source_str,
376 sizeof(source_str));
377 zlog_err("%s: XCALLOC(%zu) failure for ssmpingd source %s",
378 __PRETTY_FUNCTION__, sizeof(*ss), source_str);
379 close(sock_fd);
380 return 0;
381 }
382
383 ss->sock_fd = sock_fd;
384 ss->t_sock_read = NULL;
385 ss->source_addr = source_addr;
386 ss->creation = pim_time_monotonic_sec();
387 ss->requests = 0;
388
389 listnode_add(qpim_ssmpingd_list, ss);
390
391 ssmpingd_read_on(ss);
392
393 return ss;
394 }
395
396 int pim_ssmpingd_start(struct in_addr source_addr)
397 {
398 struct ssmpingd_sock *ss;
399
400 ss = ssmpingd_find(source_addr);
401 if (ss) {
402 /* silently ignore request to recreate entry */
403 return 0;
404 }
405
406 {
407 char source_str[INET_ADDRSTRLEN];
408 pim_inet4_dump("<src?>", source_addr, source_str,
409 sizeof(source_str));
410 zlog_info("%s: starting ssmpingd for source %s",
411 __PRETTY_FUNCTION__, source_str);
412 }
413
414 ss = ssmpingd_new(source_addr);
415 if (!ss) {
416 char source_str[INET_ADDRSTRLEN];
417 pim_inet4_dump("<src?>", source_addr, source_str,
418 sizeof(source_str));
419 zlog_warn("%s: ssmpingd_new() failure for source %s",
420 __PRETTY_FUNCTION__, source_str);
421 return -1;
422 }
423
424 return 0;
425 }
426
427 int pim_ssmpingd_stop(struct in_addr source_addr)
428 {
429 struct ssmpingd_sock *ss;
430
431 ss = ssmpingd_find(source_addr);
432 if (!ss) {
433 char source_str[INET_ADDRSTRLEN];
434 pim_inet4_dump("<src?>", source_addr, source_str,
435 sizeof(source_str));
436 zlog_warn("%s: could not find ssmpingd for source %s",
437 __PRETTY_FUNCTION__, source_str);
438 return -1;
439 }
440
441 {
442 char source_str[INET_ADDRSTRLEN];
443 pim_inet4_dump("<src?>", source_addr, source_str,
444 sizeof(source_str));
445 zlog_info("%s: stopping ssmpingd for source %s",
446 __PRETTY_FUNCTION__, source_str);
447 }
448
449 ssmpingd_delete(ss);
450
451 return 0;
452 }