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