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