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