]>
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
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,
30 #include "pim_ssmpingd.h"
34 static const char * const PIM_SSMPINGD_REPLY_GROUP
= "232.43.211.234";
37 PIM_SSMPINGD_REQUEST
= 'Q',
38 PIM_SSMPINGD_REPLY
= 'A'
41 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
);
43 void pim_ssmpingd_init()
47 zassert(!qpim_ssmpingd_list
);
49 result
= inet_pton(AF_INET
, PIM_SSMPINGD_REPLY_GROUP
, &qpim_ssmpingd_group_addr
);
54 void pim_ssmpingd_destroy()
56 if (qpim_ssmpingd_list
) {
57 list_free(qpim_ssmpingd_list
);
58 qpim_ssmpingd_list
= 0;
62 static struct ssmpingd_sock
*ssmpingd_find(struct in_addr source_addr
)
64 struct listnode
*node
;
65 struct ssmpingd_sock
*ss
;
67 if (!qpim_ssmpingd_list
)
70 for (ALL_LIST_ELEMENTS_RO(qpim_ssmpingd_list
, node
, ss
))
71 if (source_addr
.s_addr
== ss
->source_addr
.s_addr
)
77 static void ssmpingd_free(struct ssmpingd_sock
*ss
)
79 XFREE(MTYPE_PIM_SSMPINGD
, ss
);
82 static int ssmpingd_socket(struct in_addr addr
, int port
, int mttl
)
84 struct sockaddr_in sockaddr
;
87 fd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
89 zlog_err("%s: could not create socket: errno=%d: %s",
90 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
94 sockaddr
.sin_family
= AF_INET
;
95 sockaddr
.sin_addr
= addr
;
96 sockaddr
.sin_port
= htons(port
);
98 if (bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
))) {
99 char addr_str
[INET_ADDRSTRLEN
];
100 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
101 zlog_warn("%s: bind(fd=%d,addr=%s,port=%d,len=%zu) failure: errno=%d: %s",
103 fd
, addr_str
, port
, sizeof(sockaddr
),
104 errno
, safe_strerror(errno
));
109 /* Needed to obtain destination address from recvmsg() */
111 #if defined(HAVE_IP_PKTINFO)
112 /* Linux and Solaris IP_PKTINFO */
114 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
))) {
115 zlog_warn("%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
116 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
118 #elif defined(HAVE_IP_RECVDSTADDR)
119 /* BSD IP_RECVDSTADDR */
121 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
))) {
122 zlog_warn("%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
123 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
126 zlog_err("%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
127 __FILE__
, __PRETTY_FUNCTION__
);
135 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
136 (void *) &reuse
, sizeof(reuse
))) {
137 zlog_warn("%s: could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
138 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
144 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_TTL
,
145 (void *) &mttl
, sizeof(mttl
))) {
146 zlog_warn("%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
147 __PRETTY_FUNCTION__
, mttl
, fd
, errno
, safe_strerror(errno
));
152 if (setsockopt_ipv4_multicast_loop (fd
, 0)) {
153 zlog_warn("%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
155 fd
, errno
, safe_strerror(errno
));
157 return PIM_SOCK_ERR_LOOP
;
160 if (setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
161 (void *) &addr
, sizeof(addr
))) {
162 zlog_warn("%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
163 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
171 flags
= fcntl(fd
, F_GETFL
, 0);
173 zlog_warn("%s: could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
174 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
179 if (fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
)) {
180 zlog_warn("%s: could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
181 __PRETTY_FUNCTION__
, fd
, errno
, safe_strerror(errno
));
190 static void ssmpingd_delete(struct ssmpingd_sock
*ss
)
193 zassert(qpim_ssmpingd_list
);
195 THREAD_OFF(ss
->t_sock_read
);
197 if (close(ss
->sock_fd
)) {
198 char source_str
[INET_ADDRSTRLEN
];
199 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
200 zlog_warn("%s: failure closing ssmpingd sock_fd=%d for source %s: errno=%d: %s",
202 ss
->sock_fd
, source_str
, errno
, safe_strerror(errno
));
206 listnode_delete(qpim_ssmpingd_list
, ss
);
210 static void ssmpingd_sendto(struct ssmpingd_sock
*ss
,
213 struct sockaddr_in to
)
215 socklen_t tolen
= sizeof(to
);
218 sent
= sendto(ss
->sock_fd
, buf
, len
, MSG_DONTWAIT
,
219 (struct sockaddr
*)&to
, tolen
);
221 char to_str
[INET_ADDRSTRLEN
];
222 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
224 zlog_warn("%s: sendto() failure to %s,%d: fd=%d len=%d: errno=%d: %s",
226 to_str
, ntohs(to
.sin_port
), ss
->sock_fd
, len
,
227 errno
, safe_strerror(errno
));
230 zlog_warn("%s: sendto() partial to %s,%d: fd=%d len=%d: sent=%d",
232 to_str
, ntohs(to
.sin_port
), ss
->sock_fd
,
238 static int ssmpingd_read_msg(struct ssmpingd_sock
*ss
)
240 struct interface
*ifp
;
241 struct sockaddr_in from
;
242 struct sockaddr_in to
;
243 socklen_t fromlen
= sizeof(from
);
244 socklen_t tolen
= sizeof(to
);
245 ifindex_t ifindex
= -1;
251 len
= pim_socket_recvfromto(ss
->sock_fd
, buf
, sizeof(buf
),
256 char source_str
[INET_ADDRSTRLEN
];
257 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
258 zlog_warn("%s: failure receiving ssmping for source %s on fd=%d: errno=%d: %s",
259 __PRETTY_FUNCTION__
, source_str
, ss
->sock_fd
, errno
, safe_strerror(errno
));
263 ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
265 if (buf
[0] != PIM_SSMPINGD_REQUEST
) {
266 char source_str
[INET_ADDRSTRLEN
];
267 char from_str
[INET_ADDRSTRLEN
];
268 char to_str
[INET_ADDRSTRLEN
];
269 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
270 pim_inet4_dump("<from?>", from
.sin_addr
, from_str
, sizeof(from_str
));
271 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
272 zlog_warn("%s: bad ssmping type=%d from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
275 from_str
, ntohs(from
.sin_port
),
276 to_str
, ntohs(to
.sin_port
),
277 ifp
? ifp
->name
: "<iface?>",
278 ifindex
, ss
->sock_fd
,
283 if (PIM_DEBUG_SSMPINGD
) {
284 char source_str
[INET_ADDRSTRLEN
];
285 char from_str
[INET_ADDRSTRLEN
];
286 char to_str
[INET_ADDRSTRLEN
];
287 pim_inet4_dump("<src?>", ss
->source_addr
, source_str
, sizeof(source_str
));
288 pim_inet4_dump("<from?>", from
.sin_addr
, from_str
, sizeof(from_str
));
289 pim_inet4_dump("<to?>", to
.sin_addr
, to_str
, sizeof(to_str
));
290 zlog_debug("%s: recv ssmping from %s,%d to %s,%d on interface %s ifindex=%d fd=%d src=%s",
292 from_str
, ntohs(from
.sin_port
),
293 to_str
, ntohs(to
.sin_port
),
294 ifp
? ifp
->name
: "<iface?>",
295 ifindex
, ss
->sock_fd
,
299 buf
[0] = PIM_SSMPINGD_REPLY
;
302 ssmpingd_sendto(ss
, buf
, len
, from
);
304 /* multicast reply */
305 from
.sin_addr
= qpim_ssmpingd_group_addr
;
306 ssmpingd_sendto(ss
, buf
, len
, from
);
311 static int ssmpingd_sock_read(struct thread
*t
)
313 struct ssmpingd_sock
*ss
;
319 sock_fd
= THREAD_FD(t
);
320 zassert(sock_fd
== ss
->sock_fd
);
322 result
= ssmpingd_read_msg(ss
);
326 ssmpingd_read_on(ss
);
331 static void ssmpingd_read_on(struct ssmpingd_sock
*ss
)
333 zassert(!ss
->t_sock_read
);
334 thread_add_read(master
, ssmpingd_sock_read
, ss
, ss
->sock_fd
,
338 static struct ssmpingd_sock
*ssmpingd_new(struct in_addr source_addr
)
340 struct ssmpingd_sock
*ss
;
343 if (!qpim_ssmpingd_list
) {
344 qpim_ssmpingd_list
= list_new();
345 if (!qpim_ssmpingd_list
) {
346 zlog_err("%s %s: failure: qpim_ssmpingd_list=list_new()",
347 __FILE__
, __PRETTY_FUNCTION__
);
350 qpim_ssmpingd_list
->del
= (void (*)(void *)) ssmpingd_free
;
353 sock_fd
= ssmpingd_socket(source_addr
, /* port: */ 4321, /* mTTL: */ 64);
355 char source_str
[INET_ADDRSTRLEN
];
356 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
357 zlog_warn("%s: ssmpingd_socket() failure for source %s",
358 __PRETTY_FUNCTION__
, source_str
);
362 ss
= XCALLOC(MTYPE_PIM_SSMPINGD
, sizeof(*ss
));
364 char source_str
[INET_ADDRSTRLEN
];
365 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
366 zlog_err("%s: XCALLOC(%zu) failure for ssmpingd source %s",
368 sizeof(*ss
), source_str
);
373 ss
->sock_fd
= sock_fd
;
375 ss
->source_addr
= source_addr
;
376 ss
->creation
= pim_time_monotonic_sec();
379 listnode_add(qpim_ssmpingd_list
, ss
);
381 ssmpingd_read_on(ss
);
386 int pim_ssmpingd_start(struct in_addr source_addr
)
388 struct ssmpingd_sock
*ss
;
390 ss
= ssmpingd_find(source_addr
);
392 /* silently ignore request to recreate entry */
397 char source_str
[INET_ADDRSTRLEN
];
398 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
399 zlog_info("%s: starting ssmpingd for source %s",
400 __PRETTY_FUNCTION__
, source_str
);
403 ss
= ssmpingd_new(source_addr
);
405 char source_str
[INET_ADDRSTRLEN
];
406 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
407 zlog_warn("%s: ssmpingd_new() failure for source %s",
408 __PRETTY_FUNCTION__
, source_str
);
415 int pim_ssmpingd_stop(struct in_addr source_addr
)
417 struct ssmpingd_sock
*ss
;
419 ss
= ssmpingd_find(source_addr
);
421 char source_str
[INET_ADDRSTRLEN
];
422 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
423 zlog_warn("%s: could not find ssmpingd for source %s",
424 __PRETTY_FUNCTION__
, source_str
);
429 char source_str
[INET_ADDRSTRLEN
];
430 pim_inet4_dump("<src?>", source_addr
, source_str
, sizeof(source_str
));
431 zlog_info("%s: stopping ssmpingd for source %s",
432 __PRETTY_FUNCTION__
, source_str
);