/src/ndpi/src/lib/protocols/roughtime.c
Line | Count | Source |
1 | | /* |
2 | | * roughtime.c |
3 | | * |
4 | | * Copyright (C) 2024 - ntop.org |
5 | | * |
6 | | * nDPI is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * nDPI is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | */ |
20 | | |
21 | | |
22 | | #include "ndpi_protocol_ids.h" |
23 | | |
24 | | #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_ROUGHTIME |
25 | | |
26 | | #include "ndpi_api.h" |
27 | | #include "ndpi_private.h" |
28 | | |
29 | | static u_int32_t const valid_tags[] = { |
30 | | 0x00444150 /* PAD */, |
31 | | 0x00474953 /* SIG */, |
32 | | 0x00524556 /* VER */, |
33 | | 0x31545544 /* DUT1 */, |
34 | | 0x434e4f4e /* NONC */, |
35 | | 0x454c4544 /* DELE */, |
36 | | 0x48544150 /* PATH */, |
37 | | 0x49415444 /* DTAI */, |
38 | | 0x49444152 /* RADI */, |
39 | | 0x4b425550 /* PUBK */, |
40 | | 0x5041454c /* LEAP */, |
41 | | 0x5044494d /* MIDP */, |
42 | | 0x50455253 /* SREP */, |
43 | | 0x544e494d /* MINT */, |
44 | | 0x544f4f52 /* ROOT */, |
45 | | 0x54524543 /* CERT */, |
46 | | 0x5458414d /* MAXT */, |
47 | | 0x58444e49 /* INDX */, |
48 | | /* |
49 | | * It seems that some implementations are not following the specs |
50 | | * by using 0xFF instead of 0x00 as ASCII NUL. |
51 | | */ |
52 | | 0xFF444150 /* PAD */, |
53 | | 0xFF474953 /* SIG */, |
54 | | 0xFF524556 /* VER */, |
55 | | /* |
56 | | * Newer drafts may have the following additional tag. |
57 | | */ |
58 | | 0x7a7a7a7a /* ZZZZ */, |
59 | | }; |
60 | | |
61 | | static void ndpi_int_roughtime_add_connection(struct ndpi_detection_module_struct * const ndpi_struct, |
62 | | struct ndpi_flow_struct * const flow) |
63 | 0 | { |
64 | 0 | NDPI_LOG_INFO(ndpi_struct, "found roughtime\n"); |
65 | 0 | ndpi_set_detected_protocol(ndpi_struct, flow, |
66 | 0 | NDPI_PROTOCOL_ROUGHTIME, |
67 | 0 | NDPI_PROTOCOL_UNKNOWN, |
68 | 0 | NDPI_CONFIDENCE_DPI); |
69 | 0 | } |
70 | | |
71 | | static void ndpi_search_roughtime(struct ndpi_detection_module_struct *ndpi_struct, |
72 | | struct ndpi_flow_struct *flow) |
73 | 2.99k | { |
74 | 2.99k | struct ndpi_packet_struct const * const packet = &ndpi_struct->packet; |
75 | | |
76 | 2.99k | NDPI_LOG_INFO(ndpi_struct, "search roughtime\n"); |
77 | | |
78 | 2.99k | if (packet->payload_packet_len < 4) |
79 | 62 | { |
80 | 62 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
81 | 62 | return; |
82 | 62 | } |
83 | | |
84 | 2.93k | u_int32_t number_of_tags = le32toh(get_u_int32_t(packet->payload, 0)); |
85 | 2.93k | size_t const minimum_length = 4 /* number of tags (N) */ + |
86 | 2.93k | (number_of_tags - 1) * 4 /* number of tag offsets (N-1) */ + |
87 | 2.93k | (number_of_tags * 4) /* tags itself (N) */; |
88 | 2.93k | if (number_of_tags < 1 || packet->payload_packet_len < minimum_length || |
89 | 95 | number_of_tags > NDPI_ARRAY_LENGTH(valid_tags)) |
90 | 2.85k | { |
91 | 2.85k | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
92 | 2.85k | return; |
93 | 2.85k | } |
94 | | |
95 | 78 | if (number_of_tags > 1) { |
96 | 62 | u_int32_t tag_offset = le32toh(get_u_int32_t(packet->payload, 4 + (number_of_tags - 2) * 4)); |
97 | 62 | if (packet->payload_packet_len < 4 + (number_of_tags - 1) * 4 + tag_offset) |
98 | 48 | { |
99 | 48 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
100 | 48 | return; |
101 | 48 | } |
102 | 62 | } |
103 | | |
104 | 30 | size_t i; |
105 | 30 | for (i = 0; i < number_of_tags; ++i) |
106 | 30 | { |
107 | 30 | u_int32_t tag = le32toh(get_u_int32_t(packet->payload, 4 + (number_of_tags - 1) * 4 + i * 4)); |
108 | | |
109 | 30 | size_t j; |
110 | 690 | for (j = 0; j < NDPI_ARRAY_LENGTH(valid_tags); ++j) |
111 | 660 | { |
112 | 660 | if (tag == valid_tags[j]) |
113 | 0 | { |
114 | 0 | break; |
115 | 0 | } |
116 | 660 | } |
117 | 30 | if (j == NDPI_ARRAY_LENGTH(valid_tags)) |
118 | 30 | { |
119 | 30 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
120 | 30 | return; |
121 | 30 | } |
122 | 30 | } |
123 | | |
124 | 0 | ndpi_int_roughtime_add_connection(ndpi_struct, flow); |
125 | 0 | } |
126 | | |
127 | | void init_roughtime_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
128 | 8.66k | { |
129 | 8.66k | ndpi_register_dissector("Roughtime", ndpi_struct, |
130 | 8.66k | ndpi_search_roughtime, |
131 | 8.66k | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
132 | 8.66k | 1, NDPI_PROTOCOL_ROUGHTIME); |
133 | 8.66k | } |