blob: e123909f2d8d0efb24680405a7ec41e7dc501409 [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
35void sgsn_auth_init(struct sgsn_instance *sgi)
36{
37 INIT_LLIST_HEAD(&sgi->cfg.imsi_acl);
38}
39
40/* temporary IMSI ACL hack */
41struct 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
51int 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
68int 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 Erlbeck423f8bf2014-10-24 18:09:54 +020082enum sgsn_auth_state sgsn_auth_state(struct sgsn_mm_ctx *mmctx,
83 struct sgsn_config *cfg)
84{
85 char mccmnc[16];
86
87 OSMO_ASSERT(mmctx);
88
89 if (!sgsn->cfg.acl_enabled)
90 return SGSN_AUTH_ACCEPTED;
91
92 if (!strlen(mmctx->imsi)) {
93 LOGMMCTXP(LOGL_NOTICE, mmctx,
94 "Missing IMSI, authorization state not known\n");
95 return SGSN_AUTH_UNKNOWN;
96 }
97
98 /* As a temorary hack, we simply assume that the IMSI exists,
99 * as long as it is part of 'our' network */
100 snprintf(mccmnc, sizeof(mccmnc), "%03d%02d", mmctx->ra.mcc, mmctx->ra.mnc);
101 if (strncmp(mccmnc, mmctx->imsi, 5) == 0)
102 return SGSN_AUTH_ACCEPTED;
103
104 if (sgsn_acl_lookup(mmctx->imsi, &sgsn->cfg))
105 return SGSN_AUTH_ACCEPTED;
106
107 return SGSN_AUTH_REJECTED;
108}
109
110int sgsn_auth_request(struct sgsn_mm_ctx *mmctx, struct sgsn_config *cfg)
111{
112 struct sgsn_subscriber_data sd = {0};
113
114 sd.auth_state = sgsn_auth_state(mmctx, cfg);
115
116 if (sd.auth_state == SGSN_AUTH_UNKNOWN) {
117 LOGMMCTXP(LOGL_ERROR, mmctx,
118 "Missing information, authorization not possible\n");
119 sd.auth_state = SGSN_AUTH_REJECTED;
120 }
121
122 /* This will call sgsn_auth_update if auth_state has changed */
123 sgsn_update_subscriber_data(mmctx, &sd);
124 return 0;
125}
126
127void sgsn_auth_update(struct sgsn_mm_ctx *mmctx, struct sgsn_subscriber_data *sd)
128{
129 LOGMMCTXP(LOGL_INFO, mmctx, "Got authorization update: state %s\n",
130 get_value_string(auth_state_names, sd->auth_state));
131
132 mmctx->auth_state = sd->auth_state;
133
134 switch (sd->auth_state) {
135 case SGSN_AUTH_ACCEPTED:
136 gsm0408_gprs_access_granted(mmctx);
137 break;
138 case SGSN_AUTH_REJECTED:
139 gsm0408_gprs_access_denied(mmctx);
140 break;
141 default:
142 break;
143 }
144}