blob: 9b11c35108026e6a70c7a2ca82c3d83bb09e5fd7 [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>
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +020039#include <openbsc/signal.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +020040#include "openbsc/gprs_llc.h"
Harald Welteab1d5622010-05-18 19:58:38 +020041
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010042#include <time.h>
43
Daniel Willmann044ce5f2015-10-12 19:36:33 +020044#include <openssl/rand.h>
45
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010046#define GPRS_LLME_CHECK_TICK 30
47
Harald Welteab1d5622010-05-18 19:58:38 +020048extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080049
Harald Welted193cb32010-05-17 22:58:03 +020050LLIST_HEAD(sgsn_mm_ctxts);
51LLIST_HEAD(sgsn_ggsn_ctxts);
52LLIST_HEAD(sgsn_apn_ctxts);
53LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080054
Harald Welte8acd88f2010-05-18 10:57:45 +020055static const struct rate_ctr_desc mmctx_ctr_description[] = {
56 { "sign.packets.in", "Signalling Messages ( In)" },
57 { "sign.packets.out", "Signalling Messages (Out)" },
58 { "udata.packets.in", "User Data Messages ( In)" },
59 { "udata.packets.out", "User Data Messages (Out)" },
60 { "udata.bytes.in", "User Data Bytes ( In)" },
61 { "udata.bytes.out", "User Data Bytes (Out)" },
62 { "pdp_ctx_act", "PDP Context Activations " },
63 { "suspend", "SUSPEND Count " },
64 { "paging.ps", "Paging Packet Switched " },
65 { "paging.cs", "Paging Circuit Switched " },
66 { "ra_update", "Routing Area Update " },
67};
68
69static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
70 .group_name_prefix = "sgsn.mmctx",
71 .group_description = "SGSN MM Context Statistics",
72 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
73 .ctr_desc = mmctx_ctr_description,
74};
75
Harald Welteefbdee92010-06-10 00:20:12 +020076static const struct rate_ctr_desc pdpctx_ctr_description[] = {
77 { "udata.packets.in", "User Data Messages ( In)" },
78 { "udata.packets.out", "User Data Messages (Out)" },
79 { "udata.bytes.in", "User Data Bytes ( In)" },
80 { "udata.bytes.out", "User Data Bytes (Out)" },
81};
82
83static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
84 .group_name_prefix = "sgsn.pdpctx",
85 .group_description = "SGSN PDP Context Statistics",
86 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
87 .ctr_desc = pdpctx_ctr_description,
88};
89
Harald Welte9b455bf2010-03-14 15:45:01 +080090static int ra_id_equals(const struct gprs_ra_id *id1,
91 const struct gprs_ra_id *id2)
92{
93 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
94 id1->lac == id2->lac && id1->rac == id2->rac);
95}
96
Harald Weltef6bd3402010-12-23 23:34:43 +010097/* See 03.02 Chapter 2.6 */
98static inline uint32_t tlli_foreign(uint32_t tlli)
99{
100 return ((tlli | 0x80000000) & ~0x40000000);
101}
102
Harald Welte9b455bf2010-03-14 15:45:01 +0800103/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200104struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800105 const struct gprs_ra_id *raid)
106{
107 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +0200108 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800109
110 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
111 if (tlli == ctx->tlli &&
112 ra_id_equals(raid, &ctx->ra))
113 return ctx;
114 }
Harald Welteab1d5622010-05-18 19:58:38 +0200115
116 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100117 switch (tlli_type) {
118 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200119 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200120 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
121 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200122 ctx->tlli = tlli;
123 return ctx;
124 }
125 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100126 break;
127 case TLLI_FOREIGN:
128 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
129 if (tlli == tlli_foreign(ctx->tlli) &&
130 ra_id_equals(raid, &ctx->ra))
131 return ctx;
132 }
133 break;
134 default:
135 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200136 }
137
Harald Welte9b455bf2010-03-14 15:45:01 +0800138 return NULL;
139}
140
Harald Welteeaa614c2010-05-02 11:26:34 +0200141struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800142{
143 struct sgsn_mm_ctx *ctx;
144
145 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200146 if (p_tmsi == ctx->p_tmsi ||
147 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800148 return ctx;
149 }
150 return NULL;
151}
152
153struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
154{
155 struct sgsn_mm_ctx *ctx;
156
157 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
158 if (!strcmp(imsi, ctx->imsi))
159 return ctx;
160 }
161 return NULL;
162
163}
164
165/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200166struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800167 const struct gprs_ra_id *raid)
168{
Harald Welte2720e732010-05-17 00:44:57 +0200169 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800170
Harald Welte2720e732010-05-17 00:44:57 +0200171 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800172 if (!ctx)
173 return NULL;
174
175 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
176 ctx->tlli = tlli;
177 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100178 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8acd88f2010-05-18 10:57:45 +0200179 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200180 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800181
182 llist_add(&ctx->list, &sgsn_mm_ctxts);
183
184 return ctx;
185}
Harald Welted193cb32010-05-17 22:58:03 +0200186
Harald Welte7b022ee2012-07-14 12:04:04 +0200187/* this is a hard _free_ function, it doesn't clean up the PDP contexts
188 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200189static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100190{
191 struct sgsn_pdp_ctx *pdp, *pdp2;
192
Jacob Erlbecke671d252015-01-26 14:43:07 +0100193 /* Unlink from global list of MM contexts */
194 llist_del(&mm->list);
195
196 /* Free all PDP contexts */
197 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
198 sgsn_pdp_ctx_free(pdp);
199
200 rate_ctr_group_free(mm->ctrg);
201
202 talloc_free(mm);
203}
204
205void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
206{
207 struct gprs_llc_llme *llme = mm->llme;
208 uint32_t tlli = mm->tlli;
209 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200210 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100211
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800212 /* Forget about ongoing look-ups */
213 if (mm->ggsn_lookup) {
214 LOGMMCTXP(LOGL_NOTICE, mm,
215 "Cleaning mmctx with on-going query.\n");
216 mm->ggsn_lookup->mmctx = NULL;
217 mm->ggsn_lookup = NULL;
218 }
219
Jacob Erlbecke671d252015-01-26 14:43:07 +0100220 /* delete all existing PDP contexts for this MS */
221 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
222 LOGMMCTXP(LOGL_NOTICE, mm,
223 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
224 sgsn_pdp_ctx_terminate(pdp);
225 }
226
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200227 if (osmo_timer_pending(&mm->timer)) {
228 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
229 osmo_timer_del(&mm->timer);
230 }
231
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200232 memset(&sig_data, 0, sizeof(sig_data));
233 sig_data.mm = mm;
234 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
235
236
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100237 /* Detach from subscriber which is possibly freed then */
238 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100239 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100240 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100241 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100242 }
243
Jacob Erlbecke671d252015-01-26 14:43:07 +0100244 sgsn_mm_ctx_free(mm);
245 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100246
Jacob Erlbecke671d252015-01-26 14:43:07 +0100247 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
248 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec728eea2010-12-24 23:07:18 +0100249}
Harald Welte77289c22010-05-18 14:32:29 +0200250
Jacob Erlbecke671d252015-01-26 14:43:07 +0100251
Harald Welte96df6062010-06-03 06:37:26 +0200252/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200253struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
254 uint8_t nsapi)
255{
256 struct sgsn_pdp_ctx *pdp;
257
258 llist_for_each_entry(pdp, &mm->pdp_list, list) {
259 if (pdp->nsapi == nsapi)
260 return pdp;
261 }
262 return NULL;
263}
264
Harald Welte96df6062010-06-03 06:37:26 +0200265/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200266struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
267 uint8_t tid)
268{
269 struct sgsn_pdp_ctx *pdp;
270
271 llist_for_each_entry(pdp, &mm->pdp_list, list) {
272 if (pdp->ti == tid)
273 return pdp;
274 }
275 return NULL;
276}
277
Harald Welte7b022ee2012-07-14 12:04:04 +0200278/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200279struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
280 uint8_t nsapi)
281{
282 struct sgsn_pdp_ctx *pdp;
283
284 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
285 if (pdp)
286 return NULL;
287
288 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
289 if (!pdp)
290 return NULL;
291
292 pdp->mm = mm;
293 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200294 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200295 llist_add(&pdp->list, &mm->pdp_list);
296 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
297
298 return pdp;
299}
300
Harald Weltefdf453c2012-07-14 12:15:19 +0200301#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200302/*
303 * This function will not trigger any GSM DEACT PDP ACK messages, so you
304 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
305 * isn't detached already.
306 */
307void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
308{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200309 struct sgsn_signal_data sig_data;
310
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200311 OSMO_ASSERT(pdp->mm != NULL);
312
313 /* There might still be pending callbacks in libgtp. So the parts of
314 * this object relevant to GTP need to remain intact in this case. */
315
316 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
317
318 /* Force the deactivation of the SNDCP layer */
319 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
320
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200321 memset(&sig_data, 0, sizeof(sig_data));
322 sig_data.pdp = pdp;
323 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
324
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200325 /* Detach from MM context */
326 llist_del(&pdp->list);
327 pdp->mm = NULL;
328
329 sgsn_delete_pdp_ctx(pdp);
330}
331
332/*
333 * Don't call this function directly unless you know what you are doing.
334 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
335 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
336 */
Harald Welted193cb32010-05-17 22:58:03 +0200337void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
338{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200339 struct sgsn_signal_data sig_data;
340
341 memset(&sig_data, 0, sizeof(sig_data));
342 sig_data.pdp = pdp;
343 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
344
Harald Welte376d5e52010-06-28 18:57:21 +0200345 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200346 if (pdp->mm)
347 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200348 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200349
350 /* _if_ we still have a library handle, at least set it to NULL
351 * to avoid any dereferences of the now-deleted PDP context from
352 * sgsn_libgtp:cb_data_ind() */
353 if (pdp->lib) {
354 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200355 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200356 "has a libgtp handle attached to it, this shouldn't "
357 "happen!\n");
358 osmo_generate_backtrace();
359 lib->priv = NULL;
360 }
361
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800362 if (pdp->destroy_ggsn)
363 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200364 talloc_free(pdp);
365}
366
367/* GGSN contexts */
368
Harald Welte77289c22010-05-18 14:32:29 +0200369struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200370{
Harald Welte77289c22010-05-18 14:32:29 +0200371 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200372
Harald Welte77289c22010-05-18 14:32:29 +0200373 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200374 if (!ggc)
375 return NULL;
376
377 ggc->id = id;
378 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100379 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200380 /* if we are called from config file parse, this gsn doesn't exist yet */
381 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200382 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200383
384 return ggc;
385}
386
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100387void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
388{
389 llist_del(&ggc->list);
390 talloc_free(ggc);
391}
392
Harald Welte77289c22010-05-18 14:32:29 +0200393struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200394{
Harald Welte77289c22010-05-18 14:32:29 +0200395 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200396
397 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
398 if (id == ggc->id)
399 return ggc;
400 }
401 return NULL;
402}
403
Harald Weltea9b473a2010-12-24 21:13:26 +0100404struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
405{
406 struct sgsn_ggsn_ctx *ggc;
407
408 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
409 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
410 return ggc;
411 }
412 return NULL;
413}
414
415
Harald Welte77289c22010-05-18 14:32:29 +0200416struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200417{
Harald Welte77289c22010-05-18 14:32:29 +0200418 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200419
Harald Welte77289c22010-05-18 14:32:29 +0200420 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200421 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200422 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200423 return ggc;
424}
425
426/* APN contexts */
427
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100428static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200429{
430 struct apn_ctx *actx;
431
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100432 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200433 if (!actx)
434 return NULL;
435 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100436 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
437
438 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200439
440 return actx;
441}
442
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100443void sgsn_apn_ctx_free(struct apn_ctx *actx)
444{
445 llist_del(&actx->list);
446 talloc_free(actx);
447}
448
449struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
450{
451 struct apn_ctx *actx;
452 struct apn_ctx *found_actx = NULL;
453 size_t imsi_prio = 0;
454 size_t name_prio = 0;
455 size_t name_req_len = strlen(name);
456
457 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
458 size_t name_ref_len, imsi_ref_len;
459 const char *name_ref_start, *name_match_start;
460
461 imsi_ref_len = strlen(actx->imsi_prefix);
462 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
463 continue;
464
465 if (imsi_ref_len < imsi_prio)
466 continue;
467
468 /* IMSI matches */
469
470 name_ref_start = &actx->name[0];
471 if (name_ref_start[0] == '*') {
472 /* Suffix match */
473 name_ref_start += 1;
474 name_ref_len = strlen(name_ref_start);
475 if (name_ref_len > name_req_len)
476 continue;
477 } else {
478 name_ref_len = strlen(name_ref_start);
479 if (name_ref_len != name_req_len)
480 continue;
481 }
482
483 name_match_start = name + (name_req_len - name_ref_len);
484 if (strcasecmp(name_match_start, name_ref_start) != 0)
485 continue;
486
487 /* IMSI and name match */
488
489 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
490 /* Lower priority, skip */
491 continue;
492
493 imsi_prio = imsi_ref_len;
494 name_prio = name_ref_len;
495 found_actx = actx;
496 }
497 return found_actx;
498}
499
500struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200501{
502 struct apn_ctx *actx;
503
504 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100505 if (strcasecmp(name, actx->name) == 0 &&
506 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200507 return actx;
508 }
509 return NULL;
510}
511
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100512struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200513{
514 struct apn_ctx *actx;
515
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100516 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200517 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100518 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200519
520 return actx;
521}
Harald Welte6463c072010-05-18 17:04:55 +0200522
523uint32_t sgsn_alloc_ptmsi(void)
524{
525 struct sgsn_mm_ctx *mm;
526 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100527 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200528
529restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200530 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
531 goto failed;
532
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100533 /* Enforce that the 2 MSB are set without loosing the distance between
534 * identical values. Since rand() has no duplicate values within a
535 * period (because the size of the state is the same like the size of
536 * the random value), this leads to a distance of period/4 when the
537 * distribution of the 2 MSB is uniform. This approach fails with a
538 * probability of (3/4)^max_retries, only 1% of the approaches will
539 * need more than 16 numbers (even distribution assumed).
540 *
541 * Alternatively, a freeze list could be used if another PRNG is used
542 * or when this approach proves to be not sufficient.
543 */
544 if (ptmsi >= 0xC0000000) {
545 if (!max_retries--)
546 goto failed;
547 goto restart;
548 }
549 ptmsi |= 0xC0000000;
550
551 if (ptmsi == GSM_RESERVED_TMSI) {
552 if (!max_retries--)
553 goto failed;
554 goto restart;
555 }
556
Harald Welte6463c072010-05-18 17:04:55 +0200557 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200558 if (mm->p_tmsi == ptmsi) {
559 if (!max_retries--)
560 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200561 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200562 }
Harald Welte6463c072010-05-18 17:04:55 +0200563 }
564
565 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200566
567failed:
568 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
569 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200570}
Harald Weltea9b473a2010-12-24 21:13:26 +0100571
572static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
573{
574 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
575 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
576 else {
577 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200578 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100579 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200580 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100581 sgsn_pdp_ctx_free(pdp);
582 }
583}
584
585/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100586 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100587int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
588{
589 struct sgsn_mm_ctx *mm;
590 int num = 0;
591
592 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
593 struct sgsn_pdp_ctx *pdp;
594 llist_for_each_entry(pdp, &mm->pdp_list, list) {
595 if (pdp->ggsn == ggsn) {
596 drop_one_pdp(pdp);
597 num++;
598 }
599 }
600 }
601
602 return num;
603}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200604
605int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
606{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +0100607 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200608}
609
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100610void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200611{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100612 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100613 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100614
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100615 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200616}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100617
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400618static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
619{
620 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
621 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
622}
623
624/**
625 * The tlv_parsed tp parameter will be modified to insert a
626 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
627 * PDP context handling.
628 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100629struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
630 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800631 enum gsm48_gsm_cause *gsm_cause,
632 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100633{
634 char req_apn_str[GSM_APN_LENGTH] = {0};
635 const struct apn_ctx *apn_ctx = NULL;
636 const char *selected_apn_str = NULL;
637 struct sgsn_subscriber_pdp_data *pdp;
638 struct sgsn_ggsn_ctx *ggsn = NULL;
639 int allow_any_apn = 0;
640
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800641 out_apn_str[0] = '\0';
642
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100643 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
644 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
645 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
646 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
647 return NULL;
648 }
649
650 gprs_apn_to_str(req_apn_str,
651 TLVP_VAL(tp, GSM48_IE_GSM_APN),
652 TLVP_LEN(tp, GSM48_IE_GSM_APN));
653
654 if (strcmp(req_apn_str, "*") == 0)
655 req_apn_str[0] = 0;
656 }
657
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800658 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100659 allow_any_apn = 1;
660
661 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
662 /* No specific APN requested, check for an APN that is both
663 * granted and configured */
664
665 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
666 if (strcmp(pdp->apn_str, "*") == 0)
667 {
668 allow_any_apn = 1;
669 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400670 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100671 continue;
672 }
673 if (!llist_empty(&sgsn_apn_ctxts)) {
674 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
675 /* Not configured */
676 if (apn_ctx == NULL)
677 continue;
678 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400679 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100680 selected_apn_str = pdp->apn_str;
681 break;
682 }
683 } else if (!allow_any_apn) {
684 /* Check whether the given APN is granted */
685 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
686 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400687 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100688 selected_apn_str = req_apn_str;
689 allow_any_apn = 1;
690 continue;
691 }
692 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400693 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100694 selected_apn_str = req_apn_str;
695 break;
696 }
697 }
698 } else if (strlen(req_apn_str) != 0) {
699 /* Any APN is allowed */
700 selected_apn_str = req_apn_str;
701 } else {
702 /* Prefer the GGSN associated with the wildcard APN */
703 selected_apn_str = "";
704 }
705
706 if (!allow_any_apn && selected_apn_str == NULL) {
707 /* Access not granted */
708 LOGMMCTXP(LOGL_NOTICE, mmctx,
709 "The requested APN '%s' is not allowed\n",
710 req_apn_str);
711 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
712 return NULL;
713 }
714
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800715 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200716 if (selected_apn_str)
717 strcpy(out_apn_str, selected_apn_str);
718 else
719 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800720
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100721 if (apn_ctx == NULL && selected_apn_str)
722 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
723
724 if (apn_ctx != NULL) {
725 ggsn = apn_ctx->ggsn;
726 } else if (llist_empty(&sgsn_apn_ctxts)) {
727 /* No configuration -> use GGSN 0 */
728 ggsn = sgsn_ggsn_ctx_by_id(0);
729 } else if (allow_any_apn &&
730 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
731 /* No APN given and no default configuration -> Use GGSN 0 */
732 ggsn = sgsn_ggsn_ctx_by_id(0);
733 } else {
734 /* No matching configuration found */
735 LOGMMCTXP(LOGL_NOTICE, mmctx,
736 "The selected APN '%s' has not been configured\n",
737 selected_apn_str);
738 *gsm_cause = GSM_CAUSE_MISSING_APN;
739 return NULL;
740 }
741
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800742 if (!ggsn) {
743 LOGMMCTXP(LOGL_NOTICE, mmctx,
744 "No static GGSN configured. Selected APN '%s'\n",
745 selected_apn_str);
746 return NULL;
747 }
748
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100749 LOGMMCTXP(LOGL_INFO, mmctx,
750 "Found GGSN %d for APN '%s' (requested '%s')\n",
751 ggsn->id, selected_apn_str ? selected_apn_str : "---",
752 req_apn_str);
753
754 return ggsn;
755}
756
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100757static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
758{
759 struct sgsn_mm_ctx *mmctx = NULL;
760
761 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
762 if (llme == mmctx->llme) {
763 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
764 return;
765 }
766 }
767
768 /* No MM context found */
769 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
770 llme->tlli);
771 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
772}
773
774static void sgsn_llme_check_cb(void *data_)
775{
776 struct gprs_llc_llme *llme, *llme_tmp;
777 struct timespec now_tp;
778 time_t now, age;
779 time_t max_age = gprs_max_time_to_idle();
780
781 int rc;
782
783 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
784 OSMO_ASSERT(rc >= 0);
785 now = now_tp.tv_sec;
786
787 LOGP(DGPRS, LOGL_DEBUG,
788 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
789
790 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
791 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
792 llme->age_timestamp = now;
793
794 age = now - llme->age_timestamp;
795
796 if (age > max_age || age < 0) {
797 LOGP(DGPRS, LOGL_INFO,
798 "Inactivity timeout for TLLI 0x%08x, age %d\n",
799 llme->tlli, (int)age);
800 sgsn_llme_cleanup_free(llme);
801 }
802 }
803
804 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
805}
806
807void sgsn_inst_init()
808{
809 sgsn->llme_timer.cb = sgsn_llme_check_cb;
810 sgsn->llme_timer.data = NULL;
811
812 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
813}
814