This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
lynxis lazus gerrit-no-reply at lists.osmocom.orglynxis lazus has uploaded this change for review. ( https://gerrit.osmocom.org/9257 Change subject: gprs_gmm: introduce a GMM Attach Request FSM ...................................................................... gprs_gmm: introduce a GMM Attach Request FSM The old GMM Attach Request handling used a recursive function which can not handle certain states and is quite complex and hard to extend. The new FSM handles such request in a FSM and can be called multiple times. Change-Id: I58b9c17be9776a03bb2a5b21e99135cfefc8c912 --- M include/osmocom/sgsn/gprs_sgsn.h M src/gprs/Makefile.am M src/gprs/gprs_gmm.c A src/gprs/gprs_gmm_attach.c A src/gprs/gprs_gmm_attach.h M src/gprs/gprs_sgsn.c M tests/sgsn/Makefile.am 7 files changed, 404 insertions(+), 8 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-sgsn refs/changes/57/9257/1 diff --git a/include/osmocom/sgsn/gprs_sgsn.h b/include/osmocom/sgsn/gprs_sgsn.h index 64e5619..2a01a99 100644 --- a/include/osmocom/sgsn/gprs_sgsn.h +++ b/include/osmocom/sgsn/gprs_sgsn.h @@ -4,6 +4,7 @@ #include <stdint.h> #include <netinet/in.h> +#include <osmocom/core/fsm.h> #include <osmocom/core/timer.h> #include <osmocom/gsm/gsm48.h> @@ -168,6 +169,8 @@ struct ranap_ue_conn_ctx *ue_ctx; struct service_info service; } iu; + struct osmo_fsm_inst *gmm_att_req_fsm; + uint32_t gmm_att_identy_type; /* VLR number */ uint32_t new_sgsn_addr; /* Authentication Triplet */ diff --git a/src/gprs/Makefile.am b/src/gprs/Makefile.am index 4c7f730..aa6b08c 100644 --- a/src/gprs/Makefile.am +++ b/src/gprs/Makefile.am @@ -58,6 +58,7 @@ $(NULL) osmo_sgsn_SOURCES = \ + gprs_gmm_attach.c \ gprs_gmm.c \ gprs_sgsn.c \ gprs_sndcp.c \ diff --git a/src/gprs/gprs_gmm.c b/src/gprs/gprs_gmm.c index 57d3883..4236870 100644 --- a/src/gprs/gprs_gmm.c +++ b/src/gprs/gprs_gmm.c @@ -64,6 +64,8 @@ #include <pdp.h> +#include "gprs_gmm_attach.h" + #define PTMSI_ALLOC extern struct sgsn_instance *sgsn; @@ -791,7 +793,7 @@ /* FIXME: enable LLC cipheirng */ /* Check if we can let the mobile station enter */ - return gsm48_gmm_authorize(ctx); + return osmo_fsm_inst_dispatch(ctx->gmm_att_req_fsm, E_AUTH_RESP_RECV, NULL); } /* Section 9.4.10: Authentication and Ciphering Failure */ @@ -1107,7 +1109,10 @@ { ctx->sec_ctx = OSMO_AUTH_TYPE_NONE; - gsm48_gmm_authorize(ctx); + if (ctx->gmm_att_req_fsm->state != ST_INIT) + osmo_fsm_inst_dispatch(ctx->gmm_att_req_fsm, E_VLR_ANSWERED, (void *) 0); + else + gsm48_gmm_authorize(ctx); } void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx) @@ -1118,7 +1123,8 @@ "Authorized, continuing procedure, IMSI=%s\n", ctx->imsi); /* Continue with the authorization */ - gsm48_gmm_authorize(ctx); + if (ctx->gmm_att_req_fsm->state != ST_INIT) + osmo_fsm_inst_dispatch(ctx->gmm_att_req_fsm, E_VLR_ANSWERED, (void *) 0); break; default: LOGMMCTXP(LOGL_INFO, ctx, @@ -1139,8 +1145,8 @@ "with cause '%s' (%d)\n", get_value_string(gsm48_gmm_cause_names, gmm_cause), gmm_cause); - gsm48_tx_gmm_att_rej(ctx, gmm_cause); - mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT"); + if (ctx->gmm_att_req_fsm->state != ST_INIT) + osmo_fsm_inst_dispatch(ctx->gmm_att_req_fsm, E_REJECT, (void *) (long) gmm_cause); break; case GMM_REGISTERED_NORMAL: case GMM_REGISTERED_SUSPENDED: @@ -1183,6 +1189,7 @@ { struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; + long mi_typel = mi_type; char mi_string[GSM48_MI_SIZE]; gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); @@ -1235,7 +1242,7 @@ } /* Check if we can let the mobile station enter */ - return gsm48_gmm_authorize(ctx); + return osmo_fsm_inst_dispatch(ctx->gmm_att_req_fsm, E_IDEN_RESP_RECV, (void *)mi_typel); } /* Allocate a new P-TMSI and change context state */ @@ -1425,8 +1432,8 @@ gprs_llgmm_assign(ctx->gb.llme, ctx->gb.tlli, ctx->gb.tlli_new); } - ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ; - return gsm48_gmm_authorize(ctx); + osmo_fsm_inst_dispatch(ctx->gmm_att_req_fsm, E_ATTACH_REQ_RECV, msg); + return 0; err_inval: LOGPC(DMM, LOGL_INFO, "\n"); @@ -2023,6 +2030,7 @@ mmctx_set_mm_state(mmctx, MM_READY); rc = 0; + osmo_fsm_inst_dispatch(mmctx->gmm_att_req_fsm, E_ATTACH_COMPLETE_RECV, 0); memset(&sig_data, 0, sizeof(sig_data)); sig_data.mm = mmctx; osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data); diff --git a/src/gprs/gprs_gmm_attach.c b/src/gprs/gprs_gmm_attach.c new file mode 100644 index 0000000..cac660e --- /dev/null +++ b/src/gprs/gprs_gmm_attach.c @@ -0,0 +1,345 @@ +#include "gprs_gmm_attach.h" + +#include <osmocom/gsm/protocol/gsm_04_08_gprs.h> +#include <osmocom/sgsn/debug.h> +#include <osmocom/sgsn/gprs_gmm.h> +#include <osmocom/sgsn/sgsn.h> + +#define X(s) (1 << (s)) + +extern const struct value_string gmm_attach_req_fsm_event_names[]; +extern struct osmo_fsm gmm_attach_req_fsm; + +static int require_identity_imei = 1; +static int require_auth = 1; + +static void st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + + ctx->auth_state = SGSN_AUTH_UNKNOWN; + + /* + * TODO: remove pending_req as soon the sgsn_auth code doesn't depend + * on it. + * pending_req must be set, even this fsm doesn't use it, because + * the sgsn_auth code is using this too + */ + ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ; + + if (require_identity_imei) { + ctx->gmm_att_identy_type = GSM_MI_TYPE_IMEI; + osmo_fsm_inst_state_chg(fi, ST_IDENTIY, sgsn->cfg.timers.T3370, 3370); + } else if (!strlen(ctx->imsi)) { + ctx->gmm_att_identy_type = GSM_MI_TYPE_IMSI; + osmo_fsm_inst_state_chg(fi, ST_IDENTIY, sgsn->cfg.timers.T3370, 3370); + } else if (require_auth) + osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360); + else + osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350); +} + +static void st_identity_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + int ret = 0; + + ctx->num_T_exp = 0; + + switch (ctx->gmm_att_identy_type) { + case GSM_MI_TYPE_IMEI: + case GSM_MI_TYPE_IMSI: + break; + default: + /* TODO logging */ + osmo_fsm_inst_dispatch(fi, E_REJECT, NULL); + return; + } + + ctx->t3370_id_type = ctx->gmm_att_identy_type; + ret = gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_identy_type); + if (ret < 0) { + LOGPFSM(fi, "Can not send tx_gmm_id %d.\n", ret); + osmo_fsm_inst_dispatch(fi, E_REJECT, NULL); + } +} + +static void st_identity(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + + /* check if we received a identity response */ + long type = (long) data; + switch (type) { + case GSM_MI_TYPE_IMEI: + case GSM_MI_TYPE_IMSI: + break; + default: + LOGMMCTXP(LOGL_ERROR, ctx, "Unknown mi type: 0x%lx, rejecting MS.\n", type); + osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL); + return; + } + + if (type != ctx->gmm_att_identy_type) { + /* ignore wrong package */ + /* TODO logging */ + return; + } + + if (type == GSM_MI_TYPE_IMEI && !strlen(ctx->imsi)) { + ctx->gmm_att_identy_type = GSM_MI_TYPE_IMSI; + osmo_fsm_inst_state_chg(fi, ST_IDENTIY, sgsn->cfg.timers.T3370, 3370); + } else if (require_auth) + osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360); + else + osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350); +} + +static void st_auth_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + enum sgsn_auth_state auth_state; + + ctx->num_T_exp = 0; + + /* TODO: remove this layer violation. Don't parse any auth_policy here + * The correct way would be to ask the SGSN is this mmctx has to be auth + * regardless of the state. + * Otherwise someone else could steal the TLLI and just use it without further + * auth. + */ + if (sgsn->cfg.auth_policy != SGSN_AUTH_POLICY_REMOTE) { + /* we can "trust" sgsn_auth_state as long it's not remote */ + auth_state = sgsn_auth_state(ctx); + } else { + auth_state = ctx->auth_state; + } + + switch(auth_state) { + case SGSN_AUTH_UNKNOWN: /* the SGSN doesn know this MS */ + osmo_fsm_inst_state_chg(fi, ST_ASK_VLR, sgsn->cfg.timers.T3350, 3350); + break; + case SGSN_AUTH_REJECTED: + /* TODO: correct GMM cause */ + osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_GPRS_NOTALLOWED); + break; + case SGSN_AUTH_ACCEPTED: + osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350); + break; + case SGSN_AUTH_AUTHENTICATE: + if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) { + /* invalid key material */ + osmo_fsm_inst_state_chg(fi, ST_ASK_VLR, sgsn->cfg.timers.T3350, 3350); + } + + struct gsm_auth_tuple *at = &ctx->auth_triplet; + if (gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, + false) < 0) { + /* network failure */ + osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL); + } + break; + case SGSN_AUTH_UMTS_RESYNC: + /* FIXME!! */ + break; + } +} + +static void st_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + + if (event == E_AUTH_RESP_RECV) { + sgsn_auth_request(ctx); + osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350); + } +} + +static void st_accept_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + + ctx->num_T_exp = 0; + + /* TODO: remove pending_req as soon the sgsn_auth code doesn't depend on it */ + ctx->pending_req = 0; + gsm48_tx_gmm_att_ack(ctx); +} + +static void st_accept(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + + switch(event) { + case E_ATTACH_COMPLETE_RECV: + /* TODO: #ifdef ! PTMSI_ALLOC is not supported */ + extract_subscr_msisdn(ctx); + extract_subscr_hlr(ctx); + osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0); + break; + } +} + +static void st_reject(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + long reject_cause = (long) data; + + if (reject_cause != GMM_DISCARD_MS_WITHOUT_REJECT) + gsm48_tx_gmm_att_rej(ctx, (uint8_t) reject_cause); + + sgsn_mm_ctx_cleanup_free(ctx); +} + +static void st_ask_vlr_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + + /* ask the auth layer for more data */ + sgsn_auth_request(ctx); +} + +static void st_ask_vlr(struct osmo_fsm_inst *fi, uint32_t event, void *data) +{ + switch(event) { + case E_VLR_ANSWERED: + osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360); + break; + } +} + +static struct osmo_fsm_state gmm_attach_req_fsm_states[] = { + /* default state for non-DTX and DTX when SPEECH is in progress */ + [ST_INIT] = { + .in_event_mask = X(E_ATTACH_REQ_RECV), + .out_state_mask = X(ST_INIT) | X(ST_IDENTIY) | X(ST_AUTH) | X(ST_ACCEPT), + .name = "Init", + .action = st_init, + }, + [ST_ASK_VLR] = { + .in_event_mask = X(E_VLR_ANSWERED), + .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT), + .name = "Ask the hlr about the MS", + .onenter = st_ask_vlr_on_enter, + .action = st_ask_vlr, + }, + [ST_IDENTIY] = { + .in_event_mask = X(E_IDEN_RESP_RECV), + .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_IDENTIY) | X(ST_REJECT), + .onenter = st_identity_on_enter, + .name = "Check Identity of the MS (IMSI/IMEI)", + .action = st_identity, + }, + [ST_AUTH] = { + .in_event_mask = X(E_AUTH_RESP_RECV), + .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_ASK_VLR) | X(ST_REJECT), + .name = "Authenticate", + .onenter = st_auth_on_enter, + .action = st_auth, + }, + [ST_ACCEPT] = { + .in_event_mask = X(E_ATTACH_COMPLETE_RECV), + .out_state_mask = X(ST_INIT) | X(ST_REJECT), + .name = "Wait for Authentication Response", + .onenter = st_accept_on_enter, + .action = st_accept, + }, + [ST_REJECT] = { + .in_event_mask = X(E_REJECT), + .out_state_mask = X(ST_INIT), + .name = "Reject the MS", + .action = st_reject, + }, +}; + +const struct value_string gmm_attach_req_fsm_event_names[] = { + { E_ATTACH_REQ_RECV, "Received an attach request" }, + { E_IDEN_RESP_RECV, "Identity Request received" }, + { E_AUTH_RESP_RECV, "Authentication Request received" }, + { E_ATTACH_ACCEPTED, "Attach accepted" }, + { E_ATTACH_ACCEPT_SENT, "Attach accept sent" }, + { E_ATTACH_COMPLETE_RECV, "Attach complete received." }, + { E_REJECT, "Reject the MS"}, + { E_VLR_ANSWERED, "VLR answered"}, + { 0, NULL } +}; + +void gmm_attach_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) { + switch (event) { + case E_ATTACH_REQ_RECV: + /* always call the st_init handler */ + if (fi->state != ST_INIT) + osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0); + st_init(fi, event, data); + break; + case E_REJECT: + if (fi->state != ST_REJECT) + osmo_fsm_inst_state_chg(fi, ST_REJECT, 0, 0); + st_reject(fi, event, data); + break; + } +} + +int gmm_attach_timer_cb(struct osmo_fsm_inst *fi) +{ + struct sgsn_mm_ctx *ctx = fi->priv; + struct gsm_auth_tuple *at = &ctx->auth_triplet; + + ctx->num_T_exp++; + + switch(fi->state) { + case ST_ASK_VLR: + /* TODO: replace T3350 by a better timer or it's own + * re-use T3350 - not defined by standard */ + LOGMMCTXP(LOGL_ERROR, ctx, "HLR did not answer in time. Rejecting.\n"); + osmo_fsm_inst_dispatch(fi, E_REJECT, + (void *) GMM_CAUSE_NET_FAIL); + break; + case ST_IDENTIY: + /* T3370 */ + if (ctx->num_T_exp >= 5) { + osmo_fsm_inst_dispatch(fi, E_REJECT, + (void *) GMM_CAUSE_MS_ID_NOT_DERIVED); + break; + } + gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_identy_type); + osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3370, 0); + break; + case ST_AUTH: + /* T3360 */ + if (ctx->num_T_exp >= 5) { + osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT); + break; + } + gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, false); + osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3360, 0); + break; + case ST_ACCEPT: + /* T3350 */ + if (ctx->num_T_exp >= 5) { + osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT); + break; + } + gsm48_tx_gmm_att_ack(ctx); + osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3350, 0); + break; + } + + return 0; +} + +struct osmo_fsm gmm_attach_req_fsm = { + .name = "GMM_ATTACH_REQ_FSM", + .states = gmm_attach_req_fsm_states, + .num_states = ARRAY_SIZE(gmm_attach_req_fsm_states), + .event_names = gmm_attach_req_fsm_event_names, + .allstate_event_mask = X(E_REJECT) | X(E_ATTACH_REQ_RECV), + .allstate_action = gmm_attach_allstate_action, + .log_subsys = DMM, + .timer_cb = gmm_attach_timer_cb, +}; + +static __attribute__((constructor)) void l2tp_fsm_init(void) +{ + osmo_fsm_register(&gmm_attach_req_fsm); +} diff --git a/src/gprs/gprs_gmm_attach.h b/src/gprs/gprs_gmm_attach.h new file mode 100644 index 0000000..a1a4f61 --- /dev/null +++ b/src/gprs/gprs_gmm_attach.h @@ -0,0 +1,32 @@ +#ifndef GPRS_GMM_ATTACH_H +#define GPRS_GMM_ATTACH_H + +#include <osmocom/core/fsm.h> + +enum gmm_attach_req_fsm_states { + ST_INIT, + ST_IDENTIY, + ST_RETRIEVE_AUTH, + ST_AUTH, + ST_ASK_VLR, + ST_ACCEPT, + ST_REJECT +}; + +enum gmm_attach_req_fsm_events { + E_ATTACH_REQ_RECV, + E_IDEN_RESP_RECV, + E_AUTH_RESP_RECV, + E_ATTACH_ACCEPTED, + E_ATTACH_ACCEPT_SENT, + E_ATTACH_COMPLETE_RECV, + E_REJECT, + E_VLR_ANSWERED, +}; + +#define GMM_DISCARD_MS_WITHOUT_REJECT -1 + +extern const struct value_string gmm_attach_req_fsm_event_names[]; +extern struct osmo_fsm gmm_attach_req_fsm; + +#endif // GPRS_GMM_ATTACH_H diff --git a/src/gprs/gprs_sgsn.c b/src/gprs/gprs_sgsn.c index 688eaed..c942d19 100644 --- a/src/gprs/gprs_sgsn.c +++ b/src/gprs/gprs_sgsn.c @@ -47,6 +47,7 @@ #include <time.h> #include "../../bscconfig.h" +#include "gprs_gmm_attach.h" #if BUILD_IU #include <osmocom/ranap/iu_client.h> @@ -241,6 +242,7 @@ talloc_free(ctx); return NULL; } + ctx->gmm_att_req_fsm = osmo_fsm_inst_alloc(&gmm_attach_req_fsm, ctx, ctx, LOGL_DEBUG, "gb_gmm_req"); INIT_LLIST_HEAD(&ctx->pdp_list); llist_add(&ctx->list, &sgsn_mm_ctxts); @@ -273,6 +275,7 @@ talloc_free(ctx); return NULL; } + ctx->gmm_att_req_fsm = osmo_fsm_inst_alloc(&gmm_attach_req_fsm, ctx, ctx, LOGL_DEBUG, "iu_gmm_req"); /* Need to get RAID from IU conn */ ctx->ra = ctx->iu.ue_ctx->ra_id; @@ -350,6 +353,9 @@ gprs_subscr_put(subscr); } + if (mm->gmm_att_req_fsm) + osmo_fsm_inst_free(mm->gmm_att_req_fsm); + sgsn_mm_ctx_free(mm); mm = NULL; diff --git a/tests/sgsn/Makefile.am b/tests/sgsn/Makefile.am index 8692fc3..5972594 100644 --- a/tests/sgsn/Makefile.am +++ b/tests/sgsn/Makefile.am @@ -45,6 +45,7 @@ $(top_builddir)/src/gprs/gprs_llc.o \ $(top_builddir)/src/gprs/crc24.o \ $(top_builddir)/src/gprs/gprs_sndcp.o \ + $(top_builddir)/src/gprs/gprs_gmm_attach.o \ $(top_builddir)/src/gprs/gprs_gmm.o \ $(top_builddir)/src/gprs/gprs_sgsn.o \ $(top_builddir)/src/gprs/sgsn_vty.o \ -- To view, visit https://gerrit.osmocom.org/9257 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: osmo-sgsn Gerrit-Branch: master Gerrit-MessageType: newchange Gerrit-Change-Id: I58b9c17be9776a03bb2a5b21e99135cfefc8c912 Gerrit-Change-Number: 9257 Gerrit-PatchSet: 1 Gerrit-Owner: lynxis lazus <lynxis at fe80.eu> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180522/856ee12c/attachment.htm>