Export tuple.h / packet.h / session.h to the include directory
This commit is contained in:
@@ -236,8 +236,8 @@ static inline bool before(uint32_t seq1, uint32_t seq2)
|
||||
|
||||
static void tcp_clean(struct session *sess)
|
||||
{
|
||||
tcp_reassembly_free(sess->tcp_halfs[SESSION_DIR_C2S].assembler);
|
||||
tcp_reassembly_free(sess->tcp_halfs[SESSION_DIR_S2C].assembler);
|
||||
tcp_reassembly_free(sess->tcp_halfs[SESSION_DIRECTION_C2S].assembler);
|
||||
tcp_reassembly_free(sess->tcp_halfs[SESSION_DIRECTION_S2C].assembler);
|
||||
}
|
||||
|
||||
static int tcp_init(struct session *sess, uint8_t tcp_reassembly_enable, uint64_t tcp_reassembly_max_timeout, uint64_t tcp_reassembly_max_segments)
|
||||
@@ -247,9 +247,9 @@ static int tcp_init(struct session *sess, uint8_t tcp_reassembly_enable, uint64_
|
||||
return 0;
|
||||
}
|
||||
|
||||
sess->tcp_halfs[SESSION_DIR_C2S].assembler = tcp_reassembly_new(tcp_reassembly_max_timeout, tcp_reassembly_max_segments);
|
||||
sess->tcp_halfs[SESSION_DIR_S2C].assembler = tcp_reassembly_new(tcp_reassembly_max_timeout, tcp_reassembly_max_segments);
|
||||
if (sess->tcp_halfs[SESSION_DIR_C2S].assembler == NULL || sess->tcp_halfs[SESSION_DIR_S2C].assembler == NULL)
|
||||
sess->tcp_halfs[SESSION_DIRECTION_C2S].assembler = tcp_reassembly_new(tcp_reassembly_max_timeout, tcp_reassembly_max_segments);
|
||||
sess->tcp_halfs[SESSION_DIRECTION_S2C].assembler = tcp_reassembly_new(tcp_reassembly_max_timeout, tcp_reassembly_max_segments);
|
||||
if (sess->tcp_halfs[SESSION_DIRECTION_C2S].assembler == NULL || sess->tcp_halfs[SESSION_DIRECTION_S2C].assembler == NULL)
|
||||
{
|
||||
tcp_clean(sess);
|
||||
return -1;
|
||||
@@ -258,7 +258,7 @@ static int tcp_init(struct session *sess, uint8_t tcp_reassembly_enable, uint64_
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void tcp_update(struct session_manager *mgr, struct session *sess, enum session_dir dir, const struct pkt_layer *tcp_layer, uint64_t now)
|
||||
static void tcp_update(struct session_manager *mgr, struct session *sess, enum session_direction dir, const struct packet_layer *tcp_layer, uint64_t now)
|
||||
{
|
||||
struct tcp_segment *seg;
|
||||
struct tcphdr *hdr = (struct tcphdr *)tcp_layer->hdr_ptr;
|
||||
@@ -372,33 +372,33 @@ static void tcp_update(struct session_manager *mgr, struct session *sess, enum s
|
||||
* Session Direction
|
||||
******************************************************************************/
|
||||
|
||||
static enum session_dir identify_direction_by_port(uint16_t src_port, uint16_t dst_port)
|
||||
static enum session_direction identify_direction_by_port(uint16_t src_port, uint16_t dst_port)
|
||||
{
|
||||
// big port is client
|
||||
if (src_port > dst_port)
|
||||
{
|
||||
return SESSION_DIR_C2S;
|
||||
return SESSION_DIRECTION_C2S;
|
||||
}
|
||||
else if (src_port < dst_port)
|
||||
{
|
||||
return SESSION_DIR_S2C;
|
||||
return SESSION_DIRECTION_S2C;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if port is equal, first packet is C2S
|
||||
return SESSION_DIR_C2S;
|
||||
return SESSION_DIRECTION_C2S;
|
||||
}
|
||||
}
|
||||
|
||||
static enum session_dir identify_direction_by_history(const struct session *sess, const struct tuple6 *key)
|
||||
static enum session_direction identify_direction_by_history(const struct session *sess, const struct tuple6 *key)
|
||||
{
|
||||
if (tuple6_cmp(session_get_tuple(sess), key) == 0)
|
||||
{
|
||||
return session_get_tuple_dir(sess);
|
||||
return session_get_tuple_direction(sess);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (session_get_tuple_dir(sess) == SESSION_DIR_C2S ? SESSION_DIR_S2C : SESSION_DIR_C2S);
|
||||
return (session_get_tuple_direction(sess) == SESSION_DIRECTION_C2S ? SESSION_DIRECTION_S2C : SESSION_DIRECTION_C2S);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -443,11 +443,13 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum session_dir dir = identify_direction_by_history(sess, key);
|
||||
enum session_direction dir = identify_direction_by_history(sess, key);
|
||||
if (session_get_stat(sess, dir, STAT_RAW_PKTS_RX) < 3 || session_has_dup_traffic(sess))
|
||||
{
|
||||
if (duplicated_packet_filter_lookup(mgr->dup_pkt_filter, pkt, now))
|
||||
{
|
||||
session_inc_stat(sess, dir, STAT_DUP_PKTS_BYPASS, 1);
|
||||
session_inc_stat(sess, dir, STAT_DUP_BYTES_BYPASS, packet_get_len(pkt));
|
||||
mgr->stat.nr_tcp_pkts_bypass_hit_dup++;
|
||||
session_set_dup_traffic(sess);
|
||||
return 1;
|
||||
@@ -466,13 +468,15 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session
|
||||
* Session Manager
|
||||
******************************************************************************/
|
||||
|
||||
static void session_update(struct session *sess, enum session_state next_state, const struct packet *pkt, const struct tuple6 *key, enum session_dir dir, uint64_t now)
|
||||
static void session_update(struct session *sess, enum session_state next_state, const struct packet *pkt, const struct tuple6 *key, enum session_direction dir, uint64_t now)
|
||||
{
|
||||
if (session_get_state(sess) == SESSION_STATE_INIT)
|
||||
{
|
||||
session_set_id(sess, id_generator_alloc());
|
||||
session_set_tuple(sess, key);
|
||||
session_set_tuple_dir(sess, dir);
|
||||
session_set_timestamp(sess, SESSION_TIMESTAMP_NEW, now);
|
||||
session_set_tuple_direction(sess, dir);
|
||||
tuple6_to_str(key, sess->tuple_str, sizeof(sess->tuple_str));
|
||||
session_set_timestamp(sess, SESSION_TIMESTAMP_START, now);
|
||||
switch (key->ip_proto)
|
||||
{
|
||||
case IPPROTO_TCP:
|
||||
@@ -496,7 +500,7 @@ static void session_update(struct session *sess, enum session_state next_state,
|
||||
}
|
||||
|
||||
session_set_current_packet(sess, pkt);
|
||||
session_set_current_dir(sess, dir);
|
||||
session_set_current_direction(sess, dir);
|
||||
session_set_timestamp(sess, SESSION_TIMESTAMP_LAST, now);
|
||||
session_set_state(sess, next_state);
|
||||
}
|
||||
@@ -546,7 +550,7 @@ static void session_manager_evicte_session(struct session_manager *mgr, struct s
|
||||
|
||||
static struct session *session_manager_new_tcp_session(struct session_manager *mgr, const struct packet *pkt, const struct tuple6 *key, uint64_t now)
|
||||
{
|
||||
const struct pkt_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
|
||||
const struct packet_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
|
||||
const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
||||
uint8_t flags = tcp_hdr_get_flags(hdr);
|
||||
if (!(flags & TH_SYN))
|
||||
@@ -562,7 +566,7 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m
|
||||
session_manager_evicte_session(mgr, evic_sess, now);
|
||||
}
|
||||
|
||||
enum session_dir dir = (flags & TH_ACK) ? SESSION_DIR_S2C : SESSION_DIR_C2S;
|
||||
enum session_direction dir = (flags & TH_ACK) ? SESSION_DIRECTION_S2C : SESSION_DIRECTION_C2S;
|
||||
struct session *sess = session_pool_pop(mgr->sess_pool);
|
||||
if (sess == NULL)
|
||||
{
|
||||
@@ -571,7 +575,10 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m
|
||||
}
|
||||
session_init(sess);
|
||||
sess->mgr_stat = &mgr->stat;
|
||||
session_set_id(sess, id_generator_alloc());
|
||||
|
||||
enum session_state next_state = session_transition_run(SESSION_STATE_INIT, TCP_SYN);
|
||||
session_update(sess, next_state, pkt, key, dir, now);
|
||||
session_transition_log(sess, SESSION_STATE_INIT, next_state, TCP_SYN);
|
||||
|
||||
if (tcp_init(sess, mgr->opts.tcp_reassembly_enable, mgr->opts.tcp_reassembly_max_timeout, mgr->opts.tcp_reassembly_max_segments) == -1)
|
||||
{
|
||||
@@ -581,10 +588,6 @@ static struct session *session_manager_new_tcp_session(struct session_manager *m
|
||||
}
|
||||
tcp_update(mgr, sess, dir, tcp_layer, now);
|
||||
|
||||
enum session_state next_state = session_transition_run(SESSION_STATE_INIT, TCP_SYN);
|
||||
session_update(sess, next_state, pkt, key, dir, now);
|
||||
session_transition_log(sess, SESSION_STATE_INIT, next_state, TCP_SYN);
|
||||
|
||||
uint64_t timeout = (flags & TH_ACK) ? mgr->opts.tcp_handshake_timeout : mgr->opts.tcp_init_timeout;
|
||||
session_timer_update(mgr->sess_timer, sess, now + timeout);
|
||||
session_table_add(mgr->tcp_sess_table, key, sess);
|
||||
@@ -617,9 +620,8 @@ static struct session *session_manager_new_udp_session(struct session_manager *m
|
||||
}
|
||||
session_init(sess);
|
||||
sess->mgr_stat = &mgr->stat;
|
||||
session_set_id(sess, id_generator_alloc());
|
||||
|
||||
enum session_dir dir = identify_direction_by_port(ntohs(key->src_port), ntohs(key->dst_port));
|
||||
enum session_direction dir = identify_direction_by_port(ntohs(key->src_port), ntohs(key->dst_port));
|
||||
enum session_state next_state = session_transition_run(SESSION_STATE_INIT, UDP_DATA);
|
||||
session_update(sess, next_state, pkt, key, dir, now);
|
||||
session_transition_log(sess, SESSION_STATE_INIT, next_state, UDP_DATA);
|
||||
@@ -635,9 +637,9 @@ static struct session *session_manager_new_udp_session(struct session_manager *m
|
||||
|
||||
static int session_manager_update_tcp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key, uint64_t now)
|
||||
{
|
||||
const struct pkt_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
|
||||
const struct packet_layer *tcp_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_TCP);
|
||||
const struct tcphdr *hdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
|
||||
enum session_dir dir = identify_direction_by_history(sess, key);
|
||||
enum session_direction dir = identify_direction_by_history(sess, key);
|
||||
uint8_t flags = tcp_hdr_get_flags(hdr);
|
||||
int inputs = 0;
|
||||
inputs |= (flags & TH_SYN) ? TCP_SYN : NONE;
|
||||
@@ -666,17 +668,17 @@ static int session_manager_update_tcp_session(struct session_manager *mgr, struc
|
||||
{
|
||||
if (flags & TH_FIN)
|
||||
{
|
||||
session_set_closing_reason(sess, (dir == SESSION_DIR_C2S ? CLOSING_BY_CLIENT_FIN : CLOSING_BY_SERVER_FIN));
|
||||
session_set_closing_reason(sess, (dir == SESSION_DIRECTION_C2S ? CLOSING_BY_CLIENT_FIN : CLOSING_BY_SERVER_FIN));
|
||||
}
|
||||
if (flags & TH_RST)
|
||||
{
|
||||
session_set_closing_reason(sess, (dir == SESSION_DIR_C2S ? CLOSING_BY_CLIENT_RST : CLOSING_BY_SERVER_RST));
|
||||
session_set_closing_reason(sess, (dir == SESSION_DIRECTION_C2S ? CLOSING_BY_CLIENT_RST : CLOSING_BY_SERVER_RST));
|
||||
}
|
||||
}
|
||||
|
||||
// update timeout
|
||||
struct tcp_half *curr = &sess->tcp_halfs[dir];
|
||||
struct tcp_half *peer = &sess->tcp_halfs[(dir == SESSION_DIR_C2S ? SESSION_DIR_S2C : SESSION_DIR_C2S)];
|
||||
struct tcp_half *peer = &sess->tcp_halfs[(dir == SESSION_DIRECTION_C2S ? SESSION_DIRECTION_S2C : SESSION_DIRECTION_C2S)];
|
||||
uint64_t timeout = 0;
|
||||
switch (next_state)
|
||||
{
|
||||
@@ -725,12 +727,20 @@ static int session_manager_update_tcp_session(struct session_manager *mgr, struc
|
||||
|
||||
static int session_manager_update_udp_session(struct session_manager *mgr, struct session *sess, const struct packet *pkt, const struct tuple6 *key, uint64_t now)
|
||||
{
|
||||
enum session_dir dir = identify_direction_by_history(sess, key);
|
||||
enum session_direction dir = identify_direction_by_history(sess, key);
|
||||
enum session_state curr_state = session_get_state(sess);
|
||||
enum session_state next_state = session_transition_run(curr_state, UDP_DATA);
|
||||
session_update(sess, next_state, pkt, key, dir, now);
|
||||
session_transition_log(sess, curr_state, next_state, UDP_DATA);
|
||||
session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_data_timeout);
|
||||
|
||||
if (session_get_state(sess) == SESSION_STATE_DISCARD)
|
||||
{
|
||||
session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_discard_timeout);
|
||||
}
|
||||
else
|
||||
{
|
||||
session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_data_timeout);
|
||||
}
|
||||
|
||||
SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp);
|
||||
|
||||
@@ -888,12 +898,12 @@ void session_manager_free_session(struct session_manager *mgr, struct session *s
|
||||
break;
|
||||
}
|
||||
|
||||
packet_free((struct packet *)session_get_1st_packet(sess, SESSION_DIR_C2S));
|
||||
packet_free((struct packet *)session_get_1st_packet(sess, SESSION_DIR_S2C));
|
||||
session_set_1st_packet(sess, SESSION_DIR_C2S, NULL);
|
||||
session_set_1st_packet(sess, SESSION_DIR_S2C, NULL);
|
||||
packet_free((struct packet *)session_get_1st_packet(sess, SESSION_DIRECTION_C2S));
|
||||
packet_free((struct packet *)session_get_1st_packet(sess, SESSION_DIRECTION_S2C));
|
||||
session_set_1st_packet(sess, SESSION_DIRECTION_C2S, NULL);
|
||||
session_set_1st_packet(sess, SESSION_DIRECTION_S2C, NULL);
|
||||
session_set_current_packet(sess, NULL);
|
||||
session_set_current_dir(sess, SESSION_DIR_NONE);
|
||||
session_set_current_direction(sess, SESSION_DIRECTION_NONE);
|
||||
session_free_all_ex_data(sess);
|
||||
session_pool_push(mgr->sess_pool, sess);
|
||||
sess = NULL;
|
||||
@@ -950,15 +960,12 @@ struct session *session_manager_get_expired_session(struct session_manager *mgr,
|
||||
session_transition_log(sess, curr_state, next_state, TIMEOUT);
|
||||
session_set_state(sess, next_state);
|
||||
|
||||
uint64_t timeout = 0;
|
||||
switch (session_get_type(sess))
|
||||
{
|
||||
case SESSION_TYPE_TCP:
|
||||
timeout = mgr->opts.tcp_data_timeout;
|
||||
SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, tcp);
|
||||
break;
|
||||
case SESSION_TYPE_UDP:
|
||||
timeout = mgr->opts.udp_data_timeout;
|
||||
SESS_MGR_STAT_UPDATE(&mgr->stat, curr_state, next_state, udp);
|
||||
break;
|
||||
default:
|
||||
@@ -966,19 +973,30 @@ struct session *session_manager_get_expired_session(struct session_manager *mgr,
|
||||
break;
|
||||
}
|
||||
|
||||
// next state is closed, need to free session
|
||||
if (next_state == SESSION_STATE_CLOSED)
|
||||
{
|
||||
// need free session
|
||||
if (!session_get_closing_reason(sess))
|
||||
{
|
||||
session_set_closing_reason(sess, CLOSING_BY_TIMEOUT);
|
||||
}
|
||||
return sess;
|
||||
}
|
||||
// next state is closing, only update timeout
|
||||
else
|
||||
{
|
||||
// in closing state, only update timeout
|
||||
session_timer_update(mgr->sess_timer, sess, now + timeout);
|
||||
switch (session_get_type(sess))
|
||||
{
|
||||
case SESSION_TYPE_TCP:
|
||||
session_timer_update(mgr->sess_timer, sess, now + mgr->opts.tcp_data_timeout);
|
||||
break;
|
||||
case SESSION_TYPE_UDP:
|
||||
session_timer_update(mgr->sess_timer, sess, now + mgr->opts.udp_data_timeout);
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user