1da6c28aamw/*
2da6c28aamw * CDDL HEADER START
3da6c28aamw *
4da6c28aamw * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7da6c28aamw *
8da6c28aamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aamw * or http://www.opensolaris.org/os/licensing.
10da6c28aamw * See the License for the specific language governing permissions
11da6c28aamw * and limitations under the License.
12da6c28aamw *
13da6c28aamw * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aamw * If applicable, add the following below this CDDL HEADER, with the
16da6c28aamw * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aamw * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aamw *
19da6c28aamw * CDDL HEADER END
20da6c28aamw */
21da6c28aamw/*
229fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23da6c28aamw * Use is subject to license terms.
24a90cf9fGordon Ross *
2593bc28dGordon Ross * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26da6c28aamw */
27da6c28aamw
28bbf6f00Jordan Brown#include <smbsrv/smb_kproto.h>
29da6c28aamw
30da6c28aamw/*
31da6c28aamw * The echo request is used to test the connection to the server,
32da6c28aamw * and to see if the server is still responding.  The tid is ignored,
33da6c28aamw * so this request may be sent to the server even if there are no
34da6c28aamw * tree connections to the server.
35da6c28aamw *
36da6c28aamw * Each response echoes the data sent, though ByteCount may indicate
37da6c28aamw * no data. If echo-count is zero, no response is sent.
38da6c28aamw */
397b59d02jbsmb_sdrc_t
40faa1795jbsmb_pre_echo(smb_request_t *sr)
41faa1795jb{
4293bc28dGordon Ross	DTRACE_SMB_START(op__Echo, smb_request_t *, sr);
43faa1795jb	return (SDRC_SUCCESS);
44faa1795jb}
45faa1795jb
46faa1795jbvoid
47faa1795jbsmb_post_echo(smb_request_t *sr)
48faa1795jb{
4993bc28dGordon Ross	DTRACE_SMB_DONE(op__Echo, smb_request_t *, sr);
50faa1795jb}
51faa1795jb
527f3ef64Gordon Rossstatic unsigned short smb_max_echo = 10;
537f3ef64Gordon Ross
54faa1795jbsmb_sdrc_t
55da6c28aamwsmb_com_echo(struct smb_request *sr)
56da6c28aamw{
57da6c28aamw	unsigned short necho;
58da6c28aamw	unsigned short nbytes;
59da6c28aamw	unsigned short i;
60da6c28aamw	struct mbuf_chain reply;
61da6c28aamw	char *data;
62a90cf9fGordon Ross	uint16_t	pid_hi, pid_lo;
63a90cf9fGordon Ross
64a90cf9fGordon Ross	pid_hi = sr->smb_pid >> 16;
65a90cf9fGordon Ross	pid_lo = (uint16_t)sr->smb_pid;
66da6c28aamw
677b59d02jb	if (smbsr_decode_vwv(sr, "w", &necho) != 0)
68faa1795jb		return (SDRC_ERROR);
69da6c28aamw
707f3ef64Gordon Ross	/*
717f3ef64Gordon Ross	 * Don't let the client fool us into doing
727f3ef64Gordon Ross	 * more work than is "reasonable".
737f3ef64Gordon Ross	 */
747f3ef64Gordon Ross	if (necho > smb_max_echo)
757f3ef64Gordon Ross		necho = smb_max_echo;
767f3ef64Gordon Ross
77da6c28aamw	nbytes = sr->smb_bcc;
789fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	data = smb_srm_zalloc(sr, nbytes);
79da6c28aamw
803db3f65amw	if (smb_mbc_decodef(&sr->smb_data, "#c", nbytes, data))
81faa1795jb		return (SDRC_ERROR);
82da6c28aamw
83da6c28aamw	for (i = 1; i <= necho; ++i) {
847f3ef64Gordon Ross
857f3ef64Gordon Ross		/*
867f3ef64Gordon Ross		 * According to [MS-CIFS] 3.3.5.32 echo is
877f3ef64Gordon Ross		 * subject to cancellation.
887f3ef64Gordon Ross		 */
897f3ef64Gordon Ross		if (sr->sr_state != SMB_REQ_STATE_ACTIVE)
907f3ef64Gordon Ross			break;
917f3ef64Gordon Ross
92da6c28aamw		MBC_INIT(&reply, SMB_HEADER_ED_LEN + 10 + nbytes);
93da6c28aamw
943db3f65amw		(void) smb_mbc_encodef(&reply, SMB_HEADER_ED_FMT,
95da6c28aamw		    sr->first_smb_com,
96da6c28aamw		    sr->smb_rcls,
97da6c28aamw		    sr->smb_reh,
98da6c28aamw		    sr->smb_err,
99da6c28aamw		    sr->smb_flg | SMB_FLAGS_REPLY,
100da6c28aamw		    sr->smb_flg2,
101a90cf9fGordon Ross		    pid_hi,
102da6c28aamw		    sr->smb_sig,
103da6c28aamw		    sr->smb_tid,
104a90cf9fGordon Ross		    pid_lo,
105da6c28aamw		    sr->smb_uid,
106da6c28aamw		    sr->smb_mid);
107da6c28aamw
1083db3f65amw		(void) smb_mbc_encodef(&reply, "bww#c", 1, i,
109da6c28aamw		    nbytes, nbytes, data);
110da6c28aamw
111da6c28aamw		if (sr->session->signing.flags & SMB_SIGNING_ENABLED)
112da6c28aamw			smb_sign_reply(sr, &reply);
113da6c28aamw
114da6c28aamw		(void) smb_session_send(sr->session, 0, &reply);
1157f3ef64Gordon Ross
1167f3ef64Gordon Ross		delay(MSEC_TO_TICK(100));
117da6c28aamw	}
118da6c28aamw
119da6c28aamw	return (SDRC_NO_REPLY);
120da6c28aamw}
121