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