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