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