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