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