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;
87	sleep(2);
88EOPERL
89
90$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
91BEGIN
92{
93	ipsend = tcpsend = ipreceive = tcpreceive = 0;
94	connreq = connest = 0;
95}
96
97ip:::send
98/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
99    args[4]->ipv4_protocol == IPPROTO_TCP/
100{
101	ipsend++;
102}
103
104tcp:::send
105/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
106    args[4]->tcp_dport == $tcpport/
107{
108	tcpsend++;
109}
110
111ip:::receive
112/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
113    args[4]->ipv4_protocol == IPPROTO_TCP/
114{
115	ipreceive++;
116}
117
118tcp:::receive
119/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
120    args[4]->tcp_sport == $tcpport/
121{
122	tcpreceive++;
123}
124
125tcp:::state-change
126{
127	state_event[args[3]->tcps_state]++;
128}
129
130tcp:::connect-request
131/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
132 args[4]->tcp_dport == $tcpport/
133{
134	connreq++;
135}
136
137tcp:::connect-established
138/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
139 args[4]->tcp_sport == $tcpport/
140{
141	connest++;
142}
143
144END
145{
146	printf("Minimum TCP events seen\n\n");
147	printf("ip:::send - %s\n", ipsend >= 4 ? "yes" : "no");
148	printf("ip:::receive - %s\n", ipreceive >= 3 ? "yes" : "no");
149	printf("tcp:::send - %s\n", tcpsend >= 4 ? "yes" : "no");
150	printf("tcp:::receive - %s\n", tcpreceive >= 3 ? "yes" : "no");
151	printf("tcp:::state-change to syn-sent - %s\n",
152	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
153	printf("tcp:::state-change to established - %s\n",
154	    state_event[TCP_STATE_ESTABLISHED] >= 1 ? "yes" : "no");
155	printf("tcp:::state-change to fin-wait-1 - %s\n",
156	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
157	printf("tcp:::state-change to fin-wait-2 - %s\n",
158	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
159	printf("tcp:::state-change to time-wait - %s\n",
160	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
161	printf("tcp:::connect-request - %s\n",
162	    connreq >=1 ? "yes" : "no");
163	printf("tcp:::connect-established - %s\n",
164	    connest >=1 ? "yes" : "no");
165}
166EODTRACE
167
168status=$?
169
170cd /
171/bin/rm -rf $DIR
172
173exit $status
174