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
27 #include "lib_errors.h"
30 #include "pim_instance.h"
31 #include "pim_ssmpingd.h"
37 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "232.43.211.234";
39 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "ff3e::4321:1234";
42 enum { PIM_SSMPINGD_REQUEST
= 'Q', PIM_SSMPINGD_REPLY
= 'A' };
44 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
);
46 void pim_ssmpingd_init(struct pim_instance
*pim
)
50 assert(!pim
->ssmpingd_list
);
52 result
= inet_pton(PIM_AF
, PIM_SSMPINGD_REPLY_GROUP
,
53 &pim
->ssmpingd_group_addr
);
58 void pim_ssmpingd_destroy(struct pim_instance
*pim
)
60 if (pim
->ssmpingd_list
)
61 list_delete(&pim
->ssmpingd_list
);
64 static struct ssmpingd_sock
*ssmpingd_find(struct pim_instance
*pim
,
67 struct listnode
*node
;
68 struct ssmpingd_sock
*ss
;
70 if (!pim
->ssmpingd_list
)
73 for (ALL_LIST_ELEMENTS_RO(pim
->ssmpingd_list
, node
, ss
))
74 if (!pim_addr_cmp(source_addr
, ss
->source_addr
))
80 static void ssmpingd_free(struct ssmpingd_sock
*ss
)
82 XFREE(MTYPE_PIM_SSMPINGD
, ss
);
86 static inline int ssmpingd_setsockopt(int fd
, pim_addr addr
, int mttl
)
88 /* Needed to obtain destination address from recvmsg() */
89 #if defined(HAVE_IP_PKTINFO)
90 /* Linux and Solaris IP_PKTINFO */
92 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
94 "%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
95 __func__
, fd
, errno
, safe_strerror(errno
));
97 #elif defined(HAVE_IP_RECVDSTADDR)
98 /* BSD IP_RECVDSTADDR */
100 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
))) {
102 "%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
103 __func__
, fd
, errno
, safe_strerror(errno
));
108 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
114 if (setsockopt_ipv4_multicast_loop(fd
, 0)) {
116 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
117 __func__
, fd
, errno
, safe_strerror(errno
));
119 return PIM_SOCK_ERR_LOOP
;
122 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&addr
,
125 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
126 __func__
, fd
, errno
, safe_strerror(errno
));
131 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&mttl
,
134 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
135 __func__
, mttl
, fd
, errno
, safe_strerror(errno
));
143 static inline int ssmpingd_setsockopt(int fd
, pim_addr addr
, int mttl
)
145 setsockopt_ipv6_pktinfo(fd
, 1);
146 setsockopt_ipv6_multicast_hops(fd
, mttl
);
148 if (setsockopt_ipv6_multicast_loop(fd
, 0)) {
150 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
151 __func__
, fd
, errno
, safe_strerror(errno
));
153 return PIM_SOCK_ERR_LOOP
;
156 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, (void *)&addr
,
159 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
160 __func__
, fd
, errno
, safe_strerror(errno
));
169 static int ssmpingd_socket(pim_addr addr
, int port
, int mttl
)
171 struct sockaddr_storage sockaddr
;
174 socklen_t len
= sizeof(sockaddr
);
176 fd
= socket(PIM_AF
, SOCK_DGRAM
, IPPROTO_UDP
);
178 flog_err_sys(EC_LIB_SOCKET
,
179 "%s: could not create socket: errno=%d: %s",
180 __func__
, errno
, safe_strerror(errno
));
184 pim_socket_getsockname(fd
, (struct sockaddr
*)&sockaddr
, &len
);
186 if (bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
))) {
188 "%s: bind(fd=%d,addr=%pSUp,port=%d,len=%zu) failure: errno=%d: %s",
189 __func__
, fd
, &sockaddr
, port
, sizeof(sockaddr
), errno
,
190 safe_strerror(errno
));
196 sockopt_reuseaddr(fd
);
198 ret
= ssmpingd_setsockopt(fd
, addr
, mttl
);
200 zlog_warn("ssmpingd_setsockopt failed");
208 static void ssmpingd_delete(struct ssmpingd_sock
*ss
)
212 THREAD_OFF(ss
->t_sock_read
);
214 if (close(ss
->sock_fd
)) {
216 "%s: failure closing ssmpingd sock_fd=%d for source %pPA: errno=%d: %s",
217 __func__
, ss
->sock_fd
, &ss
->source_addr
, errno
,
218 safe_strerror(errno
));
222 listnode_delete(ss
->pim
->ssmpingd_list
, ss
);
226 static void ssmpingd_sendto(struct ssmpingd_sock
*ss
, const uint8_t *buf
,
227 int len
, struct sockaddr_storage to
)
229 socklen_t tolen
= sizeof(to
);
232 sent
= sendto(ss
->sock_fd
, buf
, len
, MSG_DONTWAIT
,
233 (struct sockaddr
*)&to
, tolen
);
237 "%s: sendto() failure to %pSUp,fd=%d len=%d: errno=%d: %s",
238 __func__
, &to
, ss
->sock_fd
, len
, errno
,
239 safe_strerror(errno
));
242 "%s: sendto() partial to %pSUp, fd=%d len=%d: sent=%d",
243 __func__
, &to
, ss
->sock_fd
, len
, sent
);
248 static int ssmpingd_read_msg(struct ssmpingd_sock
*ss
)
250 struct interface
*ifp
;
251 struct sockaddr_storage from
;
252 struct sockaddr_storage to
;
253 socklen_t fromlen
= sizeof(from
);
254 socklen_t tolen
= sizeof(to
);
255 ifindex_t ifindex
= -1;
261 len
= pim_socket_recvfromto(ss
->sock_fd
, buf
, sizeof(buf
), &from
,
262 &fromlen
, &to
, &tolen
, &ifindex
);
266 "%s: failure receiving ssmping for source %pPA on fd=%d: errno=%d: %s",
267 __func__
, &ss
->source_addr
, ss
->sock_fd
, errno
,
268 safe_strerror(errno
));
272 ifp
= if_lookup_by_index(ifindex
, ss
->pim
->vrf
->vrf_id
);
274 if (buf
[0] != PIM_SSMPINGD_REQUEST
) {
276 "%s: bad ssmping type=%d from %pSUp to %pSUp on interface %s ifindex=%d fd=%d src=%pPA",
277 __func__
, buf
[0], &from
, &to
,
278 ifp
? ifp
->name
: "<iface?>", ifindex
, ss
->sock_fd
,
283 if (PIM_DEBUG_SSMPINGD
) {
285 "%s: recv ssmping from %pSUp, to %pSUp, on interface %s ifindex=%d fd=%d src=%pPA",
286 __func__
, &from
, &to
, ifp
? ifp
->name
: "<iface?>",
287 ifindex
, ss
->sock_fd
, &ss
->source_addr
);
290 buf
[0] = PIM_SSMPINGD_REPLY
;
293 ssmpingd_sendto(ss
, buf
, len
, from
);
295 /* multicast reply */
296 memcpy(&from
, &ss
->pim
->ssmpingd_group_addr
, sizeof(pim_addr
));
297 ssmpingd_sendto(ss
, buf
, len
, from
);
302 static void ssmpingd_sock_read(struct thread
*t
)
304 struct ssmpingd_sock
*ss
;
308 ssmpingd_read_msg(ss
);
311 ssmpingd_read_on(ss
);
314 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
)
316 thread_add_read(router
->master
, ssmpingd_sock_read
, ss
, ss
->sock_fd
,
320 static struct ssmpingd_sock
*ssmpingd_new(struct pim_instance
*pim
,
321 pim_addr source_addr
)
323 struct ssmpingd_sock
*ss
;
326 if (!pim
->ssmpingd_list
) {
327 pim
->ssmpingd_list
= list_new();
328 pim
->ssmpingd_list
->del
= (void (*)(void *))ssmpingd_free
;
332 ssmpingd_socket(source_addr
, /* port: */ 4321, /* mTTL: */ 64);
334 zlog_warn("%s: ssmpingd_socket() failure for source %pPA",
335 __func__
, &source_addr
);
339 ss
= XCALLOC(MTYPE_PIM_SSMPINGD
, sizeof(*ss
));
342 ss
->sock_fd
= sock_fd
;
343 ss
->t_sock_read
= NULL
;
344 ss
->source_addr
= source_addr
;
345 ss
->creation
= pim_time_monotonic_sec();
348 listnode_add(pim
->ssmpingd_list
, ss
);
350 ssmpingd_read_on(ss
);
355 int pim_ssmpingd_start(struct pim_instance
*pim
, pim_addr source_addr
)
357 struct ssmpingd_sock
*ss
;
359 ss
= ssmpingd_find(pim
, source_addr
);
361 /* silently ignore request to recreate entry */
365 zlog_info("%s: starting ssmpingd for source %pPAs", __func__
,
368 ss
= ssmpingd_new(pim
, source_addr
);
370 zlog_warn("%s: ssmpingd_new() failure for source %pPAs",
371 __func__
, &source_addr
);
378 int pim_ssmpingd_stop(struct pim_instance
*pim
, pim_addr source_addr
)
380 struct ssmpingd_sock
*ss
;
382 ss
= ssmpingd_find(pim
, source_addr
);
384 zlog_warn("%s: could not find ssmpingd for source %pPAs",
385 __func__
, &source_addr
);
389 zlog_info("%s: stopping ssmpingd for source %pPAs", __func__
,