Jacob Erlbeck | 3b5d407 | 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 | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 24 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 25 | #include <openbsc/gsm_subscriber.h> |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 26 | #include <openbsc/gsm_04_08_gprs.h> |
Jacob Erlbeck | 423f8bf | 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 | 3b5d407 | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 35 | |
Jacob Erlbeck | f951a01 | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 36 | const struct value_string *sgsn_auth_state_names = auth_state_names; |
| 37 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 38 | void sgsn_auth_init(void) |
Jacob Erlbeck | 3b5d407 | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 39 | { |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 40 | INIT_LLIST_HEAD(&sgsn->cfg.imsi_acl); |
Jacob Erlbeck | 3b5d407 | 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 | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 85 | enum sgsn_auth_state sgsn_auth_state(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 86 | { |
| 87 | char mccmnc[16]; |
Jacob Erlbeck | 106f547 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 88 | int check_net = 0; |
| 89 | int check_acl = 0; |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 90 | |
| 91 | OSMO_ASSERT(mmctx); |
| 92 | |
Jacob Erlbeck | 106f547 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 93 | switch (sgsn->cfg.auth_policy) { |
| 94 | case SGSN_AUTH_POLICY_OPEN: |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 95 | return SGSN_AUTH_ACCEPTED; |
| 96 | |
Jacob Erlbeck | 106f547 | 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 | be2c8d9 | 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 | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 110 | if (mmctx->subscr->flags & GPRS_SUBSCRIBER_UPDATE_PENDING_MASK) |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 111 | return mmctx->auth_state; |
| 112 | |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 113 | if (mmctx->subscr->sgsn_data->authenticate && |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 114 | (!mmctx->is_authenticated || |
| 115 | mmctx->subscr->sgsn_data->auth_triplets_updated)) |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 116 | return SGSN_AUTH_AUTHENTICATE; |
| 117 | |
Jacob Erlbeck | be2c8d9 | 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 | 106f547 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Jacob Erlbeck | 423f8bf | 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 | 106f547 | 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 | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 138 | |
Jacob Erlbeck | 106f547 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 139 | if (check_acl && sgsn_acl_lookup(mmctx->imsi, &sgsn->cfg)) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 140 | return SGSN_AUTH_ACCEPTED; |
| 141 | |
| 142 | return SGSN_AUTH_REJECTED; |
| 143 | } |
| 144 | |
Jacob Erlbeck | 98a95ac | 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 | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 150 | int sgsn_auth_request(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 151 | { |
Jacob Erlbeck | 98a95ac | 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 | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 157 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting authorization\n"); |
| 158 | |
Jacob Erlbeck | 98a95ac | 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 | |
| 164 | need_update_location = |
| 165 | mmctx->subscr == NULL || |
| 166 | mmctx->pending_req == GSM48_MT_GMM_ATTACH_REQ; |
| 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 | |
| 174 | if (mmctx->subscr->sgsn_data->authenticate && !mmctx->is_authenticated) { |
| 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 | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 181 | LOGMMCTXP(LOGL_INFO, mmctx, |
Jacob Erlbeck | 98a95ac | 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 | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 188 | } |
Jacob Erlbeck | 98a95ac | 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"); |
| 194 | if (gprs_subscr_request_update_location(mmctx) >= 0) |
| 195 | return 0; |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 196 | } |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 197 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 198 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 202 | void sgsn_auth_update(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 203 | { |
Jacob Erlbeck | f951a01 | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 204 | enum sgsn_auth_state auth_state; |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 205 | struct gsm_subscriber *subscr = mmctx->subscr; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 206 | struct gsm_auth_tuple *at; |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 207 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 208 | auth_state = sgsn_auth_state(mmctx); |
Jacob Erlbeck | 3ea2260 | 2014-12-19 18:17:10 +0100 | [diff] [blame] | 209 | |
| 210 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Updating authorization (%s -> %s)\n", |
| 211 | get_value_string(sgsn_auth_state_names, mmctx->auth_state), |
| 212 | get_value_string(sgsn_auth_state_names, auth_state)); |
| 213 | |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 214 | if (auth_state == SGSN_AUTH_UNKNOWN && subscr && |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 215 | !(subscr->flags & GPRS_SUBSCRIBER_UPDATE_PENDING_MASK)) { |
| 216 | /* Reject requests if gprs_subscr_request_update_location fails */ |
Jacob Erlbeck | f951a01 | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 217 | LOGMMCTXP(LOGL_ERROR, mmctx, |
| 218 | "Missing information, authorization not possible\n"); |
| 219 | auth_state = SGSN_AUTH_REJECTED; |
| 220 | } |
| 221 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 222 | if (auth_state == SGSN_AUTH_AUTHENTICATE && |
| 223 | mmctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) { |
| 224 | /* The current tuple is not valid, but we are possibly called |
| 225 | * because new auth tuples have been received */ |
| 226 | at = sgsn_auth_get_tuple(mmctx, mmctx->auth_triplet.key_seq); |
| 227 | if (!at) { |
| 228 | LOGMMCTXP(LOGL_ERROR, mmctx, |
| 229 | "Missing auth tuples, authorization not possible\n"); |
| 230 | auth_state = SGSN_AUTH_REJECTED; |
| 231 | } else { |
| 232 | mmctx->auth_triplet = *at; |
| 233 | } |
| 234 | } |
| 235 | |
Jacob Erlbeck | f951a01 | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 236 | if (mmctx->auth_state == auth_state) |
| 237 | return; |
| 238 | |
| 239 | LOGMMCTXP(LOGL_INFO, mmctx, "Got authorization update: state %s -> %s\n", |
| 240 | get_value_string(sgsn_auth_state_names, mmctx->auth_state), |
| 241 | get_value_string(sgsn_auth_state_names, auth_state)); |
| 242 | |
| 243 | mmctx->auth_state = auth_state; |
| 244 | |
| 245 | switch (auth_state) { |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 246 | case SGSN_AUTH_AUTHENTICATE: |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 247 | if (subscr) |
| 248 | subscr->sgsn_data->auth_triplets_updated = 0; |
| 249 | |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 250 | gsm0408_gprs_authenticate(mmctx); |
| 251 | break; |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 252 | case SGSN_AUTH_ACCEPTED: |
| 253 | gsm0408_gprs_access_granted(mmctx); |
| 254 | break; |
| 255 | case SGSN_AUTH_REJECTED: |
Jacob Erlbeck | 98647ca | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 256 | if (subscr && (subscr->flags & GPRS_SUBSCRIBER_CANCELLED) != 0) |
| 257 | gsm0408_gprs_access_cancelled(mmctx); |
| 258 | else |
| 259 | gsm0408_gprs_access_denied(mmctx); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 260 | break; |
| 261 | default: |
| 262 | break; |
| 263 | } |
| 264 | } |
Jacob Erlbeck | 7921ab1 | 2014-12-08 15:52:00 +0100 | [diff] [blame] | 265 | |
| 266 | struct gsm_auth_tuple *sgsn_auth_get_tuple(struct sgsn_mm_ctx *mmctx, |
| 267 | unsigned key_seq) |
| 268 | { |
| 269 | unsigned count; |
| 270 | unsigned idx; |
| 271 | struct gsm_auth_tuple *at = NULL; |
| 272 | |
| 273 | struct sgsn_subscriber_data *sdata; |
| 274 | |
| 275 | if (!mmctx->subscr) |
| 276 | return NULL; |
| 277 | |
| 278 | if (key_seq == GSM_KEY_SEQ_INVAL) |
| 279 | /* Start with 0 after increment module array size */ |
| 280 | idx = ARRAY_SIZE(sdata->auth_triplets) - 1; |
| 281 | else |
| 282 | idx = key_seq; |
| 283 | |
| 284 | sdata = mmctx->subscr->sgsn_data; |
| 285 | |
| 286 | /* Find next tuple */ |
| 287 | for (count = ARRAY_SIZE(sdata->auth_triplets); count > 0; count--) { |
| 288 | idx = (idx + 1) % ARRAY_SIZE(sdata->auth_triplets); |
| 289 | |
| 290 | if (sdata->auth_triplets[idx].key_seq == GSM_KEY_SEQ_INVAL) |
| 291 | continue; |
| 292 | |
| 293 | if (sdata->auth_triplets[idx].use_count == 0) { |
| 294 | at = &sdata->auth_triplets[idx]; |
| 295 | at->use_count = 1; |
| 296 | return at; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return NULL; |
| 301 | } |