1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 
  25 #pragma D depends_on module ip
  26 #pragma D depends_on provider ip
  27 
  28 inline int IPH_DF = @IPH_DF@;
  29 #pragma D binding "1.5" IPH_DF
  30 inline int IPH_MF = @IPH_MF@;
  31 #pragma D binding "1.5" IPH_MF
  32 
  33 #pragma D binding "1.5" IPPROTO_IP
  34 inline int IPPROTO_IP = @IPPROTO_IP@;
  35 #pragma D binding "1.5" IPPROTO_HOPOPTS
  36 inline int IPPROTO_HOPOPTS = @IPPROTO_HOPOPTS@;
  37 #pragma D binding "1.5" IPPROTO_ICMP
  38 inline int IPPROTO_ICMP = @IPPROTO_ICMP@;
  39 #pragma D binding "1.5" IPPROTO_IGMP
  40 inline int IPPROTO_IGMP = @IPPROTO_IGMP@;
  41 #pragma D binding "1.5" IPPROTO_GGP
  42 inline int IPPROTO_GGP = @IPPROTO_GGP@;
  43 #pragma D binding "1.5" IPPROTO_ENCAP
  44 inline int IPPROTO_ENCAP = @IPPROTO_ENCAP@;
  45 #pragma D binding "1.5" IPPROTO_TCP
  46 inline int IPPROTO_TCP = @IPPROTO_TCP@;
  47 #pragma D binding "1.5" IPPROTO_EGP
  48 inline int IPPROTO_EGP = @IPPROTO_EGP@;
  49 #pragma D binding "1.5" IPPROTO_PUP
  50 inline int IPPROTO_PUP = @IPPROTO_PUP@;
  51 #pragma D binding "1.5" IPPROTO_UDP
  52 inline int IPPROTO_UDP = @IPPROTO_UDP@;
  53 #pragma D binding "1.5" IPPROTO_IDP
  54 inline int IPPROTO_IDP = @IPPROTO_IDP@;
  55 #pragma D binding "1.5" IPPROTO_IPV6
  56 inline int IPPROTO_IPV6 = @IPPROTO_IPV6@;
  57 #pragma D binding "1.5" IPPROTO_ROUTING
  58 inline int IPPROTO_ROUTING = @IPPROTO_ROUTING@;
  59 #pragma D binding "1.5" IPPROTO_FRAGMENT
  60 inline int IPPROTO_FRAGMENT = @IPPROTO_FRAGMENT@;
  61 #pragma D binding "1.5" IPPROTO_RSVP
  62 inline int IPPROTO_RSVP = @IPPROTO_RSVP@;
  63 #pragma D binding "1.5" IPPROTO_ESP
  64 inline int IPPROTO_ESP = @IPPROTO_ESP@;
  65 #pragma D binding "1.5" IPPROTO_AH
  66 inline int IPPROTO_AH = @IPPROTO_AH@;
  67 #pragma D binding "1.5" IPPROTO_ICMPV6
  68 inline int IPPROTO_ICMPV6 = @IPPROTO_ICMPV6@;
  69 #pragma D binding "1.5" IPPROTO_NONE
  70 inline int IPPROTO_NONE = @IPPROTO_NONE@;
  71 #pragma D binding "1.5" IPPROTO_DSTOPTS
  72 inline int IPPROTO_DSTOPTS = @IPPROTO_DSTOPTS@;
  73 #pragma D binding "1.5" IPPROTO_HELLO
  74 inline int IPPROTO_HELLO = @IPPROTO_HELLO@;
  75 #pragma D binding "1.5" IPPROTO_ND
  76 inline int IPPROTO_ND = @IPPROTO_ND@;
  77 #pragma D binding "1.5" IPPROTO_EON
  78 inline int IPPROTO_EON = @IPPROTO_EON@;
  79 #pragma D binding "1.5" IPPROTO_OSPF
  80 inline int IPPROTO_OSPF = @IPPROTO_OSPF@;
  81 #pragma D binding "1.5" IPPROTO_PIM
  82 inline int IPPROTO_PIM = @IPPROTO_PIM@;
  83 #pragma D binding "1.5" IPPROTO_SCTP
  84 inline int IPPROTO_SCTP = @IPPROTO_SCTP@;
  85 #pragma D binding "1.5" IPPROTO_RAW
  86 inline int IPPROTO_RAW = @IPPROTO_RAW@;
  87 #pragma D binding "1.5" IPPROTO_MAX
  88 inline int IPPROTO_MAX = @IPPROTO_MAX@;
  89 #pragma D binding "1.10" IPPROTO_DCCP
  90 inline int IPPROTO_DCCP = @IPPROTO_DCCP@;
  91 
  92 /*
  93  * pktinfo is where packet ID info can be made available for deeper
  94  * analysis if packet IDs become supported by the kernel in the future.
  95  * The pkt_addr member is currently always NULL.
  96  */
  97 typedef struct pktinfo {
  98         uintptr_t pkt_addr;
  99 } pktinfo_t;
 100 
 101 /*
 102  * csinfo is where connection state info is made available.
 103  */
 104 typedef struct csinfo {
 105         uintptr_t cs_addr;
 106         uint64_t cs_cid;
 107         pid_t cs_pid;
 108         zoneid_t cs_zoneid;
 109 } csinfo_t;
 110 
 111 /*
 112  * ipinfo contains common IP info for both IPv4 and IPv6.
 113  */
 114 typedef struct ipinfo {
 115         uint8_t ip_ver;                 /* IP version (4, 6) */
 116         uint32_t ip_plength;            /* payload length */
 117         string ip_saddr;                /* source address */
 118         string ip_daddr;                /* destination address */
 119 } ipinfo_t;
 120 
 121 /*
 122  * ifinfo contains network interface info.
 123  */
 124 typedef struct ifinfo {
 125         string if_name;                 /* interface name */
 126         int8_t if_local;                /* is delivered locally */
 127         netstackid_t if_ipstack;        /* ipstack ID */
 128         uintptr_t if_addr;              /* pointer to raw ill_t */
 129 } ifinfo_t;
 130 
 131 /*
 132  * ipv4info is a translated version of the IPv4 header (with raw pointer).
 133  * These values are NULL if the packet is not IPv4.
 134  */
 135 typedef struct ipv4info {
 136         uint8_t ipv4_ver;               /* IP version (4) */
 137         uint8_t ipv4_ihl;               /* header length, bytes */
 138         uint8_t ipv4_tos;               /* type of service field */
 139         uint16_t ipv4_length;           /* length (header + payload) */
 140         uint16_t ipv4_ident;            /* identification */
 141         uint8_t ipv4_flags;             /* IP flags */
 142         uint16_t ipv4_offset;           /* fragment offset */
 143         uint8_t ipv4_ttl;               /* time to live */
 144         uint8_t ipv4_protocol;          /* next level protocol */
 145         string ipv4_protostr;           /* next level protocol, as a string */
 146         uint16_t ipv4_checksum;         /* header checksum */
 147         ipaddr_t ipv4_src;              /* source address */
 148         ipaddr_t ipv4_dst;              /* destination address */
 149         string ipv4_saddr;              /* source address, string */
 150         string ipv4_daddr;              /* destination address, string */
 151         ipha_t *ipv4_hdr;               /* pointer to raw header */
 152 } ipv4info_t;
 153 
 154 /*
 155  * ipv6info is a translated version of the IPv6 header (with raw pointer).
 156  * These values are NULL if the packet is not IPv6.
 157  */
 158 typedef struct ipv6info {
 159         uint8_t ipv6_ver;               /* IP version (6) */
 160         uint8_t ipv6_tclass;            /* traffic class */
 161         uint32_t ipv6_flow;             /* flow label */
 162         uint16_t ipv6_plen;             /* payload length */
 163         uint8_t ipv6_nexthdr;           /* next header protocol */
 164         string ipv6_nextstr;            /* next header protocol, as a string */
 165         uint8_t ipv6_hlim;              /* hop limit */
 166         in6_addr_t *ipv6_src;           /* source address */
 167         in6_addr_t *ipv6_dst;           /* destination address */
 168         string ipv6_saddr;              /* source address, string */
 169         string ipv6_daddr;              /* destination address, string */
 170         ip6_t *ipv6_hdr;                /* pointer to raw header */
 171 } ipv6info_t;
 172 
 173 /*
 174  * void_ip_t is a void pointer to either an IPv4 or IPv6 header.  It has
 175  * its own type name so that a translator can be determined.
 176  */
 177 typedef uintptr_t void_ip_t;
 178 
 179 /*
 180  * __dtrace_ipsr_ill_t is used by the translator to take an ill_t plus an
 181  * additional arg6 from the ip:::send and ip:::recieve probes, and translate
 182  * them to an ifinfo_t.
 183  */
 184 typedef ill_t __dtrace_ipsr_ill_t;
 185 
 186 /*
 187  * __dtrace_tcp_void_ip_t is used by the translator to take either the
 188  * non-NULL void_ip_t * passed in or, if it is NULL, uses arg3 (tcp_t *)
 189  * from the tcp:::send and tcp:::recieve probes to translate to an ipinfo_t.
 190  * When no headers are available in the TCP fusion case for tcp:::send
 191  * and tcp:::receive case, this allows us to present the consumer with header
 192  * data based on the tcp_t * content in order to hide the implementation
 193  * details of TCP fusion.
 194  */
 195 typedef void * __dtrace_tcp_void_ip_t;
 196 
 197 #pragma D binding "1.5" translator
 198 translator pktinfo_t < mblk_t *M > {
 199         pkt_addr = NULL;
 200 };
 201 
 202 #pragma D binding "1.5" translator
 203 translator csinfo_t < conn_t *C > {
 204         cs_addr = NULL;
 205 };
 206 
 207 #pragma D binding "1.6.3" translator
 208 translator csinfo_t < ip_xmit_attr_t *C > {
 209         cs_addr = (uintptr_t)C;
 210         cs_cid = C ? C->ixa_conn_id : NULL;
 211         cs_pid = C ? C->ixa_cpid : -1;
 212         cs_zoneid = C ?
 213             (C->ixa_ipst == NULL || C->ixa_ipst->ips_netstack == NULL ||
 214             C->ixa_ipst->ips_netstack->netstack_stackid ==
 215             @GLOBAL_NETSTACKID@ ||
 216             C->ixa_cred == NULL ||
 217             C->ixa_cred->cr_zone == NULL ||
 218             C->ixa_cred->cr_uid == -1 ?
 219             C->ixa_zoneid : C->ixa_cred->cr_zone->zone_id) : -1;
 220 };
 221 
 222 #pragma D binding "1.5" translator
 223 translator ipinfo_t < ipha_t *I > {
 224         ip_ver = I->ipha_version_and_hdr_length >> 4;
 225         ip_plength = ntohs(I->ipha_length) -
 226             ((I->ipha_version_and_hdr_length & 0xf) << 2);
 227         ip_saddr = inet_ntoa(&I->ipha_src);
 228         ip_daddr = inet_ntoa(&I->ipha_dst);
 229 };
 230 
 231 #pragma D binding "1.5" translator
 232 translator ipinfo_t < ip6_t *I > {
 233         ip_ver = *(uint8_t *)I >> 4;
 234         ip_plength = ntohs(I->ip6_ctlun.ip6_un1.ip6_un1_plen);
 235         ip_saddr = inet_ntoa6(&I->ip6_src);
 236         ip_daddr = inet_ntoa6(&I->ip6_dst);
 237 };
 238 
 239 #pragma D binding "1.5" translator
 240 translator ipinfo_t < void_ip_t *I > {
 241         ip_ver = I != NULL ? *(uint8_t *)I >> 4 : 0;
 242         ip_plength = I != NULL ? (*(uint8_t *)I >> 4 == 4 ?
 243             ntohs(((ipha_t *)I)->ipha_length) -
 244             ((((ipha_t *)I)->ipha_version_and_hdr_length & 0xf) << 2) :
 245             *(uint8_t *)I >> 4 == 6 ?
 246             ntohs(((ip6_t *)I)->ip6_ctlun.ip6_un1.ip6_un1_plen) : 0) : 0;
 247         ip_saddr = I != NULL ? (*(uint8_t *)I >> 4 == 4 ?
 248             inet_ntoa(&((ipha_t *)I)->ipha_src) : *(uint8_t *)I >> 4 == 6 ?
 249             inet_ntoa6(&((ip6_t *)I)->ip6_src) : "<unknown>") : "<unknown>";
 250         ip_daddr = I != NULL ? (*(uint8_t *)I >> 4 == 4 ?
 251             inet_ntoa(&((ipha_t *)I)->ipha_dst) : *(uint8_t *)I >> 4 == 6 ?
 252             inet_ntoa6(&((ip6_t *)I)->ip6_dst) : "<unknown>") : "<unknown>";
 253 };
 254 
 255 #pragma D binding "1.5" translator
 256 translator ifinfo_t < __dtrace_ipsr_ill_t *I > {
 257         if_name = I != NULL ? stringof(I->ill_name) : "<null>";
 258         if_ipstack = I != NULL ? I->ill_ipst->ips_netstack->netstack_stackid
 259             : 0;
 260         if_local = arg6;                /* probe dependent */
 261         if_addr = (uintptr_t)I;
 262 };
 263 
 264 /*
 265  * Translate to an ipinfo_t * from either the non-NULL void_ip_t * passed in,
 266  * or use arg3 (tcp_t *) to fabricate ip header info.
 267  */
 268 #pragma D binding "1.6.3" translator
 269 translator ipinfo_t < __dtrace_tcp_void_ip_t *I > {
 270         ip_ver = I != NULL ? *(uint8_t *)I >> 4 :
 271             arg3 != NULL ? ((tcp_t *)arg3)->tcp_connp->conn_ipversion : 0;
 272         ip_plength =
 273             I != NULL && *(uint8_t *)I >> 4 == 4 ?
 274             ntohs(((ipha_t *)I)->ipha_length) -
 275             ((((ipha_t *)I)->ipha_version_and_hdr_length & 0xf) << 2) :
 276             I != NULL && *(uint8_t *)I >> 4 == 6 ?
 277             ntohs(((ip6_t *)I)->ip6_ctlun.ip6_un1.ip6_un1_plen) :
 278             I != NULL ? 0 :
 279             arg3 != NULL && probename == "send" ?
 280             ((tcp_t *)arg3)->tcp_last_sent_len + @TCP_MIN_HEADER_LENGTH@ :
 281             arg3 != NULL && probename == "receive" ?
 282             ((tcp_t *)arg3)->tcp_last_recv_len + @TCP_MIN_HEADER_LENGTH@ :
 283             0;
 284         ip_saddr =
 285             I != NULL && *(uint8_t *)I >> 4 == 4 ?
 286             inet_ntoa(&((ipha_t *)I)->ipha_src) :
 287             I != NULL && *(uint8_t *)I >> 4 == 6 ?
 288             inet_ntoa6(&((ip6_t *)I)->ip6_src) :
 289             I != NULL ? "<unknown>" :
 290             arg3 != NULL && probename == "send" ?
 291             inet_ntoa6(&((tcp_t *)arg3)->tcp_connp->connua_v6addr.connua_laddr):
 292             arg3 != NULL && probename == "receive" ?
 293             inet_ntoa6(&((tcp_t *)arg3)->tcp_connp->connua_v6addr.connua_faddr):
 294             "<unknown>";
 295         ip_daddr =
 296             I != NULL && *(uint8_t *)I >> 4 == 4 ?
 297             inet_ntoa(&((ipha_t *)I)->ipha_dst) :
 298             I != NULL && *(uint8_t *)I >> 4 == 6 ?
 299             inet_ntoa6(&((ip6_t *)I)->ip6_dst) :
 300             I != NULL ? "<unknown>" :
 301             arg3 != NULL && probename == "send" ?
 302             inet_ntoa6(&((tcp_t *)arg3)->tcp_connp->connua_v6addr.connua_faddr):
 303             arg3 != NULL && probename == "receive" ?
 304             inet_ntoa6(&((tcp_t *)arg3)->tcp_connp->connua_v6addr.connua_laddr):
 305             "<unknown>";
 306 };
 307 
 308 #pragma D binding "1.5" translator
 309 translator ipv4info_t < ipha_t *I > {
 310         ipv4_ver = I != NULL ? I->ipha_version_and_hdr_length >> 4 : 0;
 311         ipv4_ihl = I != NULL ? (I->ipha_version_and_hdr_length & 0xf) << 2 : 0;
 312         ipv4_tos = I != NULL ? I->ipha_type_of_service : 0;
 313         ipv4_length = I != NULL ? ntohs(I->ipha_length) : 0;
 314         ipv4_ident = I != NULL ? ntohs(I->ipha_ident) : 0;
 315         ipv4_flags = I != NULL ? ntohs(I->ipha_fragment_offset_and_flags) >>
 316             12 : 0;
 317         ipv4_offset = I != NULL ? ntohs(I->ipha_fragment_offset_and_flags) &
 318             0x0fff : 0;
 319         ipv4_ttl = I != NULL ? I->ipha_ttl : 0;
 320         ipv4_protocol = I != NULL ? I->ipha_protocol : 0;
 321         ipv4_protostr = I == NULL ? "<null>" :
 322             I->ipha_protocol == IPPROTO_TCP     ? "TCP"    :
 323             I->ipha_protocol == IPPROTO_UDP     ? "UDP"    :
 324             I->ipha_protocol == IPPROTO_IP      ? "IP"     :
 325             I->ipha_protocol == IPPROTO_ICMP    ? "ICMP"   :
 326             I->ipha_protocol == IPPROTO_IGMP    ? "IGMP"   :
 327             I->ipha_protocol == IPPROTO_EGP     ? "EGP"    :
 328             I->ipha_protocol == IPPROTO_IPV6    ? "IPv6"   :
 329             I->ipha_protocol == IPPROTO_ROUTING ? "ROUTE"  :
 330             I->ipha_protocol == IPPROTO_ESP     ? "ESP"    :
 331             I->ipha_protocol == IPPROTO_AH      ? "AH"     :
 332             I->ipha_protocol == IPPROTO_ICMPV6  ? "ICMPv6" :
 333             I->ipha_protocol == IPPROTO_OSPF    ? "OSPF"   :
 334             I->ipha_protocol == IPPROTO_SCTP    ? "SCTP"   :
 335             I->ipha_protocol == IPPROTO_RAW     ? "RAW"    :
 336             lltostr((uint64_t)I->ipha_protocol);
 337         ipv4_checksum = I != NULL ? ntohs(I->ipha_hdr_checksum) : 0;
 338         ipv4_src = I != NULL ? I->ipha_src : 0;
 339         ipv4_dst = I != NULL ? I->ipha_dst : 0;
 340         ipv4_saddr = I != NULL ? inet_ntoa(&I->ipha_src) : "<null>";
 341         ipv4_daddr = I != NULL ? inet_ntoa(&I->ipha_dst) : "<null>";
 342         ipv4_hdr = I;
 343 };
 344 
 345 #pragma D binding "1.5" translator
 346 translator ipv6info_t < ip6_t *I > {
 347         ipv6_ver = I != NULL ? I->ip6_ctlun.ip6_un2_vfc >> 4 : 0;
 348         ipv6_tclass = I != NULL ? ((I->ip6_ctlun.ip6_un1.ip6_un1_flow &&
 349             0x0fffffff) >> 20) : 0;
 350         ipv6_flow = I != NULL ? I->ip6_ctlun.ip6_un1.ip6_un1_flow &&
 351             0x000fffff : 0;
 352         ipv6_plen = I != NULL ? ntohs(I->ip6_ctlun.ip6_un1.ip6_un1_plen) : 0;
 353         ipv6_nexthdr = I != NULL ? I->ip6_ctlun.ip6_un1.ip6_un1_nxt : 0;
 354         ipv6_nextstr = I == NULL ? "<null>" :
 355             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_TCP     ? "TCP"    :
 356             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_UDP     ? "UDP"    :
 357             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_IP      ? "IP"     :
 358             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_ICMP    ? "ICMP"   :
 359             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_IGMP    ? "IGMP"   :
 360             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_EGP     ? "EGP"    :
 361             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_IPV6    ? "IPv6"   :
 362             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_ROUTING ? "ROUTE"  :
 363             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_ESP     ? "ESP"    :
 364             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_AH      ? "AH"     :
 365             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_ICMPV6  ? "ICMPv6" :
 366             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_OSPF    ? "OSPF"   :
 367             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_SCTP    ? "SCTP"   :
 368             I->ip6_ctlun.ip6_un1.ip6_un1_nxt == IPPROTO_RAW     ? "RAW"    :
 369             lltostr((uint64_t)I->ip6_ctlun.ip6_un1.ip6_un1_nxt);
 370         ipv6_hlim = I != NULL ? I->ip6_ctlun.ip6_un1.ip6_un1_hlim : 0;
 371         ipv6_src = I != NULL ? &I->ip6_src : 0;
 372         ipv6_dst = I != NULL ? &I->ip6_dst : 0;
 373         ipv6_saddr = I != NULL ? inet_ntoa6(&I->ip6_src) : "<null>";
 374         ipv6_daddr = I != NULL ? inet_ntoa6(&I->ip6_dst) : "<null>";
 375         ipv6_hdr = I;
 376 };