Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mcs.c « libfreerdp-core - github.com/FreeRDP/FreeRDP-old.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1a2715607503f86b92811bc1ba3e2251e75a3e0 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
   FreeRDP: A Remote Desktop Protocol client.
   Multipoint Communications Service

   Copyright (C) Matthew Chapman 1999-2008

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "frdp.h"
#include "iso.h"
#include "chan.h"
#include "secure.h"
#include "rdp.h"
#include "asn1.h"
#include "tcp.h"
#include <freerdp/rdpset.h>
#include <freerdp/utils/memory.h>

#include "mcs.h"

/* Output a DOMAIN_PARAMS structure (ASN.1 BER) */
static void
mcs_out_domain_params(STREAM s, int max_channels, int max_users, int max_tokens, int max_pdusize)
{
	ber_out_header(s, MCS_TAG_DOMAIN_PARAMS, 32);
	ber_out_integer(s, max_channels);
	ber_out_integer(s, max_users);
	ber_out_integer(s, max_tokens);
	ber_out_integer(s, 1);	/* num_priorities */
	ber_out_integer(s, 0);	/* min_throughput */
	ber_out_integer(s, 1);	/* max_height */
	ber_out_integer(s, max_pdusize);
	ber_out_integer(s, 2);	/* ver_protocol */
}

/* Parse a DOMAIN_PARAMS structure (ASN.1 BER) */
static RD_BOOL
mcs_parse_domain_params(rdpMcs * mcs, STREAM s)
{
	int length;

	ber_parse_header(mcs, s, MCS_TAG_DOMAIN_PARAMS, &length);
	in_uint8s(s, length);

	return s_check(s);
}

/* Send an MCS_CONNECT_INITIAL message (ASN.1 BER) */
static void
mcs_send_connect_initial(rdpMcs * mcs)
{
	STREAM s;
	int length;
	int gccCCrq_length;
	struct stream gccCCrq;

	gccCCrq.size = 512;
	gccCCrq.p = gccCCrq.data = (uint8 *) xmalloc(gccCCrq.size);
	gccCCrq.end = gccCCrq.data + gccCCrq.size;

	sec_out_gcc_conference_create_request(mcs->sec, &gccCCrq);
	gccCCrq_length = gccCCrq.end - gccCCrq.data;
	length = 9 + 3 * 34 + 4 + gccCCrq_length;

	s = iso_init(mcs->iso, length + 5);

	ber_out_header(s, MCS_CONNECT_INITIAL, length);	/* ConnectMCSPDU connect-initial */
	ber_out_header(s, BER_TAG_OCTET_STRING, 1);	/* callingDomainSelector */
	out_uint8(s, 1);
	ber_out_header(s, BER_TAG_OCTET_STRING, 1);	/* calledDomainSelector */
	out_uint8(s, 1);

	ber_out_header(s, BER_TAG_BOOLEAN, 1);	/* upwardFlag */
	out_uint8(s, 0xFF);

	mcs_out_domain_params(s, 34, 2, 0, 0xFFFF);	/* targetParameters */
	mcs_out_domain_params(s, 1, 1, 1, 0x420);	/* minimumParameters */
	mcs_out_domain_params(s, 0xFFFF, 0xFC17, 0xFFFF, 0xFFFF);	/* maximumParameters */

	ber_out_header(s, BER_TAG_OCTET_STRING, gccCCrq_length);	/* userData */
	out_uint8p(s, gccCCrq.data, gccCCrq_length);
	xfree(gccCCrq.data);

	s_mark_end(s);
	iso_send(mcs->iso, s);
}

/* Expect a MCS_CONNECT_RESPONSE message (ASN.1 BER) */
static RD_BOOL
mcs_recv_connect_response(rdpMcs * mcs)
{
	STREAM s;
	int length;
	uint8 result;

	s = iso_recv(mcs->iso, NULL);

	if (s == NULL)
		return False;

	ber_parse_header(mcs, s, MCS_CONNECT_RESPONSE, &length);

	ber_parse_header(mcs, s, BER_TAG_RESULT, &length);
	in_uint8(s, result);
	if (result != 0)
	{
		ui_error(mcs->sec->rdp->inst, "MCS connect: %d\n", result);
		return False;
	}

	ber_parse_header(mcs, s, BER_TAG_INTEGER, &length);
	in_uint8s(s, length);	/* connect id */
	mcs_parse_domain_params(mcs, s);

	ber_parse_header(mcs, s, BER_TAG_OCTET_STRING, &length);

	sec_process_mcs_data(mcs->sec, s);
	return s_check_end(s);
}

/* Send an EDrq message (ASN.1 PER) */
static void
mcs_send_edrq(rdpMcs * mcs)
{
	STREAM s;

	s = iso_init(mcs->iso, 5);

	out_uint8(s, (T125_DOMAINMCSPDU_ErectDomainRequest << 2));
	out_uint16_le(s, 1);	/* height */
	out_uint16_le(s, 1);	/* interval */

	s_mark_end(s);
	iso_send(mcs->iso, s);
}

/* Send an AUrq message (ASN.1 PER) */
static void
mcs_send_aurq(rdpMcs * mcs)
{
	STREAM s;

	s = iso_init(mcs->iso, 1);

	out_uint8(s, (T125_DOMAINMCSPDU_AttachUserRequest << 2));

	s_mark_end(s);
	iso_send(mcs->iso, s);
}

/* Expect a AUcf message (ASN.1 PER) */
static RD_BOOL
mcs_recv_aucf(rdpMcs * mcs, uint16 * mcs_userid)
{
	uint8 opcode, result;
	STREAM s;

	s = iso_recv(mcs->iso, NULL);
	if (s == NULL)
		return False;

	in_uint8(s, opcode);
	if ((opcode >> 2) != T125_DOMAINMCSPDU_AttachUserConfirm)
	{
		ui_error(mcs->sec->rdp->inst, "expected AUcf, got %d\n", opcode);
		return False;
	}

	in_uint8(s, result);
	if (result != 0)
	{
		ui_error(mcs->sec->rdp->inst, "AUrq: %d\n", result);
		return False;
	}

	if (opcode & 2)
		in_uint16_be(s, *mcs_userid);

	return s_check_end(s);
}

/* Send a CJrq message (ASN.1 PER) */
static void
mcs_send_cjrq(rdpMcs * mcs, uint16 chanid)
{
	STREAM s;

	DEBUG_MCS("Sending CJRQ for channel #%d", chanid);

	s = iso_init(mcs->iso, 5);

	out_uint8(s, (T125_DOMAINMCSPDU_ChannelJoinRequest << 2));
	out_uint16_be(s, mcs->mcs_userid);
	out_uint16_be(s, chanid);

	s_mark_end(s);
	iso_send(mcs->iso, s);
}

/* Expect a CJcf message (ASN.1 PER) */
static RD_BOOL
mcs_recv_cjcf(rdpMcs * mcs)
{
	uint8 opcode, result;
	STREAM s;

	s = iso_recv(mcs->iso, NULL);
	if (s == NULL)
		return False;

	in_uint8(s, opcode);
	if ((opcode >> 2) != T125_DOMAINMCSPDU_ChannelJoinConfirm)
	{
		ui_error(mcs->sec->rdp->inst, "expected CJcf, got %d\n", opcode);
		return False;
	}

	in_uint8(s, result);
	if (result != 0)
	{
		ui_error(mcs->sec->rdp->inst, "CJrq: %d\n", result);
		return False;
	}

	in_uint8s(s, 4);	/* mcs_userid, req_chanid */
	if (opcode & 2)
		in_uint8s(s, 2);	/* join_chanid */

	return s_check_end(s);
}

/* Initialise an MCS transport data packet */
STREAM
mcs_init(rdpMcs * mcs, int length)
{
	STREAM s;

	s = iso_init(mcs->iso, length + 8);
	s_push_layer(s, mcs_hdr, 8);

	return s;
}

/* Initialise a fast path data packet */
STREAM
mcs_fp_init(rdpMcs * mcs, int length)
{
	STREAM s;

	s = iso_fp_init(mcs->iso, length);
	return s;
}

/* Send an MCS transport data packet to a specific channel */
void
mcs_send_to_channel(rdpMcs * mcs, STREAM s, uint16 channel)
{
	uint16 length;

	s_pop_layer(s, mcs_hdr);
	length = s->end - s->p - 8;
	length |= 0x8000;

	out_uint8(s, (T125_DOMAINMCSPDU_SendDataRequest << 2));
	out_uint16_be(s, mcs->mcs_userid);
	out_uint16_be(s, channel);
	out_uint8(s, 0x70);	/* flags */
	out_uint16_be(s, length);

	iso_send(mcs->iso, s);
}

/* Send an MCS transport data packet to the global channel */
void
mcs_send(rdpMcs * mcs, STREAM s)
{
	mcs_send_to_channel(mcs, s, MCS_GLOBAL_CHANNEL);
}

/* Send a fast path data packet to the global channel */
void
mcs_fp_send(rdpMcs * mcs, STREAM s, uint32 flags)
{
	iso_fp_send(mcs->iso, s, flags);
}

/* Receive an MCS transport data packet */
STREAM
mcs_recv(rdpMcs * mcs, isoRecvType * ptype, uint16 * channel)
{
	STREAM s;
	uint8 byte;
	uint8 pduType;
	uint16 length;

	*channel = (uint16)-1;	/* default: bogus value */
	s = iso_recv(mcs->iso, ptype);

	if (s == NULL)
		return NULL;

	if (*ptype == ISO_RECV_X224)
	{
		/* Parse mcsSDin (MCS Send Data Indication PDU, see [T125] section 7, part 7): */

		in_uint8(s, pduType);
		pduType >>= 2;

		if (pduType != T125_DOMAINMCSPDU_SendDataIndication)
		{
			if (pduType != T125_DOMAINMCSPDU_DisconnectProviderUltimatum)
			{
				ui_error(mcs->sec->rdp->inst, "expected data, got %d\n", pduType);
			}
			return NULL;
		}

		in_uint8s(s, 2);		/* initiator */
		in_uint16_be(s, *channel);	/* channelId */
		in_uint8s(s, 1);		/* flags */

		in_uint8(s, byte);
		length = (uint16) byte;

		if (byte & 0x80)
		{
			length &= ~0x80;
			length <<= 8;
			in_uint8(s, byte);
			length += (uint16) byte;
		}
	}

	return s;
}

/* Establish a connection up to the MCS layer */
RD_BOOL
mcs_connect(rdpMcs * mcs)
{
	int i;
	int mcs_id;
	rdpSet * settings;

	mcs_send_connect_initial(mcs);
	if (!mcs_recv_connect_response(mcs))
	{
		ui_error(mcs->sec->rdp->inst, "invalid mcs_recv_connect_response\n");
		goto error;
	}

	mcs_send_edrq(mcs);

	mcs_send_aurq(mcs);
	if (!mcs_recv_aucf(mcs, &(mcs->mcs_userid)))
	{
		ui_error(mcs->sec->rdp->inst, "invalid mcs_recv_aucf\n");
		goto error;
	}

	mcs_send_cjrq(mcs, mcs->mcs_userid + MCS_USERCHANNEL_BASE);

	if (!mcs_recv_cjcf(mcs))
	{
		ui_error(mcs->sec->rdp->inst, "invalid mcs_recv_cjcf\n");
		goto error;
	}

	mcs_send_cjrq(mcs, MCS_GLOBAL_CHANNEL);
	if (!mcs_recv_cjcf(mcs))
	{
		ui_error(mcs->sec->rdp->inst, "invalid mcs_recv_cjcf\n");
		goto error;
	}

	settings = mcs->sec->rdp->settings;
	for (i = 0; i < settings->num_channels; i++)
	{
		mcs_id = settings->channels[i].chan_id;
		if (mcs_id >= mcs->mcs_userid + MCS_USERCHANNEL_BASE)
		{
			ui_warning(mcs->sec->rdp->inst, "channel %d got id %d >= %d\n", i, mcs_id, mcs->mcs_userid + MCS_USERCHANNEL_BASE);
		}
		mcs_send_cjrq(mcs, mcs_id);
		if (!mcs_recv_cjcf(mcs))
		{
			ui_error(mcs->sec->rdp->inst, "channel %d id %d invalid mcs_recv_cjcf\n", i, mcs_id);
			goto error;
		}
	}
	return True;

error:
	iso_disconnect(mcs->iso);
	return False;
}

/* Disconnect from the MCS layer */
void
mcs_disconnect(rdpMcs * mcs)
{
	iso_disconnect(mcs->iso);
}

rdpMcs *
mcs_new(struct rdp_sec * sec)
{
	rdpMcs * self;

	self = (rdpMcs *) xmalloc(sizeof(rdpMcs));
	if (self != NULL)
	{
		memset(self, 0, sizeof(rdpMcs));
		self->sec = sec;
		self->iso = iso_new(self);
		self->chan = vchan_new(self);
	}
	return self;
}

void
mcs_free(rdpMcs * mcs)
{
	if (mcs != NULL)
	{
		vchan_free(mcs->chan);
		iso_free(mcs->iso);
		xfree(mcs);
	}
}