blob: d5a8555b7e33cabf7f0c7a0466eeaae20f84ad48 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR) Autentication FSM */
2
3/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22
23#include <osmocom/core/fsm.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/gsm/gsup.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020026#include <osmocom/msc/vlr.h>
27#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020028
29#include "vlr_core.h"
30#include "vlr_auth_fsm.h"
31
32#define S(x) (1 << (x))
33
34static const struct value_string fsm_auth_event_names[] = {
35 OSMO_VALUE_STRING(VLR_AUTH_E_START),
36 OSMO_VALUE_STRING(VLR_AUTH_E_HLR_SAI_ACK),
37 OSMO_VALUE_STRING(VLR_AUTH_E_HLR_SAI_NACK),
38 OSMO_VALUE_STRING(VLR_AUTH_E_HLR_SAI_ABORT),
39 OSMO_VALUE_STRING(VLR_AUTH_E_MS_AUTH_RESP),
40 OSMO_VALUE_STRING(VLR_AUTH_E_MS_AUTH_FAIL),
41 OSMO_VALUE_STRING(VLR_AUTH_E_MS_ID_IMSI),
42 { 0, NULL }
43};
44
Harald Welteb8b85a12016-06-17 00:06:42 +020045/* private state of the auth_fsm_instance */
46struct auth_fsm_priv {
47 struct vlr_subscr *vsub;
48 bool by_imsi;
49 bool is_r99;
50 bool is_utran;
51 bool auth_requested;
52
Neels Hofmeyr33f53412017-10-29 02:11:18 +010053 int auth_tuple_max_reuse_count; /* see vlr->cfg instead */
Harald Welteb8b85a12016-06-17 00:06:42 +020054};
55
56/***********************************************************************
57 * Utility functions
58 ***********************************************************************/
59
60/* Always use either vlr_subscr_get_auth_tuple() or vlr_subscr_has_auth_tuple()
61 * instead, to ensure proper use count.
62 * Return an auth tuple with the lowest use_count among the auth tuples. If
Neels Hofmeyr33f53412017-10-29 02:11:18 +010063 * max_reuse_count >= 0, return NULL if all available auth tuples have a use
64 * count > max_reuse_count. If max_reuse_count is negative, return a currently
Harald Welteb8b85a12016-06-17 00:06:42 +020065 * least used auth tuple without enforcing a maximum use count. If there are
66 * no auth tuples, return NULL.
67 */
68static struct gsm_auth_tuple *
Neels Hofmeyr33f53412017-10-29 02:11:18 +010069_vlr_subscr_next_auth_tuple(struct vlr_subscr *vsub, int max_reuse_count)
Harald Welteb8b85a12016-06-17 00:06:42 +020070{
71 unsigned int count;
72 unsigned int idx;
73 struct gsm_auth_tuple *at = NULL;
74 unsigned int key_seq = GSM_KEY_SEQ_INVAL;
75
76 if (!vsub)
77 return NULL;
78
79 if (vsub->last_tuple)
80 key_seq = vsub->last_tuple->key_seq;
81
82 if (key_seq == GSM_KEY_SEQ_INVAL)
83 /* Start with 0 after increment modulo array size */
84 idx = ARRAY_SIZE(vsub->auth_tuples) - 1;
85 else
86 idx = key_seq;
87
88 for (count = ARRAY_SIZE(vsub->auth_tuples); count > 0; count--) {
89 idx = (idx + 1) % ARRAY_SIZE(vsub->auth_tuples);
90
91 if (vsub->auth_tuples[idx].key_seq == GSM_KEY_SEQ_INVAL)
92 continue;
93
94 if (!at || vsub->auth_tuples[idx].use_count < at->use_count)
95 at = &vsub->auth_tuples[idx];
96 }
97
Neels Hofmeyr33f53412017-10-29 02:11:18 +010098 if (!at || (max_reuse_count >= 0 && at->use_count > max_reuse_count))
Harald Welteb8b85a12016-06-17 00:06:42 +020099 return NULL;
100
101 return at;
102}
103
104/* Return an auth tuple and increment its use count. */
105static struct gsm_auth_tuple *
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100106vlr_subscr_get_auth_tuple(struct vlr_subscr *vsub, int max_reuse_count)
Harald Welteb8b85a12016-06-17 00:06:42 +0200107{
108 struct gsm_auth_tuple *at = _vlr_subscr_next_auth_tuple(vsub,
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100109 max_reuse_count);
Harald Welteb8b85a12016-06-17 00:06:42 +0200110 if (!at)
111 return NULL;
112 at->use_count++;
113 return at;
114}
115
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100116/* Return whether an auth tuple with a matching use_count is available. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200117static bool vlr_subscr_has_auth_tuple(struct vlr_subscr *vsub,
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100118 int max_reuse_count)
Harald Welteb8b85a12016-06-17 00:06:42 +0200119{
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100120 return _vlr_subscr_next_auth_tuple(vsub, max_reuse_count) != NULL;
Harald Welteb8b85a12016-06-17 00:06:42 +0200121}
122
123static bool check_auth_resp(struct vlr_subscr *vsub, bool is_r99,
124 bool is_utran, const uint8_t *res,
125 uint8_t res_len)
126{
127 struct gsm_auth_tuple *at = vsub->last_tuple;
128 struct osmo_auth_vector *vec = &at->vec;
129 bool check_umts;
Neels Hofmeyr11d2ce32018-03-10 00:25:20 +0100130 bool res_is_umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200131 OSMO_ASSERT(at);
132
Neels Hofmeyra9099bc2018-03-10 04:22:50 +0100133 LOGVSUBP(LOGL_DEBUG, vsub, "AUTH on %s received %s: %s (%u bytes)\n",
134 is_utran ? "UTRAN" : "GERAN",
135 is_utran ? "RES" : "SRES/RES",
136 osmo_hexdump_nospc(res, res_len), res_len);
Harald Welteb8b85a12016-06-17 00:06:42 +0200137
138 /* RES must be present and at least 32bit */
Neels Hofmeyr11d2ce32018-03-10 00:25:20 +0100139 if (!res || !res_len) {
140 LOGVSUBP(LOGL_NOTICE, vsub, "AUTH SRES/RES missing\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200141 goto out_false;
142 }
143
Neels Hofmeyr11d2ce32018-03-10 00:25:20 +0100144 /* We're deciding the UMTS AKA-ness of the response by the RES size. So let's make sure we can't
145 * mix them up by size. On UTRAN, we expect full length RES always, no way to mix up there. */
146 if (!is_utran && vec->res_len == sizeof(vec->sres))
147 LOGVSUBP(LOGL_ERROR, vsub, "Unforeseen situation: UMTS AKA's RES length"
148 " equals the size of SRES: %u -- this code wants to differentiate"
149 " the two by their size, which won't work properly now.\n", vec->res_len);
150
151 /* RES must be either vec->res_len (UMTS AKA) or sizeof(sres) (GSM AKA) */
152 if (res_len == vec->res_len)
153 res_is_umts_aka = true;
154 else if (res_len == sizeof(vec->sres))
155 res_is_umts_aka = false;
156 else {
157 if (is_utran)
158 LOGVSUBP(LOGL_NOTICE, vsub, "AUTH RES has invalid length: %u."
159 " Expected %u (UMTS AKA)\n",
160 res_len, vec->res_len);
161 else
162 LOGVSUBP(LOGL_NOTICE, vsub, "AUTH SRES/RES has invalid length: %u."
163 " Expected either %zu (GSM AKA) or %u (UMTS AKA)\n",
164 res_len, sizeof(vec->sres), vec->res_len);
165 goto out_false;
166 }
167
168 check_umts = (is_r99
169 && (vec->auth_types & OSMO_AUTH_TYPE_UMTS)
170 && res_is_umts_aka);
171
172 /* Even on an R99 capable MS with a UMTS AKA capable USIM,
173 * the MS may still choose to only perform GSM AKA, as
174 * long as the bearer is GERAN -- never on UTRAN: */
175 if (is_utran && !check_umts) {
176 LOGVSUBP(LOGL_ERROR, vsub,
177 "AUTH via UTRAN, cannot allow GSM AKA"
178 " (MS is %sR99 capable, vec has %sUMTS AKA tokens, res_len=%u is %s)\n",
179 is_r99 ? "" : "NOT ",
180 (vec->auth_types & OSMO_AUTH_TYPE_UMTS) ? "" : "NO ",
181 res_len, (res_len == vec->res_len)? "valid" : "INVALID on UTRAN");
182 goto out_false;
Harald Welteb8b85a12016-06-17 00:06:42 +0200183 }
184
185 if (check_umts) {
186 if (res_len != vec->res_len
187 || memcmp(res, vec->res, res_len)) {
188 LOGVSUBP(LOGL_INFO, vsub, "UMTS AUTH failure:"
189 " mismatching res (expected res=%s)\n",
190 osmo_hexdump(vec->res, vec->res_len));
191 goto out_false;
192 }
193
194 LOGVSUBP(LOGL_INFO, vsub, "AUTH established UMTS security"
195 " context\n");
196 vsub->sec_ctx = VLR_SEC_CTX_UMTS;
197 return true;
198 } else {
199 if (res_len != sizeof(vec->sres)
200 || memcmp(res, vec->sres, sizeof(vec->sres))) {
201 LOGVSUBP(LOGL_INFO, vsub, "GSM AUTH failure:"
202 " mismatching sres (expected sres=%s)\n",
203 osmo_hexdump(vec->sres, sizeof(vec->sres)));
204 goto out_false;
205 }
206
207 LOGVSUBP(LOGL_INFO, vsub, "AUTH established GSM security"
208 " context\n");
209 vsub->sec_ctx = VLR_SEC_CTX_GSM;
210 return true;
211 }
212
213out_false:
214 vsub->sec_ctx = VLR_SEC_CTX_NONE;
215 return false;
216}
217
218static void auth_fsm_onenter_failed(struct osmo_fsm_inst *fi, uint32_t prev_state)
219{
220 struct auth_fsm_priv *afp = fi->priv;
221 struct vlr_subscr *vsub = afp->vsub;
222
223 /* If authentication hasn't even started, e.g. the HLR sent no auth
224 * info, then we also don't need to tell the HLR about an auth failure.
225 */
Max770fbd22018-01-24 12:48:33 +0100226 if (afp->auth_requested) {
227 int rc = vlr_subscr_tx_auth_fail_rep(vsub);
228 if (rc < 0)
229 LOGVSUBP(LOGL_ERROR, vsub, "Failed to communicate AUTH failure to HLR\n");
230 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200231}
232
Neels Hofmeyr15809592018-04-06 02:57:51 +0200233static const char *vlr_auth_fsm_result_name(enum gsm48_reject_value result)
234{
235 if (!result)
236 return "PASSED";
237 return get_value_string(gsm48_gmm_cause_names, result);
238}
239
Harald Welteb8b85a12016-06-17 00:06:42 +0200240/* Terminate the Auth FSM Instance and notify parent */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200241static void auth_fsm_term(struct osmo_fsm_inst *fi, enum gsm48_reject_value result)
Harald Welteb8b85a12016-06-17 00:06:42 +0200242{
243 struct auth_fsm_priv *afp = fi->priv;
244 struct vlr_subscr *vsub = afp->vsub;
245
246 LOGPFSM(fi, "Authentication terminating with result %s\n",
Neels Hofmeyr15809592018-04-06 02:57:51 +0200247 vlr_auth_fsm_result_name(result));
Harald Welteb8b85a12016-06-17 00:06:42 +0200248
249 /* Do one final state transition (mostly for logging purpose) */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200250 if (!result)
Harald Welteb8b85a12016-06-17 00:06:42 +0200251 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTHENTICATED, 0, 0);
252 else
253 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTH_FAILED, 0, 0);
254
255 /* return the result to the parent FSM */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200256 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, &result);
Harald Welteb8b85a12016-06-17 00:06:42 +0200257 vsub->auth_fsm = NULL;
258}
259
260/* back-end function transmitting authentication. Caller ensures we have valid
261 * tuple */
262static int _vlr_subscr_authenticate(struct osmo_fsm_inst *fi)
263{
264 struct auth_fsm_priv *afp = fi->priv;
265 struct vlr_subscr *vsub = afp->vsub;
266 struct gsm_auth_tuple *at;
Neels Hofmeyrfe718bc2018-03-11 01:24:33 +0100267 bool use_umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200268
269 /* Caller ensures we have vectors available */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100270 at = vlr_subscr_get_auth_tuple(vsub, afp->auth_tuple_max_reuse_count);
Harald Welteb8b85a12016-06-17 00:06:42 +0200271 if (!at) {
272 LOGPFSML(fi, LOGL_ERROR, "A previous check ensured that an"
273 " auth tuple was available, but now there is in fact"
274 " none.\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200275 auth_fsm_term(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200276 return -1;
277 }
278
Neels Hofmeyrfe718bc2018-03-11 01:24:33 +0100279 use_umts_aka = vlr_use_umts_aka(&at->vec, afp->is_r99);
280 LOGPFSM(fi, "got auth tuple: use_count=%d key_seq=%d"
281 " -- will use %s AKA (is_r99=%s, at->vec.auth_types=0x%x)\n",
282 at->use_count, at->key_seq,
283 use_umts_aka ? "UMTS" : "GSM", afp->is_r99 ? "yes" : "no", at->vec.auth_types);
Harald Welteb8b85a12016-06-17 00:06:42 +0200284
285 /* Transmit auth req to subscriber */
286 afp->auth_requested = true;
287 vsub->last_tuple = at;
Neels Hofmeyrfe718bc2018-03-11 01:24:33 +0100288 vsub->vlr->ops.tx_auth_req(vsub->msc_conn_ref, at, use_umts_aka);
Harald Welteb8b85a12016-06-17 00:06:42 +0200289 return 0;
290}
291
292/***********************************************************************
293 * FSM State Action functions
294 ***********************************************************************/
295
296/* Initial State of TS 23.018 AUT_VLR */
297static void auth_fsm_needs_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data)
298{
299 struct auth_fsm_priv *afp = fi->priv;
300 struct vlr_subscr *vsub = afp->vsub;
301
302 OSMO_ASSERT(event == VLR_AUTH_E_START);
303
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100304 /* Start off with the default max_reuse_count, possibly change that if we
Harald Welteb8b85a12016-06-17 00:06:42 +0200305 * need to re-use an old tuple. */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100306 afp->auth_tuple_max_reuse_count = vsub->vlr->cfg.auth_tuple_max_reuse_count;
Harald Welteb8b85a12016-06-17 00:06:42 +0200307
308 /* Check if we have vectors available */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100309 if (!vlr_subscr_has_auth_tuple(vsub, afp->auth_tuple_max_reuse_count)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200310 /* Obtain_Authentication_Sets_VLR */
Max770fbd22018-01-24 12:48:33 +0100311 int rc = vlr_subscr_req_sai(vsub, NULL, NULL);
312 if (rc < 0)
313 LOGPFSM(fi, "Failed to request Authentication Sets from VLR\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200314 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH_WAIT_AI,
315 GSM_29002_TIMER_M, 0);
316 } else {
317 /* go straight ahead with sending auth request */
318 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP,
319 vlr_timer(vsub->vlr, 3260), 3260);
320 _vlr_subscr_authenticate(fi);
321 }
322}
323
324/* Waiting for Authentication Info from HLR */
325static void auth_fsm_wait_ai(struct osmo_fsm_inst *fi, uint32_t event,
326 void *data)
327{
328 struct auth_fsm_priv *afp = fi->priv;
329 struct vlr_subscr *vsub = afp->vsub;
330 struct osmo_gsup_message *gsup = data;
331
332 if (event == VLR_AUTH_E_HLR_SAI_NACK)
333 LOGPFSM(fi, "GSUP: rx Auth Info Error cause: %d: %s\n",
334 gsup->cause,
335 get_value_string(gsm48_gmm_cause_names, gsup->cause));
336
337 /* We are in what corresponds to the
338 * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */
339 if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors)
340 || (event == VLR_AUTH_E_HLR_SAI_NACK &&
341 gsup->cause != GMM_CAUSE_IMSI_UNKNOWN)
342 || (event == VLR_AUTH_E_HLR_SAI_ABORT)) {
343 if (vsub->vlr->cfg.auth_reuse_old_sets_on_error
344 && vlr_subscr_has_auth_tuple(vsub, -1)) {
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100345 /* To re-use an old tuple, disable the max_reuse_count
Harald Welteb8b85a12016-06-17 00:06:42 +0200346 * constraint. */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100347 afp->auth_tuple_max_reuse_count = -1;
Harald Welteb8b85a12016-06-17 00:06:42 +0200348 goto pass;
349 }
350 /* result = procedure error */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200351 auth_fsm_term(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200352 return;
353 }
354
355 switch (event) {
356 case VLR_AUTH_E_HLR_SAI_ACK:
357 vlr_subscr_update_tuples(vsub, gsup);
358 goto pass;
359 break;
360 case VLR_AUTH_E_HLR_SAI_NACK:
361 auth_fsm_term(fi,
362 gsup->cause == GMM_CAUSE_IMSI_UNKNOWN?
Neels Hofmeyr15809592018-04-06 02:57:51 +0200363 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR
364 : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200365 break;
366 }
367
368 return;
369pass:
370 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP,
371 vlr_timer(vsub->vlr, 3260), 3260);
372 _vlr_subscr_authenticate(fi);
373}
374
375/* Waiting for Authentication Response from MS */
376static void auth_fsm_wait_auth_resp(struct osmo_fsm_inst *fi, uint32_t event,
377 void *data)
378{
379 struct auth_fsm_priv *afp = fi->priv;
380 struct vlr_subscr *vsub = afp->vsub;
381 struct vlr_instance *vlr = vsub->vlr;
382 struct vlr_auth_resp_par *par = data;
383 int rc;
384
385 switch (event) {
386 case VLR_AUTH_E_MS_AUTH_RESP:
387 rc = check_auth_resp(vsub, par->is_r99, par->is_utran,
388 par->res, par->res_len);
389 if (rc == false) {
390 if (!afp->by_imsi) {
391 vlr->ops.tx_id_req(vsub->msc_conn_ref,
392 GSM_MI_TYPE_IMSI);
393 osmo_fsm_inst_state_chg(fi,
394 VLR_SUB_AS_WAIT_ID_IMSI,
395 vlr_timer(vlr, 3270), 3270);
396 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200397 auth_fsm_term(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200398 }
399 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200400 auth_fsm_term(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200401 }
402 break;
403 case VLR_AUTH_E_MS_AUTH_FAIL:
404 if (par->auts) {
405 /* First failure, start re-sync attempt */
Max770fbd22018-01-24 12:48:33 +0100406 rc = vlr_subscr_req_sai(vsub, par->auts,
Harald Welteb8b85a12016-06-17 00:06:42 +0200407 vsub->last_tuple->vec.rand);
408 osmo_fsm_inst_state_chg(fi,
409 VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC,
410 GSM_29002_TIMER_M, 0);
411 } else
Neels Hofmeyr15809592018-04-06 02:57:51 +0200412 auth_fsm_term(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200413 break;
414 }
415}
416
417/* Waiting for Authentication Info from HLR (resync case) */
418static void auth_fsm_wait_ai_resync(struct osmo_fsm_inst *fi,
419 uint32_t event, void *data)
420{
421 struct auth_fsm_priv *afp = fi->priv;
422 struct vlr_subscr *vsub = afp->vsub;
423 struct osmo_gsup_message *gsup = data;
424
425 /* We are in what corresponds to the
426 * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */
427 if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) ||
428 (event == VLR_AUTH_E_HLR_SAI_NACK &&
429 gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) ||
430 (event == VLR_AUTH_E_HLR_SAI_ABORT)) {
431 /* result = procedure error */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200432 auth_fsm_term(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200433 }
434 switch (event) {
435 case VLR_AUTH_E_HLR_SAI_ACK:
436 vlr_subscr_update_tuples(vsub, gsup);
437 goto pass;
438 break;
439 case VLR_AUTH_E_HLR_SAI_NACK:
440 auth_fsm_term(fi,
441 gsup->cause == GMM_CAUSE_IMSI_UNKNOWN?
Neels Hofmeyr15809592018-04-06 02:57:51 +0200442 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR
443 : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200444 break;
445 }
446
447 return;
448pass:
449 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP_RESYNC,
450 vlr_timer(vsub->vlr, 3260), 3260);
451 _vlr_subscr_authenticate(fi);
452}
453
454/* Waiting for AUTH RESP from MS (re-sync case) */
455static void auth_fsm_wait_auth_resp_resync(struct osmo_fsm_inst *fi,
456 uint32_t event, void *data)
457{
458 struct auth_fsm_priv *afp = fi->priv;
459 struct vlr_subscr *vsub = afp->vsub;
460 struct vlr_auth_resp_par *par = data;
461 struct vlr_instance *vlr = vsub->vlr;
462 int rc;
463
464 switch (event) {
465 case VLR_AUTH_E_MS_AUTH_RESP:
466 rc = check_auth_resp(vsub, par->is_r99, par->is_utran,
467 par->res, par->res_len);
468 if (rc == false) {
469 if (!afp->by_imsi) {
470 vlr->ops.tx_id_req(vsub->msc_conn_ref,
471 GSM_MI_TYPE_IMSI);
472 osmo_fsm_inst_state_chg(fi,
473 VLR_SUB_AS_WAIT_ID_IMSI,
474 vlr_timer(vlr, 3270), 3270);
475 } else {
476 /* Result = Aborted */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200477 auth_fsm_term(fi, GSM48_REJECT_SYNCH_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200478 }
479 } else {
480 /* Result = Pass */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200481 auth_fsm_term(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200482 }
483 break;
484 case VLR_AUTH_E_MS_AUTH_FAIL:
485 /* Second failure: Result = Fail */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200486 auth_fsm_term(fi, GSM48_REJECT_SYNCH_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200487 break;
488 }
489}
490
491/* AUT_VLR waiting for Obtain_IMSI_VLR result */
492static void auth_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event,
493 void *data)
494{
495 struct auth_fsm_priv *afp = fi->priv;
496 struct vlr_subscr *vsub = afp->vsub;
497 const char *mi_string = data;
498
499 switch (event) {
500 case VLR_AUTH_E_MS_ID_IMSI:
501 if (vsub->imsi[0]
502 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
503 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
504 " %s\n", mi_string);
505 } else {
506 strncpy(vsub->imsi, mi_string, sizeof(vsub->imsi));
507 vsub->imsi[sizeof(vsub->imsi)-1] = '\0';
508 }
509 /* retry with identity=IMSI */
510 afp->by_imsi = true;
511 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH, 0, 0);
512 osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL);
513 break;
514 }
515}
516
517static const struct osmo_fsm_state auth_fsm_states[] = {
518 [VLR_SUB_AS_NEEDS_AUTH] = {
519 .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH),
520 .in_event_mask = S(VLR_AUTH_E_START),
521 .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI) |
522 S(VLR_SUB_AS_WAIT_RESP),
523 .action = auth_fsm_needs_auth,
524 },
525 [VLR_SUB_AS_NEEDS_AUTH_WAIT_AI] = {
526 .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI),
527 .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) |
528 S(VLR_AUTH_E_HLR_SAI_NACK),
529 .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) |
530 S(VLR_SUB_AS_WAIT_RESP),
531 .action = auth_fsm_wait_ai,
532 },
533 [VLR_SUB_AS_WAIT_RESP] = {
534 .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP),
535 .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) |
536 S(VLR_AUTH_E_MS_AUTH_FAIL),
537 .out_state_mask = S(VLR_SUB_AS_WAIT_ID_IMSI) |
538 S(VLR_SUB_AS_AUTH_FAILED) |
539 S(VLR_SUB_AS_AUTHENTICATED) |
540 S(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC),
541 .action = auth_fsm_wait_auth_resp,
542 },
543 [VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC] = {
544 .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC),
545 .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) |
546 S(VLR_AUTH_E_HLR_SAI_NACK),
547 .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) |
548 S(VLR_SUB_AS_WAIT_RESP_RESYNC),
549 .action = auth_fsm_wait_ai_resync,
550 },
551 [VLR_SUB_AS_WAIT_RESP_RESYNC] = {
552 .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP_RESYNC),
553 .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) |
554 S(VLR_AUTH_E_MS_AUTH_FAIL),
555 .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) |
556 S(VLR_SUB_AS_AUTHENTICATED),
557 .action = auth_fsm_wait_auth_resp_resync,
558 },
559 [VLR_SUB_AS_WAIT_ID_IMSI] = {
560 .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_ID_IMSI),
561 .in_event_mask = S(VLR_AUTH_E_MS_ID_IMSI),
562 .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH),
563 .action = auth_fsm_wait_imsi,
564 },
565 [VLR_SUB_AS_AUTHENTICATED] = {
566 .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTHENTICATED),
567 .in_event_mask = 0,
568 .out_state_mask = 0,
569 },
570 [VLR_SUB_AS_AUTH_FAILED] = {
571 .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTH_FAILED),
572 .in_event_mask = 0,
573 .out_state_mask = 0,
574 .onenter = auth_fsm_onenter_failed,
575 },
576};
577
578struct osmo_fsm vlr_auth_fsm = {
579 .name = "VLR_Authenticate",
580 .states = auth_fsm_states,
581 .num_states = ARRAY_SIZE(auth_fsm_states),
582 .allstate_event_mask = 0,
583 .allstate_action = NULL,
584 .log_subsys = DVLR,
585 .event_names = fsm_auth_event_names,
586};
587
588/***********************************************************************
589 * User API (for SGSN/MSC code)
590 ***********************************************************************/
591
592/* MSC->VLR: Start Procedure Authenticate_VLR (TS 23.012 Ch. 4.1.2.2) */
593struct osmo_fsm_inst *auth_fsm_start(struct vlr_subscr *vsub,
594 uint32_t log_level,
595 struct osmo_fsm_inst *parent,
596 uint32_t parent_term_event,
597 bool is_r99,
598 bool is_utran)
599{
600 struct osmo_fsm_inst *fi;
601 struct auth_fsm_priv *afp;
602
603 fi = osmo_fsm_inst_alloc_child(&vlr_auth_fsm, parent,
604 parent_term_event);
Neels Hofmeyrc698ab92017-11-07 13:23:13 +0100605 if (!fi) {
606 osmo_fsm_inst_dispatch(parent, parent_term_event, 0);
607 return NULL;
608 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200609
610 afp = talloc_zero(fi, struct auth_fsm_priv);
611 if (!afp) {
612 osmo_fsm_inst_dispatch(parent, parent_term_event, 0);
613 return NULL;
614 }
615
616 afp->vsub = vsub;
617 if (vsub->imsi[0])
618 afp->by_imsi = true;
619 afp->is_r99 = is_r99;
620 afp->is_utran = is_utran;
621 fi->priv = afp;
622 vsub->auth_fsm = fi;
623
624 osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL);
625
626 return fi;
627}