1#!/usr/bin/env ksh93
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27#
28# Test tcp:::state-change and tcp:::{send,receive} by connecting to
29# the remote ssh service and sending a test message. This should result
30# in a "Protocol mismatch" response and a close of the connection.
31# A number of state transition events along with tcp send and receive
32# events for the message should result.
33#
34# This may fail due to:
35#
36# 1. A change to the ip stack breaking expected probe behavior,
37#    which is the reason we are testing.
38# 2. The lo0 interface missing or not up.
39# 3. The remote ssh service is not online.
40# 4. An unlikely race causes the unlocked global send/receive
41#    variables to be corrupted.
42#
43# This test performs a TCP connection to the ssh service (port 22) and
44# checks that at least the following packet counts were traced:
45#
46# 4 x ip:::send (2 during connection setup, 2 during connection teardown)
47# 4 x tcp:::send (2 during connection setup, 2 during connection teardown)
48# 5 x ip:::receive (1 during connection setup, the response, 1 window update,
49#                   1 banner line, 2 during connection teardown)
50# 5 x tcp:::receive (1 during connection setup, the response, 1 window update,
51#                    1 banner line, 2 during connection teardown)
52#
53
54if (( $# != 1 )); then
55	print -u2 "expected one argument: <dtrace-path>"
56	exit 2
57fi
58
59dtrace=$1
60getaddr=./get.ipv4remote.pl
61tcpport=22
62DIR=/var/tmp/dtest.$$
63
64if [[ ! -x $getaddr ]]; then
65	print -u2 "could not find or execute sub program: $getaddr"
66	exit 3
67fi
68$getaddr $tcpport | read source dest
69if (( $? != 0 )); then
70	exit 4
71fi
72
73mkdir $DIR
74cd $DIR
75
76cat > test.pl <<-EOPERL
77	use IO::Socket;
78	my \$s = IO::Socket::INET->new(
79	    Proto => "tcp",
80	    PeerAddr => "$dest",
81	    PeerPort => $tcpport,
82	    Timeout => 3);
83	die "Could not connect to host $dest port $tcpport" unless \$s;
84	readline \$s;
85	close \$s;
86	sleep(2);
87EOPERL
88
89$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
90BEGIN
91{
92	ipsend = tcpsend = ipreceive = tcpreceive = 0;
93	connreq = connest = 0;
94}
95
96ip:::send
97/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
98    args[4]->ipv4_protocol == IPPROTO_TCP/
99{
100	ipsend++;
101}
102
103tcp:::send
104/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
105    args[4]->tcp_dport == $tcpport/
106{
107	tcpsend++;
108}
109
110ip:::receive
111/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
112    args[4]->ipv4_protocol == IPPROTO_TCP/
113{
114	ipreceive++;
115}
116
117tcp:::receive
118/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
119    args[4]->tcp_sport == $tcpport/
120{
121	tcpreceive++;
122}
123
124tcp:::state-change
125{
126	state_event[args[3]->tcps_state]++;
127}
128
129tcp:::connect-request
130/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
131 args[4]->tcp_dport == $tcpport/
132{
133	connreq++;
134}
135
136tcp:::connect-established
137/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
138 args[4]->tcp_sport == $tcpport/
139{
140	connest++;
141}
142
143END
144{
145	printf("Minimum TCP events seen\n\n");
146	printf("ip:::send - %s\n", ipsend >= 4 ? "yes" : "no");
147	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
148	printf("tcp:::send - %s\n", tcpsend >= 4 ? "yes" : "no");
149	printf("tcp:::receive - %s\n", tcpreceive >= 5 ? "yes" : "no");
150	printf("tcp:::state-change to syn-sent - %s\n",
151	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
152	printf("tcp:::state-change to established - %s\n",
153	    state_event[TCP_STATE_ESTABLISHED] >= 1 ? "yes" : "no");
154	printf("tcp:::state-change to fin-wait-1 - %s\n",
155	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
156	printf("tcp:::state-change to fin-wait-2 - %s\n",
157	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
158	printf("tcp:::state-change to time-wait - %s\n",
159	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
160	printf("tcp:::connect-request - %s\n",
161	    connreq >=1 ? "yes" : "no");
162	printf("tcp:::connect-established - %s\n",
163	    connest >=1 ? "yes" : "no");
164}
165EODTRACE
166
167status=$?
168
169cd /
170/bin/rm -rf $DIR
171
172exit $status
173