]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
Enhanced PIMv1 support.
[tcpdump] / print-ip.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.76 1999-11-22 07:25:27 fenner Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_var.h>
39 #include <netinet/udp.h>
40 #include <netinet/udp_var.h>
41 #include <netinet/tcp.h>
42
43 #ifdef HAVE_MALLOC_H
44 #include <malloc.h>
45 #endif
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "addrtoname.h"
52 #include "interface.h"
53 #include "extract.h" /* must come after interface.h */
54
55 /* Compatibility */
56 #ifndef IPPROTO_ND
57 #define IPPROTO_ND 77
58 #endif
59
60 #ifndef IN_CLASSD
61 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
62 #endif
63
64 /* (following from ipmulti/mrouted/prune.h) */
65
66 /*
67 * The packet format for a traceroute request.
68 */
69 struct tr_query {
70 u_int tr_src; /* traceroute source */
71 u_int tr_dst; /* traceroute destination */
72 u_int tr_raddr; /* traceroute response address */
73 #if defined(WORDS_BIGENDIAN) || (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN))
74 struct {
75 u_int ttl : 8; /* traceroute response ttl */
76 u_int qid : 24; /* traceroute query id */
77 } q;
78 #else
79 struct {
80 u_int qid : 24; /* traceroute query id */
81 u_int ttl : 8; /* traceroute response ttl */
82 } q;
83 #endif
84 };
85
86 #define tr_rttl q.ttl
87 #define tr_qid q.qid
88
89 /*
90 * Traceroute response format. A traceroute response has a tr_query at the
91 * beginning, followed by one tr_resp for each hop taken.
92 */
93 struct tr_resp {
94 u_int tr_qarr; /* query arrival time */
95 u_int tr_inaddr; /* incoming interface address */
96 u_int tr_outaddr; /* outgoing interface address */
97 u_int tr_rmtaddr; /* parent address in source tree */
98 u_int tr_vifin; /* input packet count on interface */
99 u_int tr_vifout; /* output packet count on interface */
100 u_int tr_pktcnt; /* total incoming packets for src-grp */
101 u_char tr_rproto; /* routing proto deployed on router */
102 u_char tr_fttl; /* ttl required to forward on outvif */
103 u_char tr_smask; /* subnet mask for src addr */
104 u_char tr_rflags; /* forwarding error codes */
105 };
106
107 /* defs within mtrace */
108 #define TR_QUERY 1
109 #define TR_RESP 2
110
111 /* fields for tr_rflags (forwarding error codes) */
112 #define TR_NO_ERR 0
113 #define TR_WRONG_IF 1
114 #define TR_PRUNED 2
115 #define TR_OPRUNED 3
116 #define TR_SCOPED 4
117 #define TR_NO_RTE 5
118 #define TR_NO_FWD 7
119 #define TR_NO_SPACE 0x81
120 #define TR_OLD_ROUTER 0x82
121
122 /* fields for tr_rproto (routing protocol) */
123 #define TR_PROTO_DVMRP 1
124 #define TR_PROTO_MOSPF 2
125 #define TR_PROTO_PIM 3
126 #define TR_PROTO_CBT 4
127
128 static void print_mtrace(register const u_char *bp, register u_int len)
129 {
130 register struct tr_query *tr = (struct tr_query *)(bp + 8);
131
132 printf("mtrace %d: %s to %s reply-to %s", tr->tr_qid,
133 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
134 ipaddr_string(&tr->tr_raddr));
135 if (IN_CLASSD(ntohl(tr->tr_raddr)))
136 printf(" with-ttl %d", tr->tr_rttl);
137 }
138
139 static void print_mresp(register const u_char *bp, register u_int len)
140 {
141 register struct tr_query *tr = (struct tr_query *)(bp + 8);
142
143 printf("mresp %d: %s to %s reply-to %s", tr->tr_qid,
144 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
145 ipaddr_string(&tr->tr_raddr));
146 if (IN_CLASSD(ntohl(tr->tr_raddr)))
147 printf(" with-ttl %d", tr->tr_rttl);
148 }
149
150 static void
151 igmp_print(register const u_char *bp, register u_int len,
152 register const u_char *bp2)
153 {
154 register const struct ip *ip;
155
156 ip = (const struct ip *)bp2;
157 (void)printf("%s > %s: ",
158 ipaddr_string(&ip->ip_src),
159 ipaddr_string(&ip->ip_dst));
160
161 TCHECK2(bp[0], 8);
162 switch (bp[0]) {
163 case 0x11:
164 (void)printf("igmp query");
165 if (*(int *)&bp[4])
166 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
167 if (len != 8)
168 (void)printf(" [len %d]", len);
169 break;
170 case 0x12:
171 (void)printf("igmp report %s", ipaddr_string(&bp[4]));
172 if (len != 8)
173 (void)printf(" [len %d]", len);
174 break;
175 case 0x16:
176 (void)printf("igmp nreport %s", ipaddr_string(&bp[4]));
177 break;
178 case 0x17:
179 (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
180 break;
181 case 0x13:
182 (void)printf("igmp dvmrp");
183 if (len < 8)
184 (void)printf(" [len %d]", len);
185 else
186 dvmrp_print(bp, len);
187 break;
188 case 0x14:
189 (void)printf("igmp pim");
190 pimv1_print(bp, len);
191 break;
192 case 0x1e:
193 print_mresp(bp, len);
194 break;
195 case 0x1f:
196 print_mtrace(bp, len);
197 break;
198 default:
199 (void)printf("igmp-%d", bp[0] & 0xf);
200 break;
201 }
202 if ((bp[0] >> 4) != 1)
203 (void)printf(" [v%d]", bp[0] >> 4);
204
205 TCHECK2(bp[0], len);
206 if (vflag) {
207 /* Check the IGMP checksum */
208 if (in_cksum((const u_short*)bp, len, 0))
209 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
210 }
211 return;
212 trunc:
213 fputs("[|igmp]", stdout);
214 }
215
216 /*
217 * print the recorded route in an IP RR, LSRR or SSRR option.
218 */
219 static void
220 ip_printroute(const char *type, register const u_char *cp, u_int length)
221 {
222 register u_int ptr = cp[2] - 1;
223 register u_int len;
224
225 printf(" %s{", type);
226 if ((length + 1) & 3)
227 printf(" [bad length %d]", length);
228 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
229 printf(" [bad ptr %d]", cp[2]);
230
231 type = "";
232 for (len = 3; len < length; len += 4) {
233 if (ptr == len)
234 type = "#";
235 printf("%s%s", type, ipaddr_string(&cp[len]));
236 type = " ";
237 }
238 printf("%s}", ptr == len? "#" : "");
239 }
240
241 static void
242 ip_printts(register const u_char *cp, u_int length)
243 {
244 register u_int ptr = cp[2] - 1;
245 register u_int len;
246 int hoplen;
247 char *type;
248
249 printf(" TS{");
250 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
251 if ((length - 4) & (hoplen-1))
252 printf("[bad length %d]", length);
253 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
254 printf("[bad ptr %d]", cp[2]);
255 switch (cp[3]&0xF) {
256 case IPOPT_TS_TSONLY:
257 printf("TSONLY");
258 break;
259 case IPOPT_TS_TSANDADDR:
260 printf("TS+ADDR");
261 break;
262 /*
263 * prespecified should really be 3, but some ones might send 2
264 * instead, and the IPOPT_TS_PRESPEC constant can apparently
265 * have both values, so we have to hard-code it here.
266 */
267
268 case 2:
269 printf("PRESPEC2.0");
270 break;
271 case 3: /* IPOPT_TS_PRESPEC */
272 printf("PRESPEC");
273 break;
274 default:
275 printf("[bad ts type %d]", cp[3]&0xF);
276 goto done;
277 }
278
279 type = " ";
280 for (len = 4; len < length; len += hoplen) {
281 if (ptr == len)
282 type = " ^ ";
283 printf("%s%d@%s", type, ntohl(*(u_int32_t *)&cp[len+hoplen-4]),
284 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
285 type = " ";
286 }
287
288 done:
289 printf("%s", ptr == len ? " ^ " : "");
290
291 if (cp[3]>>4)
292 printf(" [%d hops not recorded]} ", cp[3]>>4);
293 else
294 printf("}");
295 }
296
297 /*
298 * print IP options.
299 */
300 static void
301 ip_optprint(register const u_char *cp, u_int length)
302 {
303 register u_int len;
304
305 for (; length > 0; cp += len, length -= len) {
306 int tt = *cp;
307
308 len = (tt == IPOPT_NOP || tt == IPOPT_EOL) ? 1 : cp[1];
309 if (len <= 0) {
310 printf("[|ip op len %d]", len);
311 return;
312 }
313 if (&cp[1] >= snapend || cp + len > snapend) {
314 printf("[|ip]");
315 return;
316 }
317 switch (tt) {
318
319 case IPOPT_EOL:
320 printf(" EOL");
321 if (length > 1)
322 printf("-%d", length - 1);
323 return;
324
325 case IPOPT_NOP:
326 printf(" NOP");
327 break;
328
329 case IPOPT_TS:
330 ip_printts(cp, len);
331 break;
332
333 #ifndef IPOPT_SECURITY
334 #define IPOPT_SECURITY 130
335 #endif /* IPOPT_SECURITY */
336 case IPOPT_SECURITY:
337 printf(" SECURITY{%d}", len);
338 break;
339
340 case IPOPT_RR:
341 ip_printroute("RR", cp, len);
342 break;
343
344 case IPOPT_SSRR:
345 ip_printroute("SSRR", cp, len);
346 break;
347
348 case IPOPT_LSRR:
349 ip_printroute("LSRR", cp, len);
350 break;
351
352 #ifndef IPOPT_RA
353 #define IPOPT_RA 148 /* router alert */
354 #endif
355 case IPOPT_RA:
356 printf(" RA");
357 if (len != 4)
358 printf("{%d}", len);
359 else if (cp[2] || cp[3])
360 printf("%d.%d", cp[2], cp[3]);
361 break;
362
363 default:
364 printf(" IPOPT-%d{%d}", cp[0], len);
365 break;
366 }
367 }
368 }
369
370 /*
371 * compute an IP header checksum.
372 * don't modifiy the packet.
373 */
374 u_short
375 in_cksum(const u_short *addr, register int len, u_short csum)
376 {
377 int nleft = len;
378 const u_short *w = addr;
379 u_short answer;
380 int sum = csum;
381
382 /*
383 * Our algorithm is simple, using a 32 bit accumulator (sum),
384 * we add sequential 16 bit words to it, and at the end, fold
385 * back all the carry bits from the top 16 bits into the lower
386 * 16 bits.
387 */
388 while (nleft > 1) {
389 sum += *w++;
390 nleft -= 2;
391 }
392 if (nleft == 1)
393 sum += htons(*(u_char *)w<<8);
394
395 /*
396 * add back carry outs from top 16 bits to low 16 bits
397 */
398 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
399 sum += (sum >> 16); /* add carry */
400 answer = ~sum; /* truncate to 16 bits */
401 return (answer);
402 }
403
404 /*
405 * print an IP datagram.
406 */
407 void
408 ip_print(register const u_char *bp, register u_int length)
409 {
410 register const struct ip *ip;
411 register u_int hlen, len, len0, off;
412 register const u_char *cp;
413 u_char nh;
414 int advance;
415
416 ip = (const struct ip *)bp;
417 #ifdef LBL_ALIGN
418 /*
419 * If the IP header is not aligned, copy into abuf.
420 * This will never happen with BPF. It does happen raw packet
421 * dumps from -r.
422 */
423 if ((long)ip & 3) {
424 static u_char *abuf = NULL;
425 static int didwarn = 0;
426
427 if (abuf == NULL) {
428 abuf = (u_char *)malloc(snaplen);
429 if (abuf == NULL)
430 error("ip_print: malloc");
431 }
432 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
433 snapend += abuf - (u_char *)ip;
434 packetp = abuf;
435 ip = (struct ip *)abuf;
436 /* We really want libpcap to give us aligned packets */
437 if (!didwarn) {
438 warning("compensating for unaligned libpcap packets");
439 ++didwarn;
440 }
441 }
442 #endif
443 if ((u_char *)(ip + 1) > snapend) {
444 printf("[|ip]");
445 return;
446 }
447 if (length < sizeof (struct ip)) {
448 (void)printf("truncated-ip %d", length);
449 return;
450 }
451 hlen = ip->ip_hl * 4;
452
453 len = ntohs(ip->ip_len);
454 if (length < len)
455 (void)printf("truncated-ip - %d bytes missing!",
456 len - length);
457 len -= hlen;
458 len0 = len;
459
460 /*
461 * If this is fragment zero, hand it to the next higher
462 * level protocol.
463 */
464 off = ntohs(ip->ip_off);
465 if ((off & 0x1fff) == 0) {
466 cp = (const u_char *)ip + hlen;
467 nh = ip->ip_p;
468
469 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
470 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
471 ipaddr_string(&ip->ip_dst));
472 }
473 again:
474 switch (nh) {
475
476 #ifndef IPPROTO_AH
477 #define IPPROTO_AH 51
478 #endif
479 case IPPROTO_AH:
480 nh = *cp;
481 advance = ah_print(cp, (const u_char *)ip);
482 cp += advance;
483 len -= advance;
484 goto again;
485
486 #ifndef IPPROTO_ESP
487 #define IPPROTO_ESP 50
488 #endif
489 case IPPROTO_ESP:
490 {
491 int enh;
492 advance = esp_print(cp, (const u_char *)ip, &enh);
493 cp += advance;
494 len -= advance;
495 if (enh < 0)
496 break;
497 nh = enh & 0xff;
498 goto again;
499 }
500
501 #ifndef IPPROTO_IPCOMP
502 #define IPPROTO_IPCOMP 108
503 #endif
504 case IPPROTO_IPCOMP:
505 {
506 int enh;
507 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
508 cp += advance;
509 len -= advance;
510 if (enh < 0)
511 break;
512 nh = enh & 0xff;
513 goto again;
514 }
515
516 case IPPROTO_TCP:
517 tcp_print(cp, len, (const u_char *)ip);
518 break;
519
520 case IPPROTO_UDP:
521 udp_print(cp, len, (const u_char *)ip);
522 break;
523
524 case IPPROTO_ICMP:
525 icmp_print(cp, len, (const u_char *)ip);
526 break;
527
528 #ifndef IPPROTO_IGRP
529 #define IPPROTO_IGRP 9
530 #endif
531 case IPPROTO_IGRP:
532 igrp_print(cp, len, (const u_char *)ip);
533 break;
534
535 case IPPROTO_ND:
536 #if 0
537 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
538 ipaddr_string(&ip->ip_dst));
539 #endif
540 (void)printf(" nd %d", len);
541 break;
542
543 case IPPROTO_EGP:
544 egp_print(cp, len, (const u_char *)ip);
545 break;
546
547 #ifndef IPPROTO_OSPF
548 #define IPPROTO_OSPF 89
549 #endif
550 case IPPROTO_OSPF:
551 ospf_print(cp, len, (const u_char *)ip);
552 break;
553
554 #ifndef IPPROTO_IGMP
555 #define IPPROTO_IGMP 2
556 #endif
557 case IPPROTO_IGMP:
558 igmp_print(cp, len, (const u_char *)ip);
559 break;
560
561 case 4:
562 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
563 #if 0
564 if (vflag)
565 (void)printf("%s > %s: ",
566 ipaddr_string(&ip->ip_src),
567 ipaddr_string(&ip->ip_dst));
568 #endif
569 ip_print(cp, len);
570 if (! vflag) {
571 printf(" (ipip)");
572 return;
573 }
574 break;
575
576 #ifdef INET6
577 #ifndef IP6PROTO_ENCAP
578 #define IP6PROTO_ENCAP 41
579 #endif
580 case IP6PROTO_ENCAP:
581 /* ip6-in-ip encapsulation */
582 #if 0
583 if (vflag)
584 (void)printf("%s > %s: ",
585 ipaddr_string(&ip->ip_src),
586 ipaddr_string(&ip->ip_dst));
587 #endif
588 ip6_print(cp, len);
589 if (! vflag) {
590 printf(" (encap)");
591 return;
592 }
593 break;
594 #endif /*INET6*/
595
596
597 #ifndef IPPROTO_GRE
598 #define IPPROTO_GRE 47
599 #endif
600 case IPPROTO_GRE:
601 if (vflag)
602 (void)printf("gre %s > %s: ",
603 ipaddr_string(&ip->ip_src),
604 ipaddr_string(&ip->ip_dst));
605 /* do it */
606 gre_print(cp, len);
607 if (! vflag) {
608 printf(" (gre encap)");
609 return;
610 }
611 break;
612
613 #ifndef IPPROTO_MOBILE
614 #define IPPROTO_MOBILE 55
615 #endif
616 case IPPROTO_MOBILE:
617 if (vflag)
618 (void)printf("mobile %s > %s: ",
619 ipaddr_string(&ip->ip_src),
620 ipaddr_string(&ip->ip_dst));
621 mobile_print(cp, len);
622 if (! vflag) {
623 printf(" (mobile encap)");
624 return;
625 }
626 break;
627
628 #ifndef IPPROTO_PIM
629 #define IPPROTO_PIM 103
630 #endif
631 case IPPROTO_PIM:
632 pim_print(cp, len);
633 break;
634
635 default:
636 #if 0
637 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
638 ipaddr_string(&ip->ip_dst));
639 #endif
640 (void)printf(" ip-proto-%d %d", nh, len);
641 break;
642 }
643 }
644
645 /* Ultra quiet now means that all this stuff should be suppressed */
646 /* res 3-Nov-98 */
647 if (qflag > 1) return;
648
649
650 /*
651 * for fragmented datagrams, print id:size@offset. On all
652 * but the last stick a "+". For unfragmented datagrams, note
653 * the don't fragment flag.
654 */
655 len = len0; /* get the original length */
656 if (off & 0x3fff) {
657 /*
658 * if this isn't the first frag, we're missing the
659 * next level protocol header. print the ip addr.
660 */
661 if (off & 0x1fff)
662 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
663 ipaddr_string(&ip->ip_dst));
664 #ifndef IP_MF
665 #define IP_MF 0x2000
666 #endif /* IP_MF */
667 #ifndef IP_DF
668 #define IP_DF 0x4000
669 #endif /* IP_DF */
670 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
671 (off & 0x1fff) * 8,
672 (off & IP_MF)? "+" : "");
673
674 } else if (off & IP_DF)
675 (void)printf(" (DF)");
676
677 if (ip->ip_tos) {
678 (void)printf(" [tos 0x%x", (int)ip->ip_tos);
679 /* ECN bits */
680 if (ip->ip_tos&0x02) {
681 (void)printf(",ECT");
682 if (ip->ip_tos&0x01)
683 (void)printf(",CE");
684 }
685 (void)printf("] ");
686 }
687
688 if (ip->ip_ttl <= 1)
689 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
690
691 if (vflag) {
692 int sum;
693 char *sep = "";
694
695 printf(" (");
696 if (ip->ip_ttl > 1) {
697 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
698 sep = ", ";
699 }
700 if ((off & 0x3fff) == 0) {
701 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
702 sep = ", ";
703 }
704 if ((u_char *)ip + hlen <= snapend) {
705 sum = in_cksum((const u_short *)ip, hlen, 0);
706 if (sum != 0) {
707 (void)printf("%sbad cksum %x!", sep,
708 ntohs(ip->ip_sum));
709 sep = ", ";
710 }
711 }
712 if ((hlen -= sizeof(struct ip)) > 0) {
713 (void)printf("%soptlen=%d", sep, hlen);
714 ip_optprint((u_char *)(ip + 1), hlen);
715 }
716 printf(")");
717 }
718 }