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