]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
0ab20d3b15b1cfc09ea97878169d913bb3b92794
[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.110 2002-07-28 04:14:21 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
37 #include <netdb.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "addrtoname.h"
44 #include "interface.h"
45 #include "extract.h" /* must come after interface.h */
46
47 #include "ip.h"
48
49 /* Compatibility */
50 #ifndef IPPROTO_ND
51 #define IPPROTO_ND 77
52 #endif
53
54 /*
55 * print the recorded route in an IP RR, LSRR or SSRR option.
56 */
57 static void
58 ip_printroute(const char *type, register const u_char *cp, u_int length)
59 {
60 register u_int ptr = cp[2] - 1;
61 register u_int len;
62
63 printf(" %s{", type);
64 if ((length + 1) & 3)
65 printf(" [bad length %d]", length);
66 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
67 printf(" [bad ptr %d]", cp[2]);
68
69 type = "";
70 for (len = 3; len < length; len += 4) {
71 if (ptr == len)
72 type = "#";
73 printf("%s%s", type, ipaddr_string(&cp[len]));
74 type = " ";
75 }
76 printf("%s}", ptr == len? "#" : "");
77 }
78
79 /*
80 * If source-routing is present, return the final destination.
81 * Otherwise, return IP destination.
82 *
83 * This is used for UDP and TCP pseudo-header in the checksum
84 * calculation.
85 */
86 u_int32_t
87 ip_finddst(const struct ip *ip)
88 {
89 int length;
90 int len;
91 const u_char *cp;
92 uint32_t retval;
93
94 cp = (const u_char *)(ip + 1);
95 length = (IP_HL(ip) << 2) - sizeof(struct ip);
96
97 for (; length > 0; cp += len, length -= len) {
98 int tt = *cp;
99
100 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
101 len = 1;
102 else {
103 if (&cp[1] >= snapend) {
104 return 0;
105 }
106 len = cp[1];
107 }
108 if (len <= 0) {
109 return 0;
110 }
111 if (&cp[1] >= snapend || cp + len > snapend) {
112 return 0;
113 }
114 switch (tt) {
115
116 case IPOPT_SSRR:
117 case IPOPT_LSRR:
118 memcpy(&retval, cp + len - 4, 4);
119 return retval;
120 }
121 }
122 return ip->ip_dst.s_addr;
123 }
124
125 static void
126 ip_printts(register const u_char *cp, u_int length)
127 {
128 register u_int ptr = cp[2] - 1;
129 register u_int len = 0;
130 int hoplen;
131 char *type;
132
133 printf(" TS{");
134 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
135 if ((length - 4) & (hoplen-1))
136 printf("[bad length %d]", length);
137 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
138 printf("[bad ptr %d]", cp[2]);
139 switch (cp[3]&0xF) {
140 case IPOPT_TS_TSONLY:
141 printf("TSONLY");
142 break;
143 case IPOPT_TS_TSANDADDR:
144 printf("TS+ADDR");
145 break;
146 /*
147 * prespecified should really be 3, but some ones might send 2
148 * instead, and the IPOPT_TS_PRESPEC constant can apparently
149 * have both values, so we have to hard-code it here.
150 */
151
152 case 2:
153 printf("PRESPEC2.0");
154 break;
155 case 3: /* IPOPT_TS_PRESPEC */
156 printf("PRESPEC");
157 break;
158 default:
159 printf("[bad ts type %d]", cp[3]&0xF);
160 goto done;
161 }
162
163 type = " ";
164 for (len = 4; len < length; len += hoplen) {
165 if (ptr == len)
166 type = " ^ ";
167 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
168 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
169 type = " ";
170 }
171
172 done:
173 printf("%s", ptr == len ? " ^ " : "");
174
175 if (cp[3]>>4)
176 printf(" [%d hops not recorded]} ", cp[3]>>4);
177 else
178 printf("}");
179 }
180
181 /*
182 * print IP options.
183 */
184 static void
185 ip_optprint(register const u_char *cp, u_int length)
186 {
187 register u_int len;
188
189 for (; length > 0; cp += len, length -= len) {
190 int tt = *cp;
191
192 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
193 len = 1;
194 else {
195 if (&cp[1] >= snapend) {
196 printf("[|ip]");
197 return;
198 }
199 len = cp[1];
200 }
201 if (len <= 0) {
202 printf("[|ip op len %d]", len);
203 return;
204 }
205 if (&cp[1] >= snapend || cp + len > snapend) {
206 printf("[|ip]");
207 return;
208 }
209 switch (tt) {
210
211 case IPOPT_EOL:
212 printf(" EOL");
213 if (length > 1)
214 printf("-%d", length - 1);
215 return;
216
217 case IPOPT_NOP:
218 printf(" NOP");
219 break;
220
221 case IPOPT_TS:
222 ip_printts(cp, len);
223 break;
224
225 #ifndef IPOPT_SECURITY
226 #define IPOPT_SECURITY 130
227 #endif /* IPOPT_SECURITY */
228 case IPOPT_SECURITY:
229 printf(" SECURITY{%d}", len);
230 break;
231
232 case IPOPT_RR:
233 ip_printroute("RR", cp, len);
234 break;
235
236 case IPOPT_SSRR:
237 ip_printroute("SSRR", cp, len);
238 break;
239
240 case IPOPT_LSRR:
241 ip_printroute("LSRR", cp, len);
242 break;
243
244 #ifndef IPOPT_RA
245 #define IPOPT_RA 148 /* router alert */
246 #endif
247 case IPOPT_RA:
248 printf(" RA");
249 if (len != 4)
250 printf("{%d}", len);
251 else if (cp[2] || cp[3])
252 printf("%d.%d", cp[2], cp[3]);
253 break;
254
255 default:
256 printf(" IPOPT-%d{%d}", cp[0], len);
257 break;
258 }
259 }
260 }
261
262 /*
263 * compute an IP header checksum.
264 * don't modifiy the packet.
265 */
266 u_short
267 in_cksum(const u_short *addr, register u_int len, int csum)
268 {
269 int nleft = len;
270 const u_short *w = addr;
271 u_short answer;
272 int sum = csum;
273
274 /*
275 * Our algorithm is simple, using a 32 bit accumulator (sum),
276 * we add sequential 16 bit words to it, and at the end, fold
277 * back all the carry bits from the top 16 bits into the lower
278 * 16 bits.
279 */
280 while (nleft > 1) {
281 sum += *w++;
282 nleft -= 2;
283 }
284 if (nleft == 1)
285 sum += htons(*(u_char *)w<<8);
286
287 /*
288 * add back carry outs from top 16 bits to low 16 bits
289 */
290 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
291 sum += (sum >> 16); /* add carry */
292 answer = ~sum; /* truncate to 16 bits */
293 return (answer);
294 }
295
296 /*
297 * Given the host-byte-order value of the checksum field in a packet
298 * header, and the network-byte-order computed checksum of the data
299 * that the checksum covers (including the checksum itself), compute
300 * what the checksum field *should* have been.
301 */
302 u_int16_t
303 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
304 {
305 u_int32_t shouldbe;
306
307 /*
308 * The value that should have gone into the checksum field
309 * is the negative of the value gotten by summing up everything
310 * *but* the checksum field.
311 *
312 * We can compute that by subtracting the value of the checksum
313 * field from the sum of all the data in the packet, and then
314 * computing the negative of that value.
315 *
316 * "sum" is the value of the checksum field, and "computed_sum"
317 * is the negative of the sum of all the data in the packets,
318 * so that's -(-computed_sum - sum), or (sum + computed_sum).
319 *
320 * All the arithmetic in question is one's complement, so the
321 * addition must include an end-around carry; we do this by
322 * doing the arithmetic in 32 bits (with no sign-extension),
323 * and then adding the upper 16 bits of the sum, which contain
324 * the carry, to the lower 16 bits of the sum, and then do it
325 * again in case *that* sum produced a carry.
326 *
327 * As RFC 1071 notes, the checksum can be computed without
328 * byte-swapping the 16-bit words; summing 16-bit words
329 * on a big-endian machine gives a big-endian checksum, which
330 * can be directly stuffed into the big-endian checksum fields
331 * in protocol headers, and summing words on a little-endian
332 * machine gives a little-endian checksum, which must be
333 * byte-swapped before being stuffed into a big-endian checksum
334 * field.
335 *
336 * "computed_sum" is a network-byte-order value, so we must put
337 * it in host byte order before subtracting it from the
338 * host-byte-order value from the header; the adjusted checksum
339 * will be in host byte order, which is what we'll return.
340 */
341 shouldbe = sum;
342 shouldbe += ntohs(computed_sum);
343 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
344 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
345 return shouldbe;
346 }
347
348 /*
349 * print an IP datagram.
350 */
351 void
352 ip_print(register const u_char *bp, register u_int length)
353 {
354 register const struct ip *ip;
355 register u_int hlen, len, len0, off;
356 register const u_char *cp;
357 u_char nh;
358 int advance;
359 struct protoent *proto;
360
361 ip = (const struct ip *)bp;
362 #ifdef LBL_ALIGN
363 /*
364 * If the IP header is not aligned, copy into abuf.
365 */
366 if ((long)ip & 3) {
367 static u_char *abuf = NULL;
368 static int didwarn = 0;
369
370 if (abuf == NULL) {
371 abuf = (u_char *)malloc(snaplen);
372 if (abuf == NULL)
373 error("ip_print: malloc");
374 }
375 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
376 snapend += abuf - (u_char *)ip;
377 packetp = abuf;
378 ip = (struct ip *)abuf;
379 /* We really want libpcap to give us aligned packets */
380 if (!didwarn) {
381 warning("compensating for unaligned libpcap packets");
382 ++didwarn;
383 }
384 }
385 #endif
386 if ((u_char *)(ip + 1) > snapend) {
387 printf("[|ip]");
388 return;
389 }
390 if (length < sizeof (struct ip)) {
391 (void)printf("truncated-ip %d", length);
392 return;
393 }
394 hlen = IP_HL(ip) * 4;
395 if (hlen < sizeof (struct ip)) {
396 (void)printf("bad-hlen %d", hlen);
397 return;
398 }
399
400 len = ntohs(ip->ip_len);
401 if (length < len)
402 (void)printf("truncated-ip - %d bytes missing! ",
403 len - length);
404 len -= hlen;
405 len0 = len;
406
407 printf("IP ");
408
409 off = ntohs(ip->ip_off);
410
411 if (vflag) {
412 (void)printf("(tos 0x%x", (int)ip->ip_tos);
413 /* ECN bits */
414 if (ip->ip_tos & 0x03) {
415 switch (ip->ip_tos & 0x03) {
416 case 1:
417 (void)printf(",ECT(1)");
418 break;
419 case 2:
420 (void)printf(",ECT(0)");
421 break;
422 case 3:
423 (void)printf(",CE");
424 }
425 }
426
427 if (ip->ip_ttl >= 1)
428 (void)printf(", ttl %d", (int)ip->ip_ttl);
429
430 if ((off & 0x3fff) == 0)
431 (void)printf(", id %d", (int)ntohs(ip->ip_id));
432 (void)printf(", len %d) ", (int)ntohs(ip->ip_len));
433 }
434
435 /*
436 * If this is fragment zero, hand it to the next higher
437 * level protocol.
438 */
439 if ((off & 0x1fff) == 0) {
440 cp = (const u_char *)ip + hlen;
441 nh = ip->ip_p;
442
443 #ifndef IPPROTO_SCTP
444 #define IPPROTO_SCTP 132
445 #endif
446 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
447 nh != IPPROTO_SCTP) {
448 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
449 ipaddr_string(&ip->ip_dst));
450 }
451 again:
452 switch (nh) {
453
454 #ifndef IPPROTO_AH
455 #define IPPROTO_AH 51
456 #endif
457 case IPPROTO_AH:
458 nh = *cp;
459 advance = ah_print(cp, (const u_char *)ip);
460 cp += advance;
461 len -= advance;
462 goto again;
463
464 #ifndef IPPROTO_ESP
465 #define IPPROTO_ESP 50
466 #endif
467 case IPPROTO_ESP:
468 {
469 int enh, padlen;
470 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
471 cp += advance;
472 len -= advance + padlen;
473 if (enh < 0)
474 break;
475 nh = enh & 0xff;
476 goto again;
477 }
478
479 #ifndef IPPROTO_IPCOMP
480 #define IPPROTO_IPCOMP 108
481 #endif
482 case IPPROTO_IPCOMP:
483 {
484 int enh;
485 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
486 cp += advance;
487 len -= advance;
488 if (enh < 0)
489 break;
490 nh = enh & 0xff;
491 goto again;
492 }
493
494 case IPPROTO_SCTP:
495 sctp_print(cp, (const u_char *)ip, len);
496 break;
497
498 case IPPROTO_TCP:
499 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
500 break;
501
502 case IPPROTO_UDP:
503 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
504 break;
505
506 case IPPROTO_ICMP:
507 icmp_print(cp, len, (const u_char *)ip);
508 break;
509
510 #ifndef IPPROTO_IGRP
511 #define IPPROTO_IGRP 9
512 #endif
513 case IPPROTO_IGRP:
514 igrp_print(cp, len, (const u_char *)ip);
515 break;
516
517 case IPPROTO_ND:
518 (void)printf(" nd %d", len);
519 break;
520
521 case IPPROTO_EGP:
522 egp_print(cp, len, (const u_char *)ip);
523 break;
524
525 #ifndef IPPROTO_OSPF
526 #define IPPROTO_OSPF 89
527 #endif
528 case IPPROTO_OSPF:
529 ospf_print(cp, len, (const u_char *)ip);
530 break;
531
532 #ifndef IPPROTO_IGMP
533 #define IPPROTO_IGMP 2
534 #endif
535 case IPPROTO_IGMP:
536 igmp_print(cp, len);
537 break;
538
539 case 4:
540 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
541 ip_print(cp, len);
542 if (! vflag) {
543 printf(" (ipip-proto-4)");
544 return;
545 }
546 break;
547
548 #ifdef INET6
549 #ifndef IP6PROTO_ENCAP
550 #define IP6PROTO_ENCAP 41
551 #endif
552 case IP6PROTO_ENCAP:
553 /* ip6-in-ip encapsulation */
554 ip6_print(cp, len);
555 break;
556 #endif /*INET6*/
557
558
559 #ifndef IPPROTO_GRE
560 #define IPPROTO_GRE 47
561 #endif
562 case IPPROTO_GRE:
563 /* do it */
564 gre_print(cp, len);
565 break;
566
567 #ifndef IPPROTO_MOBILE
568 #define IPPROTO_MOBILE 55
569 #endif
570 case IPPROTO_MOBILE:
571 mobile_print(cp, len);
572 break;
573
574 #ifndef IPPROTO_PIM
575 #define IPPROTO_PIM 103
576 #endif
577 case IPPROTO_PIM:
578 pim_print(cp, len);
579 break;
580
581 #ifndef IPPROTO_VRRP
582 #define IPPROTO_VRRP 112
583 #endif
584 case IPPROTO_VRRP:
585 vrrp_print(cp, len, ip->ip_ttl);
586 break;
587
588 default:
589 if ((proto = getprotobynumber(nh)) != NULL)
590 (void)printf(" %s", proto->p_name);
591 else
592 (void)printf(" ip-proto-%d", nh);
593 printf(" %d", len);
594 break;
595 }
596 }
597
598 /* Ultra quiet now means that all this stuff should be suppressed */
599 /* res 3-Nov-98 */
600 if (qflag > 1) return;
601
602
603 /*
604 * for fragmented datagrams, print id:size@offset. On all
605 * but the last stick a "+". For unfragmented datagrams, note
606 * the don't fragment flag.
607 */
608 len = len0; /* get the original length */
609 if (off & 0x3fff) {
610 /*
611 * if this isn't the first frag, we're missing the
612 * next level protocol header. print the ip addr
613 * and the protocol.
614 */
615 if (off & 0x1fff) {
616 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
617 ipaddr_string(&ip->ip_dst));
618 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
619 (void)printf(" %s", proto->p_name);
620 else
621 (void)printf(" ip-proto-%d", ip->ip_p);
622 }
623 #ifndef IP_MF
624 #define IP_MF 0x2000
625 #endif /* IP_MF */
626 #ifndef IP_DF
627 #define IP_DF 0x4000
628 #endif /* IP_DF */
629 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
630 (off & 0x1fff) * 8,
631 (off & IP_MF)? "+" : "");
632
633 } else if (off & IP_DF)
634 (void)printf(" (DF)");
635
636 if (vflag) {
637 u_int16_t sum, ip_sum;
638 char *sep = "";
639
640 if ((u_char *)ip + hlen <= snapend) {
641 sum = in_cksum((const u_short *)ip, hlen, 0);
642 if (sum != 0) {
643 ip_sum = ntohs(ip->ip_sum);
644 (void)printf("%sbad cksum %x (->%x)!", sep,
645 ip_sum,
646 in_cksum_shouldbe(ip_sum, sum));
647 sep = ", ";
648 }
649 }
650 if ((hlen -= sizeof(struct ip)) > 0) {
651 (void)printf("%soptlen=%d", sep, hlen);
652 ip_optprint((u_char *)(ip + 1), hlen);
653 }
654 }
655
656 }
657
658 void
659 ipN_print(register const u_char *bp, register u_int length)
660 {
661 struct ip *ip, hdr;
662
663 ip = (struct ip *)bp;
664 if (length < 4) {
665 (void)printf("truncated-ip %d", length);
666 return;
667 }
668 memcpy (&hdr, (char *)ip, 4);
669 switch (IP_V(&hdr)) {
670 case 4:
671 ip_print (bp, length);
672 return;
673 #ifdef INET6
674 case 6:
675 ip6_print (bp, length);
676 return;
677 #endif
678 default:
679 (void)printf("unknown ip %d", IP_V(&hdr));
680 return;
681 }
682 }
683
684