]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
From Neil Spring:
[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
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <tcpdump-stdinc.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "addrtoname.h"
34 #include "interface.h"
35 #include "extract.h" /* must come after interface.h */
36
37 #ifndef lint
38 static const char rcsid[] _U_ =
39 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.128.2.1 2003-11-15 22:28:52 guy Exp $ (LBL)";
40 #endif
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 register const u_char *cp;
362 u_char nh;
363 int advance;
364 struct protoent *proto;
365 u_int16_t sum, ip_sum;
366
367 ip = (const struct ip *)bp;
368 if (IP_V(ip) != 4) { /* print version if != 4 */
369 printf("IP%u ", IP_V(ip));
370 if (IP_V(ip) == 6)
371 printf(", wrong link-layer encapsulation");
372 }
373 else
374 printf("IP ");
375
376 if ((u_char *)(ip + 1) > snapend) {
377 printf("[|ip]");
378 return;
379 }
380 if (length < sizeof (struct ip)) {
381 (void)printf("truncated-ip %d", length);
382 return;
383 }
384 hlen = IP_HL(ip) * 4;
385 if (hlen < sizeof (struct ip)) {
386 (void)printf("bad-hlen %d", hlen);
387 return;
388 }
389
390 len = EXTRACT_16BITS(&ip->ip_len);
391 if (length < len)
392 (void)printf("truncated-ip - %d bytes missing! ",
393 len - length);
394 len -= hlen;
395 len0 = len;
396
397 off = EXTRACT_16BITS(&ip->ip_off);
398
399 if (vflag) {
400 (void)printf("(tos 0x%x", (int)ip->ip_tos);
401 /* ECN bits */
402 if (ip->ip_tos & 0x03) {
403 switch (ip->ip_tos & 0x03) {
404 case 1:
405 (void)printf(",ECT(1)");
406 break;
407 case 2:
408 (void)printf(",ECT(0)");
409 break;
410 case 3:
411 (void)printf(",CE");
412 }
413 }
414
415 if (ip->ip_ttl >= 1)
416 (void)printf(", ttl %3u", ip->ip_ttl);
417
418 /*
419 * for the firewall guys, print id, offset.
420 * On all but the last stick a "+" in the flags portion.
421 * For unfragmented datagrams, note the don't fragment flag.
422 */
423
424 (void)printf(", id %u, offset %u, flags [%s]",
425 EXTRACT_16BITS(&ip->ip_id),
426 (off & 0x1fff) * 8,
427 bittok2str(ip_frag_values, "none", off & 0xe000 ));
428
429 (void)printf(", length: %u", EXTRACT_16BITS(&ip->ip_len));
430
431 if ((hlen - sizeof(struct ip)) > 0) {
432 (void)printf(", optlength: %u (", hlen - (u_int)sizeof(struct ip));
433 ip_optprint((u_char *)(ip + 1), hlen - sizeof(struct ip));
434 printf(" )");
435 }
436
437 if ((u_char *)ip + hlen <= snapend) {
438 sum = in_cksum((const u_short *)ip, hlen, 0);
439 if (sum != 0) {
440 ip_sum = EXTRACT_16BITS(&ip->ip_sum);
441 (void)printf(", bad cksum %x (->%x)!", ip_sum,
442 in_cksum_shouldbe(ip_sum, sum));
443 }
444 }
445
446 printf(") ");
447 }
448
449 /*
450 * If this is fragment zero, hand it to the next higher
451 * level protocol.
452 */
453 if ((off & 0x1fff) == 0) {
454 cp = (const u_char *)ip + hlen;
455 nh = ip->ip_p;
456
457 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
458 nh != IPPROTO_SCTP) {
459 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
460 ipaddr_string(&ip->ip_dst));
461 }
462 again:
463 switch (nh) {
464
465 case IPPROTO_AH:
466 nh = *cp;
467 advance = ah_print(cp);
468 cp += advance;
469 len -= advance;
470 goto again;
471
472 case IPPROTO_ESP:
473 {
474 int enh, padlen;
475 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
476 cp += advance;
477 len -= advance + padlen;
478 if (enh < 0)
479 break;
480 nh = enh & 0xff;
481 goto again;
482 }
483
484 case IPPROTO_IPCOMP:
485 {
486 int enh;
487 advance = ipcomp_print(cp, &enh);
488 cp += advance;
489 len -= advance;
490 if (enh < 0)
491 break;
492 nh = enh & 0xff;
493 goto again;
494 }
495
496 case IPPROTO_SCTP:
497 sctp_print(cp, (const u_char *)ip, len);
498 break;
499
500 case IPPROTO_TCP:
501 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
502 break;
503
504 case IPPROTO_UDP:
505 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
506 break;
507
508 case IPPROTO_ICMP:
509 /* pass on the MF bit plus the offset to detect fragments */
510 icmp_print(cp, len, (const u_char *)ip, (off & 0x3fff));
511 break;
512
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);
523 break;
524
525 case IPPROTO_OSPF:
526 ospf_print(cp, len, (const u_char *)ip);
527 break;
528
529 case IPPROTO_IGMP:
530 igmp_print(cp, len);
531 break;
532
533 case IPPROTO_IPV4:
534 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
535 ip_print(cp, len);
536 if (! vflag) {
537 printf(" (ipip-proto-4)");
538 return;
539 }
540 break;
541
542 #ifdef INET6
543 case IPPROTO_IPV6:
544 /* ip6-in-ip encapsulation */
545 ip6_print(cp, len);
546 break;
547 #endif /*INET6*/
548
549 case IPPROTO_RSVP:
550 rsvp_print(cp, len);
551 break;
552
553 case IPPROTO_GRE:
554 /* do it */
555 gre_print(cp, len);
556 break;
557
558 case IPPROTO_MOBILE:
559 mobile_print(cp, len);
560 break;
561
562 case IPPROTO_PIM:
563 pim_print(cp, len);
564 break;
565
566 case IPPROTO_VRRP:
567 vrrp_print(cp, len, ip->ip_ttl);
568 break;
569
570 default:
571 if ((proto = getprotobynumber(nh)) != NULL)
572 (void)printf(" %s", proto->p_name);
573 else
574 (void)printf(" ip-proto-%d", nh);
575 printf(" %d", len);
576 break;
577 }
578 } else {
579 /* Ultra quiet now means that all this stuff should be suppressed */
580 if (qflag > 1) return;
581
582 /*
583 * if this isn't the first frag, we're missing the
584 * next level protocol header. print the ip addr
585 * and the protocol.
586 */
587 if (off & 0x1fff) {
588 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
589 ipaddr_string(&ip->ip_dst));
590 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
591 (void)printf(" %s", proto->p_name);
592 else
593 (void)printf(" ip-proto-%d", ip->ip_p);
594 }
595 }
596 }
597
598 void
599 ipN_print(register const u_char *bp, register u_int length)
600 {
601 struct ip *ip, hdr;
602
603 ip = (struct ip *)bp;
604 if (length < 4) {
605 (void)printf("truncated-ip %d", length);
606 return;
607 }
608 memcpy (&hdr, (char *)ip, 4);
609 switch (IP_V(&hdr)) {
610 case 4:
611 ip_print (bp, length);
612 return;
613 #ifdef INET6
614 case 6:
615 ip6_print (bp, length);
616 return;
617 #endif
618 default:
619 (void)printf("unknown ip %d", IP_V(&hdr));
620 return;
621 }
622 }
623
624
625