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> |
| 25 | |
| 26 | #include <openbsc/debug.h> |
| 27 | |
| 28 | const struct value_string auth_state_names[] = { |
| 29 | { SGSN_AUTH_ACCEPTED, "accepted"}, |
| 30 | { SGSN_AUTH_REJECTED, "rejected"}, |
| 31 | { SGSN_AUTH_UNKNOWN, "unknown"}, |
| 32 | { 0, NULL } |
| 33 | }; |
Jacob Erlbeck | 4760eae | 2014-10-24 15:11:03 +0200 | [diff] [blame] | 34 | |
| 35 | void sgsn_auth_init(struct sgsn_instance *sgi) |
| 36 | { |
| 37 | INIT_LLIST_HEAD(&sgi->cfg.imsi_acl); |
| 38 | } |
| 39 | |
| 40 | /* temporary IMSI ACL hack */ |
| 41 | struct imsi_acl_entry *sgsn_acl_lookup(const char *imsi, struct sgsn_config *cfg) |
| 42 | { |
| 43 | struct imsi_acl_entry *acl; |
| 44 | llist_for_each_entry(acl, &cfg->imsi_acl, list) { |
| 45 | if (!strcmp(imsi, acl->imsi)) |
| 46 | return acl; |
| 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | int sgsn_acl_add(const char *imsi, struct sgsn_config *cfg) |
| 52 | { |
| 53 | struct imsi_acl_entry *acl; |
| 54 | |
| 55 | if (sgsn_acl_lookup(imsi, cfg)) |
| 56 | return -EEXIST; |
| 57 | |
| 58 | acl = talloc_zero(NULL, struct imsi_acl_entry); |
| 59 | if (!acl) |
| 60 | return -ENOMEM; |
| 61 | strncpy(acl->imsi, imsi, sizeof(acl->imsi)); |
| 62 | |
| 63 | llist_add(&acl->list, &cfg->imsi_acl); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int sgsn_acl_del(const char *imsi, struct sgsn_config *cfg) |
| 69 | { |
| 70 | struct imsi_acl_entry *acl; |
| 71 | |
| 72 | acl = sgsn_acl_lookup(imsi, cfg); |
| 73 | if (!acl) |
| 74 | return -ENODEV; |
| 75 | |
| 76 | llist_del(&acl->list); |
| 77 | talloc_free(acl); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 82 | enum sgsn_auth_state sgsn_auth_state(struct sgsn_mm_ctx *mmctx, |
| 83 | struct sgsn_config *cfg) |
| 84 | { |
| 85 | char mccmnc[16]; |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 86 | int check_net = 0; |
| 87 | int check_acl = 0; |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 88 | |
| 89 | OSMO_ASSERT(mmctx); |
| 90 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 91 | switch (sgsn->cfg.auth_policy) { |
| 92 | case SGSN_AUTH_POLICY_OPEN: |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 93 | return SGSN_AUTH_ACCEPTED; |
| 94 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 95 | case SGSN_AUTH_POLICY_CLOSED: |
| 96 | check_net = 1; |
| 97 | check_acl = 1; |
| 98 | break; |
| 99 | |
| 100 | case SGSN_AUTH_POLICY_ACL_ONLY: |
| 101 | check_acl = 1; |
| 102 | break; |
| 103 | } |
| 104 | |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 105 | if (!strlen(mmctx->imsi)) { |
| 106 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 107 | "Missing IMSI, authorization state not known\n"); |
| 108 | return SGSN_AUTH_UNKNOWN; |
| 109 | } |
| 110 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 111 | if (check_net) { |
| 112 | /* We simply assume that the IMSI exists, as long as it is part |
| 113 | * of 'our' network */ |
| 114 | snprintf(mccmnc, sizeof(mccmnc), "%03d%02d", |
| 115 | mmctx->ra.mcc, mmctx->ra.mnc); |
| 116 | if (strncmp(mccmnc, mmctx->imsi, 5) == 0) |
| 117 | return SGSN_AUTH_ACCEPTED; |
| 118 | } |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 119 | |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 120 | if (check_acl && sgsn_acl_lookup(mmctx->imsi, &sgsn->cfg)) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 121 | return SGSN_AUTH_ACCEPTED; |
| 122 | |
| 123 | return SGSN_AUTH_REJECTED; |
| 124 | } |
| 125 | |
| 126 | int sgsn_auth_request(struct sgsn_mm_ctx *mmctx, struct sgsn_config *cfg) |
| 127 | { |
| 128 | struct sgsn_subscriber_data sd = {0}; |
| 129 | |
| 130 | sd.auth_state = sgsn_auth_state(mmctx, cfg); |
| 131 | |
| 132 | if (sd.auth_state == SGSN_AUTH_UNKNOWN) { |
| 133 | LOGMMCTXP(LOGL_ERROR, mmctx, |
| 134 | "Missing information, authorization not possible\n"); |
| 135 | sd.auth_state = SGSN_AUTH_REJECTED; |
| 136 | } |
| 137 | |
| 138 | /* This will call sgsn_auth_update if auth_state has changed */ |
| 139 | sgsn_update_subscriber_data(mmctx, &sd); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | void sgsn_auth_update(struct sgsn_mm_ctx *mmctx, struct sgsn_subscriber_data *sd) |
| 144 | { |
| 145 | LOGMMCTXP(LOGL_INFO, mmctx, "Got authorization update: state %s\n", |
| 146 | get_value_string(auth_state_names, sd->auth_state)); |
| 147 | |
| 148 | mmctx->auth_state = sd->auth_state; |
| 149 | |
| 150 | switch (sd->auth_state) { |
| 151 | case SGSN_AUTH_ACCEPTED: |
| 152 | gsm0408_gprs_access_granted(mmctx); |
| 153 | break; |
| 154 | case SGSN_AUTH_REJECTED: |
| 155 | gsm0408_gprs_access_denied(mmctx); |
| 156 | break; |
| 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | } |