1#!/usr/bin/ksh
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 the TCP handshake, the message, then a FIN)
47# 4 x tcp:::send (2 during the TCP handshake, the messages, then a FIN)
48# 3 x ip:::receive (1 during the TCP handshake, the response, then the FIN ACK)
49# 3 x tcp:::receive (1 during the TCP handshake, the response, then the FIN ACK)
50#
51# For this test to work, we are assuming that the TCP handshake and
52# TCP close will enter the IP code path and not use tcp fusion.
53#
54
55if (( $# != 1 )); then
56	print -u2 "expected one argument: <dtrace-path>"
57	exit 2
58fi
59
60dtrace=$1
61getaddr=./get.ipv4remote.pl
62tcpport=22
63DIR=/var/tmp/dtest.$$
64
65if [[ ! -x $getaddr ]]; then
66	print -u2 "could not find or execute sub program: $getaddr"
67	exit 3
68fi
69$getaddr $tcpport | read source dest
70if (( $? != 0 )); then
71	exit 4
72fi
73
74mkdir $DIR
75cd $DIR
76
77cat > test.pl <<-EOPERL
78	use IO::Socket;
79	my \$s = IO::Socket::INET->new(
80	    Proto => "tcp",
81	    PeerAddr => "$dest",
82	    PeerPort => $tcpport,
83	    Timeout => 3);
84	die "Could not connect to host $dest port $tcpport" unless \$s;
85	print \$s "testing state machine transitions";
86	close \$s;
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 >= 3 ? "yes" : "no");
148	printf("tcp:::send - %s\n", tcpsend >= 4 ? "yes" : "no");
149	printf("tcp:::receive - %s\n", tcpreceive >= 3 ? "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/usr/bin/rm -rf $DIR
171
172exit $status
173