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