blob: 711540e039bc6f18f12f1825e06b97917149237c [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 Erlbeck277b71e2015-02-02 18:03:05 +010038#include <openbsc/gprs_utils.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +020039#include "openbsc/gprs_llc.h"
Harald Welteab1d5622010-05-18 19:58:38 +020040
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010041#include <time.h>
42
43#define GPRS_LLME_CHECK_TICK 30
44
Harald Welteab1d5622010-05-18 19:58:38 +020045extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080046
Harald Welted193cb32010-05-17 22:58:03 +020047LLIST_HEAD(sgsn_mm_ctxts);
48LLIST_HEAD(sgsn_ggsn_ctxts);
49LLIST_HEAD(sgsn_apn_ctxts);
50LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080051
Harald Welte8acd88f2010-05-18 10:57:45 +020052static const struct rate_ctr_desc mmctx_ctr_description[] = {
53 { "sign.packets.in", "Signalling Messages ( In)" },
54 { "sign.packets.out", "Signalling Messages (Out)" },
55 { "udata.packets.in", "User Data Messages ( In)" },
56 { "udata.packets.out", "User Data Messages (Out)" },
57 { "udata.bytes.in", "User Data Bytes ( In)" },
58 { "udata.bytes.out", "User Data Bytes (Out)" },
59 { "pdp_ctx_act", "PDP Context Activations " },
60 { "suspend", "SUSPEND Count " },
61 { "paging.ps", "Paging Packet Switched " },
62 { "paging.cs", "Paging Circuit Switched " },
63 { "ra_update", "Routing Area Update " },
64};
65
66static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
67 .group_name_prefix = "sgsn.mmctx",
68 .group_description = "SGSN MM Context Statistics",
69 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
70 .ctr_desc = mmctx_ctr_description,
71};
72
Harald Welteefbdee92010-06-10 00:20:12 +020073static const struct rate_ctr_desc pdpctx_ctr_description[] = {
74 { "udata.packets.in", "User Data Messages ( In)" },
75 { "udata.packets.out", "User Data Messages (Out)" },
76 { "udata.bytes.in", "User Data Bytes ( In)" },
77 { "udata.bytes.out", "User Data Bytes (Out)" },
78};
79
80static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
81 .group_name_prefix = "sgsn.pdpctx",
82 .group_description = "SGSN PDP Context Statistics",
83 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
84 .ctr_desc = pdpctx_ctr_description,
85};
86
Harald Welte9b455bf2010-03-14 15:45:01 +080087static int ra_id_equals(const struct gprs_ra_id *id1,
88 const struct gprs_ra_id *id2)
89{
90 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
91 id1->lac == id2->lac && id1->rac == id2->rac);
92}
93
Harald Weltef6bd3402010-12-23 23:34:43 +010094/* See 03.02 Chapter 2.6 */
95static inline uint32_t tlli_foreign(uint32_t tlli)
96{
97 return ((tlli | 0x80000000) & ~0x40000000);
98}
99
Harald Welte9b455bf2010-03-14 15:45:01 +0800100/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200101struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800102 const struct gprs_ra_id *raid)
103{
104 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +0200105 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800106
107 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
108 if (tlli == ctx->tlli &&
109 ra_id_equals(raid, &ctx->ra))
110 return ctx;
111 }
Harald Welteab1d5622010-05-18 19:58:38 +0200112
113 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100114 switch (tlli_type) {
115 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200116 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200117 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
118 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200119 ctx->tlli = tlli;
120 return ctx;
121 }
122 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100123 break;
124 case TLLI_FOREIGN:
125 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
126 if (tlli == tlli_foreign(ctx->tlli) &&
127 ra_id_equals(raid, &ctx->ra))
128 return ctx;
129 }
130 break;
131 default:
132 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200133 }
134
Harald Welte9b455bf2010-03-14 15:45:01 +0800135 return NULL;
136}
137
Harald Welteeaa614c2010-05-02 11:26:34 +0200138struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800139{
140 struct sgsn_mm_ctx *ctx;
141
142 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200143 if (p_tmsi == ctx->p_tmsi ||
144 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800145 return ctx;
146 }
147 return NULL;
148}
149
150struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
151{
152 struct sgsn_mm_ctx *ctx;
153
154 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
155 if (!strcmp(imsi, ctx->imsi))
156 return ctx;
157 }
158 return NULL;
159
160}
161
162/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200163struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800164 const struct gprs_ra_id *raid)
165{
Harald Welte2720e732010-05-17 00:44:57 +0200166 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800167
Harald Welte2720e732010-05-17 00:44:57 +0200168 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800169 if (!ctx)
170 return NULL;
171
172 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
173 ctx->tlli = tlli;
174 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100175 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8acd88f2010-05-18 10:57:45 +0200176 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200177 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800178
179 llist_add(&ctx->list, &sgsn_mm_ctxts);
180
181 return ctx;
182}
Harald Welted193cb32010-05-17 22:58:03 +0200183
Harald Welte7b022ee2012-07-14 12:04:04 +0200184/* this is a hard _free_ function, it doesn't clean up the PDP contexts
185 * in libgtp! */
Harald Weltec728eea2010-12-24 23:07:18 +0100186void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
187{
188 struct sgsn_pdp_ctx *pdp, *pdp2;
189
Jacob Erlbecke671d252015-01-26 14:43:07 +0100190 /* Unlink from global list of MM contexts */
191 llist_del(&mm->list);
192
193 /* Free all PDP contexts */
194 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
195 sgsn_pdp_ctx_free(pdp);
196
197 rate_ctr_group_free(mm->ctrg);
198
199 talloc_free(mm);
200}
201
202void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
203{
204 struct gprs_llc_llme *llme = mm->llme;
205 uint32_t tlli = mm->tlli;
206 struct sgsn_pdp_ctx *pdp, *pdp2;
207
208 /* delete all existing PDP contexts for this MS */
209 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
210 LOGMMCTXP(LOGL_NOTICE, mm,
211 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
212 sgsn_pdp_ctx_terminate(pdp);
213 }
214
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200215 if (osmo_timer_pending(&mm->timer)) {
216 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
217 osmo_timer_del(&mm->timer);
218 }
219
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100220 /* Detach from subscriber which is possibly freed then */
221 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100222 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100223 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100224 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100225 }
226
Jacob Erlbecke671d252015-01-26 14:43:07 +0100227 sgsn_mm_ctx_free(mm);
228 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100229
Jacob Erlbecke671d252015-01-26 14:43:07 +0100230 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
231 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec728eea2010-12-24 23:07:18 +0100232}
Harald Welte77289c22010-05-18 14:32:29 +0200233
Jacob Erlbecke671d252015-01-26 14:43:07 +0100234
Harald Welte96df6062010-06-03 06:37:26 +0200235/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200236struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
237 uint8_t nsapi)
238{
239 struct sgsn_pdp_ctx *pdp;
240
241 llist_for_each_entry(pdp, &mm->pdp_list, list) {
242 if (pdp->nsapi == nsapi)
243 return pdp;
244 }
245 return NULL;
246}
247
Harald Welte96df6062010-06-03 06:37:26 +0200248/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200249struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
250 uint8_t tid)
251{
252 struct sgsn_pdp_ctx *pdp;
253
254 llist_for_each_entry(pdp, &mm->pdp_list, list) {
255 if (pdp->ti == tid)
256 return pdp;
257 }
258 return NULL;
259}
260
Harald Welte7b022ee2012-07-14 12:04:04 +0200261/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200262struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
263 uint8_t nsapi)
264{
265 struct sgsn_pdp_ctx *pdp;
266
267 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
268 if (pdp)
269 return NULL;
270
271 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
272 if (!pdp)
273 return NULL;
274
275 pdp->mm = mm;
276 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200277 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200278 llist_add(&pdp->list, &mm->pdp_list);
279 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
280
281 return pdp;
282}
283
Harald Weltefdf453c2012-07-14 12:15:19 +0200284#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200285/*
286 * This function will not trigger any GSM DEACT PDP ACK messages, so you
287 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
288 * isn't detached already.
289 */
290void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
291{
292 OSMO_ASSERT(pdp->mm != NULL);
293
294 /* There might still be pending callbacks in libgtp. So the parts of
295 * this object relevant to GTP need to remain intact in this case. */
296
297 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
298
299 /* Force the deactivation of the SNDCP layer */
300 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
301
302 /* Detach from MM context */
303 llist_del(&pdp->list);
304 pdp->mm = NULL;
305
306 sgsn_delete_pdp_ctx(pdp);
307}
308
309/*
310 * Don't call this function directly unless you know what you are doing.
311 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
312 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
313 */
Harald Welted193cb32010-05-17 22:58:03 +0200314void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
315{
Harald Welte376d5e52010-06-28 18:57:21 +0200316 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200317 if (pdp->mm)
318 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200319 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200320
321 /* _if_ we still have a library handle, at least set it to NULL
322 * to avoid any dereferences of the now-deleted PDP context from
323 * sgsn_libgtp:cb_data_ind() */
324 if (pdp->lib) {
325 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200326 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200327 "has a libgtp handle attached to it, this shouldn't "
328 "happen!\n");
329 osmo_generate_backtrace();
330 lib->priv = NULL;
331 }
332
Harald Welted193cb32010-05-17 22:58:03 +0200333 talloc_free(pdp);
334}
335
336/* GGSN contexts */
337
Harald Welte77289c22010-05-18 14:32:29 +0200338struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200339{
Harald Welte77289c22010-05-18 14:32:29 +0200340 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200341
Harald Welte77289c22010-05-18 14:32:29 +0200342 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200343 if (!ggc)
344 return NULL;
345
346 ggc->id = id;
347 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100348 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200349 /* if we are called from config file parse, this gsn doesn't exist yet */
350 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200351 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200352
353 return ggc;
354}
355
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100356void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
357{
358 llist_del(&ggc->list);
359 talloc_free(ggc);
360}
361
Harald Welte77289c22010-05-18 14:32:29 +0200362struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200363{
Harald Welte77289c22010-05-18 14:32:29 +0200364 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200365
366 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
367 if (id == ggc->id)
368 return ggc;
369 }
370 return NULL;
371}
372
Harald Weltea9b473a2010-12-24 21:13:26 +0100373struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
374{
375 struct sgsn_ggsn_ctx *ggc;
376
377 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
378 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
379 return ggc;
380 }
381 return NULL;
382}
383
384
Harald Welte77289c22010-05-18 14:32:29 +0200385struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200386{
Harald Welte77289c22010-05-18 14:32:29 +0200387 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200388
Harald Welte77289c22010-05-18 14:32:29 +0200389 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200390 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200391 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200392 return ggc;
393}
394
395/* APN contexts */
396
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100397static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200398{
399 struct apn_ctx *actx;
400
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100401 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200402 if (!actx)
403 return NULL;
404 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100405 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
406
407 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200408
409 return actx;
410}
411
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100412void sgsn_apn_ctx_free(struct apn_ctx *actx)
413{
414 llist_del(&actx->list);
415 talloc_free(actx);
416}
417
418struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
419{
420 struct apn_ctx *actx;
421 struct apn_ctx *found_actx = NULL;
422 size_t imsi_prio = 0;
423 size_t name_prio = 0;
424 size_t name_req_len = strlen(name);
425
426 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
427 size_t name_ref_len, imsi_ref_len;
428 const char *name_ref_start, *name_match_start;
429
430 imsi_ref_len = strlen(actx->imsi_prefix);
431 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
432 continue;
433
434 if (imsi_ref_len < imsi_prio)
435 continue;
436
437 /* IMSI matches */
438
439 name_ref_start = &actx->name[0];
440 if (name_ref_start[0] == '*') {
441 /* Suffix match */
442 name_ref_start += 1;
443 name_ref_len = strlen(name_ref_start);
444 if (name_ref_len > name_req_len)
445 continue;
446 } else {
447 name_ref_len = strlen(name_ref_start);
448 if (name_ref_len != name_req_len)
449 continue;
450 }
451
452 name_match_start = name + (name_req_len - name_ref_len);
453 if (strcasecmp(name_match_start, name_ref_start) != 0)
454 continue;
455
456 /* IMSI and name match */
457
458 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
459 /* Lower priority, skip */
460 continue;
461
462 imsi_prio = imsi_ref_len;
463 name_prio = name_ref_len;
464 found_actx = actx;
465 }
466 return found_actx;
467}
468
469struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200470{
471 struct apn_ctx *actx;
472
473 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100474 if (strcasecmp(name, actx->name) == 0 &&
475 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200476 return actx;
477 }
478 return NULL;
479}
480
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100481struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200482{
483 struct apn_ctx *actx;
484
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100485 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200486 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100487 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200488
489 return actx;
490}
Harald Welte6463c072010-05-18 17:04:55 +0200491
492uint32_t sgsn_alloc_ptmsi(void)
493{
494 struct sgsn_mm_ctx *mm;
495 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100496 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200497
498restart:
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100499 ptmsi = rand();
500 /* Enforce that the 2 MSB are set without loosing the distance between
501 * identical values. Since rand() has no duplicate values within a
502 * period (because the size of the state is the same like the size of
503 * the random value), this leads to a distance of period/4 when the
504 * distribution of the 2 MSB is uniform. This approach fails with a
505 * probability of (3/4)^max_retries, only 1% of the approaches will
506 * need more than 16 numbers (even distribution assumed).
507 *
508 * Alternatively, a freeze list could be used if another PRNG is used
509 * or when this approach proves to be not sufficient.
510 */
511 if (ptmsi >= 0xC0000000) {
512 if (!max_retries--)
513 goto failed;
514 goto restart;
515 }
516 ptmsi |= 0xC0000000;
517
518 if (ptmsi == GSM_RESERVED_TMSI) {
519 if (!max_retries--)
520 goto failed;
521 goto restart;
522 }
523
Harald Welte6463c072010-05-18 17:04:55 +0200524 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200525 if (mm->p_tmsi == ptmsi) {
526 if (!max_retries--)
527 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200528 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200529 }
Harald Welte6463c072010-05-18 17:04:55 +0200530 }
531
532 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200533
534failed:
535 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
536 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200537}
Harald Weltea9b473a2010-12-24 21:13:26 +0100538
539static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
540{
541 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
542 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
543 else {
544 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200545 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100546 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200547 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100548 sgsn_pdp_ctx_free(pdp);
549 }
550}
551
552/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100553 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100554int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
555{
556 struct sgsn_mm_ctx *mm;
557 int num = 0;
558
559 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
560 struct sgsn_pdp_ctx *pdp;
561 llist_for_each_entry(pdp, &mm->pdp_list, list) {
562 if (pdp->ggsn == ggsn) {
563 drop_one_pdp(pdp);
564 num++;
565 }
566 }
567 }
568
569 return num;
570}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200571
572int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
573{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +0100574 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200575}
576
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100577void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200578{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100579 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100580 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100581
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100582 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200583}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100584
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400585static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
586{
587 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
588 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
589}
590
591/**
592 * The tlv_parsed tp parameter will be modified to insert a
593 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
594 * PDP context handling.
595 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100596struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
597 struct tlv_parsed *tp,
598 enum gsm48_gsm_cause *gsm_cause)
599{
600 char req_apn_str[GSM_APN_LENGTH] = {0};
601 const struct apn_ctx *apn_ctx = NULL;
602 const char *selected_apn_str = NULL;
603 struct sgsn_subscriber_pdp_data *pdp;
604 struct sgsn_ggsn_ctx *ggsn = NULL;
605 int allow_any_apn = 0;
606
607 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
608 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
609 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
610 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
611 return NULL;
612 }
613
614 gprs_apn_to_str(req_apn_str,
615 TLVP_VAL(tp, GSM48_IE_GSM_APN),
616 TLVP_LEN(tp, GSM48_IE_GSM_APN));
617
618 if (strcmp(req_apn_str, "*") == 0)
619 req_apn_str[0] = 0;
620 }
621
622 if (mmctx->subscr == NULL ||
623 llist_empty(&mmctx->subscr->sgsn_data->pdp_list))
624 allow_any_apn = 1;
625
626 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
627 /* No specific APN requested, check for an APN that is both
628 * granted and configured */
629
630 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
631 if (strcmp(pdp->apn_str, "*") == 0)
632 {
633 allow_any_apn = 1;
634 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400635 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100636 continue;
637 }
638 if (!llist_empty(&sgsn_apn_ctxts)) {
639 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
640 /* Not configured */
641 if (apn_ctx == NULL)
642 continue;
643 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400644 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100645 selected_apn_str = pdp->apn_str;
646 break;
647 }
648 } else if (!allow_any_apn) {
649 /* Check whether the given APN is granted */
650 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
651 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400652 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100653 selected_apn_str = req_apn_str;
654 allow_any_apn = 1;
655 continue;
656 }
657 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400658 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100659 selected_apn_str = req_apn_str;
660 break;
661 }
662 }
663 } else if (strlen(req_apn_str) != 0) {
664 /* Any APN is allowed */
665 selected_apn_str = req_apn_str;
666 } else {
667 /* Prefer the GGSN associated with the wildcard APN */
668 selected_apn_str = "";
669 }
670
671 if (!allow_any_apn && selected_apn_str == NULL) {
672 /* Access not granted */
673 LOGMMCTXP(LOGL_NOTICE, mmctx,
674 "The requested APN '%s' is not allowed\n",
675 req_apn_str);
676 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
677 return NULL;
678 }
679
680 if (apn_ctx == NULL && selected_apn_str)
681 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
682
683 if (apn_ctx != NULL) {
684 ggsn = apn_ctx->ggsn;
685 } else if (llist_empty(&sgsn_apn_ctxts)) {
686 /* No configuration -> use GGSN 0 */
687 ggsn = sgsn_ggsn_ctx_by_id(0);
688 } else if (allow_any_apn &&
689 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
690 /* No APN given and no default configuration -> Use GGSN 0 */
691 ggsn = sgsn_ggsn_ctx_by_id(0);
692 } else {
693 /* No matching configuration found */
694 LOGMMCTXP(LOGL_NOTICE, mmctx,
695 "The selected APN '%s' has not been configured\n",
696 selected_apn_str);
697 *gsm_cause = GSM_CAUSE_MISSING_APN;
698 return NULL;
699 }
700
701 LOGMMCTXP(LOGL_INFO, mmctx,
702 "Found GGSN %d for APN '%s' (requested '%s')\n",
703 ggsn->id, selected_apn_str ? selected_apn_str : "---",
704 req_apn_str);
705
706 return ggsn;
707}
708
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100709static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
710{
711 struct sgsn_mm_ctx *mmctx = NULL;
712
713 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
714 if (llme == mmctx->llme) {
715 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
716 return;
717 }
718 }
719
720 /* No MM context found */
721 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
722 llme->tlli);
723 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
724}
725
726static void sgsn_llme_check_cb(void *data_)
727{
728 struct gprs_llc_llme *llme, *llme_tmp;
729 struct timespec now_tp;
730 time_t now, age;
731 time_t max_age = gprs_max_time_to_idle();
732
733 int rc;
734
735 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
736 OSMO_ASSERT(rc >= 0);
737 now = now_tp.tv_sec;
738
739 LOGP(DGPRS, LOGL_DEBUG,
740 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
741
742 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
743 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
744 llme->age_timestamp = now;
745
746 age = now - llme->age_timestamp;
747
748 if (age > max_age || age < 0) {
749 LOGP(DGPRS, LOGL_INFO,
750 "Inactivity timeout for TLLI 0x%08x, age %d\n",
751 llme->tlli, (int)age);
752 sgsn_llme_cleanup_free(llme);
753 }
754 }
755
756 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
757}
758
759void sgsn_inst_init()
760{
761 sgsn->llme_timer.cb = sgsn_llme_check_cb;
762 sgsn->llme_timer.data = NULL;
763
764 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
765}
766