xref: /freebsd/cddl/lib/libdtrace/ip.d (revision 9a5aaa97cbae024f90bb626f78c3dbde28653c58)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2013 Mark Johnston <markj@freebsd.org>
24  */
25 
26 #pragma D depends_on module kernel
27 #pragma D depends_on provider ip
28 
29 /*
30  * pktinfo is where packet ID info can be made available for deeper
31  * analysis if packet IDs become supported by the kernel in the future.
32  * The pkt_addr member is currently always NULL.
33  */
34 typedef struct pktinfo {
35 	uintptr_t pkt_addr;
36 } pktinfo_t;
37 
38 /*
39  * csinfo is where connection state info is made available.
40  */
41 typedef uint32_t zoneid_t;
42 typedef struct csinfo {
43 	uintptr_t cs_addr;
44 	uint64_t cs_cid;
45 	pid_t cs_pid;
46 	zoneid_t cs_zoneid;
47 } csinfo_t;
48 
49 /*
50  * ipinfo contains common IP info for both IPv4 and IPv6.
51  */
52 typedef struct ipinfo {
53 	uint8_t ip_ver;			/* IP version (4, 6) */
54 	uint32_t ip_plength;		/* payload length */
55 	string ip_saddr;		/* source address */
56 	string ip_daddr;		/* destination address */
57 } ipinfo_t;
58 
59 /*
60  * ifinfo contains network interface info.
61  */
62 typedef struct ifinfo {
63 	string if_name;			/* interface name */
64 	int8_t if_local;		/* is delivered locally */
65 	/*netstackid_t if_ipstack;*/	/* ipstack ID */
66 	uintptr_t if_addr;		/* pointer to raw ill_t */
67 } ifinfo_t;
68 
69 typedef uint32_t ipaddr_t;
70 typedef struct {
71 	uint8_t		ipha_version_and_hdr_length;
72 	uint8_t		ipha_type_of_service;
73 	uint16_t	ipha_length;
74 	uint16_t	ipha_ident;
75 	uint16_t	ipha_fragment_offset_and_flags;
76 	uint8_t		ipha_ttl;
77 	uint8_t		ipha_protocol;
78 	uint16_t	ipha_hdr_checksum;
79 	ipaddr_t	ipha_src;
80 	ipaddr_t	ipha_dst;
81 } ipha_t;
82 
83 /*
84  * ipv4info is a translated version of the IPv4 header (with raw pointer).
85  * These values are NULL if the packet is not IPv4.
86  */
87 typedef struct ipv4info {
88 	uint8_t ipv4_ver;		/* IP version (4) */
89 	uint8_t ipv4_ihl;		/* header length, bytes */
90 	uint8_t ipv4_tos;		/* type of service field */
91 	uint16_t ipv4_length;		/* length (header + payload) */
92 	uint16_t ipv4_ident;		/* identification */
93 	uint8_t ipv4_flags;		/* IP flags */
94 	uint16_t ipv4_offset;		/* fragment offset */
95 	uint8_t ipv4_ttl;		/* time to live */
96 	uint8_t ipv4_protocol;		/* next level protocol */
97 	string ipv4_protostr;		/* next level protocol, as a string */
98 	uint16_t ipv4_checksum;		/* header checksum */
99 	ipaddr_t ipv4_src;		/* source address */
100 	ipaddr_t ipv4_dst;		/* destination address */
101 	string ipv4_saddr;		/* source address, string */
102 	string ipv4_daddr;		/* destination address, string */
103 	ipha_t *ipv4_hdr;		/* pointer to raw header */
104 } ipv4info_t;
105 
106 /*
107  * ipv6info is a translated version of the IPv6 header (with raw pointer).
108  * These values are NULL if the packet is not IPv6.
109  */
110 typedef struct in6_addr in6_addr_t;
111 typedef struct ipv6info {
112 	uint8_t ipv6_ver;		/* IP version (6) */
113 	uint8_t ipv6_tclass;		/* traffic class */
114 	uint32_t ipv6_flow;		/* flow label */
115 	uint16_t ipv6_plen;		/* payload length */
116 	uint8_t ipv6_nexthdr;		/* next header protocol */
117 	string ipv6_nextstr;		/* next header protocol, as a string */
118 	uint8_t ipv6_hlim;		/* hop limit */
119 	in6_addr_t *ipv6_src;		/* source address */
120 	in6_addr_t *ipv6_dst;		/* destination address */
121 	string ipv6_saddr;		/* source address, string */
122 	string ipv6_daddr;		/* destination address, string */
123 	struct ip6_hdr *ipv6_hdr;	/* pointer to raw header */
124 } ipv6info_t;
125 
126 #pragma D binding "1.5" IPPROTO_IP
127 inline short IPPROTO_IP =	0;
128 #pragma D binding "1.5" IPPROTO_ICMP
129 inline short IPPROTO_ICMP =	1;
130 #pragma D binding "1.5" IPPROTO_IGMP
131 inline short IPPROTO_IGMP =	2;
132 #pragma D binding "1.5" IPPROTO_IPV4
133 inline short IPPROTO_IPV4 =	4;
134 #pragma D binding "1.5" IPPROTO_TCP
135 inline short IPPROTO_TCP =	6;
136 #pragma D binding "1.5" IPPROTO_UDP
137 inline short IPPROTO_UDP =	17;
138 #pragma D binding "1.5" IPPROTO_IPV6
139 inline short IPPROTO_IPV6 =	41;
140 #pragma D binding "1.5" IPPROTO_ROUTING
141 inline short IPPROTO_ROUTING =	43;
142 #pragma D binding "1.5" IPPROTO_FRAGMENT
143 inline short IPPROTO_FRAGMENT =	44;
144 #pragma D binding "1.5" IPPROTO_RSVP
145 inline short IPPROTO_RSVP =	46;
146 #pragma D binding "1.5" IPPROTO_GRE
147 inline short IPPROTO_GRE =	47;
148 #pragma D binding "1.5" IPPROTO_ESP
149 inline short IPPROTO_ESP =	50;
150 #pragma D binding "1.5" IPPROTO_AH
151 inline short IPPROTO_AH =	51;
152 #pragma D binding "1.5" IPPROTO_MOBILE
153 inline short IPPROTO_MOBILE =	55;
154 #pragma D binding "1.5" IPPROTO_ICMPV6
155 inline short IPPROTO_ICMPV6 =	58;
156 #pragma D binding "1.5" IPPROTO_DSTOPTS
157 inline short IPPROTO_DSTOPTS =	60;
158 #pragma D binding "1.5" IPPROTO_ETHERIP
159 inline short IPPROTO_ETHERIP =	97;
160 #pragma D binding "1.5" IPPROTO_PIM
161 inline short IPPROTO_PIM =	103;
162 #pragma D binding "1.5" IPPROTO_IPCOMP
163 inline short IPPROTO_IPCOMP =	108;
164 #pragma D binding "1.5" IPPROTO_SCTP
165 inline short IPPROTO_SCTP =	132;
166 #pragma D binding "1.5" IPPROTO_RAW
167 inline short IPPROTO_RAW =	255;
168 #pragma D binding "1.13" IPPROTO_UDPLITE
169 inline short IPPROTO_UDPLITE = 	136;
170 
171 inline uint8_t INP_IPV4	= 0x01;
172 inline uint8_t INP_IPV6 = 0x02;
173 
174 #pragma D binding "1.5" protocols
175 inline string protocols[int proto] =
176 	proto == IPPROTO_IP ? "IP" :
177 	proto == IPPROTO_ICMP ? "ICMP" :
178 	proto == IPPROTO_IGMP ? "IGMP" :
179 	proto == IPPROTO_IPV4 ? "IPV4" :
180 	proto == IPPROTO_TCP ? "TCP" :
181 	proto == IPPROTO_UDP ? "UDP" :
182 	proto == IPPROTO_IPV6 ? "IPV6" :
183 	proto == IPPROTO_ROUTING ? "ROUTING" :
184 	proto == IPPROTO_FRAGMENT ? "FRAGMENT" :
185 	proto == IPPROTO_RSVP ? "RSVP" :
186 	proto == IPPROTO_GRE ? "GRE" :
187 	proto == IPPROTO_ESP ? "ESP" :
188 	proto == IPPROTO_AH ? "AH" :
189 	proto == IPPROTO_MOBILE ? "MOBILE" :
190 	proto == IPPROTO_ICMPV6 ? "ICMPV6" :
191 	proto == IPPROTO_DSTOPTS ? "DSTOPTS" :
192 	proto == IPPROTO_ETHERIP ? "ETHERIP" :
193 	proto == IPPROTO_PIM ? "PIM" :
194 	proto == IPPROTO_IPCOMP ? "IPCOMP" :
195 	proto == IPPROTO_SCTP ? "SCTP" :
196 	proto == IPPROTO_UDPLITE ? "UDPLITE" :
197 	proto == IPPROTO_RAW ? "RAW" :
198 	"<unknown>";
199 
200 /*
201  * This field is always NULL according to the current definition of the ip
202  * probes.
203  */
204 #pragma D binding "1.5" translator
205 translator pktinfo_t < void *p > {
206 	pkt_addr =	NULL;
207 };
208 
209 #pragma D binding "1.5" translator
210 translator csinfo_t < void *p > {
211 	cs_addr =	NULL;
212 	cs_cid =	(uint64_t)p;
213 	cs_pid =	0;
214 	cs_zoneid =	0;
215 };
216 
217 #pragma D binding "1.6.3" translator
218 translator csinfo_t < struct inpcb *p > {
219 	cs_addr =	NULL;
220 	cs_cid =	(uint64_t)p;
221 	cs_pid =	0;	/* XXX */
222 	cs_zoneid =	0;
223 };
224 
225 #pragma D binding "1.5" translator
226 translator ipinfo_t < uint8_t *p > {
227 	ip_ver =	p == NULL ? 0 : ((struct ip *)p)->ip_v;
228 	ip_plength =	p == NULL ? 0 :
229 	    ((struct ip *)p)->ip_v == 4 ?
230 	    ntohs(((struct ip *)p)->ip_len) - (((struct ip *)p)->ip_hl << 2):
231 	    ntohs(((struct ip6_hdr *)p)->ip6_ctlun.ip6_un1.ip6_un1_plen);
232 	ip_saddr =	p == NULL ? "<unknown>" :
233 	    ((struct ip *)p)->ip_v == 4 ?
234 	    inet_ntoa(&((struct ip *)p)->ip_src.s_addr) :
235 	    inet_ntoa6(&((struct ip6_hdr *)p)->ip6_src);
236 	ip_daddr =	p == NULL ? "<unknown>" :
237 	    ((struct ip *)p)->ip_v == 4 ?
238 	    inet_ntoa(&((struct ip *)p)->ip_dst.s_addr) :
239 	    inet_ntoa6(&((struct ip6_hdr *)p)->ip6_dst);
240 };
241 
242 #pragma D binding "1.13" translator
243 translator ipinfo_t < struct mbuf *m > {
244 	ip_ver =	m == NULL ? 0 : ((struct ip *)m->m_data)->ip_v;
245 	ip_plength =	m == NULL ? 0 :
246 	    ((struct ip *)m->m_data)->ip_v == 4 ?
247 	    ntohs(((struct ip *)m->m_data)->ip_len) -
248 			(((struct ip *)m->m_data)->ip_hl << 2):
249 	    ntohs(((struct ip6_hdr *)m->m_data)->ip6_ctlun.ip6_un1.ip6_un1_plen);
250 	ip_saddr =	m == NULL ? "<unknown>" :
251 	    ((struct ip *)m->m_data)->ip_v == 4 ?
252 	    inet_ntoa(&((struct ip *)m->m_data)->ip_src.s_addr) :
253 	    inet_ntoa6(&((struct ip6_hdr *)m->m_data)->ip6_src);
254 	ip_daddr =	m == NULL ? "<unknown>" :
255 	    ((struct ip *)m->m_data)->ip_v == 4 ?
256 	    inet_ntoa(&((struct ip *)m->m_data)->ip_dst.s_addr) :
257 	    inet_ntoa6(&((struct ip6_hdr *)m->m_data)->ip6_dst);
258 };
259 
260 #pragma D binding "1.5" IFF_LOOPBACK
261 inline int IFF_LOOPBACK =	0x8;
262 
263 #pragma D binding "1.5" translator
264 translator ifinfo_t < struct ifnet *p > {
265 	if_name =	p == NULL ? "<unknown>" : p->if_xname;
266 	if_local =	p == NULL ? 0 : (p->if_flags & IFF_LOOPBACK) == 0 ? 0 : 1;
267 	if_addr =	(uintptr_t)p;
268 };
269 
270 #pragma D binding "1.5" translator
271 translator ipv4info_t < struct ip *p > {
272 	ipv4_ver =	p == NULL ? 0 : p->ip_v;
273 	ipv4_ihl =	p == NULL ? 0 : p->ip_hl;
274 	ipv4_tos =	p == NULL ? 0 : p->ip_tos;
275 	ipv4_length =	p == NULL ? 0 : ntohs(p->ip_len);
276 	ipv4_ident =	p == NULL ? 0 : ntohs(p->ip_id);
277 	ipv4_flags =	p == NULL ? 0 : (ntohs(p->ip_off) & 0xe000) >> 8;
278 	ipv4_offset =	p == NULL ? 0 : ntohs(p->ip_off) & 0x1fff;
279 	ipv4_ttl =	p == NULL ? 0 : p->ip_ttl;
280 	ipv4_protocol =	p == NULL ? 0 : p->ip_p;
281 	ipv4_protostr = p == NULL ? "<null>" : protocols[p->ip_p];
282 	ipv4_checksum =	p == NULL ? 0 : ntohs(p->ip_sum);
283 	ipv4_src =	p == NULL ? 0 : (ipaddr_t)ntohl(p->ip_src.s_addr);
284 	ipv4_dst =	p == NULL ? 0 : (ipaddr_t)ntohl(p->ip_dst.s_addr);
285 	ipv4_saddr =	p == NULL ? 0 : inet_ntoa(&p->ip_src.s_addr);
286 	ipv4_daddr =	p == NULL ? 0 : inet_ntoa(&p->ip_dst.s_addr);
287 	ipv4_hdr =	(ipha_t *)p;
288 };
289 
290 #pragma D binding "1.5" translator
291 translator ipv6info_t < struct ip6_hdr *p > {
292 	ipv6_ver =	p == NULL ? 0 : (ntohl(p->ip6_ctlun.ip6_un1.ip6_un1_flow) & 0xf0000000) >> 28;
293 	ipv6_tclass =	p == NULL ? 0 : (ntohl(p->ip6_ctlun.ip6_un1.ip6_un1_flow) & 0x0ff00000) >> 20;
294 	ipv6_flow =	p == NULL ? 0 : ntohl(p->ip6_ctlun.ip6_un1.ip6_un1_flow) & 0x000fffff;
295 	ipv6_plen =	p == NULL ? 0 : ntohs(p->ip6_ctlun.ip6_un1.ip6_un1_plen);
296 	ipv6_nexthdr =	p == NULL ? 0 : p->ip6_ctlun.ip6_un1.ip6_un1_nxt;
297 	ipv6_nextstr =	p == NULL ? "<null>" : protocols[p->ip6_ctlun.ip6_un1.ip6_un1_nxt];
298 	ipv6_hlim =	p == NULL ? 0 : p->ip6_ctlun.ip6_un1.ip6_un1_hlim;
299 	ipv6_src =	p == NULL ? 0 : (in6_addr_t *)&p->ip6_src;
300 	ipv6_dst =	p == NULL ? 0 : (in6_addr_t *)&p->ip6_dst;
301 	ipv6_saddr =	p == NULL ? 0 : inet_ntoa6(&p->ip6_src);
302 	ipv6_daddr =	p == NULL ? 0 : inet_ntoa6(&p->ip6_dst);
303 	ipv6_hdr =	p;
304 };
305