print-ip6.c revision 56893
1/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
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
23static const char rcsid[] =
24    "@(#) $Header: /tcpdump/master/tcpdump/print-ip6.c,v 1.2.2.1 2000/01/11 06:58:25 fenner Exp $";
25#endif
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#ifdef INET6
32
33#include <sys/param.h>
34#include <sys/time.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37
38#include <netinet/in.h>
39#include <netinet/in_systm.h>
40#include <netinet/ip.h>
41#include <netinet/ip_var.h>
42#include <netinet/udp.h>
43#include <netinet/udp_var.h>
44#include <netinet/tcp.h>
45
46#include <stdio.h>
47#ifdef __STDC__
48#include <stdlib.h>
49#endif
50#include <unistd.h>
51
52#include "interface.h"
53#include "addrtoname.h"
54
55#include <netinet/ip6.h>
56
57/*
58 * print an IP6 datagram.
59 */
60void
61ip6_print(register const u_char *bp, register int length)
62{
63	register const struct ip6_hdr *ip6;
64	register int hlen;
65	register int len;
66	register const u_char *cp;
67	int nh;
68	u_int flow;
69
70	ip6 = (const struct ip6_hdr *)bp;
71
72#ifdef TCPDUMP_ALIGN
73	/*
74	 * The IP header is not word aligned, so copy into abuf.
75	 * This will never happen with BPF.  It does happen raw packet
76	 * dumps from -r.
77	 */
78	if ((int)ip & (sizeof(long)-1)) {
79		static u_char *abuf;
80
81		if (abuf == 0)
82			abuf = (u_char *)malloc(snaplen);
83		bcopy((char *)ip, (char *)abuf, min(length, snaplen));
84		snapend += abuf - (u_char *)ip;
85		packetp = abuf;
86		ip = (struct ip6_hdr *)abuf;
87	}
88#endif
89	if ((u_char *)(ip6 + 1) > snapend) {
90		printf("[|ip6]");
91		return;
92	}
93	if (length < sizeof (struct ip6_hdr)) {
94		(void)printf("truncated-ip6 %d", length);
95		return;
96	}
97	hlen = sizeof(struct ip6_hdr);
98
99	len = ntohs(ip6->ip6_plen);
100	if (length < len + hlen)
101		(void)printf("truncated-ip6 - %d bytes missing!",
102			len + hlen - length);
103
104	cp = (const u_char *)ip6;
105	nh = ip6->ip6_nxt;
106	while (cp < snapend) {
107		cp += hlen;
108
109		if (cp == (u_char *)(ip6 + 1)
110		 && nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
111			(void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
112				     ip6addr_string(&ip6->ip6_dst));
113		}
114
115		switch (nh) {
116		case IPPROTO_HOPOPTS:
117			hlen = hbhopt_print(cp);
118			nh = *cp;
119			break;
120		case IPPROTO_DSTOPTS:
121			hlen = dstopt_print(cp);
122			nh = *cp;
123			break;
124		case IPPROTO_FRAGMENT:
125			hlen = frag6_print(cp, (const u_char *)ip6);
126			if (snapend <= cp + hlen)
127				goto end;
128			nh = *cp;
129			break;
130		case IPPROTO_ROUTING:
131			hlen = rt6_print(cp, (const u_char *)ip6);
132			nh = *cp;
133			break;
134		case IPPROTO_TCP:
135			tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
136				(const u_char *)ip6);
137			goto end;
138		case IPPROTO_UDP:
139			udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
140				(const u_char *)ip6);
141			goto end;
142		case IPPROTO_ICMPV6:
143			icmp6_print(cp, (const u_char *)ip6);
144			goto end;
145		case IPPROTO_AH:
146			hlen = ah_print(cp, (const u_char *)ip6);
147			nh = *cp;
148			break;
149		case IPPROTO_ESP:
150		    {
151			int enh;
152			cp += esp_print(cp, (const u_char *)ip6, &enh);
153			if (enh < 0)
154				goto end;
155			nh = enh & 0xff;
156			break;
157		    }
158#ifndef IPPROTO_IPCOMP
159#define IPPROTO_IPCOMP	108
160#endif
161		case IPPROTO_IPCOMP:
162		    {
163			int enh;
164			cp += ipcomp_print(cp, (const u_char *)ip6, &enh);
165			if (enh < 0)
166				goto end;
167			nh = enh & 0xff;
168			break;
169		    }
170		case IPPROTO_PIM:
171			(void)printf("PIM");
172			pim_print(cp, len);
173			goto end;
174#ifndef IPPROTO_OSPF
175#define IPPROTO_OSPF 89
176#endif
177		case IPPROTO_OSPF:
178			ospf6_print(cp, len);
179			goto end;
180		case IPPROTO_IPV6:
181			ip6_print(cp, len);
182			goto end;
183#ifndef IPPROTO_IPV4
184#define IPPROTO_IPV4	4
185#endif
186		case IPPROTO_IPV4:
187			ip_print(cp, len);
188			goto end;
189		case IPPROTO_NONE:
190			(void)printf("no next header");
191			goto end;
192
193		default:
194			(void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
195			goto end;
196		}
197	}
198
199 end:
200
201	flow = ntohl(ip6->ip6_flow);
202#if 0
203	/* rfc1883 */
204	if (flow & 0x0f000000)
205		(void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
206	if (flow & 0x00ffffff)
207		(void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
208#else
209	/* RFC 2460 */
210	if (flow & 0x0ff00000)
211		(void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
212	if (flow & 0x000fffff)
213		(void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
214#endif
215
216	if (ip6->ip6_hlim <= 1)
217		(void)printf(" [hlim %d]", (int)ip6->ip6_hlim);
218
219	if (vflag) {
220		printf(" (");
221		(void)printf("len %d", len);
222		if (ip6->ip6_hlim > 1)
223			(void)printf(", hlim %d", (int)ip6->ip6_hlim);
224		printf(")");
225	}
226}
227
228#endif /* INET6 */
229