blob: fdf4840b104cfe5071f8f89b4cbea82059851c29 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS SGSN functionality */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * 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
Harald Welte9b455bf2010-03-14 15:45:01 +080010 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080019 *
20 */
21
Harald Welteeaa614c2010-05-02 11:26:34 +020022#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080023
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010024#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/talloc.h>
26#include <osmocom/core/timer.h>
27#include <osmocom/core/rate_ctr.h>
Harald Weltefdf453c2012-07-14 12:15:19 +020028#include <osmocom/core/backtrace.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080029#include <osmocom/gprs/gprs_ns.h>
30#include <osmocom/gprs/gprs_bssgp.h>
31
Harald Welte9b455bf2010-03-14 15:45:01 +080032#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020033#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080034#include <openbsc/gprs_sgsn.h>
Harald Welteab1d5622010-05-18 19:58:38 +020035#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010036#include <openbsc/gsm_04_08_gprs.h>
37#include <openbsc/gprs_gmm.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +020038#include "openbsc/gprs_llc.h"
Harald Welteab1d5622010-05-18 19:58:38 +020039
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010040#include <time.h>
41
42#define GPRS_LLME_CHECK_TICK 30
43
Harald Welteab1d5622010-05-18 19:58:38 +020044extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080045
Harald Welted193cb32010-05-17 22:58:03 +020046LLIST_HEAD(sgsn_mm_ctxts);
47LLIST_HEAD(sgsn_ggsn_ctxts);
48LLIST_HEAD(sgsn_apn_ctxts);
49LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080050
Harald Welte8acd88f2010-05-18 10:57:45 +020051static const struct rate_ctr_desc mmctx_ctr_description[] = {
52 { "sign.packets.in", "Signalling Messages ( In)" },
53 { "sign.packets.out", "Signalling Messages (Out)" },
54 { "udata.packets.in", "User Data Messages ( In)" },
55 { "udata.packets.out", "User Data Messages (Out)" },
56 { "udata.bytes.in", "User Data Bytes ( In)" },
57 { "udata.bytes.out", "User Data Bytes (Out)" },
58 { "pdp_ctx_act", "PDP Context Activations " },
59 { "suspend", "SUSPEND Count " },
60 { "paging.ps", "Paging Packet Switched " },
61 { "paging.cs", "Paging Circuit Switched " },
62 { "ra_update", "Routing Area Update " },
63};
64
65static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
66 .group_name_prefix = "sgsn.mmctx",
67 .group_description = "SGSN MM Context Statistics",
68 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
69 .ctr_desc = mmctx_ctr_description,
70};
71
Harald Welteefbdee92010-06-10 00:20:12 +020072static const struct rate_ctr_desc pdpctx_ctr_description[] = {
73 { "udata.packets.in", "User Data Messages ( In)" },
74 { "udata.packets.out", "User Data Messages (Out)" },
75 { "udata.bytes.in", "User Data Bytes ( In)" },
76 { "udata.bytes.out", "User Data Bytes (Out)" },
77};
78
79static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
80 .group_name_prefix = "sgsn.pdpctx",
81 .group_description = "SGSN PDP Context Statistics",
82 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
83 .ctr_desc = pdpctx_ctr_description,
84};
85
Harald Welte9b455bf2010-03-14 15:45:01 +080086static int ra_id_equals(const struct gprs_ra_id *id1,
87 const struct gprs_ra_id *id2)
88{
89 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
90 id1->lac == id2->lac && id1->rac == id2->rac);
91}
92
Harald Weltef6bd3402010-12-23 23:34:43 +010093/* See 03.02 Chapter 2.6 */
94static inline uint32_t tlli_foreign(uint32_t tlli)
95{
96 return ((tlli | 0x80000000) & ~0x40000000);
97}
98
Harald Welte9b455bf2010-03-14 15:45:01 +080099/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200100struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800101 const struct gprs_ra_id *raid)
102{
103 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +0200104 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800105
106 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
107 if (tlli == ctx->tlli &&
108 ra_id_equals(raid, &ctx->ra))
109 return ctx;
110 }
Harald Welteab1d5622010-05-18 19:58:38 +0200111
112 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100113 switch (tlli_type) {
114 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200115 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200116 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
117 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200118 ctx->tlli = tlli;
119 return ctx;
120 }
121 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100122 break;
123 case TLLI_FOREIGN:
124 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
125 if (tlli == tlli_foreign(ctx->tlli) &&
126 ra_id_equals(raid, &ctx->ra))
127 return ctx;
128 }
129 break;
130 default:
131 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200132 }
133
Harald Welte9b455bf2010-03-14 15:45:01 +0800134 return NULL;
135}
136
Harald Welteeaa614c2010-05-02 11:26:34 +0200137struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800138{
139 struct sgsn_mm_ctx *ctx;
140
141 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200142 if (p_tmsi == ctx->p_tmsi ||
143 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800144 return ctx;
145 }
146 return NULL;
147}
148
149struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
150{
151 struct sgsn_mm_ctx *ctx;
152
153 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
154 if (!strcmp(imsi, ctx->imsi))
155 return ctx;
156 }
157 return NULL;
158
159}
160
161/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200162struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800163 const struct gprs_ra_id *raid)
164{
Harald Welte2720e732010-05-17 00:44:57 +0200165 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800166
Harald Welte2720e732010-05-17 00:44:57 +0200167 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800168 if (!ctx)
169 return NULL;
170
171 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
172 ctx->tlli = tlli;
173 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100174 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8acd88f2010-05-18 10:57:45 +0200175 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200176 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800177
178 llist_add(&ctx->list, &sgsn_mm_ctxts);
179
180 return ctx;
181}
Harald Welted193cb32010-05-17 22:58:03 +0200182
Harald Welte7b022ee2012-07-14 12:04:04 +0200183/* this is a hard _free_ function, it doesn't clean up the PDP contexts
184 * in libgtp! */
Harald Weltec728eea2010-12-24 23:07:18 +0100185void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
186{
187 struct sgsn_pdp_ctx *pdp, *pdp2;
188
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200189 if (osmo_timer_pending(&mm->timer)) {
190 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
191 osmo_timer_del(&mm->timer);
192 }
193
Holger Hans Peter Freythere47d4f62015-01-20 21:14:03 +0100194 /* Unlink from global list of MM contexts */
195 llist_del(&mm->list);
196
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100197 /* Detach from subscriber which is possibly freed then */
198 if (mm->subscr) {
199 struct gsm_subscriber *subscr = mm->subscr;
200 mm->subscr = NULL;
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100201 subscr->sgsn_data->mm = NULL;
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100202 gprs_subscr_delete(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100203 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100204 }
205
Harald Weltec728eea2010-12-24 23:07:18 +0100206 /* Free all PDP contexts */
207 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
208 sgsn_pdp_ctx_free(pdp);
209
210 rate_ctr_group_free(mm->ctrg);
211
212 talloc_free(mm);
213}
Harald Welte77289c22010-05-18 14:32:29 +0200214
Harald Welte96df6062010-06-03 06:37:26 +0200215/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200216struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
217 uint8_t nsapi)
218{
219 struct sgsn_pdp_ctx *pdp;
220
221 llist_for_each_entry(pdp, &mm->pdp_list, list) {
222 if (pdp->nsapi == nsapi)
223 return pdp;
224 }
225 return NULL;
226}
227
Harald Welte96df6062010-06-03 06:37:26 +0200228/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200229struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
230 uint8_t tid)
231{
232 struct sgsn_pdp_ctx *pdp;
233
234 llist_for_each_entry(pdp, &mm->pdp_list, list) {
235 if (pdp->ti == tid)
236 return pdp;
237 }
238 return NULL;
239}
240
Harald Welte7b022ee2012-07-14 12:04:04 +0200241/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200242struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
243 uint8_t nsapi)
244{
245 struct sgsn_pdp_ctx *pdp;
246
247 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
248 if (pdp)
249 return NULL;
250
251 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
252 if (!pdp)
253 return NULL;
254
255 pdp->mm = mm;
256 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200257 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200258 llist_add(&pdp->list, &mm->pdp_list);
259 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
260
261 return pdp;
262}
263
Harald Weltefdf453c2012-07-14 12:15:19 +0200264#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200265/*
266 * This function will not trigger any GSM DEACT PDP ACK messages, so you
267 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
268 * isn't detached already.
269 */
270void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
271{
272 OSMO_ASSERT(pdp->mm != NULL);
273
274 /* There might still be pending callbacks in libgtp. So the parts of
275 * this object relevant to GTP need to remain intact in this case. */
276
277 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
278
279 /* Force the deactivation of the SNDCP layer */
280 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
281
282 /* Detach from MM context */
283 llist_del(&pdp->list);
284 pdp->mm = NULL;
285
286 sgsn_delete_pdp_ctx(pdp);
287}
288
289/*
290 * Don't call this function directly unless you know what you are doing.
291 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
292 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
293 */
Harald Welted193cb32010-05-17 22:58:03 +0200294void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
295{
Harald Welte376d5e52010-06-28 18:57:21 +0200296 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200297 if (pdp->mm)
298 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200299 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200300
301 /* _if_ we still have a library handle, at least set it to NULL
302 * to avoid any dereferences of the now-deleted PDP context from
303 * sgsn_libgtp:cb_data_ind() */
304 if (pdp->lib) {
305 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200306 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200307 "has a libgtp handle attached to it, this shouldn't "
308 "happen!\n");
309 osmo_generate_backtrace();
310 lib->priv = NULL;
311 }
312
Harald Welted193cb32010-05-17 22:58:03 +0200313 talloc_free(pdp);
314}
315
316/* GGSN contexts */
317
Harald Welte77289c22010-05-18 14:32:29 +0200318struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200319{
Harald Welte77289c22010-05-18 14:32:29 +0200320 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200321
Harald Welte77289c22010-05-18 14:32:29 +0200322 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200323 if (!ggc)
324 return NULL;
325
326 ggc->id = id;
327 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100328 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200329 /* if we are called from config file parse, this gsn doesn't exist yet */
330 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200331 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200332
333 return ggc;
334}
335
Harald Welte77289c22010-05-18 14:32:29 +0200336struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200337{
Harald Welte77289c22010-05-18 14:32:29 +0200338 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200339
340 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
341 if (id == ggc->id)
342 return ggc;
343 }
344 return NULL;
345}
346
Harald Weltea9b473a2010-12-24 21:13:26 +0100347struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
348{
349 struct sgsn_ggsn_ctx *ggc;
350
351 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
352 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
353 return ggc;
354 }
355 return NULL;
356}
357
358
Harald Welte77289c22010-05-18 14:32:29 +0200359struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200360{
Harald Welte77289c22010-05-18 14:32:29 +0200361 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200362
Harald Welte77289c22010-05-18 14:32:29 +0200363 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200364 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200365 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200366 return ggc;
367}
368
369/* APN contexts */
370
371#if 0
372struct apn_ctx *apn_ctx_alloc(const char *ap_name)
373{
374 struct apn_ctx *actx;
375
376 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
377 if (!actx)
378 return NULL;
379 actx->name = talloc_strdup(actx, ap_name);
380
381 return actx;
382}
383
384struct apn_ctx *apn_ctx_by_name(const char *name)
385{
386 struct apn_ctx *actx;
387
388 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
389 if (!strcmp(name, actx->name))
390 return actx;
391 }
392 return NULL;
393}
394
395struct apn_ctx *apn_ctx_find_alloc(const char *name)
396{
397 struct apn_ctx *actx;
398
399 actx = apn_ctx_by_name(name);
400 if (!actx)
401 actx = apn_ctx_alloc(name);
402
403 return actx;
404}
405#endif
Harald Welte6463c072010-05-18 17:04:55 +0200406
407uint32_t sgsn_alloc_ptmsi(void)
408{
409 struct sgsn_mm_ctx *mm;
410 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100411 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200412
413restart:
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100414 ptmsi = rand();
415 /* Enforce that the 2 MSB are set without loosing the distance between
416 * identical values. Since rand() has no duplicate values within a
417 * period (because the size of the state is the same like the size of
418 * the random value), this leads to a distance of period/4 when the
419 * distribution of the 2 MSB is uniform. This approach fails with a
420 * probability of (3/4)^max_retries, only 1% of the approaches will
421 * need more than 16 numbers (even distribution assumed).
422 *
423 * Alternatively, a freeze list could be used if another PRNG is used
424 * or when this approach proves to be not sufficient.
425 */
426 if (ptmsi >= 0xC0000000) {
427 if (!max_retries--)
428 goto failed;
429 goto restart;
430 }
431 ptmsi |= 0xC0000000;
432
433 if (ptmsi == GSM_RESERVED_TMSI) {
434 if (!max_retries--)
435 goto failed;
436 goto restart;
437 }
438
Harald Welte6463c072010-05-18 17:04:55 +0200439 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200440 if (mm->p_tmsi == ptmsi) {
441 if (!max_retries--)
442 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200443 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200444 }
Harald Welte6463c072010-05-18 17:04:55 +0200445 }
446
447 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200448
449failed:
450 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
451 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200452}
Harald Weltea9b473a2010-12-24 21:13:26 +0100453
454static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
455{
456 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
457 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
458 else {
459 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200460 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100461 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200462 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100463 sgsn_pdp_ctx_free(pdp);
464 }
465}
466
467/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100468 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100469int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
470{
471 struct sgsn_mm_ctx *mm;
472 int num = 0;
473
474 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
475 struct sgsn_pdp_ctx *pdp;
476 llist_for_each_entry(pdp, &mm->pdp_list, list) {
477 if (pdp->ggsn == ggsn) {
478 drop_one_pdp(pdp);
479 num++;
480 }
481 }
482 }
483
484 return num;
485}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200486
487int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
488{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +0100489 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200490}
491
Jacob Erlbeck33b6dad2014-11-12 10:12:11 +0100492void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx,
493 struct gsm_subscriber *subscr)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200494{
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100495 if (!mmctx && subscr && strlen(subscr->imsi) > 0) {
496 mmctx = sgsn_mm_ctx_by_imsi(subscr->imsi);
497 OSMO_ASSERT(!mmctx || !mmctx->subscr || mmctx->subscr == subscr);
498 }
499
500 if (!mmctx) {
501 LOGP(DMM, LOGL_INFO,
502 "Subscriber data update for unregistered MM context, IMSI %s\n",
503 subscr->imsi);
504 return;
505 }
506
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100507 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100508
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100509 if (!subscr->sgsn_data->mm && !mmctx->subscr) {
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100510 mmctx->subscr = subscr_get(subscr);
Jacob Erlbecka1e03732014-12-02 11:28:38 +0100511 mmctx->subscr->sgsn_data->mm = mmctx;
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100512 }
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200513
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100514 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200515}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100516
517static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
518{
519 struct sgsn_mm_ctx *mmctx = NULL;
520
521 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
522 if (llme == mmctx->llme) {
523 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
524 return;
525 }
526 }
527
528 /* No MM context found */
529 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
530 llme->tlli);
531 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
532}
533
534static void sgsn_llme_check_cb(void *data_)
535{
536 struct gprs_llc_llme *llme, *llme_tmp;
537 struct timespec now_tp;
538 time_t now, age;
539 time_t max_age = gprs_max_time_to_idle();
540
541 int rc;
542
543 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
544 OSMO_ASSERT(rc >= 0);
545 now = now_tp.tv_sec;
546
547 LOGP(DGPRS, LOGL_DEBUG,
548 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
549
550 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
551 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
552 llme->age_timestamp = now;
553
554 age = now - llme->age_timestamp;
555
556 if (age > max_age || age < 0) {
557 LOGP(DGPRS, LOGL_INFO,
558 "Inactivity timeout for TLLI 0x%08x, age %d\n",
559 llme->tlli, (int)age);
560 sgsn_llme_cleanup_free(llme);
561 }
562 }
563
564 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
565}
566
567void sgsn_inst_init()
568{
569 sgsn->llme_timer.cb = sgsn_llme_check_cb;
570 sgsn->llme_timer.data = NULL;
571
572 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
573}
574