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