blob: c4dc9d7ec5fca850cb9b53a1a354319f53572536 [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>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010028#include <osmocom/core/stats.h>
Harald Weltefdf453c2012-07-14 12:15:19 +020029#include <osmocom/core/backtrace.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080030#include <osmocom/gprs/gprs_ns.h>
31#include <osmocom/gprs/gprs_bssgp.h>
32
Harald Welte9b455bf2010-03-14 15:45:01 +080033#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020034#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080035#include <openbsc/gprs_sgsn.h>
Harald Welteab1d5622010-05-18 19:58:38 +020036#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010037#include <openbsc/gsm_04_08_gprs.h>
38#include <openbsc/gprs_gmm.h>
Jacob Erlbeck277b71e2015-02-02 18:03:05 +010039#include <openbsc/gprs_utils.h>
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +020040#include <openbsc/signal.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +020041#include "openbsc/gprs_llc.h"
Harald Welteab1d5622010-05-18 19:58:38 +020042
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010043#include <time.h>
44
Daniel Willmann044ce5f2015-10-12 19:36:33 +020045#include <openssl/rand.h>
46
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010047#define GPRS_LLME_CHECK_TICK 30
48
Harald Welteab1d5622010-05-18 19:58:38 +020049extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080050
Harald Welted193cb32010-05-17 22:58:03 +020051LLIST_HEAD(sgsn_mm_ctxts);
52LLIST_HEAD(sgsn_ggsn_ctxts);
53LLIST_HEAD(sgsn_apn_ctxts);
54LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080055
Harald Welte8acd88f2010-05-18 10:57:45 +020056static const struct rate_ctr_desc mmctx_ctr_description[] = {
57 { "sign.packets.in", "Signalling Messages ( In)" },
58 { "sign.packets.out", "Signalling Messages (Out)" },
59 { "udata.packets.in", "User Data Messages ( In)" },
60 { "udata.packets.out", "User Data Messages (Out)" },
61 { "udata.bytes.in", "User Data Bytes ( In)" },
62 { "udata.bytes.out", "User Data Bytes (Out)" },
63 { "pdp_ctx_act", "PDP Context Activations " },
64 { "suspend", "SUSPEND Count " },
65 { "paging.ps", "Paging Packet Switched " },
66 { "paging.cs", "Paging Circuit Switched " },
67 { "ra_update", "Routing Area Update " },
68};
69
70static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
71 .group_name_prefix = "sgsn.mmctx",
72 .group_description = "SGSN MM Context Statistics",
73 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
74 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010075 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020076};
77
Harald Welteefbdee92010-06-10 00:20:12 +020078static const struct rate_ctr_desc pdpctx_ctr_description[] = {
79 { "udata.packets.in", "User Data Messages ( In)" },
80 { "udata.packets.out", "User Data Messages (Out)" },
81 { "udata.bytes.in", "User Data Bytes ( In)" },
82 { "udata.bytes.out", "User Data Bytes (Out)" },
83};
84
85static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
86 .group_name_prefix = "sgsn.pdpctx",
87 .group_description = "SGSN PDP Context Statistics",
88 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
89 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010090 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +020091};
92
Harald Welte9b455bf2010-03-14 15:45:01 +080093static int ra_id_equals(const struct gprs_ra_id *id1,
94 const struct gprs_ra_id *id2)
95{
96 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
97 id1->lac == id2->lac && id1->rac == id2->rac);
98}
99
Harald Weltef6bd3402010-12-23 23:34:43 +0100100/* See 03.02 Chapter 2.6 */
101static inline uint32_t tlli_foreign(uint32_t tlli)
102{
103 return ((tlli | 0x80000000) & ~0x40000000);
104}
105
Harald Welte9b455bf2010-03-14 15:45:01 +0800106/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200107struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800108 const struct gprs_ra_id *raid)
109{
110 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +0200111 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800112
113 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
114 if (tlli == ctx->tlli &&
115 ra_id_equals(raid, &ctx->ra))
116 return ctx;
117 }
Harald Welteab1d5622010-05-18 19:58:38 +0200118
119 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100120 switch (tlli_type) {
121 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200122 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200123 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
124 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200125 ctx->tlli = tlli;
126 return ctx;
127 }
128 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100129 break;
130 case TLLI_FOREIGN:
131 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
132 if (tlli == tlli_foreign(ctx->tlli) &&
133 ra_id_equals(raid, &ctx->ra))
134 return ctx;
135 }
136 break;
137 default:
138 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200139 }
140
Harald Welte9b455bf2010-03-14 15:45:01 +0800141 return NULL;
142}
143
Harald Welteeaa614c2010-05-02 11:26:34 +0200144struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800145{
146 struct sgsn_mm_ctx *ctx;
147
148 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200149 if (p_tmsi == ctx->p_tmsi ||
150 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800151 return ctx;
152 }
153 return NULL;
154}
155
156struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
157{
158 struct sgsn_mm_ctx *ctx;
159
160 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
161 if (!strcmp(imsi, ctx->imsi))
162 return ctx;
163 }
164 return NULL;
165
166}
167
168/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200169struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800170 const struct gprs_ra_id *raid)
171{
Harald Welte2720e732010-05-17 00:44:57 +0200172 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800173
Harald Welte2720e732010-05-17 00:44:57 +0200174 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800175 if (!ctx)
176 return NULL;
177
178 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
179 ctx->tlli = tlli;
180 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100181 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8acd88f2010-05-18 10:57:45 +0200182 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200183 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800184
185 llist_add(&ctx->list, &sgsn_mm_ctxts);
186
187 return ctx;
188}
Harald Welted193cb32010-05-17 22:58:03 +0200189
Harald Welte7b022ee2012-07-14 12:04:04 +0200190/* this is a hard _free_ function, it doesn't clean up the PDP contexts
191 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200192static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100193{
194 struct sgsn_pdp_ctx *pdp, *pdp2;
195
Jacob Erlbecke671d252015-01-26 14:43:07 +0100196 /* Unlink from global list of MM contexts */
197 llist_del(&mm->list);
198
199 /* Free all PDP contexts */
200 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
201 sgsn_pdp_ctx_free(pdp);
202
203 rate_ctr_group_free(mm->ctrg);
204
205 talloc_free(mm);
206}
207
208void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
209{
210 struct gprs_llc_llme *llme = mm->llme;
211 uint32_t tlli = mm->tlli;
212 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200213 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100214
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800215 /* Forget about ongoing look-ups */
216 if (mm->ggsn_lookup) {
217 LOGMMCTXP(LOGL_NOTICE, mm,
218 "Cleaning mmctx with on-going query.\n");
219 mm->ggsn_lookup->mmctx = NULL;
220 mm->ggsn_lookup = NULL;
221 }
222
Jacob Erlbecke671d252015-01-26 14:43:07 +0100223 /* delete all existing PDP contexts for this MS */
224 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
225 LOGMMCTXP(LOGL_NOTICE, mm,
226 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
227 sgsn_pdp_ctx_terminate(pdp);
228 }
229
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200230 if (osmo_timer_pending(&mm->timer)) {
231 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
232 osmo_timer_del(&mm->timer);
233 }
234
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200235 memset(&sig_data, 0, sizeof(sig_data));
236 sig_data.mm = mm;
237 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
238
239
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100240 /* Detach from subscriber which is possibly freed then */
241 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100242 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100243 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100244 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100245 }
246
Jacob Erlbecke671d252015-01-26 14:43:07 +0100247 sgsn_mm_ctx_free(mm);
248 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100249
Jacob Erlbecke671d252015-01-26 14:43:07 +0100250 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
251 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec728eea2010-12-24 23:07:18 +0100252}
Harald Welte77289c22010-05-18 14:32:29 +0200253
Jacob Erlbecke671d252015-01-26 14:43:07 +0100254
Harald Welte96df6062010-06-03 06:37:26 +0200255/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200256struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
257 uint8_t nsapi)
258{
259 struct sgsn_pdp_ctx *pdp;
260
261 llist_for_each_entry(pdp, &mm->pdp_list, list) {
262 if (pdp->nsapi == nsapi)
263 return pdp;
264 }
265 return NULL;
266}
267
Harald Welte96df6062010-06-03 06:37:26 +0200268/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200269struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
270 uint8_t tid)
271{
272 struct sgsn_pdp_ctx *pdp;
273
274 llist_for_each_entry(pdp, &mm->pdp_list, list) {
275 if (pdp->ti == tid)
276 return pdp;
277 }
278 return NULL;
279}
280
Harald Welte7b022ee2012-07-14 12:04:04 +0200281/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200282struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
283 uint8_t nsapi)
284{
285 struct sgsn_pdp_ctx *pdp;
286
287 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
288 if (pdp)
289 return NULL;
290
291 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
292 if (!pdp)
293 return NULL;
294
295 pdp->mm = mm;
296 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200297 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200298 llist_add(&pdp->list, &mm->pdp_list);
299 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
300
301 return pdp;
302}
303
Harald Weltefdf453c2012-07-14 12:15:19 +0200304#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200305/*
306 * This function will not trigger any GSM DEACT PDP ACK messages, so you
307 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
308 * isn't detached already.
309 */
310void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
311{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200312 struct sgsn_signal_data sig_data;
313
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200314 OSMO_ASSERT(pdp->mm != NULL);
315
316 /* There might still be pending callbacks in libgtp. So the parts of
317 * this object relevant to GTP need to remain intact in this case. */
318
319 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
320
321 /* Force the deactivation of the SNDCP layer */
322 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
323
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200324 memset(&sig_data, 0, sizeof(sig_data));
325 sig_data.pdp = pdp;
326 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
327
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200328 /* Detach from MM context */
329 llist_del(&pdp->list);
330 pdp->mm = NULL;
331
332 sgsn_delete_pdp_ctx(pdp);
333}
334
335/*
336 * Don't call this function directly unless you know what you are doing.
337 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
338 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
339 */
Harald Welted193cb32010-05-17 22:58:03 +0200340void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
341{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200342 struct sgsn_signal_data sig_data;
343
344 memset(&sig_data, 0, sizeof(sig_data));
345 sig_data.pdp = pdp;
346 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
347
Harald Welte376d5e52010-06-28 18:57:21 +0200348 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200349 if (pdp->mm)
350 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200351 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200352
353 /* _if_ we still have a library handle, at least set it to NULL
354 * to avoid any dereferences of the now-deleted PDP context from
355 * sgsn_libgtp:cb_data_ind() */
356 if (pdp->lib) {
357 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200358 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200359 "has a libgtp handle attached to it, this shouldn't "
360 "happen!\n");
361 osmo_generate_backtrace();
362 lib->priv = NULL;
363 }
364
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800365 if (pdp->destroy_ggsn)
366 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200367 talloc_free(pdp);
368}
369
370/* GGSN contexts */
371
Harald Welte77289c22010-05-18 14:32:29 +0200372struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200373{
Harald Welte77289c22010-05-18 14:32:29 +0200374 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200375
Harald Welte77289c22010-05-18 14:32:29 +0200376 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200377 if (!ggc)
378 return NULL;
379
380 ggc->id = id;
381 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100382 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200383 /* if we are called from config file parse, this gsn doesn't exist yet */
384 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200385 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200386
387 return ggc;
388}
389
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100390void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
391{
392 llist_del(&ggc->list);
393 talloc_free(ggc);
394}
395
Harald Welte77289c22010-05-18 14:32:29 +0200396struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200397{
Harald Welte77289c22010-05-18 14:32:29 +0200398 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200399
400 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
401 if (id == ggc->id)
402 return ggc;
403 }
404 return NULL;
405}
406
Harald Weltea9b473a2010-12-24 21:13:26 +0100407struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
408{
409 struct sgsn_ggsn_ctx *ggc;
410
411 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
412 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
413 return ggc;
414 }
415 return NULL;
416}
417
418
Harald Welte77289c22010-05-18 14:32:29 +0200419struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200420{
Harald Welte77289c22010-05-18 14:32:29 +0200421 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200422
Harald Welte77289c22010-05-18 14:32:29 +0200423 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200424 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200425 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200426 return ggc;
427}
428
429/* APN contexts */
430
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100431static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200432{
433 struct apn_ctx *actx;
434
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100435 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200436 if (!actx)
437 return NULL;
438 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100439 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
440
441 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200442
443 return actx;
444}
445
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100446void sgsn_apn_ctx_free(struct apn_ctx *actx)
447{
448 llist_del(&actx->list);
449 talloc_free(actx);
450}
451
452struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
453{
454 struct apn_ctx *actx;
455 struct apn_ctx *found_actx = NULL;
456 size_t imsi_prio = 0;
457 size_t name_prio = 0;
458 size_t name_req_len = strlen(name);
459
460 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
461 size_t name_ref_len, imsi_ref_len;
462 const char *name_ref_start, *name_match_start;
463
464 imsi_ref_len = strlen(actx->imsi_prefix);
465 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
466 continue;
467
468 if (imsi_ref_len < imsi_prio)
469 continue;
470
471 /* IMSI matches */
472
473 name_ref_start = &actx->name[0];
474 if (name_ref_start[0] == '*') {
475 /* Suffix match */
476 name_ref_start += 1;
477 name_ref_len = strlen(name_ref_start);
478 if (name_ref_len > name_req_len)
479 continue;
480 } else {
481 name_ref_len = strlen(name_ref_start);
482 if (name_ref_len != name_req_len)
483 continue;
484 }
485
486 name_match_start = name + (name_req_len - name_ref_len);
487 if (strcasecmp(name_match_start, name_ref_start) != 0)
488 continue;
489
490 /* IMSI and name match */
491
492 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
493 /* Lower priority, skip */
494 continue;
495
496 imsi_prio = imsi_ref_len;
497 name_prio = name_ref_len;
498 found_actx = actx;
499 }
500 return found_actx;
501}
502
503struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200504{
505 struct apn_ctx *actx;
506
507 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100508 if (strcasecmp(name, actx->name) == 0 &&
509 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200510 return actx;
511 }
512 return NULL;
513}
514
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100515struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200516{
517 struct apn_ctx *actx;
518
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100519 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200520 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100521 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200522
523 return actx;
524}
Harald Welte6463c072010-05-18 17:04:55 +0200525
526uint32_t sgsn_alloc_ptmsi(void)
527{
528 struct sgsn_mm_ctx *mm;
529 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100530 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200531
532restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200533 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
534 goto failed;
535
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100536 /* Enforce that the 2 MSB are set without loosing the distance between
537 * identical values. Since rand() has no duplicate values within a
538 * period (because the size of the state is the same like the size of
539 * the random value), this leads to a distance of period/4 when the
540 * distribution of the 2 MSB is uniform. This approach fails with a
541 * probability of (3/4)^max_retries, only 1% of the approaches will
542 * need more than 16 numbers (even distribution assumed).
543 *
544 * Alternatively, a freeze list could be used if another PRNG is used
545 * or when this approach proves to be not sufficient.
546 */
547 if (ptmsi >= 0xC0000000) {
548 if (!max_retries--)
549 goto failed;
550 goto restart;
551 }
552 ptmsi |= 0xC0000000;
553
554 if (ptmsi == GSM_RESERVED_TMSI) {
555 if (!max_retries--)
556 goto failed;
557 goto restart;
558 }
559
Harald Welte6463c072010-05-18 17:04:55 +0200560 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200561 if (mm->p_tmsi == ptmsi) {
562 if (!max_retries--)
563 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200564 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200565 }
Harald Welte6463c072010-05-18 17:04:55 +0200566 }
567
568 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200569
570failed:
571 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
572 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200573}
Harald Weltea9b473a2010-12-24 21:13:26 +0100574
575static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
576{
577 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
578 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
579 else {
580 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200581 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100582 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200583 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100584 sgsn_pdp_ctx_free(pdp);
585 }
586}
587
588/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100589 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100590int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
591{
592 struct sgsn_mm_ctx *mm;
593 int num = 0;
594
595 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
596 struct sgsn_pdp_ctx *pdp;
597 llist_for_each_entry(pdp, &mm->pdp_list, list) {
598 if (pdp->ggsn == ggsn) {
599 drop_one_pdp(pdp);
600 num++;
601 }
602 }
603 }
604
605 return num;
606}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200607
608int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
609{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +0100610 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200611}
612
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100613void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200614{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100615 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100616 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100617
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100618 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200619}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100620
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400621static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
622{
623 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
624 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
625}
626
627/**
628 * The tlv_parsed tp parameter will be modified to insert a
629 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
630 * PDP context handling.
631 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100632struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
633 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800634 enum gsm48_gsm_cause *gsm_cause,
635 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100636{
637 char req_apn_str[GSM_APN_LENGTH] = {0};
638 const struct apn_ctx *apn_ctx = NULL;
639 const char *selected_apn_str = NULL;
640 struct sgsn_subscriber_pdp_data *pdp;
641 struct sgsn_ggsn_ctx *ggsn = NULL;
642 int allow_any_apn = 0;
643
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800644 out_apn_str[0] = '\0';
645
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100646 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
647 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
648 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
649 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
650 return NULL;
651 }
652
653 gprs_apn_to_str(req_apn_str,
654 TLVP_VAL(tp, GSM48_IE_GSM_APN),
655 TLVP_LEN(tp, GSM48_IE_GSM_APN));
656
657 if (strcmp(req_apn_str, "*") == 0)
658 req_apn_str[0] = 0;
659 }
660
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800661 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100662 allow_any_apn = 1;
663
664 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
665 /* No specific APN requested, check for an APN that is both
666 * granted and configured */
667
668 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
669 if (strcmp(pdp->apn_str, "*") == 0)
670 {
671 allow_any_apn = 1;
672 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400673 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100674 continue;
675 }
676 if (!llist_empty(&sgsn_apn_ctxts)) {
677 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
678 /* Not configured */
679 if (apn_ctx == NULL)
680 continue;
681 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400682 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100683 selected_apn_str = pdp->apn_str;
684 break;
685 }
686 } else if (!allow_any_apn) {
687 /* Check whether the given APN is granted */
688 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
689 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400690 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100691 selected_apn_str = req_apn_str;
692 allow_any_apn = 1;
693 continue;
694 }
695 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400696 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100697 selected_apn_str = req_apn_str;
698 break;
699 }
700 }
701 } else if (strlen(req_apn_str) != 0) {
702 /* Any APN is allowed */
703 selected_apn_str = req_apn_str;
704 } else {
705 /* Prefer the GGSN associated with the wildcard APN */
706 selected_apn_str = "";
707 }
708
709 if (!allow_any_apn && selected_apn_str == NULL) {
710 /* Access not granted */
711 LOGMMCTXP(LOGL_NOTICE, mmctx,
712 "The requested APN '%s' is not allowed\n",
713 req_apn_str);
714 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
715 return NULL;
716 }
717
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800718 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200719 if (selected_apn_str)
720 strcpy(out_apn_str, selected_apn_str);
721 else
722 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800723
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100724 if (apn_ctx == NULL && selected_apn_str)
725 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
726
727 if (apn_ctx != NULL) {
728 ggsn = apn_ctx->ggsn;
729 } else if (llist_empty(&sgsn_apn_ctxts)) {
730 /* No configuration -> use GGSN 0 */
731 ggsn = sgsn_ggsn_ctx_by_id(0);
732 } else if (allow_any_apn &&
733 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
734 /* No APN given and no default configuration -> Use GGSN 0 */
735 ggsn = sgsn_ggsn_ctx_by_id(0);
736 } else {
737 /* No matching configuration found */
738 LOGMMCTXP(LOGL_NOTICE, mmctx,
739 "The selected APN '%s' has not been configured\n",
740 selected_apn_str);
741 *gsm_cause = GSM_CAUSE_MISSING_APN;
742 return NULL;
743 }
744
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800745 if (!ggsn) {
746 LOGMMCTXP(LOGL_NOTICE, mmctx,
747 "No static GGSN configured. Selected APN '%s'\n",
748 selected_apn_str);
749 return NULL;
750 }
751
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100752 LOGMMCTXP(LOGL_INFO, mmctx,
753 "Found GGSN %d for APN '%s' (requested '%s')\n",
754 ggsn->id, selected_apn_str ? selected_apn_str : "---",
755 req_apn_str);
756
757 return ggsn;
758}
759
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100760static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
761{
762 struct sgsn_mm_ctx *mmctx = NULL;
763
764 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
765 if (llme == mmctx->llme) {
766 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
767 return;
768 }
769 }
770
771 /* No MM context found */
772 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
773 llme->tlli);
774 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
775}
776
777static void sgsn_llme_check_cb(void *data_)
778{
779 struct gprs_llc_llme *llme, *llme_tmp;
780 struct timespec now_tp;
781 time_t now, age;
782 time_t max_age = gprs_max_time_to_idle();
783
784 int rc;
785
786 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
787 OSMO_ASSERT(rc >= 0);
788 now = now_tp.tv_sec;
789
790 LOGP(DGPRS, LOGL_DEBUG,
791 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
792
793 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
794 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
795 llme->age_timestamp = now;
796
797 age = now - llme->age_timestamp;
798
799 if (age > max_age || age < 0) {
800 LOGP(DGPRS, LOGL_INFO,
801 "Inactivity timeout for TLLI 0x%08x, age %d\n",
802 llme->tlli, (int)age);
803 sgsn_llme_cleanup_free(llme);
804 }
805 }
806
807 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
808}
809
810void sgsn_inst_init()
811{
812 sgsn->llme_timer.cb = sgsn_llme_check_cb;
813 sgsn->llme_timer.data = NULL;
814
815 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
816}
817