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