open5gs/src/s6a_auth.c

210 lines
6.5 KiB
C
Raw Normal View History

2017-02-28 10:22:01 +00:00
#define TRACE_MODULE _s6a_auth
#include "core_debug.h"
#include "s6a_app.h"
#include "s6a_auth.h"
struct sess_state {
2017-02-28 11:31:06 +00:00
c_int32_t randval; /* a random value to store in Test-AVP */
struct timespec ts; /* Time of sending the message */
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-02-28 12:55:19 +00:00
int s6_send_auth_req()
{
struct msg *req = NULL;
struct avp *avp;
union avp_value val;
struct sess_state *mi = NULL, *svg;
struct session *sess = NULL;
/* Create the random value to store with the session */
mi = malloc(sizeof(struct sess_state));
d_assert(mi, return -1, "malloc failed: %s", strerror(errno));
mi->randval = (int32_t)random();
/* Create the request */
d_assert(fd_msg_new( s6a_cmd_air, MSGFL_ALLOC_ETEID, &req ) == 0,
free(mi); return -1,);
/* 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, );
/* 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 Origin-Host & Origin-Realm */
d_assert(fd_msg_add_origin(req, 0) == 0, goto out, );
/* Set the User-Name AVP if needed*/
#define S6A_USER_NAME "01045238277"
d_assert(fd_msg_avp_new(s6a_user_name, 0, &avp) == 0, goto out,);
val.os.data = (unsigned char *)(S6A_USER_NAME);
val.os.len = strlen(S6A_USER_NAME);
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 Auth-Session-Statee AVP if needed*/
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 the Visited-PLMN-Id AVP if needed*/
c_uint8_t plmn[3] = { 0x00, 0xf1, 0x10 };
d_assert(fd_msg_avp_new(s6a_visited_plmn_id, 0, &avp) == 0, goto out,);
val.os.data = plmn;
val.os.len = 3;
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,);
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,);
/* Log sending the message */
d_info("SEND %x to '%s' (-)\n", svg->randval, fd_g_config->cnf_diamrlm);
/* 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,,);
free(mi);
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-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 &&
(void *)mi == data, fd_msg_free(*msg); *msg = NULL; return,);
2017-02-28 11:31:06 +00:00
/* Value of Result Code */
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_search_avp(*msg, s6a_result_code, &avp) == 0, goto out,);
2017-02-28 11:31:06 +00:00
if (avp)
{
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_avp_hdr(avp, &hdr) == 0, goto out,);
if (hdr->avp_value->i32 != ER_DIAMETER_SUCCESS)
2017-02-28 11:31:06 +00:00
error++;
}
else
{
2017-02-28 12:55:19 +00:00
d_error("No 'Result-Code'");
2017-02-28 11:31:06 +00:00
error++;
}
/* Value of Origin-Host */
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_search_avp(*msg, s6a_origin_host, &avp) == 0, goto out,);
2017-02-28 11:31:06 +00:00
if (avp)
{
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_avp_hdr(avp, &hdr) == 0, goto out,);
2017-02-28 10:22:01 +00:00
d_info("From '%.*s' ",
2017-02-28 11:31:06 +00:00
(int)hdr->avp_value->os.len, hdr->avp_value->os.data);
}
else
{
2017-02-28 12:55:19 +00:00
d_error("No 'Origin-Host'");
2017-02-28 11:31:06 +00:00
error++;
}
/* Value of Origin-Realm */
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_search_avp(*msg, s6a_origin_realm, &avp) == 0, goto out,);
2017-02-28 11:31:06 +00:00
if (avp)
{
2017-02-28 12:55:19 +00:00
d_assert(fd_msg_avp_hdr(avp, &hdr) == 0, goto out,);
2017-02-28 10:22:01 +00:00
d_info("('%.*s') ",
2017-02-28 11:31:06 +00:00
(int)hdr->avp_value->os.len, hdr->avp_value->os.data);
}
else
{
2017-02-28 12:55:19 +00:00
d_error("No 'Origin-Realm'");
2017-02-28 11:31:06 +00:00
error++;
}
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
out:
2017-02-28 11:31:06 +00:00
/* Free the message */
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;
free(mi);
return;
2017-02-28 10:22:01 +00:00
}