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