/
packet-coap.c
1895 lines (1683 loc) · 63.2 KB
/
packet-coap.c
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* packet-coap.c
* Routines for CoAP packet disassembly
* draft-ietf-core-coap-14.txt
* draft-ietf-core-block-10.txt
* draft-ietf-core-observe-16.txt
* draft-ietf-core-link-format-06.txt
* Shoichi Sakane <sakane@tanu.org>
*
* Changes for draft-ietf-core-coap-17.txt
* Hauke Mehrtens <hauke@hauke-m.de>
*
* Support for CoAP over TCP, TLS and WebSockets
* https://tools.ietf.org/html/rfc8323
* Peter Wu <peter@lekensteyn.nl>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <epan/conversation.h>
#include <epan/packet.h>
#include <epan/reassemble.h>
#include <epan/proto_data.h>
#include <epan/expert.h>
#include <epan/wmem_scopes.h>
#include <epan/to_str.h>
#include <epan/strutil.h>
#include "packet-dtls.h"
#include "packet-coap.h"
#include "packet-media-type.h"
#include "packet-tcp.h"
#include "packet-tls.h"
void proto_register_coap(void);
static dissector_table_t coap_tmf_media_type_dissector_table;
static dissector_table_t media_type_dissector_table;
static int proto_coap;
/*
* Used only to register the "CoAP for Thread Management Framework"
* dissector, which uses the same protocol and field IDs as the
* regular CoAP dissector, as it's just "CoAP except that we interpret
* application/octet-stream as meaning Thread Management Framework
* messages", because the Thread protocol, for whatever reasons (trying
* to keep the CoAP layer of their messages as short and simple to parse
* as possible, to save power and reduce the chances of low-power
* transmissions being misreceived?), did not register a media type for
* its messages and a 'cf' value for that media type.
*/
static int proto_coap_for_tmf;
static int hf_coap_length;
static int hf_coap_version;
static int hf_coap_ttype;
static int hf_coap_token_len;
static int hf_coap_token;
static int hf_coap_mid;
static int hf_coap_response_in;
static int hf_coap_response_to;
static int hf_coap_response_time;
static int hf_coap_request_resend_in;
static int hf_coap_response_resend_in;
static int hf_coap_oscore_kid;
static int hf_coap_oscore_kid_context;
static int hf_coap_oscore_piv;
static int hf_block_payload;
static int hf_block_length;
static int hf_blocks;
static int hf_block;
static int hf_block_overlap;
static int hf_block_overlap_conflicts;
static int hf_block_multiple_tails;
static int hf_block_too_long;
static int hf_block_error;
static int hf_block_count;
static int hf_block_reassembled_in;
static int hf_block_reassembled_length;
static gint ett_coap;
static gint ett_block;
static gint ett_blocks;
static expert_field ei_retransmitted;
static COAP_COMMON_LIST_T(dissect_coap_hf);
static dissector_handle_t coap_tcp_tls_handle;
static dissector_handle_t coap_other_handle;
static dissector_handle_t coap_for_tmf_handle;
static dissector_handle_t oscore_handle;
/* CoAP's IANA-assigned TCP/UDP port numbers */
#define DEFAULT_COAP_PORT 5683
#define DEFAULT_COAPS_PORT 5684
/* indicators whether those are to be showed or not */
#define DEFAULT_COAP_CTYPE_VALUE ~0U
#define DEFAULT_COAP_BLOCK_NUMBER ~0U
/* Macros specific to the OCF version options */
#define COAP_OCF_VERSION_SUB_MASK 0x3F
#define COAP_OCF_VERSION_MINOR_MASK 0x7C0
#define COAP_OCF_VERSION_MAJOR_MASK 0xF800
#define COAP_OCF_VERSION_MINOR_OFFSET 6
#define COAP_OCF_VERSION_MAJOR_OFFSET 11
/*
* Transaction Type
*/
#define TT_CON 0 // Confirmable
#define TT_NON 1 // Non-Confirmable
static const value_string vals_ttype[] = {
{ 0, "Confirmable" },
{ 1, "Non-Confirmable" },
{ 2, "Acknowledgement" },
{ 3, "Reset" },
{ 0, NULL },
};
static const value_string vals_ttype_short[] = {
{ 0, "CON" },
{ 1, "NON" },
{ 2, "ACK" },
{ 3, "RST" },
{ 0, NULL },
};
/*
* Method Code
* Response Code
* "c.dd" denotes (c << 5) | dd
*/
static const value_string vals_code[] = {
{ 0, "Empty Message" },
/* Method Codes */
{ 1, "GET" },
{ 2, "POST" },
{ 3, "PUT" },
{ 4, "DELETE" },
{ 5, "FETCH" }, /* RFC 8132 */
{ 6, "PATCH" }, /* RFC 8132 */
{ 7, "iPATCH" }, /* RFC 8132 */
/* Response Codes */
{ 65, "2.01 Created" },
{ 66, "2.02 Deleted" },
{ 67, "2.03 Valid" },
{ 68, "2.04 Changed" },
{ 69, "2.05 Content" },
{ 95, "2.31 Continue" },
{ 128, "4.00 Bad Request" },
{ 129, "4.01 Unauthorized" },
{ 130, "4.02 Bad Option" },
{ 131, "4.03 Forbidden" },
{ 132, "4.04 Not Found" },
{ 133, "4.05 Method Not Allowed" },
{ 134, "4.06 Not Acceptable" },
{ 136, "4.08 Request Entity Incomplete" }, /* RFC 7959 */
{ 137, "4.09 Conflict" }, /* RFC 8132 */
{ 140, "4.12 Precondition Failed" },
{ 141, "4.13 Request Entity Too Large" },
{ 143, "4.15 Unsupported Content-Format" },
{ 150, "4.22 Unprocessable Entity" }, /* RFC 8132 */
{ 157, "4.29 Too Many Requests" }, /* RFC 8516 */
{ 160, "5.00 Internal Server Error" },
{ 161, "5.01 Not Implemented" },
{ 162, "5.02 Bad Gateway" },
{ 163, "5.03 Service Unavailable" },
{ 164, "5.04 Gateway Timeout" },
{ 165, "5.05 Proxying Not Supported" },
{ 168, "5.08 Hop Limit Reached" }, /* RFC 8768 */
/* Signalling Codes */
{ 225, "7.01 CSM" }, /* RFC 8323 */
{ 226, "7.02 Ping" }, /* RFC 8323 */
{ 227, "7.03 Pong" }, /* RFC 8323 */
{ 228, "7.04 Release" }, /* RFC 8323 */
{ 229, "7.05 Abort" }, /* RFC 8323 */
{ 0, NULL },
};
value_string_ext coap_vals_code_ext = VALUE_STRING_EXT_INIT(vals_code);
const value_string coap_vals_observe_options[] = {
{ 0, "Register" },
{ 1, "Deregister" },
{ 0, NULL },
};
/*
* Option Headers
* No-Option must not be included in this structure, is handled in the function
* of the dissector, especially.
*/
#define COAP_OPT_IF_MATCH 1
#define COAP_OPT_URI_HOST 3
#define COAP_OPT_ETAG 4
#define COAP_OPT_IF_NONE_MATCH 5
#define COAP_OPT_OBSERVE 6 /* RFC 7641 / RFC 8613 */
#define COAP_OPT_URI_PORT 7
#define COAP_OPT_LOCATION_PATH 8
#define COAP_OPT_OBJECT_SECURITY 9 /* RFC 8613 */
#define COAP_OPT_URI_PATH 11
#define COAP_OPT_CONTENT_TYPE 12
#define COAP_OPT_MAX_AGE 14
#define COAP_OPT_URI_QUERY 15
#define COAP_OPT_HOP_LIMIT 16 /* RFC 8768 */
#define COAP_OPT_ACCEPT 17
#define COAP_OPT_QBLOCK1 19 /* RFC 9177 */
#define COAP_OPT_LOCATION_QUERY 20
#define COAP_OPT_EDHOC 21 /* draft-ietf-core-oscore-edhoc*/
#define COAP_OPT_BLOCK2 23 /* RFC 7959 / RFC 8323 */
#define COAP_OPT_BLOCK1 27 /* RFC 7959 / RFC 8323 */
#define COAP_OPT_SIZE2 28 /* RFC 7959 */
#define COAP_OPT_QBLOCK2 31 /* RFC 9177 */
#define COAP_OPT_PROXY_URI 35
#define COAP_OPT_PROXY_SCHEME 39
#define COAP_OPT_SIZE1 60
#define COAP_OPT_ECHO 252 /* RFC 9175*/
#define COAP_OPT_NO_RESPONSE 258 /* RFC 7967 / RFC 8613 */
#define COAP_OPT_REQUEST_TAG 292 /* RFC 9175 */
#define COAP_OPT_OCF_ACCEPT 2049 /* OCF Core specification */
#define COAP_OPT_OCF_CONTENT 2053 /* OCF Core specification */
static const value_string vals_opt_type[] = {
{ COAP_OPT_IF_MATCH, "If-Match" },
{ COAP_OPT_URI_HOST, "Uri-Host" },
{ COAP_OPT_ETAG, "Etag" },
{ COAP_OPT_IF_NONE_MATCH, "If-None-Match" },
{ COAP_OPT_URI_PORT, "Uri-Port" },
{ COAP_OPT_LOCATION_PATH, "Location-Path" },
{ COAP_OPT_OBJECT_SECURITY,"OSCORE" },
{ COAP_OPT_URI_PATH, "Uri-Path" },
{ COAP_OPT_CONTENT_TYPE, "Content-Format" },
{ COAP_OPT_MAX_AGE, "Max-age" },
{ COAP_OPT_URI_QUERY, "Uri-Query" },
{ COAP_OPT_HOP_LIMIT, "Hop-Limit" },
{ COAP_OPT_ACCEPT, "Accept" },
{ COAP_OPT_QBLOCK1, "Q-Block1" },
{ COAP_OPT_LOCATION_QUERY, "Location-Query" },
{ COAP_OPT_EDHOC, "EDHOC" },
{ COAP_OPT_PROXY_URI, "Proxy-Uri" },
{ COAP_OPT_PROXY_SCHEME, "Proxy-Scheme" },
{ COAP_OPT_SIZE1, "Size1" },
{ COAP_OPT_OBSERVE, "Observe" },
{ COAP_OPT_BLOCK2, "Block2" },
{ COAP_OPT_BLOCK1, "Block1" },
{ COAP_OPT_SIZE2, "Size2" },
{ COAP_OPT_QBLOCK2, "Q-Block2" },
{ COAP_OPT_ECHO, "Echo" },
{ COAP_OPT_NO_RESPONSE, "No-Response" },
{ COAP_OPT_REQUEST_TAG, "Request-Tag" },
{ COAP_OPT_OCF_ACCEPT, "OCF-Accept-Content-Format-Version" },
{ COAP_OPT_OCF_CONTENT, "OCF-Content-Format-Version" },
{ 0, NULL },
};
struct coap_option_range_t {
guint type;
gint min;
gint max;
} coi[] = {
{ COAP_OPT_IF_MATCH, 0, 8 },
{ COAP_OPT_URI_HOST, 1, 255 },
{ COAP_OPT_ETAG, 1, 8 },
{ COAP_OPT_IF_NONE_MATCH, 0, 0 },
{ COAP_OPT_URI_PORT, 0, 2 },
{ COAP_OPT_LOCATION_PATH, 0, 255 },
{ COAP_OPT_OBJECT_SECURITY, 0, 255 },
{ COAP_OPT_URI_PATH, 0, 255 },
{ COAP_OPT_CONTENT_TYPE, 0, 2 },
{ COAP_OPT_MAX_AGE, 0, 4 },
{ COAP_OPT_URI_QUERY, 1, 255 },
{ COAP_OPT_HOP_LIMIT, 1, 1 },
{ COAP_OPT_ACCEPT, 0, 2 },
{ COAP_OPT_QBLOCK1, 0, 3 },
{ COAP_OPT_LOCATION_QUERY, 0, 255 },
{ COAP_OPT_EDHOC, 0, 0 },
{ COAP_OPT_PROXY_URI, 1,1034 },
{ COAP_OPT_PROXY_SCHEME, 1, 255 },
{ COAP_OPT_SIZE1, 0, 4 },
{ COAP_OPT_OBSERVE, 0, 3 },
{ COAP_OPT_BLOCK2, 0, 3 },
{ COAP_OPT_BLOCK1, 0, 3 },
{ COAP_OPT_SIZE2, 0, 4 },
{ COAP_OPT_QBLOCK2, 0, 3 },
{ COAP_OPT_ECHO, 1, 40 },
{ COAP_OPT_NO_RESPONSE, 0, 1 },
{ COAP_OPT_REQUEST_TAG, 0, 8 },
{ COAP_OPT_OCF_ACCEPT, 2, 2 },
{ COAP_OPT_OCF_CONTENT, 2, 2 },
};
static const value_string vals_ctype[] = {
{ 0, "text/plain; charset=utf-8" },
{ 16, "application/cose; cose-type=\"cose-encrypt0\"" },
{ 17, "application/cose; cose-type=\"cose-mac0\"" },
{ 18, "application/cose; cose-type=\"cose-sign1\"" },
{ 19, "application/ace+cbor" },
{ 21, "image/gif" },
{ 22, "image/jpeg" },
{ 23, "image/png" },
{ 40, "application/link-format" },
{ 41, "application/xml" },
{ 42, "application/octet-stream" },
{ 47, "application/exi" },
{ 50, "application/json" },
{ 51, "application/json-patch+json" },
{ 52, "application/merge-patch+json" },
{ 60, "application/cbor" },
{ 61, "application/cwt" },
{ 62, "application/multipart-core" },
{ 63, "application/cbor-seq" },
{ 96, "application/cose; cose-type=\"cose-encrypt\"" },
{ 97, "application/cose; cose-type=\"cose-mac\"" },
{ 98, "application/cose; cose-type=\"cose-sign\"" },
{ 101, "application/cose-key" },
{ 102, "application/cose-key-set" },
{ 110, "application/senml+json" },
{ 111, "application/sensml+json" },
{ 112, "application/senml+cbor" },
{ 113, "application/sensml+cbor" },
{ 114, "application/senml-exi" },
{ 115, "application/sensml-exi" },
{ 140, "application/yang-data+cbor; id=sid" },
{ 256, "application/coap-group+json" },
{ 257, "application/concise-problem-details+cbor" },
{ 258, "application/swid+cbor" },
{ 271, "application/dots+cbor" },
{ 272, "application/missing-blocks+cbor-seq" },
{ 280, "application/pkcs7-mime; smime-type=server-generated-key" },
{ 281, "application/pkcs7-mime; smime-type=certs-only" },
{ 284, "application/pkcs8" },
{ 285, "application/csrattrs" },
{ 286, "application/pkcs10" },
{ 287, "application/pkix-cert" },
{ 290, "application/aif+cbor" },
{ 291, "application/aif+json" },
{ 310, "application/senml+xml" },
{ 311, "application/sensml+xml" },
{ 320, "application/senml-etch+json" },
{ 322, "application/senml-etch+cbor" },
{ 340, "application/yang-data+cbor" },
{ 341, "application/yang-data+cbor; id=name" },
{ 322, "application/senml-etch+cbor" },
{ 432, "application/td+json" },
{ 433, "application/tm+json" },
{ 1542, "application/vnd.oma.lwm2m+tlv" },
{ 1543, "application/vnd.oma.lwm2m+json" },
{ 10000, "application/vnd.ocf+cbor" },
{ 10001, "application/oscore" },
{ 10002, "application/javascript" },
{ 11050, "application/json (Content Coding: deflate)" },
{ 11060, "application/cbor (Content Coding: deflate)" },
{ 11542, "application/vnd.oma.lwm2m+tlv" },
{ 11543, "application/vnd.oma.lwm2m+json" },
{ 20000, "text/css" },
{ 30000, "image/svg+xml" },
{ 0, NULL },
};
static const char *nullstr = "(null)";
static reassembly_table coap_block_reassembly_table;
static const fragment_items coap_block_frag_items = {
/* Fragment subtrees */
&ett_block,
&ett_blocks,
/* Fragment fields */
&hf_blocks,
&hf_block,
&hf_block_overlap,
&hf_block_overlap_conflicts,
&hf_block_multiple_tails,
&hf_block_too_long,
&hf_block_error,
&hf_block_count,
/* Reassembled in field */
&hf_block_reassembled_in,
/* Reassembled length field */
&hf_block_reassembled_length,
/* Reassembled data field */
NULL,
/* Tag */
"Block fragments"
};
void proto_reg_handoff_coap(void);
static conversation_t *
find_or_create_conversation_noaddrb(packet_info *pinfo, gboolean request)
{
conversation_t *conv=NULL;
address *addr_a;
address *addr_b;
guint32 port_a;
guint32 port_b;
if (pinfo->ptype != PT_TCP) {
if (request) {
addr_a = &pinfo->src;
addr_b = &pinfo->dst;
port_a = pinfo->srcport;
port_b = pinfo->destport;
} else {
addr_a = &pinfo->dst;
addr_b = &pinfo->src;
port_a = pinfo->destport;
port_b = pinfo->srcport;
}
/* Have we seen this conversation before? */
if((conv = find_conversation(pinfo->num, addr_a, addr_b,
conversation_pt_to_conversation_type(pinfo->ptype), port_a,
port_b, NO_ADDR_B|NO_PORT_B)) != NULL) {
if (pinfo->num > conv->last_frame) {
conv->last_frame = pinfo->num;
}
} else {
/* No, this is a new conversation. */
conv = conversation_new(pinfo->num, &pinfo->src,
&pinfo->dst, conversation_pt_to_conversation_type(pinfo->ptype),
pinfo->srcport, pinfo->destport, NO_ADDR2|NO_PORT2);
}
} else {
/* fetch the conversation created by the TCP dissector */
conv = find_conversation_pinfo(pinfo, 0);
DISSECTOR_ASSERT(conv);
}
return conv;
}
static gint
coap_get_opt_uint(tvbuff_t *tvb, gint offset, gint length)
{
switch (length) {
case 0:
return 0;
case 1:
return (guint)tvb_get_guint8(tvb, offset);
case 2:
return (guint)tvb_get_ntohs(tvb, offset);
case 3:
return (guint)tvb_get_ntoh24(tvb, offset);
case 4:
return (guint)tvb_get_ntohl(tvb, offset);
default:
return -1;
}
}
static gint
coap_opt_check(packet_info *pinfo, proto_tree *subtree, guint opt_num, gint opt_length, coap_common_dissect_t *dissect_hf)
{
int i;
for (i = 0; i < (int)(array_length(coi)); i++) {
if (coi[i].type == opt_num)
break;
}
if (i == (int)(array_length(coi))) {
if (opt_num >= 2048 && opt_num <= 65535) {
/* private, vendor-specific or reserved for experiments */
expert_add_info_format(pinfo, subtree, &dissect_hf->ei.opt_unknown_number,
"Unknown Option Number %u", opt_num);
} else {
expert_add_info_format(pinfo, subtree, &dissect_hf->ei.opt_invalid_number,
"Invalid Option Number %u", opt_num);
}
return -1;
}
if (opt_length < coi[i].min || opt_length > coi[i].max) {
expert_add_info_format(pinfo, subtree, &dissect_hf->ei.opt_invalid_range,
"Invalid Option Range: %d (%d < x < %d)", opt_length, coi[i].min, coi[i].max);
}
return 0;
}
static void
dissect_coap_opt_hex_string(tvbuff_t *tvb, packet_info *pinfo, proto_item *item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
const guint8 *str;
if (opt_length == 0)
str = nullstr;
else
str = tvb_bytes_to_str_punct(pinfo->pool, tvb, offset, opt_length, ' ');
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_NA);
/* add info to the head of the packet detail */
proto_item_append_text(item, ": %s", str);
}
static void
dissect_coap_opt_uint(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
guint i = 0;
if (opt_length != 0) {
i = coap_get_opt_uint(tvb, offset, opt_length);
}
proto_tree_add_uint(subtree, hf, tvb, offset, opt_length, i);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %u", i);
}
static void
dissect_coap_opt_uri_host(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, coap_info *coinfo, int hf)
{
const guint8 *str;
proto_tree_add_item_ret_string(subtree, hf, tvb, offset, opt_length, ENC_ASCII, pinfo->pool, &str);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
/* forming a uri-string
* If the 'uri host' looks an IPv6 address, assuming that the address has
* to be enclosed by brackets.
*/
if (strchr(str, ':') == NULL) {
wmem_strbuf_append_printf(coinfo->uri_str_strbuf, "coap://%s", str);
} else {
wmem_strbuf_append_printf(coinfo->uri_str_strbuf, "coap://[%s]", str);
}
}
static void
dissect_coap_opt_uri_path(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, coap_info *coinfo, int hf)
{
const guint8 *str = NULL;
wmem_strbuf_append_c(coinfo->uri_str_strbuf, '/');
if (opt_length == 0) {
str = nullstr;
} else {
str = tvb_get_string_enc(pinfo->pool, tvb, offset, opt_length, ENC_ASCII);
wmem_strbuf_append(coinfo->uri_str_strbuf, str);
}
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_ASCII);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
}
static void
dissect_coap_opt_uri_query(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, coap_info *coinfo, int hf)
{
const guint8 *str = NULL;
wmem_strbuf_append_c(coinfo->uri_query_strbuf,
(wmem_strbuf_get_len(coinfo->uri_query_strbuf) == 0) ? '?' : '&');
if (opt_length == 0) {
str = nullstr;
} else {
str = tvb_get_string_enc(pinfo->pool, tvb, offset, opt_length, ENC_ASCII);
wmem_strbuf_append(coinfo->uri_query_strbuf, str);
}
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_ASCII);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
}
static void
dissect_coap_opt_location_path(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
const guint8 *str = NULL;
if (opt_length == 0) {
str = nullstr;
} else {
str = tvb_get_string_enc(pinfo->pool, tvb, offset, opt_length, ENC_ASCII);
}
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_ASCII);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
}
static void
dissect_coap_opt_location_query(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
const guint8 *str = NULL;
if (opt_length == 0) {
str = nullstr;
} else {
str = tvb_get_string_enc(pinfo->pool, tvb, offset, opt_length, ENC_ASCII);
}
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_ASCII);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
}
/* rfc8613 */
static void
dissect_coap_opt_object_security(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, packet_info *pinfo, coap_info *coinfo, coap_common_dissect_t *dissect_hf, guint8 code_class)
{
guint8 flag_byte = 0;
gboolean reserved = FALSE;
gboolean kid_context_present = FALSE;
gboolean kid_present = FALSE;
guint8 piv_len = 0;
guint8 kid_context_len = 0;
guint8 kid_len = 0;
coinfo->object_security = TRUE;
coinfo->oscore_info->piv = NULL;
coinfo->oscore_info->piv_len = 0;
coinfo->oscore_info->request_piv = NULL;
coinfo->oscore_info->request_piv_len = 0;
coinfo->oscore_info->kid_context = NULL;
coinfo->oscore_info->kid_context_len = 0;
coinfo->oscore_info->kid = NULL;
coinfo->oscore_info->kid_len = 0;
coinfo->oscore_info->response = FALSE;
if (opt_length == 0) { /* option length is zero, means flag byte is 0x00*/
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": 00 (no Flag Byte)");
} else {
flag_byte = tvb_get_guint8(tvb, offset);
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_reserved, tvb, offset, 1, ENC_BIG_ENDIAN);
reserved = flag_byte & COAP_OBJECT_SECURITY_RESERVED_MASK;
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_kid_context_present, tvb, offset, 1, ENC_BIG_ENDIAN);
kid_context_present = flag_byte & COAP_OBJECT_SECURITY_KID_CONTEXT_MASK;
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_kid_present, tvb, offset, 1, ENC_BIG_ENDIAN);
kid_present = flag_byte & COAP_OBJECT_SECURITY_KID_MASK;
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_piv_len, tvb, offset, 1, ENC_BIG_ENDIAN);
piv_len = (flag_byte & COAP_OBJECT_SECURITY_PIVLEN_MASK) >> 0;
/* kid_len is what remains in the option after all other fields are parsed
we calculate kid_len by subtracting from option length as we parse individual fields */
kid_len = opt_length;
offset += 1;
kid_len -= 1;
if (reserved) {
/* how these bits are handled is not yet specified */
expert_add_info_format(pinfo, subtree, &dissect_hf->ei.opt_object_security_bad, "Unsupported format");
}
if (piv_len > 0) {
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_piv, tvb, offset, piv_len, ENC_NA);
coinfo->oscore_info->piv = (guint8 *) tvb_memdup(pinfo->pool, tvb, offset, piv_len);
coinfo->oscore_info->piv_len = piv_len;
if (code_class == 0) {
/* If this is a request, copy PIV to request_piv */
coinfo->oscore_info->request_piv = (guint8 *) tvb_memdup(pinfo->pool, tvb, offset, piv_len);
coinfo->oscore_info->request_piv_len = piv_len;
}
offset += piv_len;
kid_len -= piv_len;
}
if (kid_context_present) {
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_kid_context_len, tvb, offset, 1, ENC_BIG_ENDIAN);
kid_context_len = tvb_get_guint8(tvb, offset);
offset += 1;
kid_len -= 1;
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_kid_context, tvb, offset, kid_context_len, ENC_NA);
coinfo->oscore_info->kid_context = (guint8 *) tvb_memdup(pinfo->pool, tvb, offset, kid_context_len);
coinfo->oscore_info->kid_context_len = kid_context_len;
offset += kid_context_len;
kid_len -= kid_context_len;
}
if (kid_present) {
proto_tree_add_item(subtree, dissect_hf->hf.opt_object_security_kid, tvb, offset, kid_len, ENC_NA);
coinfo->oscore_info->kid = (guint8 *) tvb_memdup(pinfo->pool, tvb, offset, kid_len);
coinfo->oscore_info->kid_len = kid_len;
}
proto_item_append_text(head_item, ": Key ID:%s, Key ID Context:%s, Partial IV:%s",
coinfo->oscore_info->kid == NULL ? nullstr : bytes_to_str(pinfo->pool, coinfo->oscore_info->kid, coinfo->oscore_info->kid_len),
coinfo->oscore_info->kid_context == NULL ? nullstr : bytes_to_str(pinfo->pool, coinfo->oscore_info->kid_context, coinfo->oscore_info->kid_context_len),
coinfo->oscore_info->piv == NULL ? nullstr : bytes_to_str(pinfo->pool, coinfo->oscore_info->piv, coinfo->oscore_info->piv_len));
}
}
static void
dissect_coap_opt_proxy_uri(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
const guint8 *str = NULL;
if (opt_length == 0) {
str = nullstr;
} else {
str = tvb_get_string_enc(pinfo->pool, tvb, offset, opt_length, ENC_ASCII);
}
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_ASCII);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
}
static void
dissect_coap_opt_proxy_scheme(tvbuff_t *tvb, packet_info *pinfo, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
const guint8 *str = NULL;
if (opt_length == 0) {
str = nullstr;
} else {
str = tvb_get_string_enc(pinfo->pool, tvb, offset, opt_length, ENC_ASCII);
}
proto_tree_add_item(subtree, hf, tvb, offset, opt_length, ENC_ASCII);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", format_text_string(pinfo->pool, str));
}
static void
dissect_coap_opt_ctype(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf, coap_info *coinfo)
{
if (opt_length == 0) {
coinfo->ctype_value = 0;
} else {
coinfo->ctype_value = coap_get_opt_uint(tvb, offset, opt_length);
}
coinfo->ctype_str = val_to_str(coinfo->ctype_value, vals_ctype, "Unknown Type %u");
proto_tree_add_string(subtree, hf, tvb, offset, opt_length, coinfo->ctype_str);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", coinfo->ctype_str);
}
static void
dissect_coap_opt_accept(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hf)
{
const guint8 *str = NULL;
if (opt_length == 0) {
str = nullstr;
} else {
guint value = coap_get_opt_uint(tvb, offset, opt_length);
str = val_to_str(value, vals_ctype, "Unknown Type %u");
}
proto_tree_add_string(subtree, hf, tvb, offset, opt_length, str);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %s", str);
}
static void
dissect_coap_opt_block(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, coap_info *coinfo, coap_common_dissect_t *dissect_hf)
{
guint8 val = 0;
guint encoded_block_size;
guint block_esize;
if (opt_length == 0) {
coinfo->block_number = 0;
val = 0;
} else {
coinfo->block_number = coap_get_opt_uint(tvb, offset, opt_length) >> 4;
val = tvb_get_guint8(tvb, offset + opt_length - 1) & 0x0f;
}
proto_tree_add_uint(subtree, dissect_hf->hf.opt_block_number,
tvb, offset, opt_length, coinfo->block_number);
/* More flag in the end of the option */
coinfo->block_mflag = (val & COAP_BLOCK_MFLAG_MASK) >> 3;
proto_tree_add_uint(subtree, dissect_hf->hf.opt_block_mflag,
tvb, offset + opt_length - 1, 1, val);
/* block size */
encoded_block_size = val & COAP_BLOCK_SIZE_MASK;
block_esize = 1 << (encoded_block_size + 4);
proto_tree_add_uint_format(subtree, dissect_hf->hf.opt_block_size,
tvb, offset + opt_length - 1, 1, encoded_block_size, "Block Size: %u (%u encoded)", block_esize, encoded_block_size);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": NUM:%u, M:%u, SZ:%u",
coinfo->block_number, coinfo->block_mflag, block_esize);
}
static void
dissect_coap_opt_ocf_version(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, int hfindex)
{
guint option_value = coap_get_opt_uint(tvb, offset, opt_length);
guint sub_version = option_value & COAP_OCF_VERSION_SUB_MASK;
guint minor_version = (option_value & COAP_OCF_VERSION_MINOR_MASK) >> COAP_OCF_VERSION_MINOR_OFFSET;
guint major_version = (option_value & COAP_OCF_VERSION_MAJOR_MASK) >> COAP_OCF_VERSION_MAJOR_OFFSET;
proto_tree_add_uint(subtree, hfindex, tvb, offset, opt_length, option_value);
/* add info to the head of the packet detail */
proto_item_append_text(head_item, ": %u.%u.%u",
major_version, minor_version, sub_version);
}
static void
dissect_coap_opt_uri_port(tvbuff_t *tvb, proto_item *head_item, proto_tree *subtree, gint offset, gint opt_length, coap_info *coinfo, int hf)
{
guint port = 0;
if (opt_length != 0) {
port = coap_get_opt_uint(tvb, offset, opt_length);
}
proto_tree_add_uint(subtree, hf, tvb, offset, opt_length, port);
proto_item_append_text(head_item, ": %u", port);
/* forming a uri-string */
wmem_strbuf_append_printf(coinfo->uri_str_strbuf, ":%u", port);
}
/*
* dissector for each option of CoAP.
* return the total length of the option including the header (e.g. delta and length).
*/
static int
dissect_coap_options_main(tvbuff_t *tvb, packet_info *pinfo, proto_tree *coap_tree, gint offset, guint8 opt_count, guint *opt_num, gint offset_end, guint8 code_class, coap_info *coinfo, coap_common_dissect_t *dissect_hf)
{
guint8 opt_jump;
gint opt_length, opt_length_ext, opt_delta, opt_delta_ext;
gint opt_length_ext_off = 0;
gint8 opt_length_ext_len = 0;
gint opt_delta_ext_off = 0;
gint8 opt_delta_ext_len = 0;
gint orig_offset = offset;
proto_tree *subtree;
proto_item *item;
char strbuf[56];
opt_jump = tvb_get_guint8(tvb, offset);
if (0xff == opt_jump)
return offset;
offset += 1;
/*
* section 3.1 in coap-17:
* Option Delta: 4-bit unsigned integer. A value between 0 and 12
* indicates the Option Delta. Three values are reserved for special
* constructs:
*
* 13: An 8-bit unsigned integer follows the initial byte and
* indicates the Option Delta minus 13.
*
* 14: A 16-bit unsigned integer in network byte order follows the
* initial byte and indicates the Option Delta minus 269.
*
* 15: Reserved for the Payload Marker. If the field is set to this
* value but the entire byte is not the payload marker, this MUST
* be processed as a message format error.
*/
switch (opt_jump & 0xf0) {
case 0xd0:
opt_delta_ext = tvb_get_guint8(tvb, offset);
opt_delta_ext_off = offset;
opt_delta_ext_len = 1;
offset += 1;
opt_delta = 13;
opt_delta += opt_delta_ext;
break;
case 0xe0:
opt_delta_ext = coap_get_opt_uint(tvb, offset, 2);
opt_delta_ext_off = offset;
opt_delta_ext_len = 2;
offset += 2;
opt_delta = 269;
opt_delta += opt_delta_ext;
break;
case 0xf0:
expert_add_info_format(pinfo, coap_tree, &dissect_hf->ei.opt_length_bad,
"end-of-options marker found, but option length isn't 15");
return -1;
default:
opt_delta = ((opt_jump & 0xf0) >> 4);
break;
}
*opt_num += opt_delta;
/*
* section 3.1 in coap-17:
* Option Length: 4-bit unsigned integer. A value between 0 and 12
* indicates the length of the Option Value, in bytes. Three values
* are reserved for special constructs:
*
* 13: An 8-bit unsigned integer precedes the Option Value and
* indicates the Option Length minus 13.
*
* 14: A 16-bit unsigned integer in network byte order precedes the
* Option Value and indicates the Option Length minus 269.
*
* 15: Reserved for future use. If the field is set to this value,
* it MUST be processed as a message format error.
*/
switch (opt_jump & 0x0f) {
case 0x0d:
opt_length_ext = tvb_get_guint8(tvb, offset);
opt_length_ext_off = offset;
opt_length_ext_len = 1;
offset += 1;
opt_length = 13;
opt_length += opt_length_ext;
break;
case 0x0e:
opt_length_ext = coap_get_opt_uint(tvb, offset, 2);
opt_length_ext_off = offset;
opt_length_ext_len = 2;
offset += 2;
opt_length = 269;
opt_length += opt_length_ext;
break;
case 0x0f:
expert_add_info_format(pinfo, coap_tree, &dissect_hf->ei.opt_length_bad,
"end-of-options marker found, but option delta isn't 15");
return -1;
default:
opt_length = (opt_jump & 0x0f);
break;
}
if (opt_length > offset_end - offset) {
expert_add_info_format(pinfo, coap_tree, &dissect_hf->ei.opt_length_bad,
"option longer than the package");
return -1;
}
snprintf(strbuf, sizeof(strbuf),
"#%u: %s", opt_count, val_to_str(*opt_num, vals_opt_type,
*opt_num % 14 == 0 ? "No-Op" : "Unknown Option (%d)"));
item = proto_tree_add_string(coap_tree, dissect_hf->hf.opt_name,
tvb, orig_offset, offset - orig_offset + opt_length, strbuf);
subtree = proto_item_add_subtree(item, dissect_hf->ett.option);
coap_opt_check(pinfo, subtree, *opt_num, opt_length, dissect_hf);
snprintf(strbuf, sizeof(strbuf),
"Type %u, %s, %s%s", *opt_num,
(*opt_num & 1) ? "Critical" : "Elective",
(*opt_num & 2) ? "Unsafe" : "Safe",
((*opt_num & 0x1e) == 0x1c) ? ", NoCacheKey" : "");
proto_tree_add_string(subtree, dissect_hf->hf.opt_desc,
tvb, orig_offset, offset - orig_offset + opt_length, strbuf);
proto_tree_add_item(subtree, dissect_hf->hf.opt_delta, tvb, orig_offset, 1, ENC_BIG_ENDIAN);
proto_tree_add_item(subtree, dissect_hf->hf.opt_length, tvb, orig_offset, 1, ENC_BIG_ENDIAN);
if (opt_delta_ext_off && opt_delta_ext_len)
proto_tree_add_item(subtree, dissect_hf->hf.opt_delta_ext, tvb, opt_delta_ext_off, opt_delta_ext_len, ENC_BIG_ENDIAN);
if (opt_length_ext_off && opt_length_ext_len)
proto_tree_add_item(subtree, dissect_hf->hf.opt_length_ext, tvb, opt_length_ext_off, opt_length_ext_len, ENC_BIG_ENDIAN);
/* offset points the next to its option header */
switch (*opt_num) {
case COAP_OPT_CONTENT_TYPE:
dissect_coap_opt_ctype(tvb, item, subtree, offset,
opt_length, dissect_hf->hf.opt_ctype, coinfo);
break;