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