1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* PIM for Quagga
* Copyright (C) 2008 Everton da Silva Marques
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "if.h"
#include "log.h"
#include "memory.h"
#include "sockopt.h"
#include "vrf.h"
#include "lib_errors.h"
#include "pimd.h"
#include "pim_instance.h"
#include "pim_ssmpingd.h"
#include "pim_time.h"
#include "pim_sock.h"
#include "network.h"
#if PIM_IPV == 4
static const char *const PIM_SSMPINGD_REPLY_GROUP = "232.43.211.234";
#else
static const char *const PIM_SSMPINGD_REPLY_GROUP = "ff3e::4321:1234";
#endif
enum { PIM_SSMPINGD_REQUEST = 'Q', PIM_SSMPINGD_REPLY = 'A' };
static void ssmpingd_read_on(struct ssmpingd_sock *ss);
void pim_ssmpingd_init(struct pim_instance *pim)
{
int result;
assert(!pim->ssmpingd_list);
result = inet_pton(PIM_AF, PIM_SSMPINGD_REPLY_GROUP,
&pim->ssmpingd_group_addr);
assert(result > 0);
}
void pim_ssmpingd_destroy(struct pim_instance *pim)
{
if (pim->ssmpingd_list)
list_delete(&pim->ssmpingd_list);
}
static struct ssmpingd_sock *ssmpingd_find(struct pim_instance *pim,
pim_addr source_addr)
{
struct listnode *node;
struct ssmpingd_sock *ss;
if (!pim->ssmpingd_list)
return 0;
for (ALL_LIST_ELEMENTS_RO(pim->ssmpingd_list, node, ss))
if (!pim_addr_cmp(source_addr, ss->source_addr))
return ss;
return 0;
}
static void ssmpingd_free(struct ssmpingd_sock *ss)
{
XFREE(MTYPE_PIM_SSMPINGD, ss);
}
#if PIM_IPV == 4
static inline int ssmpingd_setsockopt(int fd, pim_addr addr, int mttl)
{
/* Needed to obtain destination address from recvmsg() */
#if defined(HAVE_IP_PKTINFO)
/* Linux and Solaris IP_PKTINFO */
int opt = 1;
if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt))) {
zlog_warn(
"%s: could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
__func__, fd, errno, safe_strerror(errno));
}
#elif defined(HAVE_IP_RECVDSTADDR)
/* BSD IP_RECVDSTADDR */
int opt = 1;
if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
zlog_warn(
"%s: could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
__func__, fd, errno, safe_strerror(errno));
}
#else
flog_err(
EC_LIB_DEVELOPMENT,
"%s %s: missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
__FILE__, __func__);
close(fd);
return -1;
#endif
if (setsockopt_ipv4_multicast_loop(fd, 0)) {
zlog_warn(
"%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
__func__, fd, errno, safe_strerror(errno));
close(fd);
return PIM_SOCK_ERR_LOOP;
}
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (void *)&addr,
sizeof(addr))) {
zlog_warn(
"%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
__func__, fd, errno, safe_strerror(errno));
close(fd);
return -1;
}
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&mttl,
sizeof(mttl))) {
zlog_warn(
"%s: could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
__func__, mttl, fd, errno, safe_strerror(errno));
close(fd);
return -1;
}
return 0;
}
#else
static inline int ssmpingd_setsockopt(int fd, pim_addr addr, int mttl)
{
setsockopt_ipv6_pktinfo(fd, 1);
setsockopt_ipv6_multicast_hops(fd, mttl);
if (setsockopt_ipv6_multicast_loop(fd, 0)) {
zlog_warn(
"%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
__func__, fd, errno, safe_strerror(errno));
close(fd);
return PIM_SOCK_ERR_LOOP;
}
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, (void *)&addr,
sizeof(addr))) {
zlog_warn(
"%s: could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
__func__, fd, errno, safe_strerror(errno));
close(fd);
return -1;
}
return 0;
}
#endif
static int ssmpingd_socket(pim_addr addr, int port, int mttl)
{
struct sockaddr_storage sockaddr;
int fd;
int ret;
socklen_t len = sizeof(sockaddr);
fd = socket(PIM_AF, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0) {
flog_err_sys(EC_LIB_SOCKET,
"%s: could not create socket: errno=%d: %s",
__func__, errno, safe_strerror(errno));
return -1;
}
pim_socket_getsockname(fd, (struct sockaddr *)&sockaddr, &len);
if (bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
zlog_warn(
"%s: bind(fd=%d,addr=%pSUp,port=%d,len=%zu) failure: errno=%d: %s",
__func__, fd, &sockaddr, port, sizeof(sockaddr), errno,
safe_strerror(errno));
close(fd);
return -1;
}
set_nonblocking(fd);
sockopt_reuseaddr(fd);
ret = ssmpingd_setsockopt(fd, addr, mttl);
if (ret) {
zlog_warn("ssmpingd_setsockopt failed");
close(fd);
return -1;
}
return fd;
}
static void ssmpingd_delete(struct ssmpingd_sock *ss)
{
assert(ss);
THREAD_OFF(ss->t_sock_read);
if (close(ss->sock_fd)) {
zlog_warn(
"%s: failure closing ssmpingd sock_fd=%d for source %pPA: errno=%d: %s",
__func__, ss->sock_fd, &ss->source_addr, errno,
safe_strerror(errno));
/* warning only */
}
listnode_delete(ss->pim->ssmpingd_list, ss);
ssmpingd_free(ss);
}
static void ssmpingd_sendto(struct ssmpingd_sock *ss, const uint8_t *buf,
int len, struct sockaddr_storage to)
{
socklen_t tolen = sizeof(to);
int sent;
sent = sendto(ss->sock_fd, buf, len, MSG_DONTWAIT,
(struct sockaddr *)&to, tolen);
if (sent != len) {
if (sent < 0) {
zlog_warn(
"%s: sendto() failure to %pSUp,fd=%d len=%d: errno=%d: %s",
__func__, &to, ss->sock_fd, len, errno,
safe_strerror(errno));
} else {
zlog_warn(
"%s: sendto() partial to %pSUp, fd=%d len=%d: sent=%d",
__func__, &to, ss->sock_fd, len, sent);
}
}
}
static int ssmpingd_read_msg(struct ssmpingd_sock *ss)
{
struct interface *ifp;
struct sockaddr_storage from;
struct sockaddr_storage to;
socklen_t fromlen = sizeof(from);
socklen_t tolen = sizeof(to);
ifindex_t ifindex = -1;
uint8_t buf[1000];
int len;
++ss->requests;
len = pim_socket_recvfromto(ss->sock_fd, buf, sizeof(buf), &from,
&fromlen, &to, &tolen, &ifindex);
if (len < 0) {
zlog_warn(
"%s: failure receiving ssmping for source %pPA on fd=%d: errno=%d: %s",
__func__, &ss->source_addr, ss->sock_fd, errno,
safe_strerror(errno));
return -1;
}
ifp = if_lookup_by_index(ifindex, ss->pim->vrf->vrf_id);
if (buf[0] != PIM_SSMPINGD_REQUEST) {
zlog_warn(
"%s: bad ssmping type=%d from %pSUp to %pSUp on interface %s ifindex=%d fd=%d src=%pPA",
__func__, buf[0], &from, &to,
ifp ? ifp->name : "<iface?>", ifindex, ss->sock_fd,
&ss->source_addr);
return 0;
}
if (PIM_DEBUG_SSMPINGD) {
zlog_debug(
"%s: recv ssmping from %pSUp, to %pSUp, on interface %s ifindex=%d fd=%d src=%pPA",
__func__, &from, &to, ifp ? ifp->name : "<iface?>",
ifindex, ss->sock_fd, &ss->source_addr);
}
buf[0] = PIM_SSMPINGD_REPLY;
/* unicast reply */
ssmpingd_sendto(ss, buf, len, from);
/* multicast reply */
memcpy(&from, &ss->pim->ssmpingd_group_addr, sizeof(pim_addr));
ssmpingd_sendto(ss, buf, len, from);
return 0;
}
static void ssmpingd_sock_read(struct thread *t)
{
struct ssmpingd_sock *ss;
ss = THREAD_ARG(t);
ssmpingd_read_msg(ss);
/* Keep reading */
ssmpingd_read_on(ss);
}
static void ssmpingd_read_on(struct ssmpingd_sock *ss)
{
thread_add_read(router->master, ssmpingd_sock_read, ss, ss->sock_fd,
&ss->t_sock_read);
}
static struct ssmpingd_sock *ssmpingd_new(struct pim_instance *pim,
pim_addr source_addr)
{
struct ssmpingd_sock *ss;
int sock_fd;
if (!pim->ssmpingd_list) {
pim->ssmpingd_list = list_new();
pim->ssmpingd_list->del = (void (*)(void *))ssmpingd_free;
}
sock_fd =
ssmpingd_socket(source_addr, /* port: */ 4321, /* mTTL: */ 64);
if (sock_fd < 0) {
zlog_warn("%s: ssmpingd_socket() failure for source %pPA",
__func__, &source_addr);
return 0;
}
ss = XCALLOC(MTYPE_PIM_SSMPINGD, sizeof(*ss));
ss->pim = pim;
ss->sock_fd = sock_fd;
ss->t_sock_read = NULL;
ss->source_addr = source_addr;
ss->creation = pim_time_monotonic_sec();
ss->requests = 0;
listnode_add(pim->ssmpingd_list, ss);
ssmpingd_read_on(ss);
return ss;
}
int pim_ssmpingd_start(struct pim_instance *pim, pim_addr source_addr)
{
struct ssmpingd_sock *ss;
ss = ssmpingd_find(pim, source_addr);
if (ss) {
/* silently ignore request to recreate entry */
return 0;
}
zlog_info("%s: starting ssmpingd for source %pPAs", __func__,
&source_addr);
ss = ssmpingd_new(pim, source_addr);
if (!ss) {
zlog_warn("%s: ssmpingd_new() failure for source %pPAs",
__func__, &source_addr);
return -1;
}
return 0;
}
int pim_ssmpingd_stop(struct pim_instance *pim, pim_addr source_addr)
{
struct ssmpingd_sock *ss;
ss = ssmpingd_find(pim, source_addr);
if (!ss) {
zlog_warn("%s: could not find ssmpingd for source %pPAs",
__func__, &source_addr);
return -1;
}
zlog_info("%s: stopping ssmpingd for source %pPAs", __func__,
&source_addr);
ssmpingd_delete(ss);
return 0;
}
|