]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_ssmpingd.c
zebra: move rtadv service from zrouter to zvrf
[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 zassert(!pim->ssmpingd_list);
45
46 result = inet_pton(AF_INET, PIM_SSMPINGD_REPLY_GROUP,
47 &pim->ssmpingd_group_addr);
48
49 zassert(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 __PRETTY_FUNCTION__, 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 __PRETTY_FUNCTION__, fd, addr_str, port,
102 sizeof(sockaddr), errno, 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 __PRETTY_FUNCTION__, fd, errno,
116 safe_strerror(errno));
117 }
118 #elif defined(HAVE_IP_RECVDSTADDR)
119 /* BSD IP_RECVDSTADDR */
120 int opt = 1;
121 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt,
122 sizeof(opt))) {
123 zlog_warn(
124 "%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
125 __PRETTY_FUNCTION__, fd, errno,
126 safe_strerror(errno));
127 }
128 #else
129 flog_err(
130 EC_LIB_DEVELOPMENT,
131 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
132 __FILE__, __PRETTY_FUNCTION__);
133 close(fd);
134 return -1;
135 #endif
136 }
137
138 {
139 int reuse = 1;
140 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse,
141 sizeof(reuse))) {
142 zlog_warn(
143 "%s: could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
144 __PRETTY_FUNCTION__, fd, errno,
145 safe_strerror(errno));
146 close(fd);
147 return -1;
148 }
149 }
150
151 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&mttl,
152 sizeof(mttl))) {
153 zlog_warn(
154 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
155 __PRETTY_FUNCTION__, mttl, fd, errno,
156 safe_strerror(errno));
157 close(fd);
158 return -1;
159 }
160
161 if (setsockopt_ipv4_multicast_loop(fd, 0)) {
162 zlog_warn(
163 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
164 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
165 close(fd);
166 return PIM_SOCK_ERR_LOOP;
167 }
168
169 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (void *)&addr,
170 sizeof(addr))) {
171 zlog_warn(
172 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
173 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
174 close(fd);
175 return -1;
176 }
177
178 {
179 long flags;
180
181 flags = fcntl(fd, F_GETFL, 0);
182 if (flags < 0) {
183 zlog_warn(
184 "%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
185 __PRETTY_FUNCTION__, fd, errno,
186 safe_strerror(errno));
187 close(fd);
188 return -1;
189 }
190
191 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
192 zlog_warn(
193 "%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
194 __PRETTY_FUNCTION__, fd, errno,
195 safe_strerror(errno));
196 close(fd);
197 return -1;
198 }
199 }
200
201 return fd;
202 }
203
204 static void ssmpingd_delete(struct ssmpingd_sock *ss)
205 {
206 zassert(ss);
207
208 THREAD_OFF(ss->t_sock_read);
209
210 if (close(ss->sock_fd)) {
211 char source_str[INET_ADDRSTRLEN];
212 pim_inet4_dump("<src?>", ss->source_addr, source_str,
213 sizeof(source_str));
214 zlog_warn(
215 "%s: failure closing ssmpingd sock_fd=%d for source %s: errno=%d: %s",
216 __PRETTY_FUNCTION__, ss->sock_fd, source_str, errno,
217 safe_strerror(errno));
218 /* warning only */
219 }
220
221 listnode_delete(ss->pim->ssmpingd_list, ss);
222 ssmpingd_free(ss);
223 }
224
225 static void ssmpingd_sendto(struct ssmpingd_sock *ss, const uint8_t *buf,
226 int len, struct sockaddr_in to)
227 {
228 socklen_t tolen = sizeof(to);
229 int sent;
230
231 sent = sendto(ss->sock_fd, buf, len, MSG_DONTWAIT,
232 (struct sockaddr *)&to, tolen);
233 if (sent != len) {
234 char to_str[INET_ADDRSTRLEN];
235 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
236 if (sent < 0) {
237 zlog_warn(
238 "%s: sendto() failure to %s,%d: fd=%d len=%d: errno=%d: %s",
239 __PRETTY_FUNCTION__, to_str, ntohs(to.sin_port),
240 ss->sock_fd, len, errno, safe_strerror(errno));
241 } else {
242 zlog_warn(
243 "%s: sendto() partial to %s,%d: fd=%d len=%d: sent=%d",
244 __PRETTY_FUNCTION__, to_str, ntohs(to.sin_port),
245 ss->sock_fd, len, sent);
246 }
247 }
248 }
249
250 static int ssmpingd_read_msg(struct ssmpingd_sock *ss)
251 {
252 struct interface *ifp;
253 struct sockaddr_in from;
254 struct sockaddr_in to;
255 socklen_t fromlen = sizeof(from);
256 socklen_t tolen = sizeof(to);
257 ifindex_t ifindex = -1;
258 uint8_t buf[1000];
259 int len;
260
261 ++ss->requests;
262
263 len = pim_socket_recvfromto(ss->sock_fd, buf, sizeof(buf), &from,
264 &fromlen, &to, &tolen, &ifindex);
265 if (len < 0) {
266 char source_str[INET_ADDRSTRLEN];
267 pim_inet4_dump("<src?>", ss->source_addr, source_str,
268 sizeof(source_str));
269 zlog_warn(
270 "%s: failure receiving ssmping for source %s on fd=%d: errno=%d: %s",
271 __PRETTY_FUNCTION__, source_str, ss->sock_fd, errno,
272 safe_strerror(errno));
273 return -1;
274 }
275
276 ifp = if_lookup_by_index(ifindex, ss->pim->vrf_id);
277
278 if (buf[0] != PIM_SSMPINGD_REQUEST) {
279 char source_str[INET_ADDRSTRLEN];
280 char from_str[INET_ADDRSTRLEN];
281 char to_str[INET_ADDRSTRLEN];
282 pim_inet4_dump("<src?>", ss->source_addr, source_str,
283 sizeof(source_str));
284 pim_inet4_dump("<from?>", from.sin_addr, from_str,
285 sizeof(from_str));
286 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
287 zlog_warn(
288 "%s: bad ssmping type=%d from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
289 __PRETTY_FUNCTION__, buf[0], from_str,
290 ntohs(from.sin_port), to_str, ntohs(to.sin_port),
291 ifp ? ifp->name : "<iface?>", ifindex, ss->sock_fd,
292 source_str);
293 return 0;
294 }
295
296 if (PIM_DEBUG_SSMPINGD) {
297 char source_str[INET_ADDRSTRLEN];
298 char from_str[INET_ADDRSTRLEN];
299 char to_str[INET_ADDRSTRLEN];
300 pim_inet4_dump("<src?>", ss->source_addr, source_str,
301 sizeof(source_str));
302 pim_inet4_dump("<from?>", from.sin_addr, from_str,
303 sizeof(from_str));
304 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
305 zlog_debug(
306 "%s: recv ssmping from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
307 __PRETTY_FUNCTION__, from_str, ntohs(from.sin_port),
308 to_str, ntohs(to.sin_port),
309 ifp ? ifp->name : "<iface?>", ifindex, ss->sock_fd,
310 source_str);
311 }
312
313 buf[0] = PIM_SSMPINGD_REPLY;
314
315 /* unicast reply */
316 ssmpingd_sendto(ss, buf, len, from);
317
318 /* multicast reply */
319 from.sin_addr = ss->pim->ssmpingd_group_addr;
320 ssmpingd_sendto(ss, buf, len, from);
321
322 return 0;
323 }
324
325 static int ssmpingd_sock_read(struct thread *t)
326 {
327 struct ssmpingd_sock *ss;
328 int result;
329
330 ss = THREAD_ARG(t);
331
332 result = ssmpingd_read_msg(ss);
333
334 /* Keep reading */
335 ssmpingd_read_on(ss);
336
337 return result;
338 }
339
340 static void ssmpingd_read_on(struct ssmpingd_sock *ss)
341 {
342 thread_add_read(router->master, ssmpingd_sock_read, ss, ss->sock_fd,
343 &ss->t_sock_read);
344 }
345
346 static struct ssmpingd_sock *ssmpingd_new(struct pim_instance *pim,
347 struct in_addr source_addr)
348 {
349 struct ssmpingd_sock *ss;
350 int sock_fd;
351
352 if (!pim->ssmpingd_list) {
353 pim->ssmpingd_list = list_new();
354 pim->ssmpingd_list->del = (void (*)(void *))ssmpingd_free;
355 }
356
357 sock_fd =
358 ssmpingd_socket(source_addr, /* port: */ 4321, /* mTTL: */ 64);
359 if (sock_fd < 0) {
360 char source_str[INET_ADDRSTRLEN];
361 pim_inet4_dump("<src?>", source_addr, source_str,
362 sizeof(source_str));
363 zlog_warn("%s: ssmpingd_socket() failure for source %s",
364 __PRETTY_FUNCTION__, source_str);
365 return 0;
366 }
367
368 ss = XCALLOC(MTYPE_PIM_SSMPINGD, sizeof(*ss));
369
370 ss->pim = pim;
371 ss->sock_fd = sock_fd;
372 ss->t_sock_read = NULL;
373 ss->source_addr = source_addr;
374 ss->creation = pim_time_monotonic_sec();
375 ss->requests = 0;
376
377 listnode_add(pim->ssmpingd_list, ss);
378
379 ssmpingd_read_on(ss);
380
381 return ss;
382 }
383
384 int pim_ssmpingd_start(struct pim_instance *pim, struct in_addr source_addr)
385 {
386 struct ssmpingd_sock *ss;
387
388 ss = ssmpingd_find(pim, source_addr);
389 if (ss) {
390 /* silently ignore request to recreate entry */
391 return 0;
392 }
393
394 {
395 char source_str[INET_ADDRSTRLEN];
396 pim_inet4_dump("<src?>", source_addr, source_str,
397 sizeof(source_str));
398 zlog_info("%s: starting ssmpingd for source %s",
399 __PRETTY_FUNCTION__, source_str);
400 }
401
402 ss = ssmpingd_new(pim, source_addr);
403 if (!ss) {
404 char source_str[INET_ADDRSTRLEN];
405 pim_inet4_dump("<src?>", source_addr, source_str,
406 sizeof(source_str));
407 zlog_warn("%s: ssmpingd_new() failure for source %s",
408 __PRETTY_FUNCTION__, source_str);
409 return -1;
410 }
411
412 return 0;
413 }
414
415 int pim_ssmpingd_stop(struct pim_instance *pim, struct in_addr source_addr)
416 {
417 struct ssmpingd_sock *ss;
418
419 ss = ssmpingd_find(pim, source_addr);
420 if (!ss) {
421 char source_str[INET_ADDRSTRLEN];
422 pim_inet4_dump("<src?>", source_addr, source_str,
423 sizeof(source_str));
424 zlog_warn("%s: could not find ssmpingd for source %s",
425 __PRETTY_FUNCTION__, source_str);
426 return -1;
427 }
428
429 {
430 char source_str[INET_ADDRSTRLEN];
431 pim_inet4_dump("<src?>", source_addr, source_str,
432 sizeof(source_str));
433 zlog_info("%s: stopping ssmpingd for source %s",
434 __PRETTY_FUNCTION__, source_str);
435 }
436
437 ssmpingd_delete(ss);
438
439 return 0;
440 }