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