freewtp/src/wtp/wtp_dfa_dtls.c

161 lines
5.5 KiB
C
Raw Normal View History

2013-05-01 14:52:55 +02:00
#include "wtp.h"
#include "capwap_dfa.h"
#include "wtp_dfa.h"
/* DTLS BIO send */
int wtp_bio_send(struct capwap_dtls* dtls, char* buffer, int length, void* param) {
struct capwap_socket* socket = ((dtls->session == CAPWAP_DTLS_CONTROL_SESSION) ? &g_wtp.acctrlsock : &g_wtp.acdatasock);
struct sockaddr_storage* wtpaddress = ((dtls->session == CAPWAP_DTLS_CONTROL_SESSION) ? &g_wtp.wtpctrladdress : &g_wtp.wtpdataaddress);
struct sockaddr_storage* acaddress = ((dtls->session == CAPWAP_DTLS_CONTROL_SESSION) ? &g_wtp.acctrladdress : &g_wtp.acdataaddress);
return capwap_sendto(socket->socket[socket->type], buffer, length, wtpaddress, acaddress);
}
/* */
2013-11-07 22:06:29 +01:00
void wtp_start_dtlssetup(struct timeout_control* timeout) {
2013-05-01 14:52:55 +02:00
ASSERT(timeout != NULL);
/* Create DTLS session */
if (!capwap_crypt_createsession(&g_wtp.ctrldtls, CAPWAP_DTLS_CONTROL_SESSION, &g_wtp.dtlscontext, wtp_bio_send, NULL)) {
2013-11-07 22:06:29 +01:00
capwap_set_timeout(g_wtp.dfa.rfcSilentInterval, timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
wtp_dfa_change_state(CAPWAP_SULKING_STATE);
2013-05-01 14:52:55 +02:00
} else {
if (capwap_crypt_open(&g_wtp.ctrldtls, &g_wtp.acctrladdress) == CAPWAP_HANDSHAKE_ERROR) {
2013-11-07 22:06:29 +01:00
capwap_set_timeout(g_wtp.dfa.rfcSilentInterval, timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
wtp_dfa_change_state(CAPWAP_SULKING_STATE);
2013-05-01 14:52:55 +02:00
} else {
capwap_set_timeout(g_wtp.dfa.rfcWaitDTLS, timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
2013-11-07 22:06:29 +01:00
wtp_dfa_change_state(CAPWAP_DTLS_CONNECT_STATE);
2013-05-01 14:52:55 +02:00
}
}
}
/* */
2013-11-07 22:06:29 +01:00
void wtp_start_datachannel(struct timeout_control* timeout) {
struct capwap_list* txfragpacket;
struct capwap_header_data capwapheader;
struct capwap_packet_txmng* txmngpacket;
2013-05-01 14:52:55 +02:00
ASSERT(timeout != NULL);
2013-11-07 22:06:29 +01:00
/* If need, create DTLS Data channel crypted */
if (g_wtp.dtlsdatapolicy & CAPWAP_ACDESC_DTLS_DATA_CHANNEL_ENABLED) {
if (!g_wtp.datadtls.enable) {
/* Create DTLS data session before send data keepalive */
if (capwap_crypt_createsession(&g_wtp.datadtls, CAPWAP_DTLS_DATA_SESSION, &g_wtp.dtlscontext, wtp_bio_send, NULL)) {
if (capwap_crypt_open(&g_wtp.datadtls, &g_wtp.acdataaddress) == CAPWAP_HANDSHAKE_CONTINUE) {
capwap_set_timeout(g_wtp.dfa.rfcWaitDTLS, timeout, CAPWAP_TIMER_CONTROL_CONNECTION); /* Wait complete dtls handshake */
} else {
wtp_teardown_connection(timeout);
}
} else {
wtp_teardown_connection(timeout);
}
return;
} else if (g_wtp.datadtls.action != CAPWAP_DTLS_ACTION_DATA) {
wtp_teardown_connection(timeout);
return;
}
}
/* Build packet */
capwap_header_init(&capwapheader, CAPWAP_RADIOID_NONE, g_wtp.binding);
capwap_header_set_keepalive_flag(&capwapheader, 1);
txmngpacket = capwap_packet_txmng_create_data_message(&capwapheader, g_wtp.mtu); /* CAPWAP_DONT_FRAGMENT */
/* Add message element */
capwap_packet_txmng_add_message_element(txmngpacket, CAPWAP_ELEMENT_SESSIONID, &g_wtp.sessionid);
/* Data keepalive complete, get fragment packets into local list */
txfragpacket = capwap_list_create();
capwap_packet_txmng_get_fragment_packets(txmngpacket, txfragpacket, 0);
if (txfragpacket->count == 1) {
/* Send Data keepalive to AC */
if (capwap_crypt_sendto_fragmentpacket(&g_wtp.datadtls, g_wtp.acdatasock.socket[g_wtp.acdatasock.type], txfragpacket, &g_wtp.wtpdataaddress, &g_wtp.acdataaddress)) {
/* Reset AC Prefered List Position */
g_wtp.acpreferedselected = 0;
/* Set timer */
capwap_kill_timeout(timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
capwap_set_timeout(g_wtp.dfa.rfcEchoInterval, timeout, CAPWAP_TIMER_CONTROL_ECHO);
capwap_set_timeout(g_wtp.dfa.rfcDataChannelDeadInterval, timeout, CAPWAP_TIMER_DATA_KEEPALIVEDEAD);
wtp_dfa_change_state(CAPWAP_RUN_STATE);
} else {
/* Error to send packets */
capwap_logging_debug("Warning: error to send data channel keepalive packet");
wtp_teardown_connection(timeout);
}
} else {
capwap_logging_debug("Warning: error to send data channel keepalive packet, fragment packet");
wtp_teardown_connection(timeout);
}
/* Free packets manager */
capwap_list_free(txfragpacket);
capwap_packet_txmng_free(txmngpacket);
2013-05-01 14:52:55 +02:00
}
/* Teardown connection */
2013-11-07 22:06:29 +01:00
void wtp_teardown_connection(struct timeout_control* timeout) {
2013-05-01 14:52:55 +02:00
ASSERT(timeout != NULL);
2013-09-16 22:10:37 +02:00
g_wtp.teardown = 1;
2013-05-01 14:52:55 +02:00
/* DTSL Control */
if (g_wtp.ctrldtls.enable) {
capwap_crypt_close(&g_wtp.ctrldtls);
}
2013-11-07 22:06:29 +01:00
2013-05-01 14:52:55 +02:00
/* DTLS Data */
if (g_wtp.datadtls.enable) {
capwap_crypt_close(&g_wtp.datadtls);
}
/* */
capwap_killall_timeout(timeout);
capwap_set_timeout(g_wtp.dfa.rfcDTLSSessionDelete, timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
wtp_dfa_change_state(CAPWAP_DTLS_TEARDOWN_STATE);
}
/* */
2013-11-07 22:06:29 +01:00
void wtp_dfa_state_dtlsteardown(struct capwap_parsed_packet* packet, struct timeout_control* timeout) {
2013-05-01 14:52:55 +02:00
ASSERT(timeout != NULL);
2013-11-07 22:06:29 +01:00
if (packet) {
return;
}
2013-05-01 14:52:55 +02:00
/* Free and reset resource */
if (g_wtp.ctrldtls.enable) {
capwap_crypt_freesession(&g_wtp.ctrldtls);
}
2013-11-07 22:06:29 +01:00
2013-05-01 14:52:55 +02:00
if (g_wtp.datadtls.enable) {
capwap_crypt_freesession(&g_wtp.datadtls);
}
/* */
if (g_wtp.acname.name) {
capwap_free(g_wtp.acname.name);
g_wtp.acname.name = NULL;
}
2013-05-01 14:52:55 +02:00
/* */
wtp_free_reference_last_request();
wtp_free_reference_last_response();
2013-06-05 19:39:03 +02:00
wtp_free_packet_rxmng(0);
wtp_free_packet_rxmng(1);
2013-05-01 14:52:55 +02:00
2013-06-05 19:39:03 +02:00
/* */
if (!g_wtp.running) {
2013-11-07 22:06:29 +01:00
wtp_dfa_change_state(CAPWAP_DEAD_STATE);
2013-06-05 19:39:03 +02:00
} else if ((g_wtp.dfa.rfcFailedDTLSSessionCount >= g_wtp.dfa.rfcMaxFailedDTLSSessionRetry) || (g_wtp.dfa.rfcFailedDTLSAuthFailCount >= g_wtp.dfa.rfcMaxFailedDTLSSessionRetry)) {
2013-11-07 22:06:29 +01:00
capwap_set_timeout(g_wtp.dfa.rfcSilentInterval, timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
wtp_dfa_change_state(CAPWAP_SULKING_STATE);
2013-05-01 14:52:55 +02:00
} else {
2013-11-07 22:06:29 +01:00
capwap_set_timeout(0, timeout, CAPWAP_TIMER_CONTROL_CONNECTION);
wtp_dfa_change_state(CAPWAP_IDLE_STATE);
2013-05-01 14:52:55 +02:00
}
}