This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
mesa-platform-quic/src/gquic_process.c

938 lines
30 KiB
C
Raw Normal View History

2020-05-12 12:18:02 +08:00
/*
2020-05-29 15:10:01 +08:00
* quic_process.c
2020-05-12 12:18:02 +08:00
*
* Created on: 2019<EFBFBD><EFBFBD>4<EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>
* Author: root
*/
2020-05-29 15:10:01 +08:00
2020-05-12 12:18:02 +08:00
#include <stdio.h>
2020-05-29 15:10:01 +08:00
#include <assert.h>
#include <stdbool.h>
#include <MESA/stream.h>
2020-05-22 18:44:00 +08:00
#include <MESA/MESA_handle_logger.h>
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
#include "gquic_process.h"
#include "quic_analysis.h"
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
int quic_getLinkState(struct _quic_context *_context)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
UCHAR state = 0;
if(0==_context->link_state)
{
state=SESSION_STATE_PENDING|SESSION_STATE_DATA;
}
else
{
state=SESSION_STATE_DATA;
}
return state;
}
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
char quic_callPlugins(struct streaminfo *pstream, struct _quic_context *_context, void *buff, int buff_len, enum quic_interested_region region_mask, void *a_packet)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
char state=PROT_STATE_GIVEME;
char app_state=APP_STATE_GIVEME;
stSessionInfo session_info={0};
2020-06-01 17:16:23 +08:00
if(region_mask==QUIC_INTEREST_KEY_MASK)
{
session_info.plugid=g_quic_param.quic_plugid;
session_info.prot_flag=0;
session_info.session_state=SESSION_STATE_CLOSE;
session_info.app_info=NULL;
session_info.buf=NULL;
session_info.buflen=0;
}
else
{
session_info.plugid=g_quic_param.quic_plugid;
session_info.prot_flag=(((unsigned long long)1)<<region_mask);
session_info.session_state=quic_getLinkState(_context) ;
session_info.app_info=(void*)(&_context->quic_info);
session_info.buf=buff;
session_info.buflen=buff_len;
}
2020-05-29 15:10:01 +08:00
state=PROT_PROCESS(&session_info, &(_context->business_pme), pstream->threadnum, pstream, a_packet);
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
if(state&PROT_STATE_DROPPKT)
{
app_state=APP_STATE_DROPPKT;
}
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
return app_state;
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
unsigned long long get_variable_length(char *p, int offset, int v_len)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
switch(v_len)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
case 1:
return (unsigned long long)(p[offset]);
2020-05-22 18:44:00 +08:00
break;
case 2:
2020-05-29 15:10:01 +08:00
return (unsigned long long)ntohs(*(unsigned short *)((char *)p+offset));
2020-05-22 18:44:00 +08:00
break;
case 3:
2020-05-29 15:10:01 +08:00
return (unsigned long long)*(p+0+offset)<<16|
(unsigned long long)*(p+1+offset)<<8|
(unsigned long long)*(p+2+offset)<<0;
break;
case 4:
return (unsigned long long)ntohl(*(unsigned int *)(p+offset));
break;
case 5:
return (unsigned long long)*((unsigned char *)(p)+0+offset)<<32|
(unsigned long long)*((unsigned char *)(p)+1+offset)<<24|
(unsigned long long)*((unsigned char *)(p)+2+offset)<<16|
(unsigned long long)*((unsigned char *)(p)+3+offset)<<8|
(unsigned long long)*((unsigned char *)(p)+4+offset)<<0;
break;
case 6:
return (unsigned long long)*((unsigned char *)(p)+0+offset)<<40|
(unsigned long long)*((unsigned char *)(p)+1+offset)<<32|
(unsigned long long)*((unsigned char *)(p)+2+offset)<<24|
(unsigned long long)*((unsigned char *)(p)+3+offset)<<16|
(unsigned long long)*((unsigned char *)(p)+4+offset)<<8|
(unsigned long long)*((unsigned char *)(p)+5+offset)<<0;
break;
case 7:
return (unsigned long long)*((unsigned char *)(p)+0+offset)<<56|
(unsigned long long)*((unsigned char *)(p)+1+offset)<<40|
(unsigned long long)*((unsigned char *)(p)+2+offset)<<32|
(unsigned long long)*((unsigned char *)(p)+3+offset)<<24|
(unsigned long long)*((unsigned char *)(p)+4+offset)<<16|
(unsigned long long)*((unsigned char *)(p)+5+offset)<<8;
(unsigned long long)*((unsigned char *)(p)+6+offset)<<0;
break;
case 8:
return (unsigned long long)*((unsigned char *)(p)+0+offset)<<56|
(unsigned long long)*((unsigned char *)(p)+1+offset)<<48|
(unsigned long long)*((unsigned char *)(p)+2+offset)<<40|
(unsigned long long)*((unsigned char *)(p)+3+offset)<<32|
(unsigned long long)*((unsigned char *)(p)+4+offset)<<24|
(unsigned long long)*((unsigned char *)(p)+5+offset)<<16|
(unsigned long long)*((unsigned char *)(p)+6+offset)<<8|
(unsigned long long)*((unsigned char *)(p)+7+offset)<<0;
2020-05-22 18:44:00 +08:00
break;
default:
break;
2020-05-12 12:18:02 +08:00
}
2020-05-29 15:10:01 +08:00
return 0;
}
2020-06-01 17:16:23 +08:00
long long bit_to_value(char *payload, unsigned char flags, unsigned long long *out_value, int *used_len)
{
switch(flags&0x3) // packet number
{
case 0x3: // 6 bytes
*out_value=get_variable_length(payload, *used_len, 6);
*used_len+=6;
break;
case 0x2: // 4 bytes
*out_value=(unsigned long long)ntohl(*(unsigned int *)(payload+*used_len));
*used_len+=4;
break;
case 0x1: // 2bytes
*out_value=(unsigned long long)ntohs(*(unsigned short *)(payload+*used_len));
*used_len+=2;
break;
default: // 1 byte
*out_value=payload[*used_len];
*used_len+=1;
break;
}
return 0;
}
2020-05-29 15:10:01 +08:00
int get_quic_tlv(char *start_pos, quic_tlv_t *tlv, int len, int type, int thread_seq)
{
if(tlv->value==NULL && len>0)
{
tlv->value=(char *)dictator_malloc(thread_seq, len+1);
memset(tlv->value, 0, len+1);
tlv->length=len;
tlv->type=type;
memcpy(tlv->value, start_pos, tlv->length);
}
return 0;
}
int get_stream_id(struct streaminfo *pstream, struct _quic_context* _context, char* payload, unsigned char frame_type, int *used_len)
{
int stream_len=0,offset_len=0;
_context->quic_info.frame_hdr.frame_type=frame_type;
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
stream_len=(frame_type&GQUIC_SPECIAL_FRAME_STREAM_ID)+1;
_context->quic_info.frame_hdr.stream_id=(unsigned int)get_variable_length(payload, *used_len, stream_len);
*used_len+=stream_len; // stream ID length
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
if(frame_type&GQUIC_SPECIAL_FRAME_STREAM_DLEN)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
_context->quic_info.frame_hdr.data_len=ntohs(*(unsigned short *)(payload+*used_len));
*used_len+=2; //data length
2020-05-12 12:18:02 +08:00
}
2020-05-22 18:44:00 +08:00
2020-06-01 17:16:23 +08:00
offset_len=(frame_type&GQUIC_SPECIAL_FRAME_STREAM_OFFSET) ? (((frame_type&GQUIC_SPECIAL_FRAME_STREAM_OFFSET))>>2)+1 : 0;
2020-05-29 15:10:01 +08:00
_context->quic_info.frame_hdr.offset=get_variable_length(payload, *used_len, offset_len);
*used_len+=offset_len;
2020-05-12 12:18:02 +08:00
2020-05-22 18:44:00 +08:00
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_FRAME",
2020-05-29 15:10:01 +08:00
"frame_type: 0X%02X stream_id: %u data length: %u offset length: %u addr: %s",
frame_type,
_context->quic_info.frame_hdr.stream_id,
_context->quic_info.frame_hdr.data_len,
offset_len,
2020-05-22 18:44:00 +08:00
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
return _context->quic_info.frame_hdr.stream_id;
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
unsigned long long get_packet_number(char* data, int offset, char pkn_len)
2020-05-22 18:44:00 +08:00
{
switch(pkn_len)
{
case 1:
2020-05-29 15:10:01 +08:00
return (unsigned long long)data[offset];
2020-05-22 18:44:00 +08:00
break;
case 2:
2020-05-29 15:10:01 +08:00
return (unsigned long long)ntohs(*(unsigned short *)(data+offset));
2020-05-22 18:44:00 +08:00
break;
case 4:
2020-05-29 15:10:01 +08:00
return (unsigned long long)ntohl(*(unsigned int *)(data+offset));
2020-05-22 18:44:00 +08:00
break;
2020-05-29 15:10:01 +08:00
case 8:
return get_variable_length(data, offset, 8);;
2020-05-22 18:44:00 +08:00
break;
}
return 0;
}
2020-05-29 15:10:01 +08:00
// GQUIC version from 0 to 43
static enum _QUIC_VERSION parse_q0to43_header(struct streaminfo *pstream, struct _quic_context* _context, char *payload, int payload_len, int *used_len)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
int i=0,len=0;
char public_flags=0;
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
struct _quic_public_header *gquic_hdr=&(_context->quic_info.quic_hdr);
public_flags=payload[*used_len];
*used_len+=1;
2020-06-01 17:16:23 +08:00
gquic_hdr->public_flags=public_flags;
if((public_flags&GQUIC_PUBLIC_FLAG_RST) && _context->is_quic==TRUE)
{
gquic_hdr->is_reset=TRUE; //Public Reset Packet
return QUIC_VERSION_UNKNOWN;
}
if(pstream->curdir==DIR_S2C && gquic_hdr->public_flags&GQUIC_PUBLIC_FLAG_VERSION)
{
#if 0
2020-06-01 17:16:23 +08:00
gquic_hdr->is_version_negotiation=TRUE; // Version Negotiation Packet
gquic_hdr->negotiation_version_num=(payload_len-*used_len)/sizeof(int);
gquic_hdr->negotiation_version_list=(unsigned int *)dictator_malloc(pstream->threadnum, payload_len-*used_len);
for(i=0; i<gquic_hdr->negotiation_version_num; i++)
{
gquic_hdr->negotiation_version_list[i]=*(unsigned int *)(payload+*used_len);
*used_len+=sizeof(unsigned int);
}
#endif
2020-06-01 17:16:23 +08:00
return QUIC_VERSION_UNKNOWN;
}
2020-05-29 15:10:01 +08:00
//For Public Reset and Version Negotiation Packets (sent by the server) which don't have a packet number
if(!public_flags&GQUIC_PUBLIC_FLAG_PKT_NUM)
{
if(public_flags&GQUIC_PUBLIC_FLAG_VERSION) //Public Reset Packet
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
return QUIC_VERSION_UNKNOWN;// todo
}
else // Version Negotiation Packet
{
return QUIC_VERSION_UNKNOWN;
2020-05-22 18:44:00 +08:00
}
}
2020-05-29 15:10:01 +08:00
if(gquic_hdr->public_flags&GQUIC_PUBLIC_FLAG_CID)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
*(unsigned long long *)gquic_hdr->server_CID=get_variable_length(payload, *used_len, sizeof(gquic_hdr->server_CID));
*used_len+=sizeof(unsigned long long); // CID length
2020-06-01 17:16:23 +08:00
_context->is_quic=TRUE;
2020-05-22 18:44:00 +08:00
}
2020-06-01 17:16:23 +08:00
if(gquic_hdr->public_flags&GQUIC_PUBLIC_FLAG_VERSION && (*(unsigned char *)(payload+*used_len)==0x51))
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
gquic_hdr->quic_version=(unsigned int)ntohl(*(unsigned int *)(payload+*used_len));
*used_len+=sizeof(int); // skip version
2020-06-01 17:16:23 +08:00
_context->is_quic=TRUE;
2020-05-22 18:44:00 +08:00
}
2020-06-01 17:16:23 +08:00
if(_context->is_quic==FALSE)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
return QUIC_VERSION_UNKNOWN;
2020-05-22 18:44:00 +08:00
}
2020-06-01 17:16:23 +08:00
bit_to_value(payload, gquic_hdr->public_flags>>4, &gquic_hdr->packet_number, used_len);
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
if(gquic_hdr->public_flags==GQUIC_PUBLIC_FLAG_NONCE)
{
*used_len+=32; //diversification nonce
}
2020-06-01 17:16:23 +08:00
// Version 11 reduced the length of null encryption authentication tag from 16 to 12 bytes
if(gquic_hdr->quic_version > GQUIC_VERSION_Q010)
{
*used_len+=12;
}
else
{
*used_len+=16;
}
2020-05-29 15:10:01 +08:00
2020-06-01 17:16:23 +08:00
// Version 34 removed entropy bits from packets and ACK frames,
// removed private flag from packet header and changed the ACK format to specify ranges of packets acknowledged rather than missing ranges.
if(gquic_hdr->quic_version < GQUIC_VERSION_Q034)
2020-05-29 15:10:01 +08:00
{
2020-06-01 17:16:23 +08:00
*used_len+=1; //private flags
2020-05-29 15:10:01 +08:00
}
2020-06-01 17:16:23 +08:00
_context->is_quic=TRUE;
2020-05-29 15:10:01 +08:00
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_IDETIFY",
"pub_flags: 0X%02X conection ID:[ destination: %llu ] version: Q%03u packet number: %llu dir(1: C2S;2: S2C): %d addr: %s",
gquic_hdr->public_flags,
*(unsigned long long *)gquic_hdr->server_CID,
(((gquic_hdr->quic_version>>8)&0x0000000F)*10) + ((gquic_hdr->quic_version)&0x0000000F),
gquic_hdr->packet_number,
pstream->curdir,
printaddr(&pstream->addr, pstream->threadnum));
return (enum _QUIC_VERSION)gquic_hdr->quic_version;
}
enum _QUIC_VERSION parse_quic_header(struct streaminfo *pstream, struct _quic_context* _context, char *payload, int payload_len, int *used_len)
{
int i=0,len=0;
char client_CID[MAX_CONNECT_ID_LEN*2]={0};
char server_CID[MAX_CONNECT_ID_LEN*2]={0};
struct _quic_public_header *long_hdr=&(_context->quic_info.quic_hdr);
long_hdr->public_flags=payload[*used_len];
*used_len+=1; //skip public flags
if(long_hdr->public_flags&0x80)
{
long_hdr->quic_version=(unsigned int)ntohl(*(unsigned int *)(payload+*used_len));
*used_len+=sizeof(int); // skip version
long_hdr->client_CID_len=(payload[*used_len]&0xF) ? (payload[*used_len]&0xF)+3 : 0;
long_hdr->server_CID_len=((payload[*used_len]>>4)&0xF) ? ((payload[*used_len]>>4)&0xF)+3 : 0;
*used_len+=sizeof(char); // both connection_id length
memcpy(long_hdr->server_CID, (void *)(payload+*used_len), long_hdr->server_CID_len);
*used_len+=long_hdr->server_CID_len; // Destination connection_id length
memcpy(long_hdr->client_CID, (void *)(payload+*used_len), long_hdr->client_CID_len);
*used_len+=long_hdr->client_CID_len; // source connection_id length
}
else
{
if(pstream->curdir==DIR_C2S)// short header only destination connection ID
{
*used_len+=long_hdr->server_CID_len; // every packet destination connection ID is same
}
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
len=(long_hdr->public_flags&0x03)+1;
long_hdr->packet_number=get_packet_number(payload, *used_len, len);
*used_len+=len;
*used_len+=12; //message authentication hash
2020-06-01 17:16:23 +08:00
_context->is_quic=TRUE;
2020-05-29 15:10:01 +08:00
for(i=0,len=0;i<long_hdr->server_CID_len; i++)
{
len+=snprintf(server_CID+len, sizeof(server_CID)-len, "%02X", long_hdr->server_CID[i]);
}
for(i=0,len=0;i<long_hdr->client_CID_len; i++)
{
len+=snprintf(client_CID+len, sizeof(client_CID)-len, "%02X", long_hdr->client_CID[i]);
}
2020-05-22 18:44:00 +08:00
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_IDETIFY",
2020-05-29 15:10:01 +08:00
"pub_flags: 0X%02X conection ID:[ destination: %s source: %s ] version: Q%03u packet number: %llu dir(1: C2S;2: S2C): %d addr: %s",
long_hdr->public_flags,
server_CID,
client_CID,
(((long_hdr->quic_version>>8)&0x0000000F)*10) + ((long_hdr->quic_version)&0x0000000F),
long_hdr->packet_number,
2020-05-22 18:44:00 +08:00
pstream->curdir,
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
return (enum _QUIC_VERSION)long_hdr->quic_version;
}
enum _QUIC_VERSION is_quic_protocol(struct streaminfo *pstream, struct _quic_context* _context, char *payload, int payload_len, int *used_len)
{
int i=0,len=0;
size_t s_id_len=0,d_id_len=0;
char d_CID[128]={0}, s_CID[128]={0};
unsigned char s_connection_id[MAX_CONNECT_ID_LEN]={0};
unsigned char d_connection_id[MAX_CONNECT_ID_LEN]={0};
enum _QUIC_VERSION quic_version=QUIC_VERSION_UNKNOWN;
if(_context->quic_info.quic_hdr.quic_version!=QUIC_VERSION_UNKNOWN)
{
//
quic_version=(enum _QUIC_VERSION)(_context->quic_info.quic_hdr.quic_version);
}
else
{
// The most significant bit (0x80) of byte 0 (the first byte) is set to 1 for long headers
(payload[*used_len]&0x80) ? (quic_version=(enum _QUIC_VERSION)ntohl(*(unsigned int *)(payload+(*used_len+1)))) : QUIC_VERSION_UNKNOWN;
}
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
switch(quic_version) // +1 meaning: skip public flags
{
case GQUIC_VERSION_Q046:
quic_version=parse_quic_header(pstream, _context, payload, payload_len, used_len);
return quic_version;
break;
case GQUIC_VERSION_Q044:
case GQUIC_VERSION_Q045:
case GQUIC_VERSION_Q048:
case GQUIC_VERSION_Q049:
case GQUIC_VERSION_Q050:
case GQUIC_VERSION_Q099:
case IQUIC_VERSION_I001:
case IQUIC_VERSION_I002:
case IQUIC_VERSION_I003:
case IQUIC_VERSION_I004:
case IQUIC_VERSION_I005:
case IQUIC_VERSION_I006:
case IQUIC_VERSION_I007:
case IQUIC_VERSION_I008:
case IQUIC_VERSION_I009:
case IQUIC_VERSION_I010:
case IQUIC_VERSION_I011:
case IQUIC_VERSION_I012:
case IQUIC_VERSION_I013:
case IQUIC_VERSION_I014:
case IQUIC_VERSION_I015:
case IQUIC_VERSION_I016:
case IQUIC_VERSION_I017:
case IQUIC_VERSION_I018:
case IQUIC_VERSION_I019:
case IQUIC_VERSION_I020:
case IQUIC_VERSION_I021:
case IQUIC_VERSION_I032:
case IQUIC_VERSION_I023:
case IQUIC_VERSION_I024:
case IQUIC_VERSION_I025:
case IQUIC_VERSION_I026:
case IQUIC_VERSION_I027:
case IQUIC_VERSION_I028:
case IQUIC_VERSION_I029:
MESA_handle_runtime_log(g_quic_param.logger, RLOG_LV_DEBUG, "QUIC_UNSUPPORT", "version: Q%03u addr: %s",
(((quic_version>>8)&0x0000000F)*10) + ((quic_version)&0x0000000F),
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
break;
default:
2020-05-29 15:10:01 +08:00
break;
}
// Q001~Q043: 0x80 is currently unused, and must be set to 0
if(payload[*used_len]>0x80)
{
return QUIC_VERSION_UNKNOWN;
}
return parse_q0to43_header(pstream, _context, payload, payload_len, used_len);
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
int parse_extension_tag(struct streaminfo *pstream, struct _quic_stream **quic_stream, void *a_packet, char *payload, int payload_len, int *used_len, int tag_num)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
int ret=0,tag_used_num=0;
int tag_type,skip_tsg=0;
int total_tag_len=0,tag_len=0;
int tag_offset_end=0,pre_tag_offset_end=0;
struct _quic_stream *stream=*quic_stream;
int tag_value_start=tag_num*4*2+(*used_len); // skip length of type and offset, type(offset)=szieof(int)
if(stream==NULL)
{
stream=(struct _quic_stream *)dictator_malloc(pstream->threadnum, sizeof(struct _quic_stream));
memset(stream, 0, sizeof(struct _quic_stream));
stream->ext_tags=(quic_tlv_t *)dictator_malloc(pstream->threadnum, tag_num*sizeof(quic_tlv_t));
memset(stream->ext_tags, 0, tag_num*sizeof(quic_tlv_t));
*quic_stream=stream;
}
else
{
quic_release_exts(pstream->threadnum, stream->ext_tags, stream->ext_tag_num);
stream->ext_tags=(quic_tlv_t *)dictator_malloc(pstream->threadnum, tag_num*sizeof(quic_tlv_t));
memset(stream->ext_tags, 0, tag_num*sizeof(quic_tlv_t));
*quic_stream=stream;
stream->ext_tag_num=0;
stream->count++;
}
while(tag_num>tag_used_num)
{
tag_type=ntohl(*(unsigned int *)(payload+*used_len));
*used_len+=sizeof(int);
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
tag_offset_end=*(unsigned int *)(payload+*used_len);
*used_len+=sizeof(int);
2020-05-22 18:44:00 +08:00
tag_len=tag_offset_end-pre_tag_offset_end;
2020-05-29 15:10:01 +08:00
if(tag_len<0 || (tag_offset_end>=payload_len) || (tag_len>payload_len-tag_value_start))
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
return -1;
2020-05-22 18:44:00 +08:00
}
switch(tag_type)
{
case TAG_PAD:
2020-05-12 12:18:02 +08:00
break;
2020-05-29 15:10:01 +08:00
case TAG_VER:
stream->ver_idx=stream->ext_tag_num;
get_quic_tlv(payload+tag_value_start, &stream->ext_tags[stream->ext_tag_num], tag_len, tag_type, pstream->threadnum);
*(unsigned int *)(stream->ext_tags[stream->ext_tag_num].value)=(unsigned int)ntohl(*(unsigned int *)(stream->ext_tags[stream->ext_tag_num].value));
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_VERSION",
"Quic version: 0X%X addr: %s",
*(unsigned int *)(stream->ext_tags[stream->ext_tag_num].value),
printaddr(&pstream->addr, pstream->threadnum));
2020-05-12 12:18:02 +08:00
break;
2020-05-29 15:10:01 +08:00
case TAG_UAID:
stream->ua_idx=stream->ext_tag_num;
get_quic_tlv(payload+tag_value_start, &stream->ext_tags[stream->ext_tag_num], tag_len, tag_type, pstream->threadnum);
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_UA",
"User Agent: %s addr: %s",
stream->ext_tags[stream->ext_tag_num].value,
printaddr(&pstream->addr, pstream->threadnum));
stream->ext_tag_num++;
2020-05-12 12:18:02 +08:00
break;
2020-05-22 18:44:00 +08:00
case TAG_SNI:
2020-05-29 15:10:01 +08:00
stream->sni_idx=stream->ext_tag_num;
get_quic_tlv(payload+tag_value_start, &stream->ext_tags[stream->ext_tag_num], tag_len, tag_type, pstream->threadnum);
MESA_handle_runtime_log(g_quic_param.logger,
2020-05-22 18:44:00 +08:00
RLOG_LV_DEBUG,
"QUIC_SNI",
"SNI: %s addr: %s",
2020-05-29 15:10:01 +08:00
stream->ext_tags[stream->ext_tag_num].value,
2020-05-22 18:44:00 +08:00
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
stream->ext_tag_num++;
2020-05-22 18:44:00 +08:00
break;
default:
2020-05-29 15:10:01 +08:00
get_quic_tlv(payload+tag_value_start, &stream->ext_tags[stream->ext_tag_num], tag_len, tag_type, pstream->threadnum);
stream->ext_tag_num++;
2020-05-22 18:44:00 +08:00
break;
2020-05-12 12:18:02 +08:00
}
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
tag_used_num++;
2020-05-22 18:44:00 +08:00
tag_value_start+=tag_len;
total_tag_len+=tag_len;
pre_tag_offset_end=tag_offset_end;
}
*used_len += total_tag_len;
2020-05-29 15:10:01 +08:00
return ret;
2020-05-12 12:18:02 +08:00
}
2020-06-01 17:16:23 +08:00
int gquic_frame_type_ack(struct streaminfo *pstream, struct _quic_context* _context, char *payload, int payload_len, int *used_len, char frame_type, void *a_packet)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
unsigned char num_timestamp;
unsigned char num_blocks=0,gap_to_next_block=0;;
unsigned short largest_acked_delta_time=0;
unsigned long long ack_block_length=0;
unsigned long long largest_observed_ack=0;
bit_to_value(payload, frame_type>>2, &largest_observed_ack, used_len); // frame_type -> LL
largest_acked_delta_time=ntohs(*(unsigned short *)(payload+*used_len));
*used_len+=sizeof(unsigned short);
if(frame_type&0x20) // frame_type -> n
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
num_blocks=(*(unsigned char *)(payload+*used_len))-1;
2020-05-22 18:44:00 +08:00
}
2020-06-01 17:16:23 +08:00
bit_to_value(payload, frame_type>>2, &ack_block_length, used_len); // frame_type -> mm
2020-05-22 18:44:00 +08:00
2020-06-01 17:16:23 +08:00
if(num_blocks>0)
{
gap_to_next_block=*(unsigned char *)(payload+*used_len);
*used_len+=1;
2020-05-22 18:44:00 +08:00
2020-06-01 17:16:23 +08:00
*used_len+=(num_blocks*sizeof(unsigned int)); //Ack block length
}
num_timestamp=*(unsigned char *)(payload+*used_len);
*used_len += 1;
2020-05-22 18:44:00 +08:00
2020-06-01 17:16:23 +08:00
if(num_timestamp > 0)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
*used_len+=sizeof(unsigned char); //Delta Largest Observed
*used_len+= sizeof(unsigned int); //First Timestamp
*used_len+=(num_timestamp-1)*(1+2); //1+2=Delta Largest Observed+Time Since Previous Timestamp
2020-05-22 18:44:00 +08:00
}
2020-06-01 17:16:23 +08:00
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_ACK",
"largest_observed_ack: %llu largest_acked_delta_time: %u num_blocks: %d ack_block_length: %d num_timestamp: %d addr: %s",
largest_observed_ack,
largest_acked_delta_time,
num_blocks,
ack_block_length,
num_timestamp,
printaddr(&pstream->addr, pstream->threadnum));
return 0;
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
int gquic_frame_type_stream(struct streaminfo *pstream, struct _quic_context* _context, char *payload, int payload_len, int *used_len, char frame_type, void *a_packet)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
int ret=0;
unsigned short tag_num = 0;
unsigned int stream_id, message_tag;
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
stream_id=get_stream_id(pstream, _context, payload, frame_type, used_len);
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
message_tag=(unsigned int)ntohl(*(unsigned int *)(payload+*used_len));
*used_len+=4;
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
tag_num=*(unsigned short *)(payload+*used_len);
*used_len+=2; //tag_num
*used_len+=2; //padding
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
switch(message_tag)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
case CHLO: //MTAG_CHLO;
ret=parse_extension_tag(pstream, &(_context->quic_info.client_hello), a_packet, payload, payload_len, used_len, tag_num);
2020-06-01 17:44:32 +08:00
if(ret>=0 && _context->call_business)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
ret=quic_callPlugins(pstream, _context, (void *)(_context->quic_info.client_hello), sizeof(void *), QUIC_CLIENT_HELLO_MASK, a_packet);
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
break;
case SHLO: //MTAG_SHLO;
ret=parse_extension_tag(pstream, &(_context->quic_info.server_hello), a_packet, payload, payload_len, used_len, tag_num);
2020-06-01 17:44:32 +08:00
if(ret>=0 && _context->call_business)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
ret=quic_callPlugins(pstream, _context, (void *)(_context->quic_info.server_hello), sizeof(void *), QUIC_SERVER_HELLO_MASK, a_packet);
2020-05-22 18:44:00 +08:00
}
2020-05-29 15:10:01 +08:00
break;
case REJ: //MTAG_REJ;
ret=parse_extension_tag(pstream, &(_context->quic_info.rejection), a_packet, payload, payload_len, used_len, tag_num);
2020-06-01 17:44:32 +08:00
if(ret>=0 && _context->call_business)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
ret=quic_callPlugins(pstream, _context, (void *)(_context->quic_info.rejection), sizeof(void *), QUIC_REJECTION_MASK, a_packet);
2020-05-12 12:18:02 +08:00
}
2020-05-29 15:10:01 +08:00
break;
default:
break;
2020-05-12 12:18:02 +08:00
}
return ret;
2020-05-29 15:10:01 +08:00
}
2020-05-12 12:18:02 +08:00
2020-05-29 15:10:01 +08:00
//frame type->stream->offset->data length
int gquic_proc_unencrypt(struct streaminfo *pstream, struct _quic_context* _context, void *a_packet, char * payload, int payload_len, int *used_len)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
unsigned int ret=0;
2020-06-01 17:16:23 +08:00
unsigned char frame_type=0;
unsigned int stream_id=0;
unsigned int error_code=0;
unsigned short reason_phrase_length=0;
unsigned long long byte_offset=0;
unsigned long long least_unacked_delta=0;
2020-05-29 15:10:01 +08:00
while(*used_len<payload_len)
{
frame_type=payload[*used_len];
*used_len+=1; //skip frame_type
switch(frame_type)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
case GQUIC_REGULAR_FRAME_PADDING:
2020-06-01 17:16:23 +08:00
return APP_STATE_GIVEME; // PADDING frame
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_RST_STREAM:
2020-06-01 17:16:23 +08:00
stream_id=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
byte_offset=get_variable_length(payload, *used_len, sizeof(unsigned long long));
*used_len+=sizeof(unsigned long long);
error_code=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_RST_STREAM",
"stream_id: %u byte_offset: %llu error_code: %u addr: %s",
stream_id,
byte_offset,
error_code,
printaddr(&pstream->addr, pstream->threadnum));
return quic_callPlugins(pstream, _context, NULL, 0, QUIC_INTEREST_KEY_MASK, a_packet);
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_CONNECTION_CLOSE:
2020-06-01 17:16:23 +08:00
error_code=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
reason_phrase_length=(unsigned short)get_variable_length(payload, *used_len, sizeof(unsigned short));
*used_len+=sizeof(unsigned short);
*used_len+=reason_phrase_length; // skip Reason Phrase
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_CONNECTION_CLOSE",
"error_code: %u reason_phrase_length: %d addr: %s",
error_code,
reason_phrase_length,
printaddr(&pstream->addr, pstream->threadnum));
return quic_callPlugins(pstream, _context, NULL, 0, QUIC_INTEREST_KEY_MASK, a_packet);
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_GOAWAY:
2020-06-01 17:16:23 +08:00
error_code=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
//Last Good Stream ID
stream_id=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
reason_phrase_length=(unsigned short)get_variable_length(payload, *used_len, sizeof(unsigned short));
*used_len+=sizeof(unsigned short);
*used_len+=reason_phrase_length; // skip Reason Phrase
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_GOAWAY",
"error_code: %u Last Good Stream ID: %u reason_phrase_length: %d addr: %s",
error_code,
stream_id,
reason_phrase_length,
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_WINDOW_UPDATE:
2020-06-01 17:16:23 +08:00
stream_id=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
byte_offset=get_variable_length(payload, *used_len, sizeof(unsigned long long));
*used_len+=sizeof(unsigned long long);
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_WINDOW_UPDATE",
"stream_id: %u byte_offset: %llu addr: %s",
stream_id,
byte_offset,
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_BLOCKED:
2020-06-01 17:16:23 +08:00
stream_id=(unsigned int)get_variable_length(payload, *used_len, sizeof(unsigned int));
*used_len+=sizeof(unsigned int);
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_STOP_WAITING",
"stream_id: %u addr: %s",
stream_id,
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_STOP_WAITING:
2020-06-01 17:16:23 +08:00
bit_to_value(payload, _context->quic_info.quic_hdr.public_flags>>4, &least_unacked_delta, used_len);
MESA_handle_runtime_log(g_quic_param.logger,
RLOG_LV_DEBUG,
"QUIC_STOP_WAITING",
"least_unacked_delta: %llu addr: %s",
least_unacked_delta,
printaddr(&pstream->addr, pstream->threadnum));
2020-05-29 15:10:01 +08:00
break;
case GQUIC_REGULAR_FRAME_PING:
2020-06-01 17:16:23 +08:00
//The PING frame contains no payload.
//The receiver of a PING frame simply needs to ACK the packet containing this frame
2020-05-29 15:10:01 +08:00
break;
default: //Regular Frame Types
if(frame_type&GQUIC_SPECIAL_FRAME_STREAM)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
ret=gquic_frame_type_stream(pstream, _context, payload, payload_len, used_len, frame_type, a_packet);
2020-05-12 12:18:02 +08:00
}
2020-06-01 17:16:23 +08:00
else if((frame_type&0xC0)==GQUIC_SPECIAL_FRAME_ACK) // high bit set 0; (frame_type: 01nullmmB)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
ret=gquic_frame_type_stream(pstream, _context, payload, payload_len, used_len, frame_type, a_packet);
2020-05-12 12:18:02 +08:00
}
2020-06-01 17:16:23 +08:00
else if((frame_type&0xE0)==GQUIC_SPECIAL_FRAME_CONGEST_FB) // high two bits set 0; (frame_type: 01nullmmB)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
//not used
2020-05-12 12:18:02 +08:00
}
2020-05-29 15:10:01 +08:00
else
{
2020-09-16 19:28:15 +08:00
return APP_STATE_GIVEME;
2020-05-29 15:10:01 +08:00
}
break;
2020-05-12 12:18:02 +08:00
}
2020-06-01 17:16:23 +08:00
if(ret&APP_STATE_DROPME || ret&APP_STATE_DROPPKT)
{
return ret;
}
2020-05-12 12:18:02 +08:00
}
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
return APP_STATE_GIVEME;
2020-05-12 12:18:02 +08:00
}
2020-05-29 15:10:01 +08:00
//QUIC_DATA:is quic data pcap;QUIC_TRUE:is handshake pcap;QUIC_RETURN_DROPME:not quic protocol;
int parse_gquic_Q046(struct streaminfo *pstream, struct _quic_context* _context, void *a_packet, char * payload, int payload_len, int *used_len)
2020-05-22 18:44:00 +08:00
{
int ret=APP_STATE_GIVEME;
2020-05-29 15:10:01 +08:00
unsigned char frame_type;
unsigned short tag_num=0;
unsigned int stream_id, message_tag;
2020-05-22 18:44:00 +08:00
while(*used_len < payload_len)
{
2020-05-29 15:10:01 +08:00
frame_type=payload[*used_len];
*used_len+=1; //skip frame_type
if(frame_type&IQUIC_FRAME_STREAM_HEX08)
2020-05-22 18:44:00 +08:00
{
ret=gquic_frame_type_stream(pstream, _context, payload, payload_len, used_len, frame_type, a_packet);
2020-05-29 15:10:01 +08:00
}
else
{
2020-09-16 19:28:15 +08:00
return APP_STATE_GIVEME; //todo
2020-05-12 12:18:02 +08:00
}
if(ret&APP_STATE_DROPME || ret&APP_STATE_DROPPKT)
{
return ret;
}
2020-05-12 12:18:02 +08:00
}
2020-05-22 18:44:00 +08:00
return APP_STATE_GIVEME;
2020-05-12 12:18:02 +08:00
}
//cid->version->nounce->pkt num->ahn hash(12)
2020-05-29 15:10:01 +08:00
int quic_process(struct streaminfo *pstream, struct _quic_context* _context, int thread_seq, void* a_packet)
2020-05-22 18:44:00 +08:00
{
2020-05-29 15:10:01 +08:00
int used_len=0;
int ret=APP_STATE_GIVEME;
enum _QUIC_VERSION is_gquic=QUIC_VERSION_UNKNOWN;
struct udpdetail *udp_detail=pstream->pudpdetail;
2020-05-22 18:44:00 +08:00
if(udp_detail->pdata==NULL || udp_detail->datalen<=0)
{
2020-05-29 15:10:01 +08:00
return APP_STATE_GIVEME;
2020-05-12 12:18:02 +08:00
}
2020-05-29 15:10:01 +08:00
is_gquic=is_quic_protocol(pstream, _context, (char *)udp_detail->pdata, udp_detail->datalen, &used_len);
if(is_gquic!=QUIC_VERSION_UNKNOWN)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:44:32 +08:00
if(_context->quic_info.quic_hdr.packet_number==1 && _context->call_business)
2020-05-29 15:10:01 +08:00
{
2020-06-01 17:16:23 +08:00
ret=quic_callPlugins(pstream, _context, &(_context->quic_info.quic_hdr.quic_version), sizeof(_context->quic_info.quic_hdr.quic_version), QUIC_USEING_VERSION_MASK, a_packet);
2020-05-29 15:10:01 +08:00
if(ret&APP_STATE_DROPME | ret&APP_STATE_DROPPKT)
{
return ret;
}
}
2020-05-22 18:44:00 +08:00
switch(is_gquic)
{
2020-05-29 15:10:01 +08:00
case GQUIC_VERSION_Q043:
ret=gquic_proc_unencrypt(pstream, _context, a_packet, (char *)udp_detail->pdata, udp_detail->datalen, &used_len);
2020-05-22 18:44:00 +08:00
break;
2020-05-29 15:10:01 +08:00
case GQUIC_VERSION_Q046:
ret=parse_gquic_Q046(pstream, _context, a_packet, (char *)udp_detail->pdata, udp_detail->datalen, &used_len);
2020-05-22 18:44:00 +08:00
break;
default:
break;
2020-05-12 12:18:02 +08:00
}
if(ret&APP_STATE_DROPME | ret&APP_STATE_DROPPKT)
{
return ret;
}
2020-05-12 12:18:02 +08:00
}
2020-06-01 17:16:23 +08:00
if(_context->is_quic==TRUE)
2020-05-22 18:44:00 +08:00
{
2020-06-01 17:16:23 +08:00
if(_context->quic_info.quic_hdr.is_reset)
{
return quic_callPlugins(pstream, _context, NULL, 0, QUIC_INTEREST_KEY_MASK, a_packet);
}
if(_context->quic_info.quic_hdr.is_version_negotiation)
{
return quic_callPlugins(pstream, _context, NULL, 0, QUIC_NEGOTIATION_VERSION_MASK, a_packet);
}
2020-05-29 15:10:01 +08:00
return APP_STATE_GIVEME;
2020-05-12 12:18:02 +08:00
}
2020-05-22 18:44:00 +08:00
2020-05-29 15:10:01 +08:00
return APP_STATE_DROPME;;
2020-05-12 12:18:02 +08:00
}
2020-05-22 18:44:00 +08:00
int quic_protocol_identify(struct streaminfo *a_stream, void *a_packet, char *out_sni, int out_sni_len)
{
int ret=APP_STATE_GIVEME;
int sni_len=0,len=0;
void *pme=NULL;
char *sni=NULL;
struct _quic_context *_context=NULL;
quic_init_stream(&pme, a_stream->threadnum);
_context=(struct _quic_context *)pme;
ret=quic_process(a_stream, _context, a_stream->threadnum, a_packet);
if(ret!=PROT_STATE_DROPME)
{
if(_context->quic_info.client_hello!=NULL)
{
sni=(char *)(_context->quic_info.client_hello->ext_tags[_context->quic_info.client_hello->sni_idx].value);
sni_len=_context->quic_info.client_hello->ext_tags[_context->quic_info.client_hello->sni_idx].length;
len= sni_len>(out_sni_len-1) ? (out_sni_len-1) : sni_len;
memcpy(out_sni, sni, len);
}
}
quic_release_stream(&pme, a_stream->threadnum);
return len;
}