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