]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
b4e26b8fe85a004e7119aedded725a7d55f19b70
[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.133 2004-03-17 13:24:09 hannes 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], proto %u",
438 EXTRACT_16BITS(&ip->ip_id),
439 (off & 0x1fff) * 8,
440 bittok2str(ip_frag_values, "none", off & 0xe000 ),
441 ip->ip_p);
442
443 (void)printf(", length: %u", EXTRACT_16BITS(&ip->ip_len));
444
445 if ((hlen - sizeof(struct ip)) > 0) {
446 (void)printf(", optlength: %u (", hlen - (u_int)sizeof(struct ip));
447 ip_optprint((u_char *)(ip + 1), hlen - sizeof(struct ip));
448 printf(" )");
449 }
450
451 if ((u_char *)ip + hlen <= snapend) {
452 sum = in_cksum((const u_short *)ip, hlen, 0);
453 if (sum != 0) {
454 ip_sum = EXTRACT_16BITS(&ip->ip_sum);
455 (void)printf(", bad cksum %x (->%x)!", ip_sum,
456 in_cksum_shouldbe(ip_sum, sum));
457 }
458 }
459
460 printf(") ");
461 }
462
463 /*
464 * If this is fragment zero, hand it to the next higher
465 * level protocol.
466 */
467 if ((off & 0x1fff) == 0) {
468 cp = (const u_char *)ip + hlen;
469 nh = ip->ip_p;
470
471 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
472 nh != IPPROTO_SCTP) {
473 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
474 ipaddr_string(&ip->ip_dst));
475 }
476 again:
477 switch (nh) {
478
479 case IPPROTO_AH:
480 nh = *cp;
481 advance = ah_print(cp);
482 if (advance <= 0)
483 break;
484 cp += advance;
485 len -= advance;
486 goto again;
487
488 case IPPROTO_ESP:
489 {
490 int enh, padlen;
491 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
492 if (advance <= 0)
493 break;
494 cp += advance;
495 len -= advance + padlen;
496 nh = enh & 0xff;
497 goto again;
498 }
499
500 case IPPROTO_IPCOMP:
501 {
502 int enh;
503 advance = ipcomp_print(cp, &enh);
504 if (advance <= 0)
505 break;
506 cp += advance;
507 len -= advance;
508 nh = enh & 0xff;
509 goto again;
510 }
511
512 case IPPROTO_SCTP:
513 sctp_print(cp, (const u_char *)ip, len);
514 break;
515
516 case IPPROTO_TCP:
517 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
518 break;
519
520 case IPPROTO_UDP:
521 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
522 break;
523
524 case IPPROTO_ICMP:
525 /* pass on the MF bit plus the offset to detect fragments */
526 icmp_print(cp, len, (const u_char *)ip, (off & 0x3fff));
527 break;
528
529 case IPPROTO_IGRP:
530 igrp_print(cp, len, (const u_char *)ip);
531 break;
532
533 case IPPROTO_ND:
534 (void)printf(" nd %d", len);
535 break;
536
537 case IPPROTO_EGP:
538 egp_print(cp);
539 break;
540
541 case IPPROTO_OSPF:
542 ospf_print(cp, len, (const u_char *)ip);
543 break;
544
545 case IPPROTO_IGMP:
546 igmp_print(cp, len);
547 break;
548
549 case IPPROTO_IPV4:
550 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
551 ip_print(cp, len);
552 if (! vflag) {
553 printf(" (ipip-proto-4)");
554 return;
555 }
556 break;
557
558 #ifdef INET6
559 case IPPROTO_IPV6:
560 /* ip6-in-ip encapsulation */
561 ip6_print(cp, len);
562 break;
563 #endif /*INET6*/
564
565 case IPPROTO_RSVP:
566 rsvp_print(cp, len);
567 break;
568
569 case IPPROTO_GRE:
570 /* do it */
571 gre_print(cp, len);
572 break;
573
574 case IPPROTO_MOBILE:
575 mobile_print(cp, len);
576 break;
577
578 case IPPROTO_PIM:
579 pim_print(cp, len);
580 break;
581
582 case IPPROTO_VRRP:
583 vrrp_print(cp, len, ip->ip_ttl);
584 break;
585
586 default:
587 if ((proto = getprotobynumber(nh)) != NULL)
588 (void)printf(" %s", proto->p_name);
589 else
590 (void)printf(" ip-proto-%d", nh);
591 printf(" %d", len);
592 break;
593 }
594 } else {
595 /* Ultra quiet now means that all this stuff should be suppressed */
596 if (qflag > 1) return;
597
598 /*
599 * if this isn't the first frag, we're missing the
600 * next level protocol header. print the ip addr
601 * and the protocol.
602 */
603 if (off & 0x1fff) {
604 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
605 ipaddr_string(&ip->ip_dst));
606 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
607 (void)printf(" %s", proto->p_name);
608 else
609 (void)printf(" ip-proto-%d", ip->ip_p);
610 }
611 }
612 }
613
614 void
615 ipN_print(register const u_char *bp, register u_int length)
616 {
617 struct ip *ip, hdr;
618
619 ip = (struct ip *)bp;
620 if (length < 4) {
621 (void)printf("truncated-ip %d", length);
622 return;
623 }
624 memcpy (&hdr, (char *)ip, 4);
625 switch (IP_V(&hdr)) {
626 case 4:
627 ip_print (bp, length);
628 return;
629 #ifdef INET6
630 case 6:
631 ip6_print (bp, length);
632 return;
633 #endif
634 default:
635 (void)printf("unknown ip %d", IP_V(&hdr));
636 return;
637 }
638 }
639
640
641