summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-coap.h
blob: b3a62978fcad98ebcf26366577c1f934d60d468c (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
/* packet-coap.h
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef __PACKET_COAP_H__
#define __PACKET_COAP_H__

#include "packet-oscore.h"

/* bitmasks */
#define COAP_VERSION_MASK					0xC0
#define COAP_TYPE_MASK						0x30
#define COAP_TOKEN_LEN_MASK					0x0F
#define COAP_BLOCK_MFLAG_MASK					0x08
#define COAP_BLOCK_SIZE_MASK					0x07
#define COAP_OBJECT_SECURITY_RESERVED_MASK			0xE0
#define COAP_OBJECT_SECURITY_KID_CONTEXT_MASK			0x10
#define COAP_OBJECT_SECURITY_KID_MASK				0x08
#define COAP_OBJECT_SECURITY_PIVLEN_MASK			0x07

/* Parent protocol for CoAP */
typedef enum {
	PARENT_WEBSOCKETS,	/* WebSockets */
	PARENT_TCP_TLS,		/* TCP or TLS */
	PARENT_OTHER		/* UDP, WAP, other packet-based protocols */
} coap_parent_protocol;

/* CoAP Message information */
typedef struct {
	const char *ctype_str;
	unsigned ctype_value;
	unsigned block_option;                     /* Indicates Block1 or Block2 option */
	unsigned block_number;
	unsigned block_mflag;
	wmem_strbuf_t *uri_str_strbuf;		/* the maximum is 1024 > 510 = Uri-Host:255 + Uri-Path:255 x 2 */
	wmem_strbuf_t *uri_query_strbuf;	/* the maximum is 1024 >         765 = Uri-Query:255 x 3 */
	bool is_coap_for_tmf;		/* CoAP for Thread Management Framework */
	bool object_security;
	oscore_info_t *oscore_info;		/* OSCORE data needed to decrypt */
} coap_info;

/* CoAP Conversation information */
typedef struct {
	wmem_map_t *messages;
} coap_conv_info;

/* CoAP Transaction tracking information */
typedef struct {
	wmem_map_t    *req_rsp;
	wmem_strbuf_t *uri_str_strbuf;
	oscore_info_t *oscore_info;		/* OSCORE transaction to decrypt response */
} coap_transaction;

typedef struct {
	uint32_t req_frame;
	uint32_t rsp_frame;
	nstime_t req_time;
} coap_request_response;

/* common header fields, subtrees and expert info for SSL and DTLS dissectors */
typedef struct coap_common_dissect {
	struct {
		/* Header fields */
		int code;
		/* Payload fields */
		int payload;
		int payload_desc;
		int payload_length;

		/* Option fields */
		int opt_name;
		int opt_desc;
		int opt_delta;
		int opt_delta_ext;
		int opt_length;
		int opt_length_ext;
		int opt_end_marker;
		int opt_ctype;
		int opt_max_age;
		int opt_proxy_uri;
		int opt_proxy_scheme;
		int opt_size1;
		int opt_etag;
		int opt_uri_host;
		int opt_location_path;
		int opt_uri_port;
		int opt_location_query;
		int opt_uri_path;
		int opt_uri_path_recon;
		int opt_observe_req;
		int opt_observe_rsp;
		int opt_hop_limit;
		int opt_accept;
		int opt_if_match;
		int opt_block_number;
		int opt_block_mflag;
		int opt_block_size;
		int opt_uri_query;
		int opt_echo;
		int opt_no_response;
		int opt_request_tag;
		int opt_ocf_version;
		int opt_ocf_accept_version;
		int opt_unknown;
		int opt_object_security_reserved;
		int opt_object_security_kid_context_present;
		int opt_object_security_kid_present;
		int opt_object_security_piv_len;
		int opt_object_security_piv;
		int opt_object_security_kid_context_len;
		int opt_object_security_kid_context;
		int opt_object_security_kid;

	/* do not forget to update COAP_COMMON_HF_LIST! */
	} hf;

	struct {
		int payload;
		int option;

	/* do not forget to update COAP_COMMON_ETT_LIST! */
	} ett;

	struct {
		/* Generic expert info for malformed packets. */
		expert_field opt_unknown_number;
		expert_field opt_invalid_number;
		expert_field opt_invalid_range;
		expert_field opt_length_bad;
		expert_field opt_object_security_bad;

	/* do not forget to update COAP_COMMON_EI_LIST! */
	} ei;
} coap_common_dissect_t;

uint8_t dissect_coap_code(tvbuff_t *tvb, proto_tree *coap_tree, int *offset, coap_common_dissect_t *dissect_hf, uint8_t *code_class);
int dissect_coap_options(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, int offset, int offset_end, uint8_t code_class, coap_info *coinfo, coap_common_dissect_t *dissect_hf);
void dissect_coap_payload(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, proto_tree *parent_tree, int offset, int offset_end, uint8_t code_class, coap_info *coinfo, coap_common_dissect_t *dissect_hf, bool oscore);

extern const value_string coap_vals_observe_options[];
extern value_string_ext coap_vals_code_ext;

/* {{{ */
#define COAP_COMMON_LIST_T(name)						\
coap_common_dissect_t name;
/* }}} */

/* {{{ */
#define COAP_COMMON_HF_LIST(name, prefix)					\
	{ & name .hf.code,							\
	  { "Code", prefix ".code",						\
	    FT_UINT8, BASE_DEC | BASE_EXT_STRING, &coap_vals_code_ext, 0x0,	\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.payload,							\
	  { "Payload",  prefix ".payload",					\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.payload_desc,						\
	  { "Payload Desc",  prefix ".payload_desc",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.payload_length,						\
	  { "Payload Length",  prefix ".payload_length",			\
	    FT_UINT32, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_name,							\
	  { "Opt Name",  prefix ".opt.name",					\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_desc,							\
	  { "Opt Desc",  prefix ".opt.desc",					\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_delta,							\
	  { "Opt Delta",  prefix ".opt.delta",					\
	    FT_UINT8, BASE_DEC, NULL, 0xf0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_delta_ext,						\
	  { "Opt Delta extended",  prefix ".opt.delta_ext",			\
	    FT_UINT16, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_length,						\
	  { "Opt Length",  prefix ".opt.length",				\
	    FT_UINT8, BASE_DEC, NULL, 0x0f,					\
	    "Option Length", HFILL }						\
	},									\
	{ & name .hf.opt_length_ext,						\
	  { "Opt Length extended",  prefix ".opt.length_ext",			\
	    FT_UINT16, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_end_marker,						\
	  { "End of options marker",  prefix ".opt.end_marker",			\
	    FT_UINT8, BASE_DEC, NULL, 0x00,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_ctype,							\
	  { "Content-type",  prefix ".opt.ctype",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_max_age,						\
	  { "Max-age",  prefix ".opt.max_age",					\
	    FT_UINT32, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_proxy_uri,						\
	  { "Proxy-Uri",  prefix ".opt.proxy_uri",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_proxy_scheme,						\
	  { "Proxy-Scheme",  prefix ".opt.proxy_scheme",			\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_size1,							\
	  { "Size1",  prefix ".opt.size1",					\
	    FT_UINT32, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_etag,							\
	  { "Etag",  prefix ".opt.etag",					\
	    FT_BYTES, BASE_NONE, NULL, 0x0,					\
	    "Option Etag", HFILL }						\
	},									\
	{ & name .hf.opt_uri_host,						\
	  { "Uri-Host",  prefix ".opt.uri_host",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_location_path,						\
	  { "Location-Path",  prefix ".opt.location_path",			\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_uri_port,						\
	  { "Uri-Port",  prefix ".opt.uri_port",				\
	    FT_UINT16, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_location_query,					\
	  { "Location-Query",  prefix ".opt.location_query",			\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_reserved,				\
	  { "Reserved",  prefix ".opt.object_security_reserved",		\
	    FT_BOOLEAN, 8, NULL, COAP_OBJECT_SECURITY_RESERVED_MASK,		\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_kid_context_present,			\
	  { "Key ID Context Present",  prefix ".opt.object_security_kid_context_present",\
	    FT_BOOLEAN, 8, NULL, COAP_OBJECT_SECURITY_KID_CONTEXT_MASK,		\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_kid_present,				\
	  { "Key ID Present",  prefix ".opt.object_security_kid_present",	\
	    FT_BOOLEAN, 8, NULL, COAP_OBJECT_SECURITY_KID_MASK,			\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_piv_len,				\
	  { "Partial IV Length",  prefix ".opt.object_security_piv_len",	\
	    FT_UINT8, BASE_DEC, NULL, COAP_OBJECT_SECURITY_PIVLEN_MASK,		\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_piv,					\
	  { "Partial IV",  prefix ".opt.object_security_piv",			\
	    FT_BYTES, BASE_NONE, NULL, 0x00,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_kid_context_len,			\
	  { "Key ID Context Length",  prefix ".opt.object_security_kid_context_len",\
	    FT_UINT8, BASE_DEC, NULL, 0x00,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_kid_context,				\
	  { "Key ID Context",  prefix ".opt.object_security_kid_context",	\
	    FT_BYTES, BASE_NONE, NULL, 0x00,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_object_security_kid,					\
	  { "Key ID",  prefix ".opt.object_security_kid",			\
	    FT_BYTES, BASE_NONE, NULL, 0x00,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_uri_path,						\
	  { "Uri-Path",  prefix ".opt.uri_path",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_uri_path_recon,					\
	  { "Uri-Path",  prefix ".opt.uri_path_recon",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_observe_req,						\
	  { "Observe",  prefix ".opt.observe",					\
	    FT_UINT32, BASE_DEC, VALS(coap_vals_observe_options), 0x0,		\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_observe_rsp,						\
	  { "Observe sequence number",  prefix ".opt.observe",			\
	    FT_UINT32, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_hop_limit,						\
	  { "Hop Limit",  prefix ".opt.hop_limit",				\
	    FT_UINT8, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_accept,						\
	  { "Accept",  prefix ".opt.accept",					\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_if_match,						\
	  { "If-Match",  prefix ".opt.if_match",				\
	    FT_BYTES, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_block_number,						\
	  { "Block Number",  prefix ".opt.block_number",			\
	    FT_UINT32, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_block_mflag,						\
	  { "More Flag",  prefix ".opt.block_mflag",				\
	    FT_UINT8, BASE_DEC, NULL, COAP_BLOCK_MFLAG_MASK,			\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_block_size,						\
	  { "Encoded Block Size",  prefix ".opt.block_size",			\
	    FT_UINT8, BASE_DEC, NULL, COAP_BLOCK_SIZE_MASK,			\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_uri_query,						\
	  { "Uri-Query",  prefix ".opt.uri_query",				\
	    FT_STRING, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_echo,							\
	  { "Echo",  prefix ".opt.opt_echo",					\
	    FT_BYTES, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_no_response,						\
	  { "No-Response",  prefix ".opt.opt_no_response",			\
	    FT_UINT8, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_request_tag,						\
	  { "Request-Tag",  prefix ".opt.opt_request_tag",			\
	    FT_BYTES, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_ocf_version,						\
	  { "OCF-Content-Format-Version",					\
	    prefix ".opt.opt_ocf_version",					\
	    FT_UINT8, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_ocf_accept_version,					\
	  { "OCF-Accept-Content-Format-Version",				\
	    prefix ".opt.opt_ocf_accept_version",				\
	    FT_UINT8, BASE_DEC, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
	{ & name .hf.opt_unknown,						\
	  { "Unknown",  prefix ".opt.unknown",					\
	    FT_BYTES, BASE_NONE, NULL, 0x0,					\
	    NULL, HFILL }							\
	},									\
/* }}} */

/* {{{ */
#define COAP_COMMON_ETT_LIST(name)						\
	& name .ett.payload,							\
	& name .ett.option,							\

/* }}} */

/* {{{ */
#define COAP_COMMON_EI_LIST(name, prefix)					\
	{ & name .ei.opt_unknown_number,					\
	  { prefix ".unknown_option_number", PI_UNDECODED, PI_WARN,		\
	    "Unknown Option Number", EXPFILL }					\
	},									\
	{ & name .ei.opt_invalid_number,					\
	  { prefix ".invalid_option_number", PI_MALFORMED, PI_WARN,		\
	    "Invalid Option Number", EXPFILL }					\
	},									\
	{ & name .ei.opt_invalid_range,						\
	  { prefix ".invalid_option_range", PI_MALFORMED, PI_WARN,		\
	    "Invalid Option Range", EXPFILL }					\
	},									\
	{ & name .ei.opt_length_bad,						\
	  { prefix ".option_length_bad", PI_MALFORMED, PI_WARN,			\
	    "Option length bad", EXPFILL }					\
	},									\
	{ & name .ei.opt_object_security_bad,					\
	  { prefix ".option_oscore_bad", PI_MALFORMED, PI_WARN,	\
	    "Invalid OSCORE Option Format", EXPFILL }			\
	},									\

/* }}} */

#endif /* __PACKET_COAP_H__ */

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */