blob: 272fec797b1f96b432d9199e65d8f427f6d2c69c [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);
160 osmo_fsm_inst_state_chg(fi, ST_ACCEPT, sgsn->cfg.timers.T3350, 3350);
161 break;
162 case E_AUTH_RESP_RECV_RESYNC:
163 if (ctx->gmm_att_req.auth_reattempt <= 1)
164 osmo_fsm_inst_state_chg(fi, ST_ASK_VLR, sgsn->cfg.timers.T3350, 3350);
165 else
166 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_CAUSE_SYNC_FAIL);
167 break;
168 }
169}
170
171static void st_accept_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
172{
173 struct sgsn_mm_ctx *ctx = fi->priv;
174
175 ctx->num_T_exp = 0;
176
177 /* TODO: remove pending_req as soon the sgsn_auth code doesn't depend on it */
178 ctx->pending_req = 0;
179 gsm48_tx_gmm_att_ack(ctx);
180}
181
182static void st_accept(struct osmo_fsm_inst *fi, uint32_t event, void *data)
183{
184 struct sgsn_mm_ctx *ctx = fi->priv;
185
186 switch(event) {
187 case E_ATTACH_COMPLETE_RECV:
188 /* TODO: #ifdef ! PTMSI_ALLOC is not supported */
189 extract_subscr_msisdn(ctx);
190 extract_subscr_hlr(ctx);
191 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
192 break;
193 }
194}
195
196static void st_reject(struct osmo_fsm_inst *fi, uint32_t event, void *data)
197{
198 struct sgsn_mm_ctx *ctx = fi->priv;
199 long reject_cause = (long) data;
200
201 if (reject_cause != GMM_DISCARD_MS_WITHOUT_REJECT)
202 gsm48_tx_gmm_att_rej(ctx, (uint8_t) reject_cause);
203
204 sgsn_mm_ctx_cleanup_free(ctx);
205}
206
207static void st_ask_vlr_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
208{
209 struct sgsn_mm_ctx *ctx = fi->priv;
210
211 /* FIXME: remove this layer violation.
212 * The VLR should send the message to the HLR and not the rx function
213 * gsm48_rx_gmm_auth_ciph_fail. Because gmm_auth_ciph_fail already send a
214 * message to the HLR, we don't send here a request. */
215 if (ctx->auth_state == SGSN_AUTH_UMTS_RESYNC)
216 return;
217
218 /* ask the auth layer for more data */
219 sgsn_auth_request(ctx);
220}
221
222static void st_ask_vlr(struct osmo_fsm_inst *fi, uint32_t event, void *data)
223{
224 switch(event) {
225 case E_VLR_ANSWERED:
226 osmo_fsm_inst_state_chg(fi, ST_AUTH, sgsn->cfg.timers.T3360, 3360);
227 break;
228 }
229}
230
231static struct osmo_fsm_state gmm_attach_req_fsm_states[] = {
232 /* default state for non-DTX and DTX when SPEECH is in progress */
233 [ST_INIT] = {
234 .in_event_mask = X(E_ATTACH_REQ_RECV),
235 .out_state_mask = X(ST_INIT) | X(ST_IDENTIY) | X(ST_AUTH) | X(ST_ACCEPT),
236 .name = "Init",
237 .action = st_init,
238 },
239 [ST_ASK_VLR] = {
240 .in_event_mask = X(E_VLR_ANSWERED),
241 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_REJECT),
242 .name = "AskVLR",
243 .onenter = st_ask_vlr_on_enter,
244 .action = st_ask_vlr,
245 },
246 [ST_IDENTIY] = {
247 .in_event_mask = X(E_IDEN_RESP_RECV),
248 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_IDENTIY) | X(ST_REJECT),
249 .onenter = st_identity_on_enter,
250 .name = "CheckIdentity",
251 .action = st_identity,
252 },
253 [ST_AUTH] = {
254 .in_event_mask = X(E_AUTH_RESP_RECV_SUCCESS) | X(E_AUTH_RESP_RECV_RESYNC),
255 .out_state_mask = X(ST_INIT) | X(ST_AUTH) | X(ST_ACCEPT) | X(ST_ASK_VLR) | X(ST_REJECT),
256 .name = "Authenticate",
257 .onenter = st_auth_on_enter,
258 .action = st_auth,
259 },
260 [ST_ACCEPT] = {
261 .in_event_mask = X(E_ATTACH_COMPLETE_RECV),
262 .out_state_mask = X(ST_INIT) | X(ST_REJECT),
263 .name = "WaitAttachComplete",
264 .onenter = st_accept_on_enter,
265 .action = st_accept,
266 },
267 [ST_REJECT] = {
268 .in_event_mask = X(E_REJECT),
269 .out_state_mask = X(ST_INIT),
270 .name = "Reject",
271 .action = st_reject,
272 },
273};
274
275const struct value_string gmm_attach_req_fsm_event_names[] = {
276 { E_ATTACH_REQ_RECV, "Received an attach request" },
277 { E_IDEN_RESP_RECV, "Identity Request received" },
278 { E_AUTH_RESP_RECV_SUCCESS, "Authentication Response received" },
279 { E_AUTH_RESP_RECV_RESYNC, "Authentication Failure with resync received" },
280 { E_ATTACH_ACCEPTED, "Attach accepted" },
281 { E_ATTACH_ACCEPT_SENT, "Attach accept sent" },
282 { E_ATTACH_COMPLETE_RECV, "Attach complete received." },
283 { E_REJECT, "Reject the MS"},
284 { E_VLR_ANSWERED, "VLR answered"},
285 { 0, NULL }
286};
287
288void gmm_attach_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) {
289 struct sgsn_mm_ctx *ctx = fi->priv;
290 struct msgb *new_attach_req = data;
291
292 switch (event) {
293 case E_ATTACH_REQ_RECV:
294 switch (fi->state) {
295 case ST_INIT:
296 case ST_REJECT:
297 st_init(fi, event, data);
298 break;
299
300 case ST_ACCEPT:
301 /* TODO: drop all state (e.g. PDP Ctx) and do this procedure */
302 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
303 st_init(fi, event, data);
304 break;
305
306 case ST_ASK_VLR:
307 case ST_AUTH:
308 case ST_IDENTIY:
309 case ST_RETRIEVE_AUTH:
310 /* 04.08 4.7.3.1.6 d) Abnormal Case
311 * Only do action if Req IEs differs. */
312 if (ctx->gmm_att_req.attach_req &&
313 gprs_gmm_attach_req_ies(new_attach_req, ctx->gmm_att_req.attach_req)) {
314 osmo_fsm_inst_state_chg(fi, ST_INIT, 0, 0);
315 st_init(fi, event, data);
316 }
317 break;
318 }
319 break;
320 case E_REJECT:
321 if (fi->state != ST_REJECT)
322 osmo_fsm_inst_state_chg(fi, ST_REJECT, 0, 0);
323 st_reject(fi, event, data);
324 break;
325 }
326}
327
328int gmm_attach_timer_cb(struct osmo_fsm_inst *fi)
329{
330 struct sgsn_mm_ctx *ctx = fi->priv;
331 struct gsm_auth_tuple *at = &ctx->auth_triplet;
332
333 ctx->num_T_exp++;
334
335 switch(fi->state) {
336 case ST_ASK_VLR:
337 /* TODO: replace T3350 by a better timer or it's own
338 * re-use T3350 - not defined by standard */
339 LOGMMCTXP(LOGL_ERROR, ctx, "HLR did not answer in time. Rejecting.\n");
340 osmo_fsm_inst_dispatch(fi, E_REJECT,
341 (void *) GMM_CAUSE_NET_FAIL);
342 break;
343 case ST_IDENTIY:
344 /* T3370 */
345 if (ctx->num_T_exp >= 5) {
346 osmo_fsm_inst_dispatch(fi, E_REJECT,
347 (void *) GMM_CAUSE_MS_ID_NOT_DERIVED);
348 break;
349 }
350 gsm48_tx_gmm_id_req(ctx, ctx->gmm_att_req.id_type);
351 osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3370, 0);
352 break;
353 case ST_AUTH:
354 /* T3360 */
355 if (ctx->num_T_exp >= 5) {
356 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
357 break;
358 }
359 gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, false);
360 osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3360, 0);
361 break;
362 case ST_ACCEPT:
363 /* T3350 */
364 if (ctx->num_T_exp >= 5) {
365 osmo_fsm_inst_dispatch(fi, E_REJECT, (void *) GMM_DISCARD_MS_WITHOUT_REJECT);
366 break;
367 }
368 gsm48_tx_gmm_att_ack(ctx);
369 osmo_timer_schedule(&fi->timer, sgsn->cfg.timers.T3350, 0);
370 break;
371 }
372
373 return 0;
374}
375
376struct osmo_fsm gmm_attach_req_fsm = {
377 .name = "GMM_ATTACH_REQ_FSM",
378 .states = gmm_attach_req_fsm_states,
379 .num_states = ARRAY_SIZE(gmm_attach_req_fsm_states),
380 .event_names = gmm_attach_req_fsm_event_names,
381 .allstate_event_mask = X(E_REJECT) | X(E_ATTACH_REQ_RECV),
382 .allstate_action = gmm_attach_allstate_action,
383 .log_subsys = DMM,
384 .timer_cb = gmm_attach_timer_cb,
385};
386
387static __attribute__((constructor)) void gprs_gmm_fsm_init(void)
388{
389 osmo_fsm_register(&gmm_attach_req_fsm);
390}
391
392void gmm_att_req_free(struct sgsn_mm_ctx *mm) {
393 if (mm->gmm_att_req.fsm)
394 osmo_fsm_inst_free(mm->gmm_att_req.fsm);
395
396 if (mm->gmm_att_req.attach_req)
397 msgb_free(mm->gmm_att_req.attach_req);
398}