]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
Squelch a Coverity warning.
[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 <netdissect-stdinc.h>
27
28 #include <string.h>
29
30 #include "netdissect.h"
31 #include "addrtoname.h"
32 #include "extract.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, 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, sizeof(uint32_t));
151 if (IP_HL(ip) == 5)
152 UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst, 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
327 again:
328 switch (ipds->nh) {
329
330 case IPPROTO_AH:
331 ipds->nh = *ipds->cp;
332 ipds->advance = ah_print(ndo, ipds->cp);
333 if (ipds->advance <= 0)
334 break;
335 ipds->cp += ipds->advance;
336 ipds->len -= ipds->advance;
337 goto again;
338
339 case IPPROTO_ESP:
340 {
341 int enh, padlen;
342 ipds->advance = esp_print(ndo, ipds->cp, ipds->len,
343 (const u_char *)ipds->ip,
344 &enh, &padlen);
345 if (ipds->advance <= 0)
346 break;
347 ipds->cp += ipds->advance;
348 ipds->len -= ipds->advance + padlen;
349 ipds->nh = enh & 0xff;
350 goto again;
351 }
352
353 case IPPROTO_IPCOMP:
354 {
355 int enh;
356 ipds->advance = ipcomp_print(ndo, ipds->cp, &enh);
357 if (ipds->advance <= 0)
358 break;
359 ipds->cp += ipds->advance;
360 ipds->len -= ipds->advance;
361 ipds->nh = enh & 0xff;
362 goto again;
363 }
364
365 case IPPROTO_SCTP:
366 sctp_print(ndo, ipds->cp, (const u_char *)ipds->ip, ipds->len);
367 break;
368
369 case IPPROTO_DCCP:
370 dccp_print(ndo, ipds->cp, (const u_char *)ipds->ip, ipds->len);
371 break;
372
373 case IPPROTO_TCP:
374 /* pass on the MF bit plus the offset to detect fragments */
375 tcp_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip,
376 ipds->off & (IP_MF|IP_OFFMASK));
377 break;
378
379 case IPPROTO_UDP:
380 /* pass on the MF bit plus the offset to detect fragments */
381 udp_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip,
382 ipds->off & (IP_MF|IP_OFFMASK));
383 break;
384
385 case IPPROTO_ICMP:
386 /* pass on the MF bit plus the offset to detect fragments */
387 icmp_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip,
388 ipds->off & (IP_MF|IP_OFFMASK));
389 break;
390
391 case IPPROTO_PIGP:
392 /*
393 * XXX - the current IANA protocol number assignments
394 * page lists 9 as "any private interior gateway
395 * (used by Cisco for their IGRP)" and 88 as
396 * "EIGRP" from Cisco.
397 *
398 * Recent BSD <netinet/in.h> headers define
399 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
400 * We define IP_PROTO_PIGP as 9 and
401 * IP_PROTO_EIGRP as 88; those names better
402 * match was the current protocol number
403 * assignments say.
404 */
405 igrp_print(ndo, ipds->cp, ipds->len);
406 break;
407
408 case IPPROTO_EIGRP:
409 eigrp_print(ndo, ipds->cp, ipds->len);
410 break;
411
412 case IPPROTO_ND:
413 ND_PRINT((ndo, " nd %d", ipds->len));
414 break;
415
416 case IPPROTO_EGP:
417 egp_print(ndo, ipds->cp, ipds->len);
418 break;
419
420 case IPPROTO_OSPF:
421 ospf_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip);
422 break;
423
424 case IPPROTO_IGMP:
425 igmp_print(ndo, ipds->cp, ipds->len);
426 break;
427
428 case IPPROTO_IPV4:
429 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
430 ip_print(ndo, ipds->cp, ipds->len);
431 if (! ndo->ndo_vflag) {
432 ND_PRINT((ndo, " (ipip-proto-4)"));
433 return;
434 }
435 break;
436
437 case IPPROTO_IPV6:
438 /* ip6-in-ip encapsulation */
439 ip6_print(ndo, ipds->cp, ipds->len);
440 break;
441
442 case IPPROTO_RSVP:
443 rsvp_print(ndo, ipds->cp, ipds->len);
444 break;
445
446 case IPPROTO_GRE:
447 /* do it */
448 gre_print(ndo, ipds->cp, ipds->len);
449 break;
450
451 case IPPROTO_MOBILE:
452 mobile_print(ndo, ipds->cp, ipds->len);
453 break;
454
455 case IPPROTO_PIM:
456 pim_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip);
457 break;
458
459 case IPPROTO_VRRP:
460 if (ndo->ndo_packettype == PT_CARP) {
461 if (ndo->ndo_vflag)
462 ND_PRINT((ndo, "carp %s > %s: ",
463 ipaddr_string(ndo, &ipds->ip->ip_src),
464 ipaddr_string(ndo, &ipds->ip->ip_dst)));
465 carp_print(ndo, ipds->cp, ipds->len, ipds->ip->ip_ttl);
466 } else {
467 if (ndo->ndo_vflag)
468 ND_PRINT((ndo, "vrrp %s > %s: ",
469 ipaddr_string(ndo, &ipds->ip->ip_src),
470 ipaddr_string(ndo, &ipds->ip->ip_dst)));
471 vrrp_print(ndo, ipds->cp, ipds->len,
472 (const u_char *)ipds->ip, ipds->ip->ip_ttl);
473 }
474 break;
475
476 case IPPROTO_PGM:
477 pgm_print(ndo, ipds->cp, ipds->len, (const u_char *)ipds->ip);
478 break;
479
480 default:
481 if (ndo->ndo_nflag==0 && (proto = getprotobynumber(ipds->nh)) != NULL)
482 ND_PRINT((ndo, " %s", proto->p_name));
483 else
484 ND_PRINT((ndo, " ip-proto-%d", ipds->nh));
485 ND_PRINT((ndo, " %d", ipds->len));
486 break;
487 }
488 }
489
490 void
491 ip_print_inner(netdissect_options *ndo,
492 const u_char *bp,
493 u_int length, u_int nh,
494 const u_char *bp2)
495 {
496 struct ip_print_demux_state ipd;
497
498 ipd.ip = (const struct ip *)bp2;
499 ipd.cp = bp;
500 ipd.len = length;
501 ipd.off = 0;
502 ipd.nh = nh;
503 ipd.advance = 0;
504
505 ip_print_demux(ndo, &ipd);
506 }
507
508
509 /*
510 * print an IP datagram.
511 */
512 void
513 ip_print(netdissect_options *ndo,
514 const u_char *bp,
515 u_int length)
516 {
517 struct ip_print_demux_state ipd;
518 struct ip_print_demux_state *ipds=&ipd;
519 const u_char *ipend;
520 u_int hlen;
521 struct cksum_vec vec[1];
522 uint16_t sum, ip_sum;
523 struct protoent *proto;
524
525 ipds->ip = (const struct ip *)bp;
526 ND_TCHECK(ipds->ip->ip_vhl);
527 if (IP_V(ipds->ip) != 4) { /* print version if != 4 */
528 if (IP_V(ipds->ip) == 6)
529 ND_PRINT((ndo, "IP6, wrong link-layer encapsulation "));
530 else
531 ND_PRINT((ndo, "IP%u ", IP_V(ipds->ip)));
532 }
533 else if (!ndo->ndo_eflag)
534 ND_PRINT((ndo, "IP "));
535
536 ND_TCHECK(*ipds->ip);
537 if (length < sizeof (struct ip)) {
538 ND_PRINT((ndo, "truncated-ip %u", length));
539 return;
540 }
541 hlen = IP_HL(ipds->ip) * 4;
542 if (hlen < sizeof (struct ip)) {
543 ND_PRINT((ndo, "bad-hlen %u", hlen));
544 return;
545 }
546
547 ipds->len = EXTRACT_16BITS(&ipds->ip->ip_len);
548 if (length < ipds->len)
549 ND_PRINT((ndo, "truncated-ip - %u bytes missing! ",
550 ipds->len - length));
551 if (ipds->len < hlen) {
552 #ifdef GUESS_TSO
553 if (ipds->len) {
554 ND_PRINT((ndo, "bad-len %u", ipds->len));
555 return;
556 }
557 else {
558 /* we guess that it is a TSO send */
559 ipds->len = length;
560 }
561 #else
562 ND_PRINT((ndo, "bad-len %u", ipds->len));
563 return;
564 #endif /* GUESS_TSO */
565 }
566
567 /*
568 * Cut off the snapshot length to the end of the IP payload.
569 */
570 ipend = bp + ipds->len;
571 if (ipend < ndo->ndo_snapend)
572 ndo->ndo_snapend = ipend;
573
574 ipds->len -= hlen;
575
576 ipds->off = EXTRACT_16BITS(&ipds->ip->ip_off);
577
578 if (ndo->ndo_vflag) {
579 ND_PRINT((ndo, "(tos 0x%x", (int)ipds->ip->ip_tos));
580 /* ECN bits */
581 if (ipds->ip->ip_tos & 0x03) {
582 switch (ipds->ip->ip_tos & 0x03) {
583 case 1:
584 ND_PRINT((ndo, ",ECT(1)"));
585 break;
586 case 2:
587 ND_PRINT((ndo, ",ECT(0)"));
588 break;
589 case 3:
590 ND_PRINT((ndo, ",CE"));
591 }
592 }
593
594 if (ipds->ip->ip_ttl >= 1)
595 ND_PRINT((ndo, ", ttl %u", ipds->ip->ip_ttl));
596
597 /*
598 * for the firewall guys, print id, offset.
599 * On all but the last stick a "+" in the flags portion.
600 * For unfragmented datagrams, note the don't fragment flag.
601 */
602
603 ND_PRINT((ndo, ", id %u, offset %u, flags [%s], proto %s (%u)",
604 EXTRACT_16BITS(&ipds->ip->ip_id),
605 (ipds->off & 0x1fff) * 8,
606 bittok2str(ip_frag_values, "none", ipds->off&0xe000),
607 tok2str(ipproto_values,"unknown",ipds->ip->ip_p),
608 ipds->ip->ip_p));
609
610 ND_PRINT((ndo, ", length %u", EXTRACT_16BITS(&ipds->ip->ip_len)));
611
612 if ((hlen - sizeof(struct ip)) > 0) {
613 ND_PRINT((ndo, ", options ("));
614 ip_optprint(ndo, (const u_char *)(ipds->ip + 1), hlen - sizeof(struct ip));
615 ND_PRINT((ndo, ")"));
616 }
617
618 if (!ndo->ndo_Kflag && (const u_char *)ipds->ip + hlen <= ndo->ndo_snapend) {
619 vec[0].ptr = (const uint8_t *)(const void *)ipds->ip;
620 vec[0].len = hlen;
621 sum = in_cksum(vec, 1);
622 if (sum != 0) {
623 ip_sum = EXTRACT_16BITS(&ipds->ip->ip_sum);
624 ND_PRINT((ndo, ", bad cksum %x (->%x)!", ip_sum,
625 in_cksum_shouldbe(ip_sum, sum)));
626 }
627 }
628
629 ND_PRINT((ndo, ")\n "));
630 }
631
632 /*
633 * If this is fragment zero, hand it to the next higher
634 * level protocol.
635 */
636 if ((ipds->off & 0x1fff) == 0) {
637 ipds->cp = (const u_char *)ipds->ip + hlen;
638 ipds->nh = ipds->ip->ip_p;
639
640 if (ipds->nh != IPPROTO_TCP && ipds->nh != IPPROTO_UDP &&
641 ipds->nh != IPPROTO_SCTP && ipds->nh != IPPROTO_DCCP) {
642 ND_PRINT((ndo, "%s > %s: ",
643 ipaddr_string(ndo, &ipds->ip->ip_src),
644 ipaddr_string(ndo, &ipds->ip->ip_dst)));
645 }
646 ip_print_demux(ndo, ipds);
647 } else {
648 /* Ultra quiet now means that all this stuff should be suppressed */
649 if (ndo->ndo_qflag > 1) return;
650
651 /*
652 * if this isn't the first frag, we're missing the
653 * next level protocol header. print the ip addr
654 * and the protocol.
655 */
656 if (ipds->off & 0x1fff) {
657 ND_PRINT((ndo, "%s > %s:", ipaddr_string(ndo, &ipds->ip->ip_src),
658 ipaddr_string(ndo, &ipds->ip->ip_dst)));
659 if (!ndo->ndo_nflag && (proto = getprotobynumber(ipds->ip->ip_p)) != NULL)
660 ND_PRINT((ndo, " %s", proto->p_name));
661 else
662 ND_PRINT((ndo, " ip-proto-%d", ipds->ip->ip_p));
663 }
664 }
665 return;
666
667 trunc:
668 ND_PRINT((ndo, "%s", tstr));
669 return;
670 }
671
672 void
673 ipN_print(netdissect_options *ndo, register const u_char *bp, register u_int length)
674 {
675 struct ip hdr;
676
677 if (length < 4) {
678 ND_PRINT((ndo, "truncated-ip %d", length));
679 return;
680 }
681 memcpy (&hdr, bp, 4);
682 switch (IP_V(&hdr)) {
683 case 4:
684 ip_print (ndo, bp, length);
685 return;
686 case 6:
687 ip6_print (ndo, bp, length);
688 return;
689 default:
690 ND_PRINT((ndo, "unknown ip %d", IP_V(&hdr)));
691 return;
692 }
693 }
694
695 /*
696 * Local Variables:
697 * c-style: whitesmith
698 * c-basic-offset: 8
699 * End:
700 */
701
702