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