]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
whitespace cleanup
[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.105 2002-06-11 17:08:49 itojun 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 <netdb.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "addrtoname.h"
44 #include "interface.h"
45 #include "extract.h" /* must come after interface.h */
46
47 #include "ip.h"
48
49 /* Compatibility */
50 #ifndef IPPROTO_ND
51 #define IPPROTO_ND 77
52 #endif
53
54 /*
55 * print the recorded route in an IP RR, LSRR or SSRR option.
56 */
57 static void
58 ip_printroute(const char *type, register const u_char *cp, u_int length)
59 {
60 register u_int ptr = cp[2] - 1;
61 register u_int len;
62
63 printf(" %s{", type);
64 if ((length + 1) & 3)
65 printf(" [bad length %d]", length);
66 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
67 printf(" [bad ptr %d]", cp[2]);
68
69 type = "";
70 for (len = 3; len < length; len += 4) {
71 if (ptr == len)
72 type = "#";
73 printf("%s%s", type, ipaddr_string(&cp[len]));
74 type = " ";
75 }
76 printf("%s}", ptr == len? "#" : "");
77 }
78
79 static void
80 ip_printts(register const u_char *cp, u_int length)
81 {
82 register u_int ptr = cp[2] - 1;
83 register u_int len = 0;
84 int hoplen;
85 char *type;
86
87 printf(" TS{");
88 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
89 if ((length - 4) & (hoplen-1))
90 printf("[bad length %d]", length);
91 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
92 printf("[bad ptr %d]", cp[2]);
93 switch (cp[3]&0xF) {
94 case IPOPT_TS_TSONLY:
95 printf("TSONLY");
96 break;
97 case IPOPT_TS_TSANDADDR:
98 printf("TS+ADDR");
99 break;
100 /*
101 * prespecified should really be 3, but some ones might send 2
102 * instead, and the IPOPT_TS_PRESPEC constant can apparently
103 * have both values, so we have to hard-code it here.
104 */
105
106 case 2:
107 printf("PRESPEC2.0");
108 break;
109 case 3: /* IPOPT_TS_PRESPEC */
110 printf("PRESPEC");
111 break;
112 default:
113 printf("[bad ts type %d]", cp[3]&0xF);
114 goto done;
115 }
116
117 type = " ";
118 for (len = 4; len < length; len += hoplen) {
119 if (ptr == len)
120 type = " ^ ";
121 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
122 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
123 type = " ";
124 }
125
126 done:
127 printf("%s", ptr == len ? " ^ " : "");
128
129 if (cp[3]>>4)
130 printf(" [%d hops not recorded]} ", cp[3]>>4);
131 else
132 printf("}");
133 }
134
135 /*
136 * print IP options.
137 */
138 static void
139 ip_optprint(register const u_char *cp, u_int length)
140 {
141 register u_int len;
142
143 for (; length > 0; cp += len, length -= len) {
144 int tt = *cp;
145
146 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
147 len = 1;
148 else {
149 if (&cp[1] >= snapend) {
150 printf("[|ip]");
151 return;
152 }
153 len = cp[1];
154 }
155 if (len <= 0) {
156 printf("[|ip op len %d]", len);
157 return;
158 }
159 if (&cp[1] >= snapend || cp + len > snapend) {
160 printf("[|ip]");
161 return;
162 }
163 switch (tt) {
164
165 case IPOPT_EOL:
166 printf(" EOL");
167 if (length > 1)
168 printf("-%d", length - 1);
169 return;
170
171 case IPOPT_NOP:
172 printf(" NOP");
173 break;
174
175 case IPOPT_TS:
176 ip_printts(cp, len);
177 break;
178
179 #ifndef IPOPT_SECURITY
180 #define IPOPT_SECURITY 130
181 #endif /* IPOPT_SECURITY */
182 case IPOPT_SECURITY:
183 printf(" SECURITY{%d}", len);
184 break;
185
186 case IPOPT_RR:
187 ip_printroute("RR", cp, len);
188 break;
189
190 case IPOPT_SSRR:
191 ip_printroute("SSRR", cp, len);
192 break;
193
194 case IPOPT_LSRR:
195 ip_printroute("LSRR", cp, len);
196 break;
197
198 #ifndef IPOPT_RA
199 #define IPOPT_RA 148 /* router alert */
200 #endif
201 case IPOPT_RA:
202 printf(" RA");
203 if (len != 4)
204 printf("{%d}", len);
205 else if (cp[2] || cp[3])
206 printf("%d.%d", cp[2], cp[3]);
207 break;
208
209 default:
210 printf(" IPOPT-%d{%d}", cp[0], len);
211 break;
212 }
213 }
214 }
215
216 /*
217 * compute an IP header checksum.
218 * don't modifiy the packet.
219 */
220 u_short
221 in_cksum(const u_short *addr, register u_int len, int csum)
222 {
223 int nleft = len;
224 const u_short *w = addr;
225 u_short answer;
226 int sum = csum;
227
228 /*
229 * Our algorithm is simple, using a 32 bit accumulator (sum),
230 * we add sequential 16 bit words to it, and at the end, fold
231 * back all the carry bits from the top 16 bits into the lower
232 * 16 bits.
233 */
234 while (nleft > 1) {
235 sum += *w++;
236 nleft -= 2;
237 }
238 if (nleft == 1)
239 sum += htons(*(u_char *)w<<8);
240
241 /*
242 * add back carry outs from top 16 bits to low 16 bits
243 */
244 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
245 sum += (sum >> 16); /* add carry */
246 answer = ~sum; /* truncate to 16 bits */
247 return (answer);
248 }
249
250 /*
251 * print an IP datagram.
252 */
253 void
254 ip_print(register const u_char *bp, register u_int length)
255 {
256 register const struct ip *ip;
257 register u_int hlen, len, len0, off;
258 register const u_char *cp;
259 u_char nh;
260 int advance;
261 struct protoent *proto;
262
263 ip = (const struct ip *)bp;
264 #ifdef LBL_ALIGN
265 /*
266 * If the IP header is not aligned, copy into abuf.
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, padlen;
345 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
346 cp += advance;
347 len -= advance + padlen;
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 (void)printf(" nd %d", len);
394 break;
395
396 case IPPROTO_EGP:
397 egp_print(cp, len, (const u_char *)ip);
398 break;
399
400 #ifndef IPPROTO_OSPF
401 #define IPPROTO_OSPF 89
402 #endif
403 case IPPROTO_OSPF:
404 ospf_print(cp, len, (const u_char *)ip);
405 break;
406
407 #ifndef IPPROTO_IGMP
408 #define IPPROTO_IGMP 2
409 #endif
410 case IPPROTO_IGMP:
411 igmp_print(cp, len);
412 break;
413
414 case 4:
415 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
416 ip_print(cp, len);
417 if (! vflag) {
418 printf(" (ipip-proto-4)");
419 return;
420 }
421 break;
422
423 #ifdef INET6
424 #ifndef IP6PROTO_ENCAP
425 #define IP6PROTO_ENCAP 41
426 #endif
427 case IP6PROTO_ENCAP:
428 /* ip6-in-ip encapsulation */
429 ip6_print(cp, len);
430 break;
431 #endif /*INET6*/
432
433
434 #ifndef IPPROTO_GRE
435 #define IPPROTO_GRE 47
436 #endif
437 case IPPROTO_GRE:
438 /* do it */
439 gre_print(cp, len);
440 break;
441
442 #ifndef IPPROTO_MOBILE
443 #define IPPROTO_MOBILE 55
444 #endif
445 case IPPROTO_MOBILE:
446 mobile_print(cp, len);
447 break;
448
449 #ifndef IPPROTO_PIM
450 #define IPPROTO_PIM 103
451 #endif
452 case IPPROTO_PIM:
453 pim_print(cp, len);
454 break;
455
456 #ifndef IPPROTO_VRRP
457 #define IPPROTO_VRRP 112
458 #endif
459 case IPPROTO_VRRP:
460 vrrp_print(cp, len, ip->ip_ttl);
461 break;
462
463 default:
464 if ((proto = getprotobynumber(nh)) != NULL)
465 (void)printf(" %s", proto->p_name);
466 else
467 (void)printf(" ip-proto-%d", nh);
468 printf(" %d", len);
469 break;
470 }
471 }
472
473 /* Ultra quiet now means that all this stuff should be suppressed */
474 /* res 3-Nov-98 */
475 if (qflag > 1) return;
476
477
478 /*
479 * for fragmented datagrams, print id:size@offset. On all
480 * but the last stick a "+". For unfragmented datagrams, note
481 * the don't fragment flag.
482 */
483 len = len0; /* get the original length */
484 if (off & 0x3fff) {
485 /*
486 * if this isn't the first frag, we're missing the
487 * next level protocol header. print the ip addr
488 * and the protocol.
489 */
490 if (off & 0x1fff) {
491 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
492 ipaddr_string(&ip->ip_dst));
493 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
494 (void)printf(" %s", proto->p_name);
495 else
496 (void)printf(" ip-proto-%d", ip->ip_p);
497 }
498 #ifndef IP_MF
499 #define IP_MF 0x2000
500 #endif /* IP_MF */
501 #ifndef IP_DF
502 #define IP_DF 0x4000
503 #endif /* IP_DF */
504 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
505 (off & 0x1fff) * 8,
506 (off & IP_MF)? "+" : "");
507
508 } else if (off & IP_DF)
509 (void)printf(" (DF)");
510
511 if (ip->ip_tos) {
512 (void)printf(" [tos 0x%x", (int)ip->ip_tos);
513 /* ECN bits */
514 if (ip->ip_tos & 0x03) {
515 switch (ip->ip_tos & 0x03) {
516 case 1:
517 (void)printf(",ECT(1)");
518 break;
519 case 2:
520 (void)printf(",ECT(0)");
521 break;
522 case 3:
523 (void)printf(",CE");
524 }
525 }
526 (void)printf("] ");
527 }
528
529 if (ip->ip_ttl <= 1)
530 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
531
532 if (vflag) {
533 int sum;
534 char *sep = "";
535
536 printf(" (");
537 if (ip->ip_ttl > 1) {
538 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
539 sep = ", ";
540 }
541 if ((off & 0x3fff) == 0) {
542 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
543 sep = ", ";
544 }
545 (void)printf("%slen %d", sep, (int)ntohs(ip->ip_len));
546 sep = ", ";
547 if ((u_char *)ip + hlen <= snapend) {
548 sum = in_cksum((const u_short *)ip, hlen, 0);
549 if (sum != 0) {
550 (void)printf("%sbad cksum %x (->%x)!", sep,
551 ntohs(ip->ip_sum),
552 ntohs(ip->ip_sum)-sum);
553 sep = ", ";
554 }
555 }
556 if ((hlen -= sizeof(struct ip)) > 0) {
557 (void)printf("%soptlen=%d", sep, hlen);
558 ip_optprint((u_char *)(ip + 1), hlen);
559 }
560 printf(")");
561 }
562 }
563
564 void
565 ipN_print(register const u_char *bp, register u_int length)
566 {
567 struct ip *ip, hdr;
568
569 ip = (struct ip *)bp;
570 if (length < 4) {
571 (void)printf("truncated-ip %d", length);
572 return;
573 }
574 memcpy (&hdr, (char *)ip, 4);
575 switch (IP_V(&hdr)) {
576 case 4:
577 ip_print (bp, length);
578 return;
579 #ifdef INET6
580 case 6:
581 ip6_print (bp, length);
582 return;
583 #endif
584 default:
585 (void)printf("unknown ip %d", IP_V(&hdr));
586 return;
587 }
588 }