blob: 57f73c5da8f4ab3e5dd5187de3d7c3faa07f506c [file] [log] [blame]
Jacob Erlbecke8b69682014-11-12 10:12:11 +01001/* MS subscriber data handling */
2
3/* (C) 2014 by sysmocom s.f.m.c. GmbH
Holger Hans Peter Freyther786cfee2015-04-23 09:53:53 -04004 * (C) 2015 by Holger Hans Peter Freyther
Jacob Erlbecke8b69682014-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 Welte35ade5e2016-04-20 17:11:43 +020023#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welte4e349d62016-04-25 19:07:34 +020024#include <osmocom/gsm/gsup.h>
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +010025#include <osmocom/core/utils.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010026#include <openbsc/gsm_subscriber.h>
Neels Hofmeyrdb1d8012016-12-08 21:22:58 +010027#include <openbsc/gsup_client.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010028
29#include <openbsc/sgsn.h>
30#include <openbsc/gprs_sgsn.h>
31#include <openbsc/gprs_gmm.h>
Jacob Erlbeck94a346a2014-12-17 14:03:35 +010032#include <openbsc/gprs_utils.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010033
34#include <openbsc/debug.h>
35
Jacob Erlbeck233715c2014-12-18 12:46:47 +010036#include <netinet/in.h>
37#include <arpa/inet.h>
38
Jacob Erlbeck87972662015-01-08 15:18:39 +010039#define SGSN_SUBSCR_MAX_RETRIES 3
40#define SGSN_SUBSCR_RETRY_INTERVAL 10
41
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +010042#define LOGGSUPP(level, gsup, fmt, args...) \
43 LOGP(DGPRS, level, "GSUP(%s) " fmt, \
44 (gsup)->imsi, \
45 ## args)
46
Jacob Erlbecke8b69682014-11-12 10:12:11 +010047extern void *tall_bsc_ctx;
48
Neels Hofmeyr0e5c1e12016-12-08 21:19:57 +010049static int gsup_read_cb(struct gsup_client *gsupc, struct msgb *msg);
Jacob Erlbeck233715c2014-12-18 12:46:47 +010050
51/* TODO: Some functions are specific to the SGSN, but this file is more general
52 * (it has gprs_* name). Either move these functions elsewhere, split them and
53 * move a part, or replace the gprs_ prefix by sgsn_. The applies to
54 * gprs_subscr_init, gsup_read_cb, and gprs_subscr_tx_gsup_message.
55 */
56
57int gprs_subscr_init(struct sgsn_instance *sgi)
Jacob Erlbecke8b69682014-11-12 10:12:11 +010058{
Jacob Erlbeck233715c2014-12-18 12:46:47 +010059 const char *addr_str;
60
61 if (!sgi->cfg.gsup_server_addr.sin_addr.s_addr)
62 return 0;
63
64 addr_str = inet_ntoa(sgi->cfg.gsup_server_addr.sin_addr);
65
Neels Hofmeyr0e5c1e12016-12-08 21:19:57 +010066 sgi->gsup_client = gsup_client_create(
Jacob Erlbeck233715c2014-12-18 12:46:47 +010067 addr_str, sgi->cfg.gsup_server_port,
Neels Hofmeyr38b5e272015-10-12 11:57:37 +020068 &gsup_read_cb,
69 &sgi->cfg.oap);
Jacob Erlbeck233715c2014-12-18 12:46:47 +010070
71 if (!sgi->gsup_client)
72 return -1;
73
74 return 1;
75}
76
Neels Hofmeyr0e5c1e12016-12-08 21:19:57 +010077static int gsup_read_cb(struct gsup_client *gsupc, struct msgb *msg)
Jacob Erlbeck233715c2014-12-18 12:46:47 +010078{
79 int rc;
80
81 rc = gprs_subscr_rx_gsup_message(msg);
Jacob Erlbeckedb67a12014-12-19 19:15:55 +010082 msgb_free(msg);
Jacob Erlbeck233715c2014-12-18 12:46:47 +010083 if (rc < 0)
84 return -1;
85
86 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +010087}
88
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +010089int gprs_subscr_purge(struct gsm_subscriber *subscr);
90
Jacob Erlbeck359cafa2014-12-02 11:28:38 +010091static struct sgsn_subscriber_data *sgsn_subscriber_data_alloc(void *ctx)
92{
93 struct sgsn_subscriber_data *sdata;
Jacob Erlbeckb1332b62014-12-08 15:52:00 +010094 int idx;
Jacob Erlbeck359cafa2014-12-02 11:28:38 +010095
96 sdata = talloc_zero(ctx, struct sgsn_subscriber_data);
97
Jacob Erlbeckf96779f2015-01-19 11:10:04 +010098 sdata->error_cause = SGSN_ERROR_CAUSE_NONE;
99
Jacob Erlbeckb1332b62014-12-08 15:52:00 +0100100 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
101 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
102
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100103 INIT_LLIST_HEAD(&sdata->pdp_list);
104
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100105 return sdata;
106}
107
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100108struct sgsn_subscriber_pdp_data* sgsn_subscriber_pdp_data_alloc(
109 struct sgsn_subscriber_data *sdata)
110{
111 struct sgsn_subscriber_pdp_data* pdata;
112
113 pdata = talloc_zero(sdata, struct sgsn_subscriber_pdp_data);
114
115 llist_add_tail(&pdata->list, &sdata->pdp_list);
116
117 return pdata;
118}
119
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100120struct gsm_subscriber *gprs_subscr_get_or_create(const char *imsi)
121{
122 struct gsm_subscriber *subscr;
123
124 subscr = subscr_get_or_create(NULL, imsi);
125 if (!subscr)
126 return NULL;
127
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100128 if (!subscr->sgsn_data)
129 subscr->sgsn_data = sgsn_subscriber_data_alloc(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100130 return subscr;
131}
132
133struct gsm_subscriber *gprs_subscr_get_by_imsi(const char *imsi)
134{
135 return subscr_active_by_imsi(NULL, imsi);
136}
137
Jacob Erlbecke71ab2f2015-01-26 11:07:24 +0100138void gprs_subscr_cleanup(struct gsm_subscriber *subscr)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100139{
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100140 if (subscr->sgsn_data->mm) {
141 subscr_put(subscr->sgsn_data->mm->subscr);
142 subscr->sgsn_data->mm->subscr = NULL;
143 subscr->sgsn_data->mm = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100144 }
145
Holger Hans Peter Freytherd95cb732015-01-20 21:14:03 +0100146 if (subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE) {
147 gprs_subscr_purge(subscr);
148 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100149 }
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100150}
151
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100152void gprs_subscr_cancel(struct gsm_subscriber *subscr)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100153{
154 subscr->authorized = 0;
155 subscr->flags |= GPRS_SUBSCRIBER_CANCELLED;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100156 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100157
158 gprs_subscr_update(subscr);
Jacob Erlbecke71ab2f2015-01-26 11:07:24 +0100159 gprs_subscr_cleanup(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100160}
161
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100162static int gprs_subscr_tx_gsup_message(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200163 struct osmo_gsup_message *gsup_msg)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100164{
Neels Hofmeyr0e5c1e12016-12-08 21:19:57 +0100165 struct msgb *msg = gsup_client_msgb_alloc();
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100166
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100167 if (strlen(gsup_msg->imsi) == 0 && subscr)
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +0100168 osmo_strlcpy(gsup_msg->imsi, subscr->imsi,
169 sizeof(gsup_msg->imsi));
Max92d8d0a2016-07-01 15:24:44 +0200170 gsup_msg->cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Harald Weltee2b53492016-04-25 14:53:43 +0200171 osmo_gsup_encode(msg, gsup_msg);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100172
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100173 LOGGSUBSCRP(LOGL_INFO, subscr,
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100174 "Sending GSUP, will send: %s\n", msgb_hexdump(msg));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100175
Jacob Erlbeck233715c2014-12-18 12:46:47 +0100176 if (!sgsn->gsup_client) {
177 msgb_free(msg);
178 return -ENOTSUP;
179 }
180
Neels Hofmeyr0e5c1e12016-12-08 21:19:57 +0100181 return gsup_client_send(sgsn->gsup_client, msg);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100182}
183
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100184static int gprs_subscr_tx_gsup_error_reply(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200185 struct osmo_gsup_message *gsup_orig,
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100186 enum gsm48_gmm_cause cause)
187{
Harald Weltee2b53492016-04-25 14:53:43 +0200188 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100189
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +0100190 osmo_strlcpy(gsup_reply.imsi, gsup_orig->imsi,
191 sizeof(gsup_reply.imsi));
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100192 gsup_reply.cause = cause;
193 gsup_reply.message_type =
Harald Weltee2b53492016-04-25 14:53:43 +0200194 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100195
196 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
197}
198
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100199static int gprs_subscr_handle_gsup_auth_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200200 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100201{
202 unsigned idx;
203 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
204
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100205 LOGGSUBSCRP(LOGL_INFO, subscr,
Harald Welte34ef4c52016-04-20 13:13:19 +0200206 "Got SendAuthenticationInfoResult, num_auth_vectors = %zu\n",
207 gsup_msg->num_auth_vectors);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100208
Harald Welte34ef4c52016-04-20 13:13:19 +0200209 if (gsup_msg->num_auth_vectors > 0) {
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100210 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
211
212 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
213 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
214 }
215
Harald Welte34ef4c52016-04-20 13:13:19 +0200216 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
217 size_t key_seq = idx;
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100218 LOGGSUBSCRP(LOGL_DEBUG, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400219 "Adding auth tuple, cksn = %zu\n", key_seq);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100220 if (key_seq >= ARRAY_SIZE(sdata->auth_triplets)) {
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100221 LOGGSUBSCRP(LOGL_NOTICE, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400222 "Skipping auth triplet with invalid cksn %zu\n",
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100223 key_seq);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100224 continue;
225 }
Harald Welte34ef4c52016-04-20 13:13:19 +0200226 sdata->auth_triplets[key_seq].vec = gsup_msg->auth_vectors[idx];
227 sdata->auth_triplets[key_seq].key_seq = key_seq;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100228 }
229
230 sdata->auth_triplets_updated = 1;
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100231 sdata->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100232
233 gprs_subscr_update_auth_info(subscr);
234
235 return 0;
236}
237
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100238static int gprs_subscr_pdp_data_clear(struct gsm_subscriber *subscr)
239{
240 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
241 int count = 0;
242
243 llist_for_each_entry_safe(pdp, pdp2, &subscr->sgsn_data->pdp_list, list) {
244 llist_del(&pdp->list);
245 talloc_free(pdp);
246 count += 1;
247 }
248
249 return count;
250}
251
252static struct sgsn_subscriber_pdp_data *gprs_subscr_pdp_data_get_by_id(
253 struct gsm_subscriber *subscr, unsigned context_id)
254{
255 struct sgsn_subscriber_pdp_data *pdp;
256
257 llist_for_each_entry(pdp, &subscr->sgsn_data->pdp_list, list) {
258 if (pdp->context_id == context_id)
259 return pdp;
260 }
261
262 return NULL;
263}
264
265
266static void gprs_subscr_gsup_insert_data(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200267 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100268{
Holger Hans Peter Freyther786cfee2015-04-23 09:53:53 -0400269 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100270 unsigned idx;
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100271 int rc;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100272
Holger Hans Peter Freyther786cfee2015-04-23 09:53:53 -0400273 if (gsup_msg->msisdn_enc) {
274 if (gsup_msg->msisdn_enc_len > sizeof(sdata->msisdn)) {
275 LOGP(DGPRS, LOGL_ERROR, "MSISDN too long (%zu)\n",
276 gsup_msg->msisdn_enc_len);
277 sdata->msisdn_len = 0;
278 } else {
279 memcpy(sdata->msisdn, gsup_msg->msisdn_enc,
280 gsup_msg->msisdn_enc_len);
281 sdata->msisdn_len = gsup_msg->msisdn_enc_len;
282 }
283 }
284
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200285 if (gsup_msg->hlr_enc) {
286 if (gsup_msg->hlr_enc_len > sizeof(sdata->hlr)) {
287 LOGP(DGPRS, LOGL_ERROR, "HLR-Number too long (%zu)\n",
288 gsup_msg->hlr_enc_len);
289 sdata->hlr_len = 0;
290 } else {
291 memcpy(sdata->hlr, gsup_msg->hlr_enc,
292 gsup_msg->hlr_enc_len);
293 sdata->hlr_len = gsup_msg->hlr_enc_len;
294 }
295 }
296
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100297 if (gsup_msg->pdp_info_compl) {
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100298 rc = gprs_subscr_pdp_data_clear(subscr);
299 if (rc > 0)
300 LOGP(DGPRS, LOGL_INFO, "Cleared existing PDP info\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100301 }
302
303 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
Harald Weltee2b53492016-04-25 14:53:43 +0200304 struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100305 size_t ctx_id = pdp_info->context_id;
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100306 struct sgsn_subscriber_pdp_data *pdp_data;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100307
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100308 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
309 LOGGSUBSCRP(LOGL_ERROR, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400310 "APN too long, context id = %zu, APN = %s\n",
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100311 ctx_id, osmo_hexdump(pdp_info->apn_enc,
312 pdp_info->apn_enc_len));
313 continue;
314 }
315
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400316 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
317 LOGGSUBSCRP(LOGL_ERROR, subscr,
318 "QoS info too long (%zu)\n",
319 pdp_info->qos_enc_len);
320 continue;
321 }
322
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100323 LOGGSUBSCRP(LOGL_INFO, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400324 "Will set PDP info, context id = %zu, APN = %s\n",
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100325 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
326
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100327 /* Set PDP info [ctx_id] */
328 pdp_data = gprs_subscr_pdp_data_get_by_id(subscr, ctx_id);
329 if (!pdp_data) {
330 pdp_data = sgsn_subscriber_pdp_data_alloc(subscr->sgsn_data);
331 pdp_data->context_id = ctx_id;
332 }
333
334 OSMO_ASSERT(pdp_data != NULL);
335 pdp_data->pdp_type = pdp_info->pdp_type;
336 gprs_apn_to_str(pdp_data->apn_str,
337 pdp_info->apn_enc, pdp_info->apn_enc_len);
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400338 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
339 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100340 }
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100341}
342
343static int gprs_subscr_handle_gsup_upd_loc_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200344 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100345{
Harald Weltec21455b2016-05-05 18:25:12 +0200346 /* contrary to MAP, we allow piggy-backing subscriber data onto
347 * the UPDATE LOCATION RESULT, and don't mandate the use of a
348 * separate nested INSERT SUBSCRIBER DATA transaction */
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100349 gprs_subscr_gsup_insert_data(subscr, gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100350
351 subscr->authorized = 1;
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100352 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100353
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100354 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
355
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100356 gprs_subscr_update(subscr);
357 return 0;
358}
359
Maxe405f152017-02-15 11:43:59 +0100360static int gprs_subscr_handle_gsup_dsd_req(struct gsm_subscriber *subscr,
361 struct osmo_gsup_message *gsup_msg)
362{
363 struct osmo_gsup_message gsup_reply = {0};
364
365 if (gsup_msg->cn_domain != OSMO_GSUP_CN_DOMAIN_PS) {
366 LOGGSUBSCRP(LOGL_ERROR, subscr,
367 "Rx GSUP message %s not supported for CS\n",
368 osmo_gsup_message_type_name(gsup_msg->message_type));
369 gsup_reply.cause = GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
370 gsup_reply.message_type = OSMO_GSUP_MSGT_DELETE_DATA_ERROR;
371 } else {
372 gsm0408_gprs_access_cancelled(subscr->sgsn_data->mm,
373 GMM_CAUSE_GPRS_NOTALLOWED);
374 gsup_reply.message_type = OSMO_GSUP_MSGT_DELETE_DATA_RESULT;
375 }
376
377 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
378}
379
Harald Weltec21455b2016-05-05 18:25:12 +0200380static int gprs_subscr_handle_gsup_isd_req(struct gsm_subscriber *subscr,
381 struct osmo_gsup_message *gsup_msg)
382{
383 struct osmo_gsup_message gsup_reply = {0};
384
385 gprs_subscr_gsup_insert_data(subscr, gsup_msg);
386
387 subscr->authorized = 1;
388 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
389 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
390 gprs_subscr_update(subscr);
391
392 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
393 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
394}
395
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100396static int check_cause(int cause)
397{
398 switch (cause) {
399 case GMM_CAUSE_IMSI_UNKNOWN ... GMM_CAUSE_ILLEGAL_ME:
400 case GMM_CAUSE_GPRS_NOTALLOWED ... GMM_CAUSE_NO_GPRS_PLMN:
401 return EACCES;
402
403 case GMM_CAUSE_MSC_TEMP_NOTREACH ... GMM_CAUSE_CONGESTION:
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100404 return EHOSTUNREACH;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100405
406 case GMM_CAUSE_SEM_INCORR_MSG ... GMM_CAUSE_PROTO_ERR_UNSPEC:
407 default:
408 return EINVAL;
409 }
410}
411
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100412static int gprs_subscr_handle_gsup_auth_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200413 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100414{
415 unsigned idx;
416 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100417 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100418
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100419 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100420
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100421 LOGGSUBSCRP(LOGL_DEBUG, subscr,
422 "Send authentication info has failed with cause %d, "
423 "handled as: %s\n",
424 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100425
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100426 switch (cause_err) {
427 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100428 LOGGSUBSCRP(LOGL_NOTICE, subscr,
429 "GPRS send auth info req failed, access denied, "
430 "GMM cause = '%s' (%d)\n",
431 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
432 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100433 /* Clear auth tuples */
434 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
435 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
436 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100437
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100438 subscr->authorized = 0;
439 sdata->error_cause = gsup_msg->cause;
440 gprs_subscr_update_auth_info(subscr);
441 break;
442
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100443 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100444 LOGGSUBSCRP(LOGL_NOTICE, subscr,
445 "GPRS send auth info req failed, GMM cause = '%s' (%d)\n",
446 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
447 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100448
449 sdata->error_cause = gsup_msg->cause;
450 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100451 break;
452
453 default:
454 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100455 LOGGSUBSCRP(LOGL_ERROR, subscr,
456 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
457 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
458 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100459 break;
460 }
461
462 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100463}
464
465static int gprs_subscr_handle_gsup_upd_loc_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200466 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100467{
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100468 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100469
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100470 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100471
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100472 LOGGSUBSCRP(LOGL_DEBUG, subscr,
473 "Update location has failed with cause %d, handled as: %s\n",
474 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100475
476 switch (cause_err) {
477 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100478 LOGGSUBSCRP(LOGL_NOTICE, subscr,
479 "GPRS update location failed, access denied, "
480 "GMM cause = '%s' (%d)\n",
481 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
482 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100483
484 subscr->authorized = 0;
485 subscr->sgsn_data->error_cause = gsup_msg->cause;
486 gprs_subscr_update_auth_info(subscr);
487 break;
488
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100489 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100490 LOGGSUBSCRP(LOGL_NOTICE, subscr,
491 "GPRS update location failed, GMM cause = '%s' (%d)\n",
492 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
493 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100494
495 subscr->sgsn_data->error_cause = gsup_msg->cause;
496 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100497 break;
498
499 default:
500 case EINVAL:
Jacob Erlbeck387d6d92014-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);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100505 break;
506 }
507
508 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100509}
510
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100511static int gprs_subscr_handle_gsup_purge_no_subscr(
Harald Weltee2b53492016-04-25 14:53:43 +0200512 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100513{
Harald Weltee2b53492016-04-25 14:53:43 +0200514 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100515 LOGGSUPP(LOGL_NOTICE, gsup_msg,
516 "Purge MS has failed with cause '%s' (%d)\n",
517 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
518 gsup_msg->cause);
519 return -gsup_msg->cause;
520 }
521
522 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
523 return 0;
524}
525
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100526static int gprs_subscr_handle_gsup_purge_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200527 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100528{
529 LOGGSUBSCRP(LOGL_INFO, subscr, "Completing purge MS\n");
530
531 /* Force silent cancellation */
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100532 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100533 gprs_subscr_cancel(subscr);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100534
535 return 0;
536}
537
538static int gprs_subscr_handle_gsup_purge_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200539 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100540{
541 LOGGSUBSCRP(LOGL_NOTICE, subscr,
542 "Purge MS has failed with cause '%s' (%d)\n",
543 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
544 gsup_msg->cause);
545
546 /* In GSM 09.02, 19.1.4.4, the text and the SDL diagram imply that
547 * the subscriber data is not removed if the request has failed. On the
548 * other hand, keeping the subscriber data in either error case
549 * (subscriber unknown, syntactical message error, connection error)
550 * doesn't seem to give any advantage, since the data will be restored
551 * on the next Attach Request anyway.
552 * This approach ensures, that the subscriber record will not stick if
553 * an error happens.
554 */
555
556 /* TODO: Check whether this behaviour is acceptable and either just
557 * remove this TODO-notice or change the implementation to not delete
558 * the subscriber data (eventually resetting the ENABLE_PURGE flag and
559 * restarting the expiry timer based on the cause).
560 *
561 * Subscriber Unknown: cancel subscr
562 * Temporary network problems: do nothing (handled by timer based retry)
563 * Message problems (syntax, nyi, ...): cancel subscr (retry won't help)
564 */
565
566 gprs_subscr_handle_gsup_purge_res(subscr, gsup_msg);
567
568 return -gsup_msg->cause;
569}
570
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100571static int gprs_subscr_handle_loc_cancel_req(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200572 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100573{
Harald Weltee2b53492016-04-25 14:53:43 +0200574 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbecke7fea452015-04-07 17:49:48 +0200575 int is_update_procedure = !gsup_msg->cancel_type ||
Harald Weltee2b53492016-04-25 14:53:43 +0200576 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100577
Jacob Erlbeck3b0d0c02015-01-27 14:56:40 +0100578 LOGGSUBSCRP(LOGL_INFO, subscr, "Cancelling MS subscriber (%s)\n",
579 is_update_procedure ?
580 "update procedure" : "subscription withdraw");
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100581
Harald Weltee2b53492016-04-25 14:53:43 +0200582 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100583 gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
584
Jacob Erlbeck3b0d0c02015-01-27 14:56:40 +0100585 if (is_update_procedure)
586 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
587 else
588 /* Since a withdraw cause is not specified, just abort the
589 * current attachment. The following re-attachment should then
590 * be rejected with a proper cause value.
591 */
592 subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
593
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100594 gprs_subscr_cancel(subscr);
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100595
596 return 0;
597}
598
Harald Weltee2b53492016-04-25 14:53:43 +0200599static int gprs_subscr_handle_unknown_imsi(struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100600{
Harald Weltee2b53492016-04-25 14:53:43 +0200601 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100602 gprs_subscr_tx_gsup_error_reply(NULL, gsup_msg,
603 GMM_CAUSE_IMSI_UNKNOWN);
604 LOGP(DGPRS, LOGL_NOTICE,
605 "Unknown IMSI %s, discarding GSUP request "
606 "of type 0x%02x\n",
607 gsup_msg->imsi, gsup_msg->message_type);
Harald Weltee2b53492016-04-25 14:53:43 +0200608 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100609 LOGP(DGPRS, LOGL_NOTICE,
610 "Unknown IMSI %s, discarding GSUP error "
611 "of type 0x%02x, cause '%s' (%d)\n",
612 gsup_msg->imsi, gsup_msg->message_type,
613 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
614 gsup_msg->cause);
615 } else {
616 LOGP(DGPRS, LOGL_NOTICE,
617 "Unknown IMSI %s, discarding GSUP response "
618 "of type 0x%02x\n",
619 gsup_msg->imsi, gsup_msg->message_type);
620 }
621
622 return -GMM_CAUSE_IMSI_UNKNOWN;
623}
624
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100625int gprs_subscr_rx_gsup_message(struct msgb *msg)
626{
627 uint8_t *data = msgb_l2(msg);
628 size_t data_len = msgb_l2len(msg);
629 int rc = 0;
630
Harald Weltee2b53492016-04-25 14:53:43 +0200631 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100632 struct gsm_subscriber *subscr;
633
Harald Weltee2b53492016-04-25 14:53:43 +0200634 rc = osmo_gsup_decode(data, data_len, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100635 if (rc < 0) {
636 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100637 "decoding GSUP message fails with error '%s' (%d)\n",
638 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100639 return rc;
640 }
641
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100642 if (!gsup_msg.imsi[0]) {
643 LOGP(DGPRS, LOGL_ERROR, "Missing IMSI in GSUP message\n");
644
Harald Weltee2b53492016-04-25 14:53:43 +0200645 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100646 gprs_subscr_tx_gsup_error_reply(NULL, &gsup_msg,
647 GMM_CAUSE_INV_MAND_INFO);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100648 return -GMM_CAUSE_INV_MAND_INFO;
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100649 }
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100650
Harald Weltee2b53492016-04-25 14:53:43 +0200651 if (!gsup_msg.cause && OSMO_GSUP_IS_MSGT_ERROR(gsup_msg.message_type))
Jacob Erlbeck9ca3ace2015-01-29 14:17:51 +0100652 gsup_msg.cause = GMM_CAUSE_NET_FAIL;
653
Jacob Erlbeck629dacc2015-01-15 17:50:16 +0100654 subscr = gprs_subscr_get_by_imsi(gsup_msg.imsi);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100655
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100656 if (!subscr) {
657 switch (gsup_msg.message_type) {
Harald Weltee2b53492016-04-25 14:53:43 +0200658 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
659 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100660 return gprs_subscr_handle_gsup_purge_no_subscr(&gsup_msg);
661 default:
662 return gprs_subscr_handle_unknown_imsi(&gsup_msg);
663 }
664 }
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100665
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100666 LOGGSUBSCRP(LOGL_INFO, subscr,
Maxe405f152017-02-15 11:43:59 +0100667 "Received GSUP message %s\n",
668 osmo_gsup_message_type_name(gsup_msg.message_type));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100669
670 switch (gsup_msg.message_type) {
Harald Weltee2b53492016-04-25 14:53:43 +0200671 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100672 rc = gprs_subscr_handle_loc_cancel_req(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100673 break;
674
Harald Weltee2b53492016-04-25 14:53:43 +0200675 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100676 rc = gprs_subscr_handle_gsup_auth_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100677 break;
678
Harald Weltee2b53492016-04-25 14:53:43 +0200679 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100680 rc = gprs_subscr_handle_gsup_auth_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100681 break;
682
Harald Weltee2b53492016-04-25 14:53:43 +0200683 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100684 rc = gprs_subscr_handle_gsup_upd_loc_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100685 break;
686
Harald Weltee2b53492016-04-25 14:53:43 +0200687 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100688 rc = gprs_subscr_handle_gsup_upd_loc_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100689 break;
690
Harald Weltee2b53492016-04-25 14:53:43 +0200691 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100692 rc = gprs_subscr_handle_gsup_purge_err(subscr, &gsup_msg);
693 break;
694
Harald Weltee2b53492016-04-25 14:53:43 +0200695 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100696 rc = gprs_subscr_handle_gsup_purge_res(subscr, &gsup_msg);
697 break;
698
Harald Weltee2b53492016-04-25 14:53:43 +0200699 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Weltec21455b2016-05-05 18:25:12 +0200700 rc = gprs_subscr_handle_gsup_isd_req(subscr, &gsup_msg);
701 break;
702
Harald Weltee2b53492016-04-25 14:53:43 +0200703 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Maxe405f152017-02-15 11:43:59 +0100704 rc = gprs_subscr_handle_gsup_dsd_req(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100705 break;
706
707 default:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100708 LOGGSUBSCRP(LOGL_ERROR, subscr,
Maxe405f152017-02-15 11:43:59 +0100709 "Rx GSUP message %s not valid at SGSN\n",
710 osmo_gsup_message_type_name(gsup_msg.message_type));
Harald Weltee2b53492016-04-25 14:53:43 +0200711 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100712 gprs_subscr_tx_gsup_error_reply(
713 subscr, &gsup_msg, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
714 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100715 break;
716 };
717
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100718 subscr_put(subscr);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100719
720 return rc;
721}
722
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100723int gprs_subscr_purge(struct gsm_subscriber *subscr)
724{
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200725 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Harald Weltee2b53492016-04-25 14:53:43 +0200726 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100727
728 LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n");
729
Harald Weltee2b53492016-04-25 14:53:43 +0200730 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200731
732 /* Provide the HLR number in case it is known */
733 gsup_msg.hlr_enc_len = sdata->hlr_len;
734 gsup_msg.hlr_enc = sdata->hlr;
735
Jacob Erlbeck0c8e6bd2015-02-03 19:45:46 +0100736 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100737}
738
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100739int gprs_subscr_query_auth_info(struct gsm_subscriber *subscr)
740{
Harald Weltee2b53492016-04-25 14:53:43 +0200741 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100742
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100743 LOGGSUBSCRP(LOGL_INFO, subscr,
744 "subscriber auth info is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100745
Harald Weltee2b53492016-04-25 14:53:43 +0200746 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100747 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
748}
749
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100750int gprs_subscr_location_update(struct gsm_subscriber *subscr)
751{
Harald Weltee2b53492016-04-25 14:53:43 +0200752 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100753
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100754 LOGGSUBSCRP(LOGL_INFO, subscr,
755 "subscriber data is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100756
Harald Weltee2b53492016-04-25 14:53:43 +0200757 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100758 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100759}
760
761void gprs_subscr_update(struct gsm_subscriber *subscr)
762{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100763 LOGGSUBSCRP(LOGL_DEBUG, subscr, "Updating subscriber data\n");
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100764
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100765 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100766 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
767
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100768 if (subscr->sgsn_data->mm)
769 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100770}
771
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100772void gprs_subscr_update_auth_info(struct gsm_subscriber *subscr)
773{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100774 LOGGSUBSCRP(LOGL_DEBUG, subscr,
775 "Updating subscriber authentication info\n");
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100776
777 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
778 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
779
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100780 if (subscr->sgsn_data->mm)
781 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100782}
783
784struct gsm_subscriber *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mmctx)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100785{
786 struct gsm_subscriber *subscr = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100787
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100788 if (mmctx->subscr)
789 return subscr_get(mmctx->subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100790
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100791 if (mmctx->imsi[0])
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100792 subscr = gprs_subscr_get_by_imsi(mmctx->imsi);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100793
794 if (!subscr) {
795 subscr = gprs_subscr_get_or_create(mmctx->imsi);
796 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100797 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100798 }
799
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +0100800 osmo_strlcpy(subscr->equipment.imei, mmctx->imei,
801 sizeof(subscr->equipment.imei));
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100802
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100803 if (subscr->lac != mmctx->ra.lac)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100804 subscr->lac = mmctx->ra.lac;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100805
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100806 subscr->sgsn_data->mm = mmctx;
807 mmctx->subscr = subscr_get(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100808
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100809 return subscr;
810}
811
812int gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx)
813{
814 struct gsm_subscriber *subscr = NULL;
815 int rc;
816
817 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber data update\n");
818
819 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
820
821 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
822
823 rc = gprs_subscr_location_update(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100824 subscr_put(subscr);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100825 return rc;
826}
827
828int gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx)
829{
830 struct gsm_subscriber *subscr = NULL;
831 int rc;
832
833 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber authentication info\n");
834
835 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
836
837 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
838
839 rc = gprs_subscr_query_auth_info(subscr);
840 subscr_put(subscr);
841 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100842}