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