/src/ndpi/src/lib/protocols/radius_proto.c
Line | Count | Source |
1 | | /* |
2 | | * radius_proto.c |
3 | | * |
4 | | * Copyright (C) 2012-24 - 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_RADIUS |
24 | | |
25 | | #include "ndpi_api.h" |
26 | | #include "ndpi_private.h" |
27 | | |
28 | | #define RADIUS_PORT 1812 |
29 | | #define RADIUS_PORT_ACC 1813 |
30 | | #define RADIUS_PORT_ACC_ALTERNATIVE 18013 |
31 | | |
32 | | |
33 | | struct radius_header { |
34 | | u_int8_t code; |
35 | | u_int8_t packet_id; |
36 | | u_int16_t len; |
37 | | }; |
38 | | |
39 | | static void ndpi_check_radius(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) |
40 | 978k | { |
41 | 978k | struct ndpi_packet_struct *packet = &ndpi_struct->packet; |
42 | | // const u_int8_t *packet_payload = packet->payload; |
43 | 978k | u_int32_t payload_len = packet->payload_packet_len; |
44 | | |
45 | 978k | if((packet->udp->dest == htons(RADIUS_PORT) || packet->udp->source == htons(RADIUS_PORT) || |
46 | 978k | packet->udp->dest == htons(RADIUS_PORT_ACC) || packet->udp->source == htons(RADIUS_PORT_ACC) || |
47 | 978k | packet->udp->dest == htons(RADIUS_PORT_ACC_ALTERNATIVE) || packet->udp->source == htons(RADIUS_PORT_ACC_ALTERNATIVE))) { |
48 | 4.60k | struct radius_header *h = (struct radius_header*)packet->payload; |
49 | | /* RFC2865: The minimum length is 20 and maximum length is 4096. */ |
50 | 4.60k | if((payload_len < 20) || (payload_len > 4096)) { |
51 | 255 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
52 | 255 | return; |
53 | 255 | } |
54 | | |
55 | 4.34k | if((h->code > 0) |
56 | 4.34k | && (h->code <= 13) |
57 | 4.34k | && (ntohs(h->len) == payload_len)) { |
58 | 1.44k | NDPI_LOG_INFO(ndpi_struct, "Found radius\n"); |
59 | 1.44k | ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_RADIUS, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); |
60 | 1.44k | return; |
61 | 1.44k | } |
62 | 4.34k | } |
63 | 976k | if(flow->packet_counter > 3) |
64 | 72.1k | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
65 | 976k | return; |
66 | 978k | } |
67 | | |
68 | | static void ndpi_search_radius(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) |
69 | 978k | { |
70 | 978k | NDPI_LOG_DBG(ndpi_struct, "search radius\n"); |
71 | | |
72 | 978k | ndpi_check_radius(ndpi_struct, flow); |
73 | 978k | } |
74 | | |
75 | | |
76 | | void init_radius_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
77 | 14.7k | { |
78 | 14.7k | register_dissector("Radius", ndpi_struct, |
79 | 14.7k | ndpi_search_radius, |
80 | 14.7k | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD, |
81 | 14.7k | 1, NDPI_PROTOCOL_RADIUS); |
82 | 14.7k | } |