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
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,
20 $QuaggaId: $Format:%an, %ai, %h$ $
29 #include "pim_ssmpingd.h"
35 static const char * const PIM_SSMPINGD_REPLY_GROUP = "232.43.211.234";
38 PIM_SSMPINGD_REQUEST = 'Q',
39 PIM_SSMPINGD_REPLY = 'A'
42 static void ssmpingd_read_on(struct ssmpingd_sock *ss);
44 void pim_ssmpingd_init()
48 zassert(!qpim_ssmpingd_list);
50 result = inet_pton(AF_INET, PIM_SSMPINGD_REPLY_GROUP, &qpim_ssmpingd_group_addr);
55 void pim_ssmpingd_destroy()
57 if (qpim_ssmpingd_list) {
58 list_free(qpim_ssmpingd_list);
59 qpim_ssmpingd_list = 0;
63 static struct ssmpingd_sock *ssmpingd_find(struct in_addr source_addr)
65 struct listnode *node;
66 struct ssmpingd_sock *ss;
68 if (!qpim_ssmpingd_list)
71 for (ALL_LIST_ELEMENTS_RO(qpim_ssmpingd_list, node, ss))
72 if (source_addr.s_addr == ss->source_addr.s_addr)
78 static void ssmpingd_free(struct ssmpingd_sock *ss)
80 XFREE(MTYPE_PIM_SSMPINGD, ss);
83 static int ssmpingd_socket(struct in_addr addr, int port, int mttl)
85 struct sockaddr_in sockaddr;
88 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
90 zlog_err("%s: could not create socket: errno=%d: %s",
91 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
95 sockaddr.sin_family = AF_INET;
96 sockaddr.sin_addr = addr;
97 sockaddr.sin_port = htons(port);
99 if (bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
101 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
102 zlog_warn("%s: bind(fd=%d,addr=%s,port=%d,len=%zu) failure: errno=%d: %s",
104 fd, addr_str, port, sizeof(sockaddr),
105 errno, safe_strerror(errno));
110 /* Needed to obtain destination address from recvmsg() */
112 #if defined(HAVE_IP_PKTINFO)
113 /* Linux and Solaris IP_PKTINFO */
115 if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt))) {
116 zlog_warn("%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
117 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
119 #elif defined(HAVE_IP_RECVDSTADDR)
120 /* BSD IP_RECVDSTADDR */
122 if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
123 zlog_warn("%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
124 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
127 zlog_err("%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
128 __FILE__, __PRETTY_FUNCTION__);
136 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
137 (void *) &reuse, sizeof(reuse))) {
138 zlog_warn("%s: could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
139 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
145 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
146 (void *) &mttl, sizeof(mttl))) {
147 zlog_warn("%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
148 __PRETTY_FUNCTION__, mttl, fd, errno, safe_strerror(errno));
155 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
156 (void *) &loop, sizeof(loop))) {
157 zlog_warn("%s: could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
159 loop ? "enable" : "disable",
160 fd, errno, safe_strerror(errno));
162 return PIM_SOCK_ERR_LOOP;
166 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
167 (void *) &addr, sizeof(addr))) {
168 zlog_warn("%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
169 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
177 flags = fcntl(fd, F_GETFL, 0);
179 zlog_warn("%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
180 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
185 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
186 zlog_warn("%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
187 __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
196 static void ssmpingd_delete(struct ssmpingd_sock *ss)
199 zassert(qpim_ssmpingd_list);
201 THREAD_OFF(ss->t_sock_read);
203 if (close(ss->sock_fd)) {
205 char source_str[100];
206 pim_inet4_dump("<src?>", ss->source_addr, source_str, sizeof(source_str));
207 zlog_warn("%s: failure closing ssmpingd sock_fd=%d for source %s: errno=%d: %s",
209 ss->sock_fd, source_str, e, safe_strerror(e));
213 listnode_delete(qpim_ssmpingd_list, ss);
217 static void ssmpingd_sendto(struct ssmpingd_sock *ss,
220 struct sockaddr_in to)
222 socklen_t tolen = sizeof(to);
225 sent = sendto(ss->sock_fd, buf, len, MSG_DONTWAIT,
226 (struct sockaddr *)&to, tolen);
230 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
232 zlog_warn("%s: sendto() failure to %s,%d: fd=%d len=%d: errno=%d: %s",
234 to_str, ntohs(to.sin_port), ss->sock_fd, len,
235 e, safe_strerror(e));
238 zlog_warn("%s: sendto() partial to %s,%d: fd=%d len=%d: sent=%d",
240 to_str, ntohs(to.sin_port), ss->sock_fd,
246 static int ssmpingd_read_msg(struct ssmpingd_sock *ss)
248 struct interface *ifp;
249 struct sockaddr_in from;
250 struct sockaddr_in to;
251 socklen_t fromlen = sizeof(from);
252 socklen_t tolen = sizeof(to);
253 ifindex_t ifindex = -1;
259 len = pim_socket_recvfromto(ss->sock_fd, buf, sizeof(buf),
264 char source_str[100];
265 pim_inet4_dump("<src?>", ss->source_addr, source_str, sizeof(source_str));
266 zlog_warn("%s: failure receiving ssmping for source %s on fd=%d: errno=%d: %s",
267 __PRETTY_FUNCTION__, source_str, ss->sock_fd, errno, safe_strerror(errno));
271 ifp = if_lookup_by_index(ifindex);
273 if (buf[0] != PIM_SSMPINGD_REQUEST) {
274 char source_str[100];
277 pim_inet4_dump("<src?>", ss->source_addr, source_str, sizeof(source_str));
278 pim_inet4_dump("<from?>", from.sin_addr, from_str, sizeof(from_str));
279 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
280 zlog_warn("%s: bad ssmping type=%d from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
283 from_str, ntohs(from.sin_port),
284 to_str, ntohs(to.sin_port),
285 ifp ? ifp->name : "<iface?>",
286 ifindex, ss->sock_fd,
291 if (PIM_DEBUG_SSMPINGD) {
292 char source_str[100];
295 pim_inet4_dump("<src?>", ss->source_addr, source_str, sizeof(source_str));
296 pim_inet4_dump("<from?>", from.sin_addr, from_str, sizeof(from_str));
297 pim_inet4_dump("<to?>", to.sin_addr, to_str, sizeof(to_str));
298 zlog_debug("%s: recv ssmping from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
300 from_str, ntohs(from.sin_port),
301 to_str, ntohs(to.sin_port),
302 ifp ? ifp->name : "<iface?>",
303 ifindex, ss->sock_fd,
307 buf[0] = PIM_SSMPINGD_REPLY;
310 ssmpingd_sendto(ss, buf, len, from);
312 /* multicast reply */
313 from.sin_addr = qpim_ssmpingd_group_addr;
314 ssmpingd_sendto(ss, buf, len, from);
319 static int ssmpingd_sock_read(struct thread *t)
321 struct ssmpingd_sock *ss;
330 sock_fd = THREAD_FD(t);
331 zassert(sock_fd == ss->sock_fd);
333 result = ssmpingd_read_msg(ss);
337 ssmpingd_read_on(ss);
342 static void ssmpingd_read_on(struct ssmpingd_sock *ss)
344 zassert(!ss->t_sock_read);
345 THREAD_READ_ON(master, ss->t_sock_read,
346 ssmpingd_sock_read, ss, ss->sock_fd);
349 static struct ssmpingd_sock *ssmpingd_new(struct in_addr source_addr)
351 struct ssmpingd_sock *ss;
354 if (!qpim_ssmpingd_list) {
355 qpim_ssmpingd_list = list_new();
356 if (!qpim_ssmpingd_list) {
357 zlog_err("%s %s: failure: qpim_ssmpingd_list=list_new()",
358 __FILE__, __PRETTY_FUNCTION__);
361 qpim_ssmpingd_list->del = (void (*)(void *)) ssmpingd_free;
364 sock_fd = ssmpingd_socket(source_addr, /* port: */ 4321, /* mTTL: */ 64);
366 char source_str[100];
367 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
368 zlog_warn("%s: ssmpingd_socket() failure for source %s",
369 __PRETTY_FUNCTION__, source_str);
373 ss = XMALLOC(MTYPE_PIM_SSMPINGD, sizeof(*ss));
375 char source_str[100];
376 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
377 zlog_err("%s: XMALLOC(%zu) failure for ssmpingd source %s",
379 sizeof(*ss), source_str);
384 ss->sock_fd = sock_fd;
386 ss->source_addr = source_addr;
387 ss->creation = pim_time_monotonic_sec();
390 listnode_add(qpim_ssmpingd_list, ss);
392 ssmpingd_read_on(ss);
397 int pim_ssmpingd_start(struct in_addr source_addr)
399 struct ssmpingd_sock *ss;
401 ss = ssmpingd_find(source_addr);
403 /* silently ignore request to recreate entry */
408 char source_str[100];
409 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
410 zlog_info("%s: starting ssmpingd for source %s",
411 __PRETTY_FUNCTION__, source_str);
414 ss = ssmpingd_new(source_addr);
416 char source_str[100];
417 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
418 zlog_warn("%s: ssmpingd_new() failure for source %s",
419 __PRETTY_FUNCTION__, source_str);
426 int pim_ssmpingd_stop(struct in_addr source_addr)
428 struct ssmpingd_sock *ss;
430 ss = ssmpingd_find(source_addr);
432 char source_str[100];
433 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
434 zlog_warn("%s: could not find ssmpingd for source %s",
435 __PRETTY_FUNCTION__, source_str);
440 char source_str[100];
441 pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
442 zlog_info("%s: stopping ssmpingd for source %s",
443 __PRETTY_FUNCTION__, source_str);