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