1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2008 Everton da Silva Marques
14 #include "lib_errors.h"
17 #include "pim_instance.h"
18 #include "pim_ssmpingd.h"
24 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "232.43.211.234";
26 static const char *const PIM_SSMPINGD_REPLY_GROUP
= "ff3e::4321:1234";
29 enum { PIM_SSMPINGD_REQUEST
= 'Q', PIM_SSMPINGD_REPLY
= 'A' };
31 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
);
33 void pim_ssmpingd_init(struct pim_instance
*pim
)
37 assert(!pim
->ssmpingd_list
);
39 result
= inet_pton(PIM_AF
, PIM_SSMPINGD_REPLY_GROUP
,
40 &pim
->ssmpingd_group_addr
);
45 void pim_ssmpingd_destroy(struct pim_instance
*pim
)
47 if (pim
->ssmpingd_list
)
48 list_delete(&pim
->ssmpingd_list
);
51 static struct ssmpingd_sock
*ssmpingd_find(struct pim_instance
*pim
,
54 struct listnode
*node
;
55 struct ssmpingd_sock
*ss
;
57 if (!pim
->ssmpingd_list
)
60 for (ALL_LIST_ELEMENTS_RO(pim
->ssmpingd_list
, node
, ss
))
61 if (!pim_addr_cmp(source_addr
, ss
->source_addr
))
67 static void ssmpingd_free(struct ssmpingd_sock
*ss
)
69 XFREE(MTYPE_PIM_SSMPINGD
, ss
);
73 static inline int ssmpingd_setsockopt(int fd
, pim_addr addr
, int mttl
)
75 /* Needed to obtain destination address from recvmsg() */
76 #if defined(HAVE_IP_PKTINFO)
77 /* Linux and Solaris IP_PKTINFO */
79 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
81 "%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
82 __func__
, fd
, errno
, safe_strerror(errno
));
84 #elif defined(HAVE_IP_RECVDSTADDR)
85 /* BSD IP_RECVDSTADDR */
87 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
))) {
89 "%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
90 __func__
, fd
, errno
, safe_strerror(errno
));
95 "%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
101 if (setsockopt_ipv4_multicast_loop(fd
, 0)) {
103 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
104 __func__
, fd
, errno
, safe_strerror(errno
));
106 return PIM_SOCK_ERR_LOOP
;
109 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
, (void *)&addr
,
112 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
113 __func__
, fd
, errno
, safe_strerror(errno
));
118 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&mttl
,
121 "%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
122 __func__
, mttl
, fd
, errno
, safe_strerror(errno
));
130 static inline int ssmpingd_setsockopt(int fd
, pim_addr addr
, int mttl
)
132 setsockopt_ipv6_pktinfo(fd
, 1);
133 setsockopt_ipv6_multicast_hops(fd
, mttl
);
135 if (setsockopt_ipv6_multicast_loop(fd
, 0)) {
137 "%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
138 __func__
, fd
, errno
, safe_strerror(errno
));
140 return PIM_SOCK_ERR_LOOP
;
143 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, (void *)&addr
,
146 "%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
147 __func__
, fd
, errno
, safe_strerror(errno
));
156 static int ssmpingd_socket(pim_addr addr
, int port
, int mttl
)
158 struct sockaddr_storage sockaddr
;
161 socklen_t len
= sizeof(sockaddr
);
163 fd
= socket(PIM_AF
, SOCK_DGRAM
, IPPROTO_UDP
);
165 flog_err_sys(EC_LIB_SOCKET
,
166 "%s: could not create socket: errno=%d: %s",
167 __func__
, errno
, safe_strerror(errno
));
171 pim_socket_getsockname(fd
, (struct sockaddr
*)&sockaddr
, &len
);
173 if (bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
))) {
175 "%s: bind(fd=%d,addr=%pSUp,port=%d,len=%zu) failure: errno=%d: %s",
176 __func__
, fd
, &sockaddr
, port
, sizeof(sockaddr
), errno
,
177 safe_strerror(errno
));
183 sockopt_reuseaddr(fd
);
185 ret
= ssmpingd_setsockopt(fd
, addr
, mttl
);
187 zlog_warn("ssmpingd_setsockopt failed");
195 static void ssmpingd_delete(struct ssmpingd_sock
*ss
)
199 EVENT_OFF(ss
->t_sock_read
);
201 if (close(ss
->sock_fd
)) {
203 "%s: failure closing ssmpingd sock_fd=%d for source %pPA: errno=%d: %s",
204 __func__
, ss
->sock_fd
, &ss
->source_addr
, errno
,
205 safe_strerror(errno
));
209 listnode_delete(ss
->pim
->ssmpingd_list
, ss
);
213 static void ssmpingd_sendto(struct ssmpingd_sock
*ss
, const uint8_t *buf
,
214 int len
, struct sockaddr_storage to
)
216 socklen_t tolen
= sizeof(to
);
219 sent
= sendto(ss
->sock_fd
, buf
, len
, MSG_DONTWAIT
,
220 (struct sockaddr
*)&to
, tolen
);
224 "%s: sendto() failure to %pSUp,fd=%d len=%d: errno=%d: %s",
225 __func__
, &to
, ss
->sock_fd
, len
, errno
,
226 safe_strerror(errno
));
229 "%s: sendto() partial to %pSUp, fd=%d len=%d: sent=%d",
230 __func__
, &to
, ss
->sock_fd
, len
, sent
);
235 static int ssmpingd_read_msg(struct ssmpingd_sock
*ss
)
237 struct interface
*ifp
;
238 struct sockaddr_storage from
;
239 struct sockaddr_storage to
;
240 socklen_t fromlen
= sizeof(from
);
241 socklen_t tolen
= sizeof(to
);
242 ifindex_t ifindex
= -1;
248 len
= pim_socket_recvfromto(ss
->sock_fd
, buf
, sizeof(buf
), &from
,
249 &fromlen
, &to
, &tolen
, &ifindex
);
253 "%s: failure receiving ssmping for source %pPA on fd=%d: errno=%d: %s",
254 __func__
, &ss
->source_addr
, ss
->sock_fd
, errno
,
255 safe_strerror(errno
));
259 ifp
= if_lookup_by_index(ifindex
, ss
->pim
->vrf
->vrf_id
);
261 if (buf
[0] != PIM_SSMPINGD_REQUEST
) {
263 "%s: bad ssmping type=%d from %pSUp to %pSUp on interface %s ifindex=%d fd=%d src=%pPA",
264 __func__
, buf
[0], &from
, &to
,
265 ifp
? ifp
->name
: "<iface?>", ifindex
, ss
->sock_fd
,
270 if (PIM_DEBUG_SSMPINGD
) {
272 "%s: recv ssmping from %pSUp, to %pSUp, on interface %s ifindex=%d fd=%d src=%pPA",
273 __func__
, &from
, &to
, ifp
? ifp
->name
: "<iface?>",
274 ifindex
, ss
->sock_fd
, &ss
->source_addr
);
277 buf
[0] = PIM_SSMPINGD_REPLY
;
280 ssmpingd_sendto(ss
, buf
, len
, from
);
282 /* multicast reply */
283 memcpy(&from
, &ss
->pim
->ssmpingd_group_addr
, sizeof(pim_addr
));
284 ssmpingd_sendto(ss
, buf
, len
, from
);
289 static void ssmpingd_sock_read(struct event
*t
)
291 struct ssmpingd_sock
*ss
;
295 ssmpingd_read_msg(ss
);
298 ssmpingd_read_on(ss
);
301 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
)
303 event_add_read(router
->master
, ssmpingd_sock_read
, ss
, ss
->sock_fd
,
307 static struct ssmpingd_sock
*ssmpingd_new(struct pim_instance
*pim
,
308 pim_addr source_addr
)
310 struct ssmpingd_sock
*ss
;
313 if (!pim
->ssmpingd_list
) {
314 pim
->ssmpingd_list
= list_new();
315 pim
->ssmpingd_list
->del
= (void (*)(void *))ssmpingd_free
;
319 ssmpingd_socket(source_addr
, /* port: */ 4321, /* mTTL: */ 64);
321 zlog_warn("%s: ssmpingd_socket() failure for source %pPA",
322 __func__
, &source_addr
);
326 ss
= XCALLOC(MTYPE_PIM_SSMPINGD
, sizeof(*ss
));
329 ss
->sock_fd
= sock_fd
;
330 ss
->t_sock_read
= NULL
;
331 ss
->source_addr
= source_addr
;
332 ss
->creation
= pim_time_monotonic_sec();
335 listnode_add(pim
->ssmpingd_list
, ss
);
337 ssmpingd_read_on(ss
);
342 int pim_ssmpingd_start(struct pim_instance
*pim
, pim_addr source_addr
)
344 struct ssmpingd_sock
*ss
;
346 ss
= ssmpingd_find(pim
, source_addr
);
348 /* silently ignore request to recreate entry */
352 zlog_info("%s: starting ssmpingd for source %pPAs", __func__
,
355 ss
= ssmpingd_new(pim
, source_addr
);
357 zlog_warn("%s: ssmpingd_new() failure for source %pPAs",
358 __func__
, &source_addr
);
365 int pim_ssmpingd_stop(struct pim_instance
*pim
, pim_addr source_addr
)
367 struct ssmpingd_sock
*ss
;
369 ss
= ssmpingd_find(pim
, source_addr
);
371 zlog_warn("%s: could not find ssmpingd for source %pPAs",
372 __func__
, &source_addr
);
376 zlog_info("%s: stopping ssmpingd for source %pPAs", __func__
,