]>
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";
36 PIM_SSMPINGD_REQUEST
= 'Q',
37 PIM_SSMPINGD_REPLY
= 'A'
40 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
);
42 void pim_ssmpingd_init()
46 zassert(!qpim_ssmpingd_list
);
48 result
= inet_pton(AF_INET
, PIM_SSMPINGD_REPLY_GROUP
, &qpim_ssmpingd_group_addr
);
53 void pim_ssmpingd_destroy()
55 if (qpim_ssmpingd_list
) {
56 list_free(qpim_ssmpingd_list
);
57 qpim_ssmpingd_list
= 0;
61 static struct ssmpingd_sock
*ssmpingd_find(struct in_addr source_addr
)
63 struct listnode
*node
;
64 struct ssmpingd_sock
*ss
;
66 if (!qpim_ssmpingd_list
)
69 for (ALL_LIST_ELEMENTS_RO(qpim_ssmpingd_list
, node
, ss
))
70 if (source_addr
.s_addr
== ss
->source_addr
.s_addr
)
76 static void ssmpingd_free(struct ssmpingd_sock
*ss
)
78 XFREE(MTYPE_PIM_SSMPINGD
, ss
);
81 static int ssmpingd_socket(struct in_addr addr
, int port
, int mttl
)
83 struct sockaddr_in sockaddr
;
86 fd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
88 zlog_err("%s: could not create socket: errno=%d: %s",
89 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
93 sockaddr
.sin_family
= AF_INET
;
94 sockaddr
.sin_addr
= addr
;
95 sockaddr
.sin_port
= htons(port
);
97 if (bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
))) {
98 char addr_str
[INET_ADDRSTRLEN
];
99 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
100 zlog_warn("%s: bind(fd=%d,addr=%s,port=%d,len=%zu) failure: errno=%d: %s",
102 fd
, addr_str
, port
, sizeof(sockaddr
),
103 errno
, safe_strerror(errno
));
108 /* Needed to obtain destination address from recvmsg() */
110 #if defined(HAVE_IP_PKTINFO)
111 /* Linux and Solaris IP_PKTINFO */
113 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
114 zlog_warn("%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
115 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
117 #elif defined(HAVE_IP_RECVDSTADDR)
118 /* BSD IP_RECVDSTADDR */
120 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
))) {
121 zlog_warn("%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
122 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
125 zlog_err("%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
126 __FILE__
, __PRETTY_FUNCTION__
);
134 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
135 (void *) &reuse
, sizeof(reuse
))) {
136 zlog_warn("%s: could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
137 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
143 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
,
144 (void *) &mttl
, sizeof(mttl
))) {
145 zlog_warn("%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
146 __PRETTY_FUNCTION__
, mttl
, fd
, errno
, safe_strerror(errno
));
151 if (setsockopt_ipv4_multicast_loop (fd
, 0)) {
152 zlog_warn("%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
154 fd
, errno
, safe_strerror(errno
));
156 return PIM_SOCK_ERR_LOOP
;
159 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
160 (void *) &addr
, sizeof(addr
))) {
161 zlog_warn("%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
162 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
170 flags
= fcntl(fd
, F_GETFL
, 0);
172 zlog_warn("%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
173 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
178 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
)) {
179 zlog_warn("%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
180 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
189 static void ssmpingd_delete(struct ssmpingd_sock
*ss
)
192 zassert(qpim_ssmpingd_list
);
194 THREAD_OFF(ss
->t_sock_read
);
196 if (close(ss
->sock_fd
)) {
197 char source_str
[INET_ADDRSTRLEN
];
198 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
199 zlog_warn("%s: failure closing ssmpingd sock_fd=%d for source %s: errno=%d: %s",
201 ss
->sock_fd
, source_str
, errno
, safe_strerror(errno
));
205 listnode_delete(qpim_ssmpingd_list
, ss
);
209 static void ssmpingd_sendto(struct ssmpingd_sock
*ss
,
212 struct sockaddr_in to
)
214 socklen_t tolen
= sizeof(to
);
217 sent
= sendto(ss
->sock_fd
, buf
, len
, MSG_DONTWAIT
,
218 (struct sockaddr
*)&to
, tolen
);
220 char to_str
[INET_ADDRSTRLEN
];
221 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
223 zlog_warn("%s: sendto() failure to %s,%d: fd=%d len=%d: errno=%d: %s",
225 to_str
, ntohs(to
.sin_port
), ss
->sock_fd
, len
,
226 errno
, safe_strerror(errno
));
229 zlog_warn("%s: sendto() partial to %s,%d: fd=%d len=%d: sent=%d",
231 to_str
, ntohs(to
.sin_port
), ss
->sock_fd
,
237 static int ssmpingd_read_msg(struct ssmpingd_sock
*ss
)
239 struct interface
*ifp
;
240 struct sockaddr_in from
;
241 struct sockaddr_in to
;
242 socklen_t fromlen
= sizeof(from
);
243 socklen_t tolen
= sizeof(to
);
244 ifindex_t ifindex
= -1;
250 len
= pim_socket_recvfromto(ss
->sock_fd
, buf
, sizeof(buf
),
255 char source_str
[INET_ADDRSTRLEN
];
256 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
257 zlog_warn("%s: failure receiving ssmping for source %s on fd=%d: errno=%d: %s",
258 __PRETTY_FUNCTION__
, source_str
, ss
->sock_fd
, errno
, safe_strerror(errno
));
262 ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
264 if (buf
[0] != PIM_SSMPINGD_REQUEST
) {
265 char source_str
[INET_ADDRSTRLEN
];
266 char from_str
[INET_ADDRSTRLEN
];
267 char to_str
[INET_ADDRSTRLEN
];
268 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
269 pim_inet4_dump("<from?>", from
.sin_addr
, from_str
, sizeof(from_str
));
270 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
271 zlog_warn("%s: bad ssmping type=%d from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
274 from_str
, ntohs(from
.sin_port
),
275 to_str
, ntohs(to
.sin_port
),
276 ifp
? ifp
->name
: "<iface?>",
277 ifindex
, ss
->sock_fd
,
282 if (PIM_DEBUG_SSMPINGD
) {
283 char source_str
[INET_ADDRSTRLEN
];
284 char from_str
[INET_ADDRSTRLEN
];
285 char to_str
[INET_ADDRSTRLEN
];
286 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
287 pim_inet4_dump("<from?>", from
.sin_addr
, from_str
, sizeof(from_str
));
288 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
289 zlog_debug("%s: recv ssmping from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
291 from_str
, ntohs(from
.sin_port
),
292 to_str
, ntohs(to
.sin_port
),
293 ifp
? ifp
->name
: "<iface?>",
294 ifindex
, ss
->sock_fd
,
298 buf
[0] = PIM_SSMPINGD_REPLY
;
301 ssmpingd_sendto(ss
, buf
, len
, from
);
303 /* multicast reply */
304 from
.sin_addr
= qpim_ssmpingd_group_addr
;
305 ssmpingd_sendto(ss
, buf
, len
, from
);
310 static int ssmpingd_sock_read(struct thread
*t
)
312 struct ssmpingd_sock
*ss
;
318 sock_fd
= THREAD_FD(t
);
319 zassert(sock_fd
== ss
->sock_fd
);
321 result
= ssmpingd_read_msg(ss
);
325 ssmpingd_read_on(ss
);
330 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
)
332 zassert(!ss
->t_sock_read
);
333 thread_add_read(master
, ssmpingd_sock_read
, ss
, ss
->sock_fd
,
337 static struct ssmpingd_sock
*ssmpingd_new(struct in_addr source_addr
)
339 struct ssmpingd_sock
*ss
;
342 if (!qpim_ssmpingd_list
) {
343 qpim_ssmpingd_list
= list_new();
344 if (!qpim_ssmpingd_list
) {
345 zlog_err("%s %s: failure: qpim_ssmpingd_list=list_new()",
346 __FILE__
, __PRETTY_FUNCTION__
);
349 qpim_ssmpingd_list
->del
= (void (*)(void *)) ssmpingd_free
;
352 sock_fd
= ssmpingd_socket(source_addr
, /* port: */ 4321, /* mTTL: */ 64);
354 char source_str
[INET_ADDRSTRLEN
];
355 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
356 zlog_warn("%s: ssmpingd_socket() failure for source %s",
357 __PRETTY_FUNCTION__
, source_str
);
361 ss
= XCALLOC(MTYPE_PIM_SSMPINGD
, sizeof(*ss
));
363 char source_str
[INET_ADDRSTRLEN
];
364 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
365 zlog_err("%s: XCALLOC(%zu) failure for ssmpingd source %s",
367 sizeof(*ss
), source_str
);
372 ss
->sock_fd
= sock_fd
;
374 ss
->source_addr
= source_addr
;
375 ss
->creation
= pim_time_monotonic_sec();
378 listnode_add(qpim_ssmpingd_list
, ss
);
380 ssmpingd_read_on(ss
);
385 int pim_ssmpingd_start(struct in_addr source_addr
)
387 struct ssmpingd_sock
*ss
;
389 ss
= ssmpingd_find(source_addr
);
391 /* silently ignore request to recreate entry */
396 char source_str
[INET_ADDRSTRLEN
];
397 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
398 zlog_info("%s: starting ssmpingd for source %s",
399 __PRETTY_FUNCTION__
, source_str
);
402 ss
= ssmpingd_new(source_addr
);
404 char source_str
[INET_ADDRSTRLEN
];
405 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
406 zlog_warn("%s: ssmpingd_new() failure for source %s",
407 __PRETTY_FUNCTION__
, source_str
);
414 int pim_ssmpingd_stop(struct in_addr source_addr
)
416 struct ssmpingd_sock
*ss
;
418 ss
= ssmpingd_find(source_addr
);
420 char source_str
[INET_ADDRSTRLEN
];
421 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
422 zlog_warn("%s: could not find ssmpingd for source %s",
423 __PRETTY_FUNCTION__
, source_str
);
428 char source_str
[INET_ADDRSTRLEN
];
429 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
430 zlog_info("%s: stopping ssmpingd for source %s",
431 __PRETTY_FUNCTION__
, source_str
);