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