Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 1 | /* MS authorization and subscriber data handling */ |
| 2 | |
| 3 | /* (C) 2009-2010 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 | |
Harald Welte | 35ade5e | 2016-04-20 17:11:43 +0200 | [diff] [blame] | 22 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 23 | #include <osmocom/core/utils.h> |
Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 24 | #include <openbsc/sgsn.h> |
| 25 | #include <openbsc/gprs_sgsn.h> |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 26 | #include <openbsc/gprs_gmm.h> |
Neels Hofmeyr | e2c94d6 | 2017-01-10 00:49:56 +0100 | [diff] [blame^] | 27 | #include <openbsc/gprs_subscriber.h> |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
| 29 | |
| 30 | const struct value_string auth_state_names[] = { |
| 31 | { SGSN_AUTH_ACCEPTED, "accepted"}, |
| 32 | { SGSN_AUTH_REJECTED, "rejected"}, |
| 33 | { SGSN_AUTH_UNKNOWN, "unknown"}, |
Harald Welte | 84125e7 | 2016-05-05 18:31:37 +0200 | [diff] [blame] | 34 | { SGSN_AUTH_AUTHENTICATE, "authenticate" }, |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 35 | { 0, NULL } |
| 36 | }; |
Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 37 | |
Jacob Erlbeck | 802d78a | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 38 | const struct value_string *sgsn_auth_state_names = auth_state_names; |
| 39 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 40 | void sgsn_auth_init(void) |
Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 41 | { |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 42 | INIT_LLIST_HEAD(&sgsn->cfg.imsi_acl); |
Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* temporary IMSI ACL hack */ |
| 46 | struct imsi_acl_entry *sgsn_acl_lookup(const char *imsi, struct sgsn_config *cfg) |
| 47 | { |
| 48 | struct imsi_acl_entry *acl; |
| 49 | llist_for_each_entry(acl, &cfg->imsi_acl, list) { |
| 50 | if (!strcmp(imsi, acl->imsi)) |
| 51 | return acl; |
| 52 | } |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | int sgsn_acl_add(const char *imsi, struct sgsn_config *cfg) |
| 57 | { |
| 58 | struct imsi_acl_entry *acl; |
| 59 | |
| 60 | if (sgsn_acl_lookup(imsi, cfg)) |
| 61 | return -EEXIST; |
| 62 | |
| 63 | acl = talloc_zero(NULL, struct imsi_acl_entry); |
| 64 | if (!acl) |
| 65 | return -ENOMEM; |
Neels Hofmeyr | 5644c2b | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 66 | osmo_strlcpy(acl->imsi, imsi, sizeof(acl->imsi)); |
Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 67 | |
| 68 | llist_add(&acl->list, &cfg->imsi_acl); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int sgsn_acl_del(const char *imsi, struct sgsn_config *cfg) |
| 74 | { |
| 75 | struct imsi_acl_entry *acl; |
| 76 | |
| 77 | acl = sgsn_acl_lookup(imsi, cfg); |
| 78 | if (!acl) |
| 79 | return -ENODEV; |
| 80 | |
| 81 | llist_del(&acl->list); |
| 82 | talloc_free(acl); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 87 | enum sgsn_auth_state sgsn_auth_state(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 88 | { |
| 89 | char mccmnc[16]; |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 90 | int check_net = 0; |
| 91 | int check_acl = 0; |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 92 | |
| 93 | OSMO_ASSERT(mmctx); |
| 94 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 95 | switch (sgsn->cfg.auth_policy) { |
| 96 | case SGSN_AUTH_POLICY_OPEN: |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 97 | return SGSN_AUTH_ACCEPTED; |
| 98 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 99 | case SGSN_AUTH_POLICY_CLOSED: |
| 100 | check_net = 1; |
| 101 | check_acl = 1; |
| 102 | break; |
| 103 | |
| 104 | case SGSN_AUTH_POLICY_ACL_ONLY: |
| 105 | check_acl = 1; |
| 106 | break; |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 107 | |
| 108 | case SGSN_AUTH_POLICY_REMOTE: |
| 109 | if (!mmctx->subscr) |
| 110 | return mmctx->auth_state; |
| 111 | |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 112 | if (mmctx->subscr->flags & GPRS_SUBSCRIBER_UPDATE_PENDING_MASK) |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 113 | return mmctx->auth_state; |
| 114 | |
Jacob Erlbeck | 16b17ed | 2014-12-17 13:20:08 +0100 | [diff] [blame] | 115 | if (sgsn->cfg.require_authentication && |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 116 | (!mmctx->is_authenticated || |
| 117 | mmctx->subscr->sgsn_data->auth_triplets_updated)) |
Jacob Erlbeck | d812699 | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 118 | return SGSN_AUTH_AUTHENTICATE; |
| 119 | |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 120 | if (mmctx->subscr->authorized) |
| 121 | return SGSN_AUTH_ACCEPTED; |
| 122 | |
| 123 | return SGSN_AUTH_REJECTED; |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 126 | if (!strlen(mmctx->imsi)) { |
| 127 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 128 | "Missing IMSI, authorization state not known\n"); |
| 129 | return SGSN_AUTH_UNKNOWN; |
| 130 | } |
| 131 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 132 | if (check_net) { |
| 133 | /* We simply assume that the IMSI exists, as long as it is part |
| 134 | * of 'our' network */ |
| 135 | snprintf(mccmnc, sizeof(mccmnc), "%03d%02d", |
| 136 | mmctx->ra.mcc, mmctx->ra.mnc); |
| 137 | if (strncmp(mccmnc, mmctx->imsi, 5) == 0) |
| 138 | return SGSN_AUTH_ACCEPTED; |
| 139 | } |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 140 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 141 | if (check_acl && sgsn_acl_lookup(mmctx->imsi, &sgsn->cfg)) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 142 | return SGSN_AUTH_ACCEPTED; |
| 143 | |
| 144 | return SGSN_AUTH_REJECTED; |
| 145 | } |
| 146 | |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 147 | /* |
| 148 | * This function is directly called by e.g. the GMM layer. It returns either |
| 149 | * after calling sgsn_auth_update directly or after triggering an asynchronous |
| 150 | * procedure which will call sgsn_auth_update later on. |
| 151 | */ |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 152 | int sgsn_auth_request(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 153 | { |
Neels Hofmeyr | e2c94d6 | 2017-01-10 00:49:56 +0100 | [diff] [blame^] | 154 | struct gprs_subscr *subscr; |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 155 | struct gsm_auth_tuple *at; |
| 156 | int need_update_location; |
| 157 | int rc; |
| 158 | |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 159 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting authorization\n"); |
| 160 | |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 161 | if (sgsn->cfg.auth_policy != SGSN_AUTH_POLICY_REMOTE) { |
| 162 | sgsn_auth_update(mmctx); |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jacob Erlbeck | 6ff7f64 | 2014-12-19 18:08:48 +0100 | [diff] [blame] | 166 | need_update_location = sgsn->cfg.require_update_location && |
| 167 | (mmctx->subscr == NULL || |
| 168 | mmctx->pending_req == GSM48_MT_GMM_ATTACH_REQ); |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 169 | |
| 170 | /* This has the side effect of registering the subscr with the mmctx */ |
| 171 | subscr = gprs_subscr_get_or_create_by_mmctx(mmctx); |
Neels Hofmeyr | e2c94d6 | 2017-01-10 00:49:56 +0100 | [diff] [blame^] | 172 | gprs_subscr_put(subscr); |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 173 | |
| 174 | OSMO_ASSERT(mmctx->subscr != NULL); |
| 175 | |
Jacob Erlbeck | 16b17ed | 2014-12-17 13:20:08 +0100 | [diff] [blame] | 176 | if (sgsn->cfg.require_authentication && !mmctx->is_authenticated) { |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 177 | /* Find next tuple */ |
| 178 | at = sgsn_auth_get_tuple(mmctx, mmctx->auth_triplet.key_seq); |
| 179 | |
| 180 | if (!at) { |
| 181 | /* No valid tuple found, request fresh ones */ |
| 182 | mmctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 183 | LOGMMCTXP(LOGL_INFO, mmctx, |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 184 | "Requesting authentication tuples\n"); |
| 185 | rc = gprs_subscr_request_auth_info(mmctx); |
| 186 | if (rc >= 0) |
| 187 | return 0; |
| 188 | |
| 189 | return rc; |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 190 | } |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 191 | |
| 192 | mmctx->auth_triplet = *at; |
| 193 | } else if (need_update_location) { |
| 194 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 195 | "Missing information, requesting subscriber data\n"); |
Jacob Erlbeck | 6ff7f64 | 2014-12-19 18:08:48 +0100 | [diff] [blame] | 196 | rc = gprs_subscr_request_update_location(mmctx); |
| 197 | if (rc >= 0) |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 198 | return 0; |
Jacob Erlbeck | 6ff7f64 | 2014-12-19 18:08:48 +0100 | [diff] [blame] | 199 | |
| 200 | return rc; |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 201 | } |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 202 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 203 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 207 | void sgsn_auth_update(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 208 | { |
Jacob Erlbeck | 802d78a | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 209 | enum sgsn_auth_state auth_state; |
Neels Hofmeyr | e2c94d6 | 2017-01-10 00:49:56 +0100 | [diff] [blame^] | 210 | struct gprs_subscr *subscr = mmctx->subscr; |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 211 | struct gsm_auth_tuple *at; |
Jacob Erlbeck | 4101008 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 212 | int gmm_cause; |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 213 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 214 | auth_state = sgsn_auth_state(mmctx); |
Jacob Erlbeck | 56af9f4 | 2014-12-19 18:17:10 +0100 | [diff] [blame] | 215 | |
| 216 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Updating authorization (%s -> %s)\n", |
| 217 | get_value_string(sgsn_auth_state_names, mmctx->auth_state), |
| 218 | get_value_string(sgsn_auth_state_names, auth_state)); |
| 219 | |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 220 | if (auth_state == SGSN_AUTH_UNKNOWN && subscr && |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 221 | !(subscr->flags & GPRS_SUBSCRIBER_UPDATE_PENDING_MASK)) { |
| 222 | /* Reject requests if gprs_subscr_request_update_location fails */ |
Jacob Erlbeck | 802d78a | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 223 | LOGMMCTXP(LOGL_ERROR, mmctx, |
| 224 | "Missing information, authorization not possible\n"); |
| 225 | auth_state = SGSN_AUTH_REJECTED; |
| 226 | } |
| 227 | |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 228 | if (auth_state == SGSN_AUTH_AUTHENTICATE && |
| 229 | mmctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) { |
| 230 | /* The current tuple is not valid, but we are possibly called |
| 231 | * because new auth tuples have been received */ |
| 232 | at = sgsn_auth_get_tuple(mmctx, mmctx->auth_triplet.key_seq); |
| 233 | if (!at) { |
| 234 | LOGMMCTXP(LOGL_ERROR, mmctx, |
| 235 | "Missing auth tuples, authorization not possible\n"); |
| 236 | auth_state = SGSN_AUTH_REJECTED; |
| 237 | } else { |
| 238 | mmctx->auth_triplet = *at; |
| 239 | } |
| 240 | } |
| 241 | |
Jacob Erlbeck | 802d78a | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 242 | if (mmctx->auth_state == auth_state) |
| 243 | return; |
| 244 | |
| 245 | LOGMMCTXP(LOGL_INFO, mmctx, "Got authorization update: state %s -> %s\n", |
| 246 | get_value_string(sgsn_auth_state_names, mmctx->auth_state), |
| 247 | get_value_string(sgsn_auth_state_names, auth_state)); |
| 248 | |
| 249 | mmctx->auth_state = auth_state; |
| 250 | |
| 251 | switch (auth_state) { |
Jacob Erlbeck | d812699 | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 252 | case SGSN_AUTH_AUTHENTICATE: |
Jacob Erlbeck | 828059f | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 253 | if (subscr) |
| 254 | subscr->sgsn_data->auth_triplets_updated = 0; |
| 255 | |
Jacob Erlbeck | d812699 | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 256 | gsm0408_gprs_authenticate(mmctx); |
| 257 | break; |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 258 | case SGSN_AUTH_ACCEPTED: |
| 259 | gsm0408_gprs_access_granted(mmctx); |
| 260 | break; |
| 261 | case SGSN_AUTH_REJECTED: |
Jacob Erlbeck | f96779f | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 262 | gmm_cause = |
| 263 | subscr ? subscr->sgsn_data->error_cause : |
| 264 | SGSN_ERROR_CAUSE_NONE; |
Jacob Erlbeck | 4101008 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 265 | |
Jacob Erlbeck | bf0b874 | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 266 | if (subscr && (subscr->flags & GPRS_SUBSCRIBER_CANCELLED) != 0) |
Jacob Erlbeck | 4101008 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 267 | gsm0408_gprs_access_cancelled(mmctx, gmm_cause); |
Jacob Erlbeck | bf0b874 | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 268 | else |
Jacob Erlbeck | 4101008 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 269 | gsm0408_gprs_access_denied(mmctx, gmm_cause); |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 270 | break; |
| 271 | default: |
| 272 | break; |
| 273 | } |
| 274 | } |
Jacob Erlbeck | b1332b6 | 2014-12-08 15:52:00 +0100 | [diff] [blame] | 275 | |
| 276 | struct gsm_auth_tuple *sgsn_auth_get_tuple(struct sgsn_mm_ctx *mmctx, |
| 277 | unsigned key_seq) |
| 278 | { |
| 279 | unsigned count; |
| 280 | unsigned idx; |
| 281 | struct gsm_auth_tuple *at = NULL; |
| 282 | |
| 283 | struct sgsn_subscriber_data *sdata; |
| 284 | |
| 285 | if (!mmctx->subscr) |
| 286 | return NULL; |
| 287 | |
| 288 | if (key_seq == GSM_KEY_SEQ_INVAL) |
| 289 | /* Start with 0 after increment module array size */ |
| 290 | idx = ARRAY_SIZE(sdata->auth_triplets) - 1; |
| 291 | else |
| 292 | idx = key_seq; |
| 293 | |
| 294 | sdata = mmctx->subscr->sgsn_data; |
| 295 | |
| 296 | /* Find next tuple */ |
| 297 | for (count = ARRAY_SIZE(sdata->auth_triplets); count > 0; count--) { |
| 298 | idx = (idx + 1) % ARRAY_SIZE(sdata->auth_triplets); |
| 299 | |
| 300 | if (sdata->auth_triplets[idx].key_seq == GSM_KEY_SEQ_INVAL) |
| 301 | continue; |
| 302 | |
| 303 | if (sdata->auth_triplets[idx].use_count == 0) { |
| 304 | at = &sdata->auth_triplets[idx]; |
| 305 | at->use_count = 1; |
| 306 | return at; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return NULL; |
| 311 | } |