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