xref: /openbsd/usr.sbin/tcpdump/print-ip6.c (revision 898184e3)
1 /*	$OpenBSD: print-ip6.c,v 1.15 2011/09/17 16:45:42 naddy Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #ifdef INET6
25 
26 #include <sys/param.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
38 
39 #include <inttypes.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "interface.h"
46 #include "addrtoname.h"
47 
48 #include <netinet/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 hlen;
58 	register int len;
59 	register const u_char *cp;
60 	int nh;
61 	u_int flow;
62 
63 	ip6 = (const struct ip6_hdr *)bp;
64 
65 	/*
66 	 * The IP header is not word aligned, so copy into abuf.
67 	 * This will never happen with BPF.  It does happen with
68 	 * raw packet dumps from -r.
69 	 */
70 	if ((intptr_t)ip6 & (sizeof(long)-1)) {
71 		static u_char *abuf = NULL;
72 		static int didwarn = 0;
73 		int clen = snapend - bp;
74 		if (clen > snaplen)
75 			clen = snaplen;
76 
77 		if (abuf == NULL) {
78 			abuf = (u_char *)malloc(snaplen);
79 			if (abuf == NULL)
80 				error("ip6_print: malloc");
81 		}
82 		memmove((char *)abuf, (char *)ip6, min(length, clen));
83 		snapend = abuf + clen;
84 		packetp = bp = abuf;
85 		ip6 = (struct ip6_hdr *)abuf;
86 		/* We really want libpcap to give us aligned packets */
87 		if (!didwarn) {
88 			warning("compensating for unaligned libpcap packets");
89 			++didwarn;
90 		}
91 	}
92 
93 	if ((u_char *)(ip6 + 1) > snapend) {
94 		printf("[|ip6]");
95 		return;
96 	}
97 	if (length < sizeof (struct ip6_hdr)) {
98 		(void)printf("truncated-ip6 %d", length);
99 		return;
100 	}
101 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
102 		(void)printf("bad-ip6-version %u", ip6->ip6_vfc >> 4);
103 		return;
104 	}
105 	hlen = sizeof(struct ip6_hdr);
106 
107 	len = ntohs(ip6->ip6_plen);
108 	if (length < len + hlen)
109 		(void)printf("truncated-ip6 - %d bytes missing!",
110 			len + hlen - length);
111 
112 	cp = (const u_char *)ip6;
113 	nh = ip6->ip6_nxt;
114 	while (cp + hlen <= snapend) {
115 		cp += hlen;
116 
117 #ifndef IPPROTO_IPV4
118 #define IPPROTO_IPV4	4
119 #endif
120 		if (cp == (u_char *)(ip6 + 1) &&
121 		    nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
122 		    nh != IPPROTO_ESP && nh != IPPROTO_AH &&
123 		    (vflag || (nh != IPPROTO_IPV4 && nh != IPPROTO_IPV6))) {
124 			(void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
125 				     ip6addr_string(&ip6->ip6_dst));
126 		}
127 
128 		switch (nh) {
129 		case IPPROTO_HOPOPTS:
130 			hlen = hbhopt_print(cp);
131 			nh = *cp;
132 			break;
133 		case IPPROTO_DSTOPTS:
134 			hlen = dstopt_print(cp);
135 			nh = *cp;
136 			break;
137 		case IPPROTO_FRAGMENT:
138 			hlen = frag6_print(cp, (const u_char *)ip6);
139 			if (snapend < cp + hlen)
140 				goto end;
141 			nh = *cp;
142 			break;
143 		case IPPROTO_ROUTING:
144 			hlen = rt6_print(cp, (const u_char *)ip6);
145 			nh = *cp;
146 			break;
147 		case IPPROTO_TCP:
148 			tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
149 				(const u_char *)ip6);
150 			goto end;
151 		case IPPROTO_UDP:
152 			udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
153 				(const u_char *)ip6);
154 			goto end;
155 		case IPPROTO_ESP:
156 			esp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
157 				(const u_char *)ip6);
158 			goto end;
159 		case IPPROTO_AH:
160 			ah_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
161 				(const u_char *)ip6);
162 			goto end;
163 		case IPPROTO_ICMPV6:
164 			icmp6_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
165 				(const u_char *)ip6);
166 			goto end;
167 		case IPPROTO_PIM:
168 			(void)printf("PIM");
169 			pim_print(cp, len);
170 			goto end;
171 #ifndef IPPROTO_OSPF
172 #define IPPROTO_OSPF 89
173 #endif
174 		case IPPROTO_OSPF:
175 			ospf6_print(cp, len);
176 			goto end;
177 		case IPPROTO_IPV6:
178 			ip6_print(cp, len);
179 			if (! vflag)
180 				printf(" (encap)");
181 			goto end;
182 		case IPPROTO_IPV4:
183 			ip_print(cp, len);
184 			if (! vflag)
185 				printf(" (encap)");
186 			goto end;
187 		case IPPROTO_NONE:
188 			(void)printf("no next header");
189 			goto end;
190 
191 #ifndef IPPROTO_CARP
192 #define IPPROTO_CARP 112
193 #endif
194 		case IPPROTO_CARP:
195 			if (packettype == PT_VRRP)
196 				vrrp_print(cp, len, ip6->ip6_hlim);
197 			else
198 				carp_print(cp, len, ip6->ip6_hlim);
199 			break;
200 
201 		default:
202 			(void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
203 			goto end;
204 		}
205 		if (hlen == 0)
206 			break;
207 	}
208 
209  end:
210 
211 	flow = ntohl(ip6->ip6_flow);
212 #if 0
213 	/* rfc1883 */
214 	if (flow & 0x0f000000)
215 		(void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
216 	if (flow & 0x00ffffff)
217 		(void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
218 #else
219 	/* RFC 2460 */
220 	if (flow & 0x0ff00000)
221 		(void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
222 	if (flow & 0x000fffff)
223 		(void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
224 #endif
225 
226 	if (ip6->ip6_hlim <= 1)
227 		(void)printf(" [hlim %d]", (int)ip6->ip6_hlim);
228 
229 	if (vflag) {
230 		printf(" (");
231 		(void)printf("len %d", len);
232 		if (ip6->ip6_hlim > 1)
233 			(void)printf(", hlim %d", (int)ip6->ip6_hlim);
234 		printf(")");
235 	}
236 }
237 
238 #endif /* INET6 */
239