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