Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 1 | #include <osmocom/core/tdef.h> |
Neels Hofmeyr | 3c7656a | 2022-03-07 15:38:59 +0100 | [diff] [blame] | 2 | #include <osmocom/crypt/utran_cipher.h> |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 3 | |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 4 | #include <osmocom/sgsn/gprs_gmm_attach.h> |
| 5 | |
| 6 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
| 7 | #include <osmocom/sgsn/debug.h> |
| 8 | #include <osmocom/sgsn/gprs_gmm.h> |
Pau Espin Pedrol | 58101ea | 2023-01-09 12:29:27 +0100 | [diff] [blame^] | 9 | #include <osmocom/sgsn/mmctx.h> |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 10 | #include <osmocom/sgsn/sgsn.h> |
| 11 | |
| 12 | #define X(s) (1 << (s)) |
| 13 | |
| 14 | static int require_identity_imei = 1; |
| 15 | static int require_auth = 1; |
| 16 | |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 17 | static const struct osmo_tdef_state_timeout gmm_attach_fsm_timeouts[32] = { |
| 18 | [ST_IDENTIY] = { .T=3370 }, |
| 19 | [ST_AUTH] = { .T=3360 }, |
| 20 | [ST_ACCEPT] = { .T=3350 }, |
| 21 | [ST_ASK_VLR] = { .T=3350 }, |
| 22 | [ST_IU_SECURITY_CMD] = { .T=3350 }, |
| 23 | }; |
| 24 | |
| 25 | #define gmm_attach_fsm_state_chg(fi, NEXT_STATE) \ |
| 26 | osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, gmm_attach_fsm_timeouts, sgsn->cfg.T_defs, -1) |
| 27 | |
| 28 | |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 29 | static void st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 30 | { |
| 31 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 32 | struct msgb *attach_req = data; |
| 33 | |
| 34 | /* we can run st_init multiple times */ |
| 35 | if (ctx->gmm_att_req.attach_req) |
| 36 | msgb_free(ctx->gmm_att_req.attach_req); |
| 37 | |
| 38 | ctx->gmm_att_req.attach_req = msgb_copy(attach_req, "Attach Request"); |
| 39 | ctx->auth_state = SGSN_AUTH_UNKNOWN; |
| 40 | ctx->gmm_att_req.auth_reattempt = 0; |
| 41 | |
| 42 | /* |
| 43 | * TODO: remove pending_req as soon the sgsn_auth code doesn't depend |
| 44 | * on it. |
| 45 | * pending_req must be set, even this fsm doesn't use it, because |
| 46 | * the sgsn_auth code is using this too |
| 47 | */ |
| 48 | ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ; |
| 49 | |
| 50 | if (require_identity_imei) { |
| 51 | ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMEI; |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 52 | gmm_attach_fsm_state_chg(fi, ST_IDENTIY); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 53 | } else if (!strlen(ctx->imsi)) { |
| 54 | ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMSI; |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 55 | gmm_attach_fsm_state_chg(fi, ST_IDENTIY); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 56 | } else if (require_auth) |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 57 | gmm_attach_fsm_state_chg(fi, ST_AUTH); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 58 | else |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 59 | gmm_attach_fsm_state_chg(fi, ST_ACCEPT); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void st_identity_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 63 | { |
| 64 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 65 | int ret = 0; |
| 66 | |
| 67 | ctx->num_T_exp = 0; |
| 68 | |
| 69 | switch (ctx->gmm_att_req.id_type) { |
| 70 | case GSM_MI_TYPE_IMEI: |
| 71 | case GSM_MI_TYPE_IMSI: |
| 72 | break; |
| 73 | default: |
| 74 | /* TODO logging */ |
| 75 | osmo_fsm_inst_dispatch(fi, E_REJECT, NULL); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | ctx->t3370_id_type = ctx->gmm_att_req.id_type; |
| 80 | ret = gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type); |
| 81 | if (ret < 0) { |
| 82 | LOGPFSM(fi, "Can not send tx_gmm_id %d.\n", ret); |
| 83 | osmo_fsm_inst_dispatch(fi, E_REJECT, NULL); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void st_identity(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 88 | { |
| 89 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 90 | |
| 91 | OSMO_ASSERT(event == E_IDEN_RESP_RECV); |
| 92 | |
| 93 | /* check if we received a identity response */ |
| 94 | long type = (long) data; |
| 95 | switch (type) { |
| 96 | case GSM_MI_TYPE_IMEI: |
| 97 | case GSM_MI_TYPE_IMSI: |
| 98 | break; |
| 99 | default: |
| 100 | LOGMMCTXP(LOGL_ERROR, ctx, "Unknown mi type: 0x%lx, rejecting MS.\n", type); |
| 101 | osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (type != ctx->gmm_att_req.id_type) { |
| 106 | /* ignore wrong package */ |
| 107 | /* TODO logging */ |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (type == GSM_MI_TYPE_IMEI && !strlen(ctx->imsi)) { |
| 112 | ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMSI; |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 113 | gmm_attach_fsm_state_chg(fi, ST_IDENTIY); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 114 | } else if (require_auth) |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 115 | gmm_attach_fsm_state_chg(fi, ST_AUTH); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 116 | else |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 117 | gmm_attach_fsm_state_chg(fi, ST_ACCEPT); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void st_auth_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 121 | { |
| 122 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 123 | enum sgsn_auth_state auth_state; |
| 124 | |
| 125 | ctx->num_T_exp = 0; |
| 126 | |
| 127 | /* TODO: remove this layer violation. Don't parse any auth_policy here |
| 128 | * The correct way would be to ask the SGSN is this mmctx has to be auth |
| 129 | * regardless of the state. |
| 130 | * Otherwise someone else could steal the TLLI and just use it without further |
| 131 | * auth. |
| 132 | */ |
| 133 | if (sgsn->cfg.auth_policy != SGSN_AUTH_POLICY_REMOTE) { |
| 134 | /* we can "trust" sgsn_auth_state as long it's not remote */ |
| 135 | auth_state = sgsn_auth_state(ctx); |
| 136 | } else { |
| 137 | auth_state = ctx->auth_state; |
| 138 | } |
| 139 | |
| 140 | switch(auth_state) { |
| 141 | case SGSN_AUTH_UMTS_RESYNC: /* ask the vlr for a new vector to match the simcards seq */ |
| 142 | case SGSN_AUTH_UNKNOWN: /* the SGSN doesn know this MS */ |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 143 | gmm_attach_fsm_state_chg(fi, ST_ASK_VLR); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 144 | break; |
| 145 | case SGSN_AUTH_REJECTED: |
| 146 | /* TODO: correct GMM cause */ |
| 147 | osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_GPRS_NOTALLOWED); |
| 148 | break; |
| 149 | case SGSN_AUTH_ACCEPTED: |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 150 | gmm_attach_fsm_state_chg(fi, ST_ACCEPT); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 151 | break; |
| 152 | case SGSN_AUTH_AUTHENTICATE: |
| 153 | if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) { |
| 154 | /* invalid key material */ |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 155 | gmm_attach_fsm_state_chg(fi, ST_ASK_VLR); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | struct gsm_auth_tuple *at = &ctx->auth_triplet; |
| 159 | if (gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, |
| 160 | false) < 0) { |
| 161 | /* network failure */ |
| 162 | osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL); |
| 163 | } |
| 164 | ctx->gmm_att_req.auth_reattempt++; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void st_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 170 | { |
| 171 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 172 | |
| 173 | switch (event) { |
| 174 | case E_AUTH_RESP_RECV_SUCCESS: |
| 175 | sgsn_auth_request(ctx); |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 176 | #ifdef BUILD_IU |
| 177 | if (ctx->ran_type == MM_CTX_T_UTRAN_Iu && !ctx->iu.ue_ctx->integrity_active) |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 178 | gmm_attach_fsm_state_chg(fi, ST_IU_SECURITY_CMD); |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 179 | else |
| 180 | #endif /* BUILD_IU */ |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 181 | gmm_attach_fsm_state_chg(fi, ST_ACCEPT); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 182 | break; |
| 183 | case E_AUTH_RESP_RECV_RESYNC: |
| 184 | if (ctx->gmm_att_req.auth_reattempt <= 1) |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 185 | gmm_attach_fsm_state_chg(fi, ST_ASK_VLR); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 186 | else |
| 187 | osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_SYNC_FAIL); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void st_accept_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 193 | { |
| 194 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 195 | |
| 196 | ctx->num_T_exp = 0; |
| 197 | |
| 198 | /* TODO: remove pending_req as soon the sgsn_auth code doesn't depend on it */ |
| 199 | ctx->pending_req = 0; |
| 200 | gsm48_tx_gmm_att_ack(ctx); |
| 201 | } |
| 202 | |
| 203 | static void st_accept(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 204 | { |
| 205 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 206 | |
| 207 | switch(event) { |
| 208 | case E_ATTACH_COMPLETE_RECV: |
| 209 | /* TODO: #ifdef ! PTMSI_ALLOC is not supported */ |
| 210 | extract_subscr_msisdn(ctx); |
| 211 | extract_subscr_hlr(ctx); |
| 212 | osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0); |
| 213 | break; |
Max | b6f8b74 | 2019-02-13 14:48:59 +0100 | [diff] [blame] | 214 | case E_VLR_ANSWERED: |
| 215 | extract_subscr_msisdn(ctx); |
| 216 | extract_subscr_hlr(ctx); |
| 217 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 218 | "Unusual event: if MS got no data connection, check that it has APN configured.\n"); |
| 219 | break; |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | static void st_reject(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 224 | { |
| 225 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 226 | long reject_cause = (long) data; |
| 227 | |
| 228 | if (reject_cause != GMM_DISCARD_MS_WITHOUT_REJECT) |
| 229 | gsm48_tx_gmm_att_rej(ctx, (uint8_t) reject_cause); |
| 230 | |
| 231 | sgsn_mm_ctx_cleanup_free(ctx); |
| 232 | } |
| 233 | |
| 234 | static void st_ask_vlr_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 235 | { |
| 236 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 237 | |
| 238 | /* FIXME: remove this layer violation. |
| 239 | * The VLR should send the message to the HLR and not the rx function |
| 240 | * gsm48_rx_gmm_auth_ciph_fail. Because gmm_auth_ciph_fail already send a |
| 241 | * message to the HLR, we don't send here a request. */ |
| 242 | if (ctx->auth_state == SGSN_AUTH_UMTS_RESYNC) |
| 243 | return; |
| 244 | |
| 245 | /* ask the auth layer for more data */ |
| 246 | sgsn_auth_request(ctx); |
| 247 | } |
| 248 | |
| 249 | static void st_ask_vlr(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 250 | { |
| 251 | switch(event) { |
| 252 | case E_VLR_ANSWERED: |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 253 | gmm_attach_fsm_state_chg(fi, ST_AUTH); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 258 | static void st_iu_security_cmd_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 259 | { |
| 260 | #ifdef BUILD_IU |
| 261 | struct sgsn_mm_ctx *ctx = fi->priv; |
Neels Hofmeyr | 3c7656a | 2022-03-07 15:38:59 +0100 | [diff] [blame] | 262 | bool send_ck; |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 263 | |
| 264 | /* TODO: shouldn't this set always? not only when the integrity_active? */ |
| 265 | if (ctx->iu.ue_ctx->integrity_active) { |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 266 | gmm_attach_fsm_state_chg(fi, ST_ACCEPT); |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
Neels Hofmeyr | 3c7656a | 2022-03-07 15:38:59 +0100 | [diff] [blame] | 270 | /* Is any encryption above UEA0 enabled? */ |
| 271 | send_ck = sgsn->cfg.uea_encryption_mask > (1 << OSMO_UTRAN_UEA0); |
| 272 | LOGMMCTXP(LOGL_DEBUG, ctx, "Iu Security Mode Command: %s encryption key (UEA encryption mask = 0x%x)\n", |
| 273 | send_ck ? "sending" : "not sending", sgsn->cfg.uea_encryption_mask); |
| 274 | |
| 275 | /* FIXME: we should send the set of allowed UEA, as in ranap_new_msg_sec_mod_cmd2(). However, this |
| 276 | * is not possible in the iu_client API. See OS#5487. */ |
| 277 | ranap_iu_tx_sec_mode_cmd(ctx->iu.ue_ctx, &ctx->auth_triplet.vec, send_ck, ctx->iu.new_key); |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 278 | ctx->iu.new_key = 0; |
| 279 | #endif |
| 280 | } |
| 281 | |
| 282 | static void st_iu_security_cmd(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 283 | { |
| 284 | switch(event) { |
Pau Espin Pedrol | ce0a0e9 | 2021-03-25 16:35:04 +0100 | [diff] [blame] | 285 | case E_VLR_ANSWERED: |
| 286 | /* We may receive an event due to rx |
| 287 | * OSMO_GSUP_MSGT_INSERT_DATA_REQUEST here. Do nothing, update of |
| 288 | * subscriber is done by lower layers before event is signalled. |
| 289 | * In this state we simply wait for E_IU_SECURITY_CMD_COMPLETE |
| 290 | */ |
| 291 | break; |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 292 | case E_IU_SECURITY_CMD_COMPLETE: |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 293 | gmm_attach_fsm_state_chg(fi, ST_ACCEPT); |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 298 | static struct osmo_fsm_state gmm_attach_req_fsm_states[] = { |
| 299 | /* default state for non-DTX and DTX when SPEECH is in progress */ |
| 300 | [ST_INIT] = { |
| 301 | .in_event_mask = X(E_ATTACH_REQ_RECV), |
| 302 | .out_state_mask = X(ST_INIT) | X(ST_IDENTIY) | X(ST_AUTH) | X(ST_ACCEPT), |
| 303 | .name = "Init", |
| 304 | .action = st_init, |
| 305 | }, |
| 306 | [ST_ASK_VLR] = { |
| 307 | .in_event_mask = X(E_VLR_ANSWERED), |
| 308 | .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT), |
| 309 | .name = "AskVLR", |
| 310 | .onenter = st_ask_vlr_on_enter, |
| 311 | .action = st_ask_vlr, |
| 312 | }, |
| 313 | [ST_IDENTIY] = { |
| 314 | .in_event_mask = X(E_IDEN_RESP_RECV), |
| 315 | .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_IDENTIY) | X(ST_REJECT), |
| 316 | .onenter = st_identity_on_enter, |
| 317 | .name = "CheckIdentity", |
| 318 | .action = st_identity, |
| 319 | }, |
| 320 | [ST_AUTH] = { |
| 321 | .in_event_mask = X(E_AUTH_RESP_RECV_SUCCESS) | X(E_AUTH_RESP_RECV_RESYNC), |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 322 | .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_IU_SECURITY_CMD) | X(ST_ACCEPT) | X(ST_ASK_VLR) | X(ST_REJECT), |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 323 | .name = "Authenticate", |
| 324 | .onenter = st_auth_on_enter, |
| 325 | .action = st_auth, |
| 326 | }, |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 327 | [ST_IU_SECURITY_CMD] = { |
Pau Espin Pedrol | ce0a0e9 | 2021-03-25 16:35:04 +0100 | [diff] [blame] | 328 | .in_event_mask = X(E_IU_SECURITY_CMD_COMPLETE) | X(E_VLR_ANSWERED), |
Alexander Couzens | 5455756 | 2018-09-27 21:15:30 +0200 | [diff] [blame] | 329 | .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT), |
| 330 | .name = "IuSecurityCommand", |
| 331 | .onenter = st_iu_security_cmd_on_enter, |
| 332 | .action = st_iu_security_cmd, |
| 333 | }, |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 334 | [ST_ACCEPT] = { |
Max | b6f8b74 | 2019-02-13 14:48:59 +0100 | [diff] [blame] | 335 | .in_event_mask = X(E_ATTACH_COMPLETE_RECV) | X(E_VLR_ANSWERED), |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 336 | .out_state_mask = X(ST_INIT) | X(ST_REJECT), |
| 337 | .name = "WaitAttachComplete", |
| 338 | .onenter = st_accept_on_enter, |
| 339 | .action = st_accept, |
| 340 | }, |
| 341 | [ST_REJECT] = { |
| 342 | .in_event_mask = X(E_REJECT), |
| 343 | .out_state_mask = X(ST_INIT), |
| 344 | .name = "Reject", |
| 345 | .action = st_reject, |
| 346 | }, |
| 347 | }; |
| 348 | |
| 349 | const struct value_string gmm_attach_req_fsm_event_names[] = { |
Pau Espin Pedrol | f913265 | 2019-08-14 16:55:12 +0200 | [diff] [blame] | 350 | OSMO_VALUE_STRING(E_ATTACH_REQ_RECV), |
| 351 | OSMO_VALUE_STRING(E_IDEN_RESP_RECV), |
| 352 | OSMO_VALUE_STRING(E_AUTH_RESP_RECV_SUCCESS), |
| 353 | OSMO_VALUE_STRING(E_AUTH_RESP_RECV_RESYNC), |
| 354 | OSMO_VALUE_STRING(E_ATTACH_ACCEPTED), |
| 355 | OSMO_VALUE_STRING(E_ATTACH_ACCEPT_SENT), |
| 356 | OSMO_VALUE_STRING(E_ATTACH_COMPLETE_RECV), |
| 357 | OSMO_VALUE_STRING(E_IU_SECURITY_CMD_COMPLETE), |
| 358 | OSMO_VALUE_STRING(E_REJECT), |
| 359 | OSMO_VALUE_STRING(E_VLR_ANSWERED), |
| 360 | { 0, NULL } |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | void gmm_attach_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) { |
| 364 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 365 | struct msgb *new_attach_req = data; |
| 366 | |
| 367 | switch (event) { |
| 368 | case E_ATTACH_REQ_RECV: |
| 369 | switch (fi->state) { |
| 370 | case ST_INIT: |
| 371 | case ST_REJECT: |
| 372 | st_init(fi, event, data); |
| 373 | break; |
| 374 | |
| 375 | case ST_ACCEPT: |
| 376 | /* TODO: drop all state (e.g. PDP Ctx) and do this procedure */ |
| 377 | osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0); |
| 378 | st_init(fi, event, data); |
| 379 | break; |
| 380 | |
| 381 | case ST_ASK_VLR: |
| 382 | case ST_AUTH: |
| 383 | case ST_IDENTIY: |
| 384 | case ST_RETRIEVE_AUTH: |
| 385 | /* 04.08 4.7.3.1.6 d) Abnormal Case |
| 386 | * Only do action if Req IEs differs. */ |
| 387 | if (ctx->gmm_att_req.attach_req && |
| 388 | gprs_gmm_attach_req_ies(new_attach_req, ctx->gmm_att_req.attach_req)) { |
| 389 | osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0); |
| 390 | st_init(fi, event, data); |
| 391 | } |
| 392 | break; |
| 393 | } |
| 394 | break; |
| 395 | case E_REJECT: |
| 396 | if (fi->state != ST_REJECT) |
| 397 | osmo_fsm_inst_state_chg(fi, ST_REJECT, 0, 0); |
| 398 | st_reject(fi, event, data); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | int gmm_attach_timer_cb(struct osmo_fsm_inst *fi) |
| 404 | { |
| 405 | struct sgsn_mm_ctx *ctx = fi->priv; |
| 406 | struct gsm_auth_tuple *at = &ctx->auth_triplet; |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 407 | unsigned long t_secs; |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 408 | |
| 409 | ctx->num_T_exp++; |
| 410 | |
| 411 | switch(fi->state) { |
| 412 | case ST_ASK_VLR: |
| 413 | /* TODO: replace T3350 by a better timer or it's own |
| 414 | * re-use T3350 - not defined by standard */ |
| 415 | LOGMMCTXP(LOGL_ERROR, ctx, "HLR did not answer in time. Rejecting.\n"); |
| 416 | osmo_fsm_inst_dispatch(fi, E_REJECT, |
| 417 | (void *) GMM_CAUSE_NET_FAIL); |
| 418 | break; |
| 419 | case ST_IDENTIY: |
| 420 | /* T3370 */ |
| 421 | if (ctx->num_T_exp >= 5) { |
| 422 | osmo_fsm_inst_dispatch(fi, E_REJECT, |
| 423 | (void *) GMM_CAUSE_MS_ID_NOT_DERIVED); |
| 424 | break; |
| 425 | } |
| 426 | gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type); |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 427 | t_secs = osmo_tdef_get(sgsn->cfg.T_defs, 3370, OSMO_TDEF_S, -1); |
| 428 | osmo_timer_schedule(&fi->timer, t_secs, 0); |
| 429 | |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 430 | break; |
| 431 | case ST_AUTH: |
| 432 | /* T3360 */ |
| 433 | if (ctx->num_T_exp >= 5) { |
| 434 | osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT); |
| 435 | break; |
| 436 | } |
| 437 | gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, false); |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 438 | t_secs = osmo_tdef_get(sgsn->cfg.T_defs, 3360, OSMO_TDEF_S, -1); |
| 439 | osmo_timer_schedule(&fi->timer, t_secs, 0); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 440 | break; |
| 441 | case ST_ACCEPT: |
| 442 | /* T3350 */ |
| 443 | if (ctx->num_T_exp >= 5) { |
| 444 | osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT); |
| 445 | break; |
| 446 | } |
| 447 | gsm48_tx_gmm_att_ack(ctx); |
Pau Espin Pedrol | a299d65 | 2019-08-14 19:11:10 +0200 | [diff] [blame] | 448 | t_secs = osmo_tdef_get(sgsn->cfg.T_defs, 3350, OSMO_TDEF_S, -1); |
| 449 | osmo_timer_schedule(&fi->timer, t_secs, 0); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | struct osmo_fsm gmm_attach_req_fsm = { |
| 457 | .name = "GMM_ATTACH_REQ_FSM", |
| 458 | .states = gmm_attach_req_fsm_states, |
| 459 | .num_states = ARRAY_SIZE(gmm_attach_req_fsm_states), |
| 460 | .event_names = gmm_attach_req_fsm_event_names, |
| 461 | .allstate_event_mask = X(E_REJECT) | X(E_ATTACH_REQ_RECV), |
| 462 | .allstate_action = gmm_attach_allstate_action, |
| 463 | .log_subsys = DMM, |
| 464 | .timer_cb = gmm_attach_timer_cb, |
| 465 | }; |
| 466 | |
| 467 | static __attribute__((constructor)) void gprs_gmm_fsm_init(void) |
| 468 | { |
Harald Welte | a868e17 | 2019-12-01 13:23:53 +0100 | [diff] [blame] | 469 | OSMO_ASSERT(osmo_fsm_register(&gmm_attach_req_fsm) == 0); |
Alexander Couzens | f7198d7 | 2018-05-22 18:29:14 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void gmm_att_req_free(struct sgsn_mm_ctx *mm) { |
| 473 | if (mm->gmm_att_req.fsm) |
| 474 | osmo_fsm_inst_free(mm->gmm_att_req.fsm); |
| 475 | |
| 476 | if (mm->gmm_att_req.attach_req) |
| 477 | msgb_free(mm->gmm_att_req.attach_req); |
| 478 | } |