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