open5gs/src/mme/s6a_sm.c

238 lines
7.7 KiB
C
Raw Normal View History

2017-02-28 10:22:01 +00:00
#define TRACE_MODULE _s6a_auth
#include "core_debug.h"
2017-03-06 01:30:55 +00:00
#include "core_pool.h"
2017-02-28 10:22:01 +00:00
2017-03-04 15:50:22 +00:00
#include "s6a_lib.h"
2017-03-06 01:00:30 +00:00
#include "s6a_sm.h"
2017-03-06 01:30:55 +00:00
#define SIZE_OF_SESS_STATE_POOL 32
2017-03-06 01:00:30 +00:00
static struct session_handler *s6a_mme_reg = NULL;
2017-02-28 10:22:01 +00:00
struct sess_state {
2017-03-06 01:30:55 +00:00
ue_ctx_t *ue;
2017-02-28 11:31:06 +00:00
struct timespec ts; /* Time of sending the message */
2017-02-28 12:55:19 +00:00
};
2017-03-06 01:30:55 +00:00
pool_declare(sess_state_pool, struct sess_state, SIZE_OF_SESS_STATE_POOL);
2017-02-28 12:55:19 +00:00
static void s6a_aia_cb(void *data, struct msg **msg);
2017-02-28 10:22:01 +00:00
/* Cb called when an answer is received */
2017-03-06 00:00:37 +00:00
int s6a_send_auth_info_req(ue_ctx_t *ue, c_uint8_t *plmn_id)
2017-02-28 12:55:19 +00:00
{
struct msg *req = NULL;
struct avp *avp;
2017-02-28 15:00:28 +00:00
struct avp *avpch;
2017-02-28 12:55:19 +00:00
union avp_value val;
struct sess_state *mi = NULL, *svg;
struct session *sess = NULL;
2017-03-06 00:00:37 +00:00
d_assert(ue, return -1, "Null Param");
2017-02-28 12:55:19 +00:00
/* Create the random value to store with the session */
2017-03-06 01:30:55 +00:00
pool_alloc_node(&sess_state_pool, &mi);
2017-02-28 12:55:19 +00:00
d_assert(mi, return -1, "malloc failed: %s", strerror(errno));
2017-03-06 01:30:55 +00:00
mi->ue = ue;
2017-02-28 12:55:19 +00:00
/* Create the request */
2017-03-06 01:00:30 +00:00
d_assert(fd_msg_new(s6a_cmd_air, MSGFL_ALLOC_ETEID, &req) == 0,
2017-03-06 01:30:55 +00:00
pool_free_node(&sess_state_pool, mi); return -1,);
2017-02-28 12:55:19 +00:00
/* Create a new session */
#define S6A_APP_SID_OPT "app_s6a"
d_assert(fd_msg_new_session(req, (os0_t)S6A_APP_SID_OPT,
CONSTSTRLEN(S6A_APP_SID_OPT)) == 0, goto out,);
d_assert(fd_msg_sess_get(fd_g_config->cnf_dict, req, &sess, NULL) == 0,
goto out, );
2017-02-28 15:00:28 +00:00
2017-02-28 15:21:20 +00:00
/* Set the Auth-Session-Statee AVP */
2017-02-28 15:00:28 +00:00
d_assert(fd_msg_avp_new(s6a_auth_session_state, 0, &avp) == 0, goto out,);
val.i32 = 1;
d_assert(fd_msg_avp_setvalue(avp, &val) == 0, goto out,);
d_assert(fd_msg_avp_add(req, MSG_BRW_LAST_CHILD, avp) == 0, goto out,);
/* Set Origin-Host & Origin-Realm */
d_assert(fd_msg_add_origin(req, 0) == 0, goto out, );
2017-02-28 12:55:19 +00:00
/* Set the Destination-Realm AVP */
d_assert(fd_msg_avp_new(s6a_destination_realm, 0, &avp) == 0, goto out,);
val.os.data = (unsigned char *)(fd_g_config->cnf_diamrlm);
val.os.len = strlen(fd_g_config->cnf_diamrlm);
d_assert(fd_msg_avp_setvalue(avp, &val) == 0, goto out, );
d_assert(fd_msg_avp_add(req, MSG_BRW_LAST_CHILD, avp) == 0, goto out, );
/* Set the User-Name AVP if needed*/
d_assert(fd_msg_avp_new(s6a_user_name, 0, &avp) == 0, goto out,);
2017-03-06 00:00:37 +00:00
val.os.data = ue->imsi;
val.os.len = ue->imsi_len;
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_avp_setvalue(avp, &val) == 0, goto out, );
d_assert(fd_msg_avp_add(req, MSG_BRW_LAST_CHILD, avp) == 0, goto out,);
2017-02-28 15:00:28 +00:00
/* Add the Authentication-Info */
d_assert(fd_msg_avp_new(s6a_req_eutran_auth_info, 0, &avp) == 0, goto out,);
d_assert(fd_msg_avp_new(s6a_number_of_requested_vectors, 0,
&avpch) == 0, goto out,);
2017-03-05 14:43:46 +00:00
val.u32 = 1;
2017-02-28 15:00:28 +00:00
d_assert(fd_msg_avp_setvalue (avpch, &val) == 0, goto out,);
d_assert(fd_msg_avp_add (avp, MSG_BRW_LAST_CHILD, avpch) == 0, goto out,);
d_assert(fd_msg_avp_new(s6a_immediate_response_preferred, 0,
&avpch) == 0, goto out,);
2017-03-05 14:43:46 +00:00
val.u32 = 1;
2017-02-28 15:00:28 +00:00
d_assert(fd_msg_avp_setvalue(avpch, &val) == 0, goto out,);
d_assert(fd_msg_avp_add(avp, MSG_BRW_LAST_CHILD, avpch) == 0, goto out,);
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_avp_add(req, MSG_BRW_LAST_CHILD, avp) == 0, goto out,);
/* Set the Visited-PLMN-Id AVP if needed*/
2017-03-03 10:07:18 +00:00
d_assert(fd_msg_avp_new(s6a_visited_plmn_id, 0, &avp) == 0, goto out,);
2017-03-05 14:43:46 +00:00
val.os.data = plmn_id;
2017-03-03 10:07:18 +00:00
val.os.len = PLMN_ID_LEN;
d_assert(fd_msg_avp_setvalue(avp, &val) == 0, goto out,);
d_assert(fd_msg_avp_add(req, MSG_BRW_LAST_CHILD, avp) == 0, goto out,);
2017-02-28 12:55:19 +00:00
d_assert(clock_gettime(CLOCK_REALTIME, &mi->ts) == 0, goto out,);
/* Keep a pointer to the session data for debug purpose,
* in real life we would not need it */
svg = mi;
/* Store this value in the session */
d_assert(fd_sess_state_store(s6a_mme_reg, sess, &mi) == 0, goto out,);
/* Send the request */
d_assert(fd_msg_send(&req, s6a_aia_cb, svg) == 0, goto out,);
/* Increment the counter */
d_assert(pthread_mutex_lock(&s6a_config->stats_lock) == 0,,);
s6a_config->stats.nb_sent++;
d_assert(pthread_mutex_unlock(&s6a_config->stats_lock) == 0,, );
return 0;
out:
d_assert(fd_msg_free(req) == 0,,);
2017-03-06 01:30:55 +00:00
pool_free_node(&sess_state_pool, mi);
2017-02-28 12:55:19 +00:00
return -1;
}
static void s6a_aia_cb(void *data, struct msg **msg)
2017-02-28 10:22:01 +00:00
{
2017-02-28 11:31:06 +00:00
struct sess_state *mi = NULL;
struct timespec ts;
struct session *sess;
struct avp *avp;
struct avp_hdr *hdr;
unsigned long dur;
int error = 0;
2017-02-28 12:55:19 +00:00
int new;
2017-03-06 01:30:55 +00:00
ue_ctx_t *ue = NULL;
2017-02-28 11:31:06 +00:00
CHECK_SYS_DO(clock_gettime(CLOCK_REALTIME, &ts), return);
2017-02-28 10:22:01 +00:00
2017-02-28 11:31:06 +00:00
/* Search the session, retrieve its data */
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_sess_get(fd_g_config->cnf_dict, *msg, &sess, &new) == 0 &&
new == 0, return,);
d_assert(fd_sess_state_retrieve(s6a_mme_reg, sess, &mi) == 0 &&
2017-03-06 01:30:55 +00:00
mi && (void *)mi == data, fd_msg_free(*msg); *msg = NULL; return,);
ue = mi->ue;
d_assert(ue, error++; goto out,);
2017-02-28 11:31:06 +00:00
/* Value of Result Code */
2017-03-06 01:30:55 +00:00
d_assert(fd_msg_search_avp(*msg, s6a_result_code, &avp) == 0 && avp,
error++; goto out,);
d_assert(fd_msg_avp_hdr(avp, &hdr) == 0 && hdr, error++; goto out,);
if (hdr->avp_value->i32 != ER_DIAMETER_SUCCESS)
2017-02-28 11:31:06 +00:00
{
2017-03-06 01:30:55 +00:00
d_error("ERROR DIAMETER Result Code(%d)", hdr->avp_value->i32);
2017-02-28 11:31:06 +00:00
error++;
2017-03-06 01:30:55 +00:00
goto out;
2017-02-28 11:31:06 +00:00
}
/* Value of Origin-Host */
2017-03-06 01:30:55 +00:00
d_assert(fd_msg_search_avp(*msg, s6a_origin_host, &avp) == 0 && avp,
error++; goto out,);
d_assert(fd_msg_avp_hdr(avp, &hdr) == 0 && hdr, error++; goto out,);
2017-02-28 11:31:06 +00:00
/* Value of Origin-Realm */
2017-03-06 01:30:55 +00:00
d_assert(fd_msg_search_avp(*msg, s6a_origin_realm, &avp) == 0 && avp,
error++; goto out,);
d_assert(fd_msg_avp_hdr(avp, &hdr) == 0 && hdr, error++; goto out,);
2017-02-28 11:31:06 +00:00
2017-03-06 01:30:55 +00:00
out:
/* Free the message */
2017-02-28 12:55:19 +00:00
d_assert(pthread_mutex_lock(&s6a_config->stats_lock) == 0,,);
2017-02-28 11:31:06 +00:00
dur = ((ts.tv_sec - mi->ts.tv_sec) * 1000000) +
2017-02-28 10:22:01 +00:00
((ts.tv_nsec - mi->ts.tv_nsec) / 1000);
2017-02-28 11:31:06 +00:00
if (s6a_config->stats.nb_recv)
{
/* Ponderate in the avg */
s6a_config->stats.avg = (s6a_config->stats.avg *
2017-02-28 10:40:44 +00:00
s6a_config->stats.nb_recv + dur) / (s6a_config->stats.nb_recv + 1);
2017-02-28 11:31:06 +00:00
/* Min, max */
if (dur < s6a_config->stats.shortest)
s6a_config->stats.shortest = dur;
if (dur > s6a_config->stats.longest)
s6a_config->stats.longest = dur;
}
else
{
s6a_config->stats.shortest = dur;
s6a_config->stats.longest = dur;
s6a_config->stats.avg = dur;
}
if (error)
s6a_config->stats.nb_errs++;
else
s6a_config->stats.nb_recv++;
2017-02-28 12:55:19 +00:00
d_assert(pthread_mutex_unlock(&s6a_config->stats_lock) == 0,,);
2017-02-28 11:31:06 +00:00
/* Display how long it took */
if (ts.tv_nsec > mi->ts.tv_nsec)
2017-02-28 10:22:01 +00:00
d_info("in %d.%06ld sec",
2017-02-28 11:31:06 +00:00
(int)(ts.tv_sec - mi->ts.tv_sec),
(long)(ts.tv_nsec - mi->ts.tv_nsec) / 1000);
else
2017-02-28 10:22:01 +00:00
d_info("in %d.%06ld sec",
2017-02-28 11:31:06 +00:00
(int)(ts.tv_sec + 1 - mi->ts.tv_sec),
(long)(1000000000 + ts.tv_nsec - mi->ts.tv_nsec) / 1000);
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_free(*msg) == 0,,);
2017-02-28 11:31:06 +00:00
*msg = NULL;
2017-03-06 01:30:55 +00:00
pool_free_node(&sess_state_pool, mi);
2017-02-28 10:22:01 +00:00
}
2017-03-06 01:00:30 +00:00
status_t s6a_sm_init(void)
{
status_t rv;
rv = s6a_init(MODE_MME);
if (rv != CORE_OK) return rv;
2017-03-06 01:30:55 +00:00
pool_init(&sess_state_pool, SIZE_OF_SESS_STATE_POOL);
2017-03-06 01:00:30 +00:00
d_assert(fd_sess_handler_create(&s6a_mme_reg,
(void *)free, NULL, NULL) == 0, return -1,);
return CORE_OK;
}
void s6a_sm_final(void)
{
d_assert(fd_sess_handler_destroy(&s6a_mme_reg, NULL) == 0,,);
2017-03-06 01:30:55 +00:00
d_print("%d not freed in sess_state_pool[%d] of S6A-SM\n",
pool_size(&sess_state_pool) - pool_avail(&sess_state_pool),
pool_size(&sess_state_pool));
pool_final(&sess_state_pool);
2017-03-06 01:00:30 +00:00
s6a_final();
}