]>
git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_ssmpingd.c
3 * Copyright (C) 2008 Everton da Silva Marques
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.
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.
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
29 #include "pim_ssmpingd.h"
33 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "232.43.211.234";
35 enum { PIM_SSMPINGD_REQUEST
= 'Q', PIM_SSMPINGD_REPLY
= 'A' };
37 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
);
39 void pim_ssmpingd_init(struct pim_instance
*pim
)
43 zassert(!pim
->ssmpingd_list
);
45 result
= inet_pton(AF_INET
, PIM_SSMPINGD_REPLY_GROUP
,
46 &pim
->ssmpingd_group_addr
);
51 void pim_ssmpingd_destroy(struct pim_instance
*pim
)
53 if (pim
->ssmpingd_list
) {
54 list_free(pim
->ssmpingd_list
);
55 pim
->ssmpingd_list
= 0;
59 static struct ssmpingd_sock
*ssmpingd_find(struct pim_instance
*pim
,
60 struct in_addr source_addr
)
62 struct listnode
*node
;
63 struct ssmpingd_sock
*ss
;
65 if (!pim
->ssmpingd_list
)
68 for (ALL_LIST_ELEMENTS_RO(pim
->ssmpingd_list
, node
, ss
))
69 if (source_addr
.s_addr
== ss
->source_addr
.s_addr
)
75 static void ssmpingd_free(struct ssmpingd_sock
*ss
)
77 XFREE(MTYPE_PIM_SSMPINGD
, ss
);
80 static int ssmpingd_socket(struct in_addr addr
, int port
, int mttl
)
82 struct sockaddr_in sockaddr
;
85 fd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
87 zlog_err("%s: could not create socket: errno=%d: %s",
88 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
92 sockaddr
.sin_family
= AF_INET
;
93 sockaddr
.sin_addr
= addr
;
94 sockaddr
.sin_port
= htons(port
);
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
));
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
));
107 /* Needed to obtain destination address from recvmsg() */
109 #if defined(HAVE_IP_PKTINFO)
110 /* Linux and Solaris IP_PKTINFO */
112 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
114 "%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
115 __PRETTY_FUNCTION__
, fd
, errno
,
116 safe_strerror(errno
));
118 #elif defined(HAVE_IP_RECVDSTADDR)
119 /* BSD IP_RECVDSTADDR */
121 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
,
124 "%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
125 __PRETTY_FUNCTION__
, fd
, errno
,
126 safe_strerror(errno
));
130 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
131 __FILE__
, __PRETTY_FUNCTION__
);
139 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&reuse
,
142 "%s: could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
143 __PRETTY_FUNCTION__
, fd
, errno
,
144 safe_strerror(errno
));
150 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&mttl
,
153 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
154 __PRETTY_FUNCTION__
, mttl
, fd
, errno
,
155 safe_strerror(errno
));
160 if (setsockopt_ipv4_multicast_loop(fd
, 0)) {
162 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
163 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
165 return PIM_SOCK_ERR_LOOP
;
168 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&addr
,
171 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
172 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
180 flags
= fcntl(fd
, F_GETFL
, 0);
183 "%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
184 __PRETTY_FUNCTION__
, fd
, errno
,
185 safe_strerror(errno
));
190 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
)) {
192 "%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
193 __PRETTY_FUNCTION__
, fd
, errno
,
194 safe_strerror(errno
));
203 static void ssmpingd_delete(struct ssmpingd_sock
*ss
)
207 THREAD_OFF(ss
->t_sock_read
);
209 if (close(ss
->sock_fd
)) {
210 char source_str
[INET_ADDRSTRLEN
];
211 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
,
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
));
220 listnode_delete(ss
->pim
->ssmpingd_list
, ss
);
224 static void ssmpingd_sendto(struct ssmpingd_sock
*ss
, const uint8_t *buf
,
225 int len
, struct sockaddr_in to
)
227 socklen_t tolen
= sizeof(to
);
230 sent
= sendto(ss
->sock_fd
, buf
, len
, MSG_DONTWAIT
,
231 (struct sockaddr
*)&to
, tolen
);
233 char to_str
[INET_ADDRSTRLEN
];
234 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
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
));
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
);
249 static int ssmpingd_read_msg(struct ssmpingd_sock
*ss
)
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;
262 len
= pim_socket_recvfromto(ss
->sock_fd
, buf
, sizeof(buf
), &from
,
263 &fromlen
, &to
, &tolen
, &ifindex
);
265 char source_str
[INET_ADDRSTRLEN
];
266 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
,
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
));
275 ifp
= if_lookup_by_index(ifindex
, ss
->pim
->vrf_id
);
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
,
283 pim_inet4_dump("<from?>", from
.sin_addr
, from_str
,
285 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
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
,
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
,
301 pim_inet4_dump("<from?>", from
.sin_addr
, from_str
,
303 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
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
,
312 buf
[0] = PIM_SSMPINGD_REPLY
;
315 ssmpingd_sendto(ss
, buf
, len
, from
);
317 /* multicast reply */
318 from
.sin_addr
= ss
->pim
->ssmpingd_group_addr
;
319 ssmpingd_sendto(ss
, buf
, len
, from
);
324 static int ssmpingd_sock_read(struct thread
*t
)
326 struct ssmpingd_sock
*ss
;
331 result
= ssmpingd_read_msg(ss
);
334 ssmpingd_read_on(ss
);
339 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
)
341 thread_add_read(master
, ssmpingd_sock_read
, ss
, ss
->sock_fd
,
345 static struct ssmpingd_sock
*ssmpingd_new(struct pim_instance
*pim
,
346 struct in_addr source_addr
)
348 struct ssmpingd_sock
*ss
;
351 if (!pim
->ssmpingd_list
) {
352 pim
->ssmpingd_list
= list_new();
353 if (!pim
->ssmpingd_list
) {
355 "%s %s: failure: qpim_ssmpingd_list=list_new()",
356 __FILE__
, __PRETTY_FUNCTION__
);
359 pim
->ssmpingd_list
->del
= (void (*)(void *))ssmpingd_free
;
363 ssmpingd_socket(source_addr
, /* port: */ 4321, /* mTTL: */ 64);
365 char source_str
[INET_ADDRSTRLEN
];
366 pim_inet4_dump("<src?>", source_addr
, source_str
,
368 zlog_warn("%s: ssmpingd_socket() failure for source %s",
369 __PRETTY_FUNCTION__
, source_str
);
373 ss
= XCALLOC(MTYPE_PIM_SSMPINGD
, sizeof(*ss
));
375 char source_str
[INET_ADDRSTRLEN
];
376 pim_inet4_dump("<src?>", source_addr
, source_str
,
378 zlog_err("%s: XCALLOC(%zu) failure for ssmpingd source %s",
379 __PRETTY_FUNCTION__
, sizeof(*ss
), source_str
);
385 ss
->sock_fd
= sock_fd
;
386 ss
->t_sock_read
= NULL
;
387 ss
->source_addr
= source_addr
;
388 ss
->creation
= pim_time_monotonic_sec();
391 listnode_add(pim
->ssmpingd_list
, ss
);
393 ssmpingd_read_on(ss
);
398 int pim_ssmpingd_start(struct pim_instance
*pim
, struct in_addr source_addr
)
400 struct ssmpingd_sock
*ss
;
402 ss
= ssmpingd_find(pim
, source_addr
);
404 /* silently ignore request to recreate entry */
409 char source_str
[INET_ADDRSTRLEN
];
410 pim_inet4_dump("<src?>", source_addr
, source_str
,
412 zlog_info("%s: starting ssmpingd for source %s",
413 __PRETTY_FUNCTION__
, source_str
);
416 ss
= ssmpingd_new(pim
, source_addr
);
418 char source_str
[INET_ADDRSTRLEN
];
419 pim_inet4_dump("<src?>", source_addr
, source_str
,
421 zlog_warn("%s: ssmpingd_new() failure for source %s",
422 __PRETTY_FUNCTION__
, source_str
);
429 int pim_ssmpingd_stop(struct pim_instance
*pim
, struct in_addr source_addr
)
431 struct ssmpingd_sock
*ss
;
433 ss
= ssmpingd_find(pim
, source_addr
);
435 char source_str
[INET_ADDRSTRLEN
];
436 pim_inet4_dump("<src?>", source_addr
, source_str
,
438 zlog_warn("%s: could not find ssmpingd for source %s",
439 __PRETTY_FUNCTION__
, source_str
);
444 char source_str
[INET_ADDRSTRLEN
];
445 pim_inet4_dump("<src?>", source_addr
, source_str
,
447 zlog_info("%s: stopping ssmpingd for source %s",
448 __PRETTY_FUNCTION__
, source_str
);