]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
4f9129b45807064a8d06ee3c31b96a8d4008b778
[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[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip6.c,v 1.32.2.5 2003-11-19 01:28:19 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 #include "extract.h"
42
43 #include "ip6.h"
44 #include "ipproto.h"
45
46 /*
47 * print an IP6 datagram.
48 */
49 void
50 ip6_print(register const u_char *bp, register u_int length)
51 {
52 register const struct ip6_hdr *ip6;
53 register int advance;
54 register u_int len;
55 const u_char *ipend;
56 register const u_char *cp;
57 int nh;
58 int fragmented = 0;
59 u_int flow;
60
61 ip6 = (const struct ip6_hdr *)bp;
62
63 TCHECK(*ip6);
64 if (length < sizeof (struct ip6_hdr)) {
65 (void)printf("truncated-ip6 %d", length);
66 return;
67 }
68
69 len = EXTRACT_16BITS(&ip6->ip6_plen) + sizeof(struct ip6_hdr);
70 if (length < len)
71 (void)printf("truncated-ip6 - %d bytes missing!",
72 len - length);
73
74 /*
75 * Cut off the snapshot length to the end of the IP payload.
76 */
77 ipend = bp + len;
78 if (ipend < snapend)
79 snapend = ipend;
80
81 cp = (const u_char *)ip6;
82 advance = sizeof(struct ip6_hdr);
83 nh = ip6->ip6_nxt;
84 while (cp < snapend && advance > 0) {
85 cp += advance;
86 len -= advance;
87
88 if (cp == (const u_char *)(ip6 + 1) &&
89 nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
90 nh != IPPROTO_SCTP) {
91 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
92 ip6addr_string(&ip6->ip6_dst));
93 }
94
95 switch (nh) {
96 case IPPROTO_HOPOPTS:
97 advance = hbhopt_print(cp);
98 nh = *cp;
99 break;
100 case IPPROTO_DSTOPTS:
101 advance = dstopt_print(cp);
102 nh = *cp;
103 break;
104 case IPPROTO_FRAGMENT:
105 advance = frag6_print(cp, (const u_char *)ip6);
106 if (snapend <= cp + advance)
107 goto end;
108 nh = *cp;
109 fragmented = 1;
110 break;
111
112 case IPPROTO_MOBILITY:
113 /*
114 * XXX - we don't use "advance"; is this
115 * header always a final header?
116 */
117 advance = mobility_print(cp, (const u_char *)ip6);
118 nh = *cp;
119 goto end;
120 case IPPROTO_ROUTING:
121 advance = rt6_print(cp, (const u_char *)ip6);
122 nh = *cp;
123 break;
124 case IPPROTO_SCTP:
125 sctp_print(cp, (const u_char *)ip6, len);
126 goto end;
127 case IPPROTO_TCP:
128 tcp_print(cp, len, (const u_char *)ip6, fragmented);
129 goto end;
130 case IPPROTO_UDP:
131 udp_print(cp, len, (const u_char *)ip6, fragmented);
132 goto end;
133 case IPPROTO_ICMPV6:
134 icmp6_print(cp, len, (const u_char *)ip6, fragmented);
135 goto end;
136 case IPPROTO_AH:
137 advance = ah_print(cp);
138 nh = *cp;
139 break;
140 case IPPROTO_ESP:
141 {
142 int enh, padlen;
143 advance = esp_print(cp, (const u_char *)ip6, &enh, &padlen);
144 nh = enh & 0xff;
145 len -= padlen;
146 break;
147 }
148 case IPPROTO_IPCOMP:
149 {
150 int enh;
151 advance = ipcomp_print(cp, &enh);
152 nh = enh & 0xff;
153 break;
154 }
155
156 case IPPROTO_PIM:
157 pim_print(cp, len);
158 goto end;
159 case IPPROTO_OSPF:
160 ospf6_print(cp, len);
161 goto end;
162
163 case IPPROTO_IPV6:
164 ip6_print(cp, len);
165 goto end;
166
167 case IPPROTO_IPV4:
168 ip_print(cp, len);
169 goto end;
170
171 case IPPROTO_NONE:
172 (void)printf("no next header");
173 goto end;
174
175 default:
176 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
177 goto end;
178 }
179 }
180
181 end:
182
183 flow = EXTRACT_32BITS(&ip6->ip6_flow);
184 #if 0
185 /* rfc1883 */
186 if (flow & 0x0f000000)
187 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
188 if (flow & 0x00ffffff)
189 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
190 #else
191 /* RFC 2460 */
192 if (flow & 0x0ff00000)
193 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
194 if (flow & 0x000fffff)
195 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
196 #endif
197
198 if (ip6->ip6_hlim <= 1)
199 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim);
200
201 if (vflag) {
202 printf(" (");
203 (void)printf("len %d", len);
204 if (ip6->ip6_hlim > 1)
205 (void)printf(", hlim %d", (int)ip6->ip6_hlim);
206 printf(")");
207 }
208 return;
209 trunc:
210 (void)printf("[|ip6]");
211 }
212
213 #endif /* INET6 */