summaryrefslogtreecommitdiff
path: root/usr/src/cmd/lms/LMEConnectionCompat.cpp
blob: 6a91067872517d1cd07e241c42a993f4d61b96bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*******************************************************************************
 * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of Intel Corp. nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cerrno>
#include "types.h"
#include "LMEConnection.h"
#include "LMS_if_compat.h"
#include "Lock.h"
#include "glue.h"

#ifdef _LINUX
#define _strnicmp strncasecmp
#endif

extern glue plugin;

const GUID LMEConnection::_guidCompat = {0x3d98d9b7, 0x1ce8, 0x4252, {0xb3, 0x37, 0x2e, 0xff, 0x10, 0x6e, 0xf2, 0x9f}};

int LMEConnection::CompatSendMessage(UINT8 connID, UINT32 len, unsigned char *buffer)
{
	if (!IsInitialized()) {
		PRINT("[Compat]State: not connected to HECI.\n");
		return -1;
	}

	unsigned char sendBuf[1024 + sizeof(LMS_SEND_DATA_MESSAGE)];
	LMS_SEND_DATA_MESSAGE *msg;

	if (len > 1024) {
		return -1;
	}

	msg = (LMS_SEND_DATA_MESSAGE *)sendBuf;
	msg->MessageType = LMS_MESSAGE_TYPE_SEND_DATA;
	msg->ConnectionId = connID;
	msg->DataLength = htons(len);
	memcpy(msg->Data, buffer, len);

	return _sendMessage(sendBuf, sizeof(LMS_SEND_DATA_MESSAGE) + len);
}

void LMEConnection::CompatCloseConnection(int connID, int status)
{
	if (!IsInitialized()) {
		PRINT("[Compat]State: not connected to HECI.\n");
		return;
	}

	LMS_CLOSE_CONNECTION_MESSAGE msg;

	msg.MessageType = LMS_MESSAGE_TYPE_CLOSE_CONNECTION;
	msg.ConnectionId = connID;
	msg.ClosingReason = status;

	_sendMessage((unsigned char *)&msg, sizeof(msg));
}

bool LMEConnection::CompatProtocolVersion()
{
	if (!IsInitialized()) {
		PRINT("[Compat]State: not connected to HECI.\n");
		return false;
	}

	LMS_PROTO_VERSION_MESSAGE msg;

	memset(&msg, 0, sizeof(msg));
	msg.MessageType = LMS_MESSAGE_TYPE_PROTO_VERSION;
	msg.ConnectionId = 0;
	msg.Protocol = 0;

	PRINT("[Compat]Sending Protocol Version to LME\n");
	int bytesWritten = _sendMessage((unsigned char *)&msg, sizeof(msg));
	return (bytesWritten == sizeof(msg));
}

bool LMEConnection::CompatRequestIPFQDN()
{
	if (!IsInitialized()) {
		PRINT("[Compat]State: not connected to HECI.\n");
		return false;
	}

	LMS_IP_FQDN_REQUEST_MESSAGE msg;

	memset(&msg, 0, sizeof(msg));
	msg.MessageType = LMS_MESSAGE_TYPE_IP_FQDN_REQUEST;
	msg.ConnectionId = 0;

	PRINT("[Compat]Sending IP_FQDN request to LME\n");
	int bytesWritten = _sendMessage((unsigned char *)&msg, sizeof(msg));
	return (bytesWritten == sizeof(msg));
}

bool LMEConnection::CompatOpenConnection(in_port_t mePort, ATAddress addr, unsigned int &connID)
{
	if (!IsInitialized()) {
		PRINT("[Compat]State: not connected to HECI.\n");
		return false;
	}

	unsigned char currReqID = _reqID++;
	bool ret = false;
	LMS_OPEN_CONNECTION_EX_MESSAGE openConnectionExMsg;
	LMS_OPEN_CONNECTION_MESSAGE openConnectionMsg;
	unsigned char *msg = NULL;
	int msgLen = 0;
	size_t addrSize = 0;
	const void *inAddr = addr.inAddr(addrSize);

	if (protocolVer == LMS_PROCOL_VERSION_COMPAT) {
		memset(&openConnectionExMsg, 0, sizeof(openConnectionExMsg));
		openConnectionExMsg.MessageType = LMS_MESSAGE_TYPE_OPEN_CONNECTION_EX;
		openConnectionExMsg.ConnectionId = 0;
		openConnectionExMsg.Protocol = LMS_PROTOCOL_TYPE_TCP_IPV4;
		openConnectionExMsg.Flags = 0;
		openConnectionExMsg.OpenRequestId = currReqID;
		memcpy(openConnectionExMsg.Host, inAddr, addrSize);
		openConnectionExMsg.HostPort = htons(addr.inPort());
		openConnectionExMsg.MEPort = htons(mePort);

		msg = (unsigned char *)&openConnectionExMsg;
		msgLen = sizeof(openConnectionExMsg);
		PRINT("[Compat]OpenConnectionEx %x (%d) p=%d mp=%d\n",
			*(int *)inAddr, addrSize, addr.inPort(), mePort);
	}
	else {
		memset(&openConnectionMsg, 0, sizeof(openConnectionMsg));
		openConnectionMsg.MessageType = LMS_MESSAGE_TYPE_OPEN_CONNECTION;
		openConnectionMsg.ConnectionId = 0;
		openConnectionMsg.Protocol = LMS_PROTOCOL_TYPE_TCP_IPV4;
		openConnectionMsg.OpenRequestId = currReqID;
		memcpy(openConnectionMsg.HostIPAddress, inAddr, addrSize);
		openConnectionMsg.HostPort = htons(addr.inPort());
		openConnectionMsg.MEPort = htons(mePort);

		msg = (unsigned char *)&openConnectionMsg;
		msgLen = sizeof(openConnectionMsg);
		PRINT("[Compat]OpenConnection %x (%d) p=%d mp=%d\n",
			*(int *)inAddr, addrSize, addr.inPort(), mePort);
	}

	// save as pending request
	CompatConnection conn;
	conn.event = new Event();
	conn.status = LMS_CONNECTION_STATUS_FAILED;
	conn.connID = 0;

	_compatMapLock.acquire();
	_compatPendingConnections[currReqID] = conn;
	_compatMapLock.release();

	int bytesWritten;
	bytesWritten = _sendMessage(msg, msgLen);
	if (bytesWritten != msgLen) {
		goto out;
	}

	if (conn.event->wait(10000) == false) {
		// no response from FW
		goto out;
	}

	ret = true;

out:
	{
		Lock ml(_compatMapLock);

		if (_compatPendingConnections[currReqID].status != LMS_CONNECTION_STATUS_OK) {
			ret = false;
		} else {
			connID = _compatPendingConnections[currReqID].connID;
		}
		_compatPendingConnections.erase(currReqID);
	}

	delete conn.event;
	conn.event = NULL;

	return ret;
}

void LMEConnection::_doRXCompat()
{
	unsigned int bytesRead;
	int status = 1;

	_threadStartedEvent.set();

	unsigned char *rxBuffer = new unsigned char[_heciCompat.GetBufferSize()];

	while (true) {
		bytesRead = (unsigned int)_receiveMessage(rxBuffer, _heciCompat.GetBufferSize());

		if ((int)bytesRead < 0) {
			PRINT("[Compat]Error receiving data from HECI\n");
			Deinit();
			break;
		}

		if (bytesRead == 0) {
			// ERROR
			continue;
		}

		PRINT("[Compat]Received from LME %d bytes (msg type %02d)\n", bytesRead, rxBuffer[0]);

		if (bytesRead < 2) {
			// ERROR
			continue;
		}

		if (plugin.preprocess(rxBuffer, bytesRead) == LMS_DROPPED) {
			continue;
		}

		switch (rxBuffer[0]) {
		case LMS_MESSAGE_TYPE_PROTO_VERSION_REPLY:
			CompatRequestIPFQDN();
			break;

		case LMS_MESSAGE_TYPE_CLOSE_CONNECTION: 
		case LMS_MESSAGE_TYPE_SEND_DATA:
		case LMS_MESSAGE_TYPE_IP_FQDN:
			_cb(_cbParam, rxBuffer, bytesRead, &status);
			break;

		case LMS_MESSAGE_TYPE_OPEN_CONNECTION_REPLY:
			{
				LMS_OPEN_CONNECTION_REPLY_MESSAGE *repMsg =
				    (LMS_OPEN_CONNECTION_REPLY_MESSAGE *)rxBuffer;

				Lock ml(_compatMapLock);

				CompatConnMap::iterator itr;
				itr = _compatPendingConnections.find(repMsg->OpenRequestId);
				if (itr != _compatPendingConnections.end()) {
					(*itr).second.connID = repMsg->ConnectionId;
					(*itr).second.status = repMsg->Status;
					(*itr).second.event->set();
					PRINT("[Compat]Open connection reply %d %d =%d\n", repMsg->OpenRequestId, repMsg->ConnectionId, repMsg->Status);
				}
			}
			break;

		case LMS_MESSAGE_TYPE_OPEN_CONNECTION_EX:
			{
				// report incoming connection request
				_cb(_cbParam, rxBuffer, bytesRead, &status);

				if (IsInitialized() && (status == 1)) {
					if (plugin.retry(rxBuffer, bytesRead) == LMS_DROPPED) {
						continue;
					} else {
						_cb(_cbParam, rxBuffer, bytesRead, &status);
					}
				}

				LMS_OPEN_CONNECTION_EX_MESSAGE *msg =
				    (LMS_OPEN_CONNECTION_EX_MESSAGE *)rxBuffer;

				if ((msg->Flags & HOSTNAME_BIT) != 0) {
					PRINT("[Compat]Got client connection request %d for host %s, port %d\n",
						msg->ConnectionId, msg->Host, ntohs(msg->HostPort));
				}
				else {
					PRINT("[Compat]Got client connection request %d for IP %s, port %d\n",
						msg->ConnectionId, inet_ntoa(*((struct in_addr *)msg->Host)), ntohs(msg->HostPort));
				}

				LMS_OPEN_CONNECTION_REPLY_MESSAGE repMsg;
				memset(&repMsg, 0, sizeof(repMsg));

				repMsg.MessageType = LMS_MESSAGE_TYPE_OPEN_CONNECTION_REPLY;
				repMsg.ConnectionId = msg->ConnectionId;
				if (status == 0) {
					repMsg.Status = LMS_CONNECTION_STATUS_OK;
				} else {
					repMsg.Status = LMS_CONNECTION_STATUS_FAILED;
				}

				DWORD bytesWritten;
				bytesWritten = _sendMessage((unsigned char *)&repMsg, sizeof(repMsg));
				if (bytesWritten != sizeof(repMsg)) {
					PRINT("[Compat]Send Open Connection Reply failed: bytesWritten: %lu\n", bytesWritten);
				}
			}
			break;

		default:
			// Uknown request. Ignore
			break;
		}

		if (IsInitialized()) {
			plugin.postprocess(rxBuffer, bytesRead, status);
		}
	}

	if (rxBuffer != NULL) {
		delete[] rxBuffer;
	}
}