aboutsummaryrefslogtreecommitdiff
path: root/src/lib/protocols/quic.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/protocols/quic.c')
-rw-r--r--src/lib/protocols/quic.c229
1 files changed, 94 insertions, 135 deletions
diff --git a/src/lib/protocols/quic.c b/src/lib/protocols/quic.c
index 2bece25a7..8050a9b61 100644
--- a/src/lib/protocols/quic.c
+++ b/src/lib/protocols/quic.c
@@ -1,11 +1,12 @@
/*
* quic.c
*
+ * Copyright (C) 2012-16 - ntop.org
+ *
+ * Based on code of:
* Andrea Buscarinu - <andrea.buscarinu@gmail.com>
* Michele Campus - <campus@ntop.org>
*
- * Copyright (C) 2012-15 - ntop.org
- *
* This module is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@@ -21,164 +22,122 @@
*
*/
-
#include "ndpi_api.h"
-#define QUIC_NO_V_RES_RSV 0xC3 // 1100 0011
-
-#define QUIC_CID_MASK 0x0C // 0000 1100
-#define QUIC_VER_MASK 0x01 // 0000 0001
-#define QUIC_SEQ_MASK 0x30 // 0011 0000
-
-#define CID_LEN_8 0x0C // 0000 1100
-#define CID_LEN_4 0x08 // 0000 1000
-#define CID_LEN_1 0x04 // 0000 0100
-#define CID_LEN_0 0x00 // 0000 0000
-
-#define SEQ_LEN_6 0x30 // 0011 0000
-#define SEQ_LEN_4 0x20 // 0010 0000
-#define SEQ_LEN_2 0x10 // 0001 0000
-#define SEQ_LEN_1 0x00 // 0000 0000
-
-#define SEQ_CONV(ARR) (ARR[0] | ARR[1] | ARR[2] | ARR[3] | ARR[4] | ARR[5] << 8)
-
-
#ifdef NDPI_PROTOCOL_QUIC
-static void ndpi_int_quic_add_connection(struct ndpi_detection_module_struct
- *ndpi_struct, struct ndpi_flow_struct *flow)
+
+static int quic_ports(u_int16_t sport, u_int16_t dport)
{
- ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_QUIC, NDPI_PROTOCOL_UNKNOWN);
+ if ((sport == 443 || dport == 443 || sport == 80 || dport == 80) &&
+ (sport != 123 && dport != 123))
+ return 1;
+
+ return 0;
}
-static int connect_id(const unsigned char pflags)
-{
- u_int cid_len;
-
- // Check CID length.
- switch (pflags & QUIC_CID_MASK)
- {
- case CID_LEN_8: cid_len = 8; break;
- case CID_LEN_4: cid_len = 4; break;
- case CID_LEN_1: cid_len = 1; break;
- case CID_LEN_0: cid_len = 0; break;
- default:
- return -1;
-
- }
- // Return offset.
- return cid_len + 1;
+/* ***************************************************************** */
+
+static int quic_len(u_int8_t l) {
+ switch(l) {
+ case 0:
+ return(1);
+ break;
+ case 1:
+ return(2);
+ break;
+ case 2:
+ return(4);
+ break;
+ case 3:
+ return(8);
+ break;
+ }
+
+ return(0); /* NOTREACHED */
}
-static int sequence(const unsigned char *payload)
+/* ***************************************************************** */
+
+void ndpi_search_quic(struct ndpi_detection_module_struct *ndpi_struct,
+ struct ndpi_flow_struct *flow)
{
- unsigned char conv[6] = {0};
- u_int seq_value = -1;
- int seq_lens;
- int cid_offs;
+ struct ndpi_packet_struct *packet = &flow->packet;
+ u_int32_t udp_len = packet->payload_packet_len;
+ u_int version_len = ((packet->payload[0] & 0x01) == 0) ? 0 : 4;
+ u_int cid_len = quic_len((packet->payload[0] & 0x0C) >> 2);
+ u_int seq_len = quic_len((packet->payload[0] & 0x30) >> 4);
+ u_int quic_hlen = 1 /* flags */ + version_len + seq_len + cid_len;
+
+ if(packet->udp != NULL
+ && (udp_len > (quic_hlen+4 /* QXXX */))
+ && ((packet->payload[0] & 0xC2) == 0x00)
+ && (quic_ports(ntohs(packet->udp->source), ntohs(packet->udp->dest)))
+ ) {
+ char *begin;
int i;
- // Search SEQ bytes length.
- switch (payload[0] & QUIC_SEQ_MASK)
- {
- case SEQ_LEN_6: seq_lens = 6; break;
- case SEQ_LEN_4: seq_lens = 4; break;
- case SEQ_LEN_2: seq_lens = 2; break;
- case SEQ_LEN_1: seq_lens = 1; break;
- default:
- return -1;
- }
- // Retrieve SEQ offset.
- cid_offs = connect_id(payload[0]);
-
- if (cid_offs >= 0 && seq_lens > 0)
- {
- for (i = 0; i < seq_lens; i++)
- conv[i] = payload[cid_offs + i];
-
- seq_value = SEQ_CONV(conv);
- }
-
- // Return SEQ dec value;
- return seq_value;
-}
+ if((version_len > 0) && (packet->payload[1+cid_len] != 'Q'))
+ goto no_quic;
-void ndpi_search_quic(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
-{
- struct ndpi_packet_struct *packet = &flow->packet;
- int ver_offs;
-
- if(packet->udp != NULL) {
-
- u_int16_t sport = ntohs(packet->udp->source), dport = ntohs(packet->udp->dest);
-
- NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "calculating quic over udp.\n");
-
- if((((sport == 80) || (dport == 80) || (sport == 443) || (dport == 443))))
- {
- NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n");
- NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC);
-
-
- // Settings without version. First check if PUBLIC FLAGS & SEQ bytes are 0x0. SEQ must be 1 at least.
- if ((packet->payload[0] == 0x00 && packet->payload[1] != 0x00) || ((packet->payload[0] & QUIC_NO_V_RES_RSV) == 0))
- {
- if (sequence(packet->payload) < 1)
- {
-
- NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n");
- NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC);
- }
+ NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found QUIC.\n");
+ ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_QUIC, NDPI_PROTOCOL_UNKNOWN);
- NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found quic.\n");
- ndpi_int_quic_add_connection(ndpi_struct, flow);
- }
+ if(udp_len > quic_hlen + 17 + 4 &&
+ !strncmp((char*)&packet->payload[quic_hlen+17], "CHLO" /* Client Hello */, 4)) {
+ /* Check if SNI (Server Name Identification) is present */
+ for(i=quic_hlen+12; i<udp_len-3; i++) {
+ if((packet->payload[i] == 'S')
+ && (packet->payload[i+1] == 'N')
+ && (packet->payload[i+2] == 'I')
+ && (packet->payload[i+3] == 0)) {
+ u_int32_t offset = *((u_int32_t*)&packet->payload[i+4]);
+ u_int32_t prev_offset = *((u_int32_t*)&packet->payload[i-4]);
+ int len = offset-prev_offset;
+ int sni_offset = i+prev_offset+1;
+
+ while((sni_offset < udp_len) && (packet->payload[sni_offset] == '-'))
+ sni_offset++;
- // Check if version, than the CID length.
- else if (packet->payload[0] & QUIC_VER_MASK)
- {
- // Skip CID length.
- ver_offs = connect_id(packet->payload[0]);
-
- if (ver_offs >= 0)
- {
- unsigned char vers[] = {packet->payload[ver_offs], packet->payload[ver_offs + 1],
- packet->payload[ver_offs + 2], packet->payload[ver_offs + 3]};
-
- // Version Match.
- if ((vers[0] == 'Q' && vers[1] == '0') &&
- ((vers[2] == '2' && (vers[3] == '5' || vers[3] == '4' || vers[3] == '3' || vers[3] == '2' ||
- vers[3] == '1' || vers[3] == '0')) ||
- (vers[2] == '1' && (vers[3] == '9' || vers[3] == '8' || vers[3] == '7' || vers[3] == '6' ||
- vers[3] == '5' || vers[3] == '4' || vers[3] == '3' || vers[3] == '2' ||
- vers[3] == '1' || vers[3] == '0')) ||
- (vers[2] == '0' && vers[3] == '9')))
-
- {
- NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "found quic.\n");
- ndpi_int_quic_add_connection(ndpi_struct, flow);
+ if((sni_offset+len) < udp_len) {
+ int max_len = sizeof(flow->host_server_name)-1, j = 0;
+
+ if(len > max_len) len = max_len;
+
+ while((len > 0) && (sni_offset < udp_len)) {
+ flow->host_server_name[j++] = packet->payload[sni_offset];
+ sni_offset++, len--;
}
+
+ ndpi_match_host_subprotocol(ndpi_struct, flow,
+ (char *)flow->host_server_name,
+ strlen((const char*)flow->host_server_name),
+ NDPI_PROTOCOL_QUIC);
+
}
+
+ break;
}
- }
- else
- {
- NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude quic.\n");
- NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC);
}
}
+ return;
+ }
+
+ no_quic:
+ NDPI_LOG(NDPI_PROTOCOL_QUIC, ndpi_struct, NDPI_LOG_DEBUG, "exclude QUIC.\n");
+ NDPI_ADD_PROTOCOL_TO_BITMASK(flow->excluded_protocol_bitmask, NDPI_PROTOCOL_QUIC);
}
+/* ***************************************************************** */
-void init_quic_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask)
+void init_quic_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id,
+ NDPI_PROTOCOL_BITMASK *detection_bitmask)
{
- ndpi_set_bitmask_protocol_detection("Quic", ndpi_struct, detection_bitmask, *id,
- NDPI_PROTOCOL_QUIC,
- ndpi_search_quic,
+ ndpi_set_bitmask_protocol_detection("QUIC", ndpi_struct, detection_bitmask, *id,
+ NDPI_PROTOCOL_QUIC, ndpi_search_quic,
NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD,
- SAVE_DETECTION_BITMASK_AS_UNKNOWN,
- ADD_TO_DETECTION_BITMASK);
+ SAVE_DETECTION_BITMASK_AS_UNKNOWN, ADD_TO_DETECTION_BITMASK);
*id += 1;
}
-#endif
+#endif /* NDPI_PROTOCOL_QUIC */