blob: c903a016b3f5f8672c7ff2462e8b268443d11c30 [file] [log] [blame]
Pau Espin Pedrola299d652019-08-14 19:11:10 +02001#include <osmocom/core/tdef.h>
2
Alexander Couzensf7198d72018-05-22 18:29:14 +02003#include <osmocom/sgsn/gprs_gmm_attach.h>
4
5#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
6#include <osmocom/sgsn/debug.h>
7#include <osmocom/sgsn/gprs_gmm.h>
8#include <osmocom/sgsn/sgsn.h>
9
10#define X(s) (1 << (s))
11
12static int require_identity_imei = 1;
13static int require_auth = 1;
14
Pau Espin Pedrola299d652019-08-14 19:11:10 +020015static const struct osmo_tdef_state_timeout gmm_attach_fsm_timeouts[32] = {
16 [ST_IDENTIY] = { .T=3370 },
17 [ST_AUTH] = { .T=3360 },
18 [ST_ACCEPT] = { .T=3350 },
19 [ST_ASK_VLR] = { .T=3350 },
20 [ST_IU_SECURITY_CMD] = { .T=3350 },
21};
22
23#define gmm_attach_fsm_state_chg(fi, NEXT_STATE) \
24 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, gmm_attach_fsm_timeouts, sgsn->cfg.T_defs, -1)
25
26
Alexander Couzensf7198d72018-05-22 18:29:14 +020027static void st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
28{
29 struct sgsn_mm_ctx *ctx = fi->priv;
30 struct msgb *attach_req = data;
31
32 /* we can run st_init multiple times */
33 if (ctx->gmm_att_req.attach_req)
34 msgb_free(ctx->gmm_att_req.attach_req);
35
36 ctx->gmm_att_req.attach_req = msgb_copy(attach_req, "Attach Request");
37 ctx->auth_state = SGSN_AUTH_UNKNOWN;
38 ctx->gmm_att_req.auth_reattempt = 0;
39
40 /*
41 * TODO: remove pending_req as soon the sgsn_auth code doesn't depend
42 * on it.
43 * pending_req must be set, even this fsm doesn't use it, because
44 * the sgsn_auth code is using this too
45 */
46 ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;
47
48 if (require_identity_imei) {
49 ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMEI;
Pau Espin Pedrola299d652019-08-14 19:11:10 +020050 gmm_attach_fsm_state_chg(fi, ST_IDENTIY);
Alexander Couzensf7198d72018-05-22 18:29:14 +020051 } else if (!strlen(ctx->imsi)) {
52 ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrola299d652019-08-14 19:11:10 +020053 gmm_attach_fsm_state_chg(fi, ST_IDENTIY);
Alexander Couzensf7198d72018-05-22 18:29:14 +020054 } else if (require_auth)
Pau Espin Pedrola299d652019-08-14 19:11:10 +020055 gmm_attach_fsm_state_chg(fi, ST_AUTH);
Alexander Couzensf7198d72018-05-22 18:29:14 +020056 else
Pau Espin Pedrola299d652019-08-14 19:11:10 +020057 gmm_attach_fsm_state_chg(fi, ST_ACCEPT);
Alexander Couzensf7198d72018-05-22 18:29:14 +020058}
59
60static void st_identity_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
61{
62 struct sgsn_mm_ctx *ctx = fi->priv;
63 int ret = 0;
64
65 ctx->num_T_exp = 0;
66
67 switch (ctx->gmm_att_req.id_type) {
68 case GSM_MI_TYPE_IMEI:
69 case GSM_MI_TYPE_IMSI:
70 break;
71 default:
72 /* TODO logging */
73 osmo_fsm_inst_dispatch(fi, E_REJECT, NULL);
74 return;
75 }
76
77 ctx->t3370_id_type = ctx->gmm_att_req.id_type;
78 ret = gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type);
79 if (ret < 0) {
80 LOGPFSM(fi, "Can not send tx_gmm_id %d.\n", ret);
81 osmo_fsm_inst_dispatch(fi, E_REJECT, NULL);
82 }
83}
84
85static void st_identity(struct osmo_fsm_inst *fi, uint32_t event, void *data)
86{
87 struct sgsn_mm_ctx *ctx = fi->priv;
88
89 OSMO_ASSERT(event == E_IDEN_RESP_RECV);
90
91 /* check if we received a identity response */
92 long type = (long) data;
93 switch (type) {
94 case GSM_MI_TYPE_IMEI:
95 case GSM_MI_TYPE_IMSI:
96 break;
97 default:
98 LOGMMCTXP(LOGL_ERROR, ctx, "Unknown mi type: 0x%lx, rejecting MS.\n", type);
99 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL);
100 return;
101 }
102
103 if (type != ctx->gmm_att_req.id_type) {
104 /* ignore wrong package */
105 /* TODO logging */
106 return;
107 }
108
109 if (type == GSM_MI_TYPE_IMEI && !strlen(ctx->imsi)) {
110 ctx->gmm_att_req.id_type = GSM_MI_TYPE_IMSI;
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200111 gmm_attach_fsm_state_chg(fi, ST_IDENTIY);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200112 } else if (require_auth)
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200113 gmm_attach_fsm_state_chg(fi, ST_AUTH);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200114 else
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200115 gmm_attach_fsm_state_chg(fi, ST_ACCEPT);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200116}
117
118static void st_auth_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
119{
120 struct sgsn_mm_ctx *ctx = fi->priv;
121 enum sgsn_auth_state auth_state;
122
123 ctx->num_T_exp = 0;
124
125 /* TODO: remove this layer violation. Don't parse any auth_policy here
126 * The correct way would be to ask the SGSN is this mmctx has to be auth
127 * regardless of the state.
128 * Otherwise someone else could steal the TLLI and just use it without further
129 * auth.
130 */
131 if (sgsn->cfg.auth_policy != SGSN_AUTH_POLICY_REMOTE) {
132 /* we can "trust" sgsn_auth_state as long it's not remote */
133 auth_state = sgsn_auth_state(ctx);
134 } else {
135 auth_state = ctx->auth_state;
136 }
137
138 switch(auth_state) {
139 case SGSN_AUTH_UMTS_RESYNC: /* ask the vlr for a new vector to match the simcards seq */
140 case SGSN_AUTH_UNKNOWN: /* the SGSN doesn know this MS */
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200141 gmm_attach_fsm_state_chg(fi, ST_ASK_VLR);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200142 break;
143 case SGSN_AUTH_REJECTED:
144 /* TODO: correct GMM cause */
145 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_GPRS_NOTALLOWED);
146 break;
147 case SGSN_AUTH_ACCEPTED:
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200148 gmm_attach_fsm_state_chg(fi, ST_ACCEPT);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200149 break;
150 case SGSN_AUTH_AUTHENTICATE:
151 if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
152 /* invalid key material */
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200153 gmm_attach_fsm_state_chg(fi, ST_ASK_VLR);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200154 }
155
156 struct gsm_auth_tuple *at = &ctx->auth_triplet;
157 if (gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq,
158 false) < 0) {
159 /* network failure */
160 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_NET_FAIL);
161 }
162 ctx->gmm_att_req.auth_reattempt++;
163 break;
164 }
165}
166
167static void st_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data)
168{
169 struct sgsn_mm_ctx *ctx = fi->priv;
170
171 switch (event) {
172 case E_AUTH_RESP_RECV_SUCCESS:
173 sgsn_auth_request(ctx);
Alexander Couzens54557562018-09-27 21:15:30 +0200174#ifdef BUILD_IU
175 if (ctx->ran_type == MM_CTX_T_UTRAN_Iu && !ctx->iu.ue_ctx->integrity_active)
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200176 gmm_attach_fsm_state_chg(fi, ST_IU_SECURITY_CMD);
Alexander Couzens54557562018-09-27 21:15:30 +0200177 else
178#endif /* BUILD_IU */
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200179 gmm_attach_fsm_state_chg(fi, ST_ACCEPT);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200180 break;
181 case E_AUTH_RESP_RECV_RESYNC:
182 if (ctx->gmm_att_req.auth_reattempt <= 1)
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200183 gmm_attach_fsm_state_chg(fi, ST_ASK_VLR);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200184 else
185 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_SYNC_FAIL);
186 break;
187 }
188}
189
190static void st_accept_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
191{
192 struct sgsn_mm_ctx *ctx = fi->priv;
193
194 ctx->num_T_exp = 0;
195
196 /* TODO: remove pending_req as soon the sgsn_auth code doesn't depend on it */
197 ctx->pending_req = 0;
198 gsm48_tx_gmm_att_ack(ctx);
199}
200
201static void st_accept(struct osmo_fsm_inst *fi, uint32_t event, void *data)
202{
203 struct sgsn_mm_ctx *ctx = fi->priv;
204
205 switch(event) {
206 case E_ATTACH_COMPLETE_RECV:
207 /* TODO: #ifdef ! PTMSI_ALLOC is not supported */
208 extract_subscr_msisdn(ctx);
209 extract_subscr_hlr(ctx);
210 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
211 break;
Maxb6f8b742019-02-13 14:48:59 +0100212 case E_VLR_ANSWERED:
213 extract_subscr_msisdn(ctx);
214 extract_subscr_hlr(ctx);
215 LOGMMCTXP(LOGL_NOTICE, ctx,
216 "Unusual event: if MS got no data connection, check that it has APN configured.\n");
217 break;
Alexander Couzensf7198d72018-05-22 18:29:14 +0200218 }
219}
220
221static void st_reject(struct osmo_fsm_inst *fi, uint32_t event, void *data)
222{
223 struct sgsn_mm_ctx *ctx = fi->priv;
224 long reject_cause = (long) data;
225
226 if (reject_cause != GMM_DISCARD_MS_WITHOUT_REJECT)
227 gsm48_tx_gmm_att_rej(ctx, (uint8_t) reject_cause);
228
229 sgsn_mm_ctx_cleanup_free(ctx);
230}
231
232static void st_ask_vlr_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
233{
234 struct sgsn_mm_ctx *ctx = fi->priv;
235
236 /* FIXME: remove this layer violation.
237 * The VLR should send the message to the HLR and not the rx function
238 * gsm48_rx_gmm_auth_ciph_fail. Because gmm_auth_ciph_fail already send a
239 * message to the HLR, we don't send here a request. */
240 if (ctx->auth_state == SGSN_AUTH_UMTS_RESYNC)
241 return;
242
243 /* ask the auth layer for more data */
244 sgsn_auth_request(ctx);
245}
246
247static void st_ask_vlr(struct osmo_fsm_inst *fi, uint32_t event, void *data)
248{
249 switch(event) {
250 case E_VLR_ANSWERED:
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200251 gmm_attach_fsm_state_chg(fi, ST_AUTH);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200252 break;
253 }
254}
255
Alexander Couzens54557562018-09-27 21:15:30 +0200256static void st_iu_security_cmd_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
257{
258#ifdef BUILD_IU
259 struct sgsn_mm_ctx *ctx = fi->priv;
Alexander Couzens54557562018-09-27 21:15:30 +0200260
261 /* TODO: shouldn't this set always? not only when the integrity_active? */
262 if (ctx->iu.ue_ctx->integrity_active) {
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200263 gmm_attach_fsm_state_chg(fi, ST_ACCEPT);
Alexander Couzens54557562018-09-27 21:15:30 +0200264 return;
265 }
266
267 ranap_iu_tx_sec_mode_cmd(ctx->iu.ue_ctx, &ctx->auth_triplet.vec, 0, ctx->iu.new_key);
268 ctx->iu.new_key = 0;
269#endif
270}
271
272static void st_iu_security_cmd(struct osmo_fsm_inst *fi, uint32_t event, void *data)
273{
274 switch(event) {
275 case E_IU_SECURITY_CMD_COMPLETE:
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200276 gmm_attach_fsm_state_chg(fi, ST_ACCEPT);
Alexander Couzens54557562018-09-27 21:15:30 +0200277 break;
278 }
279}
280
Alexander Couzensf7198d72018-05-22 18:29:14 +0200281static struct osmo_fsm_state gmm_attach_req_fsm_states[] = {
282 /* default state for non-DTX and DTX when SPEECH is in progress */
283 [ST_INIT] = {
284 .in_event_mask = X(E_ATTACH_REQ_RECV),
285 .out_state_mask = X(ST_INIT) | X(ST_IDENTIY) | X(ST_AUTH) | X(ST_ACCEPT),
286 .name = "Init",
287 .action = st_init,
288 },
289 [ST_ASK_VLR] = {
290 .in_event_mask = X(E_VLR_ANSWERED),
291 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT),
292 .name = "AskVLR",
293 .onenter = st_ask_vlr_on_enter,
294 .action = st_ask_vlr,
295 },
296 [ST_IDENTIY] = {
297 .in_event_mask = X(E_IDEN_RESP_RECV),
298 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_IDENTIY) | X(ST_REJECT),
299 .onenter = st_identity_on_enter,
300 .name = "CheckIdentity",
301 .action = st_identity,
302 },
303 [ST_AUTH] = {
304 .in_event_mask = X(E_AUTH_RESP_RECV_SUCCESS) | X(E_AUTH_RESP_RECV_RESYNC),
Alexander Couzens54557562018-09-27 21:15:30 +0200305 .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 Couzensf7198d72018-05-22 18:29:14 +0200306 .name = "Authenticate",
307 .onenter = st_auth_on_enter,
308 .action = st_auth,
309 },
Alexander Couzens54557562018-09-27 21:15:30 +0200310 [ST_IU_SECURITY_CMD] = {
311 .in_event_mask = X(E_IU_SECURITY_CMD_COMPLETE),
312 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT),
313 .name = "IuSecurityCommand",
314 .onenter = st_iu_security_cmd_on_enter,
315 .action = st_iu_security_cmd,
316 },
Alexander Couzensf7198d72018-05-22 18:29:14 +0200317 [ST_ACCEPT] = {
Maxb6f8b742019-02-13 14:48:59 +0100318 .in_event_mask = X(E_ATTACH_COMPLETE_RECV) | X(E_VLR_ANSWERED),
Alexander Couzensf7198d72018-05-22 18:29:14 +0200319 .out_state_mask = X(ST_INIT) | X(ST_REJECT),
320 .name = "WaitAttachComplete",
321 .onenter = st_accept_on_enter,
322 .action = st_accept,
323 },
324 [ST_REJECT] = {
325 .in_event_mask = X(E_REJECT),
326 .out_state_mask = X(ST_INIT),
327 .name = "Reject",
328 .action = st_reject,
329 },
330};
331
332const struct value_string gmm_attach_req_fsm_event_names[] = {
Pau Espin Pedrolf9132652019-08-14 16:55:12 +0200333 OSMO_VALUE_STRING(E_ATTACH_REQ_RECV),
334 OSMO_VALUE_STRING(E_IDEN_RESP_RECV),
335 OSMO_VALUE_STRING(E_AUTH_RESP_RECV_SUCCESS),
336 OSMO_VALUE_STRING(E_AUTH_RESP_RECV_RESYNC),
337 OSMO_VALUE_STRING(E_ATTACH_ACCEPTED),
338 OSMO_VALUE_STRING(E_ATTACH_ACCEPT_SENT),
339 OSMO_VALUE_STRING(E_ATTACH_COMPLETE_RECV),
340 OSMO_VALUE_STRING(E_IU_SECURITY_CMD_COMPLETE),
341 OSMO_VALUE_STRING(E_REJECT),
342 OSMO_VALUE_STRING(E_VLR_ANSWERED),
343 { 0, NULL }
Alexander Couzensf7198d72018-05-22 18:29:14 +0200344};
345
346void gmm_attach_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) {
347 struct sgsn_mm_ctx *ctx = fi->priv;
348 struct msgb *new_attach_req = data;
349
350 switch (event) {
351 case E_ATTACH_REQ_RECV:
352 switch (fi->state) {
353 case ST_INIT:
354 case ST_REJECT:
355 st_init(fi, event, data);
356 break;
357
358 case ST_ACCEPT:
359 /* TODO: drop all state (e.g. PDP Ctx) and do this procedure */
360 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
361 st_init(fi, event, data);
362 break;
363
364 case ST_ASK_VLR:
365 case ST_AUTH:
366 case ST_IDENTIY:
367 case ST_RETRIEVE_AUTH:
368 /* 04.08 4.7.3.1.6 d) Abnormal Case
369 * Only do action if Req IEs differs. */
370 if (ctx->gmm_att_req.attach_req &&
371 gprs_gmm_attach_req_ies(new_attach_req, ctx->gmm_att_req.attach_req)) {
372 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
373 st_init(fi, event, data);
374 }
375 break;
376 }
377 break;
378 case E_REJECT:
379 if (fi->state != ST_REJECT)
380 osmo_fsm_inst_state_chg(fi, ST_REJECT, 0, 0);
381 st_reject(fi, event, data);
382 break;
383 }
384}
385
386int gmm_attach_timer_cb(struct osmo_fsm_inst *fi)
387{
388 struct sgsn_mm_ctx *ctx = fi->priv;
389 struct gsm_auth_tuple *at = &ctx->auth_triplet;
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200390 unsigned long t_secs;
Alexander Couzensf7198d72018-05-22 18:29:14 +0200391
392 ctx->num_T_exp++;
393
394 switch(fi->state) {
395 case ST_ASK_VLR:
396 /* TODO: replace T3350 by a better timer or it's own
397 * re-use T3350 - not defined by standard */
398 LOGMMCTXP(LOGL_ERROR, ctx, "HLR did not answer in time. Rejecting.\n");
399 osmo_fsm_inst_dispatch(fi, E_REJECT,
400 (void *) GMM_CAUSE_NET_FAIL);
401 break;
402 case ST_IDENTIY:
403 /* T3370 */
404 if (ctx->num_T_exp >= 5) {
405 osmo_fsm_inst_dispatch(fi, E_REJECT,
406 (void *) GMM_CAUSE_MS_ID_NOT_DERIVED);
407 break;
408 }
409 gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type);
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200410 t_secs = osmo_tdef_get(sgsn->cfg.T_defs, 3370, OSMO_TDEF_S, -1);
411 osmo_timer_schedule(&fi->timer, t_secs, 0);
412
Alexander Couzensf7198d72018-05-22 18:29:14 +0200413 break;
414 case ST_AUTH:
415 /* T3360 */
416 if (ctx->num_T_exp >= 5) {
417 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
418 break;
419 }
420 gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, false);
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200421 t_secs = osmo_tdef_get(sgsn->cfg.T_defs, 3360, OSMO_TDEF_S, -1);
422 osmo_timer_schedule(&fi->timer, t_secs, 0);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200423 break;
424 case ST_ACCEPT:
425 /* T3350 */
426 if (ctx->num_T_exp >= 5) {
427 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
428 break;
429 }
430 gsm48_tx_gmm_att_ack(ctx);
Pau Espin Pedrola299d652019-08-14 19:11:10 +0200431 t_secs = osmo_tdef_get(sgsn->cfg.T_defs, 3350, OSMO_TDEF_S, -1);
432 osmo_timer_schedule(&fi->timer, t_secs, 0);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200433 break;
434 }
435
436 return 0;
437}
438
439struct osmo_fsm gmm_attach_req_fsm = {
440 .name = "GMM_ATTACH_REQ_FSM",
441 .states = gmm_attach_req_fsm_states,
442 .num_states = ARRAY_SIZE(gmm_attach_req_fsm_states),
443 .event_names = gmm_attach_req_fsm_event_names,
444 .allstate_event_mask = X(E_REJECT) | X(E_ATTACH_REQ_RECV),
445 .allstate_action = gmm_attach_allstate_action,
446 .log_subsys = DMM,
447 .timer_cb = gmm_attach_timer_cb,
448};
449
450static __attribute__((constructor)) void gprs_gmm_fsm_init(void)
451{
Harald Weltea868e172019-12-01 13:23:53 +0100452 OSMO_ASSERT(osmo_fsm_register(&gmm_attach_req_fsm) == 0);
Alexander Couzensf7198d72018-05-22 18:29:14 +0200453}
454
455void gmm_att_req_free(struct sgsn_mm_ctx *mm) {
456 if (mm->gmm_att_req.fsm)
457 osmo_fsm_inst_free(mm->gmm_att_req.fsm);
458
459 if (mm->gmm_att_req.attach_req)
460 msgb_free(mm->gmm_att_req.attach_req);
461}