blob: 1bebc652625151038fadbd8058c5e9d554cf8c53 [file] [log] [blame]
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +01001/* MS subscriber data handling */
2
3/* (C) 2014 by sysmocom s.f.m.c. GmbH
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -04004 * (C) 2015 by Holger Hans Peter Freyther
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +01005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Harald Welte53373bc2016-04-20 17:11:43 +020023#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welte23d77d52016-04-25 19:07:34 +020024#include <osmocom/gsm/gsup.h>
Harald Welte7e82b742017-08-12 13:43:54 +020025#include <osmocom/gsm/apn.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010026#include <osmocom/core/utils.h>
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010027#include <osmocom/core/logging.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020028#include <osmocom/sgsn/gprs_subscriber.h>
29#include <osmocom/sgsn/gsup_client.h>
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +010030
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020031#include <osmocom/sgsn/sgsn.h>
32#include <osmocom/sgsn/gprs_sgsn.h>
33#include <osmocom/sgsn/gprs_gmm.h>
34#include <osmocom/sgsn/gprs_utils.h>
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +010035
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020036#include <osmocom/sgsn/debug.h>
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +010037
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010038#include <netinet/in.h>
39#include <arpa/inet.h>
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010040#include <limits.h>
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010041
Jacob Erlbeck743dec42015-01-08 15:18:39 +010042#define SGSN_SUBSCR_MAX_RETRIES 3
43#define SGSN_SUBSCR_RETRY_INTERVAL 10
44
Jacob Erlbeck929acdf2015-01-27 13:47:24 +010045#define LOGGSUPP(level, gsup, fmt, args...) \
46 LOGP(DGPRS, level, "GSUP(%s) " fmt, \
47 (gsup)->imsi, \
48 ## args)
49
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +010050extern void *tall_bsc_ctx;
51
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010052LLIST_HEAD(_gprs_subscribers);
53struct llist_head * const gprs_subscribers = &_gprs_subscribers;
54
Neels Hofmeyr814fef02016-12-08 21:19:57 +010055static int gsup_read_cb(struct gsup_client *gsupc, struct msgb *msg);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010056
57/* TODO: Some functions are specific to the SGSN, but this file is more general
58 * (it has gprs_* name). Either move these functions elsewhere, split them and
59 * move a part, or replace the gprs_ prefix by sgsn_. The applies to
60 * gprs_subscr_init, gsup_read_cb, and gprs_subscr_tx_gsup_message.
61 */
62
63int gprs_subscr_init(struct sgsn_instance *sgi)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +010064{
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010065 const char *addr_str;
66
67 if (!sgi->cfg.gsup_server_addr.sin_addr.s_addr)
68 return 0;
69
70 addr_str = inet_ntoa(sgi->cfg.gsup_server_addr.sin_addr);
71
Neels Hofmeyr814fef02016-12-08 21:19:57 +010072 sgi->gsup_client = gsup_client_create(
Neels Hofmeyr398851f2017-03-04 03:15:53 +010073 "SGSN",
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010074 addr_str, sgi->cfg.gsup_server_port,
Neels Hofmeyr9c534fd2015-10-12 11:57:37 +020075 &gsup_read_cb,
76 &sgi->cfg.oap);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010077
78 if (!sgi->gsup_client)
79 return -1;
80
81 return 1;
82}
83
Neels Hofmeyr814fef02016-12-08 21:19:57 +010084static int gsup_read_cb(struct gsup_client *gsupc, struct msgb *msg)
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010085{
86 int rc;
87
88 rc = gprs_subscr_rx_gsup_message(msg);
Jacob Erlbecke154d8b2014-12-19 19:15:55 +010089 msgb_free(msg);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +010090 if (rc < 0)
91 return -1;
92
93 return rc;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +010094}
95
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010096int gprs_subscr_purge(struct gprs_subscr *subscr);
Jacob Erlbeck0f47b8f2015-01-06 16:32:41 +010097
Jacob Erlbecka1e03732014-12-02 11:28:38 +010098static struct sgsn_subscriber_data *sgsn_subscriber_data_alloc(void *ctx)
99{
100 struct sgsn_subscriber_data *sdata;
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100101 int idx;
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100102
103 sdata = talloc_zero(ctx, struct sgsn_subscriber_data);
104
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100105 sdata->error_cause = SGSN_ERROR_CAUSE_NONE;
106
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100107 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
108 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
109
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100110 INIT_LLIST_HEAD(&sdata->pdp_list);
111
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100112 return sdata;
113}
114
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100115struct sgsn_subscriber_pdp_data* sgsn_subscriber_pdp_data_alloc(
116 struct sgsn_subscriber_data *sdata)
117{
118 struct sgsn_subscriber_pdp_data* pdata;
119
120 pdata = talloc_zero(sdata, struct sgsn_subscriber_pdp_data);
121
122 llist_add_tail(&pdata->list, &sdata->pdp_list);
123
124 return pdata;
125}
126
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100127struct gprs_subscr *gprs_subscr_get_by_imsi(const char *imsi)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100128{
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100129 struct gprs_subscr *gsub;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100130
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100131 if (!imsi || !*imsi)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100132 return NULL;
133
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100134 llist_for_each_entry(gsub, gprs_subscribers, entry) {
135 if (!strcmp(gsub->imsi, imsi))
136 return gprs_subscr_get(gsub);
137 }
138 return NULL;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100139}
140
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100141static struct gprs_subscr *gprs_subscr_alloc(void)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100142{
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100143 struct gprs_subscr *gsub;
144 gsub = talloc_zero(tall_bsc_ctx, struct gprs_subscr);
145 if (!gsub)
146 return NULL;
147 llist_add_tail(&gsub->entry, gprs_subscribers);
148 gsub->use_count = 1;
149 gsub->tmsi = GSM_RESERVED_TMSI;
150 return gsub;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100151}
152
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100153struct gprs_subscr *gprs_subscr_get_or_create(const char *imsi)
154{
155 struct gprs_subscr *gsub;
156
157 gsub = gprs_subscr_get_by_imsi(imsi);
158 if (!gsub) {
159 gsub = gprs_subscr_alloc();
160 if (!gsub)
161 return NULL;
Neels Hofmeyrca06e042017-03-08 18:30:22 +0100162 osmo_strlcpy(gsub->imsi, imsi, sizeof(gsub->imsi));
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100163 }
164
165 if (!gsub->sgsn_data)
166 gsub->sgsn_data = sgsn_subscriber_data_alloc(gsub);
167 return gsub;
168}
169
170void gprs_subscr_cleanup(struct gprs_subscr *subscr)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100171{
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100172 if (subscr->sgsn_data->mm) {
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100173 gprs_subscr_put(subscr->sgsn_data->mm->subscr);
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100174 subscr->sgsn_data->mm->subscr = NULL;
175 subscr->sgsn_data->mm = NULL;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100176 }
177
Holger Hans Peter Freyther1d778fd2015-01-20 21:14:03 +0100178 if (subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE) {
179 gprs_subscr_purge(subscr);
180 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbeck0f47b8f2015-01-06 16:32:41 +0100181 }
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100182}
183
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100184void gprs_subscr_cancel(struct gprs_subscr *subscr)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100185{
186 subscr->authorized = 0;
187 subscr->flags |= GPRS_SUBSCRIBER_CANCELLED;
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100188 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100189
190 gprs_subscr_update(subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100191 gprs_subscr_cleanup(subscr);
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100192}
193
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100194static int gprs_subscr_tx_gsup_message(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200195 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100196{
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100197 struct msgb *msg = gsup_client_msgb_alloc();
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100198
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100199 if (strlen(gsup_msg->imsi) == 0 && subscr)
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100200 osmo_strlcpy(gsup_msg->imsi, subscr->imsi,
201 sizeof(gsup_msg->imsi));
Max896c6f82016-07-01 15:24:44 +0200202 gsup_msg->cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Harald Welte28903a92016-04-25 14:53:43 +0200203 osmo_gsup_encode(msg, gsup_msg);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100204
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100205 LOGGSUBSCRP(LOGL_INFO, subscr,
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100206 "Sending GSUP, will send: %s\n", msgb_hexdump(msg));
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100207
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100208 if (!sgsn->gsup_client) {
209 msgb_free(msg);
210 return -ENOTSUP;
211 }
212
Neels Hofmeyr814fef02016-12-08 21:19:57 +0100213 return gsup_client_send(sgsn->gsup_client, msg);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100214}
215
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100216static int gprs_subscr_tx_gsup_error_reply(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200217 struct osmo_gsup_message *gsup_orig,
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100218 enum gsm48_gmm_cause cause)
219{
Harald Welte28903a92016-04-25 14:53:43 +0200220 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100221
Neels Hofmeyr93bafb62017-01-13 03:12:08 +0100222 osmo_strlcpy(gsup_reply.imsi, gsup_orig->imsi,
223 sizeof(gsup_reply.imsi));
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100224 gsup_reply.cause = cause;
225 gsup_reply.message_type =
Harald Welte28903a92016-04-25 14:53:43 +0200226 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100227
228 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
229}
230
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100231static int gprs_subscr_handle_gsup_auth_res(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200232 struct osmo_gsup_message *gsup_msg)
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100233{
234 unsigned idx;
235 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
236
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100237 LOGGSUBSCRP(LOGL_INFO, subscr,
Harald Welte121e9a42016-04-20 13:13:19 +0200238 "Got SendAuthenticationInfoResult, num_auth_vectors = %zu\n",
239 gsup_msg->num_auth_vectors);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100240
Harald Welte121e9a42016-04-20 13:13:19 +0200241 if (gsup_msg->num_auth_vectors > 0) {
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100242 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
243
244 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
245 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
246 }
247
Harald Welte121e9a42016-04-20 13:13:19 +0200248 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
249 size_t key_seq = idx;
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100250 LOGGSUBSCRP(LOGL_DEBUG, subscr,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400251 "Adding auth tuple, cksn = %zu\n", key_seq);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100252 if (key_seq >= ARRAY_SIZE(sdata->auth_triplets)) {
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100253 LOGGSUBSCRP(LOGL_NOTICE, subscr,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400254 "Skipping auth triplet with invalid cksn %zu\n",
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100255 key_seq);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100256 continue;
257 }
Harald Welte121e9a42016-04-20 13:13:19 +0200258 sdata->auth_triplets[key_seq].vec = gsup_msg->auth_vectors[idx];
259 sdata->auth_triplets[key_seq].key_seq = key_seq;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100260 }
261
262 sdata->auth_triplets_updated = 1;
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100263 sdata->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100264
265 gprs_subscr_update_auth_info(subscr);
266
267 return 0;
268}
269
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100270static int gprs_subscr_pdp_data_clear(struct gprs_subscr *subscr)
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100271{
272 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
273 int count = 0;
274
275 llist_for_each_entry_safe(pdp, pdp2, &subscr->sgsn_data->pdp_list, list) {
276 llist_del(&pdp->list);
277 talloc_free(pdp);
278 count += 1;
279 }
280
281 return count;
282}
283
284static struct sgsn_subscriber_pdp_data *gprs_subscr_pdp_data_get_by_id(
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100285 struct gprs_subscr *subscr, unsigned context_id)
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100286{
287 struct sgsn_subscriber_pdp_data *pdp;
288
289 llist_for_each_entry(pdp, &subscr->sgsn_data->pdp_list, list) {
290 if (pdp->context_id == context_id)
291 return pdp;
292 }
293
294 return NULL;
295}
296
297
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100298static void gprs_subscr_gsup_insert_data(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200299 struct osmo_gsup_message *gsup_msg)
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100300{
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -0400301 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100302 unsigned idx;
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100303 int rc;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100304
Holger Hans Peter Freyther9ba273d2015-04-23 09:53:53 -0400305 if (gsup_msg->msisdn_enc) {
306 if (gsup_msg->msisdn_enc_len > sizeof(sdata->msisdn)) {
307 LOGP(DGPRS, LOGL_ERROR, "MSISDN too long (%zu)\n",
308 gsup_msg->msisdn_enc_len);
309 sdata->msisdn_len = 0;
310 } else {
311 memcpy(sdata->msisdn, gsup_msg->msisdn_enc,
312 gsup_msg->msisdn_enc_len);
313 sdata->msisdn_len = gsup_msg->msisdn_enc_len;
314 }
315 }
316
Holger Hans Peter Freyther10c0f562015-05-17 20:58:40 +0200317 if (gsup_msg->hlr_enc) {
318 if (gsup_msg->hlr_enc_len > sizeof(sdata->hlr)) {
319 LOGP(DGPRS, LOGL_ERROR, "HLR-Number too long (%zu)\n",
320 gsup_msg->hlr_enc_len);
321 sdata->hlr_len = 0;
322 } else {
323 memcpy(sdata->hlr, gsup_msg->hlr_enc,
324 gsup_msg->hlr_enc_len);
325 sdata->hlr_len = gsup_msg->hlr_enc_len;
326 }
327 }
328
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200329 if (gsup_msg->pdp_charg_enc && gsup_msg->pdp_charg_enc_len >= sizeof(sdata->pdp_charg)) {
330 memcpy(&sdata->pdp_charg, gsup_msg->pdp_charg_enc, sizeof(sdata->pdp_charg));
331 sdata->has_pdp_charg = 1;
332 } else {
333 sdata->has_pdp_charg = 0;
334 }
335
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100336 if (gsup_msg->pdp_info_compl) {
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100337 rc = gprs_subscr_pdp_data_clear(subscr);
338 if (rc > 0)
339 LOGP(DGPRS, LOGL_INFO, "Cleared existing PDP info\n");
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100340 }
341
342 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
Harald Welte28903a92016-04-25 14:53:43 +0200343 struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100344 size_t ctx_id = pdp_info->context_id;
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100345 struct sgsn_subscriber_pdp_data *pdp_data;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100346
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100347 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
348 LOGGSUBSCRP(LOGL_ERROR, subscr,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400349 "APN too long, context id = %zu, APN = %s\n",
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100350 ctx_id, osmo_hexdump(pdp_info->apn_enc,
351 pdp_info->apn_enc_len));
352 continue;
353 }
354
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400355 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
356 LOGGSUBSCRP(LOGL_ERROR, subscr,
357 "QoS info too long (%zu)\n",
358 pdp_info->qos_enc_len);
359 continue;
360 }
361
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100362 LOGGSUBSCRP(LOGL_INFO, subscr,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400363 "Will set PDP info, context id = %zu, APN = %s\n",
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100364 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
365
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100366 /* Set PDP info [ctx_id] */
367 pdp_data = gprs_subscr_pdp_data_get_by_id(subscr, ctx_id);
368 if (!pdp_data) {
369 pdp_data = sgsn_subscriber_pdp_data_alloc(subscr->sgsn_data);
370 pdp_data->context_id = ctx_id;
371 }
372
373 OSMO_ASSERT(pdp_data != NULL);
374 pdp_data->pdp_type = pdp_info->pdp_type;
Harald Welte7e82b742017-08-12 13:43:54 +0200375 osmo_apn_to_str(pdp_data->apn_str,
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100376 pdp_info->apn_enc, pdp_info->apn_enc_len);
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400377 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
378 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200379
380 if (pdp_info->pdp_charg_enc && pdp_info->pdp_charg_enc_len >= sizeof(pdp_data->pdp_charg)) {
381 memcpy(&pdp_data->pdp_charg, pdp_info->pdp_charg_enc, sizeof(pdp_data->pdp_charg));
382 pdp_data->has_pdp_charg = 1;
383 } else {
384 pdp_data->has_pdp_charg = 0;
385 }
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100386 }
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100387}
388
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100389static int gprs_subscr_handle_gsup_upd_loc_res(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200390 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100391{
Harald Welte5f2524f2016-05-05 18:25:12 +0200392 /* contrary to MAP, we allow piggy-backing subscriber data onto
393 * the UPDATE LOCATION RESULT, and don't mandate the use of a
394 * separate nested INSERT SUBSCRIBER DATA transaction */
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100395 gprs_subscr_gsup_insert_data(subscr, gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100396
397 subscr->authorized = 1;
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100398 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100399
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100400 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
401
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100402 gprs_subscr_update(subscr);
403 return 0;
404}
405
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100406static int gprs_subscr_handle_gsup_dsd_req(struct gprs_subscr *subscr,
Maxa66d8cf2017-02-15 11:43:59 +0100407 struct osmo_gsup_message *gsup_msg)
408{
409 struct osmo_gsup_message gsup_reply = {0};
410
411 if (gsup_msg->cn_domain != OSMO_GSUP_CN_DOMAIN_PS) {
412 LOGGSUBSCRP(LOGL_ERROR, subscr,
413 "Rx GSUP message %s not supported for CS\n",
414 osmo_gsup_message_type_name(gsup_msg->message_type));
415 gsup_reply.cause = GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
416 gsup_reply.message_type = OSMO_GSUP_MSGT_DELETE_DATA_ERROR;
417 } else {
418 gsm0408_gprs_access_cancelled(subscr->sgsn_data->mm,
419 GMM_CAUSE_GPRS_NOTALLOWED);
420 gsup_reply.message_type = OSMO_GSUP_MSGT_DELETE_DATA_RESULT;
421 }
422
423 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
424}
425
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100426static int gprs_subscr_handle_gsup_isd_req(struct gprs_subscr *subscr,
Harald Welte5f2524f2016-05-05 18:25:12 +0200427 struct osmo_gsup_message *gsup_msg)
428{
429 struct osmo_gsup_message gsup_reply = {0};
430
431 gprs_subscr_gsup_insert_data(subscr, gsup_msg);
432
433 subscr->authorized = 1;
434 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
435 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
436 gprs_subscr_update(subscr);
437
438 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
439 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
440}
441
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100442static int check_cause(int cause)
443{
444 switch (cause) {
445 case GMM_CAUSE_IMSI_UNKNOWN ... GMM_CAUSE_ILLEGAL_ME:
446 case GMM_CAUSE_GPRS_NOTALLOWED ... GMM_CAUSE_NO_GPRS_PLMN:
447 return EACCES;
448
449 case GMM_CAUSE_MSC_TEMP_NOTREACH ... GMM_CAUSE_CONGESTION:
Jacob Erlbeckf06fe292015-01-05 16:20:47 +0100450 return EHOSTUNREACH;
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100451
452 case GMM_CAUSE_SEM_INCORR_MSG ... GMM_CAUSE_PROTO_ERR_UNSPEC:
453 default:
454 return EINVAL;
455 }
456}
457
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100458static int gprs_subscr_handle_gsup_auth_err(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200459 struct osmo_gsup_message *gsup_msg)
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100460{
461 unsigned idx;
462 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100463 int cause_err;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100464
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100465 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100466
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100467 LOGGSUBSCRP(LOGL_DEBUG, subscr,
468 "Send authentication info has failed with cause %d, "
469 "handled as: %s\n",
470 gsup_msg->cause, strerror(cause_err));
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100471
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100472 switch (cause_err) {
473 case EACCES:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100474 LOGGSUBSCRP(LOGL_NOTICE, subscr,
475 "GPRS send auth info req failed, access denied, "
476 "GMM cause = '%s' (%d)\n",
477 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
478 gsup_msg->cause);
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100479 /* Clear auth tuples */
480 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
481 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
482 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100483
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100484 subscr->authorized = 0;
485 sdata->error_cause = gsup_msg->cause;
486 gprs_subscr_update_auth_info(subscr);
487 break;
488
Jacob Erlbeckf06fe292015-01-05 16:20:47 +0100489 case EHOSTUNREACH:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100490 LOGGSUBSCRP(LOGL_NOTICE, subscr,
491 "GPRS send auth info req failed, GMM cause = '%s' (%d)\n",
492 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
493 gsup_msg->cause);
Jacob Erlbeckf06fe292015-01-05 16:20:47 +0100494
495 sdata->error_cause = gsup_msg->cause;
496 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100497 break;
498
499 default:
500 case EINVAL:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100501 LOGGSUBSCRP(LOGL_ERROR, subscr,
502 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
503 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
504 gsup_msg->cause);
Alexander Couzens1cec9c42018-05-22 20:35:55 +0200505 sdata->error_cause = gsup_msg->cause;
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100506 break;
507 }
508
509 return -gsup_msg->cause;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100510}
511
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100512static int gprs_subscr_handle_gsup_upd_loc_err(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200513 struct osmo_gsup_message *gsup_msg)
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100514{
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100515 int cause_err;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100516
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100517 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100518
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100519 LOGGSUBSCRP(LOGL_DEBUG, subscr,
520 "Update location has failed with cause %d, handled as: %s\n",
521 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100522
523 switch (cause_err) {
524 case EACCES:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100525 LOGGSUBSCRP(LOGL_NOTICE, subscr,
526 "GPRS update location failed, access denied, "
527 "GMM cause = '%s' (%d)\n",
528 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
529 gsup_msg->cause);
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100530
531 subscr->authorized = 0;
532 subscr->sgsn_data->error_cause = gsup_msg->cause;
533 gprs_subscr_update_auth_info(subscr);
534 break;
535
Jacob Erlbeckf06fe292015-01-05 16:20:47 +0100536 case EHOSTUNREACH:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100537 LOGGSUBSCRP(LOGL_NOTICE, subscr,
538 "GPRS update location failed, GMM cause = '%s' (%d)\n",
539 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
540 gsup_msg->cause);
Jacob Erlbeckf06fe292015-01-05 16:20:47 +0100541
542 subscr->sgsn_data->error_cause = gsup_msg->cause;
543 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100544 break;
545
546 default:
547 case EINVAL:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100548 LOGGSUBSCRP(LOGL_ERROR, subscr,
549 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
550 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
551 gsup_msg->cause);
Jacob Erlbeck9aa99912015-01-05 18:38:41 +0100552 break;
553 }
554
555 return -gsup_msg->cause;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100556}
557
Jacob Erlbeck929acdf2015-01-27 13:47:24 +0100558static int gprs_subscr_handle_gsup_purge_no_subscr(
Harald Welte28903a92016-04-25 14:53:43 +0200559 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck929acdf2015-01-27 13:47:24 +0100560{
Harald Welte28903a92016-04-25 14:53:43 +0200561 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeck929acdf2015-01-27 13:47:24 +0100562 LOGGSUPP(LOGL_NOTICE, gsup_msg,
563 "Purge MS has failed with cause '%s' (%d)\n",
564 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
565 gsup_msg->cause);
566 return -gsup_msg->cause;
567 }
568
569 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
570 return 0;
571}
572
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100573static int gprs_subscr_handle_gsup_purge_res(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200574 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100575{
576 LOGGSUBSCRP(LOGL_INFO, subscr, "Completing purge MS\n");
577
578 /* Force silent cancellation */
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100579 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100580 gprs_subscr_cancel(subscr);
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100581
582 return 0;
583}
584
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100585static int gprs_subscr_handle_gsup_purge_err(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200586 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100587{
588 LOGGSUBSCRP(LOGL_NOTICE, subscr,
589 "Purge MS has failed with cause '%s' (%d)\n",
590 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
591 gsup_msg->cause);
592
593 /* In GSM 09.02, 19.1.4.4, the text and the SDL diagram imply that
594 * the subscriber data is not removed if the request has failed. On the
595 * other hand, keeping the subscriber data in either error case
596 * (subscriber unknown, syntactical message error, connection error)
597 * doesn't seem to give any advantage, since the data will be restored
598 * on the next Attach Request anyway.
599 * This approach ensures, that the subscriber record will not stick if
600 * an error happens.
601 */
602
603 /* TODO: Check whether this behaviour is acceptable and either just
604 * remove this TODO-notice or change the implementation to not delete
605 * the subscriber data (eventually resetting the ENABLE_PURGE flag and
606 * restarting the expiry timer based on the cause).
607 *
608 * Subscriber Unknown: cancel subscr
609 * Temporary network problems: do nothing (handled by timer based retry)
610 * Message problems (syntax, nyi, ...): cancel subscr (retry won't help)
611 */
612
613 gprs_subscr_handle_gsup_purge_res(subscr, gsup_msg);
614
615 return -gsup_msg->cause;
616}
617
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100618static int gprs_subscr_handle_loc_cancel_req(struct gprs_subscr *subscr,
Harald Welte28903a92016-04-25 14:53:43 +0200619 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck87c7ffc2015-01-08 15:29:01 +0100620{
Harald Welte28903a92016-04-25 14:53:43 +0200621 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbeck5b512052015-04-07 17:49:48 +0200622 int is_update_procedure = !gsup_msg->cancel_type ||
Harald Welte28903a92016-04-25 14:53:43 +0200623 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
Jacob Erlbeck87c7ffc2015-01-08 15:29:01 +0100624
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100625 LOGGSUBSCRP(LOGL_INFO, subscr, "Cancelling MS subscriber (%s)\n",
626 is_update_procedure ?
627 "update procedure" : "subscription withdraw");
Jacob Erlbeck87c7ffc2015-01-08 15:29:01 +0100628
Harald Welte28903a92016-04-25 14:53:43 +0200629 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Jacob Erlbeck87c7ffc2015-01-08 15:29:01 +0100630 gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
631
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100632 if (is_update_procedure)
633 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
634 else
635 /* Since a withdraw cause is not specified, just abort the
636 * current attachment. The following re-attachment should then
637 * be rejected with a proper cause value.
638 */
639 subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
640
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100641 gprs_subscr_cancel(subscr);
Jacob Erlbeck87c7ffc2015-01-08 15:29:01 +0100642
643 return 0;
644}
645
Harald Welte28903a92016-04-25 14:53:43 +0200646static int gprs_subscr_handle_unknown_imsi(struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100647{
Harald Welte28903a92016-04-25 14:53:43 +0200648 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100649 gprs_subscr_tx_gsup_error_reply(NULL, gsup_msg,
650 GMM_CAUSE_IMSI_UNKNOWN);
651 LOGP(DGPRS, LOGL_NOTICE,
652 "Unknown IMSI %s, discarding GSUP request "
653 "of type 0x%02x\n",
654 gsup_msg->imsi, gsup_msg->message_type);
Harald Welte28903a92016-04-25 14:53:43 +0200655 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100656 LOGP(DGPRS, LOGL_NOTICE,
657 "Unknown IMSI %s, discarding GSUP error "
658 "of type 0x%02x, cause '%s' (%d)\n",
659 gsup_msg->imsi, gsup_msg->message_type,
660 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
661 gsup_msg->cause);
662 } else {
663 LOGP(DGPRS, LOGL_NOTICE,
664 "Unknown IMSI %s, discarding GSUP response "
665 "of type 0x%02x\n",
666 gsup_msg->imsi, gsup_msg->message_type);
667 }
668
669 return -GMM_CAUSE_IMSI_UNKNOWN;
670}
671
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100672int gprs_subscr_rx_gsup_message(struct msgb *msg)
673{
674 uint8_t *data = msgb_l2(msg);
675 size_t data_len = msgb_l2len(msg);
676 int rc = 0;
677
Harald Welte28903a92016-04-25 14:53:43 +0200678 struct osmo_gsup_message gsup_msg = {0};
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100679 struct gprs_subscr *subscr;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100680
Harald Welte28903a92016-04-25 14:53:43 +0200681 rc = osmo_gsup_decode(data, data_len, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100682 if (rc < 0) {
683 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100684 "decoding GSUP message fails with error '%s' (%d)\n",
685 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100686 return rc;
687 }
688
Jacob Erlbeck07f6e362015-01-29 14:00:28 +0100689 if (!gsup_msg.imsi[0]) {
690 LOGP(DGPRS, LOGL_ERROR, "Missing IMSI in GSUP message\n");
691
Harald Welte28903a92016-04-25 14:53:43 +0200692 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck07f6e362015-01-29 14:00:28 +0100693 gprs_subscr_tx_gsup_error_reply(NULL, &gsup_msg,
694 GMM_CAUSE_INV_MAND_INFO);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100695 return -GMM_CAUSE_INV_MAND_INFO;
Jacob Erlbeck07f6e362015-01-29 14:00:28 +0100696 }
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100697
Harald Welte28903a92016-04-25 14:53:43 +0200698 if (!gsup_msg.cause && OSMO_GSUP_IS_MSGT_ERROR(gsup_msg.message_type))
Jacob Erlbeck9ff82892015-01-29 14:17:51 +0100699 gsup_msg.cause = GMM_CAUSE_NET_FAIL;
700
Jacob Erlbeck4dedb272015-01-15 17:50:16 +0100701 subscr = gprs_subscr_get_by_imsi(gsup_msg.imsi);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100702
Jacob Erlbeck929acdf2015-01-27 13:47:24 +0100703 if (!subscr) {
704 switch (gsup_msg.message_type) {
Harald Welte28903a92016-04-25 14:53:43 +0200705 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
706 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeck929acdf2015-01-27 13:47:24 +0100707 return gprs_subscr_handle_gsup_purge_no_subscr(&gsup_msg);
708 default:
709 return gprs_subscr_handle_unknown_imsi(&gsup_msg);
710 }
711 }
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100712
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100713 LOGGSUBSCRP(LOGL_INFO, subscr,
Maxa66d8cf2017-02-15 11:43:59 +0100714 "Received GSUP message %s\n",
715 osmo_gsup_message_type_name(gsup_msg.message_type));
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100716
717 switch (gsup_msg.message_type) {
Harald Welte28903a92016-04-25 14:53:43 +0200718 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Jacob Erlbeck87c7ffc2015-01-08 15:29:01 +0100719 rc = gprs_subscr_handle_loc_cancel_req(subscr, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100720 break;
721
Harald Welte28903a92016-04-25 14:53:43 +0200722 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100723 rc = gprs_subscr_handle_gsup_auth_res(subscr, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100724 break;
725
Harald Welte28903a92016-04-25 14:53:43 +0200726 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100727 rc = gprs_subscr_handle_gsup_auth_err(subscr, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100728 break;
729
Harald Welte28903a92016-04-25 14:53:43 +0200730 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100731 rc = gprs_subscr_handle_gsup_upd_loc_res(subscr, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100732 break;
733
Harald Welte28903a92016-04-25 14:53:43 +0200734 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100735 rc = gprs_subscr_handle_gsup_upd_loc_err(subscr, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100736 break;
737
Harald Welte28903a92016-04-25 14:53:43 +0200738 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100739 rc = gprs_subscr_handle_gsup_purge_err(subscr, &gsup_msg);
740 break;
741
Harald Welte28903a92016-04-25 14:53:43 +0200742 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100743 rc = gprs_subscr_handle_gsup_purge_res(subscr, &gsup_msg);
744 break;
745
Harald Welte28903a92016-04-25 14:53:43 +0200746 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Welte5f2524f2016-05-05 18:25:12 +0200747 rc = gprs_subscr_handle_gsup_isd_req(subscr, &gsup_msg);
748 break;
749
Harald Welte28903a92016-04-25 14:53:43 +0200750 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Maxa66d8cf2017-02-15 11:43:59 +0100751 rc = gprs_subscr_handle_gsup_dsd_req(subscr, &gsup_msg);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100752 break;
753
754 default:
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100755 LOGGSUBSCRP(LOGL_ERROR, subscr,
Maxa66d8cf2017-02-15 11:43:59 +0100756 "Rx GSUP message %s not valid at SGSN\n",
757 osmo_gsup_message_type_name(gsup_msg.message_type));
Harald Welte28903a92016-04-25 14:53:43 +0200758 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck9999fd92015-01-15 17:08:30 +0100759 gprs_subscr_tx_gsup_error_reply(
760 subscr, &gsup_msg, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
761 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100762 break;
763 };
764
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100765 gprs_subscr_put(subscr);
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100766
767 return rc;
768}
769
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100770int gprs_subscr_purge(struct gprs_subscr *subscr)
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100771{
Holger Hans Peter Freyther10c0f562015-05-17 20:58:40 +0200772 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Harald Welte28903a92016-04-25 14:53:43 +0200773 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100774
775 LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n");
776
Harald Welte28903a92016-04-25 14:53:43 +0200777 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
Holger Hans Peter Freyther10c0f562015-05-17 20:58:40 +0200778
779 /* Provide the HLR number in case it is known */
780 gsup_msg.hlr_enc_len = sdata->hlr_len;
781 gsup_msg.hlr_enc = sdata->hlr;
782
Jacob Erlbeckca69b0f2015-02-03 19:45:46 +0100783 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100784}
785
Neels Hofmeyr058cd572017-02-24 06:24:45 +0100786static int gprs_subscr_query_auth_info(struct gprs_subscr *subscr,
787 const uint8_t *auts,
788 const uint8_t *auts_rand)
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100789{
Harald Welte28903a92016-04-25 14:53:43 +0200790 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100791
Neels Hofmeyr058cd572017-02-24 06:24:45 +0100792 /* Make sure we have a complete resync or clearly no resync. */
793 OSMO_ASSERT((auts != NULL) == (auts_rand != NULL));
794
795 LOGGSUBSCRP(LOGL_INFO, subscr, "requesting auth info%s\n",
796 auts ? " with AUTS (UMTS Resynch)" : "");
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100797
Harald Welte28903a92016-04-25 14:53:43 +0200798 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
Neels Hofmeyr058cd572017-02-24 06:24:45 +0100799 gsup_msg.auts = auts;
800 gsup_msg.rand = auts_rand;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100801 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
802}
803
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100804int gprs_subscr_location_update(struct gprs_subscr *subscr)
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100805{
Harald Welte28903a92016-04-25 14:53:43 +0200806 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100807
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100808 LOGGSUBSCRP(LOGL_INFO, subscr,
809 "subscriber data is not available\n");
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100810
Harald Welte28903a92016-04-25 14:53:43 +0200811 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Jacob Erlbecka6ddc2d2014-12-12 15:01:37 +0100812 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100813}
814
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100815void gprs_subscr_update(struct gprs_subscr *subscr)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100816{
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100817 LOGGSUBSCRP(LOGL_DEBUG, subscr, "Updating subscriber data\n");
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100818
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100819 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100820 subscr->flags &= ~GPRS_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100821
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100822 if (subscr->sgsn_data->mm)
823 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100824}
825
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100826void gprs_subscr_update_auth_info(struct gprs_subscr *subscr)
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100827{
Jacob Erlbeckbf34c672014-12-23 14:24:16 +0100828 LOGGSUBSCRP(LOGL_DEBUG, subscr,
829 "Updating subscriber authentication info\n");
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100830
831 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100832 subscr->flags &= ~GPRS_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100833
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100834 if (subscr->sgsn_data->mm)
835 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100836}
837
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100838struct gprs_subscr *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100839{
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100840 struct gprs_subscr *subscr = NULL;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100841
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100842 if (mmctx->subscr)
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100843 return gprs_subscr_get(mmctx->subscr);
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100844
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100845 if (mmctx->imsi[0])
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100846 subscr = gprs_subscr_get_by_imsi(mmctx->imsi);
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100847
848 if (!subscr) {
849 subscr = gprs_subscr_get_or_create(mmctx->imsi);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100850 subscr->flags |= GPRS_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100851 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100852 }
853
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100854 osmo_strlcpy(subscr->imei, mmctx->imei, sizeof(subscr->imei));
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100855
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100856 if (subscr->lac != mmctx->ra.lac)
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100857 subscr->lac = mmctx->ra.lac;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100858
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100859 subscr->sgsn_data->mm = mmctx;
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100860 mmctx->subscr = gprs_subscr_get(subscr);
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100861
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100862 return subscr;
863}
864
865int gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx)
866{
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100867 struct gprs_subscr *subscr = NULL;
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100868 int rc;
869
870 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber data update\n");
871
872 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
873
874 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
875
876 rc = gprs_subscr_location_update(subscr);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100877 gprs_subscr_put(subscr);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100878 return rc;
879}
880
Neels Hofmeyr058cd572017-02-24 06:24:45 +0100881/*! \brief Send Update Auth Info request via GSUP, with or without resync.
882 * \param[in] mmctx MM context to request authentication tuples for.
883 * \param[in] auts 14 octet AUTS token for UMTS resync, or NULL.
884 * \param[in] auts_rand 16 octet Random token for UMTS resync, or NULL.
885 * In case of normal Authentication Info request, both \a auts and \a auts_rand
886 * must be NULL. For resync, both must be non-NULL.
887 */
888int gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx,
889 const uint8_t *auts,
890 const uint8_t *auts_rand)
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100891{
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100892 struct gprs_subscr *subscr = NULL;
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100893 int rc;
894
895 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber authentication info\n");
896
897 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
898
899 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
900
Neels Hofmeyr058cd572017-02-24 06:24:45 +0100901 rc = gprs_subscr_query_auth_info(subscr, auts, auts_rand);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100902 gprs_subscr_put(subscr);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100903 return rc;
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100904}
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100905
906static void gprs_subscr_free(struct gprs_subscr *gsub)
907{
908 llist_del(&gsub->entry);
909 talloc_free(gsub);
910}
911
912struct gprs_subscr *_gprs_subscr_get(struct gprs_subscr *gsub,
913 const char *file, int line)
914{
915 OSMO_ASSERT(gsub->use_count < INT_MAX);
916 gsub->use_count++;
917 LOGPSRC(DREF, LOGL_DEBUG, file, line,
918 "subscr %s usage increases to: %d\n",
919 gsub->imsi, gsub->use_count);
920 return gsub;
921}
922
923struct gprs_subscr *_gprs_subscr_put(struct gprs_subscr *gsub,
924 const char *file, int line)
925{
926 gsub->use_count--;
927 LOGPSRC(DREF, gsub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR,
928 file, line,
929 "subscr %s usage decreases to: %d%s\n",
930 gsub->imsi, gsub->use_count,
931 gsub->keep_in_ram? ", keep-in-ram flag is set" : "");
932 if (gsub->use_count > 0)
933 return gsub;
934 if (gsub->keep_in_ram)
935 return gsub;
936 gprs_subscr_free(gsub);
937 return NULL;
938}