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