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