2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.92 2001/01/02 23:00:01 guy Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
44 #include "addrtoname.h"
45 #include "interface.h"
46 #include "extract.h" /* must come after interface.h */
56 * print the recorded route in an IP RR, LSRR or SSRR option.
59 ip_printroute(const char *type, register const u_char *cp, u_int length)
61 register u_int ptr = cp[2] - 1;
66 printf(" [bad length %d]", length);
67 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
68 printf(" [bad ptr %d]", cp[2]);
71 for (len = 3; len < length; len += 4) {
74 printf("%s%s", type, ipaddr_string(&cp[len]));
77 printf("%s}", ptr == len? "#" : "");
81 ip_printts(register const u_char *cp, u_int length)
83 register u_int ptr = cp[2] - 1;
84 register u_int len = 0;
89 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
90 if ((length - 4) & (hoplen-1))
91 printf("[bad length %d]", length);
92 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
93 printf("[bad ptr %d]", cp[2]);
98 case IPOPT_TS_TSANDADDR:
102 * prespecified should really be 3, but some ones might send 2
103 * instead, and the IPOPT_TS_PRESPEC constant can apparently
104 * have both values, so we have to hard-code it here.
108 printf("PRESPEC2.0");
110 case 3: /* IPOPT_TS_PRESPEC */
114 printf("[bad ts type %d]", cp[3]&0xF);
119 for (len = 4; len < length; len += hoplen) {
122 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
123 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
128 printf("%s", ptr == len ? " ^ " : "");
131 printf(" [%d hops not recorded]} ", cp[3]>>4);
140 ip_optprint(register const u_char *cp, u_int length)
144 for (; length > 0; cp += len, length -= len) {
147 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
150 if (&cp[1] >= snapend) {
157 printf("[|ip op len %d]", len);
160 if (&cp[1] >= snapend || cp + len > snapend) {
169 printf("-%d", length - 1);
180 #ifndef IPOPT_SECURITY
181 #define IPOPT_SECURITY 130
182 #endif /* IPOPT_SECURITY */
184 printf(" SECURITY{%d}", len);
188 ip_printroute("RR", cp, len);
192 ip_printroute("SSRR", cp, len);
196 ip_printroute("LSRR", cp, len);
200 #define IPOPT_RA 148 /* router alert */
206 else if (cp[2] || cp[3])
207 printf("%d.%d", cp[2], cp[3]);
211 printf(" IPOPT-%d{%d}", cp[0], len);
218 * compute an IP header checksum.
219 * don't modifiy the packet.
222 in_cksum(const u_short *addr, register int len, u_short csum)
225 const u_short *w = addr;
230 * Our algorithm is simple, using a 32 bit accumulator (sum),
231 * we add sequential 16 bit words to it, and at the end, fold
232 * back all the carry bits from the top 16 bits into the lower
240 sum += htons(*(u_char *)w<<8);
243 * add back carry outs from top 16 bits to low 16 bits
245 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
246 sum += (sum >> 16); /* add carry */
247 answer = ~sum; /* truncate to 16 bits */
252 * print an IP datagram.
255 ip_print(register const u_char *bp, register u_int length)
257 register const struct ip *ip;
258 register u_int hlen, len, len0, off;
259 register const u_char *cp;
263 ip = (const struct ip *)bp;
266 * If the IP header is not aligned, copy into abuf.
267 * This will never happen with BPF. It does happen raw packet
271 static u_char *abuf = NULL;
272 static int didwarn = 0;
275 abuf = (u_char *)malloc(snaplen);
277 error("ip_print: malloc");
279 memcpy((char *)abuf, (char *)ip, min(length, snaplen));
280 snapend += abuf - (u_char *)ip;
282 ip = (struct ip *)abuf;
283 /* We really want libpcap to give us aligned packets */
285 warning("compensating for unaligned libpcap packets");
290 if ((u_char *)(ip + 1) > snapend) {
294 if (length < sizeof (struct ip)) {
295 (void)printf("truncated-ip %d", length);
298 hlen = IP_HL(ip) * 4;
299 if (hlen < sizeof (struct ip)) {
300 (void)printf("bad-hlen %d", hlen);
304 len = ntohs(ip->ip_len);
306 (void)printf("truncated-ip - %d bytes missing!",
312 * If this is fragment zero, hand it to the next higher
315 off = ntohs(ip->ip_off);
316 if ((off & 0x1fff) == 0) {
317 cp = (const u_char *)ip + hlen;
320 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
321 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
322 ipaddr_string(&ip->ip_dst));
328 #define IPPROTO_AH 51
332 advance = ah_print(cp, (const u_char *)ip);
338 #define IPPROTO_ESP 50
343 advance = esp_print(cp, (const u_char *)ip, &enh);
352 #ifndef IPPROTO_IPCOMP
353 #define IPPROTO_IPCOMP 108
358 advance = ipcomp_print(cp, (const u_char *)ip, &enh);
368 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
372 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
376 icmp_print(cp, len, (const u_char *)ip);
380 #define IPPROTO_IGRP 9
383 igrp_print(cp, len, (const u_char *)ip);
388 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
389 ipaddr_string(&ip->ip_dst));
391 (void)printf(" nd %d", len);
395 egp_print(cp, len, (const u_char *)ip);
399 #define IPPROTO_OSPF 89
402 ospf_print(cp, len, (const u_char *)ip);
406 #define IPPROTO_IGMP 2
409 igmp_print(cp, len, (const u_char *)ip);
413 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
416 (void)printf("%s > %s: ",
417 ipaddr_string(&ip->ip_src),
418 ipaddr_string(&ip->ip_dst));
428 #ifndef IP6PROTO_ENCAP
429 #define IP6PROTO_ENCAP 41
432 /* ip6-in-ip encapsulation */
435 (void)printf("%s > %s: ",
436 ipaddr_string(&ip->ip_src),
437 ipaddr_string(&ip->ip_dst));
449 #define IPPROTO_GRE 47
453 (void)printf("gre %s > %s: ",
454 ipaddr_string(&ip->ip_src),
455 ipaddr_string(&ip->ip_dst));
459 printf(" (gre encap)");
464 #ifndef IPPROTO_MOBILE
465 #define IPPROTO_MOBILE 55
469 (void)printf("mobile %s > %s: ",
470 ipaddr_string(&ip->ip_src),
471 ipaddr_string(&ip->ip_dst));
472 mobile_print(cp, len);
474 printf(" (mobile encap)");
480 #define IPPROTO_PIM 103
487 #define IPPROTO_VRRP 112
491 (void)printf("vrrp %s > %s: ",
492 ipaddr_string(&ip->ip_src),
493 ipaddr_string(&ip->ip_dst));
494 vrrp_print(cp, len, ip->ip_ttl);
499 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
500 ipaddr_string(&ip->ip_dst));
502 (void)printf(" ip-proto-%d %d", nh, len);
507 /* Ultra quiet now means that all this stuff should be suppressed */
509 if (qflag > 1) return;
513 * for fragmented datagrams, print id:size@offset. On all
514 * but the last stick a "+". For unfragmented datagrams, note
515 * the don't fragment flag.
517 len = len0; /* get the original length */
520 * if this isn't the first frag, we're missing the
521 * next level protocol header. print the ip addr.
524 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
525 ipaddr_string(&ip->ip_dst));
532 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip->ip_id), len,
534 (off & IP_MF)? "+" : "");
536 } else if (off & IP_DF)
537 (void)printf(" (DF)");
540 (void)printf(" [tos 0x%x", (int)ip->ip_tos);
542 if (ip->ip_tos&0x02) {
543 (void)printf(",ECT");
551 (void)printf(" [ttl %d]", (int)ip->ip_ttl);
558 if (ip->ip_ttl > 1) {
559 (void)printf("%sttl %d", sep, (int)ip->ip_ttl);
562 if ((off & 0x3fff) == 0) {
563 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id));
566 (void)printf("%slen %d", sep, (int)ntohs(ip->ip_len));
568 if ((u_char *)ip + hlen <= snapend) {
569 sum = in_cksum((const u_short *)ip, hlen, 0);
571 (void)printf("%sbad cksum %x!", sep,
576 if ((hlen -= sizeof(struct ip)) > 0) {
577 (void)printf("%soptlen=%d", sep, hlen);
578 ip_optprint((u_char *)(ip + 1), hlen);
585 ipN_print(register const u_char *bp, register u_int length)
589 ip = (struct ip *)bp;
591 (void)printf("truncated-ip %d", length);
594 memcpy (&hdr, (char *)ip, 4);
595 switch (IP_V(&hdr)) {
597 ip_print (bp, length);
601 ip6_print (bp, length);
605 (void)printf("unknown ip %d", IP_V(&hdr));