]> git.proxmox.com Git - mirror_frr.git/blame - pimd/mtracebis.c
Merge pull request #2520 from donaldsharp/privs_smivs
[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
20#ifdef __linux__
21
22#include "pim_igmp_mtrace.h"
23
24#include "checksum.h"
71e55fb2 25#include "prefix.h"
4d9ad5dc
MS
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>
5b02bd3a
DS
39#include <unistd.h>
40#include <getopt.h>
04a2eef3 41#include <netdb.h>
4d9ad5dc
MS
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
51static const char *progname;
52static void usage(void)
53{
71e55fb2
MS
54 fprintf(stderr, "Usage : %s <multicast source> [<multicast group>]\n",
55 progname);
4d9ad5dc
MS
56}
57static void version(void)
58{
59 fprintf(stderr, "%s %s\n", progname, MTRACEBIS_VERSION);
60}
61
04a2eef3
MS
62static 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
74static void print_line_no(int i)
75{
76 printf("%3d ", -i);
77}
78
79static 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
114static void print_rtg_proto(uint32_t rtg_proto)
115{
116 printf("%s", rtg_proto_str(rtg_proto));
117}
118
119static void print_fwd_ttl(uint32_t fwd_ttl)
120{
121 printf("thresh^ %d", fwd_ttl);
122}
123
124static 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
167static void print_fwd_code(uint32_t fwd_code)
168{
169 printf("%s", fwd_code_str(fwd_code));
170}
171
172static void print_rsp(struct igmp_mtrace_rsp *rsp)
173{
174 print_host(rsp->outgoing);
71e55fb2 175 if (rsp->fwd_code == 0 || rsp->fwd_code == MTRACE_FWD_CODE_REACHED_RP) {
04a2eef3
MS
176 print_rtg_proto(rsp->rtg_proto);
177 printf(" ");
71e55fb2
MS
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 }
04a2eef3
MS
190 print_fwd_ttl(rsp->fwd_ttl);
191 } else {
192 print_fwd_code(rsp->fwd_code);
193 }
194 printf("\n");
195}
196
197static void print_dest(struct igmp_mtrace *mtrace)
198{
199 print_line_no(0);
200 print_host(mtrace->dst_addr);
201 printf("\n");
202}
203
204static 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
215static 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
4d9ad5dc
MS
228static 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
248static 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
04a2eef3 260static int recv_response(int fd, int *hops, struct igmp_mtrace *mtracer)
4d9ad5dc
MS
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;
d7c0a89a 268 unsigned short sum;
4d9ad5dc
MS
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
18e994a0 299 if ((char *)mtrace + mtrace_len
300 > (char *)mtrace_buf + IP_AND_MTRACE_BUF_LEN)
301 return -1;
302
4d9ad5dc
MS
303 if (mtrace_len < (int)MTRACE_HDR_SIZE)
304 return -1;
305
306 sum = mtrace->checksum;
307 mtrace->checksum = 0;
308 if (sum != in_cksum(mtrace, mtrace_len)) {
309 fprintf(stderr, "mtrace checksum wrong\n");
310 return -1;
311 }
312
313 if (mtrace->type != PIM_IGMP_MTRACE_RESPONSE)
314 return -1;
315
316
317 responses = mtrace_len - sizeof(struct igmp_mtrace);
318 responses /= sizeof(struct igmp_mtrace_rsp);
319
04a2eef3
MS
320 if (responses > MTRACE_MAX_HOPS) {
321 fprintf(stderr, "mtrace too large\n");
322 return -1;
323 }
4d9ad5dc
MS
324
325 if (hops)
326 *hops = responses;
327
04a2eef3
MS
328 if (mtracer)
329 memcpy(mtracer, mtrace, mtrace_len);
4d9ad5dc
MS
330
331 return 0;
332}
333
04a2eef3
MS
334static int wait_for_response(int fd, int *hops, struct igmp_mtrace *mtrace,
335 long *ret_msec)
4d9ad5dc
MS
336{
337 fd_set readfds;
338 struct timeval timeout;
a2b6e694 339 int ret;
4d9ad5dc
MS
340 long msec, rmsec, tmsec;
341
342 FD_ZERO(&readfds);
343 FD_SET(fd, &readfds);
344
345 memset(&timeout, 0, sizeof(timeout));
346
347 timeout.tv_sec = MTRACE_TIMEOUT;
348
349 tmsec = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
350 do {
351 ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
352 if (ret <= 0)
353 return ret;
354 rmsec = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
355 msec = tmsec - rmsec;
04a2eef3
MS
356 } while (recv_response(fd, hops, mtrace) != 0);
357
358 if (ret_msec)
359 *ret_msec = msec;
4d9ad5dc
MS
360
361 return ret;
362}
363
04a2eef3
MS
364static bool check_end(struct igmp_mtrace *mtrace, int hops)
365{
366 return mtrace->src_addr.s_addr == mtrace->rsp[hops - 1].prev_hop.s_addr;
367}
368
4d9ad5dc
MS
369int main(int argc, char *const argv[])
370{
371 struct in_addr mc_source;
71e55fb2 372 struct in_addr mc_group;
4d9ad5dc
MS
373 struct in_addr iface_addr;
374 struct in_addr gw_addr;
375 struct in_addr mtrace_addr;
376 struct igmp_mtrace mtrace;
04a2eef3 377 struct igmp_mtrace *mtracep;
4d9ad5dc
MS
378 int hops = 255;
379 int rhops;
380 int maxhops = 255;
381 int perhop = 3;
382 int ifindex;
383 int unicast = 1;
384 int ttl = 64;
385 int fd = -1;
386 int ret = -1;
387 int c;
04a2eef3 388 long msec;
4d9ad5dc
MS
389 int i, j;
390 char ifname[IF_NAMESIZE];
04a2eef3 391 char mbuf[MTRACE_BUF_LEN];
71e55fb2 392 bool not_group;
4d9ad5dc
MS
393
394 mtrace_addr.s_addr = inet_addr("224.0.1.32");
395
396 uid_t uid = getuid();
397
398 if (uid != 0) {
399 printf("must run as root\n");
400 exit(EXIT_FAILURE);
401 }
402
403 if (argc <= 0)
404 progname = "mtracebis";
405 else
406 progname = argv[0];
407
71e55fb2 408 if (argc != 2 && argc != 3) {
4d9ad5dc
MS
409 usage();
410 exit(EXIT_FAILURE);
411 }
412
413 while (1) {
414 static struct option long_options[] = {
415 {"help", no_argument, 0, 'h'},
416 {"version", no_argument, 0, 'v'},
996c9314 417 {0, 0, 0, 0}};
4d9ad5dc
MS
418 int option_index = 0;
419
420 c = getopt_long(argc, argv, "vh", long_options, &option_index);
421
422 if (c == -1)
423 break;
424
425 switch (c) {
426 case 'h':
427 usage();
428 exit(0);
429 case 'v':
430 version();
431 exit(0);
432 default:
433 usage();
434 exit(EXIT_FAILURE);
435 }
436 }
437 if (inet_pton(AF_INET, argv[1], &mc_source) != 1) {
438 usage();
71e55fb2 439 fprintf(stderr, "%s: %s is not a valid IPv4 address\n", argv[0],
4d9ad5dc
MS
440 argv[1]);
441 exit(EXIT_FAILURE);
442 }
443
71e55fb2
MS
444 mc_group.s_addr = 0;
445 not_group = false;
446
447 if (argc == 3) {
448 if (inet_pton(AF_INET, argv[2], &mc_group) != 1)
449 not_group = true;
450 if (!not_group && !IPV4_CLASS_DE(ntohl(mc_group.s_addr)))
451 not_group = true;
452 }
453
454 if (not_group) {
455 usage();
456 fprintf(stderr, "%s: %s is not a valid IPv4 group address\n",
457 argv[0], argv[2]);
458 exit(EXIT_FAILURE);
459 }
460
4d9ad5dc
MS
461 ifindex = routeget(mc_source, &iface_addr, &gw_addr);
462 if (ifindex < 0) {
463 fprintf(stderr, "%s: failed to get route to source %s\n",
464 argv[0], argv[1]);
465 exit(EXIT_FAILURE);
466 }
467
468 if (if_indextoname(ifindex, ifname) == NULL) {
469 fprintf(stderr, "%s: if_indextoname error: %s\n", argv[0],
470 strerror(errno));
471 exit(EXIT_FAILURE);
472 }
473
474 /* zero mtrace struct */
475 memset((char *)&mtrace, 0, sizeof(mtrace));
476
477 /* set up query */
478 mtrace.type = PIM_IGMP_MTRACE_QUERY_REQUEST;
479 mtrace.hops = hops;
480 mtrace.checksum = 0;
71e55fb2 481 mtrace.grp_addr = mc_group;
4d9ad5dc
MS
482 mtrace.src_addr = mc_source;
483 mtrace.dst_addr = iface_addr;
484 mtrace.rsp_addr = unicast ? iface_addr : mtrace_addr;
485 mtrace.rsp_ttl = ttl;
486 mtrace.qry_id = 0xffffff & time(NULL);
487
488 mtrace.checksum = in_cksum(&mtrace, sizeof(mtrace));
489
490 fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);
491
492 if (fd < 1) {
493 fprintf(stderr, "%s: socket error: %s\n", argv[0],
494 strerror(errno));
495 exit(EXIT_FAILURE);
496 }
497
498 ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
499 strlen(ifname));
500
501 if (ret < 0) {
502 fprintf(stderr, "%s: setsockopt error: %s\n", argv[0],
503 strerror(errno));
504 ret = EXIT_FAILURE;
505 goto close_fd;
506 }
507
508 print_query(&mtrace);
509 if (send_query(fd, gw_addr, &mtrace) < 0) {
510 fprintf(stderr, "%s: sendto error: %s\n", argv[0],
511 strerror(errno));
512 ret = EXIT_FAILURE;
513 goto close_fd;
514 }
515 printf("Querying full reverse path...\n");
04a2eef3
MS
516 mtracep = (struct igmp_mtrace *)mbuf;
517 ret = wait_for_response(fd, &rhops, mtracep, &msec);
4d9ad5dc 518 if (ret > 0) {
04a2eef3 519 print_responses(mtracep, rhops, msec);
4d9ad5dc
MS
520 ret = 0;
521 goto close_fd;
522 }
523 if (ret < 0) {
524 fprintf(stderr, "%s: select error: %s\n", argv[0],
525 strerror(errno));
526 ret = EXIT_FAILURE;
527 goto close_fd;
528 }
529 printf(" * ");
530 printf("switching to hop-by-hop:\n");
04a2eef3 531 print_dest(&mtrace);
4d9ad5dc 532 for (i = 1; i < maxhops; i++) {
04a2eef3 533 print_line_no(i);
4d9ad5dc
MS
534 mtrace.hops = i;
535 for (j = 0; j < perhop; j++) {
536 mtrace.qry_id++;
537 mtrace.checksum = 0;
538 mtrace.checksum = in_cksum(&mtrace, sizeof(mtrace));
539 if (send_query(fd, gw_addr, &mtrace) < 0) {
540 fprintf(stderr, "%s: sendto error: %s\n",
541 argv[0], strerror(errno));
542 ret = EXIT_FAILURE;
543 goto close_fd;
544 }
04a2eef3 545 ret = wait_for_response(fd, &rhops, mtracep, &msec);
4d9ad5dc 546 if (ret > 0) {
04a2eef3
MS
547 if (check_end(mtracep, rhops)) {
548 print_rsp(&mtracep->rsp[rhops - 1]);
549 print_summary(mtracep, rhops, msec);
550 ret = 0;
551 goto close_fd;
552 }
4d9ad5dc 553 if (i > rhops) {
04a2eef3 554 printf(" * ...giving up.\n");
4d9ad5dc
MS
555 ret = 0;
556 goto close_fd;
557 }
04a2eef3 558 print_rsp(&mtracep->rsp[rhops - 1]);
4d9ad5dc
MS
559 break;
560 }
561 printf(" *");
562 }
563 if (ret <= 0)
564 printf("\n");
565 }
566 ret = 0;
567close_fd:
568 close(fd);
569 exit(ret);
570}
571
572#else /* __linux__ */
573
574#include <stdio.h>
575#include <stdlib.h>
576
577int main(int argc, char *argv[])
578{
579 printf("%s implemented only for GNU/Linux\n", argv[0]);
580 exit(0);
581}
582
583#endif /* __linux__ */