]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
added explicit protocol number to "ipip" - John Hawkinson <[email protected]>
[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 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.97 2001-05-29 15:35:20 mcr Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include "addrtoname.h"
43 #include "interface.h"
44 #include "extract.h" /* must come after interface.h */
45
46 #include "ip.h"
47
48 /* Compatibility */
49 #ifndef IPPROTO_ND
50 #define IPPROTO_ND 77
51 #endif
52
53 /*
54 * print the recorded route in an IP RR, LSRR or SSRR option.
55 */
56 static void
57 ip_printroute(const char *type, register const u_char *cp, u_int length)
58 {
59 register u_int ptr = cp[2] - 1;
60 register u_int len;
61
62 printf(" %s{", type);
63 if ((length + 1) & 3)
64 printf(" [bad length %d]", length);
65 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
66 printf(" [bad ptr %d]", cp[2]);
67
68 type = "";
69 for (len = 3; len < length; len += 4) {
70 if (ptr == len)
71 type = "#";
72 printf("%s%s", type, ipaddr_string(&cp[len]));
73 type = " ";
74 }
75 printf("%s}", ptr == len? "#" : "");
76 }
77
78 static void
79 ip_printts(register const u_char *cp, u_int length)
80 {
81 register u_int ptr = cp[2] - 1;
82 register u_int len = 0;
83 int hoplen;
84 char *type;
85
86 printf(" TS{");
87 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
88 if ((length - 4) & (hoplen-1))
89 printf("[bad length %d]", length);
90 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
91 printf("[bad ptr %d]", cp[2]);
92 switch (cp[3]&0xF) {
93 case IPOPT_TS_TSONLY:
94 printf("TSONLY");
95 break;
96 case IPOPT_TS_TSANDADDR:
97 printf("TS+ADDR");
98 break;
99 /*
100 * prespecified should really be 3, but some ones might send 2
101 * instead, and the IPOPT_TS_PRESPEC constant can apparently
102 * have both values, so we have to hard-code it here.
103 */
104
105 case 2:
106 printf("PRESPEC2.0");
107 break;
108 case 3: /* IPOPT_TS_PRESPEC */
109 printf("PRESPEC");
110 break;
111 default:
112 printf("[bad ts type %d]", cp[3]&0xF);
113 goto done;
114 }
115
116 type = " ";
117 for (len = 4; len < length; len += hoplen) {
118 if (ptr == len)
119 type = " ^ ";
120 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
121 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
122 type = " ";
123 }
124
125 done:
126 printf("%s", ptr == len ? " ^ " : "");
127
128 if (cp[3]>>4)
129 printf(" [%d hops not recorded]} ", cp[3]>>4);
130 else
131 printf("}");
132 }
133
134 /*
135 * print IP options.
136 */
137 static void
138 ip_optprint(register const u_char *cp, u_int length)
139 {
140 register u_int len;
141
142 for (; length > 0; cp += len, length -= len) {
143 int tt = *cp;
144
145 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
146 len = 1;
147 else {
148 if (&cp[1] >= snapend) {
149 printf("[|ip]");
150 return;
151 }
152 len = cp[1];
153 }
154 if (len <= 0) {
155 printf("[|ip op len %d]", len);
156 return;
157 }
158 if (&cp[1] >= snapend || cp + len > snapend) {
159 printf("[|ip]");
160 return;
161 }
162 switch (tt) {
163
164 case IPOPT_EOL:
165 printf(" EOL");
166 if (length > 1)
167 printf("-%d", length - 1);
168 return;
169
170 case IPOPT_NOP:
171 printf(" NOP");
172 break;
173
174 case IPOPT_TS:
175 ip_printts(cp, len);
176 break;
177
178 #ifndef IPOPT_SECURITY
179 #define IPOPT_SECURITY 130
180 #endif /* IPOPT_SECURITY */
181 case IPOPT_SECURITY:
182 printf(" SECURITY{%d}", len);
183 break;
184
185 case IPOPT_RR:
186 ip_printroute("RR", cp, len);
187 break;
188
189 case IPOPT_SSRR:
190 ip_printroute("SSRR", cp, len);
191 break;
192
193 case IPOPT_LSRR:
194 ip_printroute("LSRR", cp, len);
195 break;
196
197 #ifndef IPOPT_RA
198 #define IPOPT_RA 148 /* router alert */
199 #endif
200 case IPOPT_RA:
201 printf(" RA");
202 if (len != 4)
203 printf("{%d}", len);
204 else if (cp[2] || cp[3])
205 printf("%d.%d", cp[2], cp[3]);
206 break;
207
208 default:
209 printf(" IPOPT-%d{%d}", cp[0], len);
210 break;
211 }
212 }
213 }
214
215 /*
216 * compute an IP header checksum.
217 * don't modifiy the packet.
218 */
219 u_short
220 in_cksum(const u_short *addr, register int len, u_short csum)
221 {
222 int nleft = len;
223 const u_short *w = addr;
224 u_short answer;
225 int sum = csum;
226
227 /*
228 * Our algorithm is simple, using a 32 bit accumulator (sum),
229 * we add sequential 16 bit words to it, and at the end, fold
230 * back all the carry bits from the top 16 bits into the lower
231 * 16 bits.
232 */
233 while (nleft > 1) {
234 sum += *w++;
235 nleft -= 2;
236 }
237 if (nleft == 1)
238 sum += htons(*(u_char *)w<<8);
239
240 /*
241 * add back carry outs from top 16 bits to low 16 bits
242 */
243 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
244 sum += (sum >> 16); /* add carry */
245 answer = ~sum; /* truncate to 16 bits */
246 return (answer);
247 }
248
249 /*
250 * print an IP datagram.
251 */
252 void
253 ip_print(register const u_char *bp, register u_int length)
254 {
255 register const struct ip *ip;
256 register u_int hlen, len, len0, off;
257 register const u_char *cp;
258 u_char nh;
259 int advance;
260
261 ip = (const struct ip *)bp;
262 #ifdef LBL_ALIGN
263 /*
264 * If the IP header is not aligned, copy into abuf.
265 * This will never happen with BPF. It does happen raw packet
266 * dumps from -r.
267 */
268 if ((long)ip & 3) {
269 static u_char *abuf = NULL;
270 static int didwarn = 0;
271
272 if (abuf == NULL) {
273 abuf = (u_char *)malloc(snaplen);
274 if (abuf == NULL)
275 error("ip_print: malloc");
276 }
277 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
278 snapend += abuf - (u_char *)ip;
279 packetp = abuf;
280 ip = (struct ip *)abuf;
281 /* We really want libpcap to give us aligned packets */
282 if (!didwarn) {
283 warning("compensating for unaligned libpcap packets");
284 ++didwarn;
285 }
286 }
287 #endif
288 if ((u_char *)(ip + 1) > snapend) {
289 printf("[|ip]");
290 return;
291 }
292 if (length < sizeof (struct ip)) {
293 (void)printf("truncated-ip %d", length);
294 return;
295 }
296 hlen = IP_HL(ip) * 4;
297 if (hlen < sizeof (struct ip)) {
298 (void)printf("bad-hlen %d", hlen);
299 return;
300 }
301
302 len = ntohs(ip->ip_len);
303 if (length < len)
304 (void)printf("truncated-ip - %d bytes missing! ",
305 len - length);
306 len -= hlen;
307 len0 = len;
308
309 /*
310 * If this is fragment zero, hand it to the next higher
311 * level protocol.
312 */
313 off = ntohs(ip->ip_off);
314 if ((off & 0x1fff) == 0) {
315 cp = (const u_char *)ip + hlen;
316 nh = ip->ip_p;
317
318 #ifndef IPPROTO_SCTP
319 #define IPPROTO_SCTP 132
320 #endif
321 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
322 nh != IPPROTO_SCTP) {
323 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
324 ipaddr_string(&ip->ip_dst));
325 }
326 again:
327 switch (nh) {
328
329 #ifndef IPPROTO_AH
330 #define IPPROTO_AH 51
331 #endif
332 case IPPROTO_AH:
333 nh = *cp;
334 advance = ah_print(cp, (const u_char *)ip);
335 cp += advance;
336 len -= advance;
337 goto again;
338
339 #ifndef IPPROTO_ESP
340 #define IPPROTO_ESP 50
341 #endif
342 case IPPROTO_ESP:
343 {
344 int enh;
345 advance = esp_print(cp, (const u_char *)ip, &enh);
346 cp += advance;
347 len -= advance;
348 if (enh < 0)
349 break;
350 nh = enh & 0xff;
351 goto again;
352 }
353
354 #ifndef IPPROTO_IPCOMP
355 #define IPPROTO_IPCOMP 108
356 #endif
357 case IPPROTO_IPCOMP:
358 {
359 int enh;
360 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
361 cp += advance;
362 len -= advance;
363 if (enh < 0)
364 break;
365 nh = enh & 0xff;
366 goto again;
367 }
368
369 case IPPROTO_SCTP:
370 sctp_print(cp, (const u_char *)ip, len);
371 break;
372
373 case IPPROTO_TCP:
374 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
375 break;
376
377 case IPPROTO_UDP:
378 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
379 break;
380
381 case IPPROTO_ICMP:
382 icmp_print(cp, len, (const u_char *)ip);
383 break;
384
385 #ifndef IPPROTO_IGRP
386 #define IPPROTO_IGRP 9
387 #endif
388 case IPPROTO_IGRP:
389 igrp_print(cp, len, (const u_char *)ip);
390 break;
391
392 case IPPROTO_ND:
393 #if 0
394 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
395 ipaddr_string(&ip->ip_dst));
396 #endif
397 (void)printf(" nd %d", len);
398 break;
399
400 case IPPROTO_EGP:
401 egp_print(cp, len, (const u_char *)ip);
402 break;
403
404 #ifndef IPPROTO_OSPF
405 #define IPPROTO_OSPF 89
406 #endif
407 case IPPROTO_OSPF:
408 ospf_print(cp, len, (const u_char *)ip);
409 break;
410
411 #ifndef IPPROTO_IGMP
412 #define IPPROTO_IGMP 2
413 #endif
414 case IPPROTO_IGMP:
415 igmp_print(cp, len, (const u_char *)ip);
416 break;
417
418 case 4:
419 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
420 #if 0
421 if (vflag)
422 (void)printf("%s > %s: ",
423 ipaddr_string(&ip->ip_src),
424 ipaddr_string(&ip->ip_dst));
425 #endif
426 ip_print(cp, len);
427 if (! vflag) {
428 printf(" (ipip-proto-4)");
429 return;
430 }
431 break;
432
433 #ifdef INET6
434 #ifndef IP6PROTO_ENCAP
435 #define IP6PROTO_ENCAP 41
436 #endif
437 case IP6PROTO_ENCAP:
438 /* ip6-in-ip encapsulation */
439 #if 0
440 if (vflag)
441 (void)printf("%s > %s: ",
442 ipaddr_string(&ip->ip_src),
443 ipaddr_string(&ip->ip_dst));
444 #endif
445 ip6_print(cp, len);
446 if (! vflag) {
447 printf(" (encap)");
448 return;
449 }
450 break;
451 #endif /*INET6*/
452
453
454 #ifndef IPPROTO_GRE
455 #define IPPROTO_GRE 47
456 #endif
457 case IPPROTO_GRE:
458 if (vflag)
459 (void)printf("gre %s > %s: ",
460 ipaddr_string(&ip->ip_src),
461 ipaddr_string(&ip->ip_dst));
462 /* do it */
463 gre_print(cp, len);
464 if (! vflag) {
465 printf(" (gre encap)");
466 return;
467 }
468 break;
469
470 #ifndef IPPROTO_MOBILE
471 #define IPPROTO_MOBILE 55
472 #endif
473 case IPPROTO_MOBILE:
474 if (vflag)
475 (void)printf("mobile %s > %s: ",
476 ipaddr_string(&ip->ip_src),
477 ipaddr_string(&ip->ip_dst));
478 mobile_print(cp, len);
479 if (! vflag) {
480 printf(" (mobile encap)");
481 return;
482 }
483 break;
484
485 #ifndef IPPROTO_PIM
486 #define IPPROTO_PIM 103
487 #endif
488 case IPPROTO_PIM:
489 pim_print(cp, len);
490 break;
491
492 #ifndef IPPROTO_VRRP
493 #define IPPROTO_VRRP 112
494 #endif
495 case IPPROTO_VRRP:
496 if (vflag)
497 (void)printf("vrrp %s > %s: ",
498 ipaddr_string(&ip->ip_src),
499 ipaddr_string(&ip->ip_dst));
500 vrrp_print(cp, len, ip->ip_ttl);
501 break;
502
503 default:
504 #if 0
505 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
506 ipaddr_string(&ip->ip_dst));
507 #endif
508 (void)printf(" ip-proto-%d %d", nh, len);
509 break;
510 }
511 }
512
513 /* Ultra quiet now means that all this stuff should be suppressed */
514 /* res 3-Nov-98 */
515 if (qflag > 1) return;
516
517
518 /*
519 * for fragmented datagrams, print id:size@offset. On all
520 * but the last stick a "+". For unfragmented datagrams, note
521 * the don't fragment flag.
522 */
523 len = len0; /* get the original length */
524 if (off & 0x3fff) {
525 /*
526 * if this isn't the first frag, we're missing the
527 * next level protocol header. print the ip addr.
528 */
529 if (off & 0x1fff)
530 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
531 ipaddr_string(&ip->ip_dst));
532 #ifndef IP_MF
533 #define IP_MF 0x2000
534 #endif /* IP_MF */
535 #ifndef IP_DF
536 #define IP_DF 0x4000
537 #endif /* IP_DF */
538 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
539 (off & 0x1fff) * 8,
540 (off & IP_MF)? "+" : "");
541
542 } else if (off & IP_DF)
543 (void)printf(" (DF)");
544
545 if (ip->ip_tos) {
546 (void)printf(" [tos 0x%x", (int)ip->ip_tos);
547 /* ECN bits */
548 if (ip->ip_tos & 0x03) {
549 switch (ip->ip_tos & 0x03) {
550 case 1:
551 (void)printf(",ECT(1)");
552 break;
553 case 2:
554 (void)printf(",ECT(0)");
555 break;
556 case 3:
557 (void)printf(",CE");
558 }
559 }
560 (void)printf("] ");
561 }
562
563 if (ip->ip_ttl <= 1)
564 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
565
566 if (vflag) {
567 int sum;
568 char *sep = "";
569
570 printf(" (");
571 if (ip->ip_ttl > 1) {
572 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
573 sep = ", ";
574 }
575 if ((off & 0x3fff) == 0) {
576 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
577 sep = ", ";
578 }
579 (void)printf("%slen %d", sep, (int)ntohs(ip->ip_len));
580 sep = ", ";
581 if ((u_char *)ip + hlen <= snapend) {
582 sum = in_cksum((const u_short *)ip, hlen, 0);
583 if (sum != 0) {
584 (void)printf("%sbad cksum %x!", sep,
585 ntohs(ip->ip_sum));
586 sep = ", ";
587 }
588 }
589 if ((hlen -= sizeof(struct ip)) > 0) {
590 (void)printf("%soptlen=%d", sep, hlen);
591 ip_optprint((u_char *)(ip + 1), hlen);
592 }
593 printf(")");
594 }
595 }
596
597 void
598 ipN_print(register const u_char *bp, register u_int length)
599 {
600 struct ip *ip, hdr;
601
602 ip = (struct ip *)bp;
603 if (length < 4) {
604 (void)printf("truncated-ip %d", length);
605 return;
606 }
607 memcpy (&hdr, (char *)ip, 4);
608 switch (IP_V(&hdr)) {
609 case 4:
610 ip_print (bp, length);
611 return;
612 #ifdef INET6
613 case 6:
614 ip6_print (bp, length);
615 return;
616 #endif
617 default:
618 (void)printf("unknown ip %d", IP_V(&hdr));
619 return;
620 }
621 }