]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
better print the IP addresses native than using getname();
[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.23 2002-06-27 08:21:41 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
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 u_int length)
55 {
56 register const struct ip6_hdr *ip6;
57 register int advance;
58 register u_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 */
70 if ((u_long)ip6 & 15) {
71 static u_char *abuf;
72
73 if (abuf == NULL) {
74 abuf = malloc(snaplen);
75 if (abuf == NULL)
76 error("ip6_print: malloc");
77 }
78 memcpy(abuf, ip6, min(length, snaplen));
79 snapend += abuf - (u_char *)ip6;
80 packetp = abuf;
81 ip6 = (struct ip6_hdr *)abuf;
82 bp = abuf;
83 }
84 #endif
85 TCHECK(*ip6);
86 if (length < sizeof (struct ip6_hdr)) {
87 (void)printf("truncated-ip6 %d", length);
88 return;
89 }
90 advance = sizeof(struct ip6_hdr);
91
92 len = ntohs(ip6->ip6_plen);
93 if (length < len + advance)
94 (void)printf("truncated-ip6 - %d bytes missing!",
95 len + advance - length);
96
97 cp = (const u_char *)ip6;
98 nh = ip6->ip6_nxt;
99 while (cp < snapend) {
100 cp += advance;
101
102 if (cp == (const u_char *)(ip6 + 1)
103 && nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
104 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
105 ip6addr_string(&ip6->ip6_dst));
106 }
107
108 switch (nh) {
109 case IPPROTO_HOPOPTS:
110 advance = hbhopt_print(cp);
111 nh = *cp;
112 break;
113 case IPPROTO_DSTOPTS:
114 advance = dstopt_print(cp);
115 nh = *cp;
116 break;
117 case IPPROTO_FRAGMENT:
118 advance = frag6_print(cp, (const u_char *)ip6);
119 if (snapend <= cp + advance)
120 goto end;
121 nh = *cp;
122 fragmented = 1;
123 break;
124 #ifndef IPPROTO_MOBILITY
125 #define IPPROTO_MOBILITY 62
126 #endif
127 case IPPROTO_MOBILITY:
128 advance = mobility_print(cp, (const u_char *)ip6);
129 nh = *cp;
130 goto end;
131 case IPPROTO_ROUTING:
132 advance = rt6_print(cp, (const u_char *)ip6);
133 nh = *cp;
134 break;
135 case IPPROTO_TCP:
136 tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
137 (const u_char *)ip6, fragmented);
138 goto end;
139 case IPPROTO_UDP:
140 udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
141 (const u_char *)ip6, fragmented);
142 goto end;
143 case IPPROTO_ICMPV6:
144 icmp6_print(cp, (const u_char *)ip6);
145 goto end;
146 case IPPROTO_AH:
147 advance = ah_print(cp, (const u_char *)ip6);
148 nh = *cp;
149 break;
150 case IPPROTO_ESP:
151 {
152 int enh, padlen;
153 advance = esp_print(cp, (const u_char *)ip6, &enh, &padlen);
154 if (enh < 0)
155 goto end;
156 nh = enh & 0xff;
157 len -= padlen;
158 break;
159 }
160 #ifndef IPPROTO_IPCOMP
161 #define IPPROTO_IPCOMP 108
162 #endif
163 case IPPROTO_IPCOMP:
164 {
165 int enh;
166 advance = ipcomp_print(cp, (const u_char *)ip6, &enh);
167 if (enh < 0)
168 goto end;
169 nh = enh & 0xff;
170 break;
171 }
172
173 #ifndef IPPROTO_PIM
174 #define IPPROTO_PIM 103
175 #endif
176 case IPPROTO_PIM:
177 pim_print(cp, len);
178 goto end;
179 #ifndef IPPROTO_OSPF
180 #define IPPROTO_OSPF 89
181 #endif
182 case IPPROTO_OSPF:
183 ospf6_print(cp, len);
184 goto end;
185 case IPPROTO_IPV6:
186 ip6_print(cp, len);
187 goto end;
188 #ifndef IPPROTO_IPV4
189 #define IPPROTO_IPV4 4
190 #endif
191 case IPPROTO_IPV4:
192 ip_print(cp, len);
193 goto end;
194 case IPPROTO_NONE:
195 (void)printf("no next header");
196 goto end;
197
198 default:
199 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
200 goto end;
201 }
202 }
203
204 end:
205
206 flow = ntohl(ip6->ip6_flow);
207 #if 0
208 /* rfc1883 */
209 if (flow & 0x0f000000)
210 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
211 if (flow & 0x00ffffff)
212 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
213 #else
214 /* RFC 2460 */
215 if (flow & 0x0ff00000)
216 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
217 if (flow & 0x000fffff)
218 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
219 #endif
220
221 if (ip6->ip6_hlim <= 1)
222 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim);
223
224 if (vflag) {
225 printf(" (");
226 (void)printf("len %d", len);
227 if (ip6->ip6_hlim > 1)
228 (void)printf(", hlim %d", (int)ip6->ip6_hlim);
229 printf(")");
230 }
231 return;
232 trunc:
233 (void)printf("[|ip6]");
234 }
235
236 #endif /* INET6 */