/src/ndpi/src/lib/protocols/diameter.c
Line | Count | Source |
1 | | /* |
2 | | * diameter.c |
3 | | * |
4 | | * Copyright (C) 2018 - ntop.org |
5 | | * |
6 | | * This file is part of nDPI, an open source deep packet inspection |
7 | | * library based on the OpenDPI and PACE technology by ipoque GmbH |
8 | | * |
9 | | * nDPI is free software: you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser General Public License as published by |
11 | | * the Free Software Foundation, either version 3 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * nDPI is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
21 | | * |
22 | | * Based on code of: |
23 | | * Michele Campus - <campus@ntop.org> |
24 | | */ |
25 | | #include "ndpi_protocol_ids.h" |
26 | | |
27 | | #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_DIAMETER |
28 | | |
29 | | #include "ndpi_api.h" |
30 | | #include "ndpi_private.h" |
31 | | |
32 | | |
33 | | // Header Flags possibile values |
34 | 116k | #define DIAMETER_REQUEST 0X80 |
35 | 116k | #define DIAMETER_PROXYABLE 0X40 |
36 | 115k | #define DIAMETER_ERROR 0X20 |
37 | 56.6k | #define DIAMETER_RETRASM 0X10 |
38 | | |
39 | | typedef enum { |
40 | | AC = 271, |
41 | | AS = 274, |
42 | | CC = 272, |
43 | | CE = 257, |
44 | | DW = 280, |
45 | | DP = 282, |
46 | | RA = 258, |
47 | | ST = 275 |
48 | | } com_type_t; |
49 | | |
50 | | #define DIAM_HEADER_LEN 20 |
51 | | |
52 | | // DIAMETER header |
53 | | struct diameter_header_t |
54 | | { |
55 | | u_int8_t version; |
56 | | u_int8_t length[3]; |
57 | | u_int8_t flags; |
58 | | u_int8_t com_code[3]; |
59 | | u_int32_t app_id; |
60 | | u_int32_t hop_id; |
61 | | u_int32_t end_id; |
62 | | }; |
63 | | |
64 | | |
65 | | // Check packet |
66 | | static int is_diameter(struct ndpi_packet_struct *packet) |
67 | 4.36M | { |
68 | 4.36M | struct diameter_header_t *diameter = (struct diameter_header_t *)packet->payload; |
69 | | |
70 | 4.36M | if(packet->payload_packet_len >= sizeof(struct diameter_header_t) && |
71 | 2.52M | diameter->version == 0x01 && |
72 | 58.2k | (diameter->flags == DIAMETER_REQUEST || |
73 | 57.8k | diameter->flags == DIAMETER_PROXYABLE || |
74 | 57.2k | diameter->flags == DIAMETER_ERROR || |
75 | 56.6k | diameter->flags == DIAMETER_RETRASM)) { |
76 | | |
77 | 1.97k | u_int32_t com_code = diameter->com_code[2] + (diameter->com_code[1] << 8) + (diameter->com_code[0] << 8); |
78 | | |
79 | 1.97k | if(com_code == AC || com_code == AS || |
80 | 1.85k | com_code == CC || com_code == CE || |
81 | 1.58k | com_code == DW || com_code == DP || |
82 | 1.41k | com_code == RA || com_code == ST) |
83 | 733 | return 0; |
84 | 1.97k | } |
85 | | |
86 | 4.36M | return -1; |
87 | 4.36M | } |
88 | | |
89 | | |
90 | | static void ndpi_search_diameter(struct ndpi_detection_module_struct *ndpi_struct, |
91 | | struct ndpi_flow_struct *flow) |
92 | 4.36M | { |
93 | 4.36M | struct ndpi_packet_struct *packet = &ndpi_struct->packet; |
94 | | |
95 | 4.36M | if(packet->tcp) { |
96 | 4.36M | int ret = is_diameter(packet); |
97 | 4.36M | if(ret == 0) { |
98 | 733 | NDPI_LOG_INFO(ndpi_struct, "found Diameter\n"); |
99 | 733 | ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_DIAMETER, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); |
100 | 733 | return; |
101 | 733 | } |
102 | 4.36M | } |
103 | | |
104 | 4.36M | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
105 | 4.36M | } |
106 | | |
107 | | |
108 | | void init_diameter_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
109 | 14.9k | { |
110 | 14.9k | ndpi_register_dissector("Diameter", ndpi_struct, |
111 | 14.9k | ndpi_search_diameter, |
112 | 14.9k | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
113 | 14.9k | 1, NDPI_PROTOCOL_DIAMETER); |
114 | 14.9k | } |
115 | | |