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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #pragma D depends_on module unix
  27 #pragma D depends_on provider tcp
  28 
  29 inline int TH_FIN = @TH_FIN@;
  30 #pragma D binding "1.0" TH_FIN
  31 inline int TH_SYN = @TH_SYN@;
  32 #pragma D binding "1.0" TH_SYN
  33 inline int TH_RST = @TH_RST@;
  34 #pragma D binding "1.0" TH_RST
  35 inline int TH_PUSH = @TH_PUSH@;
  36 #pragma D binding "1.0" TH_PUSH
  37 inline int TH_ACK = @TH_ACK@;
  38 #pragma D binding "1.0" TH_ACK
  39 inline int TH_URG = @TH_URG@;
  40 #pragma D binding "1.0" TH_URG
  41 inline int TH_ECE = @TH_ECE@;
  42 #pragma D binding "1.0" TH_ECE
  43 inline int TH_CWR = @TH_CWR@;
  44 #pragma D binding "1.0" TH_CWR
  45 
  46 /*
  47  * tcpinfo is the TCP header fields.
  48  */
  49 typedef struct tcpinfo {
  50         uint16_t tcp_sport;             /* source port */
  51         uint16_t tcp_dport;             /* destination port */
  52         uint32_t tcp_seq;               /* sequence number */
  53         uint32_t tcp_ack;               /* acknowledgment number */
  54         uint8_t tcp_offset;             /* data offset, in bytes */
  55         uint8_t tcp_flags;              /* flags */
  56         uint16_t tcp_window;            /* window size */
  57         uint16_t tcp_checksum;          /* checksum */
  58         uint16_t tcp_urgent;            /* urgent data pointer */
  59         tcph_t *tcp_hdr;                /* raw TCP header */
  60 } tcpinfo_t;
  61 
  62 /*
  63  * tcpsinfo contains stable TCP details from tcp_t.
  64  */
  65 typedef struct tcpsinfo {
  66         int tcps_local;                 /* is delivered locally, boolean */
  67         int tcps_active;                /* active open (from here), boolean */
  68         string tcps_state;              /* TCP state, as a string */
  69 } tcpsinfo_t;
  70 
  71 /*
  72  * tcpnsinfo provides the new tcp state for state changes.
  73  */
  74 typedef struct tcpnsinfo {
  75         string tcps_state;              /* TCP state, as a string */
  76 } tcpnsinfo_t;
  77 
  78 /*
  79  * tcpfinfo contains additional TCP details from tcp_t, that are stable
  80  * for local (tcp-fusion) connections.
  81  */
  82 typedef struct tcpfinfo {
  83         uint16_t tcpf_sport;            /* source port */
  84         uint16_t tcpf_dport;            /* destination port */
  85 } tcpfinfo_t;
  86 
  87 #pragma D binding "1.0" translator
  88 translator tcpinfo_t < tcph_t *T > {
  89         tcp_sport = ntohs(*(uint16_t *)T->th_lport);
  90         tcp_dport = ntohs(*(uint16_t *)T->th_fport);
  91         tcp_seq = ntohl(*(uint32_t *)T->th_seq);
  92         tcp_ack = ntohl(*(uint32_t *)T->th_ack);
  93         tcp_offset = (*(uint8_t *)T->th_offset_and_rsrvd & 0xf0) >> 2;
  94         tcp_flags = *(uint8_t *)T->th_flags;
  95         tcp_window = ntohs(*(uint16_t *)T->th_win);
  96         tcp_checksum = ntohs(*(uint16_t *)T->th_sum);
  97         tcp_urgent = ntohs(*(uint16_t *)T->th_urp);
  98         tcp_hdr = T;
  99 };
 100 
 101 #pragma D binding "1.0" translator
 102 translator tcpsinfo_t < tcp_t *T > {
 103         tcps_local = T ? T->tcp_loopback : 0;
 104         tcps_active = T ? T->tcp_active_open : 0;
 105         tcps_state = T ?
 106             T->tcp_state == @TCPS_CLOSED@ ? "state-closed" :
 107             T->tcp_state == @TCPS_IDLE@ ? "state-idle" :
 108             T->tcp_state == @TCPS_BOUND@ ? "state-bound" :
 109             T->tcp_state == @TCPS_LISTEN@ ? "state-listen" :
 110             T->tcp_state == @TCPS_SYN_SENT@ ? "state-syn-sent" :
 111             T->tcp_state == @TCPS_SYN_RCVD@ ? "state-syn-received" :
 112             T->tcp_state == @TCPS_ESTABLISHED@ ? "state-established" :
 113             T->tcp_state == @TCPS_CLOSE_WAIT@ ? "state-close-wait" :
 114             T->tcp_state == @TCPS_FIN_WAIT_1@ ? "state-fin-wait1" :
 115             T->tcp_state == @TCPS_CLOSING@ ? "state-closing" :
 116             T->tcp_state == @TCPS_LAST_ACK@ ? "state-last-ack" :
 117             T->tcp_state == @TCPS_FIN_WAIT_2@ ? "state-fin-wait2" :
 118             T->tcp_state == @TCPS_TIME_WAIT@ ? "state-time-wait" :
 119             "<unknown>" : "unknown";
 120 };
 121 
 122 #pragma D binding "1.0" translator
 123 translator tcpnsinfo_t < int32_t I > {
 124         tcps_state =
 125             I == @TCPS_CLOSED@ ? "state-closed" :
 126             I == @TCPS_IDLE@ ? "state-idle" :
 127             I == @TCPS_BOUND@ ? "state-bound" :
 128             I == @TCPS_LISTEN@ ? "state-listen" :
 129             I == @TCPS_SYN_SENT@ ? "state-syn-sent" :
 130             I == @TCPS_SYN_RCVD@ ? "state-syn-received" :
 131             I == @TCPS_ESTABLISHED@ ? "state-established" :
 132             I == @TCPS_CLOSE_WAIT@ ? "state-close-wait" :
 133             I == @TCPS_FIN_WAIT_1@ ? "state-fin-wait1" :
 134             I == @TCPS_CLOSING@ ? "state-closing" :
 135             I == @TCPS_LAST_ACK@ ? "state-last-ack" :
 136             I == @TCPS_FIN_WAIT_2@ ? "state-fin-wait2" :
 137             I == @TCPS_TIME_WAIT@ ? "state-time-wait" :
 138             "<unknown>";
 139 };
 140 
 141 #pragma D binding "1.0" translator
 142 translator tcpfinfo_t < tcp_t *T > {
 143         tcpf_sport = probename == "send" ?
 144             ntohs(T->tcp_connp->u_port.tcpu_ports.tcpu_lport) :
 145             ntohs(T->tcp_connp->u_port.tcpu_ports.tcpu_fport);
 146         tcpf_dport = probename == "send" ?
 147             ntohs(T->tcp_connp->u_port.tcpu_ports.tcpu_fport) :
 148             ntohs(T->tcp_connp->u_port.tcpu_ports.tcpu_lport);
 149 };