xref: /xnu-10002.1.13/bsd/dev/dtrace/scripts/ip.d (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 2006-2012 Apple Inc.  All Rights Reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*1031c584SApple OSS Distributions  * file.
12*1031c584SApple OSS Distributions  *
13*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*1031c584SApple OSS Distributions  * limitations under the License.
20*1031c584SApple OSS Distributions  *
21*1031c584SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*1031c584SApple OSS Distributions  */
23*1031c584SApple OSS Distributions 
24*1031c584SApple OSS Distributions #pragma D depends_on library darwin.d
25*1031c584SApple OSS Distributions #pragma D depends_on module mach_kernel
26*1031c584SApple OSS Distributions #pragma D depends_on provider ip
27*1031c584SApple OSS Distributions 
28*1031c584SApple OSS Distributions /* Translators for IP dtrace provider */
29*1031c584SApple OSS Distributions 
30*1031c584SApple OSS Distributions typedef struct pktinfo {
31*1031c584SApple OSS Distributions 	struct mbuf *pkt_addr;	/* Pointer to the packet (struct mbuf) */
32*1031c584SApple OSS Distributions } pktinfo_t;
33*1031c584SApple OSS Distributions 
34*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
35*1031c584SApple OSS Distributions translator pktinfo_t < struct mbuf *m > {
36*1031c584SApple OSS Distributions 	pkt_addr = m;
37*1031c584SApple OSS Distributions };
38*1031c584SApple OSS Distributions 
39*1031c584SApple OSS Distributions typedef struct csinfo {
40*1031c584SApple OSS Distributions 	uint8_t   ip_ver;
41*1031c584SApple OSS Distributions 	uint16_t  dport;
42*1031c584SApple OSS Distributions 	uint16_t  sport;
43*1031c584SApple OSS Distributions 	string	  ip_daddr;
44*1031c584SApple OSS Distributions 	string	  ip_saddr;
45*1031c584SApple OSS Distributions 	uint8_t	  protocol;
46*1031c584SApple OSS Distributions 	struct inpcb *cs_addr;	/* Pointer to inpcb (struct inpcb) */
47*1031c584SApple OSS Distributions } csinfo_t;
48*1031c584SApple OSS Distributions 
49*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
50*1031c584SApple OSS Distributions translator csinfo_t < struct inpcb *P > {
51*1031c584SApple OSS Distributions 	cs_addr = P;
52*1031c584SApple OSS Distributions 	ip_ver = (P != NULL) ? (((P->inp_vflag & 0x2) != 0) ? 6 : 4) : 0;
53*1031c584SApple OSS Distributions 	dport = (P != NULL) ? ntohs(P->inp_fport) : 0;
54*1031c584SApple OSS Distributions 	sport = (P != NULL) ? ntohs(P->inp_lport) : 0;
55*1031c584SApple OSS Distributions 	ip_saddr = (P != NULL) ? (((P->inp_vflag & 0x2) != 0) ?
56*1031c584SApple OSS Distributions 			inet_ntoa6(&P->inp_dependladdr.inp6_local) :
57*1031c584SApple OSS Distributions 			inet_ntoa((uint32_t *)&P->inp_dependladdr.inp46_local.ia46_addr4.s_addr)) : "<null>";
58*1031c584SApple OSS Distributions 	ip_daddr = (P != NULL) ? (((P->inp_vflag & 0x2) != 0) ?
59*1031c584SApple OSS Distributions 			inet_ntoa6(&P->inp_dependfaddr.inp6_foreign) :
60*1031c584SApple OSS Distributions 			inet_ntoa((uint32_t *)&P->inp_dependfaddr.inp46_foreign.ia46_addr4.s_addr)) : "<null>";
61*1031c584SApple OSS Distributions 	protocol = P->inp_ip_p;
62*1031c584SApple OSS Distributions };
63*1031c584SApple OSS Distributions 
64*1031c584SApple OSS Distributions typedef struct ipinfo {
65*1031c584SApple OSS Distributions 	uint8_t  ip_ver;		/* IP version (4, 6) */
66*1031c584SApple OSS Distributions 	uint16_t ip_plength;		/* payload length */
67*1031c584SApple OSS Distributions 	string   ip_saddr;		/* source address */
68*1031c584SApple OSS Distributions 	string   ip_daddr;		/* destination address */
69*1031c584SApple OSS Distributions } ipinfo_t;
70*1031c584SApple OSS Distributions 
71*1031c584SApple OSS Distributions /*
72*1031c584SApple OSS Distributions  * The ip vhl byte is the first byte in struct ip. The type names are
73*1031c584SApple OSS Distributions  * different depending on whether _IP_VHL is defined or not and that will
74*1031c584SApple OSS Distributions  * confuse dtrace. So instead of using type names, just cast and extract
75*1031c584SApple OSS Distributions  * version and header length info from the ip structure.
76*1031c584SApple OSS Distributions  */
77*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
78*1031c584SApple OSS Distributions translator ipinfo_t < struct ip * ip > {
79*1031c584SApple OSS Distributions 	ip_ver = (ip != NULL) ? ((*(uint8_t *) ip) & 0xf0) >> 4 : 0;
80*1031c584SApple OSS Distributions 	ip_plength = (ip != NULL) ?
81*1031c584SApple OSS Distributions 		(ntohs(ip->ip_len) - (((*(uint8_t *) ip) & 0x0f) << 2)) : 0;
82*1031c584SApple OSS Distributions 	ip_saddr = (ip != NULL) ? inet_ntoa((uint32_t *)&ip->ip_src.s_addr) : "<null>";
83*1031c584SApple OSS Distributions 	ip_daddr = (ip != NULL) ? inet_ntoa((uint32_t *)&ip->ip_dst.s_addr) : "<null>";
84*1031c584SApple OSS Distributions };
85*1031c584SApple OSS Distributions 
86*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
87*1031c584SApple OSS Distributions translator ipinfo_t < struct ip6_hdr *ip6 > {
88*1031c584SApple OSS Distributions 	ip_ver = (ip6 != NULL) ? (ip6->ip6_ctlun.ip6_un2_vfc & 0xf0) >> 4 : 0;
89*1031c584SApple OSS Distributions 	ip_plength = (ip6 != NULL) ? (ntohs(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen)) : 0;
90*1031c584SApple OSS Distributions 	ip_saddr = (ip6 != NULL) ? inet_ntoa6(&ip6->ip6_src) : "<null>";
91*1031c584SApple OSS Distributions 	ip_daddr = (ip6 != NULL) ? inet_ntoa6(&ip6->ip6_dst) : "<null>";
92*1031c584SApple OSS Distributions };
93*1031c584SApple OSS Distributions 
94*1031c584SApple OSS Distributions /*
95*1031c584SApple OSS Distributions  * void_ip_t is a void pointer to either an IPv4 or IPv6 header. It has
96*1031c584SApple OSS Distributions  * its own type name so that a translator can be determined.
97*1031c584SApple OSS Distributions  */
98*1031c584SApple OSS Distributions typedef uintptr_t void_ip_t;
99*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
100*1031c584SApple OSS Distributions translator ipinfo_t < void_ip_t *i> {
101*1031c584SApple OSS Distributions 	ip_ver = (i != NULL) ? (*(uint8_t *)i >> 4) : 0;
102*1031c584SApple OSS Distributions 	ip_plength = (i != NULL) ? (((*(uint8_t *)i) >> 4 == 4) ?
103*1031c584SApple OSS Distributions 		ntohs(((struct ip *)i)->ip_len) -
104*1031c584SApple OSS Distributions 		(((*(uint8_t *)i) & 0x0f) << 2):
105*1031c584SApple OSS Distributions 		(((*(uint8_t *)i) >> 4 == 6) ?
106*1031c584SApple OSS Distributions 		ntohs(((struct ip6_hdr *)i)->ip6_ctlun.ip6_un1.ip6_un1_plen) : 0)) : 0;
107*1031c584SApple OSS Distributions 	ip_saddr = (i != NULL) ? ((((*(uint8_t *)i)) >> 4 == 4) ?
108*1031c584SApple OSS Distributions 		inet_ntoa((uint32_t *)&(((struct ip *)i)->ip_src.s_addr)) :
109*1031c584SApple OSS Distributions 		((((*(uint8_t *)i) >> 4) == 6) ?
110*1031c584SApple OSS Distributions 		inet_ntoa6(&((struct ip6_hdr *)i)->ip6_src) : "<unknown>")) : "<null>";
111*1031c584SApple OSS Distributions 	ip_daddr = (i != NULL) ? (((*(uint8_t *)i) >> 4 == 4) ?
112*1031c584SApple OSS Distributions 		inet_ntoa((uint32_t *)&((struct ip*)i)->ip_dst.s_addr) : ((((*(uint8_t *)i) >> 4) == 6) ?
113*1031c584SApple OSS Distributions 		inet_ntoa6(&((struct ip6_hdr *)i)->ip6_dst) : "<unknown>")) : "<null>";
114*1031c584SApple OSS Distributions };
115*1031c584SApple OSS Distributions 
116*1031c584SApple OSS Distributions typedef struct ifinfo {
117*1031c584SApple OSS Distributions 	string    if_name;		/* interface name */
118*1031c584SApple OSS Distributions 	int8_t    if_local;		/* is delivered locally */
119*1031c584SApple OSS Distributions 	int8_t    if_ipstack;		/* ipstack id */
120*1031c584SApple OSS Distributions 	struct ifnet *if_addr;		/* pointer to raw ill_t */
121*1031c584SApple OSS Distributions 	uint16_t  if_flags;		/* flags: up/down, broadcast etc. */
122*1031c584SApple OSS Distributions 	uint32_t  if_eflags;		/* extended flags */
123*1031c584SApple OSS Distributions 	uint16_t  if_unit;
124*1031c584SApple OSS Distributions } ifinfo_t;
125*1031c584SApple OSS Distributions 
126*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
127*1031c584SApple OSS Distributions translator ifinfo_t < struct ifnet *ifp > {
128*1031c584SApple OSS Distributions 	if_name = (ifp != NULL) ? ifp->if_name : "<null>";
129*1031c584SApple OSS Distributions 	if_unit = (ifp != NULL) ? ifp->if_unit : 0;
130*1031c584SApple OSS Distributions 	if_local = 0;
131*1031c584SApple OSS Distributions 	if_ipstack = 0;
132*1031c584SApple OSS Distributions 	if_addr = ifp;
133*1031c584SApple OSS Distributions 	if_flags = (ifp != NULL) ? ifp->if_flags : 0;
134*1031c584SApple OSS Distributions 	if_eflags = (ifp != NULL) ? ifp->if_eflags : 0;
135*1031c584SApple OSS Distributions 
136*1031c584SApple OSS Distributions };
137*1031c584SApple OSS Distributions 
138*1031c584SApple OSS Distributions typedef struct ipv4info {
139*1031c584SApple OSS Distributions 	uint8_t	  ipv4_ver;		/* IP version (4) */
140*1031c584SApple OSS Distributions 	uint8_t   ipv4_ihl;		/* header length, bytes */
141*1031c584SApple OSS Distributions 	uint8_t   ipv4_tos;		/* type of service field */
142*1031c584SApple OSS Distributions 	uint16_t  ipv4_length;		/* length (header + payload) */
143*1031c584SApple OSS Distributions 	uint16_t  ipv4_ident;		/* identification */
144*1031c584SApple OSS Distributions 	uint8_t   ipv4_flags;		/* IP flags */
145*1031c584SApple OSS Distributions 	uint16_t  ipv4_offset;		/* fragment offset */
146*1031c584SApple OSS Distributions 	uint8_t   ipv4_ttl;		/* time to live */
147*1031c584SApple OSS Distributions 	uint8_t   ipv4_protocol;	/* next level protocol */
148*1031c584SApple OSS Distributions 	string    ipv4_protostr;	/* next level protocol, as a string */
149*1031c584SApple OSS Distributions 	uint16_t  ipv4_checksum;	/* header checksum */
150*1031c584SApple OSS Distributions 	in_addr_t ipv4_src;		/* source address */
151*1031c584SApple OSS Distributions 	in_addr_t ipv4_dst;		/* destination address */
152*1031c584SApple OSS Distributions 	string    ipv4_saddr;		/* source address, string */
153*1031c584SApple OSS Distributions 	string    ipv4_daddr;		/* destination address, string */
154*1031c584SApple OSS Distributions 	struct ip *ipv4_hdr;		/* pointer to raw header */
155*1031c584SApple OSS Distributions } ipv4info_t;
156*1031c584SApple OSS Distributions 
157*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
158*1031c584SApple OSS Distributions translator ipv4info_t < struct ip *ip > {
159*1031c584SApple OSS Distributions 	ipv4_ver = (ip != NULL) ? (*(uint8_t *)ip & 0xf0) >> 4 : 0;
160*1031c584SApple OSS Distributions 	ipv4_ihl = (ip != NULL) ? ((*(uint8_t *)ip & 0x0f) << 2) : 0;
161*1031c584SApple OSS Distributions 	ipv4_tos = (ip!= NULL) ? ip->ip_tos : 0;
162*1031c584SApple OSS Distributions 	ipv4_length = (ip != NULL) ? ntohs(ip->ip_len) : 0;
163*1031c584SApple OSS Distributions 	ipv4_ident = (ip != NULL) ? ip->ip_id : 0;
164*1031c584SApple OSS Distributions 	ipv4_flags = (ip != NULL) ? (ntohs(ip->ip_off) & 0xe000) : 0;
165*1031c584SApple OSS Distributions 	ipv4_offset = (ip != NULL) ? (ntohs(ip->ip_off) & 0x1fff) : 0;
166*1031c584SApple OSS Distributions 	ipv4_ttl = (ip != NULL) ? ip->ip_ttl : 0;
167*1031c584SApple OSS Distributions 	ipv4_protocol = (ip != NULL) ? ip->ip_p : 0;
168*1031c584SApple OSS Distributions 	ipv4_protostr = (ip == NULL) ? "<null>" :
169*1031c584SApple OSS Distributions 			(ip->ip_p == 1) ? "ICMP" :
170*1031c584SApple OSS Distributions 			(ip->ip_p == 2) ? "IGMP" :
171*1031c584SApple OSS Distributions 			(ip->ip_p == 4) ? "IP" :
172*1031c584SApple OSS Distributions 			(ip->ip_p == 6) ? "TCP":
173*1031c584SApple OSS Distributions 			(ip->ip_p == 17) ? "UDP" :
174*1031c584SApple OSS Distributions 			(ip->ip_p == 50) ? "ESP":
175*1031c584SApple OSS Distributions 			(ip->ip_p == 51) ? "AH" :
176*1031c584SApple OSS Distributions 			(ip->ip_p == 58) ? "ICMPV6" :
177*1031c584SApple OSS Distributions 			(ip->ip_p == 255) ? "RAW" : stringof(ip->ip_p);
178*1031c584SApple OSS Distributions 	ipv4_checksum = (ip != NULL) ? ntohs(ip->ip_sum) : 0;
179*1031c584SApple OSS Distributions 	ipv4_src = (ip != NULL) ? ip->ip_src.s_addr : 0;
180*1031c584SApple OSS Distributions 	ipv4_dst = (ip != NULL) ? ip->ip_dst.s_addr : 0;
181*1031c584SApple OSS Distributions 	ipv4_saddr = (ip != NULL) ? inet_ntoa((uint32_t *)&ip->ip_src.s_addr) : "<null>";
182*1031c584SApple OSS Distributions 	ipv4_daddr = (ip != NULL) ? inet_ntoa((uint32_t *)&ip->ip_dst.s_addr) : "<null>";
183*1031c584SApple OSS Distributions 	ipv4_hdr = ip;
184*1031c584SApple OSS Distributions };
185*1031c584SApple OSS Distributions 
186*1031c584SApple OSS Distributions typedef struct ipv6info {
187*1031c584SApple OSS Distributions 	uint8_t    ipv6_ver;		/* IP version (6) */
188*1031c584SApple OSS Distributions 	uint8_t    ipv6_tclass;		/* traffic class */
189*1031c584SApple OSS Distributions 	uint32_t   ipv6_flow;		/* flow label */
190*1031c584SApple OSS Distributions 	uint16_t   ipv6_plen;		/* payload length */
191*1031c584SApple OSS Distributions 	uint8_t    ipv6_nexthdr;	/* next header protocol */
192*1031c584SApple OSS Distributions 	string     ipv6_nextstr;	/* next header protocol, as a string */
193*1031c584SApple OSS Distributions 	uint8_t    ipv6_hlim;		/* hop limit */
194*1031c584SApple OSS Distributions 	struct in6_addr *ipv6_src;	/* source address, pointer to struct in6_addr */
195*1031c584SApple OSS Distributions 	struct in6_addr *ipv6_dst;	/* destination address, pointer to struct in6_addr */
196*1031c584SApple OSS Distributions 	string     ipv6_saddr;		/* source address, string */
197*1031c584SApple OSS Distributions 	string     ipv6_daddr;		/* destination address, string */
198*1031c584SApple OSS Distributions 	struct ip6_hdr *ipv6_hdr;	/* pointer to raw header */
199*1031c584SApple OSS Distributions } ipv6info_t;
200*1031c584SApple OSS Distributions 
201*1031c584SApple OSS Distributions #pragma D binding "1.0" translator
202*1031c584SApple OSS Distributions translator ipv6info_t < struct ip6_hdr *ip6 > {
203*1031c584SApple OSS Distributions  	ipv6_ver = (ip6 != NULL) ? ip6->ip6_ctlun.ip6_un2_vfc : 10;
204*1031c584SApple OSS Distributions 	ipv6_tclass = (ip6 != NULL) ? (ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0x0ff00000) >> 20 : 0;
205*1031c584SApple OSS Distributions 	ipv6_flow = (ip6 != NULL) ? (ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0x000fffff) : 0;
206*1031c584SApple OSS Distributions 	ipv6_plen = (ip6 != NULL) ? ntohs(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen) : 0;
207*1031c584SApple OSS Distributions 	ipv6_nexthdr = (ip6 != NULL) ? ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt : 0;
208*1031c584SApple OSS Distributions 	ipv6_nextstr = (ip6 == NULL) ? "<null>" :
209*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 1) ? "ICMP" :
210*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 2) ? "IGMP" :
211*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 4) ? "IP" :
212*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 6) ? "TCP" :
213*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 17) ? "UDP" :
214*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 50) ? "ESP" :
215*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 51) ? "AH" :
216*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 58) ? "ICMPV6" :
217*1031c584SApple OSS Distributions 			(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt == 255) ? "RAW" :
218*1031c584SApple OSS Distributions 			stringof(ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt);
219*1031c584SApple OSS Distributions 	ipv6_hlim = (ip6 != NULL) ? ip6->ip6_ctlun.ip6_un1.ip6_un1_hlim : 0;
220*1031c584SApple OSS Distributions 	ipv6_src = (ip6 != NULL) ? (&ip6->ip6_src) : 0;
221*1031c584SApple OSS Distributions 	ipv6_dst = (ip6 != NULL) ? (&ip6->ip6_dst) : 0;
222*1031c584SApple OSS Distributions 	ipv6_saddr = (ip6 != NULL) ? inet_ntoa6(&ip6->ip6_src) : "<null>";
223*1031c584SApple OSS Distributions 	ipv6_daddr = (ip6 != NULL) ? inet_ntoa6(&ip6->ip6_dst) : "<null>";
224*1031c584SApple OSS Distributions 	ipv6_hdr = ip6;
225*1031c584SApple OSS Distributions };
226