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