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