blob: 0407e9e69ae6e88a52ce1da9773a99875e9f8955 [file] [log] [blame]
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +02001/* 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 Erlbeck423f8bf2014-10-24 18:09:54 +020024#include <openbsc/gprs_gmm.h>
25
26#include <openbsc/debug.h>
27
28const 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 Erlbeck3b5d4072014-10-24 15:11:03 +020034
Jacob Erlbeckf951a012014-11-07 14:17:44 +010035const struct value_string *sgsn_auth_state_names = auth_state_names;
36
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +020037void sgsn_auth_init(struct sgsn_instance *sgi)
38{
39 INIT_LLIST_HEAD(&sgi->cfg.imsi_acl);
40}
41
42/* temporary IMSI ACL hack */
43struct imsi_acl_entry *sgsn_acl_lookup(const char *imsi, struct sgsn_config *cfg)
44{
45 struct imsi_acl_entry *acl;
46 llist_for_each_entry(acl, &cfg->imsi_acl, list) {
47 if (!strcmp(imsi, acl->imsi))
48 return acl;
49 }
50 return NULL;
51}
52
53int sgsn_acl_add(const char *imsi, struct sgsn_config *cfg)
54{
55 struct imsi_acl_entry *acl;
56
57 if (sgsn_acl_lookup(imsi, cfg))
58 return -EEXIST;
59
60 acl = talloc_zero(NULL, struct imsi_acl_entry);
61 if (!acl)
62 return -ENOMEM;
63 strncpy(acl->imsi, imsi, sizeof(acl->imsi));
64
65 llist_add(&acl->list, &cfg->imsi_acl);
66
67 return 0;
68}
69
70int sgsn_acl_del(const char *imsi, struct sgsn_config *cfg)
71{
72 struct imsi_acl_entry *acl;
73
74 acl = sgsn_acl_lookup(imsi, cfg);
75 if (!acl)
76 return -ENODEV;
77
78 llist_del(&acl->list);
79 talloc_free(acl);
80
81 return 0;
82}
83
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +020084enum sgsn_auth_state sgsn_auth_state(struct sgsn_mm_ctx *mmctx,
85 struct sgsn_config *cfg)
86{
87 char mccmnc[16];
Jacob Erlbeck106f5472014-11-04 10:08:37 +010088 int check_net = 0;
89 int check_acl = 0;
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +020090
91 OSMO_ASSERT(mmctx);
92
Jacob Erlbeck106f5472014-11-04 10:08:37 +010093 switch (sgsn->cfg.auth_policy) {
94 case SGSN_AUTH_POLICY_OPEN:
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +020095 return SGSN_AUTH_ACCEPTED;
96
Jacob Erlbeck106f5472014-11-04 10:08:37 +010097 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;
105 }
106
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200107 if (!strlen(mmctx->imsi)) {
108 LOGMMCTXP(LOGL_NOTICE, mmctx,
109 "Missing IMSI, authorization state not known\n");
110 return SGSN_AUTH_UNKNOWN;
111 }
112
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100113 if (check_net) {
114 /* We simply assume that the IMSI exists, as long as it is part
115 * of 'our' network */
116 snprintf(mccmnc, sizeof(mccmnc), "%03d%02d",
117 mmctx->ra.mcc, mmctx->ra.mnc);
118 if (strncmp(mccmnc, mmctx->imsi, 5) == 0)
119 return SGSN_AUTH_ACCEPTED;
120 }
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200121
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100122 if (check_acl && sgsn_acl_lookup(mmctx->imsi, &sgsn->cfg))
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200123 return SGSN_AUTH_ACCEPTED;
124
125 return SGSN_AUTH_REJECTED;
126}
127
128int sgsn_auth_request(struct sgsn_mm_ctx *mmctx, struct sgsn_config *cfg)
129{
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100130 /* TODO: Add remote subscriber update requests here */
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200131
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100132 sgsn_auth_update(mmctx, sgsn);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200133
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200134 return 0;
135}
136
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100137void sgsn_auth_update(struct sgsn_mm_ctx *mmctx, struct sgsn_instance *sgi)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200138{
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100139 enum sgsn_auth_state auth_state;
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200140
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100141 LOGMMCTXP(LOGL_DEBUG, mmctx, "Updating authorization\n");
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200142
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100143 auth_state = sgsn_auth_state(mmctx, &sgi->cfg);
144 if (auth_state == SGSN_AUTH_UNKNOWN) {
145 /* Reject requests since remote updates are NYI */
146 LOGMMCTXP(LOGL_ERROR, mmctx,
147 "Missing information, authorization not possible\n");
148 auth_state = SGSN_AUTH_REJECTED;
149 }
150
151 if (mmctx->auth_state == auth_state)
152 return;
153
154 LOGMMCTXP(LOGL_INFO, mmctx, "Got authorization update: state %s -> %s\n",
155 get_value_string(sgsn_auth_state_names, mmctx->auth_state),
156 get_value_string(sgsn_auth_state_names, auth_state));
157
158 mmctx->auth_state = auth_state;
159
160 switch (auth_state) {
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200161 case SGSN_AUTH_ACCEPTED:
162 gsm0408_gprs_access_granted(mmctx);
163 break;
164 case SGSN_AUTH_REJECTED:
165 gsm0408_gprs_access_denied(mmctx);
166 break;
167 default:
168 break;
169 }
170}