blob: 88e037e9df3794feb726b26d3a05616581d387f5 [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
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <openbsc/gsm_subscriber.h>
Jacob Erlbeck233715c2014-12-18 12:46:47 +010023#include <openbsc/gprs_gsup_client.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010024
25#include <openbsc/sgsn.h>
26#include <openbsc/gprs_sgsn.h>
27#include <openbsc/gprs_gmm.h>
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +010028#include <openbsc/gprs_gsup_messages.h>
Jacob Erlbecke8b69682014-11-12 10:12:11 +010029
30#include <openbsc/debug.h>
31
Jacob Erlbeck233715c2014-12-18 12:46:47 +010032#include <netinet/in.h>
33#include <arpa/inet.h>
34
Jacob Erlbeck87972662015-01-08 15:18:39 +010035#define SGSN_SUBSCR_MAX_RETRIES 3
36#define SGSN_SUBSCR_RETRY_INTERVAL 10
37
Jacob Erlbecke8b69682014-11-12 10:12:11 +010038extern void *tall_bsc_ctx;
39
Jacob Erlbeck233715c2014-12-18 12:46:47 +010040static int gsup_read_cb(struct gprs_gsup_client *gsupc, struct msgb *msg);
41
42/* TODO: Some functions are specific to the SGSN, but this file is more general
43 * (it has gprs_* name). Either move these functions elsewhere, split them and
44 * move a part, or replace the gprs_ prefix by sgsn_. The applies to
45 * gprs_subscr_init, gsup_read_cb, and gprs_subscr_tx_gsup_message.
46 */
47
48int gprs_subscr_init(struct sgsn_instance *sgi)
Jacob Erlbecke8b69682014-11-12 10:12:11 +010049{
Jacob Erlbeck233715c2014-12-18 12:46:47 +010050 const char *addr_str;
51
52 if (!sgi->cfg.gsup_server_addr.sin_addr.s_addr)
53 return 0;
54
55 addr_str = inet_ntoa(sgi->cfg.gsup_server_addr.sin_addr);
56
57 sgi->gsup_client = gprs_gsup_client_create(
58 addr_str, sgi->cfg.gsup_server_port,
59 &gsup_read_cb);
60
61 if (!sgi->gsup_client)
62 return -1;
63
64 return 1;
65}
66
67static int gsup_read_cb(struct gprs_gsup_client *gsupc, struct msgb *msg)
68{
69 int rc;
70
71 rc = gprs_subscr_rx_gsup_message(msg);
Jacob Erlbeckedb67a12014-12-19 19:15:55 +010072 msgb_free(msg);
Jacob Erlbeck233715c2014-12-18 12:46:47 +010073 if (rc < 0)
74 return -1;
75
76 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +010077}
78
Jacob Erlbeckfdf8ce52015-01-08 16:23:25 +010079static int check_blocking(
80 struct gsm_subscriber *subscr,
81 enum sgsn_subscriber_proc what)
82{
83 if (subscr->sgsn_data->blocked_by == SGSN_SUBSCR_PROC_NONE ||
84 subscr->sgsn_data->blocked_by == what)
85 return 1;
86
87 return 0;
88}
89
90static void abort_blocking_procedure(struct gsm_subscriber *subscr)
91{
92 /* Best effort, stop retries at least */
93 subscr->sgsn_data->retries = SGSN_SUBSCR_MAX_RETRIES;
94}
95
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +010096static void sgsn_subscriber_timeout_cb(void *subscr_);
97int gprs_subscr_purge(struct gsm_subscriber *subscr);
98
99void gprs_subscr_stop_timer(struct gsm_subscriber *subscr)
100{
101 if (subscr->sgsn_data->timer.data) {
102 osmo_timer_del(&subscr->sgsn_data->timer);
103 subscr->sgsn_data->timer.cb = NULL;
104 OSMO_ASSERT(subscr->sgsn_data->timer.data == subscr);
105 subscr->sgsn_data->timer.data = NULL;
106 subscr_put(subscr);
107 }
108}
109
110void gprs_subscr_start_timer(struct gsm_subscriber *subscr, unsigned seconds)
111{
112 if (!subscr->sgsn_data->timer.data) {
113 subscr->sgsn_data->timer.cb = sgsn_subscriber_timeout_cb;
114 subscr->sgsn_data->timer.data = subscr_get(subscr);
Jacob Erlbeck87972662015-01-08 15:18:39 +0100115 subscr->sgsn_data->retries = 0;
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100116 }
117
118 osmo_timer_schedule(&subscr->sgsn_data->timer, seconds, 0);
119}
120
121static void sgsn_subscriber_timeout_cb(void *subscr_)
122{
123 struct gsm_subscriber *subscr = subscr_;
124
125 LOGGSUBSCRP(LOGL_INFO, subscr,
126 "Expired, deleting subscriber entry\n");
127
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100128 subscr_get(subscr);
129
130 /* Check, whether to cleanup immediately */
Jacob Erlbeck87972662015-01-08 15:18:39 +0100131 if (!(subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE) ||
132 subscr->sgsn_data->retries >= SGSN_SUBSCR_MAX_RETRIES)
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100133 goto force_cleanup;
134
135 /* Send a 'purge MS' message to the HLR */
136 if (gprs_subscr_purge(subscr) < 0)
137 goto force_cleanup;
138
139 /* Purge request has been sent */
140
Jacob Erlbeck87972662015-01-08 15:18:39 +0100141 /* Check, whether purge is still enabled */
142 if (!(subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE))
143 goto force_cleanup;
144
145 /* Make sure this will be tried again if there is no response in time */
146 subscr->sgsn_data->retries += 1;
147 gprs_subscr_start_timer(subscr, SGSN_SUBSCR_RETRY_INTERVAL);
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100148 subscr_put(subscr);
149 return;
150
151force_cleanup:
Jacob Erlbeckfdf8ce52015-01-08 16:23:25 +0100152 /* Make sure to clear blocking */
153 if (check_blocking(subscr, SGSN_SUBSCR_PROC_PURGE))
154 subscr->sgsn_data->blocked_by = SGSN_SUBSCR_PROC_NONE;
155
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100156 /* Make sure, the timer is cleaned up */
157 subscr->keep_in_ram = 0;
158 gprs_subscr_stop_timer(subscr);
159 /* The subscr is freed now, if the timer was the last user */
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100160 subscr_put(subscr);
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100161}
162
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100163static struct sgsn_subscriber_data *sgsn_subscriber_data_alloc(void *ctx)
164{
165 struct sgsn_subscriber_data *sdata;
Jacob Erlbeckb1332b62014-12-08 15:52:00 +0100166 int idx;
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100167
168 sdata = talloc_zero(ctx, struct sgsn_subscriber_data);
169
Jacob Erlbeckb1332b62014-12-08 15:52:00 +0100170 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
171 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
172
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100173 return sdata;
174}
175
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100176struct gsm_subscriber *gprs_subscr_get_or_create(const char *imsi)
177{
178 struct gsm_subscriber *subscr;
179
180 subscr = subscr_get_or_create(NULL, imsi);
181 if (!subscr)
182 return NULL;
183
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100184 if (!subscr->sgsn_data)
185 subscr->sgsn_data = sgsn_subscriber_data_alloc(subscr);
186
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100187 gprs_subscr_stop_timer(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100188
189 return subscr;
190}
191
192struct gsm_subscriber *gprs_subscr_get_by_imsi(const char *imsi)
193{
194 return subscr_active_by_imsi(NULL, imsi);
195}
196
197void gprs_subscr_delete(struct gsm_subscriber *subscr)
198{
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100199 if (subscr->sgsn_data->mm) {
200 subscr_put(subscr->sgsn_data->mm->subscr);
201 subscr->sgsn_data->mm->subscr = NULL;
202 subscr->sgsn_data->mm = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100203 }
204
205 if ((subscr->flags & GPRS_SUBSCRIBER_CANCELLED) ||
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100206 (subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT)) {
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100207 subscr->keep_in_ram = 0;
Jacob Erlbeck9bf4be92015-01-06 16:32:41 +0100208 gprs_subscr_stop_timer(subscr);
209 } else if (sgsn->cfg.subscriber_expiry_timeout != SGSN_TIMEOUT_NEVER) {
210 gprs_subscr_start_timer(subscr, sgsn->cfg.subscriber_expiry_timeout);
211 } else {
212 subscr->keep_in_ram = 1;
213 }
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100214
215 subscr_put(subscr);
216}
217
218void gprs_subscr_put_and_cancel(struct gsm_subscriber *subscr)
219{
220 subscr->authorized = 0;
221 subscr->flags |= GPRS_SUBSCRIBER_CANCELLED;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100222 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100223
224 gprs_subscr_update(subscr);
225
226 gprs_subscr_delete(subscr);
227}
228
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100229static int gprs_subscr_tx_gsup_message(struct gsm_subscriber *subscr,
230 struct gprs_gsup_message *gsup_msg)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100231{
Jacob Erlbeck233715c2014-12-18 12:46:47 +0100232 struct msgb *msg = gprs_gsup_msgb_alloc();
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100233
234 strncpy(gsup_msg->imsi, subscr->imsi, sizeof(gsup_msg->imsi) - 1);
235
236 gprs_gsup_encode(msg, gsup_msg);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100237
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100238 LOGGSUBSCRP(LOGL_INFO, subscr,
239 "Sending GSUP, will send: %s\n", msgb_hexdump(msg));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100240
Jacob Erlbeck233715c2014-12-18 12:46:47 +0100241 if (!sgsn->gsup_client) {
242 msgb_free(msg);
243 return -ENOTSUP;
244 }
245
246 return gprs_gsup_client_send(sgsn->gsup_client, msg);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100247}
248
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100249static int gprs_subscr_handle_gsup_auth_res(struct gsm_subscriber *subscr,
250 struct gprs_gsup_message *gsup_msg)
251{
252 unsigned idx;
253 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
254
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100255 LOGGSUBSCRP(LOGL_INFO, subscr,
256 "Got SendAuthenticationInfoResult, num_auth_tuples = %d\n",
257 gsup_msg->num_auth_tuples);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100258
259 if (gsup_msg->num_auth_tuples > 0) {
260 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
261
262 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
263 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
264 }
265
266 for (idx = 0; idx < gsup_msg->num_auth_tuples; idx++) {
267 size_t key_seq = gsup_msg->auth_tuples[idx].key_seq;
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100268 LOGGSUBSCRP(LOGL_DEBUG, subscr,
269 "Adding auth tuple, cksn = %d\n", key_seq);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100270 if (key_seq >= ARRAY_SIZE(sdata->auth_triplets)) {
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100271 LOGGSUBSCRP(LOGL_NOTICE, subscr,
272 "Skipping auth triplet with invalid cksn %d\n",
273 key_seq);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100274 continue;
275 }
276 sdata->auth_triplets[key_seq] = gsup_msg->auth_tuples[idx];
277 }
278
279 sdata->auth_triplets_updated = 1;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100280 sdata->error_cause = 0;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100281
282 gprs_subscr_update_auth_info(subscr);
283
284 return 0;
285}
286
287static int gprs_subscr_handle_gsup_upd_loc_res(struct gsm_subscriber *subscr,
288 struct gprs_gsup_message *gsup_msg)
289{
290 unsigned idx;
291
292 if (gsup_msg->pdp_info_compl) {
293 LOGP(DGPRS, LOGL_INFO, "Would clear existing PDP info\n");
294
295 /* TODO: clear existing PDP info entries */
296 }
297
298 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
299 struct gprs_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx];
300 size_t ctx_id = pdp_info->context_id;
301
302 LOGP(DGPRS, LOGL_INFO,
303 "Would set PDP info, context id = %d, APN = %s\n",
304 ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len));
305
306 /* TODO: set PDP info [ctx_id] */
307 }
308
309 subscr->authorized = 1;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100310 subscr->sgsn_data->error_cause = 0;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100311
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100312 subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE;
313
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100314 gprs_subscr_update(subscr);
315 return 0;
316}
317
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100318static int check_cause(int cause)
319{
320 switch (cause) {
321 case GMM_CAUSE_IMSI_UNKNOWN ... GMM_CAUSE_ILLEGAL_ME:
322 case GMM_CAUSE_GPRS_NOTALLOWED ... GMM_CAUSE_NO_GPRS_PLMN:
323 return EACCES;
324
325 case GMM_CAUSE_MSC_TEMP_NOTREACH ... GMM_CAUSE_CONGESTION:
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100326 return EHOSTUNREACH;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100327
328 case GMM_CAUSE_SEM_INCORR_MSG ... GMM_CAUSE_PROTO_ERR_UNSPEC:
329 default:
330 return EINVAL;
331 }
332}
333
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100334static int gprs_subscr_handle_gsup_auth_err(struct gsm_subscriber *subscr,
335 struct gprs_gsup_message *gsup_msg)
336{
337 unsigned idx;
338 struct sgsn_subscriber_data *sdata = subscr->sgsn_data;
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100339 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100340
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100341 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100342
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100343 LOGGSUBSCRP(LOGL_DEBUG, subscr,
344 "Send authentication info has failed with cause %d, "
345 "handled as: %s\n",
346 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100347
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100348 switch (cause_err) {
349 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100350 LOGGSUBSCRP(LOGL_NOTICE, subscr,
351 "GPRS send auth info req failed, access denied, "
352 "GMM cause = '%s' (%d)\n",
353 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
354 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100355 /* Clear auth tuples */
356 memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets));
357 for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++)
358 sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100359
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100360 subscr->authorized = 0;
361 sdata->error_cause = gsup_msg->cause;
362 gprs_subscr_update_auth_info(subscr);
363 break;
364
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100365 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100366 LOGGSUBSCRP(LOGL_NOTICE, subscr,
367 "GPRS send auth info req failed, GMM cause = '%s' (%d)\n",
368 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
369 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100370
371 sdata->error_cause = gsup_msg->cause;
372 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100373 break;
374
375 default:
376 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100377 LOGGSUBSCRP(LOGL_ERROR, subscr,
378 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
379 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
380 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100381 break;
382 }
383
384 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100385}
386
387static int gprs_subscr_handle_gsup_upd_loc_err(struct gsm_subscriber *subscr,
388 struct gprs_gsup_message *gsup_msg)
389{
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100390 int cause_err;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100391
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100392 cause_err = check_cause(gsup_msg->cause);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100393
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100394 LOGGSUBSCRP(LOGL_DEBUG, subscr,
395 "Update location has failed with cause %d, handled as: %s\n",
396 gsup_msg->cause, strerror(cause_err));
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100397
398 switch (cause_err) {
399 case EACCES:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100400 LOGGSUBSCRP(LOGL_NOTICE, subscr,
401 "GPRS update location failed, access denied, "
402 "GMM cause = '%s' (%d)\n",
403 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
404 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100405
406 subscr->authorized = 0;
407 subscr->sgsn_data->error_cause = gsup_msg->cause;
408 gprs_subscr_update_auth_info(subscr);
409 break;
410
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100411 case EHOSTUNREACH:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100412 LOGGSUBSCRP(LOGL_NOTICE, subscr,
413 "GPRS update location failed, GMM cause = '%s' (%d)\n",
414 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
415 gsup_msg->cause);
Jacob Erlbecke4dc7fc2015-01-05 16:20:47 +0100416
417 subscr->sgsn_data->error_cause = gsup_msg->cause;
418 gprs_subscr_update_auth_info(subscr);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100419 break;
420
421 default:
422 case EINVAL:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100423 LOGGSUBSCRP(LOGL_ERROR, subscr,
424 "GSUP protocol remote error, GMM cause = '%s' (%d)\n",
425 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
426 gsup_msg->cause);
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100427 break;
428 }
429
430 return -gsup_msg->cause;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100431}
432
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100433static int gprs_subscr_handle_gsup_purge_res(struct gsm_subscriber *subscr,
434 struct gprs_gsup_message *gsup_msg)
435{
436 LOGGSUBSCRP(LOGL_INFO, subscr, "Completing purge MS\n");
437
438 /* Force silent cancellation */
439 subscr->sgsn_data->error_cause = 0;
440 gprs_subscr_put_and_cancel(subscr_get(subscr));
441
442 return 0;
443}
444
445static int gprs_subscr_handle_gsup_purge_err(struct gsm_subscriber *subscr,
446 struct gprs_gsup_message *gsup_msg)
447{
448 LOGGSUBSCRP(LOGL_NOTICE, subscr,
449 "Purge MS has failed with cause '%s' (%d)\n",
450 get_value_string(gsm48_gmm_cause_names, gsup_msg->cause),
451 gsup_msg->cause);
452
453 /* In GSM 09.02, 19.1.4.4, the text and the SDL diagram imply that
454 * the subscriber data is not removed if the request has failed. On the
455 * other hand, keeping the subscriber data in either error case
456 * (subscriber unknown, syntactical message error, connection error)
457 * doesn't seem to give any advantage, since the data will be restored
458 * on the next Attach Request anyway.
459 * This approach ensures, that the subscriber record will not stick if
460 * an error happens.
461 */
462
463 /* TODO: Check whether this behaviour is acceptable and either just
464 * remove this TODO-notice or change the implementation to not delete
465 * the subscriber data (eventually resetting the ENABLE_PURGE flag and
466 * restarting the expiry timer based on the cause).
467 *
468 * Subscriber Unknown: cancel subscr
469 * Temporary network problems: do nothing (handled by timer based retry)
470 * Message problems (syntax, nyi, ...): cancel subscr (retry won't help)
471 */
472
473 gprs_subscr_handle_gsup_purge_res(subscr, gsup_msg);
474
475 return -gsup_msg->cause;
476}
477
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100478int gprs_subscr_rx_gsup_message(struct msgb *msg)
479{
480 uint8_t *data = msgb_l2(msg);
481 size_t data_len = msgb_l2len(msg);
482 int rc = 0;
483
484 struct gprs_gsup_message gsup_msg = {0};
485 struct gsm_subscriber *subscr;
486
487 rc = gprs_gsup_decode(data, data_len, &gsup_msg);
488 if (rc < 0) {
489 LOGP(DGPRS, LOGL_ERROR,
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100490 "decoding GSUP message fails with error '%s' (%d)\n",
491 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100492 return rc;
493 }
494
495 if (!gsup_msg.imsi[0])
496 return -GMM_CAUSE_INV_MAND_INFO;
497
498 if (gsup_msg.message_type == GPRS_GSUP_MSGT_INSERT_DATA_REQUEST)
499 subscr = gprs_subscr_get_or_create(gsup_msg.imsi);
500 else
501 subscr = gprs_subscr_get_by_imsi(gsup_msg.imsi);
502
503 if (!subscr) {
504 LOGP(DGPRS, LOGL_NOTICE,
505 "Unknown IMSI %s, discarding GSUP message\n", gsup_msg.imsi);
506 return -GMM_CAUSE_IMSI_UNKNOWN;
507 }
508
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100509 LOGGSUBSCRP(LOGL_INFO, subscr,
510 "Received GSUP message of type 0x%02x\n", gsup_msg.message_type);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100511
512 switch (gsup_msg.message_type) {
513 case GPRS_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
Jacob Erlbeckd1892d42015-01-05 18:38:41 +0100514 subscr->sgsn_data->error_cause = 0;
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100515 gprs_subscr_put_and_cancel(subscr);
516 subscr = NULL;
517 break;
518
519 case GPRS_GSUP_MSGT_SEND_AUTH_INFO_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100520 rc = gprs_subscr_handle_gsup_auth_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100521 break;
522
523 case GPRS_GSUP_MSGT_SEND_AUTH_INFO_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100524 rc = gprs_subscr_handle_gsup_auth_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100525 break;
526
527 case GPRS_GSUP_MSGT_UPDATE_LOCATION_RESULT:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100528 rc = gprs_subscr_handle_gsup_upd_loc_res(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100529 break;
530
531 case GPRS_GSUP_MSGT_UPDATE_LOCATION_ERROR:
Jacob Erlbeck092bbc82015-01-05 18:57:32 +0100532 rc = gprs_subscr_handle_gsup_upd_loc_err(subscr, &gsup_msg);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100533 break;
534
535 case GPRS_GSUP_MSGT_PURGE_MS_ERROR:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100536 rc = gprs_subscr_handle_gsup_purge_err(subscr, &gsup_msg);
537 break;
538
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100539 case GPRS_GSUP_MSGT_PURGE_MS_RESULT:
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100540 rc = gprs_subscr_handle_gsup_purge_res(subscr, &gsup_msg);
541 break;
542
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100543 case GPRS_GSUP_MSGT_INSERT_DATA_REQUEST:
544 case GPRS_GSUP_MSGT_DELETE_DATA_REQUEST:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100545 LOGGSUBSCRP(LOGL_ERROR, subscr,
546 "Rx GSUP message type %d not yet implemented\n",
547 gsup_msg.message_type);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100548 rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
549 break;
550
551 default:
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100552 LOGGSUBSCRP(LOGL_ERROR, subscr,
553 "Rx GSUP message type %d not valid at SGSN\n",
554 gsup_msg.message_type);
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100555 rc = -GMM_CAUSE_MSGT_INCOMP_P_STATE;
556 break;
557 };
558
559 if (subscr)
560 subscr_put(subscr);
561
562 return rc;
563}
564
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100565int gprs_subscr_purge(struct gsm_subscriber *subscr)
566{
567 struct gprs_gsup_message gsup_msg = {0};
Jacob Erlbeckfdf8ce52015-01-08 16:23:25 +0100568 int rc;
569
570 if (!check_blocking(subscr, SGSN_SUBSCR_PROC_PURGE)) {
571 LOGGSUBSCRP(
572 LOGL_NOTICE, subscr,
573 "Cannot purge MS subscriber, blocked\n");
574 return -EAGAIN;
575 }
576
577 /* GSM 09.02, 19.4.1.4 requires other MAP requests to be blocked until
578 * this procedure is completed
579 */
580 subscr->sgsn_data->blocked_by = SGSN_SUBSCR_PROC_PURGE;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100581
582 LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n");
583
584 gsup_msg.message_type = GPRS_GSUP_MSGT_PURGE_MS_REQUEST;
Jacob Erlbeckfdf8ce52015-01-08 16:23:25 +0100585 rc = gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
586 if (rc < 0)
587 subscr->sgsn_data->blocked_by = SGSN_SUBSCR_PROC_NONE;
588
589 return rc;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100590}
591
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100592int gprs_subscr_query_auth_info(struct gsm_subscriber *subscr)
593{
594 struct gprs_gsup_message gsup_msg = {0};
595
Jacob Erlbeckfdf8ce52015-01-08 16:23:25 +0100596 if (!check_blocking(subscr, SGSN_SUBSCR_PROC_UPD_AUTH)) {
597 LOGGSUBSCRP(
598 LOGL_NOTICE, subscr,
599 "Cannot start update auth info request procedure, blocked\n");
600 abort_blocking_procedure(subscr);
601 return -EAGAIN;
602 }
603
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100604 LOGGSUBSCRP(LOGL_INFO, subscr,
605 "subscriber auth info is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100606
607 gsup_msg.message_type = GPRS_GSUP_MSGT_SEND_AUTH_INFO_REQUEST;
608 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
609}
610
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100611int gprs_subscr_location_update(struct gsm_subscriber *subscr)
612{
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100613 struct gprs_gsup_message gsup_msg = {0};
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100614
Jacob Erlbeckfdf8ce52015-01-08 16:23:25 +0100615 if (!check_blocking(subscr, SGSN_SUBSCR_PROC_UPD_LOC)) {
616 LOGGSUBSCRP(
617 LOGL_NOTICE, subscr,
618 "Cannot start update location procedure, blocked\n");
619 abort_blocking_procedure(subscr);
620 return -EAGAIN;
621 }
622
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100623 LOGGSUBSCRP(LOGL_INFO, subscr,
624 "subscriber data is not available\n");
Jacob Erlbeck5641cfc2014-12-12 15:01:37 +0100625
626 gsup_msg.message_type = GPRS_GSUP_MSGT_UPDATE_LOCATION_REQUEST;
627 return gprs_subscr_tx_gsup_message(subscr, &gsup_msg);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100628}
629
630void gprs_subscr_update(struct gsm_subscriber *subscr)
631{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100632 LOGGSUBSCRP(LOGL_DEBUG, subscr, "Updating subscriber data\n");
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100633
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100634 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100635 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
636
Jacob Erlbeck359cafa2014-12-02 11:28:38 +0100637 sgsn_update_subscriber_data(subscr->sgsn_data->mm, subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100638}
639
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100640void gprs_subscr_update_auth_info(struct gsm_subscriber *subscr)
641{
Jacob Erlbeck387d6d92014-12-23 14:24:16 +0100642 LOGGSUBSCRP(LOGL_DEBUG, subscr,
643 "Updating subscriber authentication info\n");
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100644
645 subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
646 subscr->flags &= ~GSM_SUBSCRIBER_FIRST_CONTACT;
647
648 sgsn_update_subscriber_data(subscr->sgsn_data->mm, subscr);
649}
650
651struct gsm_subscriber *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mmctx)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100652{
653 struct gsm_subscriber *subscr = NULL;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100654
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100655 if (mmctx->subscr)
656 return subscr_get(mmctx->subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100657
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100658 if (mmctx->imsi[0])
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100659 subscr = gprs_subscr_get_by_imsi(mmctx->imsi);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100660
661 if (!subscr) {
662 subscr = gprs_subscr_get_or_create(mmctx->imsi);
663 subscr->flags |= GSM_SUBSCRIBER_FIRST_CONTACT;
Jacob Erlbeckb3982c12015-01-06 16:32:41 +0100664 subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100665 }
666
667 if (strcpy(subscr->equipment.imei, mmctx->imei) != 0) {
668 strncpy(subscr->equipment.imei, mmctx->imei, GSM_IMEI_LENGTH-1);
669 subscr->equipment.imei[GSM_IMEI_LENGTH-1] = 0;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100670 }
671
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100672 if (subscr->lac != mmctx->ra.lac)
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100673 subscr->lac = mmctx->ra.lac;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100674
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100675 subscr->sgsn_data->mm = mmctx;
676 mmctx->subscr = subscr_get(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100677
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100678 return subscr;
679}
680
681int gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx)
682{
683 struct gsm_subscriber *subscr = NULL;
684 int rc;
685
686 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber data update\n");
687
688 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
689
690 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING;
691
692 rc = gprs_subscr_location_update(subscr);
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100693 subscr_put(subscr);
Jacob Erlbeck828059f2014-11-28 14:55:25 +0100694 return rc;
695}
696
697int gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx)
698{
699 struct gsm_subscriber *subscr = NULL;
700 int rc;
701
702 LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber authentication info\n");
703
704 subscr = gprs_subscr_get_or_create_by_mmctx(mmctx);
705
706 subscr->flags |= GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING;
707
708 rc = gprs_subscr_query_auth_info(subscr);
709 subscr_put(subscr);
710 return rc;
Jacob Erlbecke8b69682014-11-12 10:12:11 +0100711}