]> git.proxmox.com Git - mirror_frr.git/blob - pimd/mtracebis.c
pimd: adding querying of state to mtrace
[mirror_frr.git] / pimd / mtracebis.c
1 /*
2 * Multicast Traceroute for FRRouting
3 * Copyright (C) 2018 Mladen Sablic
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #ifdef __linux__
21
22 #include "pim_igmp_mtrace.h"
23
24 #include "checksum.h"
25 #include "prefix.h"
26 #include "mtracebis_routeget.h"
27
28 #include <sys/select.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <net/if.h>
39 #include <unistd.h>
40 #include <getopt.h>
41 #include <netdb.h>
42
43 #define MTRACEBIS_VERSION "0.1"
44 #define MTRACE_TIMEOUT (5)
45
46 #define IP_HDR_LEN (sizeof(struct ip))
47 #define IP_RA_LEN (4)
48 #define MTRACE_BUF_LEN (MTRACE_HDR_SIZE + (MTRACE_MAX_HOPS * MTRACE_RSP_SIZE))
49 #define IP_AND_MTRACE_BUF_LEN (IP_HDR_LEN + IP_RA_LEN + MTRACE_BUF_LEN)
50
51 static const char *progname;
52 static void usage(void)
53 {
54 fprintf(stderr, "Usage : %s <multicast source> [<multicast group>]\n",
55 progname);
56 }
57 static void version(void)
58 {
59 fprintf(stderr, "%s %s\n", progname, MTRACEBIS_VERSION);
60 }
61
62 static void print_host(struct in_addr addr)
63 {
64 struct hostent *h;
65
66 h = gethostbyaddr(&addr, sizeof(addr), AF_INET);
67 if (h == NULL)
68 printf("?");
69 else
70 printf("%s", h->h_name);
71 printf(" (%s) ", inet_ntoa(addr));
72 }
73
74 static void print_line_no(int i)
75 {
76 printf("%3d ", -i);
77 }
78
79 static const char *rtg_proto_str(enum mtrace_rtg_proto proto)
80 {
81 static char buf[80];
82
83 buf[0] = '\0';
84
85 switch (proto) {
86 case MTRACE_RTG_PROTO_DVMRP:
87 return "DVMRP";
88 case MTRACE_RTG_PROTO_MOSPF:
89 return "MOSPF";
90 case MTRACE_RTG_PROTO_PIM:
91 return "PIM";
92 case MTRACE_RTG_PROTO_CBT:
93 return "CBT";
94 case MTRACE_RTG_PROTO_PIM_SPECIAL:
95 return "PIM special";
96 case MTRACE_RTG_PROTO_PIM_STATIC:
97 return "PIM static";
98 case MTRACE_RTG_PROTO_DVMRP_STATIC:
99 return "DVMRP static";
100 case MTRACE_RTG_PROTO_PIM_MBGP:
101 return "PIM MBGP";
102 case MTRACE_RTG_PROTO_CBT_SPECIAL:
103 return "CBT special";
104 case MTRACE_RTG_PROTO_CBT_STATIC:
105 return "CBT static";
106 case MTRACE_RTG_PROTO_PIM_ASSERT:
107 return "PIM assert";
108 default:
109 sprintf(buf, "unknown protocol (%d)", proto);
110 return buf;
111 }
112 }
113
114 static void print_rtg_proto(uint32_t rtg_proto)
115 {
116 printf("%s", rtg_proto_str(rtg_proto));
117 }
118
119 static void print_fwd_ttl(uint32_t fwd_ttl)
120 {
121 printf("thresh^ %d", fwd_ttl);
122 }
123
124 static const char *fwd_code_str(enum mtrace_fwd_code code)
125 {
126 static char buf[80];
127
128 buf[0] = '\0';
129
130 switch (code) {
131 case MTRACE_FWD_CODE_NO_ERROR:
132 return "no error";
133 case MTRACE_FWD_CODE_WRONG_IF:
134 return "wrong interface";
135 case MTRACE_FWD_CODE_PRUNE_SENT:
136 return "prune sent";
137 case MTRACE_FWD_CODE_PRUNE_RCVD:
138 return "prune received";
139 case MTRACE_FWD_CODE_SCOPED:
140 return "scoped";
141 case MTRACE_FWD_CODE_NO_ROUTE:
142 return "no route";
143 case MTRACE_FWD_CODE_WRONG_LAST_HOP:
144 return "wrong last hop";
145 case MTRACE_FWD_CODE_NOT_FORWARDING:
146 return "not forwarding";
147 case MTRACE_FWD_CODE_REACHED_RP:
148 return "reached RP";
149 case MTRACE_FWD_CODE_RPF_IF:
150 return "RPF interface";
151 case MTRACE_FWD_CODE_NO_MULTICAST:
152 return "no multicast";
153 case MTRACE_FWD_CODE_INFO_HIDDEN:
154 return "info hidden";
155 case MTRACE_FWD_CODE_NO_SPACE:
156 return "no space";
157 case MTRACE_FWD_CODE_OLD_ROUTER:
158 return "old router";
159 case MTRACE_FWD_CODE_ADMIN_PROHIB:
160 return "admin. prohib.";
161 default:
162 sprintf(buf, "unknown fwd. code (%d)", code);
163 return buf;
164 }
165 }
166
167 static void print_fwd_code(uint32_t fwd_code)
168 {
169 printf("%s", fwd_code_str(fwd_code));
170 }
171
172 static void print_rsp(struct igmp_mtrace_rsp *rsp)
173 {
174 print_host(rsp->outgoing);
175 if (rsp->fwd_code == 0 || rsp->fwd_code == MTRACE_FWD_CODE_REACHED_RP) {
176 print_rtg_proto(rsp->rtg_proto);
177 printf(" ");
178 if (rsp->fwd_code == MTRACE_FWD_CODE_REACHED_RP)
179 printf("(RP) ");
180 if (rsp->rtg_proto == MTRACE_RTG_PROTO_PIM) {
181 switch (rsp->src_mask) {
182 case MTRACE_SRC_MASK_GROUP:
183 printf("(*,G) ");
184 break;
185 case MTRACE_SRC_MASK_SOURCE:
186 printf("(S,G) ");
187 break;
188 }
189 }
190 print_fwd_ttl(rsp->fwd_ttl);
191 } else {
192 print_fwd_code(rsp->fwd_code);
193 }
194 printf("\n");
195 }
196
197 static void print_dest(struct igmp_mtrace *mtrace)
198 {
199 print_line_no(0);
200 print_host(mtrace->dst_addr);
201 printf("\n");
202 }
203
204 static void print_summary(struct igmp_mtrace *mtrace, int hops, long msec)
205 {
206 int i;
207 int t = 0;
208
209 for (i = 0; i < hops; i++)
210 t += mtrace->rsp[i].fwd_ttl;
211
212 printf("Round trip time %ld ms; total ttl of %d required.\n", msec, t);
213 }
214
215 static void print_responses(struct igmp_mtrace *mtrace, int hops, long msec)
216 {
217 int i;
218
219 print_dest(mtrace);
220
221 for (i = 0; i < hops; i++) {
222 print_line_no(i + 1);
223 print_rsp(&mtrace->rsp[i]);
224 }
225 print_summary(mtrace, hops, msec);
226 }
227
228 static int send_query(int fd, struct in_addr to_addr,
229 struct igmp_mtrace *mtrace)
230 {
231 struct sockaddr_in to;
232 socklen_t tolen;
233 int sent;
234
235 memset(&to, 0, sizeof(to));
236 to.sin_family = AF_INET;
237 to.sin_addr = to_addr;
238 tolen = sizeof(to);
239
240 sent = sendto(fd, (char *)mtrace, sizeof(*mtrace), MSG_DONTWAIT,
241 (struct sockaddr *)&to, tolen);
242
243 if (sent < 1)
244 return -1;
245 return 0;
246 }
247
248 static void print_query(struct igmp_mtrace *mtrace)
249 {
250 char src_str[INET_ADDRSTRLEN];
251 char dst_str[INET_ADDRSTRLEN];
252 char grp_str[INET_ADDRSTRLEN];
253
254 printf("* Mtrace from %s to %s via group %s\n",
255 inet_ntop(AF_INET, &mtrace->src_addr, src_str, sizeof(src_str)),
256 inet_ntop(AF_INET, &mtrace->dst_addr, dst_str, sizeof(dst_str)),
257 inet_ntop(AF_INET, &mtrace->grp_addr, grp_str, sizeof(grp_str)));
258 }
259
260 static int recv_response(int fd, int *hops, struct igmp_mtrace *mtracer)
261 {
262 int recvd;
263 char mtrace_buf[IP_AND_MTRACE_BUF_LEN];
264 struct ip *ip;
265 struct igmp_mtrace *mtrace;
266 int mtrace_len;
267 int responses;
268 unsigned short sum;
269
270 recvd = recvfrom(fd, mtrace_buf, IP_AND_MTRACE_BUF_LEN, 0, NULL, 0);
271
272 if (recvd < 1) {
273 fprintf(stderr, "recvfrom error: %s\n", strerror(errno));
274 return -1;
275 }
276
277 if (recvd < (int)sizeof(struct ip)) {
278 fprintf(stderr, "no ip header\n");
279 return -1;
280 }
281
282 ip = (struct ip *)mtrace_buf;
283
284 if (ip->ip_v != 4) {
285 fprintf(stderr, "IP not version 4\n");
286 return -1;
287 }
288
289 sum = ip->ip_sum;
290 ip->ip_sum = 0;
291
292 if (sum != in_cksum(ip, ip->ip_hl * 4))
293 return -1;
294
295 mtrace = (struct igmp_mtrace *)(mtrace_buf + (4 * ip->ip_hl));
296
297 mtrace_len = ntohs(ip->ip_len) - ip->ip_hl * 4;
298
299 if (mtrace_len < (int)MTRACE_HDR_SIZE)
300 return -1;
301
302 sum = mtrace->checksum;
303 mtrace->checksum = 0;
304 if (sum != in_cksum(mtrace, mtrace_len)) {
305 fprintf(stderr, "mtrace checksum wrong\n");
306 return -1;
307 }
308
309 if (mtrace->type != PIM_IGMP_MTRACE_RESPONSE)
310 return -1;
311
312
313 responses = mtrace_len - sizeof(struct igmp_mtrace);
314 responses /= sizeof(struct igmp_mtrace_rsp);
315
316 if (responses > MTRACE_MAX_HOPS) {
317 fprintf(stderr, "mtrace too large\n");
318 return -1;
319 }
320
321 if (hops)
322 *hops = responses;
323
324 if (mtracer)
325 memcpy(mtracer, mtrace, mtrace_len);
326
327 return 0;
328 }
329
330 static int wait_for_response(int fd, int *hops, struct igmp_mtrace *mtrace,
331 long *ret_msec)
332 {
333 fd_set readfds;
334 struct timeval timeout;
335 int ret = -1;
336 long msec, rmsec, tmsec;
337
338 FD_ZERO(&readfds);
339 FD_SET(fd, &readfds);
340
341 memset(&timeout, 0, sizeof(timeout));
342
343 timeout.tv_sec = MTRACE_TIMEOUT;
344
345 tmsec = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
346 do {
347 ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
348 if (ret <= 0)
349 return ret;
350 rmsec = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
351 msec = tmsec - rmsec;
352 } while (recv_response(fd, hops, mtrace) != 0);
353
354 if (ret_msec)
355 *ret_msec = msec;
356
357 return ret;
358 }
359
360 static bool check_end(struct igmp_mtrace *mtrace, int hops)
361 {
362 return mtrace->src_addr.s_addr == mtrace->rsp[hops - 1].prev_hop.s_addr;
363 }
364
365 int main(int argc, char *const argv[])
366 {
367 struct in_addr mc_source;
368 struct in_addr mc_group;
369 struct in_addr iface_addr;
370 struct in_addr gw_addr;
371 struct in_addr mtrace_addr;
372 struct igmp_mtrace mtrace;
373 struct igmp_mtrace *mtracep;
374 int hops = 255;
375 int rhops;
376 int maxhops = 255;
377 int perhop = 3;
378 int ifindex;
379 int unicast = 1;
380 int ttl = 64;
381 int fd = -1;
382 int ret = -1;
383 int c;
384 long msec;
385 int i, j;
386 char ifname[IF_NAMESIZE];
387 char mbuf[MTRACE_BUF_LEN];
388 bool not_group;
389
390 mtrace_addr.s_addr = inet_addr("224.0.1.32");
391
392 uid_t uid = getuid();
393
394 if (uid != 0) {
395 printf("must run as root\n");
396 exit(EXIT_FAILURE);
397 }
398
399 if (argc <= 0)
400 progname = "mtracebis";
401 else
402 progname = argv[0];
403
404 if (argc != 2 && argc != 3) {
405 usage();
406 exit(EXIT_FAILURE);
407 }
408
409 while (1) {
410 static struct option long_options[] = {
411 {"help", no_argument, 0, 'h'},
412 {"version", no_argument, 0, 'v'},
413 {0, 0, 0, 0}};
414 int option_index = 0;
415
416 c = getopt_long(argc, argv, "vh", long_options, &option_index);
417
418 if (c == -1)
419 break;
420
421 switch (c) {
422 case 'h':
423 usage();
424 exit(0);
425 case 'v':
426 version();
427 exit(0);
428 default:
429 usage();
430 exit(EXIT_FAILURE);
431 }
432 }
433 if (inet_pton(AF_INET, argv[1], &mc_source) != 1) {
434 usage();
435 fprintf(stderr, "%s: %s is not a valid IPv4 address\n", argv[0],
436 argv[1]);
437 exit(EXIT_FAILURE);
438 }
439
440 mc_group.s_addr = 0;
441 not_group = false;
442
443 if (argc == 3) {
444 if (inet_pton(AF_INET, argv[2], &mc_group) != 1)
445 not_group = true;
446 if (!not_group && !IPV4_CLASS_DE(ntohl(mc_group.s_addr)))
447 not_group = true;
448 }
449
450 if (not_group) {
451 usage();
452 fprintf(stderr, "%s: %s is not a valid IPv4 group address\n",
453 argv[0], argv[2]);
454 exit(EXIT_FAILURE);
455 }
456
457 ifindex = routeget(mc_source, &iface_addr, &gw_addr);
458 if (ifindex < 0) {
459 fprintf(stderr, "%s: failed to get route to source %s\n",
460 argv[0], argv[1]);
461 exit(EXIT_FAILURE);
462 }
463
464 if (if_indextoname(ifindex, ifname) == NULL) {
465 fprintf(stderr, "%s: if_indextoname error: %s\n", argv[0],
466 strerror(errno));
467 exit(EXIT_FAILURE);
468 }
469
470 /* zero mtrace struct */
471 memset((char *)&mtrace, 0, sizeof(mtrace));
472
473 /* set up query */
474 mtrace.type = PIM_IGMP_MTRACE_QUERY_REQUEST;
475 mtrace.hops = hops;
476 mtrace.checksum = 0;
477 mtrace.grp_addr = mc_group;
478 mtrace.src_addr = mc_source;
479 mtrace.dst_addr = iface_addr;
480 mtrace.rsp_addr = unicast ? iface_addr : mtrace_addr;
481 mtrace.rsp_ttl = ttl;
482 mtrace.qry_id = 0xffffff & time(NULL);
483
484 mtrace.checksum = in_cksum(&mtrace, sizeof(mtrace));
485
486 fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);
487
488 if (fd < 1) {
489 fprintf(stderr, "%s: socket error: %s\n", argv[0],
490 strerror(errno));
491 exit(EXIT_FAILURE);
492 }
493
494 ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
495 strlen(ifname));
496
497 if (ret < 0) {
498 fprintf(stderr, "%s: setsockopt error: %s\n", argv[0],
499 strerror(errno));
500 ret = EXIT_FAILURE;
501 goto close_fd;
502 }
503
504 print_query(&mtrace);
505 if (send_query(fd, gw_addr, &mtrace) < 0) {
506 fprintf(stderr, "%s: sendto error: %s\n", argv[0],
507 strerror(errno));
508 ret = EXIT_FAILURE;
509 goto close_fd;
510 }
511 printf("Querying full reverse path...\n");
512 mtracep = (struct igmp_mtrace *)mbuf;
513 ret = wait_for_response(fd, &rhops, mtracep, &msec);
514 if (ret > 0) {
515 print_responses(mtracep, rhops, msec);
516 ret = 0;
517 goto close_fd;
518 }
519 if (ret < 0) {
520 fprintf(stderr, "%s: select error: %s\n", argv[0],
521 strerror(errno));
522 ret = EXIT_FAILURE;
523 goto close_fd;
524 }
525 printf(" * ");
526 printf("switching to hop-by-hop:\n");
527 print_dest(&mtrace);
528 for (i = 1; i < maxhops; i++) {
529 print_line_no(i);
530 mtrace.hops = i;
531 for (j = 0; j < perhop; j++) {
532 mtrace.qry_id++;
533 mtrace.checksum = 0;
534 mtrace.checksum = in_cksum(&mtrace, sizeof(mtrace));
535 if (send_query(fd, gw_addr, &mtrace) < 0) {
536 fprintf(stderr, "%s: sendto error: %s\n",
537 argv[0], strerror(errno));
538 ret = EXIT_FAILURE;
539 goto close_fd;
540 }
541 ret = wait_for_response(fd, &rhops, mtracep, &msec);
542 if (ret > 0) {
543 if (check_end(mtracep, rhops)) {
544 print_rsp(&mtracep->rsp[rhops - 1]);
545 print_summary(mtracep, rhops, msec);
546 ret = 0;
547 goto close_fd;
548 }
549 if (i > rhops) {
550 printf(" * ...giving up.\n");
551 ret = 0;
552 goto close_fd;
553 }
554 print_rsp(&mtracep->rsp[rhops - 1]);
555 break;
556 }
557 printf(" *");
558 }
559 if (ret <= 0)
560 printf("\n");
561 }
562 ret = 0;
563 close_fd:
564 close(fd);
565 exit(ret);
566 }
567
568 #else /* __linux__ */
569
570 #include <stdio.h>
571 #include <stdlib.h>
572
573 int main(int argc, char *argv[])
574 {
575 printf("%s implemented only for GNU/Linux\n", argv[0]);
576 exit(0);
577 }
578
579 #endif /* __linux__ */