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_ssmpingd.h"
36 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "232.43.211.234";
38 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "ff3e::4321:1234";
41 enum { PIM_SSMPINGD_REQUEST
= 'Q', PIM_SSMPINGD_REPLY
= 'A' };
43 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
);
45 void pim_ssmpingd_init(struct pim_instance
*pim
)
49 assert(!pim
->ssmpingd_list
);
51 result
= inet_pton(PIM_AF
, PIM_SSMPINGD_REPLY_GROUP
,
52 &pim
->ssmpingd_group_addr
);
57 void pim_ssmpingd_destroy(struct pim_instance
*pim
)
59 if (pim
->ssmpingd_list
)
60 list_delete(&pim
->ssmpingd_list
);
63 static struct ssmpingd_sock
*ssmpingd_find(struct pim_instance
*pim
,
66 struct listnode
*node
;
67 struct ssmpingd_sock
*ss
;
69 if (!pim
->ssmpingd_list
)
72 for (ALL_LIST_ELEMENTS_RO(pim
->ssmpingd_list
, node
, ss
))
73 if (!pim_addr_cmp(source_addr
, ss
->source_addr
))
79 static void ssmpingd_free(struct ssmpingd_sock
*ss
)
81 XFREE(MTYPE_PIM_SSMPINGD
, ss
);
85 static inline int ssmpingd_setsockopt(int fd
, pim_addr addr
, int mttl
)
87 /* Needed to obtain destination address from recvmsg() */
88 #if defined(HAVE_IP_PKTINFO)
89 /* Linux and Solaris IP_PKTINFO */
91 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
93 "%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
94 __func__
, fd
, errno
, safe_strerror(errno
));
96 #elif defined(HAVE_IP_RECVDSTADDR)
97 /* BSD IP_RECVDSTADDR */
99 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
))) {
101 "%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
102 __func__
, fd
, errno
, safe_strerror(errno
));
107 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
113 if (setsockopt_ipv4_multicast_loop(fd
, 0)) {
115 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
116 __func__
, fd
, errno
, safe_strerror(errno
));
118 return PIM_SOCK_ERR_LOOP
;
121 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&addr
,
124 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
125 __func__
, fd
, errno
, safe_strerror(errno
));
130 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&mttl
,
133 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
134 __func__
, mttl
, fd
, errno
, safe_strerror(errno
));
142 static inline int ssmpingd_setsockopt(int fd
, pim_addr addr
, int mttl
)
144 setsockopt_ipv6_pktinfo(fd
, 1);
145 setsockopt_ipv6_multicast_hops(fd
, mttl
);
147 if (setsockopt_ipv6_multicast_loop(fd
, 0)) {
149 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
150 __func__
, fd
, errno
, safe_strerror(errno
));
152 return PIM_SOCK_ERR_LOOP
;
155 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, (void *)&addr
,
158 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
159 __func__
, fd
, errno
, safe_strerror(errno
));
168 static int ssmpingd_socket(pim_addr addr
, int port
, int mttl
)
170 struct sockaddr_storage sockaddr
;
173 socklen_t len
= sizeof(sockaddr
);
175 fd
= socket(PIM_AF
, SOCK_DGRAM
, IPPROTO_UDP
);
177 flog_err_sys(EC_LIB_SOCKET
,
178 "%s: could not create socket: errno=%d: %s",
179 __func__
, errno
, safe_strerror(errno
));
183 pim_socket_getsockname(fd
, (struct sockaddr
*)&sockaddr
, &len
);
185 if (bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
))) {
187 "%s: bind(fd=%d,addr=%pSUp,port=%d,len=%zu) failure: errno=%d: %s",
188 __func__
, fd
, &sockaddr
, port
, sizeof(sockaddr
), errno
,
189 safe_strerror(errno
));
195 sockopt_reuseaddr(fd
);
197 ret
= ssmpingd_setsockopt(fd
, addr
, mttl
);
199 zlog_warn("ssmpingd_setsockopt failed");
207 static void ssmpingd_delete(struct ssmpingd_sock
*ss
)
211 THREAD_OFF(ss
->t_sock_read
);
213 if (close(ss
->sock_fd
)) {
215 "%s: failure closing ssmpingd sock_fd=%d for source %pPA: errno=%d: %s",
216 __func__
, ss
->sock_fd
, &ss
->source_addr
, errno
,
217 safe_strerror(errno
));
221 listnode_delete(ss
->pim
->ssmpingd_list
, ss
);
225 static void ssmpingd_sendto(struct ssmpingd_sock
*ss
, const uint8_t *buf
,
226 int len
, struct sockaddr_storage to
)
228 socklen_t tolen
= sizeof(to
);
231 sent
= sendto(ss
->sock_fd
, buf
, len
, MSG_DONTWAIT
,
232 (struct sockaddr
*)&to
, tolen
);
236 "%s: sendto() failure to %pSUp,fd=%d len=%d: errno=%d: %s",
237 __func__
, &to
, ss
->sock_fd
, len
, errno
,
238 safe_strerror(errno
));
241 "%s: sendto() partial to %pSUp, fd=%d len=%d: sent=%d",
242 __func__
, &to
, ss
->sock_fd
, len
, sent
);
247 static int ssmpingd_read_msg(struct ssmpingd_sock
*ss
)
249 struct interface
*ifp
;
250 struct sockaddr_storage from
;
251 struct sockaddr_storage to
;
252 socklen_t fromlen
= sizeof(from
);
253 socklen_t tolen
= sizeof(to
);
254 ifindex_t ifindex
= -1;
260 len
= pim_socket_recvfromto(ss
->sock_fd
, buf
, sizeof(buf
), &from
,
261 &fromlen
, &to
, &tolen
, &ifindex
);
265 "%s: failure receiving ssmping for source %pPA on fd=%d: errno=%d: %s",
266 __func__
, &ss
->source_addr
, ss
->sock_fd
, errno
,
267 safe_strerror(errno
));
271 ifp
= if_lookup_by_index(ifindex
, ss
->pim
->vrf
->vrf_id
);
273 if (buf
[0] != PIM_SSMPINGD_REQUEST
) {
275 "%s: bad ssmping type=%d from %pSUp to %pSUp on interface %s ifindex=%d fd=%d src=%pPA",
276 __func__
, buf
[0], &from
, &to
,
277 ifp
? ifp
->name
: "<iface?>", ifindex
, ss
->sock_fd
,
282 if (PIM_DEBUG_SSMPINGD
) {
284 "%s: recv ssmping from %pSUp, to %pSUp, on interface %s ifindex=%d fd=%d src=%pPA",
285 __func__
, &from
, &to
, ifp
? ifp
->name
: "<iface?>",
286 ifindex
, ss
->sock_fd
, &ss
->source_addr
);
289 buf
[0] = PIM_SSMPINGD_REPLY
;
292 ssmpingd_sendto(ss
, buf
, len
, from
);
294 /* multicast reply */
295 memcpy(&from
, &ss
->pim
->ssmpingd_group_addr
, sizeof(pim_addr
));
296 ssmpingd_sendto(ss
, buf
, len
, from
);
301 static void ssmpingd_sock_read(struct thread
*t
)
303 struct ssmpingd_sock
*ss
;
307 ssmpingd_read_msg(ss
);
310 ssmpingd_read_on(ss
);
313 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
)
315 thread_add_read(router
->master
, ssmpingd_sock_read
, ss
, ss
->sock_fd
,
319 static struct ssmpingd_sock
*ssmpingd_new(struct pim_instance
*pim
,
320 pim_addr source_addr
)
322 struct ssmpingd_sock
*ss
;
325 if (!pim
->ssmpingd_list
) {
326 pim
->ssmpingd_list
= list_new();
327 pim
->ssmpingd_list
->del
= (void (*)(void *))ssmpingd_free
;
331 ssmpingd_socket(source_addr
, /* port: */ 4321, /* mTTL: */ 64);
333 zlog_warn("%s: ssmpingd_socket() failure for source %pPA",
334 __func__
, &source_addr
);
338 ss
= XCALLOC(MTYPE_PIM_SSMPINGD
, sizeof(*ss
));
341 ss
->sock_fd
= sock_fd
;
342 ss
->t_sock_read
= NULL
;
343 ss
->source_addr
= source_addr
;
344 ss
->creation
= pim_time_monotonic_sec();
347 listnode_add(pim
->ssmpingd_list
, ss
);
349 ssmpingd_read_on(ss
);
354 int pim_ssmpingd_start(struct pim_instance
*pim
, pim_addr source_addr
)
356 struct ssmpingd_sock
*ss
;
358 ss
= ssmpingd_find(pim
, source_addr
);
360 /* silently ignore request to recreate entry */
365 zlog_info("%s: starting ssmpingd for source %pPAs", __func__
,
369 ss
= ssmpingd_new(pim
, source_addr
);
371 zlog_warn("%s: ssmpingd_new() failure for source %pPAs",
372 __func__
, &source_addr
);
379 int pim_ssmpingd_stop(struct pim_instance
*pim
, pim_addr source_addr
)
381 struct ssmpingd_sock
*ss
;
383 ss
= ssmpingd_find(pim
, source_addr
);
385 zlog_warn("%s: could not find ssmpingd for source %pPAs",
386 __func__
, &source_addr
);
390 zlog_info("%s: stopping ssmpingd for source %pPAs", __func__
,