]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
Fix a bunch of de-constifications.
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <string.h>
29
30 #include "interface.h"
31 #include "addrtoname.h"
32 #include "extract.h" /* must come after interface.h */
33
34 #include "ip.h"
35 #include "ipproto.h"
36
37 static const char tstr[] = "[|ip]";
38
39 static const struct tok ip_option_values[] = {
40 { IPOPT_EOL, "EOL" },
41 { IPOPT_NOP, "NOP" },
42 { IPOPT_TS, "timestamp" },
43 { IPOPT_SECURITY, "security" },
44 { IPOPT_RR, "RR" },
45 { IPOPT_SSRR, "SSRR" },
46 { IPOPT_LSRR, "LSRR" },
47 { IPOPT_RA, "RA" },
48 { IPOPT_RFC1393, "traceroute" },
49 { 0, NULL }
50 };
51
52 /*
53 * print the recorded route in an IP RR, LSRR or SSRR option.
54 */
55 static void
56 ip_printroute(netdissect_options *ndo,
57 register const u_char *cp, u_int length)
58 {
59 register u_int ptr;
60 register u_int len;
61
62 if (length < 3) {
63 ND_PRINT((ndo, " [bad length %u]", length));
64 return;
65 }
66 if ((length + 1) & 3)
67 ND_PRINT((ndo, " [bad length %u]", length));
68 ptr = cp[2] - 1;
69 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
70 ND_PRINT((ndo, " [bad ptr %u]", cp[2]));
71
72 for (len = 3; len < length; len += 4) {
73 ND_PRINT((ndo, " %s", ipaddr_string(ndo, &cp[len])));
74 if (ptr > len)
75 ND_PRINT((ndo, ","));
76 }
77 }
78
79 /*
80 * If source-routing is present and valid, return the final destination.
81 * Otherwise, return IP destination.
82 *
83 * This is used for UDP and TCP pseudo-header in the checksum
84 * calculation.
85 */
86 static uint32_t
87 ip_finddst(netdissect_options *ndo,
88 const struct ip *ip)
89 {
90 int length;
91 int len;
92 const u_char *cp;
93 uint32_t retval;
94
95 cp = (const u_char *)(ip + 1);
96 length = (IP_HL(ip) << 2) - sizeof(struct ip);
97
98 for (; length > 0; cp += len, length -= len) {
99 int tt;
100
101 ND_TCHECK(*cp);
102 tt = *cp;
103 if (tt == IPOPT_EOL)
104 break;
105 else if (tt == IPOPT_NOP)
106 len = 1;
107 else {
108 ND_TCHECK(cp[1]);
109 len = cp[1];
110 if (len < 2)
111 break;
112 }
113 ND_TCHECK2(*cp, len);
114 switch (tt) {
115
116 case IPOPT_SSRR:
117 case IPOPT_LSRR:
118 if (len < 7)
119 break;
120 UNALIGNED_MEMCPY(&retval, cp + len - 4, 4);
121 return retval;
122 }
123 }
124 trunc:
125 UNALIGNED_MEMCPY(&retval, &ip->ip_dst.s_addr, sizeof(uint32_t));
126 return retval;
127 }
128
129 /*
130 * Compute a V4-style checksum by building a pseudoheader.
131 */
132 int
133 nextproto4_cksum(netdissect_options *ndo,
134 const struct ip *ip, const uint8_t *data,
135 u_int len, u_int covlen, u_int next_proto)
136 {
137 struct phdr {
138 uint32_t src;
139 uint32_t dst;
140 u_char mbz;
141 u_char proto;
142 uint16_t len;
143 } ph;
144 struct cksum_vec vec[2];
145
146 /* pseudo-header.. */
147 ph.len = htons((uint16_t)len);
148 ph.mbz = 0;
149 ph.proto = next_proto;
150 UNALIGNED_MEMCPY(&ph.src, &ip->ip_src.s_addr, sizeof(uint32_t));
151 if (IP_HL(ip) == 5)
152 UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst.s_addr, sizeof(uint32_t));
153 else
154 ph.dst = ip_finddst(ndo, ip);
155
156 vec[0].ptr = (const uint8_t *)(void *)&ph;
157 vec[0].len = sizeof(ph);
158 vec[1].ptr = data;
159 vec[1].len = covlen;
160 return (in_cksum(vec, 2));
161 }
162
163 static void
164 ip_printts(netdissect_options *ndo,
165 register const u_char *cp, u_int length)
166 {
167 register u_int ptr;
168 register u_int len;
169 int hoplen;
170 const char *type;
171
172 if (length < 4) {
173 ND_PRINT((ndo, "[bad length %u]", length));
174 return;
175 }
176 ND_PRINT((ndo, " TS{"));
177 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
178 if ((length - 4) & (hoplen-1))
179 ND_PRINT((ndo, "[bad length %u]", length));
180 ptr = cp[2] - 1;
181 len = 0;
182 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
183 ND_PRINT((ndo, "[bad ptr %u]", cp[2]));
184 switch (cp[3]&0xF) {
185 case IPOPT_TS_TSONLY:
186 ND_PRINT((ndo, "TSONLY"));
187 break;
188 case IPOPT_TS_TSANDADDR:
189 ND_PRINT((ndo, "TS+ADDR"));
190 break;
191 /*
192 * prespecified should really be 3, but some ones might send 2
193 * instead, and the IPOPT_TS_PRESPEC constant can apparently
194 * have both values, so we have to hard-code it here.
195 */
196
197 case 2:
198 ND_PRINT((ndo, "PRESPEC2.0"));
199 break;
200 case 3: /* IPOPT_TS_PRESPEC */
201 ND_PRINT((ndo, "PRESPEC"));
202 break;
203 default:
204 ND_PRINT((ndo, "[bad ts type %d]", cp[3]&0xF));
205 goto done;
206 }
207
208 type = " ";
209 for (len = 4; len < length; len += hoplen) {
210 if (ptr == len)
211 type = " ^ ";
212 ND_PRINT((ndo, "%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
213 hoplen!=8 ? "" : ipaddr_string(ndo, &cp[len])));
214 type = " ";
215 }
216
217 done:
218 ND_PRINT((ndo, "%s", ptr == len ? " ^ " : ""));
219
220 if (cp[3]>>4)
221 ND_PRINT((ndo, " [%d hops not recorded]} ", cp[3]>>4));
222 else
223 ND_PRINT((ndo, "}"));
224 }
225
226 /*
227 * print IP options.
228 */
229 static void
230 ip_optprint(netdissect_options *ndo,
231 register const u_char *cp, u_int length)
232 {
233 register u_int option_len;
234 const char *sep = "";
235
236 for (; length > 0; cp += option_len, length -= option_len) {
237 u_int option_code;
238
239 ND_PRINT((ndo, "%s", sep));
240 sep = ",";
241
242 ND_TCHECK(*cp);
243 option_code = *cp;
244
245 ND_PRINT((ndo, "%s",
246 tok2str(ip_option_values,"unknown %u",option_code)));
247
248 if (option_code == IPOPT_NOP ||
249 option_code == IPOPT_EOL)
250 option_len = 1;
251
252 else {
253 ND_TCHECK(cp[1]);
254 option_len = cp[1];
255 if (option_len < 2) {
256 ND_PRINT((ndo, " [bad length %u]", option_len));
257 return;
258 }
259 }
260
261 if (option_len > length) {
262 ND_PRINT((ndo, " [bad length %u]", option_len));
263 return;
264 }
265
266 ND_TCHECK2(*cp, option_len);
267
268 switch (option_code) {
269 case IPOPT_EOL:
270 return;
271
272 case IPOPT_TS:
273 ip_printts(ndo, cp, option_len);
274 break;
275
276 case IPOPT_RR: /* fall through */
277 case IPOPT_SSRR:
278 case IPOPT_LSRR:
279 ip_printroute(ndo, cp, option_len);
280 break;
281
282 case IPOPT_RA:
283 if (option_len < 4) {
284 ND_PRINT((ndo, " [bad length %u]", option_len));
285 break;
286 }
287 ND_TCHECK(cp[3]);
288 if (EXTRACT_16BITS(&cp[2]) != 0)
289 ND_PRINT((ndo, " value %u", EXTRACT_16BITS(&cp[2])));
290 break;
291
292 case IPOPT_NOP: /* nothing to print - fall through */
293 case IPOPT_SECURITY:
294 default:
295 break;
296 }
297 }
298 return;
299
300 trunc:
301 ND_PRINT((ndo, "%s", tstr));
302 }
303
304 #define IP_RES 0x8000
305
306 static const struct tok ip_frag_values[] = {
307 { IP_MF, "+" },
308 { IP_DF, "DF" },
309 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
310 { 0, NULL }
311 };
312
313 struct ip_print_demux_state {
314 const struct ip *ip;
315 const u_char *cp;
316 u_int len, off;
317 u_char nh;
318 int advance;
319 };
320
321 static void
322 ip_print_demux(netdissect_options *ndo,
323 struct ip_print_demux_state *ipds)
324 {
325 struct protoent *proto;
326 struct cksum_vec vec[1];
327
328 again:
329 switch (ipds->nh) {
330
331 case IPPROTO_AH:
332 ipds->nh = *ipds->cp;
333 ipds->advance = ah_print(ndo, ipds->cp);
334 if (ipds->advance <= 0)
335 break;
336 ipds->cp += ipds->advance;
337 ipds->len -= ipds->advance;
338 goto again;
339
340 case IPPROTO_ESP:
341 {
342 int enh, padlen;
343 ipds->advance = esp_print(ndo, ipds->cp, ipds->len,
344 (const u_char *)ipds->ip,
345 &enh, &padlen);
346 if (ipds->advance <= 0)
347 break;
348 ipds->cp += ipds->advance;
349 ipds->len -= ipds->advance + padlen;
350 ipds->nh = enh & 0xff;
351 goto again;
352 }
353
354 case IPPROTO_IPCOMP:
355 {
356 int enh;
357 ipds->advance = ipcomp_print(ndo, ipds->cp, &enh);
358 if (ipds->advance <= 0)
359 break;
360 ipds->cp += ipds->advance;
361 ipds->len -= ipds->advance;
362 ipds->nh = enh & 0xff;
363 goto again;
364 }
365
366 case IPPROTO_SCTP:
367 sctp_print(ndo, ipds->cp, (const u_char *)ipds->ip, ipds->len);
368 break;
369
370 case IPPROTO_DCCP:
371 dccp_print(ndo, ipds->cp, (const u_char *)ipds->ip, ipds->len);
372 break;
373
374 case IPPROTO_TCP:
375 /* pass on the MF bit plus the offset to detect fragments */
376 tcp_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip,
377 ipds->off & (IP_MF|IP_OFFMASK));
378 break;
379
380 case IPPROTO_UDP:
381 /* pass on the MF bit plus the offset to detect fragments */
382 udp_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip,
383 ipds->off & (IP_MF|IP_OFFMASK));
384 break;
385
386 case IPPROTO_ICMP:
387 /* pass on the MF bit plus the offset to detect fragments */
388 icmp_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip,
389 ipds->off & (IP_MF|IP_OFFMASK));
390 break;
391
392 case IPPROTO_PIGP:
393 /*
394 * XXX - the current IANA protocol number assignments
395 * page lists 9 as "any private interior gateway
396 * (used by Cisco for their IGRP)" and 88 as
397 * "EIGRP" from Cisco.
398 *
399 * Recent BSD <netinet/in.h> headers define
400 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
401 * We define IP_PROTO_PIGP as 9 and
402 * IP_PROTO_EIGRP as 88; those names better
403 * match was the current protocol number
404 * assignments say.
405 */
406 igrp_print(ndo, ipds->cp, ipds->len);
407 break;
408
409 case IPPROTO_EIGRP:
410 eigrp_print(ndo, ipds->cp, ipds->len);
411 break;
412
413 case IPPROTO_ND:
414 ND_PRINT((ndo, " nd %d", ipds->len));
415 break;
416
417 case IPPROTO_EGP:
418 egp_print(ndo, ipds->cp, ipds->len);
419 break;
420
421 case IPPROTO_OSPF:
422 ospf_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip);
423 break;
424
425 case IPPROTO_IGMP:
426 igmp_print(ndo, ipds->cp, ipds->len);
427 break;
428
429 case IPPROTO_IPV4:
430 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
431 ip_print(ndo, ipds->cp, ipds->len);
432 if (! ndo->ndo_vflag) {
433 ND_PRINT((ndo, " (ipip-proto-4)"));
434 return;
435 }
436 break;
437
438 case IPPROTO_IPV6:
439 /* ip6-in-ip encapsulation */
440 ip6_print(ndo, ipds->cp, ipds->len);
441 break;
442
443 case IPPROTO_RSVP:
444 rsvp_print(ndo, ipds->cp, ipds->len);
445 break;
446
447 case IPPROTO_GRE:
448 /* do it */
449 gre_print(ndo, ipds->cp, ipds->len);
450 break;
451
452 case IPPROTO_MOBILE:
453 mobile_print(ndo, ipds->cp, ipds->len);
454 break;
455
456 case IPPROTO_PIM:
457 vec[0].ptr = ipds->cp;
458 vec[0].len = ipds->len;
459 pim_print(ndo, ipds->cp, ipds->len, in_cksum(vec, 1));
460 break;
461
462 case IPPROTO_VRRP:
463 if (ndo->ndo_packettype == PT_CARP) {
464 if (ndo->ndo_vflag)
465 ND_PRINT((ndo, "carp %s > %s: ",
466 ipaddr_string(ndo, &ipds->ip->ip_src),
467 ipaddr_string(ndo, &ipds->ip->ip_dst)));
468 carp_print(ndo, ipds->cp, ipds->len, ipds->ip->ip_ttl);
469 } else {
470 if (ndo->ndo_vflag)
471 ND_PRINT((ndo, "vrrp %s > %s: ",
472 ipaddr_string(ndo, &ipds->ip->ip_src),
473 ipaddr_string(ndo, &ipds->ip->ip_dst)));
474 vrrp_print(ndo, ipds->cp, ipds->len,
475 (const u_char *)ipds->ip, ipds->ip->ip_ttl);
476 }
477 break;
478
479 case IPPROTO_PGM:
480 pgm_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip);
481 break;
482
483 default:
484 if (ndo->ndo_nflag==0 && (proto = getprotobynumber(ipds->nh)) != NULL)
485 ND_PRINT((ndo, " %s", proto->p_name));
486 else
487 ND_PRINT((ndo, " ip-proto-%d", ipds->nh));
488 ND_PRINT((ndo, " %d", ipds->len));
489 break;
490 }
491 }
492
493 void
494 ip_print_inner(netdissect_options *ndo,
495 const u_char *bp,
496 u_int length, u_int nh,
497 const u_char *bp2)
498 {
499 struct ip_print_demux_state ipd;
500
501 ipd.ip = (const struct ip *)bp2;
502 ipd.cp = bp;
503 ipd.len = length;
504 ipd.off = 0;
505 ipd.nh = nh;
506 ipd.advance = 0;
507
508 ip_print_demux(ndo, &ipd);
509 }
510
511
512 /*
513 * print an IP datagram.
514 */
515 void
516 ip_print(netdissect_options *ndo,
517 const u_char *bp,
518 u_int length)
519 {
520 struct ip_print_demux_state ipd;
521 struct ip_print_demux_state *ipds=&ipd;
522 const u_char *ipend;
523 u_int hlen;
524 struct cksum_vec vec[1];
525 uint16_t sum, ip_sum;
526 struct protoent *proto;
527
528 ipds->ip = (const struct ip *)bp;
529 ND_TCHECK(ipds->ip->ip_vhl);
530 if (IP_V(ipds->ip) != 4) { /* print version if != 4 */
531 if (IP_V(ipds->ip) == 6)
532 ND_PRINT((ndo, "IP6, wrong link-layer encapsulation "));
533 else
534 ND_PRINT((ndo, "IP%u ", IP_V(ipds->ip)));
535 }
536 else if (!ndo->ndo_eflag)
537 ND_PRINT((ndo, "IP "));
538
539 ND_TCHECK(*ipds->ip);
540 if (length < sizeof (struct ip)) {
541 ND_PRINT((ndo, "truncated-ip %u", length));
542 return;
543 }
544 hlen = IP_HL(ipds->ip) * 4;
545 if (hlen < sizeof (struct ip)) {
546 ND_PRINT((ndo, "bad-hlen %u", hlen));
547 return;
548 }
549
550 ipds->len = EXTRACT_16BITS(&ipds->ip->ip_len);
551 if (length < ipds->len)
552 ND_PRINT((ndo, "truncated-ip - %u bytes missing! ",
553 ipds->len - length));
554 if (ipds->len < hlen) {
555 #ifdef GUESS_TSO
556 if (ipds->len) {
557 ND_PRINT((ndo, "bad-len %u", ipds->len));
558 return;
559 }
560 else {
561 /* we guess that it is a TSO send */
562 ipds->len = length;
563 }
564 #else
565 ND_PRINT((ndo, "bad-len %u", ipds->len));
566 return;
567 #endif /* GUESS_TSO */
568 }
569
570 /*
571 * Cut off the snapshot length to the end of the IP payload.
572 */
573 ipend = bp + ipds->len;
574 if (ipend < ndo->ndo_snapend)
575 ndo->ndo_snapend = ipend;
576
577 ipds->len -= hlen;
578
579 ipds->off = EXTRACT_16BITS(&ipds->ip->ip_off);
580
581 if (ndo->ndo_vflag) {
582 ND_PRINT((ndo, "(tos 0x%x", (int)ipds->ip->ip_tos));
583 /* ECN bits */
584 if (ipds->ip->ip_tos & 0x03) {
585 switch (ipds->ip->ip_tos & 0x03) {
586 case 1:
587 ND_PRINT((ndo, ",ECT(1)"));
588 break;
589 case 2:
590 ND_PRINT((ndo, ",ECT(0)"));
591 break;
592 case 3:
593 ND_PRINT((ndo, ",CE"));
594 }
595 }
596
597 if (ipds->ip->ip_ttl >= 1)
598 ND_PRINT((ndo, ", ttl %u", ipds->ip->ip_ttl));
599
600 /*
601 * for the firewall guys, print id, offset.
602 * On all but the last stick a "+" in the flags portion.
603 * For unfragmented datagrams, note the don't fragment flag.
604 */
605
606 ND_PRINT((ndo, ", id %u, offset %u, flags [%s], proto %s (%u)",
607 EXTRACT_16BITS(&ipds->ip->ip_id),
608 (ipds->off & 0x1fff) * 8,
609 bittok2str(ip_frag_values, "none", ipds->off&0xe000),
610 tok2str(ipproto_values,"unknown",ipds->ip->ip_p),
611 ipds->ip->ip_p));
612
613 ND_PRINT((ndo, ", length %u", EXTRACT_16BITS(&ipds->ip->ip_len)));
614
615 if ((hlen - sizeof(struct ip)) > 0) {
616 ND_PRINT((ndo, ", options ("));
617 ip_optprint(ndo, (const u_char *)(ipds->ip + 1), hlen - sizeof(struct ip));
618 ND_PRINT((ndo, ")"));
619 }
620
621 if (!ndo->ndo_Kflag && (const u_char *)ipds->ip + hlen <= ndo->ndo_snapend) {
622 vec[0].ptr = (const uint8_t *)(const void *)ipds->ip;
623 vec[0].len = hlen;
624 sum = in_cksum(vec, 1);
625 if (sum != 0) {
626 ip_sum = EXTRACT_16BITS(&ipds->ip->ip_sum);
627 ND_PRINT((ndo, ", bad cksum %x (->%x)!", ip_sum,
628 in_cksum_shouldbe(ip_sum, sum)));
629 }
630 }
631
632 ND_PRINT((ndo, ")\n "));
633 }
634
635 /*
636 * If this is fragment zero, hand it to the next higher
637 * level protocol.
638 */
639 if ((ipds->off & 0x1fff) == 0) {
640 ipds->cp = (const u_char *)ipds->ip + hlen;
641 ipds->nh = ipds->ip->ip_p;
642
643 if (ipds->nh != IPPROTO_TCP && ipds->nh != IPPROTO_UDP &&
644 ipds->nh != IPPROTO_SCTP && ipds->nh != IPPROTO_DCCP) {
645 ND_PRINT((ndo, "%s > %s: ",
646 ipaddr_string(ndo, &ipds->ip->ip_src),
647 ipaddr_string(ndo, &ipds->ip->ip_dst)));
648 }
649 ip_print_demux(ndo, ipds);
650 } else {
651 /* Ultra quiet now means that all this stuff should be suppressed */
652 if (ndo->ndo_qflag > 1) return;
653
654 /*
655 * if this isn't the first frag, we're missing the
656 * next level protocol header. print the ip addr
657 * and the protocol.
658 */
659 if (ipds->off & 0x1fff) {
660 ND_PRINT((ndo, "%s > %s:", ipaddr_string(ndo, &ipds->ip->ip_src),
661 ipaddr_string(ndo, &ipds->ip->ip_dst)));
662 if (!ndo->ndo_nflag && (proto = getprotobynumber(ipds->ip->ip_p)) != NULL)
663 ND_PRINT((ndo, " %s", proto->p_name));
664 else
665 ND_PRINT((ndo, " ip-proto-%d", ipds->ip->ip_p));
666 }
667 }
668 return;
669
670 trunc:
671 ND_PRINT((ndo, "%s", tstr));
672 return;
673 }
674
675 void
676 ipN_print(netdissect_options *ndo, register const u_char *bp, register u_int length)
677 {
678 struct ip hdr;
679
680 if (length < 4) {
681 ND_PRINT((ndo, "truncated-ip %d", length));
682 return;
683 }
684 memcpy (&hdr, bp, 4);
685 switch (IP_V(&hdr)) {
686 case 4:
687 ip_print (ndo, bp, length);
688 return;
689 case 6:
690 ip6_print (ndo, bp, length);
691 return;
692 default:
693 ND_PRINT((ndo, "unknown ip %d", IP_V(&hdr)));
694 return;
695 }
696 }
697
698 /*
699 * Local Variables:
700 * c-style: whitesmith
701 * c-basic-offset: 8
702 * End:
703 */
704
705