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