blob: 60265104d120454b50f2cacdbda4fe4dbd706231 [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 */
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +010068static struct vlr_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;
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +010073 struct vlr_auth_tuple *at = NULL;
74 unsigned int key_seq = VLR_KEY_SEQ_INVAL;
Harald Welteb8b85a12016-06-17 00:06:42 +020075
76 if (!vsub)
77 return NULL;
78
79 if (vsub->last_tuple)
80 key_seq = vsub->last_tuple->key_seq;
81
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +010082 if (key_seq == VLR_KEY_SEQ_INVAL)
Harald Welteb8b85a12016-06-17 00:06:42 +020083 /* 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
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +010091 if (vsub->auth_tuples[idx].key_seq == VLR_KEY_SEQ_INVAL)
Harald Welteb8b85a12016-06-17 00:06:42 +020092 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. */
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100105static struct vlr_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{
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100108 struct vlr_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{
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100127 struct vlr_auth_tuple *at = vsub->last_tuple;
Harald Welteb8b85a12016-06-17 00:06:42 +0200128 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{
Harald Welteb8b85a12016-06-17 00:06:42 +0200243 LOGPFSM(fi, "Authentication terminating with result %s\n",
Neels Hofmeyr15809592018-04-06 02:57:51 +0200244 vlr_auth_fsm_result_name(result));
Harald Welteb8b85a12016-06-17 00:06:42 +0200245
246 /* Do one final state transition (mostly for logging purpose) */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200247 if (!result)
Harald Welteb8b85a12016-06-17 00:06:42 +0200248 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTHENTICATED, 0, 0);
249 else
250 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTH_FAILED, 0, 0);
251
252 /* return the result to the parent FSM */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200253 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, &result);
Neels Hofmeyr868f5052018-12-28 00:20:20 +0100254}
255
256static void auth_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
257{
258 struct auth_fsm_priv *afp = fi->priv;
259 struct vlr_subscr *vsub = afp->vsub;
Harald Welteb8b85a12016-06-17 00:06:42 +0200260 vsub->auth_fsm = NULL;
261}
262
263/* back-end function transmitting authentication. Caller ensures we have valid
264 * tuple */
265static int _vlr_subscr_authenticate(struct osmo_fsm_inst *fi)
266{
267 struct auth_fsm_priv *afp = fi->priv;
268 struct vlr_subscr *vsub = afp->vsub;
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100269 struct vlr_auth_tuple *at;
Neels Hofmeyrfe718bc2018-03-11 01:24:33 +0100270 bool use_umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200271
272 /* Caller ensures we have vectors available */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100273 at = vlr_subscr_get_auth_tuple(vsub, afp->auth_tuple_max_reuse_count);
Harald Welteb8b85a12016-06-17 00:06:42 +0200274 if (!at) {
275 LOGPFSML(fi, LOGL_ERROR, "A previous check ensured that an"
276 " auth tuple was available, but now there is in fact"
277 " none.\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200278 auth_fsm_term(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200279 return -1;
280 }
281
Neels Hofmeyrfe718bc2018-03-11 01:24:33 +0100282 use_umts_aka = vlr_use_umts_aka(&at->vec, afp->is_r99);
283 LOGPFSM(fi, "got auth tuple: use_count=%d key_seq=%d"
284 " -- will use %s AKA (is_r99=%s, at->vec.auth_types=0x%x)\n",
285 at->use_count, at->key_seq,
286 use_umts_aka ? "UMTS" : "GSM", afp->is_r99 ? "yes" : "no", at->vec.auth_types);
Harald Welteb8b85a12016-06-17 00:06:42 +0200287
288 /* Transmit auth req to subscriber */
289 afp->auth_requested = true;
290 vsub->last_tuple = at;
Neels Hofmeyrfe718bc2018-03-11 01:24:33 +0100291 vsub->vlr->ops.tx_auth_req(vsub->msc_conn_ref, at, use_umts_aka);
Harald Welteb8b85a12016-06-17 00:06:42 +0200292 return 0;
293}
294
295/***********************************************************************
296 * FSM State Action functions
297 ***********************************************************************/
298
299/* Initial State of TS 23.018 AUT_VLR */
300static void auth_fsm_needs_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data)
301{
302 struct auth_fsm_priv *afp = fi->priv;
303 struct vlr_subscr *vsub = afp->vsub;
304
305 OSMO_ASSERT(event == VLR_AUTH_E_START);
306
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100307 /* Start off with the default max_reuse_count, possibly change that if we
Harald Welteb8b85a12016-06-17 00:06:42 +0200308 * need to re-use an old tuple. */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100309 afp->auth_tuple_max_reuse_count = vsub->vlr->cfg.auth_tuple_max_reuse_count;
Harald Welteb8b85a12016-06-17 00:06:42 +0200310
311 /* Check if we have vectors available */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100312 if (!vlr_subscr_has_auth_tuple(vsub, afp->auth_tuple_max_reuse_count)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200313 /* Obtain_Authentication_Sets_VLR */
Max770fbd22018-01-24 12:48:33 +0100314 int rc = vlr_subscr_req_sai(vsub, NULL, NULL);
315 if (rc < 0)
316 LOGPFSM(fi, "Failed to request Authentication Sets from VLR\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200317 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH_WAIT_AI,
318 GSM_29002_TIMER_M, 0);
319 } else {
320 /* go straight ahead with sending auth request */
321 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP,
322 vlr_timer(vsub->vlr, 3260), 3260);
323 _vlr_subscr_authenticate(fi);
324 }
325}
326
327/* Waiting for Authentication Info from HLR */
328static void auth_fsm_wait_ai(struct osmo_fsm_inst *fi, uint32_t event,
329 void *data)
330{
331 struct auth_fsm_priv *afp = fi->priv;
332 struct vlr_subscr *vsub = afp->vsub;
333 struct osmo_gsup_message *gsup = data;
334
335 if (event == VLR_AUTH_E_HLR_SAI_NACK)
336 LOGPFSM(fi, "GSUP: rx Auth Info Error cause: %d: %s\n",
337 gsup->cause,
338 get_value_string(gsm48_gmm_cause_names, gsup->cause));
339
340 /* We are in what corresponds to the
341 * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */
342 if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors)
343 || (event == VLR_AUTH_E_HLR_SAI_NACK &&
344 gsup->cause != GMM_CAUSE_IMSI_UNKNOWN)
345 || (event == VLR_AUTH_E_HLR_SAI_ABORT)) {
346 if (vsub->vlr->cfg.auth_reuse_old_sets_on_error
347 && vlr_subscr_has_auth_tuple(vsub, -1)) {
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100348 /* To re-use an old tuple, disable the max_reuse_count
Harald Welteb8b85a12016-06-17 00:06:42 +0200349 * constraint. */
Neels Hofmeyr33f53412017-10-29 02:11:18 +0100350 afp->auth_tuple_max_reuse_count = -1;
Harald Welteb8b85a12016-06-17 00:06:42 +0200351 goto pass;
352 }
353 /* result = procedure error */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200354 auth_fsm_term(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200355 return;
356 }
357
358 switch (event) {
359 case VLR_AUTH_E_HLR_SAI_ACK:
360 vlr_subscr_update_tuples(vsub, gsup);
361 goto pass;
362 break;
363 case VLR_AUTH_E_HLR_SAI_NACK:
364 auth_fsm_term(fi,
365 gsup->cause == GMM_CAUSE_IMSI_UNKNOWN?
Neels Hofmeyr15809592018-04-06 02:57:51 +0200366 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR
367 : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200368 break;
369 }
370
371 return;
372pass:
373 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP,
374 vlr_timer(vsub->vlr, 3260), 3260);
375 _vlr_subscr_authenticate(fi);
376}
377
378/* Waiting for Authentication Response from MS */
379static void auth_fsm_wait_auth_resp(struct osmo_fsm_inst *fi, uint32_t event,
380 void *data)
381{
382 struct auth_fsm_priv *afp = fi->priv;
383 struct vlr_subscr *vsub = afp->vsub;
384 struct vlr_instance *vlr = vsub->vlr;
385 struct vlr_auth_resp_par *par = data;
386 int rc;
387
388 switch (event) {
389 case VLR_AUTH_E_MS_AUTH_RESP:
390 rc = check_auth_resp(vsub, par->is_r99, par->is_utran,
391 par->res, par->res_len);
392 if (rc == false) {
393 if (!afp->by_imsi) {
394 vlr->ops.tx_id_req(vsub->msc_conn_ref,
395 GSM_MI_TYPE_IMSI);
396 osmo_fsm_inst_state_chg(fi,
397 VLR_SUB_AS_WAIT_ID_IMSI,
398 vlr_timer(vlr, 3270), 3270);
399 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200400 auth_fsm_term(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200401 }
402 } else {
Neels Hofmeyr15809592018-04-06 02:57:51 +0200403 auth_fsm_term(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200404 }
405 break;
406 case VLR_AUTH_E_MS_AUTH_FAIL:
407 if (par->auts) {
408 /* First failure, start re-sync attempt */
Max770fbd22018-01-24 12:48:33 +0100409 rc = vlr_subscr_req_sai(vsub, par->auts,
Harald Welteb8b85a12016-06-17 00:06:42 +0200410 vsub->last_tuple->vec.rand);
411 osmo_fsm_inst_state_chg(fi,
412 VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC,
413 GSM_29002_TIMER_M, 0);
414 } else
Neels Hofmeyr15809592018-04-06 02:57:51 +0200415 auth_fsm_term(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200416 break;
417 }
418}
419
420/* Waiting for Authentication Info from HLR (resync case) */
421static void auth_fsm_wait_ai_resync(struct osmo_fsm_inst *fi,
422 uint32_t event, void *data)
423{
424 struct auth_fsm_priv *afp = fi->priv;
425 struct vlr_subscr *vsub = afp->vsub;
426 struct osmo_gsup_message *gsup = data;
427
428 /* We are in what corresponds to the
429 * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */
430 if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) ||
431 (event == VLR_AUTH_E_HLR_SAI_NACK &&
432 gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) ||
433 (event == VLR_AUTH_E_HLR_SAI_ABORT)) {
434 /* result = procedure error */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200435 auth_fsm_term(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200436 }
437 switch (event) {
438 case VLR_AUTH_E_HLR_SAI_ACK:
439 vlr_subscr_update_tuples(vsub, gsup);
440 goto pass;
441 break;
442 case VLR_AUTH_E_HLR_SAI_NACK:
443 auth_fsm_term(fi,
444 gsup->cause == GMM_CAUSE_IMSI_UNKNOWN?
Neels Hofmeyr15809592018-04-06 02:57:51 +0200445 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR
446 : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200447 break;
448 }
449
450 return;
451pass:
452 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP_RESYNC,
453 vlr_timer(vsub->vlr, 3260), 3260);
454 _vlr_subscr_authenticate(fi);
455}
456
457/* Waiting for AUTH RESP from MS (re-sync case) */
458static void auth_fsm_wait_auth_resp_resync(struct osmo_fsm_inst *fi,
459 uint32_t event, void *data)
460{
461 struct auth_fsm_priv *afp = fi->priv;
462 struct vlr_subscr *vsub = afp->vsub;
463 struct vlr_auth_resp_par *par = data;
464 struct vlr_instance *vlr = vsub->vlr;
465 int rc;
466
467 switch (event) {
468 case VLR_AUTH_E_MS_AUTH_RESP:
469 rc = check_auth_resp(vsub, par->is_r99, par->is_utran,
470 par->res, par->res_len);
471 if (rc == false) {
472 if (!afp->by_imsi) {
473 vlr->ops.tx_id_req(vsub->msc_conn_ref,
474 GSM_MI_TYPE_IMSI);
475 osmo_fsm_inst_state_chg(fi,
476 VLR_SUB_AS_WAIT_ID_IMSI,
477 vlr_timer(vlr, 3270), 3270);
478 } else {
479 /* Result = Aborted */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200480 auth_fsm_term(fi, GSM48_REJECT_SYNCH_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200481 }
482 } else {
483 /* Result = Pass */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200484 auth_fsm_term(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200485 }
486 break;
487 case VLR_AUTH_E_MS_AUTH_FAIL:
488 /* Second failure: Result = Fail */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200489 auth_fsm_term(fi, GSM48_REJECT_SYNCH_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200490 break;
491 }
492}
493
494/* AUT_VLR waiting for Obtain_IMSI_VLR result */
495static void auth_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event,
496 void *data)
497{
498 struct auth_fsm_priv *afp = fi->priv;
499 struct vlr_subscr *vsub = afp->vsub;
500 const char *mi_string = data;
501
502 switch (event) {
503 case VLR_AUTH_E_MS_ID_IMSI:
504 if (vsub->imsi[0]
505 && !vlr_subscr_matches_imsi(vsub, mi_string)) {
506 LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:"
507 " %s\n", mi_string);
508 } else {
509 strncpy(vsub->imsi, mi_string, sizeof(vsub->imsi));
510 vsub->imsi[sizeof(vsub->imsi)-1] = '\0';
511 }
512 /* retry with identity=IMSI */
513 afp->by_imsi = true;
514 osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH, 0, 0);
515 osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL);
516 break;
517 }
518}
519
520static const struct osmo_fsm_state auth_fsm_states[] = {
521 [VLR_SUB_AS_NEEDS_AUTH] = {
522 .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH),
523 .in_event_mask = S(VLR_AUTH_E_START),
524 .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI) |
525 S(VLR_SUB_AS_WAIT_RESP),
526 .action = auth_fsm_needs_auth,
527 },
528 [VLR_SUB_AS_NEEDS_AUTH_WAIT_AI] = {
529 .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI),
530 .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) |
531 S(VLR_AUTH_E_HLR_SAI_NACK),
532 .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) |
533 S(VLR_SUB_AS_WAIT_RESP),
534 .action = auth_fsm_wait_ai,
535 },
536 [VLR_SUB_AS_WAIT_RESP] = {
537 .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP),
538 .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) |
539 S(VLR_AUTH_E_MS_AUTH_FAIL),
540 .out_state_mask = S(VLR_SUB_AS_WAIT_ID_IMSI) |
541 S(VLR_SUB_AS_AUTH_FAILED) |
542 S(VLR_SUB_AS_AUTHENTICATED) |
543 S(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC),
544 .action = auth_fsm_wait_auth_resp,
545 },
546 [VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC] = {
547 .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC),
548 .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) |
549 S(VLR_AUTH_E_HLR_SAI_NACK),
550 .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) |
551 S(VLR_SUB_AS_WAIT_RESP_RESYNC),
552 .action = auth_fsm_wait_ai_resync,
553 },
554 [VLR_SUB_AS_WAIT_RESP_RESYNC] = {
555 .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP_RESYNC),
556 .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) |
557 S(VLR_AUTH_E_MS_AUTH_FAIL),
558 .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) |
559 S(VLR_SUB_AS_AUTHENTICATED),
560 .action = auth_fsm_wait_auth_resp_resync,
561 },
562 [VLR_SUB_AS_WAIT_ID_IMSI] = {
563 .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_ID_IMSI),
564 .in_event_mask = S(VLR_AUTH_E_MS_ID_IMSI),
565 .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH),
566 .action = auth_fsm_wait_imsi,
567 },
568 [VLR_SUB_AS_AUTHENTICATED] = {
569 .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTHENTICATED),
570 .in_event_mask = 0,
571 .out_state_mask = 0,
572 },
573 [VLR_SUB_AS_AUTH_FAILED] = {
574 .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTH_FAILED),
575 .in_event_mask = 0,
576 .out_state_mask = 0,
577 .onenter = auth_fsm_onenter_failed,
578 },
579};
580
581struct osmo_fsm vlr_auth_fsm = {
582 .name = "VLR_Authenticate",
583 .states = auth_fsm_states,
584 .num_states = ARRAY_SIZE(auth_fsm_states),
585 .allstate_event_mask = 0,
586 .allstate_action = NULL,
587 .log_subsys = DVLR,
588 .event_names = fsm_auth_event_names,
Neels Hofmeyr868f5052018-12-28 00:20:20 +0100589 .cleanup = auth_fsm_cleanup,
Harald Welteb8b85a12016-06-17 00:06:42 +0200590};
591
592/***********************************************************************
593 * User API (for SGSN/MSC code)
594 ***********************************************************************/
595
596/* MSC->VLR: Start Procedure Authenticate_VLR (TS 23.012 Ch. 4.1.2.2) */
597struct osmo_fsm_inst *auth_fsm_start(struct vlr_subscr *vsub,
598 uint32_t log_level,
599 struct osmo_fsm_inst *parent,
600 uint32_t parent_term_event,
601 bool is_r99,
602 bool is_utran)
603{
604 struct osmo_fsm_inst *fi;
605 struct auth_fsm_priv *afp;
606
607 fi = osmo_fsm_inst_alloc_child(&vlr_auth_fsm, parent,
608 parent_term_event);
Neels Hofmeyrc698ab92017-11-07 13:23:13 +0100609 if (!fi) {
610 osmo_fsm_inst_dispatch(parent, parent_term_event, 0);
611 return NULL;
612 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200613
614 afp = talloc_zero(fi, struct auth_fsm_priv);
615 if (!afp) {
616 osmo_fsm_inst_dispatch(parent, parent_term_event, 0);
617 return NULL;
618 }
619
620 afp->vsub = vsub;
621 if (vsub->imsi[0])
622 afp->by_imsi = true;
623 afp->is_r99 = is_r99;
624 afp->is_utran = is_utran;
625 fi->priv = afp;
626 vsub->auth_fsm = fi;
627
628 osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL);
629
630 return fi;
631}
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100632
633bool auth_try_reuse_tuple(struct vlr_subscr *vsub, uint8_t key_seq)
634{
635 int max_reuse_count = vsub->vlr->cfg.auth_tuple_max_reuse_count;
636 struct vlr_auth_tuple *at = vsub->last_tuple;
637
638 if (!at)
639 return false;
640 if ((max_reuse_count >= 0) && (at->use_count > max_reuse_count))
641 return false;
642 if (at->key_seq != key_seq)
643 return false;
644 at->use_count++;
645 return true;
646}