forked from OISF/suricata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-layer-dnp3.c
2658 lines (2273 loc) · 75.7 KB
/
app-layer-dnp3.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
/* Copyright (C) 2015 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "suricata-common.h"
#include "stream.h"
#include "util-byte.h"
#include "util-unittest.h"
#include "util-hashlist.h"
#include "util-print.h"
#include "app-layer-protos.h"
#include "app-layer-parser.h"
#include "app-layer-detect-proto.h"
#include "app-layer-dnp3.h"
#include "app-layer-dnp3-objects.h"
/* For hexdump(). */
#include "app-layer-dcerpc-common.h"
/* Default number of unreplied requests to be considered a flood. */
#define DNP3_DEFAULT_REQ_FLOOD_COUNT 500
#define DNP3_DEFAULT_PORT "20000"
/* Expected values for the start bytes. */
#define DNP3_START_BYTE0 0x05
#define DNP3_START_BYTE1 0x64
/* Minimum length for a DNP3 frame. */
#define DNP3_MIN_LEN 5
/* Length of each CRC. */
#define DNP3_CRC_LEN 2
/* DNP3 block size. After the link header a CRC is inserted after
* after 16 bytes of data. */
#define DNP3_BLOCK_SIZE 16
/* Maximum transport layer sequence number. */
#define DNP3_MAX_TRAN_SEQNO 64
/* Maximum application layer sequence number. */
#define DNP3_MAX_APP_SEQNO 16
/* The number of bytes in the header that are counted as part of the
* header length field. */
#define DNP3_LINK_HDR_LEN 5
/* Link function codes. */
enum {
DNP3_LINK_FC_CONFIRMED_USER_DATA = 3,
DNP3_LINK_FC_UNCONFIRMED_USER_DATA
};
/* Reserved addresses. */
#define DNP3_RESERVED_ADDR_MIN 0xfff0
#define DNP3_RESERVED_ADDR_MAX 0xfffb
/* Source addresses must be < 0xfff0. */
#define DNP3_SRC_ADDR_MAX 0xfff0
#define DNP3_OBJ_TIME_SIZE 6 /* AKA UINT48. */
#define DNP3_OBJ_G12_V1_SIZE 11
#define DNP3_OBJ_G12_V2_SIZE 11
#define DNP3_OBJ_G12_V3_SIZE 1
/* Extract the prefix code from the object qualifier. */
#define DNP3_OBJ_PREFIX(x) ((x >> 4) & 0x7)
/* Extract the range code from the object qualifier. */
#define DNP3_OBJ_RANGE(x) (x & 0xf)
/* Decoder event map. */
SCEnumCharMap dnp3_decoder_event_table[] = {
{"FLOODED", DNP3_DECODER_EVENT_FLOODED},
{"LEN_TOO_SMALL", DNP3_DECODER_EVENT_LEN_TOO_SMALL},
{"BAD_LINK_CRC", DNP3_DECODER_EVENT_BAD_LINK_CRC},
{"BAD_TRANSPORT_CRC", DNP3_DECODER_EVENT_BAD_TRANSPORT_CRC},
{"MALFORMED", DNP3_DECODER_EVENT_MALFORMED},
{"UNKNOWN_OBJECT", DNP3_DECODER_EVENT_UNKNOWN_OBJECT},
{NULL, -1},
};
/* Some DNP3 servers start with a banner. */
static const char banner[] = "DNP3";
/* Calculate the next transport sequence number. */
#define NEXT_TH_SEQNO(current) ((current + 1) % DNP3_MAX_TRAN_SEQNO)
/* Calculate the next application sequence number. */
#define NEXT_APP_SEQNO(current) ((current + 1) % DNP3_MAX_APP_SEQNO)
/* CRC table generated by pycrc - http://github.com/tpircher/pycrc.
* - Polynomial: 0x3d65. */
static const uint16_t crc_table[256] = {
0x0000, 0x365e, 0x6cbc, 0x5ae2, 0xd978, 0xef26, 0xb5c4, 0x839a,
0xff89, 0xc9d7, 0x9335, 0xa56b, 0x26f1, 0x10af, 0x4a4d, 0x7c13,
0xb26b, 0x8435, 0xded7, 0xe889, 0x6b13, 0x5d4d, 0x07af, 0x31f1,
0x4de2, 0x7bbc, 0x215e, 0x1700, 0x949a, 0xa2c4, 0xf826, 0xce78,
0x29af, 0x1ff1, 0x4513, 0x734d, 0xf0d7, 0xc689, 0x9c6b, 0xaa35,
0xd626, 0xe078, 0xba9a, 0x8cc4, 0x0f5e, 0x3900, 0x63e2, 0x55bc,
0x9bc4, 0xad9a, 0xf778, 0xc126, 0x42bc, 0x74e2, 0x2e00, 0x185e,
0x644d, 0x5213, 0x08f1, 0x3eaf, 0xbd35, 0x8b6b, 0xd189, 0xe7d7,
0x535e, 0x6500, 0x3fe2, 0x09bc, 0x8a26, 0xbc78, 0xe69a, 0xd0c4,
0xacd7, 0x9a89, 0xc06b, 0xf635, 0x75af, 0x43f1, 0x1913, 0x2f4d,
0xe135, 0xd76b, 0x8d89, 0xbbd7, 0x384d, 0x0e13, 0x54f1, 0x62af,
0x1ebc, 0x28e2, 0x7200, 0x445e, 0xc7c4, 0xf19a, 0xab78, 0x9d26,
0x7af1, 0x4caf, 0x164d, 0x2013, 0xa389, 0x95d7, 0xcf35, 0xf96b,
0x8578, 0xb326, 0xe9c4, 0xdf9a, 0x5c00, 0x6a5e, 0x30bc, 0x06e2,
0xc89a, 0xfec4, 0xa426, 0x9278, 0x11e2, 0x27bc, 0x7d5e, 0x4b00,
0x3713, 0x014d, 0x5baf, 0x6df1, 0xee6b, 0xd835, 0x82d7, 0xb489,
0xa6bc, 0x90e2, 0xca00, 0xfc5e, 0x7fc4, 0x499a, 0x1378, 0x2526,
0x5935, 0x6f6b, 0x3589, 0x03d7, 0x804d, 0xb613, 0xecf1, 0xdaaf,
0x14d7, 0x2289, 0x786b, 0x4e35, 0xcdaf, 0xfbf1, 0xa113, 0x974d,
0xeb5e, 0xdd00, 0x87e2, 0xb1bc, 0x3226, 0x0478, 0x5e9a, 0x68c4,
0x8f13, 0xb94d, 0xe3af, 0xd5f1, 0x566b, 0x6035, 0x3ad7, 0x0c89,
0x709a, 0x46c4, 0x1c26, 0x2a78, 0xa9e2, 0x9fbc, 0xc55e, 0xf300,
0x3d78, 0x0b26, 0x51c4, 0x679a, 0xe400, 0xd25e, 0x88bc, 0xbee2,
0xc2f1, 0xf4af, 0xae4d, 0x9813, 0x1b89, 0x2dd7, 0x7735, 0x416b,
0xf5e2, 0xc3bc, 0x995e, 0xaf00, 0x2c9a, 0x1ac4, 0x4026, 0x7678,
0x0a6b, 0x3c35, 0x66d7, 0x5089, 0xd313, 0xe54d, 0xbfaf, 0x89f1,
0x4789, 0x71d7, 0x2b35, 0x1d6b, 0x9ef1, 0xa8af, 0xf24d, 0xc413,
0xb800, 0x8e5e, 0xd4bc, 0xe2e2, 0x6178, 0x5726, 0x0dc4, 0x3b9a,
0xdc4d, 0xea13, 0xb0f1, 0x86af, 0x0535, 0x336b, 0x6989, 0x5fd7,
0x23c4, 0x159a, 0x4f78, 0x7926, 0xfabc, 0xcce2, 0x9600, 0xa05e,
0x6e26, 0x5878, 0x029a, 0x34c4, 0xb75e, 0x8100, 0xdbe2, 0xedbc,
0x91af, 0xa7f1, 0xfd13, 0xcb4d, 0x48d7, 0x7e89, 0x246b, 0x1235
};
/**
* \brief Compute the CRC for a buffer.
*
* \param buf Buffer to create CRC from.
* \param len Length of buffer (number of bytes to use for CRC).
*/
static uint16_t DNP3ComputeCRC(const uint8_t *buf, uint32_t len)
{
const uint8_t *byte = buf;
uint16_t crc = 0;
int idx;
while (len--) {
idx = (crc ^ *byte) & 0xff;
crc = (crc_table[idx] ^ (crc >> 8)) & 0xffff;
byte++;
}
return ~crc & 0xffff;
}
/**
* \brief Check the CRC of a block.
*
* \param block The block of data with CRC to be checked.
* \param len The size of the data block.
*
* \retval 1 if CRC is OK, otherwise 0.
*/
static int DNP3CheckCRC(const uint8_t *block, uint32_t len)
{
uint32_t crc_offset;
uint16_t crc;
/* Need at least one byte plus the CRC. */
if (len < DNP3_CRC_LEN + 1) {
return 0;
}
crc_offset = len - DNP3_CRC_LEN;
crc = DNP3ComputeCRC(block, len - DNP3_CRC_LEN);
if (((crc & 0xff) == block[crc_offset]) &&
((crc >> 8) == block[crc_offset + 1])) {
return 1;
}
return 0;
}
/**
* \brief Check the CRC of the link header.
*
* \param header Point to the link header.
*
* \retval 1 if header CRC is OK, otherwise 0.
*/
static int DNP3CheckLinkHeaderCRC(const DNP3LinkHeader *header)
{
return DNP3CheckCRC((uint8_t *)header, sizeof(DNP3LinkHeader));
}
/**
* \brief Check user data CRCs.
*
* \param data Pointer to user data.
* \param len Length of user data.
*
* \retval 1 if CRCs are OK, otherwise 0.
*/
static int DNP3CheckUserDataCRCs(const uint8_t *data, uint32_t len)
{
uint32_t offset = 0;
uint32_t block_size;
while (offset < len) {
if (len - offset >= DNP3_BLOCK_SIZE + DNP3_CRC_LEN) {
block_size = DNP3_BLOCK_SIZE + DNP3_CRC_LEN;
}
else {
block_size = len - offset;
}
if (!DNP3CheckCRC(data + offset, block_size)) {
/* Once failed, may as well return immediately. */
return 0;
}
offset += block_size;
}
return 1;
}
/**
* \brief Check the DNP3 frame start bytes.
*
* \retval 1 if valid, 0 if not.
*/
static int DNP3CheckStartBytes(const DNP3LinkHeader *header)
{
return header->start_byte0 == DNP3_START_BYTE0 &&
header->start_byte1 == DNP3_START_BYTE1;
}
/**
* \brief Check if a frame contains a banner.
*
* Some servers (outstations) appear to send back a banner that fails
* the normal frame checks. So first check for a banner.
*
* \retval 1 if a banner is found, 0 if not.
*/
static int DNP3ContainsBanner(const uint8_t *input, uint32_t len)
{
return memmem(input, len, banner, strlen(banner)) != NULL;
}
/**
* \brief DNP3 probing parser.
*/
static uint16_t DNP3ProbingParser(uint8_t *input, uint32_t len,
uint32_t *offset)
{
DNP3LinkHeader *hdr = (DNP3LinkHeader *)input;
/* Check that we have the minimum amount of bytes. */
if (len < sizeof(DNP3LinkHeader)) {
SCLogDebug("Length too small to be a DNP3 header.");
return ALPROTO_UNKNOWN;
}
/* May be a banner. */
if (DNP3ContainsBanner(input, len)) {
SCLogDebug("Packet contains a DNP3 banner.");
goto end;
}
/* Verify start value (from AN2013-004b). */
if (!DNP3CheckStartBytes(hdr)) {
SCLogDebug("Invalid start bytes.");
return ALPROTO_FAILED;
}
/* Verify minimum length. */
if (hdr->len < DNP3_MIN_LEN) {
SCLogDebug("Packet too small to be a valid DNP3 fragment.");
return ALPROTO_FAILED;
}
end:
SCLogDebug("Detected DNP3.");
return ALPROTO_DNP3;
}
/**
* \brief Caculate the length of the transport layer with CRCs removed.
*
* \param input_len The length of the transport layer buffer.
*
* \retval The length of the buffer after CRCs are removed.
*/
static int DNP3CalculateTransportLengthWithoutCRCs(uint32_t input_len)
{
/* Too small. */
if (input_len < DNP3_CRC_LEN) {
return -1;
}
/* Get the number of complete blocks. */
int blocks = input_len / (DNP3_BLOCK_SIZE + DNP3_CRC_LEN);
/* And the number of bytes in the last block. */
int rem = input_len - (blocks * (DNP3_BLOCK_SIZE + DNP3_CRC_LEN));
if (rem) {
if (rem < DNP3_CRC_LEN) {
return -1;
}
return (blocks * DNP3_BLOCK_SIZE) + (rem - DNP3_CRC_LEN);
}
else {
return (blocks * DNP3_BLOCK_SIZE);
}
}
/**
* \brief Reassemble the application layer by stripping the CRCs.
*
* Remove the CRCs from the user data blocks. The output is the user
* data with the CRCs removed as well as the transport header removed,
* but the input data still needs to include the transport header as
* its part of the first user data block.
*
* If the output length passed in is non-null, the new input data will
* be appended, and the output length pointer incremented as needed.
*
* \param input Input buffer starting at the transport header (which
* will be removed from the output).
* \param input_len Length of the input buffer.
* \param output Pointer to output buffer (may be realloc'd).
* \param output_len Pointer to output length.
*
* \retval 1 if reassembly was successful, otherwise 0.
*/
static int DNP3ReassembleApplicationLayer(const uint8_t *input,
uint32_t input_len, uint8_t **output, uint32_t *output_len)
{
int len = DNP3CalculateTransportLengthWithoutCRCs(input_len);
if (len <= 0) {
return 0;
}
/* Remove one byte for the transport header and make sure we have
* at least one byte of user data. */
if (--len < 1) {
return 0;
}
if (*output == NULL) {
*output = SCCalloc(1, len);
if (unlikely(*output == NULL)) {
return 0;
}
}
else {
uint8_t *ptr = SCRealloc(*output, (size_t)(*output_len + len));
if (unlikely(ptr == NULL)) {
return 0;
}
*output = ptr;
}
int offset = 0, block_size;
while ((uint32_t)offset < input_len) {
if (input_len - offset > DNP3_BLOCK_SIZE + DNP3_CRC_LEN) {
block_size = DNP3_BLOCK_SIZE + DNP3_CRC_LEN;
}
else {
block_size = input_len - offset;
}
/* If handling the first block (offset is 0), trim off the
* first byte which is the transport header, and not part of
* the application data. */
if (offset == 0) {
offset++;
block_size--;
}
/* Need at least 3 bytes to continue. One for application
* data, and 2 for the CRC. If not, return failure for
* malformed frame. */
if (block_size < DNP3_CRC_LEN + 1) {
SCLogDebug("Not enough data to continue.");
return 0;
}
/* Make sure there is enough space to write into. */
if (block_size - DNP3_CRC_LEN > len) {
SCLogDebug("Not enough data to continue.");
return 0;
}
memcpy(*output + *output_len, input + offset,
block_size - DNP3_CRC_LEN);
*output_len += block_size - DNP3_CRC_LEN;
offset += block_size;
len -= block_size - DNP3_CRC_LEN;
}
return 1;
}
/**
* \brief Allocate a DNP3 state object.
*
* The DNP3 state object represents a single DNP3 TCP session.
*/
static void *DNP3StateAlloc(void)
{
SCEnter();
DNP3State *dnp3;
dnp3 = (DNP3State *)SCCalloc(1, sizeof(DNP3State));
if (unlikely(dnp3 == NULL)) {
return NULL;
}
TAILQ_INIT(&dnp3->tx_list);
SCReturnPtr(dnp3, "void");
}
/**
* \brief Set a DNP3 application layer event.
*
* Sets an event on the current transaction object.
*/
static void DNP3SetEvent(DNP3State *dnp3, uint8_t event)
{
if (dnp3 && dnp3->curr) {
AppLayerDecoderEventsSetEventRaw(&dnp3->curr->decoder_events, event);
dnp3->events++;
}
else {
SCLogWarning(SC_ERR_ALPARSER,
"Fail set set event, state or txn was NULL.");
}
}
/**
* \brief Set a DNP3 application layer event on a transaction.
*/
static void DNP3SetEventTx(DNP3Transaction *tx, uint8_t event)
{
AppLayerDecoderEventsSetEventRaw(&tx->decoder_events, event);
tx->dnp3->events++;
}
/**
* \brief Allocation a DNP3 transaction.
*/
static DNP3Transaction *DNP3TxAlloc(DNP3State *dnp3)
{
DNP3Transaction *tx = SCCalloc(1, sizeof(DNP3Transaction));
if (unlikely(tx == NULL)) {
return NULL;
}
dnp3->transaction_max++;
dnp3->unreplied++;
dnp3->curr = tx;
tx->dnp3 = dnp3;
tx->tx_num = dnp3->transaction_max;
TAILQ_INIT(&tx->request_objects);
TAILQ_INIT(&tx->response_objects);
TAILQ_INSERT_TAIL(&dnp3->tx_list, tx, next);
/* Check for flood state. */
if (dnp3->unreplied > DNP3_DEFAULT_REQ_FLOOD_COUNT) {
DNP3SetEvent(dnp3, DNP3_DECODER_EVENT_FLOODED);
dnp3->flooded = 1;
}
return tx;
}
/**
* \brief Calculate the length of a link frame with CRCs.
*
* This is required as the length parameter in the DNP3 header does not
* include the added CRCs.
*
* \param length The length from the DNP3 link header.
*
* \retval The length of the frame with CRCs included or 0 if the length isn't
* long enough to be a valid DNP3 frame.
*/
static uint32_t DNP3CalculateLinkLength(uint8_t length)
{
uint32_t frame_len = 0;
int rem;
/* Fail early if the length is less than the minimum size. */
if (length < DNP3_LINK_HDR_LEN) {
return 0;
}
/* Subtract the 5 bytes of the header that are included in the
* length. */
length -= DNP3_LINK_HDR_LEN;
rem = length % DNP3_BLOCK_SIZE;
frame_len = (length / DNP3_BLOCK_SIZE) * (DNP3_BLOCK_SIZE + DNP3_CRC_LEN);
if (rem) {
frame_len += rem + DNP3_CRC_LEN;
}
return frame_len + sizeof(DNP3LinkHeader);
}
/**
* \brief Check if the link function code specifies user data.
*
* \param header Point to link header.
*
* \retval 1 if frame contains user data, otherwise 0.
*/
static int DNP3IsUserData(const DNP3LinkHeader *header)
{
switch (DNP3_LINK_FC(header->control)) {
case DNP3_LINK_FC_CONFIRMED_USER_DATA:
case DNP3_LINK_FC_UNCONFIRMED_USER_DATA:
return 1;
default:
return 0;
}
}
/**
* \brief Check if the frame has user data.
*
* Check if the DNP3 frame actually has user data by checking if data
* exists after the headers.
*
* \retval 1 if user data exists, otherwise 0.
*/
static int DNP3HasUserData(const DNP3LinkHeader *header)
{
if (DNP3_LINK_DIR(header->control)) {
return header->len >= DNP3_LINK_HDR_LEN + sizeof(DNP3TransportHeader) +
sizeof(DNP3ApplicationHeader);
}
else {
return header->len >= DNP3_LINK_HDR_LEN + sizeof(DNP3TransportHeader) +
sizeof(DNP3ApplicationHeader) + sizeof(DNP3InternalInd);
}
}
/**
* \brief Reset a DNP3Buffer.
*/
static void DNP3BufferReset(DNP3Buffer *buffer)
{
buffer->offset = 0;
buffer->len = 0;
}
/**
* \brief Add data to a DNP3 buffer, enlarging the buffer if required.
*
* \param buffer Buffer to add data data.
* \param data Data to be added to buffer.
* \param len Size of data to be added to buffer.
*
* \param 1 if data was added successful, otherwise 0.
*/
static int DNP3BufferAdd(DNP3Buffer *buffer, const uint8_t *data, uint32_t len)
{
if (buffer->size == 0) {
buffer->buffer = SCCalloc(1, len);
if (unlikely(buffer->buffer == NULL)) {
return 0;
}
buffer->size = len;
}
else if (buffer->len + len > buffer->size) {
uint8_t *tmp = SCRealloc(buffer->buffer, buffer->len + len);
if (unlikely(tmp == NULL)) {
return 0;
}
buffer->buffer = tmp;
buffer->size = buffer->len + len;
}
memcpy(buffer->buffer + buffer->len, data, len);
buffer->len += len;
return 1;
}
/**
* \brief Trim a DNP3 buffer.
*
* Trimming a buffer moves the data in the buffer up to the front of
* the buffer freeing up room at the end for more incoming data.
*
* \param buffer The buffer to trim.
*/
static void DNP3BufferTrim(DNP3Buffer *buffer)
{
if (buffer->offset == buffer->len) {
DNP3BufferReset(buffer);
}
else if (buffer->offset > 0) {
memmove(buffer->buffer, buffer->buffer + buffer->offset,
buffer->len - buffer->offset);
buffer->len = buffer->len - buffer->offset;
buffer->offset = 0;
}
}
/**
* \brief Free a DNP3 object.
*/
static void DNP3ObjectFree(DNP3Object *object)
{
if (object->points != NULL) {
DNP3FreeObjectPointList(object->group, object->variation,
object->points);
}
SCFree(object);
}
/**
* \breif Allocate a DNP3 object.
*/
static DNP3Object *DNP3ObjectAlloc(void)
{
DNP3Object *object = SCCalloc(1, sizeof(*object));
if (unlikely(object == NULL)) {
return NULL;
}
object->points = DNP3PointListAlloc();
if (object->points == NULL) {
DNP3ObjectFree(object);
return NULL;
}
return object;
}
/**
* \brief Decode DNP3 application objects.
*
* This function decoded known DNP3 application objects. As the
* protocol isn't self describing, we can only decode the buffer while
* the application objects are known. As soon as an unknown
* group/variation is hit, we must stop processing.
*
* \param buf the input buffer
* \param len length of the input buffer
* \param objects pointer to list where decoded objects will be stored.
*
* \retval 1 if all objects decoded, 0 if all objects could not be decoded (
* unknown group/variations)
*/
static int DNP3DecodeApplicationObjects(DNP3Transaction *tx, const uint8_t *buf,
uint32_t len, DNP3ObjectList *objects)
{
int retval = 0;
if (buf == NULL || len == 0) {
return 1;
}
while (len) {
uint32_t offset = 0;
if (len < sizeof(DNP3ObjHeader)) {
goto done;
}
DNP3ObjHeader *header = (DNP3ObjHeader *)buf;
offset += sizeof(DNP3ObjHeader);
DNP3Object *object = DNP3ObjectAlloc();
if (unlikely(object == NULL)) {
goto done;
}
TAILQ_INSERT_TAIL(objects, object, next);
object->group = header->group;
object->variation = header->variation;
object->qualifier = header->qualifier;
object->prefix_code = DNP3_OBJ_PREFIX(header->qualifier);
object->range_code = DNP3_OBJ_RANGE(header->qualifier);
/* IEEE 1815-2012, Table 4-5. */
switch (object->range_code) {
case 0x00:
case 0x03: {
/* 1 octet start and stop indexes OR 1 octet start and
* stop virtual addresses. */
if (offset + (sizeof(uint8_t) * 2) > len) {
/* Not enough data. */
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->start = buf[offset++];
object->stop = buf[offset++];
object->count = object->stop - object->start + 1;
break;
}
case 0x01:
case 0x04: {
/* 2 octet start and stop indexes OR 2 octect start
* and stop virtual addresses. */
if (offset + (sizeof(uint16_t) * 2) > len) {
/* Not enough data. */
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->start = DNP3_SWAP16(*(uint16_t *)(buf + offset));
offset += sizeof(uint16_t);
object->stop = DNP3_SWAP16(*(uint16_t *)(buf + offset));
offset += sizeof(uint16_t);
object->count = object->stop - object->start + 1;
break;
}
case 0x02:
case 0x05: {
/* 4 octet start and stop indexes OR 4 octect start
* and stop virtual addresses. */
if (offset + (sizeof(uint32_t) * 2) > len) {
/* Not enough data. */
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->start = DNP3_SWAP32(*(uint32_t *)(buf + offset));
offset += sizeof(uint32_t);
object->stop = DNP3_SWAP32(*(uint32_t *)(buf + offset));
offset += sizeof(uint32_t);
object->count = object->stop - object->start + 1;
break;
}
case 0x06:
/* No range field. */
object->count = 0;
break;
case 0x07:
/* 1 octet count of objects. */
if (offset + sizeof(uint8_t) > len) {
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->count = buf[offset];
offset += sizeof(uint8_t);
break;
case 0x08: {
/* 2 octet count of objects. */
if (offset + sizeof(uint16_t) > len) {
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->count = DNP3_SWAP16(*(uint16_t *)(buf + offset));
offset += sizeof(uint16_t);
break;
}
case 0x09: {
/* 4 octet count of objects. */
if (offset + sizeof(uint32_t) > len) {
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->count = DNP3_SWAP32(*(uint32_t *)(buf + offset));
offset += sizeof(uint32_t);
break;
}
case 0x0b: {
if (offset + sizeof(uint8_t) > len) {
/* Not enough data. */
SCLogDebug("Not enough data.");
goto not_enough_data;
}
object->count = *(uint8_t *)(buf + offset);
offset += sizeof(uint8_t);
break;
}
default:
SCLogDebug("Range code 0x%02x is reserved.",
object->range_code);
goto done;
}
buf += offset;
len -= offset;
if (object->variation == 0 || object->count == 0) {
goto next;
}
int event = DNP3DecodeObject(header->group, header->variation, &buf,
&len, object->prefix_code, object->start, object->count,
object->points);
if (event) {
DNP3SetEventTx(tx, DNP3_DECODER_EVENT_UNKNOWN_OBJECT);
goto done;
}
next:
continue;
}
/* All objects were decoded. */
retval = 1;
not_enough_data:
done:
return retval;
}
/**
* \brief Handle DNP3 request user data.
*
* \param dnp3 the current DNP3State
* \param input pointer to the DNP3 frame (starting with link header)
* \param input_len length of the input frame
*/
static void DNP3HandleUserDataRequest(DNP3State *dnp3, const uint8_t *input,
uint32_t input_len)
{
DNP3LinkHeader *lh;
DNP3TransportHeader th;
DNP3ApplicationHeader *ah;
DNP3Transaction *tx = NULL, *ttx;
lh = (DNP3LinkHeader *)input;
if (!DNP3CheckUserDataCRCs(input + sizeof(DNP3LinkHeader),
input_len - sizeof(DNP3LinkHeader))) {
return;
}
th = input[sizeof(DNP3LinkHeader)];
if (!DNP3_TH_FIR(th)) {
TAILQ_FOREACH(ttx, &dnp3->tx_list, next) {
if (ttx->request_lh.src == lh->src &&
ttx->request_lh.dst == lh->dst &&
ttx->has_request &&
!ttx->request_done &&
NEXT_TH_SEQNO(DNP3_TH_SEQ(ttx->request_th)) == DNP3_TH_SEQ(th))
{
tx = ttx;
break;
}
}
if (tx == NULL) {
return;
}
/* Update the saved transport header so subsequent segments
* will be matched to this sequence number. */
tx->response_th = th;
}
else {
ah = (DNP3ApplicationHeader *)(input + sizeof(DNP3LinkHeader) +
sizeof(DNP3TransportHeader));
/* Ignore confirms - for now. */
if (ah->function_code == DNP3_APP_FC_CONFIRM) {
return;
}
/* Create a transaction. */
tx = DNP3TxAlloc(dnp3);
if (unlikely(tx == NULL)) {
return;
}
tx->request_lh = *lh;
tx->request_th = th;
tx->request_ah = *ah;
tx->has_request = 1;
}
if (!DNP3ReassembleApplicationLayer(input + sizeof(DNP3LinkHeader),
input_len - sizeof(DNP3LinkHeader),
&tx->request_buffer, &tx->request_buffer_len)) {
/* Malformed, set event and mark as done. */
DNP3SetEvent(dnp3, DNP3_DECODER_EVENT_MALFORMED);
tx->request_done = 1;
return;
}
/* If this is not the final segment, just return. */
if (!DNP3_TH_FIN(th)) {
return;
}
tx->request_done = 1;
/* Some function codes do not expect a reply. */
switch (tx->request_ah.function_code) {
case DNP3_APP_FC_CONFIRM:
case DNP3_APP_FC_DIR_OPERATE_NR:
case DNP3_APP_FC_FREEZE_NR:
case DNP3_APP_FC_FREEZE_CLEAR_NR:
case DNP3_APP_FC_FREEZE_AT_TIME_NR:
case DNP3_APP_FC_AUTH_REQ_NR:
tx->response_done = 1;
default:
break;
}
if (DNP3DecodeApplicationObjects(
tx, tx->request_buffer + sizeof(DNP3ApplicationHeader),
tx->request_buffer_len - sizeof(DNP3ApplicationHeader),
&tx->request_objects)) {
tx->request_complete = 1;
}
}
static void DNP3HandleUserDataResponse(DNP3State *dnp3, const uint8_t *input,
uint32_t input_len)
{
DNP3LinkHeader *lh;
DNP3TransportHeader th;
DNP3ApplicationHeader *ah;
DNP3InternalInd *iin;
DNP3Transaction *tx = NULL, *ttx;
uint32_t offset = 0;
lh = (DNP3LinkHeader *)input;
offset += sizeof(DNP3LinkHeader);
if (!DNP3CheckUserDataCRCs(input + offset, input_len - offset)) {
return;
}
th = input[offset++];
if (!DNP3_TH_FIR(th)) {
TAILQ_FOREACH(ttx, &dnp3->tx_list, next) {
if (ttx->response_lh.src == lh->src &&
ttx->response_lh.dst == lh->dst &&
ttx->has_response && !ttx->response_done &&
NEXT_TH_SEQNO(DNP3_TH_SEQ(ttx->response_th)) == DNP3_TH_SEQ(th))
{
tx = ttx;
break;
}
}
if (tx == NULL) {
return;
}
/* Replace the transport header in the transaction with this
* one in case there are more frames. */
tx->response_th = th;
}
else {
ah = (DNP3ApplicationHeader *)(input + offset);
offset += sizeof(DNP3ApplicationHeader);
iin = (DNP3InternalInd *)(input + offset);
if (ah->function_code == DNP3_APP_FC_UNSOLICITED_RESP) {
tx = DNP3TxAlloc(dnp3);
if (unlikely(tx == NULL)) {
return;
}
/* There is no request associated with an unsolicited
* response, so mark the request done as far as
* transaction state handling is concerned. */
tx->request_done = 1;
}
else {
/* Find transaction. */
TAILQ_FOREACH(ttx, &dnp3->tx_list, next) {
if (ttx->has_request &&
ttx->request_done &&
ttx->request_lh.src == lh->dst &&
ttx->request_lh.dst == lh->src &&
!ttx->has_response &&
!ttx->response_done &&
DNP3_APP_SEQ(ttx->request_ah.control) == DNP3_APP_SEQ(ah->control)) {
tx = ttx;
break;
}
}
if (tx == NULL) {
return;
}