xref: /openbsd/usr.sbin/tcpdump/print-ip6.c (revision 133306f0)
1 /*	$OpenBSD: print-ip6.c,v 1.1 2000/04/26 21:35:41 jakob 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 #ifndef lint
25 static const char rcsid[] =
26     "@(#) /master/usr.sbin/tcpdump/tcpdump/print-ip.c,v 2.1 1995/02/03 18:14:45 polk Exp (LBL)";
27 #endif
28 
29 #ifdef INET6
30 
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_var.h>
40 #include <netinet/udp.h>
41 #include <netinet/udp_var.h>
42 #include <netinet/tcp.h>
43 
44 #include <stdio.h>
45 #ifdef __STDC__
46 #include <stdlib.h>
47 #endif
48 #include <unistd.h>
49 
50 #include "interface.h"
51 #include "addrtoname.h"
52 
53 #include <netinet/ip6.h>
54 
55 /*
56  * print an IP6 datagram.
57  */
58 void
59 ip6_print(register const u_char *bp, register int length)
60 {
61 	register const struct ip6_hdr *ip6;
62 	register int hlen;
63 	register int len;
64 	register const u_char *cp;
65 	int nh;
66 	u_int flow;
67 
68 	ip6 = (const struct ip6_hdr *)bp;
69 
70 #ifdef TCPDUMP_ALIGN
71 	/*
72 	 * The IP header is not word aligned, so copy into abuf.
73 	 * This will never happen with BPF.  It does happen raw packet
74 	 * dumps from -r.
75 	 */
76 	if ((int)ip & (sizeof(long)-1)) {
77 		static u_char *abuf;
78 
79 		if (abuf == 0)
80 			abuf = (u_char *)malloc(snaplen);
81 		bcopy((char *)ip, (char *)abuf, min(length, snaplen));
82 		snapend += abuf - (u_char *)ip;
83 		packetp = abuf;
84 		ip = (struct ip6_hdr *)abuf;
85 	}
86 #endif
87 	if ((u_char *)(ip6 + 1) > snapend) {
88 		printf("[|ip6]");
89 		return;
90 	}
91 	if (length < sizeof (struct ip6_hdr)) {
92 		(void)printf("truncated-ip6 %d", length);
93 		return;
94 	}
95 	hlen = sizeof(struct ip6_hdr);
96 
97 	len = ntohs(ip6->ip6_plen);
98 	if (length < len + hlen)
99 		(void)printf("truncated-ip6 - %d bytes missing!",
100 			len + hlen - length);
101 
102 	cp = (const u_char *)ip6;
103 	nh = ip6->ip6_nxt;
104 	while (cp < snapend) {
105 		cp += hlen;
106 
107 		if (cp == (u_char *)(ip6 + 1)
108 		 && nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
109 			(void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
110 				     ip6addr_string(&ip6->ip6_dst));
111 		}
112 
113 		switch (nh) {
114 		case IPPROTO_HOPOPTS:
115 			hlen = hbhopt_print(cp);
116 			nh = *cp;
117 			break;
118 		case IPPROTO_DSTOPTS:
119 			hlen = dstopt_print(cp);
120 			nh = *cp;
121 			break;
122 		case IPPROTO_FRAGMENT:
123 			hlen = frag6_print(cp, (const u_char *)ip6);
124 			if (snapend <= cp + hlen)
125 				goto end;
126 			nh = *cp;
127 			break;
128 		case IPPROTO_ROUTING:
129 			hlen = rt6_print(cp, (const u_char *)ip6);
130 			nh = *cp;
131 			break;
132 		case IPPROTO_TCP:
133 			tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
134 				(const u_char *)ip6);
135 			goto end;
136 		case IPPROTO_UDP:
137 			udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
138 				(const u_char *)ip6);
139 			goto end;
140 		case IPPROTO_ICMPV6:
141 			icmp6_print(cp, (const u_char *)ip6);
142 			goto end;
143 		case IPPROTO_PIM:
144 			(void)printf("PIM");
145 			pim_print(cp, len);
146 			goto end;
147 #ifndef IPPROTO_OSPF
148 #define IPPROTO_OSPF 89
149 #endif
150 		case IPPROTO_OSPF:
151 			ospf6_print(cp, len);
152 			goto end;
153 		case IPPROTO_IPV6:
154 			ip6_print(cp, len);
155 			goto end;
156 #ifndef IPPROTO_IPV4
157 #define IPPROTO_IPV4	4
158 #endif
159 		case IPPROTO_IPV4:
160 			ip_print(cp, len);
161 			goto end;
162 		case IPPROTO_NONE:
163 			(void)printf("no next header");
164 			goto end;
165 
166 		default:
167 			(void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
168 			goto end;
169 		}
170 	}
171 
172  end:
173 
174 	flow = ntohl(ip6->ip6_flow);
175 #if 0
176 	/* rfc1883 */
177 	if (flow & 0x0f000000)
178 		(void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
179 	if (flow & 0x00ffffff)
180 		(void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
181 #else
182 	/* RFC 2460 */
183 	if (flow & 0x0ff00000)
184 		(void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
185 	if (flow & 0x000fffff)
186 		(void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
187 #endif
188 
189 	if (ip6->ip6_hlim <= 1)
190 		(void)printf(" [hlim %d]", (int)ip6->ip6_hlim);
191 
192 	if (vflag) {
193 		printf(" (");
194 		(void)printf("len %d", len);
195 		if (ip6->ip6_hlim > 1)
196 			(void)printf(", hlim %d", (int)ip6->ip6_hlim);
197 		printf(")");
198 	}
199 }
200 
201 #endif /* INET6 */
202