blob: d3e2ea748d4bbd6187553f1e177e4ffd30c47ad8 [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
Harald Weltec21455b2016-05-05 18:25:12 +0200360static int gprs_subscr_handle_gsup_isd_req(struct gsm_subscriber *subscr,
361 struct osmo_gsup_message *gsup_msg)
362{
363 struct osmo_gsup_message gsup_reply = {0};
364
365 gprs_subscr_gsup_insert_data(subscr, gsup_msg);
366
367 subscr->authorized = 1;
368 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
369 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
370 gprs_subscr_update(subscr);
371
372 gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT;
373 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
374}
375
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100376static int check_cause(int cause)
377{
378 switch (cause) {
379 case GMM_CAUSE_IMSI_UNKNOWN ... GMM_CAUSE_ILLEGAL_ME:
380 case GMM_CAUSE_GPRS_NOTALLOWED ... GMM_CAUSE_NO_GPRS_PLMN:
381 return EACCES;
382
383 case GMM_CAUSE_MSC_TEMP_NOTREACH ... GMM_CAUSE_CONGESTION:
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100384 return EHOSTUNREACH;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100385
386 case GMM_CAUSE_SEM_INCORR_MSG ... GMM_CAUSE_PROTO_ERR_UNSPEC:
387 default:
388 return EINVAL;
389 }
390}
391
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100392static int gprs_subscr_handle_gsup_auth_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200393 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100394{
395 unsigned idx;
396 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100397 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100398
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100399 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100400
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100401 LOGGSUBSCRP(LOGL_DEBUG, subscr,
402 "Send authentication info has failed with cause %d, "
403 "handled as: %s\n",
404 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100405
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100406 switch (cause_err) {
407 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100408 LOGGSUBSCRP(LOGL_NOTICE, subscr,
409 "GPRS send auth info req failed, access denied, "
410 "GMM cause = '%s' (%d)\n",
411 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
412 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100413 /* Clear auth tuples */
414 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
415 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
416 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100417
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100418 subscr->authorized = 0;
419 sdata->error_cause = gsup_msg->cause;
420 gprs_subscr_update_auth_info(subscr);
421 break;
422
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100423 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100424 LOGGSUBSCRP(LOGL_NOTICE, subscr,
425 "GPRS send auth info req failed, GMM cause = '%s' (%d)\n",
426 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
427 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100428
429 sdata->error_cause = gsup_msg->cause;
430 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100431 break;
432
433 default:
434 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100435 LOGGSUBSCRP(LOGL_ERROR, subscr,
436 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
437 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
438 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100439 break;
440 }
441
442 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100443}
444
445static int gprs_subscr_handle_gsup_upd_loc_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200446 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100447{
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100448 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100449
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100450 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100451
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100452 LOGGSUBSCRP(LOGL_DEBUG, subscr,
453 "Update location has failed with cause %d, handled as: %s\n",
454 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100455
456 switch (cause_err) {
457 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100458 LOGGSUBSCRP(LOGL_NOTICE, subscr,
459 "GPRS update location failed, access denied, "
460 "GMM cause = '%s' (%d)\n",
461 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
462 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100463
464 subscr->authorized = 0;
465 subscr->sgsn_data->error_cause = gsup_msg->cause;
466 gprs_subscr_update_auth_info(subscr);
467 break;
468
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100469 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100470 LOGGSUBSCRP(LOGL_NOTICE, subscr,
471 "GPRS update location failed, GMM cause = '%s' (%d)\n",
472 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
473 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100474
475 subscr->sgsn_data->error_cause = gsup_msg->cause;
476 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100477 break;
478
479 default:
480 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100481 LOGGSUBSCRP(LOGL_ERROR, subscr,
482 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
483 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
484 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100485 break;
486 }
487
488 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100489}
490
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100491static int gprs_subscr_handle_gsup_purge_no_subscr(
Harald Weltee2b53492016-04-25 14:53:43 +0200492 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100493{
Harald Weltee2b53492016-04-25 14:53:43 +0200494 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100495 LOGGSUPP(LOGL_NOTICE, gsup_msg,
496 "Purge MS has failed with cause '%s' (%d)\n",
497 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
498 gsup_msg->cause);
499 return -gsup_msg->cause;
500 }
501
502 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
503 return 0;
504}
505
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100506static int gprs_subscr_handle_gsup_purge_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200507 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100508{
509 LOGGSUBSCRP(LOGL_INFO, subscr, "Completing purge MS\n");
510
511 /* Force silent cancellation */
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100512 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100513 gprs_subscr_cancel(subscr);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100514
515 return 0;
516}
517
518static int gprs_subscr_handle_gsup_purge_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200519 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100520{
521 LOGGSUBSCRP(LOGL_NOTICE, subscr,
522 "Purge MS has failed with cause '%s' (%d)\n",
523 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
524 gsup_msg->cause);
525
526 /* In GSM 09.02, 19.1.4.4, the text and the SDL diagram imply that
527 * the subscriber data is not removed if the request has failed. On the
528 * other hand, keeping the subscriber data in either error case
529 * (subscriber unknown, syntactical message error, connection error)
530 * doesn't seem to give any advantage, since the data will be restored
531 * on the next Attach Request anyway.
532 * This approach ensures, that the subscriber record will not stick if
533 * an error happens.
534 */
535
536 /* TODO: Check whether this behaviour is acceptable and either just
537 * remove this TODO-notice or change the implementation to not delete
538 * the subscriber data (eventually resetting the ENABLE_PURGE flag and
539 * restarting the expiry timer based on the cause).
540 *
541 * Subscriber Unknown: cancel subscr
542 * Temporary network problems: do nothing (handled by timer based retry)
543 * Message problems (syntax, nyi, ...): cancel subscr (retry won't help)
544 */
545
546 gprs_subscr_handle_gsup_purge_res(subscr, gsup_msg);
547
548 return -gsup_msg->cause;
549}
550
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100551static int gprs_subscr_handle_loc_cancel_req(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200552 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100553{
Harald Weltee2b53492016-04-25 14:53:43 +0200554 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbecke7fea452015-04-07 17:49:48 +0200555 int is_update_procedure = !gsup_msg->cancel_type ||
Harald Weltee2b53492016-04-25 14:53:43 +0200556 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100557
Jacob Erlbeck3b0d0c02015-01-27 14:56:40 +0100558 LOGGSUBSCRP(LOGL_INFO, subscr, "Cancelling MS subscriber (%s)\n",
559 is_update_procedure ?
560 "update procedure" : "subscription withdraw");
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100561
Harald Weltee2b53492016-04-25 14:53:43 +0200562 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100563 gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
564
Jacob Erlbeck3b0d0c02015-01-27 14:56:40 +0100565 if (is_update_procedure)
566 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
567 else
568 /* Since a withdraw cause is not specified, just abort the
569 * current attachment. The following re-attachment should then
570 * be rejected with a proper cause value.
571 */
572 subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
573
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100574 gprs_subscr_cancel(subscr);
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100575
576 return 0;
577}
578
Harald Weltee2b53492016-04-25 14:53:43 +0200579static int gprs_subscr_handle_unknown_imsi(struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100580{
Harald Weltee2b53492016-04-25 14:53:43 +0200581 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100582 gprs_subscr_tx_gsup_error_reply(NULL, gsup_msg,
583 GMM_CAUSE_IMSI_UNKNOWN);
584 LOGP(DGPRS, LOGL_NOTICE,
585 "Unknown IMSI %s, discarding GSUP request "
586 "of type 0x%02x\n",
587 gsup_msg->imsi, gsup_msg->message_type);
Harald Weltee2b53492016-04-25 14:53:43 +0200588 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100589 LOGP(DGPRS, LOGL_NOTICE,
590 "Unknown IMSI %s, discarding GSUP error "
591 "of type 0x%02x, cause '%s' (%d)\n",
592 gsup_msg->imsi, gsup_msg->message_type,
593 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
594 gsup_msg->cause);
595 } else {
596 LOGP(DGPRS, LOGL_NOTICE,
597 "Unknown IMSI %s, discarding GSUP response "
598 "of type 0x%02x\n",
599 gsup_msg->imsi, gsup_msg->message_type);
600 }
601
602 return -GMM_CAUSE_IMSI_UNKNOWN;
603}
604
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100605int gprs_subscr_rx_gsup_message(struct msgb *msg)
606{
607 uint8_t *data = msgb_l2(msg);
608 size_t data_len = msgb_l2len(msg);
609 int rc = 0;
610
Harald Weltee2b53492016-04-25 14:53:43 +0200611 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100612 struct gsm_subscriber *subscr;
613
Harald Weltee2b53492016-04-25 14:53:43 +0200614 rc = osmo_gsup_decode(data, data_len, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100615 if (rc < 0) {
616 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100617 "decoding GSUP message fails with error '%s' (%d)\n",
618 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100619 return rc;
620 }
621
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100622 if (!gsup_msg.imsi[0]) {
623 LOGP(DGPRS, LOGL_ERROR, "Missing IMSI in GSUP message\n");
624
Harald Weltee2b53492016-04-25 14:53:43 +0200625 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100626 gprs_subscr_tx_gsup_error_reply(NULL, &gsup_msg,
627 GMM_CAUSE_INV_MAND_INFO);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100628 return -GMM_CAUSE_INV_MAND_INFO;
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100629 }
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100630
Harald Weltee2b53492016-04-25 14:53:43 +0200631 if (!gsup_msg.cause && OSMO_GSUP_IS_MSGT_ERROR(gsup_msg.message_type))
Jacob Erlbeck9ca3ace2015-01-29 14:17:51 +0100632 gsup_msg.cause = GMM_CAUSE_NET_FAIL;
633
Jacob Erlbeck629dacc2015-01-15 17:50:16 +0100634 subscr = gprs_subscr_get_by_imsi(gsup_msg.imsi);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100635
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100636 if (!subscr) {
637 switch (gsup_msg.message_type) {
Harald Weltee2b53492016-04-25 14:53:43 +0200638 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
639 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100640 return gprs_subscr_handle_gsup_purge_no_subscr(&gsup_msg);
641 default:
642 return gprs_subscr_handle_unknown_imsi(&gsup_msg);
643 }
644 }
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100645
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100646 LOGGSUBSCRP(LOGL_INFO, subscr,
647 "Received GSUP message of type 0x%02x\n", gsup_msg.message_type);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100648
649 switch (gsup_msg.message_type) {
Harald Weltee2b53492016-04-25 14:53:43 +0200650 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100651 rc = gprs_subscr_handle_loc_cancel_req(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100652 break;
653
Harald Weltee2b53492016-04-25 14:53:43 +0200654 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100655 rc = gprs_subscr_handle_gsup_auth_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100656 break;
657
Harald Weltee2b53492016-04-25 14:53:43 +0200658 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100659 rc = gprs_subscr_handle_gsup_auth_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100660 break;
661
Harald Weltee2b53492016-04-25 14:53:43 +0200662 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100663 rc = gprs_subscr_handle_gsup_upd_loc_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100664 break;
665
Harald Weltee2b53492016-04-25 14:53:43 +0200666 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100667 rc = gprs_subscr_handle_gsup_upd_loc_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100668 break;
669
Harald Weltee2b53492016-04-25 14:53:43 +0200670 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100671 rc = gprs_subscr_handle_gsup_purge_err(subscr, &gsup_msg);
672 break;
673
Harald Weltee2b53492016-04-25 14:53:43 +0200674 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100675 rc = gprs_subscr_handle_gsup_purge_res(subscr, &gsup_msg);
676 break;
677
Harald Weltee2b53492016-04-25 14:53:43 +0200678 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
Harald Weltec21455b2016-05-05 18:25:12 +0200679 rc = gprs_subscr_handle_gsup_isd_req(subscr, &gsup_msg);
680 break;
681
Harald Weltee2b53492016-04-25 14:53:43 +0200682 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100683 LOGGSUBSCRP(LOGL_ERROR, subscr,
684 "Rx GSUP message type %d not yet implemented\n",
685 gsup_msg.message_type);
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100686 gprs_subscr_tx_gsup_error_reply(subscr, &gsup_msg,
687 GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100688 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
689 break;
690
691 default:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100692 LOGGSUBSCRP(LOGL_ERROR, subscr,
693 "Rx GSUP message type %d not valid at SGSN\n",
694 gsup_msg.message_type);
Harald Weltee2b53492016-04-25 14:53:43 +0200695 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100696 gprs_subscr_tx_gsup_error_reply(
697 subscr, &gsup_msg, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
698 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100699 break;
700 };
701
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100702 subscr_put(subscr);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100703
704 return rc;
705}
706
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100707int gprs_subscr_purge(struct gsm_subscriber *subscr)
708{
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200709 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Harald Weltee2b53492016-04-25 14:53:43 +0200710 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100711
712 LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n");
713
Harald Weltee2b53492016-04-25 14:53:43 +0200714 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200715
716 /* Provide the HLR number in case it is known */
717 gsup_msg.hlr_enc_len = sdata->hlr_len;
718 gsup_msg.hlr_enc = sdata->hlr;
719
Jacob Erlbeck0c8e6bd2015-02-03 19:45:46 +0100720 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100721}
722
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100723int gprs_subscr_query_auth_info(struct gsm_subscriber *subscr)
724{
Harald Weltee2b53492016-04-25 14:53:43 +0200725 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100726
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100727 LOGGSUBSCRP(LOGL_INFO, subscr,
728 "subscriber auth info is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100729
Harald Weltee2b53492016-04-25 14:53:43 +0200730 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100731 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
732}
733
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100734int gprs_subscr_location_update(struct gsm_subscriber *subscr)
735{
Harald Weltee2b53492016-04-25 14:53:43 +0200736 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100737
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100738 LOGGSUBSCRP(LOGL_INFO, subscr,
739 "subscriber data is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100740
Harald Weltee2b53492016-04-25 14:53:43 +0200741 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100742 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100743}
744
745void gprs_subscr_update(struct gsm_subscriber *subscr)
746{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100747 LOGGSUBSCRP(LOGL_DEBUG, subscr, "Updating subscriber data\n");
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100748
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100749 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100750 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
751
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100752 if (subscr->sgsn_data->mm)
753 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100754}
755
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100756void gprs_subscr_update_auth_info(struct gsm_subscriber *subscr)
757{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100758 LOGGSUBSCRP(LOGL_DEBUG, subscr,
759 "Updating subscriber authentication info\n");
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100760
761 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
762 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
763
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100764 if (subscr->sgsn_data->mm)
765 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100766}
767
768struct gsm_subscriber *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mmctx)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100769{
770 struct gsm_subscriber *subscr = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100771
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100772 if (mmctx->subscr)
773 return subscr_get(mmctx->subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100774
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100775 if (mmctx->imsi[0])
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100776 subscr = gprs_subscr_get_by_imsi(mmctx->imsi);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100777
778 if (!subscr) {
779 subscr = gprs_subscr_get_or_create(mmctx->imsi);
780 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100781 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100782 }
783
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +0100784 osmo_strlcpy(subscr->equipment.imei, mmctx->imei,
785 sizeof(subscr->equipment.imei));
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100786
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100787 if (subscr->lac != mmctx->ra.lac)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100788 subscr->lac = mmctx->ra.lac;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100789
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100790 subscr->sgsn_data->mm = mmctx;
791 mmctx->subscr = subscr_get(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100792
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100793 return subscr;
794}
795
796int gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx)
797{
798 struct gsm_subscriber *subscr = NULL;
799 int rc;
800
801 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber data update\n");
802
803 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
804
805 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
806
807 rc = gprs_subscr_location_update(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100808 subscr_put(subscr);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100809 return rc;
810}
811
812int gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx)
813{
814 struct gsm_subscriber *subscr = NULL;
815 int rc;
816
817 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber authentication info\n");
818
819 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
820
821 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
822
823 rc = gprs_subscr_query_auth_info(subscr);
824 subscr_put(subscr);
825 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100826}