summaryrefslogtreecommitdiff
path: root/src/libtnccs/plugins/tnccs_20/messages/ietf/pb_error_msg.c
blob: 05621b7cb0ffa3383a9bc4e33a1edb203edd5602 (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
/*
 * Copyright (C) 2010 Sansar Choinyambuu
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pb_error_msg.h"

#include <tnc/tnccs/tnccs.h>

#include <bio/bio_writer.h>
#include <bio/bio_reader.h>
#include <pen/pen.h>
#include <utils/debug.h>

ENUM(pb_tnc_error_code_names, PB_ERROR_UNEXPECTED_BATCH_TYPE,
							  PB_ERROR_VERSION_NOT_SUPPORTED,
	"Unexpected Batch Type",
	"Invalid Parameter",
	"Local Error",
	"Unsupported Mandatory Message",
	"Version Not Supported"
);

typedef struct private_pb_error_msg_t private_pb_error_msg_t;

/**
 *   PB-Error message (see section 4.9 of RFC 5793)
 *
 *      0                   1                   2                   3
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |    Flags      |              Error Code Vendor ID             |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |           Error Code          |           Reserved            |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *     |                Error Parameters (Variable Length)             |
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

#define ERROR_FLAG_NONE		0x00
#define ERROR_FLAG_FATAL	(1<<7)
#define ERROR_RESERVED	 	0x0000
#define ERROR_HEADER_SIZE	8

/**
 * Private data of a pb_error_msg_t object.
 *
 */
struct private_pb_error_msg_t {
	/**
	 * Public pb_error_msg_t interface.
	 */
	pb_error_msg_t public;

	/**
	 * PB-TNC message type
	 */
	pen_type_t type;

	/**
	 * Fatal flag
	 */
	bool fatal;

	/**
	 * PB Error Code Vendor ID
	 */
	uint32_t vendor_id;

	/**
	 * PB Error Code
	 */
	uint16_t error_code;

	/**
	 * PB Error Offset
	 */
	uint32_t error_offset;

	/**
	 * Bad PB-TNC version received
	 */
	uint8_t bad_version;

	/**
	 * Encoded message
	 */
	chunk_t encoding;

	/**
	 * reference count
	 */
	refcount_t ref;
};

METHOD(pb_tnc_msg_t, get_type, pen_type_t,
	private_pb_error_msg_t *this)
{
	return this->type;
}

METHOD(pb_tnc_msg_t, get_encoding, chunk_t,
	private_pb_error_msg_t *this)
{
	return this->encoding;
}

METHOD(pb_tnc_msg_t, build, void,
	private_pb_error_msg_t *this)
{
	bio_writer_t *writer;

	if (this->encoding.ptr)
	{
		return;
	}

	/* build message header */
	writer = bio_writer_create(ERROR_HEADER_SIZE);
	writer->write_uint8 (writer, this->fatal ?
						 ERROR_FLAG_FATAL : ERROR_FLAG_NONE);
	writer->write_uint24(writer, this->vendor_id);
	writer->write_uint16(writer, this->error_code);
	writer->write_uint16(writer, ERROR_RESERVED);

	/* build message body */
	if (this->error_code == PB_ERROR_VERSION_NOT_SUPPORTED)
	{
		/* Bad version */
		writer->write_uint8(writer, this->bad_version);
		writer->write_uint8(writer, PB_TNC_VERSION); /* Max version */
		writer->write_uint8(writer, PB_TNC_VERSION); /* Min version */
		writer->write_uint8(writer, 0x00);           /* Reserved */
	}
	else
	{
		/* Error Offset */
		writer->write_uint32(writer, this->error_offset);
	}
	this->encoding = writer->get_buf(writer);
	this->encoding = chunk_clone(this->encoding);
	writer->destroy(writer);
}

METHOD(pb_tnc_msg_t, process, status_t,
	private_pb_error_msg_t *this, uint32_t *offset)
{
	uint8_t flags, max_version, min_version;
	uint16_t reserved;
	bio_reader_t *reader;

	if (this->encoding.len < ERROR_HEADER_SIZE)
	{
		DBG1(DBG_TNC,"%N message is shorter than header size of %u bytes",
			 pb_tnc_msg_type_names, PB_MSG_ERROR, ERROR_HEADER_SIZE);
		*offset = 0;
		return FAILED;
	}

	/* process message header */
	reader = bio_reader_create(this->encoding);
	reader->read_uint8 (reader, &flags);
	reader->read_uint24(reader, &this->vendor_id);
	reader->read_uint16(reader, &this->error_code);
	reader->read_uint16(reader, &reserved);
	this->fatal = (flags & ERROR_FLAG_FATAL) != ERROR_FLAG_NONE;

	if (this->vendor_id == PEN_IETF && reader->remaining(reader) == 4)
	{
		if (this->error_code == PB_ERROR_VERSION_NOT_SUPPORTED)
		{
			reader->read_uint8(reader, &this->bad_version);
			reader->read_uint8(reader, &max_version);
			reader->read_uint8(reader, &min_version);
		}
		else
		{
			reader->read_uint32(reader, &this->error_offset);
		}
	}
	reader->destroy(reader);

	return SUCCESS;
}

METHOD(pb_tnc_msg_t, get_ref, pb_tnc_msg_t*,
	private_pb_error_msg_t *this)
{
	ref_get(&this->ref);
	return &this->public.pb_interface;
}

METHOD(pb_tnc_msg_t, destroy, void,
	private_pb_error_msg_t *this)
{
	if (ref_put(&this->ref))
	{
		free(this->encoding.ptr);
		free(this);
	}
}

METHOD(pb_error_msg_t, get_fatal_flag, bool,
	private_pb_error_msg_t *this)
{
	return this->fatal;
}

METHOD(pb_error_msg_t, get_vendor_id, uint32_t,
	private_pb_error_msg_t *this)
{
	return this->vendor_id;
}

METHOD(pb_error_msg_t, get_error_code, uint16_t,
	private_pb_error_msg_t *this)
{
	return this->error_code;
}

METHOD(pb_error_msg_t, get_offset, uint32_t,
	private_pb_error_msg_t *this)
{
	return this->error_offset;
}

METHOD(pb_error_msg_t, get_bad_version, uint8_t,
	private_pb_error_msg_t *this)
{
	return this->bad_version;
}

METHOD(pb_error_msg_t, set_bad_version, void,
	private_pb_error_msg_t *this, uint8_t version)
{
	this->bad_version = version;
}

/**
 * See header
 */
pb_tnc_msg_t* pb_error_msg_create(bool fatal, uint32_t vendor_id,
								  pb_tnc_error_code_t error_code)
{
	private_pb_error_msg_t *this;

	INIT(this,
		.public = {
			.pb_interface = {
				.get_type = _get_type,
				.get_encoding = _get_encoding,
				.build = _build,
				.process = _process,
				.get_ref = _get_ref,
				.destroy = _destroy,
			},
			.get_fatal_flag = _get_fatal_flag,
			.get_vendor_id = _get_vendor_id,
			.get_error_code = _get_error_code,
			.get_offset = _get_offset,
			.get_bad_version = _get_bad_version,
			.set_bad_version = _set_bad_version,
		},
		.type = { PEN_IETF, PB_MSG_ERROR },
		.ref = 1,
		.fatal = fatal,
		.vendor_id = vendor_id,
		.error_code = error_code,
	);

	return &this->public.pb_interface;
}

/**
 * See header
 */
pb_tnc_msg_t* pb_error_msg_create_with_offset(bool fatal, uint32_t vendor_id,
											  pb_tnc_error_code_t error_code,
											  uint32_t error_offset)
{
	private_pb_error_msg_t *this;

	INIT(this,
		.public = {
			.pb_interface = {
				.get_type = _get_type,
				.get_encoding = _get_encoding,
				.build = _build,
				.process = _process,
				.get_ref = _get_ref,
				.destroy = _destroy,
			},
			.get_fatal_flag = _get_fatal_flag,
			.get_vendor_id = _get_vendor_id,
			.get_error_code = _get_error_code,
			.get_offset = _get_offset,
			.get_bad_version = _get_bad_version,
			.set_bad_version = _set_bad_version,
		},
		.type = { PEN_IETF, PB_MSG_ERROR },
		.ref = 1,
		.fatal = fatal,
		.vendor_id = vendor_id,
		.error_code = error_code,
		.error_offset = error_offset,
	);

	return &this->public.pb_interface;
}

/**
 * See header
 */
pb_tnc_msg_t *pb_error_msg_create_from_data(chunk_t data)
{
	private_pb_error_msg_t *this;

	INIT(this,
		.public = {
			.pb_interface = {
				.get_type = _get_type,
				.get_encoding = _get_encoding,
				.build = _build,
				.process = _process,
				.get_ref = _get_ref,
				.destroy = _destroy,
			},
			.get_fatal_flag = _get_fatal_flag,
			.get_vendor_id = _get_vendor_id,
			.get_error_code = _get_error_code,
			.get_offset = _get_offset,
			.get_bad_version = _get_bad_version,
			.set_bad_version = _set_bad_version,
		},
		.type = { PEN_IETF, PB_MSG_ERROR },
		.ref = 1,
		.encoding = chunk_clone(data),
	);

	return &this->public.pb_interface;
}