]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
add code for printing MakeDir and StoreStatus. Also change date
[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.72 1999-10-30 07:36:37 itojun Exp $ (LBL)";
25 #endif
26
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
38
39 #ifdef HAVE_MALLOC_H
40 #include <malloc.h>
41 #endif
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include "addrtoname.h"
48 #include "interface.h"
49 #include "extract.h" /* must come after interface.h */
50
51 /* Compatibility */
52 #ifndef IPPROTO_ND
53 #define IPPROTO_ND 77
54 #endif
55
56 #ifndef IN_CLASSD
57 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
58 #endif
59
60 /* (following from ipmulti/mrouted/prune.h) */
61
62 /*
63 * The packet format for a traceroute request.
64 */
65 struct tr_query {
66 u_int tr_src; /* traceroute source */
67 u_int tr_dst; /* traceroute destination */
68 u_int tr_raddr; /* traceroute response address */
69 #if defined(WORDS_BIGENDIAN) || (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN))
70 struct {
71 u_int ttl : 8; /* traceroute response ttl */
72 u_int qid : 24; /* traceroute query id */
73 } q;
74 #else
75 struct {
76 u_int qid : 24; /* traceroute query id */
77 u_int ttl : 8; /* traceroute response ttl */
78 } q;
79 #endif
80 };
81
82 #define tr_rttl q.ttl
83 #define tr_qid q.qid
84
85 /*
86 * Traceroute response format. A traceroute response has a tr_query at the
87 * beginning, followed by one tr_resp for each hop taken.
88 */
89 struct tr_resp {
90 u_int tr_qarr; /* query arrival time */
91 u_int tr_inaddr; /* incoming interface address */
92 u_int tr_outaddr; /* outgoing interface address */
93 u_int tr_rmtaddr; /* parent address in source tree */
94 u_int tr_vifin; /* input packet count on interface */
95 u_int tr_vifout; /* output packet count on interface */
96 u_int tr_pktcnt; /* total incoming packets for src-grp */
97 u_char tr_rproto; /* routing proto deployed on router */
98 u_char tr_fttl; /* ttl required to forward on outvif */
99 u_char tr_smask; /* subnet mask for src addr */
100 u_char tr_rflags; /* forwarding error codes */
101 };
102
103 /* defs within mtrace */
104 #define TR_QUERY 1
105 #define TR_RESP 2
106
107 /* fields for tr_rflags (forwarding error codes) */
108 #define TR_NO_ERR 0
109 #define TR_WRONG_IF 1
110 #define TR_PRUNED 2
111 #define TR_OPRUNED 3
112 #define TR_SCOPED 4
113 #define TR_NO_RTE 5
114 #define TR_NO_FWD 7
115 #define TR_NO_SPACE 0x81
116 #define TR_OLD_ROUTER 0x82
117
118 /* fields for tr_rproto (routing protocol) */
119 #define TR_PROTO_DVMRP 1
120 #define TR_PROTO_MOSPF 2
121 #define TR_PROTO_PIM 3
122 #define TR_PROTO_CBT 4
123
124 static void print_mtrace(register const u_char *bp, register u_int len)
125 {
126 register struct tr_query *tr = (struct tr_query *)(bp + 8);
127
128 printf("mtrace %d: %s to %s reply-to %s", tr->tr_qid,
129 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
130 ipaddr_string(&tr->tr_raddr));
131 if (IN_CLASSD(ntohl(tr->tr_raddr)))
132 printf(" with-ttl %d", tr->tr_rttl);
133 }
134
135 static void print_mresp(register const u_char *bp, register u_int len)
136 {
137 register struct tr_query *tr = (struct tr_query *)(bp + 8);
138
139 printf("mresp %d: %s to %s reply-to %s", tr->tr_qid,
140 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst),
141 ipaddr_string(&tr->tr_raddr));
142 if (IN_CLASSD(ntohl(tr->tr_raddr)))
143 printf(" with-ttl %d", tr->tr_rttl);
144 }
145
146 static void
147 igmp_print(register const u_char *bp, register u_int len,
148 register const u_char *bp2)
149 {
150 register const struct ip *ip;
151
152 ip = (const struct ip *)bp2;
153 (void)printf("%s > %s: ",
154 ipaddr_string(&ip->ip_src),
155 ipaddr_string(&ip->ip_dst));
156
157 TCHECK2(bp[0], 8);
158 switch (bp[0]) {
159 case 0x11:
160 (void)printf("igmp query");
161 if (*(int *)&bp[4])
162 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4]));
163 if (len != 8)
164 (void)printf(" [len %d]", len);
165 break;
166 case 0x12:
167 (void)printf("igmp report %s", ipaddr_string(&bp[4]));
168 if (len != 8)
169 (void)printf(" [len %d]", len);
170 break;
171 case 0x16:
172 (void)printf("igmp nreport %s", ipaddr_string(&bp[4]));
173 break;
174 case 0x17:
175 (void)printf("igmp leave %s", ipaddr_string(&bp[4]));
176 break;
177 case 0x13:
178 (void)printf("igmp dvmrp");
179 if (len < 8)
180 (void)printf(" [len %d]", len);
181 else
182 dvmrp_print(bp, len);
183 break;
184 case 0x14:
185 (void)printf("igmp pim");
186 igmp_pim_print(bp, len);
187 break;
188 case 0x1e:
189 print_mresp(bp, len);
190 break;
191 case 0x1f:
192 print_mtrace(bp, len);
193 break;
194 default:
195 (void)printf("igmp-%d", bp[0] & 0xf);
196 break;
197 }
198 if ((bp[0] >> 4) != 1)
199 (void)printf(" [v%d]", bp[0] >> 4);
200
201 TCHECK2(bp[0], len);
202 if (vflag) {
203 /* Check the IGMP checksum */
204 u_int32_t sum = 0;
205 int count;
206 const u_short *sp = (u_short *)bp;
207
208 for (count = len / 2; --count >= 0; )
209 sum += *sp++;
210 if (len & 1)
211 sum += ntohs(*(u_char *) sp << 8);
212 while (sum >> 16)
213 sum = (sum & 0xffff) + (sum >> 16);
214 sum = 0xffff & ~sum;
215 if (sum != 0)
216 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2]));
217 }
218 return;
219 trunc:
220 fputs("[|igmp]", stdout);
221 }
222
223 /*
224 * print the recorded route in an IP RR, LSRR or SSRR option.
225 */
226 static void
227 ip_printroute(const char *type, register const u_char *cp, u_int length)
228 {
229 register u_int ptr = cp[2] - 1;
230 register u_int len;
231
232 printf(" %s{", type);
233 if ((length + 1) & 3)
234 printf(" [bad length %d]", length);
235 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
236 printf(" [bad ptr %d]", cp[2]);
237
238 type = "";
239 for (len = 3; len < length; len += 4) {
240 if (ptr == len)
241 type = "#";
242 printf("%s%s", type, ipaddr_string(&cp[len]));
243 type = " ";
244 }
245 printf("%s}", ptr == len? "#" : "");
246 }
247
248 /*
249 * print IP options.
250 */
251 static void
252 ip_optprint(register const u_char *cp, u_int length)
253 {
254 register u_int len;
255
256 for (; length > 0; cp += len, length -= len) {
257 int tt = *cp;
258
259 len = (tt == IPOPT_NOP || tt == IPOPT_EOL) ? 1 : cp[1];
260 if (len <= 0) {
261 printf("[|ip op len %d]", len);
262 return;
263 }
264 if (&cp[1] >= snapend || cp + len > snapend) {
265 printf("[|ip]");
266 return;
267 }
268 switch (tt) {
269
270 case IPOPT_EOL:
271 printf(" EOL");
272 if (length > 1)
273 printf("-%d", length - 1);
274 return;
275
276 case IPOPT_NOP:
277 printf(" NOP");
278 break;
279
280 case IPOPT_TS:
281 printf(" TS{%d}", len);
282 break;
283
284 case IPOPT_SECURITY:
285 printf(" SECURITY{%d}", len);
286 break;
287
288 case IPOPT_RR:
289 printf(" RR{%d}=", len);
290 ip_printroute("RR", cp, len);
291 break;
292
293 case IPOPT_SSRR:
294 ip_printroute("SSRR", cp, len);
295 break;
296
297 case IPOPT_LSRR:
298 ip_printroute("LSRR", cp, len);
299 break;
300
301 default:
302 printf(" IPOPT-%d{%d}", cp[0], len);
303 break;
304 }
305 }
306 }
307
308 /*
309 * compute an IP header checksum.
310 * don't modifiy the packet.
311 */
312 static int
313 in_cksum(const struct ip *ip)
314 {
315 register const u_short *sp = (u_short *)ip;
316 register u_int32_t sum = 0;
317 register int count;
318
319 /*
320 * No need for endian conversions.
321 */
322 for (count = ip->ip_hl * 2; --count >= 0; )
323 sum += *sp++;
324 while (sum > 0xffff)
325 sum = (sum & 0xffff) + (sum >> 16);
326 sum = ~sum & 0xffff;
327
328 return (sum);
329 }
330
331 /*
332 * print an IP datagram.
333 */
334 void
335 ip_print(register const u_char *bp, register u_int length)
336 {
337 register const struct ip *ip;
338 register u_int hlen, len, len0, off;
339 register const u_char *cp;
340 u_char nh;
341 int advance;
342
343 ip = (const struct ip *)bp;
344 #ifdef LBL_ALIGN
345 /*
346 * If the IP header is not aligned, copy into abuf.
347 * This will never happen with BPF. It does happen raw packet
348 * dumps from -r.
349 */
350 if ((long)ip & 3) {
351 static u_char *abuf = NULL;
352 static int didwarn = 0;
353
354 if (abuf == NULL) {
355 abuf = (u_char *)malloc(snaplen);
356 if (abuf == NULL)
357 error("ip_print: malloc");
358 }
359 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
360 snapend += abuf - (u_char *)ip;
361 packetp = abuf;
362 ip = (struct ip *)abuf;
363 /* We really want libpcap to give us aligned packets */
364 if (!didwarn) {
365 warning("compensating for unaligned libpcap packets");
366 ++didwarn;
367 }
368 }
369 #endif
370 if ((u_char *)(ip + 1) > snapend) {
371 printf("[|ip]");
372 return;
373 }
374 if (length < sizeof (struct ip)) {
375 (void)printf("truncated-ip %d", length);
376 return;
377 }
378 hlen = ip->ip_hl * 4;
379
380 len = ntohs(ip->ip_len);
381 if (length < len)
382 (void)printf("truncated-ip - %d bytes missing!",
383 len - length);
384 len -= hlen;
385 len0 = len;
386
387 /*
388 * If this is fragment zero, hand it to the next higher
389 * level protocol.
390 */
391 off = ntohs(ip->ip_off);
392 if ((off & 0x1fff) == 0) {
393 cp = (const u_char *)ip + hlen;
394 nh = ip->ip_p;
395
396 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
397 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
398 ipaddr_string(&ip->ip_dst));
399 }
400 again:
401 switch (nh) {
402
403 #ifndef IPPROTO_AH
404 #define IPPROTO_AH 51
405 #endif
406 case IPPROTO_AH:
407 nh = *cp;
408 advance = ah_print(cp, (const u_char *)ip);
409 cp += advance;
410 len -= advance;
411 goto again;
412
413 #ifndef IPPROTO_ESP
414 #define IPPROTO_ESP 50
415 #endif
416 case IPPROTO_ESP:
417 {
418 int enh;
419 advance = esp_print(cp, (const u_char *)ip, &enh);
420 cp += advance;
421 len -= advance;
422 if (enh < 0)
423 break;
424 nh = enh & 0xff;
425 goto again;
426 }
427
428 #ifndef IPPROTO_IPCOMP
429 #define IPPROTO_IPCOMP 108
430 #endif
431 case IPPROTO_IPCOMP:
432 {
433 int enh;
434 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
435 cp += advance;
436 len -= advance;
437 if (enh < 0)
438 break;
439 nh = enh & 0xff;
440 goto again;
441 }
442
443 case IPPROTO_TCP:
444 tcp_print(cp, len, (const u_char *)ip);
445 break;
446
447 case IPPROTO_UDP:
448 udp_print(cp, len, (const u_char *)ip);
449 break;
450
451 case IPPROTO_ICMP:
452 icmp_print(cp, (const u_char *)ip);
453 break;
454
455 #ifndef IPPROTO_IGRP
456 #define IPPROTO_IGRP 9
457 #endif
458 case IPPROTO_IGRP:
459 igrp_print(cp, len, (const u_char *)ip);
460 break;
461
462 case IPPROTO_ND:
463 #if 0
464 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
465 ipaddr_string(&ip->ip_dst));
466 #endif
467 (void)printf(" nd %d", len);
468 break;
469
470 case IPPROTO_EGP:
471 egp_print(cp, len, (const u_char *)ip);
472 break;
473
474 #ifndef IPPROTO_OSPF
475 #define IPPROTO_OSPF 89
476 #endif
477 case IPPROTO_OSPF:
478 ospf_print(cp, len, (const u_char *)ip);
479 break;
480
481 #ifndef IPPROTO_IGMP
482 #define IPPROTO_IGMP 2
483 #endif
484 case IPPROTO_IGMP:
485 igmp_print(cp, len, (const u_char *)ip);
486 break;
487
488 case 4:
489 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
490 #if 0
491 if (vflag)
492 (void)printf("%s > %s: ",
493 ipaddr_string(&ip->ip_src),
494 ipaddr_string(&ip->ip_dst));
495 #endif
496 ip_print(cp, len);
497 if (! vflag) {
498 printf(" (ipip)");
499 return;
500 }
501 break;
502
503 #ifdef INET6
504 #ifndef IP6PROTO_ENCAP
505 #define IP6PROTO_ENCAP 41
506 #endif
507 case IP6PROTO_ENCAP:
508 /* ip6-in-ip encapsulation */
509 #if 0
510 if (vflag)
511 (void)printf("%s > %s: ",
512 ipaddr_string(&ip->ip_src),
513 ipaddr_string(&ip->ip_dst));
514 #endif
515 ip6_print(cp, len);
516 if (! vflag) {
517 printf(" (encap)");
518 return;
519 }
520 break;
521 #endif /*INET6*/
522
523
524 #ifndef IPPROTO_GRE
525 #define IPPROTO_GRE 47
526 #endif
527 case IPPROTO_GRE:
528 if (vflag)
529 (void)printf("gre %s > %s: ",
530 ipaddr_string(&ip->ip_src),
531 ipaddr_string(&ip->ip_dst));
532 /* do it */
533 gre_print(cp, len);
534 if (! vflag) {
535 printf(" (gre encap)");
536 return;
537 }
538 break;
539
540 #ifndef IPPROTO_MOBILE
541 #define IPPROTO_MOBILE 55
542 #endif
543 case IPPROTO_MOBILE:
544 if (vflag)
545 (void)printf("mobile %s > %s: ",
546 ipaddr_string(&ip->ip_src),
547 ipaddr_string(&ip->ip_dst));
548 mobile_print(cp, len);
549 if (! vflag) {
550 printf(" (mobile encap)");
551 return;
552 }
553 break;
554
555 #ifndef IPPROTO_PIM
556 #define IPPROTO_PIM 103
557 #endif
558 case IPPROTO_PIM:
559 pim_print(cp, len);
560 break;
561
562 default:
563 #if 0
564 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
565 ipaddr_string(&ip->ip_dst));
566 #endif
567 (void)printf(" ip-proto-%d %d", nh, len);
568 break;
569 }
570 }
571
572 /*
573 * for fragmented datagrams, print id:size@offset. On all
574 * but the last stick a "+". For unfragmented datagrams, note
575 * the don't fragment flag.
576 */
577 len = len0; /* get the original length */
578 if (off & 0x3fff) {
579 /*
580 * if this isn't the first frag, we're missing the
581 * next level protocol header. print the ip addr.
582 */
583 if (off & 0x1fff)
584 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
585 ipaddr_string(&ip->ip_dst));
586 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
587 (off & 0x1fff) * 8,
588 (off & IP_MF)? "+" : "");
589 } else if (off & IP_DF)
590 (void)printf(" (DF)");
591
592 if (ip->ip_tos)
593 (void)printf(" [tos 0x%x]", (int)ip->ip_tos);
594 if (ip->ip_ttl <= 1)
595 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
596
597 if (vflag) {
598 int sum;
599 char *sep = "";
600
601 printf(" (");
602 if (ip->ip_ttl > 1) {
603 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
604 sep = ", ";
605 }
606 if ((off & 0x3fff) == 0) {
607 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
608 sep = ", ";
609 }
610 if ((u_char *)ip + hlen <= snapend) {
611 sum = in_cksum(ip);
612 if (sum != 0) {
613 (void)printf("%sbad cksum %x!", sep,
614 ntohs(ip->ip_sum));
615 sep = ", ";
616 }
617 }
618 if ((hlen -= sizeof(struct ip)) > 0) {
619 (void)printf("%soptlen=%d", sep, hlen);
620 ip_optprint((u_char *)(ip + 1), hlen);
621 }
622 printf(")");
623 }
624 }