blob: d9bc10a1895d94ed55037534fefbeedbbfb491f4 [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>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010024#include <openbsc/gsm_subscriber.h>
Jacob Erlbeck233715c2014-12-18 12:46:47 +010025#include <openbsc/gprs_gsup_client.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010026
27#include <openbsc/sgsn.h>
28#include <openbsc/gprs_sgsn.h>
29#include <openbsc/gprs_gmm.h>
Harald Weltee2b53492016-04-25 14:53:43 +020030#include <openbsc/osmo_gsup_messages.h>
Jacob Erlbeck94a346a2014-12-17 14:03:35 +010031#include <openbsc/gprs_utils.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010032
33#include <openbsc/debug.h>
34
Jacob Erlbeck233715c2014-12-18 12:46:47 +010035#include <netinet/in.h>
36#include <arpa/inet.h>
37
Jacob Erlbeck87972662015-01-08 15:18:39 +010038#define SGSN_SUBSCR_MAX_RETRIES 3
39#define SGSN_SUBSCR_RETRY_INTERVAL 10
40
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +010041#define LOGGSUPP(level, gsup, fmt, args...) \
42 LOGP(DGPRS, level, "GSUP(%s) " fmt, \
43 (gsup)->imsi, \
44 ## args)
45
Jacob Erlbecke8b69682014-11-12 10:12:11 +010046extern void *tall_bsc_ctx;
47
Jacob Erlbeck233715c2014-12-18 12:46:47 +010048static int gsup_read_cb(struct gprs_gsup_client *gsupc, struct msgb *msg);
49
50/* TODO: Some functions are specific to the SGSN, but this file is more general
51 * (it has gprs_* name). Either move these functions elsewhere, split them and
52 * move a part, or replace the gprs_ prefix by sgsn_. The applies to
53 * gprs_subscr_init, gsup_read_cb, and gprs_subscr_tx_gsup_message.
54 */
55
56int gprs_subscr_init(struct sgsn_instance *sgi)
Jacob Erlbecke8b69682014-11-12 10:12:11 +010057{
Jacob Erlbeck233715c2014-12-18 12:46:47 +010058 const char *addr_str;
59
60 if (!sgi->cfg.gsup_server_addr.sin_addr.s_addr)
61 return 0;
62
63 addr_str = inet_ntoa(sgi->cfg.gsup_server_addr.sin_addr);
64
65 sgi->gsup_client = gprs_gsup_client_create(
66 addr_str, sgi->cfg.gsup_server_port,
Neels Hofmeyr38b5e272015-10-12 11:57:37 +020067 &gsup_read_cb,
68 &sgi->cfg.oap);
Jacob Erlbeck233715c2014-12-18 12:46:47 +010069
70 if (!sgi->gsup_client)
71 return -1;
72
73 return 1;
74}
75
76static int gsup_read_cb(struct gprs_gsup_client *gsupc, struct msgb *msg)
77{
78 int rc;
79
80 rc = gprs_subscr_rx_gsup_message(msg);
Jacob Erlbeckedb67a12014-12-19 19:15:55 +010081 msgb_free(msg);
Jacob Erlbeck233715c2014-12-18 12:46:47 +010082 if (rc < 0)
83 return -1;
84
85 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +010086}
87
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +010088int gprs_subscr_purge(struct gsm_subscriber *subscr);
89
Jacob Erlbeck359cafa2014-12-02 11:28:38 +010090static struct sgsn_subscriber_data *sgsn_subscriber_data_alloc(void *ctx)
91{
92 struct sgsn_subscriber_data *sdata;
Jacob Erlbeckb1332b62014-12-08 15:52:00 +010093 int idx;
Jacob Erlbeck359cafa2014-12-02 11:28:38 +010094
95 sdata = talloc_zero(ctx, struct sgsn_subscriber_data);
96
Jacob Erlbeckf96779f2015-01-19 11:10:04 +010097 sdata->error_cause = SGSN_ERROR_CAUSE_NONE;
98
Jacob Erlbeckb1332b62014-12-08 15:52:00 +010099 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
100 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
101
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100102 INIT_LLIST_HEAD(&sdata->pdp_list);
103
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100104 return sdata;
105}
106
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100107struct sgsn_subscriber_pdp_data* sgsn_subscriber_pdp_data_alloc(
108 struct sgsn_subscriber_data *sdata)
109{
110 struct sgsn_subscriber_pdp_data* pdata;
111
112 pdata = talloc_zero(sdata, struct sgsn_subscriber_pdp_data);
113
114 llist_add_tail(&pdata->list, &sdata->pdp_list);
115
116 return pdata;
117}
118
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100119struct gsm_subscriber *gprs_subscr_get_or_create(const char *imsi)
120{
121 struct gsm_subscriber *subscr;
122
123 subscr = subscr_get_or_create(NULL, imsi);
124 if (!subscr)
125 return NULL;
126
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100127 if (!subscr->sgsn_data)
128 subscr->sgsn_data = sgsn_subscriber_data_alloc(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100129 return subscr;
130}
131
132struct gsm_subscriber *gprs_subscr_get_by_imsi(const char *imsi)
133{
134 return subscr_active_by_imsi(NULL, imsi);
135}
136
Jacob Erlbecke71ab2f2015-01-26 11:07:24 +0100137void gprs_subscr_cleanup(struct gsm_subscriber *subscr)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100138{
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100139 if (subscr->sgsn_data->mm) {
140 subscr_put(subscr->sgsn_data->mm->subscr);
141 subscr->sgsn_data->mm->subscr = NULL;
142 subscr->sgsn_data->mm = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100143 }
144
Holger Hans Peter Freytherd95cb732015-01-20 21:14:03 +0100145 if (subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE) {
146 gprs_subscr_purge(subscr);
147 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100148 }
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100149}
150
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100151void gprs_subscr_cancel(struct gsm_subscriber *subscr)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100152{
153 subscr->authorized = 0;
154 subscr->flags |= GPRS_SUBSCRIBER_CANCELLED;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100155 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100156
157 gprs_subscr_update(subscr);
Jacob Erlbecke71ab2f2015-01-26 11:07:24 +0100158 gprs_subscr_cleanup(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100159}
160
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100161static int gprs_subscr_tx_gsup_message(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200162 struct osmo_gsup_message *gsup_msg)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100163{
Jacob Erlbeck233715c2014-12-18 12:46:47 +0100164 struct msgb *msg = gprs_gsup_msgb_alloc();
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100165
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100166 if (strlen(gsup_msg->imsi) == 0 && subscr)
167 strncpy(gsup_msg->imsi, subscr->imsi, sizeof(gsup_msg->imsi) - 1);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100168
Harald Weltee2b53492016-04-25 14:53:43 +0200169 osmo_gsup_encode(msg, gsup_msg);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100170
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100171 LOGGSUBSCRP(LOGL_INFO, subscr,
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100172 "Sending GSUP, will send: %s\n", msgb_hexdump(msg));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100173
Jacob Erlbeck233715c2014-12-18 12:46:47 +0100174 if (!sgsn->gsup_client) {
175 msgb_free(msg);
176 return -ENOTSUP;
177 }
178
179 return gprs_gsup_client_send(sgsn->gsup_client, msg);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100180}
181
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100182static int gprs_subscr_tx_gsup_error_reply(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200183 struct osmo_gsup_message *gsup_orig,
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100184 enum gsm48_gmm_cause cause)
185{
Harald Weltee2b53492016-04-25 14:53:43 +0200186 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100187
188 strncpy(gsup_reply.imsi, gsup_orig->imsi, sizeof(gsup_reply.imsi) - 1);
189 gsup_reply.cause = cause;
190 gsup_reply.message_type =
Harald Weltee2b53492016-04-25 14:53:43 +0200191 OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type);
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100192
193 return gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
194}
195
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100196static int gprs_subscr_handle_gsup_auth_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200197 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100198{
199 unsigned idx;
200 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
201
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100202 LOGGSUBSCRP(LOGL_INFO, subscr,
Harald Welte34ef4c52016-04-20 13:13:19 +0200203 "Got SendAuthenticationInfoResult, num_auth_vectors = %zu\n",
204 gsup_msg->num_auth_vectors);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100205
Harald Welte34ef4c52016-04-20 13:13:19 +0200206 if (gsup_msg->num_auth_vectors > 0) {
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100207 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
208
209 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
210 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
211 }
212
Harald Welte34ef4c52016-04-20 13:13:19 +0200213 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
214 size_t key_seq = idx;
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100215 LOGGSUBSCRP(LOGL_DEBUG, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400216 "Adding auth tuple, cksn = %zu\n", key_seq);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100217 if (key_seq >= ARRAY_SIZE(sdata->auth_triplets)) {
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100218 LOGGSUBSCRP(LOGL_NOTICE, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400219 "Skipping auth triplet with invalid cksn %zu\n",
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100220 key_seq);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100221 continue;
222 }
Harald Welte34ef4c52016-04-20 13:13:19 +0200223 sdata->auth_triplets[key_seq].vec = gsup_msg->auth_vectors[idx];
224 sdata->auth_triplets[key_seq].key_seq = key_seq;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100225 }
226
227 sdata->auth_triplets_updated = 1;
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100228 sdata->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100229
230 gprs_subscr_update_auth_info(subscr);
231
232 return 0;
233}
234
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100235static int gprs_subscr_pdp_data_clear(struct gsm_subscriber *subscr)
236{
237 struct sgsn_subscriber_pdp_data *pdp, *pdp2;
238 int count = 0;
239
240 llist_for_each_entry_safe(pdp, pdp2, &subscr->sgsn_data->pdp_list, list) {
241 llist_del(&pdp->list);
242 talloc_free(pdp);
243 count += 1;
244 }
245
246 return count;
247}
248
249static struct sgsn_subscriber_pdp_data *gprs_subscr_pdp_data_get_by_id(
250 struct gsm_subscriber *subscr, unsigned context_id)
251{
252 struct sgsn_subscriber_pdp_data *pdp;
253
254 llist_for_each_entry(pdp, &subscr->sgsn_data->pdp_list, list) {
255 if (pdp->context_id == context_id)
256 return pdp;
257 }
258
259 return NULL;
260}
261
262
263static void gprs_subscr_gsup_insert_data(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200264 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100265{
Holger Hans Peter Freyther786cfee2015-04-23 09:53:53 -0400266 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100267 unsigned idx;
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100268 int rc;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100269
Holger Hans Peter Freyther786cfee2015-04-23 09:53:53 -0400270 if (gsup_msg->msisdn_enc) {
271 if (gsup_msg->msisdn_enc_len > sizeof(sdata->msisdn)) {
272 LOGP(DGPRS, LOGL_ERROR, "MSISDN too long (%zu)\n",
273 gsup_msg->msisdn_enc_len);
274 sdata->msisdn_len = 0;
275 } else {
276 memcpy(sdata->msisdn, gsup_msg->msisdn_enc,
277 gsup_msg->msisdn_enc_len);
278 sdata->msisdn_len = gsup_msg->msisdn_enc_len;
279 }
280 }
281
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200282 if (gsup_msg->hlr_enc) {
283 if (gsup_msg->hlr_enc_len > sizeof(sdata->hlr)) {
284 LOGP(DGPRS, LOGL_ERROR, "HLR-Number too long (%zu)\n",
285 gsup_msg->hlr_enc_len);
286 sdata->hlr_len = 0;
287 } else {
288 memcpy(sdata->hlr, gsup_msg->hlr_enc,
289 gsup_msg->hlr_enc_len);
290 sdata->hlr_len = gsup_msg->hlr_enc_len;
291 }
292 }
293
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100294 if (gsup_msg->pdp_info_compl) {
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100295 rc = gprs_subscr_pdp_data_clear(subscr);
296 if (rc > 0)
297 LOGP(DGPRS, LOGL_INFO, "Cleared existing PDP info\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100298 }
299
300 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
Harald Weltee2b53492016-04-25 14:53:43 +0200301 struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100302 size_t ctx_id = pdp_info->context_id;
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100303 struct sgsn_subscriber_pdp_data *pdp_data;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100304
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100305 if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) {
306 LOGGSUBSCRP(LOGL_ERROR, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400307 "APN too long, context id = %zu, APN = %s\n",
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100308 ctx_id, osmo_hexdump(pdp_info->apn_enc,
309 pdp_info->apn_enc_len));
310 continue;
311 }
312
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400313 if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) {
314 LOGGSUBSCRP(LOGL_ERROR, subscr,
315 "QoS info too long (%zu)\n",
316 pdp_info->qos_enc_len);
317 continue;
318 }
319
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100320 LOGGSUBSCRP(LOGL_INFO, subscr,
Holger Hans Peter Freyther844f4b82015-04-23 11:55:23 -0400321 "Will set PDP info, context id = %zu, APN = %s\n",
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100322 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
323
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100324 /* Set PDP info [ctx_id] */
325 pdp_data = gprs_subscr_pdp_data_get_by_id(subscr, ctx_id);
326 if (!pdp_data) {
327 pdp_data = sgsn_subscriber_pdp_data_alloc(subscr->sgsn_data);
328 pdp_data->context_id = ctx_id;
329 }
330
331 OSMO_ASSERT(pdp_data != NULL);
332 pdp_data->pdp_type = pdp_info->pdp_type;
333 gprs_apn_to_str(pdp_data->apn_str,
334 pdp_info->apn_enc, pdp_info->apn_enc_len);
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400335 memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len);
336 pdp_data->qos_subscribed_len = pdp_info->qos_enc_len;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100337 }
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100338}
339
340static int gprs_subscr_handle_gsup_upd_loc_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200341 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck94a346a2014-12-17 14:03:35 +0100342{
343 gprs_subscr_gsup_insert_data(subscr, gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100344
345 subscr->authorized = 1;
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100346 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100347
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100348 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
349
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100350 gprs_subscr_update(subscr);
351 return 0;
352}
353
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100354static int check_cause(int cause)
355{
356 switch (cause) {
357 case GMM_CAUSE_IMSI_UNKNOWN ... GMM_CAUSE_ILLEGAL_ME:
358 case GMM_CAUSE_GPRS_NOTALLOWED ... GMM_CAUSE_NO_GPRS_PLMN:
359 return EACCES;
360
361 case GMM_CAUSE_MSC_TEMP_NOTREACH ... GMM_CAUSE_CONGESTION:
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100362 return EHOSTUNREACH;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100363
364 case GMM_CAUSE_SEM_INCORR_MSG ... GMM_CAUSE_PROTO_ERR_UNSPEC:
365 default:
366 return EINVAL;
367 }
368}
369
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100370static int gprs_subscr_handle_gsup_auth_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200371 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100372{
373 unsigned idx;
374 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100375 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100376
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100377 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100378
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100379 LOGGSUBSCRP(LOGL_DEBUG, subscr,
380 "Send authentication info has failed with cause %d, "
381 "handled as: %s\n",
382 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100383
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100384 switch (cause_err) {
385 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100386 LOGGSUBSCRP(LOGL_NOTICE, subscr,
387 "GPRS send auth info req failed, access denied, "
388 "GMM cause = '%s' (%d)\n",
389 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
390 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100391 /* Clear auth tuples */
392 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
393 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
394 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100395
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100396 subscr->authorized = 0;
397 sdata->error_cause = gsup_msg->cause;
398 gprs_subscr_update_auth_info(subscr);
399 break;
400
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100401 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100402 LOGGSUBSCRP(LOGL_NOTICE, subscr,
403 "GPRS send auth info req failed, GMM cause = '%s' (%d)\n",
404 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
405 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100406
407 sdata->error_cause = gsup_msg->cause;
408 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100409 break;
410
411 default:
412 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100413 LOGGSUBSCRP(LOGL_ERROR, subscr,
414 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
415 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
416 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100417 break;
418 }
419
420 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100421}
422
423static int gprs_subscr_handle_gsup_upd_loc_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200424 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100425{
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100426 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100427
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100428 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100429
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100430 LOGGSUBSCRP(LOGL_DEBUG, subscr,
431 "Update location has failed with cause %d, handled as: %s\n",
432 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100433
434 switch (cause_err) {
435 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100436 LOGGSUBSCRP(LOGL_NOTICE, subscr,
437 "GPRS update location failed, access denied, "
438 "GMM cause = '%s' (%d)\n",
439 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
440 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100441
442 subscr->authorized = 0;
443 subscr->sgsn_data->error_cause = gsup_msg->cause;
444 gprs_subscr_update_auth_info(subscr);
445 break;
446
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100447 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100448 LOGGSUBSCRP(LOGL_NOTICE, subscr,
449 "GPRS update location failed, GMM cause = '%s' (%d)\n",
450 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
451 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100452
453 subscr->sgsn_data->error_cause = gsup_msg->cause;
454 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100455 break;
456
457 default:
458 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100459 LOGGSUBSCRP(LOGL_ERROR, subscr,
460 "GSUP protocol remote error, 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 break;
464 }
465
466 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100467}
468
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100469static int gprs_subscr_handle_gsup_purge_no_subscr(
Harald Weltee2b53492016-04-25 14:53:43 +0200470 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100471{
Harald Weltee2b53492016-04-25 14:53:43 +0200472 if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100473 LOGGSUPP(LOGL_NOTICE, gsup_msg,
474 "Purge MS has failed with cause '%s' (%d)\n",
475 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
476 gsup_msg->cause);
477 return -gsup_msg->cause;
478 }
479
480 LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n");
481 return 0;
482}
483
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100484static int gprs_subscr_handle_gsup_purge_res(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200485 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100486{
487 LOGGSUBSCRP(LOGL_INFO, subscr, "Completing purge MS\n");
488
489 /* Force silent cancellation */
Jacob Erlbeckf96779f2015-01-19 11:10:04 +0100490 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100491 gprs_subscr_cancel(subscr);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100492
493 return 0;
494}
495
496static int gprs_subscr_handle_gsup_purge_err(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200497 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100498{
499 LOGGSUBSCRP(LOGL_NOTICE, subscr,
500 "Purge MS has failed with cause '%s' (%d)\n",
501 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
502 gsup_msg->cause);
503
504 /* In GSM 09.02, 19.1.4.4, the text and the SDL diagram imply that
505 * the subscriber data is not removed if the request has failed. On the
506 * other hand, keeping the subscriber data in either error case
507 * (subscriber unknown, syntactical message error, connection error)
508 * doesn't seem to give any advantage, since the data will be restored
509 * on the next Attach Request anyway.
510 * This approach ensures, that the subscriber record will not stick if
511 * an error happens.
512 */
513
514 /* TODO: Check whether this behaviour is acceptable and either just
515 * remove this TODO-notice or change the implementation to not delete
516 * the subscriber data (eventually resetting the ENABLE_PURGE flag and
517 * restarting the expiry timer based on the cause).
518 *
519 * Subscriber Unknown: cancel subscr
520 * Temporary network problems: do nothing (handled by timer based retry)
521 * Message problems (syntax, nyi, ...): cancel subscr (retry won't help)
522 */
523
524 gprs_subscr_handle_gsup_purge_res(subscr, gsup_msg);
525
526 return -gsup_msg->cause;
527}
528
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100529static int gprs_subscr_handle_loc_cancel_req(struct gsm_subscriber *subscr,
Harald Weltee2b53492016-04-25 14:53:43 +0200530 struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100531{
Harald Weltee2b53492016-04-25 14:53:43 +0200532 struct osmo_gsup_message gsup_reply = {0};
Jacob Erlbecke7fea452015-04-07 17:49:48 +0200533 int is_update_procedure = !gsup_msg->cancel_type ||
Harald Weltee2b53492016-04-25 14:53:43 +0200534 gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE;
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100535
Jacob Erlbeck3b0d0c02015-01-27 14:56:40 +0100536 LOGGSUBSCRP(LOGL_INFO, subscr, "Cancelling MS subscriber (%s)\n",
537 is_update_procedure ?
538 "update procedure" : "subscription withdraw");
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100539
Harald Weltee2b53492016-04-25 14:53:43 +0200540 gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT;
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100541 gprs_subscr_tx_gsup_message(subscr, &gsup_reply);
542
Jacob Erlbeck3b0d0c02015-01-27 14:56:40 +0100543 if (is_update_procedure)
544 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
545 else
546 /* Since a withdraw cause is not specified, just abort the
547 * current attachment. The following re-attachment should then
548 * be rejected with a proper cause value.
549 */
550 subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
551
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100552 gprs_subscr_cancel(subscr);
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100553
554 return 0;
555}
556
Harald Weltee2b53492016-04-25 14:53:43 +0200557static int gprs_subscr_handle_unknown_imsi(struct osmo_gsup_message *gsup_msg)
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100558{
Harald Weltee2b53492016-04-25 14:53:43 +0200559 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) {
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100560 gprs_subscr_tx_gsup_error_reply(NULL, gsup_msg,
561 GMM_CAUSE_IMSI_UNKNOWN);
562 LOGP(DGPRS, LOGL_NOTICE,
563 "Unknown IMSI %s, discarding GSUP request "
564 "of type 0x%02x\n",
565 gsup_msg->imsi, gsup_msg->message_type);
Harald Weltee2b53492016-04-25 14:53:43 +0200566 } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) {
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100567 LOGP(DGPRS, LOGL_NOTICE,
568 "Unknown IMSI %s, discarding GSUP error "
569 "of type 0x%02x, cause '%s' (%d)\n",
570 gsup_msg->imsi, gsup_msg->message_type,
571 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
572 gsup_msg->cause);
573 } else {
574 LOGP(DGPRS, LOGL_NOTICE,
575 "Unknown IMSI %s, discarding GSUP response "
576 "of type 0x%02x\n",
577 gsup_msg->imsi, gsup_msg->message_type);
578 }
579
580 return -GMM_CAUSE_IMSI_UNKNOWN;
581}
582
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100583int gprs_subscr_rx_gsup_message(struct msgb *msg)
584{
585 uint8_t *data = msgb_l2(msg);
586 size_t data_len = msgb_l2len(msg);
587 int rc = 0;
588
Harald Weltee2b53492016-04-25 14:53:43 +0200589 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100590 struct gsm_subscriber *subscr;
591
Harald Weltee2b53492016-04-25 14:53:43 +0200592 rc = osmo_gsup_decode(data, data_len, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100593 if (rc < 0) {
594 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100595 "decoding GSUP message fails with error '%s' (%d)\n",
596 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100597 return rc;
598 }
599
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100600 if (!gsup_msg.imsi[0]) {
601 LOGP(DGPRS, LOGL_ERROR, "Missing IMSI in GSUP message\n");
602
Harald Weltee2b53492016-04-25 14:53:43 +0200603 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100604 gprs_subscr_tx_gsup_error_reply(NULL, &gsup_msg,
605 GMM_CAUSE_INV_MAND_INFO);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100606 return -GMM_CAUSE_INV_MAND_INFO;
Jacob Erlbeck6c8c8ab2015-01-29 14:00:28 +0100607 }
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100608
Harald Weltee2b53492016-04-25 14:53:43 +0200609 if (!gsup_msg.cause && OSMO_GSUP_IS_MSGT_ERROR(gsup_msg.message_type))
Jacob Erlbeck9ca3ace2015-01-29 14:17:51 +0100610 gsup_msg.cause = GMM_CAUSE_NET_FAIL;
611
Jacob Erlbeck629dacc2015-01-15 17:50:16 +0100612 subscr = gprs_subscr_get_by_imsi(gsup_msg.imsi);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100613
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100614 if (!subscr) {
615 switch (gsup_msg.message_type) {
Harald Weltee2b53492016-04-25 14:53:43 +0200616 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
617 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckc7c990c2015-01-27 13:47:24 +0100618 return gprs_subscr_handle_gsup_purge_no_subscr(&gsup_msg);
619 default:
620 return gprs_subscr_handle_unknown_imsi(&gsup_msg);
621 }
622 }
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100623
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100624 LOGGSUBSCRP(LOGL_INFO, subscr,
625 "Received GSUP message of type 0x%02x\n", gsup_msg.message_type);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100626
627 switch (gsup_msg.message_type) {
Harald Weltee2b53492016-04-25 14:53:43 +0200628 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100629 rc = gprs_subscr_handle_loc_cancel_req(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100630 break;
631
Harald Weltee2b53492016-04-25 14:53:43 +0200632 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100633 rc = gprs_subscr_handle_gsup_auth_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100634 break;
635
Harald Weltee2b53492016-04-25 14:53:43 +0200636 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100637 rc = gprs_subscr_handle_gsup_auth_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100638 break;
639
Harald Weltee2b53492016-04-25 14:53:43 +0200640 case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100641 rc = gprs_subscr_handle_gsup_upd_loc_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100642 break;
643
Harald Weltee2b53492016-04-25 14:53:43 +0200644 case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100645 rc = gprs_subscr_handle_gsup_upd_loc_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100646 break;
647
Harald Weltee2b53492016-04-25 14:53:43 +0200648 case OSMO_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100649 rc = gprs_subscr_handle_gsup_purge_err(subscr, &gsup_msg);
650 break;
651
Harald Weltee2b53492016-04-25 14:53:43 +0200652 case OSMO_GSUP_MSGT_PURGE_MS_RESULT:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100653 rc = gprs_subscr_handle_gsup_purge_res(subscr, &gsup_msg);
654 break;
655
Harald Weltee2b53492016-04-25 14:53:43 +0200656 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
657 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100658 LOGGSUBSCRP(LOGL_ERROR, subscr,
659 "Rx GSUP message type %d not yet implemented\n",
660 gsup_msg.message_type);
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100661 gprs_subscr_tx_gsup_error_reply(subscr, &gsup_msg,
662 GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100663 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
664 break;
665
666 default:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100667 LOGGSUBSCRP(LOGL_ERROR, subscr,
668 "Rx GSUP message type %d not valid at SGSN\n",
669 gsup_msg.message_type);
Harald Weltee2b53492016-04-25 14:53:43 +0200670 if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type))
Jacob Erlbeck4f414862015-01-15 17:08:30 +0100671 gprs_subscr_tx_gsup_error_reply(
672 subscr, &gsup_msg, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
673 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100674 break;
675 };
676
Jacob Erlbeck00b8b912015-01-08 15:29:01 +0100677 subscr_put(subscr);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100678
679 return rc;
680}
681
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100682int gprs_subscr_purge(struct gsm_subscriber *subscr)
683{
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200684 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Harald Weltee2b53492016-04-25 14:53:43 +0200685 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100686
687 LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n");
688
Harald Weltee2b53492016-04-25 14:53:43 +0200689 gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST;
Holger Hans Peter Freytherfe4a9f62015-05-17 20:58:40 +0200690
691 /* Provide the HLR number in case it is known */
692 gsup_msg.hlr_enc_len = sdata->hlr_len;
693 gsup_msg.hlr_enc = sdata->hlr;
694
Jacob Erlbeck0c8e6bd2015-02-03 19:45:46 +0100695 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100696}
697
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100698int gprs_subscr_query_auth_info(struct gsm_subscriber *subscr)
699{
Harald Weltee2b53492016-04-25 14:53:43 +0200700 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100701
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100702 LOGGSUBSCRP(LOGL_INFO, subscr,
703 "subscriber auth info is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100704
Harald Weltee2b53492016-04-25 14:53:43 +0200705 gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100706 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
707}
708
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100709int gprs_subscr_location_update(struct gsm_subscriber *subscr)
710{
Harald Weltee2b53492016-04-25 14:53:43 +0200711 struct osmo_gsup_message gsup_msg = {0};
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100712
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100713 LOGGSUBSCRP(LOGL_INFO, subscr,
714 "subscriber data is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100715
Harald Weltee2b53492016-04-25 14:53:43 +0200716 gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100717 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100718}
719
720void gprs_subscr_update(struct gsm_subscriber *subscr)
721{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100722 LOGGSUBSCRP(LOGL_DEBUG, subscr, "Updating subscriber data\n");
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100723
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100724 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100725 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
726
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100727 if (subscr->sgsn_data->mm)
728 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100729}
730
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100731void gprs_subscr_update_auth_info(struct gsm_subscriber *subscr)
732{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100733 LOGGSUBSCRP(LOGL_DEBUG, subscr,
734 "Updating subscriber authentication info\n");
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100735
736 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
737 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
738
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100739 if (subscr->sgsn_data->mm)
740 sgsn_update_subscriber_data(subscr->sgsn_data->mm);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100741}
742
743struct gsm_subscriber *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mmctx)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100744{
745 struct gsm_subscriber *subscr = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100746
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100747 if (mmctx->subscr)
748 return subscr_get(mmctx->subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100749
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100750 if (mmctx->imsi[0])
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100751 subscr = gprs_subscr_get_by_imsi(mmctx->imsi);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100752
753 if (!subscr) {
754 subscr = gprs_subscr_get_or_create(mmctx->imsi);
755 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100756 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100757 }
758
759 if (strcpy(subscr->equipment.imei, mmctx->imei) != 0) {
Harald Welte02884f22016-04-20 17:50:17 +0200760 strncpy(subscr->equipment.imei, mmctx->imei,
761 sizeof(subscr->equipment.imei)-1);
762 subscr->equipment.imei[sizeof(subscr->equipment.imei)-1] = 0;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100763 }
764
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100765 if (subscr->lac != mmctx->ra.lac)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100766 subscr->lac = mmctx->ra.lac;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100767
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100768 subscr->sgsn_data->mm = mmctx;
769 mmctx->subscr = subscr_get(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100770
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100771 return subscr;
772}
773
774int gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx)
775{
776 struct gsm_subscriber *subscr = NULL;
777 int rc;
778
779 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber data update\n");
780
781 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
782
783 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
784
785 rc = gprs_subscr_location_update(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100786 subscr_put(subscr);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100787 return rc;
788}
789
790int gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx)
791{
792 struct gsm_subscriber *subscr = NULL;
793 int rc;
794
795 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber authentication info\n");
796
797 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
798
799 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
800
801 rc = gprs_subscr_query_auth_info(subscr);
802 subscr_put(subscr);
803 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100804}