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