|
| 1 | +/* |
| 2 | + * yggdrasil.c |
| 3 | + * |
| 4 | + * Copyright (C) 2026 - 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 | +#include "ndpi_protocol_ids.h" |
| 22 | + |
| 23 | +#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_YGGDRASIL |
| 24 | + |
| 25 | +#include "ndpi_api.h" |
| 26 | +#include "ndpi_private.h" |
| 27 | + |
| 28 | + |
| 29 | +static void ndpi_int_yggdrasil_add_connection(struct ndpi_detection_module_struct *ndpi_struct, |
| 30 | + struct ndpi_flow_struct *flow) |
| 31 | +{ |
| 32 | + NDPI_LOG_INFO(ndpi_struct, "found Yggdrasil\n"); |
| 33 | + if (flow->detected_protocol_stack[0] != NDPI_PROTOCOL_UNKNOWN) { |
| 34 | + ndpi_set_detected_protocol_keeping_master(ndpi_struct, flow, NDPI_PROTOCOL_YGGDRASIL, |
| 35 | + NDPI_CONFIDENCE_DPI); |
| 36 | + } else { |
| 37 | + ndpi_set_detected_protocol(ndpi_struct, flow, |
| 38 | + NDPI_PROTOCOL_YGGDRASIL, |
| 39 | + NDPI_PROTOCOL_UNKNOWN, |
| 40 | + NDPI_CONFIDENCE_DPI); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +static int ndpi_search_yggdrasil_http(struct ndpi_detection_module_struct *ndpi_struct, |
| 45 | + struct ndpi_flow_struct *flow) |
| 46 | +{ |
| 47 | + struct ndpi_packet_struct *packet = &ndpi_struct->packet; |
| 48 | + |
| 49 | + NDPI_LOG_DBG(ndpi_struct, "search Yggdrasil\n"); |
| 50 | + |
| 51 | + if (flow->detected_protocol_stack[0] != NDPI_PROTOCOL_HTTP && |
| 52 | + flow->detected_protocol_stack[1] != NDPI_PROTOCOL_HTTP) |
| 53 | + { |
| 54 | + return -1; |
| 55 | + } |
| 56 | + |
| 57 | + if (packet->parsed_lines == 0) |
| 58 | + { |
| 59 | + ndpi_parse_packet_line_info(ndpi_struct, flow); |
| 60 | + } |
| 61 | + |
| 62 | + if (packet->parsed_lines > 0) |
| 63 | + { |
| 64 | + size_t i; |
| 65 | + |
| 66 | + for (i = 0; i < packet->parsed_lines && packet->line[i].len > 0; ++i) |
| 67 | + { |
| 68 | + if (LINE_STARTS(packet->line[i], "Sec-Websocket-Protocol") != 0 && |
| 69 | + LINE_ENDS(packet->line[i], "ygg-ws") != 0) |
| 70 | + { |
| 71 | + return 0; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 77 | + return -1; |
| 78 | +} |
| 79 | + |
| 80 | +static void ndpi_search_yggdrasil(struct ndpi_detection_module_struct *ndpi_struct, |
| 81 | + struct ndpi_flow_struct *flow) |
| 82 | +{ |
| 83 | + if (ndpi_search_yggdrasil_http(ndpi_struct, flow) == 0) { |
| 84 | + ndpi_int_yggdrasil_add_connection(ndpi_struct, flow); |
| 85 | + return; |
| 86 | + } else { |
| 87 | + struct ndpi_packet_struct *packet = &ndpi_struct->packet; |
| 88 | + |
| 89 | + if (packet->payload_packet_len < 5) { |
| 90 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (get_u_int32_t(packet->payload, 0) == htonl(0x6D657461) // "meta" |
| 95 | + && get_u_int8_t(packet->payload, 4) == 0x00) |
| 96 | + { |
| 97 | + ndpi_int_yggdrasil_add_connection(ndpi_struct, flow); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
| 103 | +} |
| 104 | + |
| 105 | +void init_yggdrasil_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
| 106 | +{ |
| 107 | + ndpi_register_dissector("Yggdrasil", ndpi_struct, |
| 108 | + ndpi_search_yggdrasil, |
| 109 | + NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
| 110 | + 1, NDPI_PROTOCOL_YGGDRASIL); |
| 111 | +} |
0 commit comments