]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
fix complation on IPv4 host (tested with plain FreeBSD228).
[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.71 1999-10-30 05:30:20 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 case IPPROTO_AH:
404 nh = *cp;
405 advance = ah_print(cp, (const u_char *)ip);
406 cp += advance;
407 len -= advance;
408 goto again;
409
410 case IPPROTO_ESP:
411 {
412 int enh;
413 advance = esp_print(cp, (const u_char *)ip, &enh);
414 cp += advance;
415 len -= advance;
416 if (enh < 0)
417 break;
418 nh = enh & 0xff;
419 goto again;
420 }
421
422 #ifndef IPPROTO_IPCOMP
423 #define IPPROTO_IPCOMP 108
424 #endif
425 case IPPROTO_IPCOMP:
426 {
427 int enh;
428 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
429 cp += advance;
430 len -= advance;
431 if (enh < 0)
432 break;
433 nh = enh & 0xff;
434 goto again;
435 }
436
437 case IPPROTO_TCP:
438 tcp_print(cp, len, (const u_char *)ip);
439 break;
440
441 case IPPROTO_UDP:
442 udp_print(cp, len, (const u_char *)ip);
443 break;
444
445 case IPPROTO_ICMP:
446 icmp_print(cp, (const u_char *)ip);
447 break;
448
449 #ifndef IPPROTO_IGRP
450 #define IPPROTO_IGRP 9
451 #endif
452 case IPPROTO_IGRP:
453 igrp_print(cp, len, (const u_char *)ip);
454 break;
455
456 case IPPROTO_ND:
457 #if 0
458 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
459 ipaddr_string(&ip->ip_dst));
460 #endif
461 (void)printf(" nd %d", len);
462 break;
463
464 case IPPROTO_EGP:
465 egp_print(cp, len, (const u_char *)ip);
466 break;
467
468 #ifndef IPPROTO_OSPF
469 #define IPPROTO_OSPF 89
470 #endif
471 case IPPROTO_OSPF:
472 ospf_print(cp, len, (const u_char *)ip);
473 break;
474
475 #ifndef IPPROTO_IGMP
476 #define IPPROTO_IGMP 2
477 #endif
478 case IPPROTO_IGMP:
479 igmp_print(cp, len, (const u_char *)ip);
480 break;
481
482 case 4:
483 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
484 #if 0
485 if (vflag)
486 (void)printf("%s > %s: ",
487 ipaddr_string(&ip->ip_src),
488 ipaddr_string(&ip->ip_dst));
489 #endif
490 ip_print(cp, len);
491 if (! vflag) {
492 printf(" (ipip)");
493 return;
494 }
495 break;
496
497 #ifdef INET6
498 #ifndef IP6PROTO_ENCAP
499 #define IP6PROTO_ENCAP 41
500 #endif
501 case IP6PROTO_ENCAP:
502 /* ip6-in-ip encapsulation */
503 #if 0
504 if (vflag)
505 (void)printf("%s > %s: ",
506 ipaddr_string(&ip->ip_src),
507 ipaddr_string(&ip->ip_dst));
508 #endif
509 ip6_print(cp, len);
510 if (! vflag) {
511 printf(" (encap)");
512 return;
513 }
514 break;
515 #endif /*INET6*/
516
517
518 #ifndef IPPROTO_GRE
519 #define IPPROTO_GRE 47
520 #endif
521 case IPPROTO_GRE:
522 if (vflag)
523 (void)printf("gre %s > %s: ",
524 ipaddr_string(&ip->ip_src),
525 ipaddr_string(&ip->ip_dst));
526 /* do it */
527 gre_print(cp, len);
528 if (! vflag) {
529 printf(" (gre encap)");
530 return;
531 }
532 break;
533
534 #ifndef IPPROTO_MOBILE
535 #define IPPROTO_MOBILE 55
536 #endif
537 case IPPROTO_MOBILE:
538 if (vflag)
539 (void)printf("mobile %s > %s: ",
540 ipaddr_string(&ip->ip_src),
541 ipaddr_string(&ip->ip_dst));
542 mobile_print(cp, len);
543 if (! vflag) {
544 printf(" (mobile encap)");
545 return;
546 }
547 break;
548
549 #ifndef IPPROTO_PIM
550 #define IPPROTO_PIM 103
551 #endif
552 case IPPROTO_PIM:
553 pim_print(cp, len);
554 break;
555
556 default:
557 #if 0
558 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
559 ipaddr_string(&ip->ip_dst));
560 #endif
561 (void)printf(" ip-proto-%d %d", nh, len);
562 break;
563 }
564 }
565
566 /*
567 * for fragmented datagrams, print id:size@offset. On all
568 * but the last stick a "+". For unfragmented datagrams, note
569 * the don't fragment flag.
570 */
571 len = len0; /* get the original length */
572 if (off & 0x3fff) {
573 /*
574 * if this isn't the first frag, we're missing the
575 * next level protocol header. print the ip addr.
576 */
577 if (off & 0x1fff)
578 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
579 ipaddr_string(&ip->ip_dst));
580 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
581 (off & 0x1fff) * 8,
582 (off & IP_MF)? "+" : "");
583 } else if (off & IP_DF)
584 (void)printf(" (DF)");
585
586 if (ip->ip_tos)
587 (void)printf(" [tos 0x%x]", (int)ip->ip_tos);
588 if (ip->ip_ttl <= 1)
589 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
590
591 if (vflag) {
592 int sum;
593 char *sep = "";
594
595 printf(" (");
596 if (ip->ip_ttl > 1) {
597 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
598 sep = ", ";
599 }
600 if ((off & 0x3fff) == 0) {
601 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
602 sep = ", ";
603 }
604 if ((u_char *)ip + hlen <= snapend) {
605 sum = in_cksum(ip);
606 if (sum != 0) {
607 (void)printf("%sbad cksum %x!", sep,
608 ntohs(ip->ip_sum));
609 sep = ", ";
610 }
611 }
612 if ((hlen -= sizeof(struct ip)) > 0) {
613 (void)printf("%soptlen=%d", sep, hlen);
614 ip_optprint((u_char *)(ip + 1), hlen);
615 }
616 printf(")");
617 }
618 }