]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
Switch to config.h instead of passing defines in DEFS.
[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.74 1999-11-21 09:36:53 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 igmp_pim_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 case 2:
263 printf("PRESPEC2.0");
264 break;
265 case IPOPT_TS_PRESPEC:
266 printf("PRESPEC");
267 break;
268 default:
269 printf("[bad ts type %d]", cp[3]&0xF);
270 goto done;
271 }
272
273 type = " ";
274 for (len = 4; len < length; len += hoplen) {
275 if (ptr == len)
276 type = " ^ ";
277 printf("%s%d@%s", type, ntohl(*(u_int32_t *)&cp[len+hoplen-4]),
278 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
279 type = " ";
280 }
281
282 done:
283 printf("%s", ptr == len ? " ^ " : "");
284
285 if (cp[3]>>4)
286 printf(" [%d hops not recorded]} ", cp[3]>>4);
287 else
288 printf("}");
289 }
290
291 /*
292 * print IP options.
293 */
294 static void
295 ip_optprint(register const u_char *cp, u_int length)
296 {
297 register u_int len;
298
299 for (; length > 0; cp += len, length -= len) {
300 int tt = *cp;
301
302 len = (tt == IPOPT_NOP || tt == IPOPT_EOL) ? 1 : cp[1];
303 if (len <= 0) {
304 printf("[|ip op len %d]", len);
305 return;
306 }
307 if (&cp[1] >= snapend || cp + len > snapend) {
308 printf("[|ip]");
309 return;
310 }
311 switch (tt) {
312
313 case IPOPT_EOL:
314 printf(" EOL");
315 if (length > 1)
316 printf("-%d", length - 1);
317 return;
318
319 case IPOPT_NOP:
320 printf(" NOP");
321 break;
322
323 case IPOPT_TS:
324 ip_printts(cp, len);
325 break;
326
327 #ifndef IPOPT_SECURITY
328 #define IPOPT_SECURITY 130
329 #endif /* IPOPT_SECURITY */
330 case IPOPT_SECURITY:
331 printf(" SECURITY{%d}", len);
332 break;
333
334 case IPOPT_RR:
335 ip_printroute("RR", cp, len);
336 break;
337
338 case IPOPT_SSRR:
339 ip_printroute("SSRR", cp, len);
340 break;
341
342 case IPOPT_LSRR:
343 ip_printroute("LSRR", cp, len);
344 break;
345
346 #ifndef IPOPT_RA
347 #define IPOPT_RA 148 /* router alert */
348 #endif
349 case IPOPT_RA:
350 printf(" RA");
351 if (len != 4)
352 printf("{%d}", len);
353 else if (cp[2] || cp[3])
354 printf("%d.%d", cp[2], cp[3]);
355 break;
356
357 default:
358 printf(" IPOPT-%d{%d}", cp[0], len);
359 break;
360 }
361 }
362 }
363
364 /*
365 * compute an IP header checksum.
366 * don't modifiy the packet.
367 */
368 u_short
369 in_cksum(const u_short *addr, register int len, u_short csum)
370 {
371 int nleft = len;
372 const u_short *w = addr;
373 u_short answer;
374 int sum = csum;
375
376 /*
377 * Our algorithm is simple, using a 32 bit accumulator (sum),
378 * we add sequential 16 bit words to it, and at the end, fold
379 * back all the carry bits from the top 16 bits into the lower
380 * 16 bits.
381 */
382 while (nleft > 1) {
383 sum += *w++;
384 nleft -= 2;
385 }
386 if (nleft == 1)
387 sum += htons(*(u_char *)w<<8);
388
389 /*
390 * add back carry outs from top 16 bits to low 16 bits
391 */
392 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
393 sum += (sum >> 16); /* add carry */
394 answer = ~sum; /* truncate to 16 bits */
395 return (answer);
396 }
397
398 /*
399 * print an IP datagram.
400 */
401 void
402 ip_print(register const u_char *bp, register u_int length)
403 {
404 register const struct ip *ip;
405 register u_int hlen, len, len0, off;
406 register const u_char *cp;
407 u_char nh;
408 int advance;
409
410 ip = (const struct ip *)bp;
411 #ifdef LBL_ALIGN
412 /*
413 * If the IP header is not aligned, copy into abuf.
414 * This will never happen with BPF. It does happen raw packet
415 * dumps from -r.
416 */
417 if ((long)ip & 3) {
418 static u_char *abuf = NULL;
419 static int didwarn = 0;
420
421 if (abuf == NULL) {
422 abuf = (u_char *)malloc(snaplen);
423 if (abuf == NULL)
424 error("ip_print: malloc");
425 }
426 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
427 snapend += abuf - (u_char *)ip;
428 packetp = abuf;
429 ip = (struct ip *)abuf;
430 /* We really want libpcap to give us aligned packets */
431 if (!didwarn) {
432 warning("compensating for unaligned libpcap packets");
433 ++didwarn;
434 }
435 }
436 #endif
437 if ((u_char *)(ip + 1) > snapend) {
438 printf("[|ip]");
439 return;
440 }
441 if (length < sizeof (struct ip)) {
442 (void)printf("truncated-ip %d", length);
443 return;
444 }
445 hlen = ip->ip_hl * 4;
446
447 len = ntohs(ip->ip_len);
448 if (length < len)
449 (void)printf("truncated-ip - %d bytes missing!",
450 len - length);
451 len -= hlen;
452 len0 = len;
453
454 /*
455 * If this is fragment zero, hand it to the next higher
456 * level protocol.
457 */
458 off = ntohs(ip->ip_off);
459 if ((off & 0x1fff) == 0) {
460 cp = (const u_char *)ip + hlen;
461 nh = ip->ip_p;
462
463 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
464 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
465 ipaddr_string(&ip->ip_dst));
466 }
467 again:
468 switch (nh) {
469
470 #ifndef IPPROTO_AH
471 #define IPPROTO_AH 51
472 #endif
473 case IPPROTO_AH:
474 nh = *cp;
475 advance = ah_print(cp, (const u_char *)ip);
476 cp += advance;
477 len -= advance;
478 goto again;
479
480 #ifndef IPPROTO_ESP
481 #define IPPROTO_ESP 50
482 #endif
483 case IPPROTO_ESP:
484 {
485 int enh;
486 advance = esp_print(cp, (const u_char *)ip, &enh);
487 cp += advance;
488 len -= advance;
489 if (enh < 0)
490 break;
491 nh = enh & 0xff;
492 goto again;
493 }
494
495 #ifndef IPPROTO_IPCOMP
496 #define IPPROTO_IPCOMP 108
497 #endif
498 case IPPROTO_IPCOMP:
499 {
500 int enh;
501 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
502 cp += advance;
503 len -= advance;
504 if (enh < 0)
505 break;
506 nh = enh & 0xff;
507 goto again;
508 }
509
510 case IPPROTO_TCP:
511 tcp_print(cp, len, (const u_char *)ip);
512 break;
513
514 case IPPROTO_UDP:
515 udp_print(cp, len, (const u_char *)ip);
516 break;
517
518 case IPPROTO_ICMP:
519 icmp_print(cp, len, (const u_char *)ip);
520 break;
521
522 #ifndef IPPROTO_IGRP
523 #define IPPROTO_IGRP 9
524 #endif
525 case IPPROTO_IGRP:
526 igrp_print(cp, len, (const u_char *)ip);
527 break;
528
529 case IPPROTO_ND:
530 #if 0
531 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
532 ipaddr_string(&ip->ip_dst));
533 #endif
534 (void)printf(" nd %d", len);
535 break;
536
537 case IPPROTO_EGP:
538 egp_print(cp, len, (const u_char *)ip);
539 break;
540
541 #ifndef IPPROTO_OSPF
542 #define IPPROTO_OSPF 89
543 #endif
544 case IPPROTO_OSPF:
545 ospf_print(cp, len, (const u_char *)ip);
546 break;
547
548 #ifndef IPPROTO_IGMP
549 #define IPPROTO_IGMP 2
550 #endif
551 case IPPROTO_IGMP:
552 igmp_print(cp, len, (const u_char *)ip);
553 break;
554
555 case 4:
556 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
557 #if 0
558 if (vflag)
559 (void)printf("%s > %s: ",
560 ipaddr_string(&ip->ip_src),
561 ipaddr_string(&ip->ip_dst));
562 #endif
563 ip_print(cp, len);
564 if (! vflag) {
565 printf(" (ipip)");
566 return;
567 }
568 break;
569
570 #ifdef INET6
571 #ifndef IP6PROTO_ENCAP
572 #define IP6PROTO_ENCAP 41
573 #endif
574 case IP6PROTO_ENCAP:
575 /* ip6-in-ip encapsulation */
576 #if 0
577 if (vflag)
578 (void)printf("%s > %s: ",
579 ipaddr_string(&ip->ip_src),
580 ipaddr_string(&ip->ip_dst));
581 #endif
582 ip6_print(cp, len);
583 if (! vflag) {
584 printf(" (encap)");
585 return;
586 }
587 break;
588 #endif /*INET6*/
589
590
591 #ifndef IPPROTO_GRE
592 #define IPPROTO_GRE 47
593 #endif
594 case IPPROTO_GRE:
595 if (vflag)
596 (void)printf("gre %s > %s: ",
597 ipaddr_string(&ip->ip_src),
598 ipaddr_string(&ip->ip_dst));
599 /* do it */
600 gre_print(cp, len);
601 if (! vflag) {
602 printf(" (gre encap)");
603 return;
604 }
605 break;
606
607 #ifndef IPPROTO_MOBILE
608 #define IPPROTO_MOBILE 55
609 #endif
610 case IPPROTO_MOBILE:
611 if (vflag)
612 (void)printf("mobile %s > %s: ",
613 ipaddr_string(&ip->ip_src),
614 ipaddr_string(&ip->ip_dst));
615 mobile_print(cp, len);
616 if (! vflag) {
617 printf(" (mobile encap)");
618 return;
619 }
620 break;
621
622 #ifndef IPPROTO_PIM
623 #define IPPROTO_PIM 103
624 #endif
625 case IPPROTO_PIM:
626 pim_print(cp, len);
627 break;
628
629 default:
630 #if 0
631 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
632 ipaddr_string(&ip->ip_dst));
633 #endif
634 (void)printf(" ip-proto-%d %d", nh, len);
635 break;
636 }
637 }
638
639 /*
640 * for fragmented datagrams, print id:size@offset. On all
641 * but the last stick a "+". For unfragmented datagrams, note
642 * the don't fragment flag.
643 */
644 len = len0; /* get the original length */
645 if (off & 0x3fff) {
646 /*
647 * if this isn't the first frag, we're missing the
648 * next level protocol header. print the ip addr.
649 */
650 if (off & 0x1fff)
651 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
652 ipaddr_string(&ip->ip_dst));
653 #ifndef IP_MF
654 #define IP_MF 0x2000
655 #endif /* IP_MF */
656 #ifndef IP_DF
657 #define IP_DF 0x4000
658 #endif /* IP_DF */
659 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
660 (off & 0x1fff) * 8,
661 (off & IP_MF)? "+" : "");
662
663 } else if (off & IP_DF)
664 (void)printf(" (DF)");
665
666 if (ip->ip_tos) {
667 (void)printf(" [tos 0x%x", (int)ip->ip_tos);
668 /* ECN bits */
669 if (ip->ip_tos&0x02) {
670 (void)printf(",ECT");
671 if (ip->ip_tos&0x01)
672 (void)printf(",CE");
673 }
674 (void)printf("] ");
675 }
676
677 if (ip->ip_ttl <= 1)
678 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
679
680 if (vflag) {
681 int sum;
682 char *sep = "";
683
684 printf(" (");
685 if (ip->ip_ttl > 1) {
686 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
687 sep = ", ";
688 }
689 if ((off & 0x3fff) == 0) {
690 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
691 sep = ", ";
692 }
693 if ((u_char *)ip + hlen <= snapend) {
694 sum = in_cksum((const u_short *)ip, hlen, 0);
695 if (sum != 0) {
696 (void)printf("%sbad cksum %x!", sep,
697 ntohs(ip->ip_sum));
698 sep = ", ";
699 }
700 }
701 if ((hlen -= sizeof(struct ip)) > 0) {
702 (void)printf("%soptlen=%d", sep, hlen);
703 ip_optprint((u_char *)(ip + 1), hlen);
704 }
705 printf(")");
706 }
707 }