blob: 6fdb3af401f52affecda701d761be83c86be9b19 [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;
198 }
199}
200
201static void st_reject(struct osmo_fsm_inst *fi, uint32_t event, void *data)
202{
203 struct sgsn_mm_ctx *ctx = fi->priv;
204 long reject_cause = (long) data;
205
206 if (reject_cause != GMM_DISCARD_MS_WITHOUT_REJECT)
207 gsm48_tx_gmm_att_rej(ctx, (uint8_t) reject_cause);
208
209 sgsn_mm_ctx_cleanup_free(ctx);
210}
211
212static void st_ask_vlr_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
213{
214 struct sgsn_mm_ctx *ctx = fi->priv;
215
216 /* FIXME: remove this layer violation.
217 * The VLR should send the message to the HLR and not the rx function
218 * gsm48_rx_gmm_auth_ciph_fail. Because gmm_auth_ciph_fail already send a
219 * message to the HLR, we don't send here a request. */
220 if (ctx->auth_state == SGSN_AUTH_UMTS_RESYNC)
221 return;
222
223 /* ask the auth layer for more data */
224 sgsn_auth_request(ctx);
225}
226
227static void st_ask_vlr(struct osmo_fsm_inst *fi, uint32_t event, void *data)
228{
229 switch(event) {
230 case E_VLR_ANSWERED:
231 osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360);
232 break;
233 }
234}
235
Alexander Couzens54557562018-09-27 21:15:30 +0200236static void st_iu_security_cmd_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
237{
238#ifdef BUILD_IU
239 struct sgsn_mm_ctx *ctx = fi->priv;
Alexander Couzens54557562018-09-27 21:15:30 +0200240
241 /* TODO: shouldn't this set always? not only when the integrity_active? */
242 if (ctx->iu.ue_ctx->integrity_active) {
243 osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);
244 return;
245 }
246
247 ranap_iu_tx_sec_mode_cmd(ctx->iu.ue_ctx, &ctx->auth_triplet.vec, 0, ctx->iu.new_key);
248 ctx->iu.new_key = 0;
249#endif
250}
251
252static void st_iu_security_cmd(struct osmo_fsm_inst *fi, uint32_t event, void *data)
253{
254 switch(event) {
255 case E_IU_SECURITY_CMD_COMPLETE:
256 osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);
257 break;
258 }
259}
260
Alexander Couzensf7198d72018-05-22 18:29:14 +0200261static struct osmo_fsm_state gmm_attach_req_fsm_states[] = {
262 /* default state for non-DTX and DTX when SPEECH is in progress */
263 [ST_INIT] = {
264 .in_event_mask = X(E_ATTACH_REQ_RECV),
265 .out_state_mask = X(ST_INIT) | X(ST_IDENTIY) | X(ST_AUTH) | X(ST_ACCEPT),
266 .name = "Init",
267 .action = st_init,
268 },
269 [ST_ASK_VLR] = {
270 .in_event_mask = X(E_VLR_ANSWERED),
271 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT),
272 .name = "AskVLR",
273 .onenter = st_ask_vlr_on_enter,
274 .action = st_ask_vlr,
275 },
276 [ST_IDENTIY] = {
277 .in_event_mask = X(E_IDEN_RESP_RECV),
278 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_IDENTIY) | X(ST_REJECT),
279 .onenter = st_identity_on_enter,
280 .name = "CheckIdentity",
281 .action = st_identity,
282 },
283 [ST_AUTH] = {
284 .in_event_mask = X(E_AUTH_RESP_RECV_SUCCESS) | X(E_AUTH_RESP_RECV_RESYNC),
Alexander Couzens54557562018-09-27 21:15:30 +0200285 .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 +0200286 .name = "Authenticate",
287 .onenter = st_auth_on_enter,
288 .action = st_auth,
289 },
Alexander Couzens54557562018-09-27 21:15:30 +0200290 [ST_IU_SECURITY_CMD] = {
291 .in_event_mask = X(E_IU_SECURITY_CMD_COMPLETE),
292 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT),
293 .name = "IuSecurityCommand",
294 .onenter = st_iu_security_cmd_on_enter,
295 .action = st_iu_security_cmd,
296 },
Alexander Couzensf7198d72018-05-22 18:29:14 +0200297 [ST_ACCEPT] = {
298 .in_event_mask = X(E_ATTACH_COMPLETE_RECV),
299 .out_state_mask = X(ST_INIT) | X(ST_REJECT),
300 .name = "WaitAttachComplete",
301 .onenter = st_accept_on_enter,
302 .action = st_accept,
303 },
304 [ST_REJECT] = {
305 .in_event_mask = X(E_REJECT),
306 .out_state_mask = X(ST_INIT),
307 .name = "Reject",
308 .action = st_reject,
309 },
310};
311
312const struct value_string gmm_attach_req_fsm_event_names[] = {
313 { E_ATTACH_REQ_RECV, "Received an attach request" },
314 { E_IDEN_RESP_RECV, "Identity Request received" },
315 { E_AUTH_RESP_RECV_SUCCESS, "Authentication Response received" },
316 { E_AUTH_RESP_RECV_RESYNC, "Authentication Failure with resync received" },
317 { E_ATTACH_ACCEPTED, "Attach accepted" },
318 { E_ATTACH_ACCEPT_SENT, "Attach accept sent" },
319 { E_ATTACH_COMPLETE_RECV, "Attach complete received." },
Alexander Couzens54557562018-09-27 21:15:30 +0200320 { E_IU_SECURITY_CMD_COMPLETE, "IU Security Command Complete received." },
Alexander Couzensf7198d72018-05-22 18:29:14 +0200321 { E_REJECT, "Reject the MS"},
322 { E_VLR_ANSWERED, "VLR answered"},
323 { 0, NULL }
324};
325
326void gmm_attach_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) {
327 struct sgsn_mm_ctx *ctx = fi->priv;
328 struct msgb *new_attach_req = data;
329
330 switch (event) {
331 case E_ATTACH_REQ_RECV:
332 switch (fi->state) {
333 case ST_INIT:
334 case ST_REJECT:
335 st_init(fi, event, data);
336 break;
337
338 case ST_ACCEPT:
339 /* TODO: drop all state (e.g. PDP Ctx) and do this procedure */
340 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
341 st_init(fi, event, data);
342 break;
343
344 case ST_ASK_VLR:
345 case ST_AUTH:
346 case ST_IDENTIY:
347 case ST_RETRIEVE_AUTH:
348 /* 04.08 4.7.3.1.6 d) Abnormal Case
349 * Only do action if Req IEs differs. */
350 if (ctx->gmm_att_req.attach_req &&
351 gprs_gmm_attach_req_ies(new_attach_req, ctx->gmm_att_req.attach_req)) {
352 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
353 st_init(fi, event, data);
354 }
355 break;
356 }
357 break;
358 case E_REJECT:
359 if (fi->state != ST_REJECT)
360 osmo_fsm_inst_state_chg(fi, ST_REJECT, 0, 0);
361 st_reject(fi, event, data);
362 break;
363 }
364}
365
366int gmm_attach_timer_cb(struct osmo_fsm_inst *fi)
367{
368 struct sgsn_mm_ctx *ctx = fi->priv;
369 struct gsm_auth_tuple *at = &ctx->auth_triplet;
370
371 ctx->num_T_exp++;
372
373 switch(fi->state) {
374 case ST_ASK_VLR:
375 /* TODO: replace T3350 by a better timer or it's own
376 * re-use T3350 - not defined by standard */
377 LOGMMCTXP(LOGL_ERROR, ctx, "HLR did not answer in time. Rejecting.\n");
378 osmo_fsm_inst_dispatch(fi, E_REJECT,
379 (void *) GMM_CAUSE_NET_FAIL);
380 break;
381 case ST_IDENTIY:
382 /* T3370 */
383 if (ctx->num_T_exp >= 5) {
384 osmo_fsm_inst_dispatch(fi, E_REJECT,
385 (void *) GMM_CAUSE_MS_ID_NOT_DERIVED);
386 break;
387 }
388 gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type);
389 osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3370, 0);
390 break;
391 case ST_AUTH:
392 /* T3360 */
393 if (ctx->num_T_exp >= 5) {
394 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
395 break;
396 }
397 gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, false);
398 osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3360, 0);
399 break;
400 case ST_ACCEPT:
401 /* T3350 */
402 if (ctx->num_T_exp >= 5) {
403 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
404 break;
405 }
406 gsm48_tx_gmm_att_ack(ctx);
407 osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3350, 0);
408 break;
409 }
410
411 return 0;
412}
413
414struct osmo_fsm gmm_attach_req_fsm = {
415 .name = "GMM_ATTACH_REQ_FSM",
416 .states = gmm_attach_req_fsm_states,
417 .num_states = ARRAY_SIZE(gmm_attach_req_fsm_states),
418 .event_names = gmm_attach_req_fsm_event_names,
419 .allstate_event_mask = X(E_REJECT) | X(E_ATTACH_REQ_RECV),
420 .allstate_action = gmm_attach_allstate_action,
421 .log_subsys = DMM,
422 .timer_cb = gmm_attach_timer_cb,
423};
424
425static __attribute__((constructor)) void gprs_gmm_fsm_init(void)
426{
427 osmo_fsm_register(&gmm_attach_req_fsm);
428}
429
430void gmm_att_req_free(struct sgsn_mm_ctx *mm) {
431 if (mm->gmm_att_req.fsm)
432 osmo_fsm_inst_free(mm->gmm_att_req.fsm);
433
434 if (mm->gmm_att_req.attach_req)
435 msgb_free(mm->gmm_att_req.attach_req);
436}