blob: ad90d3c164a34bb95b9f028425672dbc0591799e [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
44#define GPRS_LLME_CHECK_TICK 30
45
Harald Welteab1d5622010-05-18 19:58:38 +020046extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080047
Harald Welted193cb32010-05-17 22:58:03 +020048LLIST_HEAD(sgsn_mm_ctxts);
49LLIST_HEAD(sgsn_ggsn_ctxts);
50LLIST_HEAD(sgsn_apn_ctxts);
51LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080052
Harald Welte8acd88f2010-05-18 10:57:45 +020053static const struct rate_ctr_desc mmctx_ctr_description[] = {
54 { "sign.packets.in", "Signalling Messages ( In)" },
55 { "sign.packets.out", "Signalling Messages (Out)" },
56 { "udata.packets.in", "User Data Messages ( In)" },
57 { "udata.packets.out", "User Data Messages (Out)" },
58 { "udata.bytes.in", "User Data Bytes ( In)" },
59 { "udata.bytes.out", "User Data Bytes (Out)" },
60 { "pdp_ctx_act", "PDP Context Activations " },
61 { "suspend", "SUSPEND Count " },
62 { "paging.ps", "Paging Packet Switched " },
63 { "paging.cs", "Paging Circuit Switched " },
64 { "ra_update", "Routing Area Update " },
65};
66
67static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
68 .group_name_prefix = "sgsn.mmctx",
69 .group_description = "SGSN MM Context Statistics",
70 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
71 .ctr_desc = mmctx_ctr_description,
72};
73
Harald Welteefbdee92010-06-10 00:20:12 +020074static const struct rate_ctr_desc pdpctx_ctr_description[] = {
75 { "udata.packets.in", "User Data Messages ( In)" },
76 { "udata.packets.out", "User Data Messages (Out)" },
77 { "udata.bytes.in", "User Data Bytes ( In)" },
78 { "udata.bytes.out", "User Data Bytes (Out)" },
79};
80
81static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
82 .group_name_prefix = "sgsn.pdpctx",
83 .group_description = "SGSN PDP Context Statistics",
84 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
85 .ctr_desc = pdpctx_ctr_description,
86};
87
Harald Welte9b455bf2010-03-14 15:45:01 +080088static int ra_id_equals(const struct gprs_ra_id *id1,
89 const struct gprs_ra_id *id2)
90{
91 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
92 id1->lac == id2->lac && id1->rac == id2->rac);
93}
94
Harald Weltef6bd3402010-12-23 23:34:43 +010095/* See 03.02 Chapter 2.6 */
96static inline uint32_t tlli_foreign(uint32_t tlli)
97{
98 return ((tlli | 0x80000000) & ~0x40000000);
99}
100
Harald Welte9b455bf2010-03-14 15:45:01 +0800101/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200102struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800103 const struct gprs_ra_id *raid)
104{
105 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +0200106 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800107
108 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
109 if (tlli == ctx->tlli &&
110 ra_id_equals(raid, &ctx->ra))
111 return ctx;
112 }
Harald Welteab1d5622010-05-18 19:58:38 +0200113
114 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100115 switch (tlli_type) {
116 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200117 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200118 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
119 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200120 ctx->tlli = tlli;
121 return ctx;
122 }
123 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100124 break;
125 case TLLI_FOREIGN:
126 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
127 if (tlli == tlli_foreign(ctx->tlli) &&
128 ra_id_equals(raid, &ctx->ra))
129 return ctx;
130 }
131 break;
132 default:
133 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200134 }
135
Harald Welte9b455bf2010-03-14 15:45:01 +0800136 return NULL;
137}
138
Harald Welteeaa614c2010-05-02 11:26:34 +0200139struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800140{
141 struct sgsn_mm_ctx *ctx;
142
143 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200144 if (p_tmsi == ctx->p_tmsi ||
145 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800146 return ctx;
147 }
148 return NULL;
149}
150
151struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
152{
153 struct sgsn_mm_ctx *ctx;
154
155 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
156 if (!strcmp(imsi, ctx->imsi))
157 return ctx;
158 }
159 return NULL;
160
161}
162
163/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200164struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800165 const struct gprs_ra_id *raid)
166{
Harald Welte2720e732010-05-17 00:44:57 +0200167 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800168
Harald Welte2720e732010-05-17 00:44:57 +0200169 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800170 if (!ctx)
171 return NULL;
172
173 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
174 ctx->tlli = tlli;
175 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100176 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8acd88f2010-05-18 10:57:45 +0200177 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200178 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800179
180 llist_add(&ctx->list, &sgsn_mm_ctxts);
181
182 return ctx;
183}
Harald Welted193cb32010-05-17 22:58:03 +0200184
Harald Welte7b022ee2012-07-14 12:04:04 +0200185/* this is a hard _free_ function, it doesn't clean up the PDP contexts
186 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200187static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100188{
189 struct sgsn_pdp_ctx *pdp, *pdp2;
190
Jacob Erlbecke671d252015-01-26 14:43:07 +0100191 /* Unlink from global list of MM contexts */
192 llist_del(&mm->list);
193
194 /* Free all PDP contexts */
195 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
196 sgsn_pdp_ctx_free(pdp);
197
198 rate_ctr_group_free(mm->ctrg);
199
200 talloc_free(mm);
201}
202
203void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
204{
205 struct gprs_llc_llme *llme = mm->llme;
206 uint32_t tlli = mm->tlli;
207 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200208 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100209
210 /* delete all existing PDP contexts for this MS */
211 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
212 LOGMMCTXP(LOGL_NOTICE, mm,
213 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
214 sgsn_pdp_ctx_terminate(pdp);
215 }
216
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200217 if (osmo_timer_pending(&mm->timer)) {
218 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
219 osmo_timer_del(&mm->timer);
220 }
221
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200222 memset(&sig_data, 0, sizeof(sig_data));
223 sig_data.mm = mm;
224 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
225
226
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100227 /* Detach from subscriber which is possibly freed then */
228 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100229 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100230 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100231 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100232 }
233
Jacob Erlbecke671d252015-01-26 14:43:07 +0100234 sgsn_mm_ctx_free(mm);
235 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100236
Jacob Erlbecke671d252015-01-26 14:43:07 +0100237 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
238 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec728eea2010-12-24 23:07:18 +0100239}
Harald Welte77289c22010-05-18 14:32:29 +0200240
Jacob Erlbecke671d252015-01-26 14:43:07 +0100241
Harald Welte96df6062010-06-03 06:37:26 +0200242/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200243struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
244 uint8_t nsapi)
245{
246 struct sgsn_pdp_ctx *pdp;
247
248 llist_for_each_entry(pdp, &mm->pdp_list, list) {
249 if (pdp->nsapi == nsapi)
250 return pdp;
251 }
252 return NULL;
253}
254
Harald Welte96df6062010-06-03 06:37:26 +0200255/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200256struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
257 uint8_t tid)
258{
259 struct sgsn_pdp_ctx *pdp;
260
261 llist_for_each_entry(pdp, &mm->pdp_list, list) {
262 if (pdp->ti == tid)
263 return pdp;
264 }
265 return NULL;
266}
267
Harald Welte7b022ee2012-07-14 12:04:04 +0200268/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200269struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
270 uint8_t nsapi)
271{
272 struct sgsn_pdp_ctx *pdp;
273
274 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
275 if (pdp)
276 return NULL;
277
278 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
279 if (!pdp)
280 return NULL;
281
282 pdp->mm = mm;
283 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200284 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200285 llist_add(&pdp->list, &mm->pdp_list);
286 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
287
288 return pdp;
289}
290
Harald Weltefdf453c2012-07-14 12:15:19 +0200291#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200292/*
293 * This function will not trigger any GSM DEACT PDP ACK messages, so you
294 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
295 * isn't detached already.
296 */
297void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
298{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200299 struct sgsn_signal_data sig_data;
300
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200301 OSMO_ASSERT(pdp->mm != NULL);
302
303 /* There might still be pending callbacks in libgtp. So the parts of
304 * this object relevant to GTP need to remain intact in this case. */
305
306 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
307
308 /* Force the deactivation of the SNDCP layer */
309 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
310
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200311 memset(&sig_data, 0, sizeof(sig_data));
312 sig_data.pdp = pdp;
313 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
314
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200315 /* Detach from MM context */
316 llist_del(&pdp->list);
317 pdp->mm = NULL;
318
319 sgsn_delete_pdp_ctx(pdp);
320}
321
322/*
323 * Don't call this function directly unless you know what you are doing.
324 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
325 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
326 */
Harald Welted193cb32010-05-17 22:58:03 +0200327void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
328{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200329 struct sgsn_signal_data sig_data;
330
331 memset(&sig_data, 0, sizeof(sig_data));
332 sig_data.pdp = pdp;
333 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
334
Harald Welte376d5e52010-06-28 18:57:21 +0200335 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200336 if (pdp->mm)
337 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200338 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200339
340 /* _if_ we still have a library handle, at least set it to NULL
341 * to avoid any dereferences of the now-deleted PDP context from
342 * sgsn_libgtp:cb_data_ind() */
343 if (pdp->lib) {
344 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200345 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200346 "has a libgtp handle attached to it, this shouldn't "
347 "happen!\n");
348 osmo_generate_backtrace();
349 lib->priv = NULL;
350 }
351
Harald Welted193cb32010-05-17 22:58:03 +0200352 talloc_free(pdp);
353}
354
355/* GGSN contexts */
356
Harald Welte77289c22010-05-18 14:32:29 +0200357struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200358{
Harald Welte77289c22010-05-18 14:32:29 +0200359 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200360
Harald Welte77289c22010-05-18 14:32:29 +0200361 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200362 if (!ggc)
363 return NULL;
364
365 ggc->id = id;
366 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100367 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200368 /* if we are called from config file parse, this gsn doesn't exist yet */
369 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200370 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200371
372 return ggc;
373}
374
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100375void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
376{
377 llist_del(&ggc->list);
378 talloc_free(ggc);
379}
380
Harald Welte77289c22010-05-18 14:32:29 +0200381struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200382{
Harald Welte77289c22010-05-18 14:32:29 +0200383 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200384
385 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
386 if (id == ggc->id)
387 return ggc;
388 }
389 return NULL;
390}
391
Harald Weltea9b473a2010-12-24 21:13:26 +0100392struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
393{
394 struct sgsn_ggsn_ctx *ggc;
395
396 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
397 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
398 return ggc;
399 }
400 return NULL;
401}
402
403
Harald Welte77289c22010-05-18 14:32:29 +0200404struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200405{
Harald Welte77289c22010-05-18 14:32:29 +0200406 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200407
Harald Welte77289c22010-05-18 14:32:29 +0200408 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200409 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200410 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200411 return ggc;
412}
413
414/* APN contexts */
415
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100416static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200417{
418 struct apn_ctx *actx;
419
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100420 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200421 if (!actx)
422 return NULL;
423 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100424 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
425
426 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200427
428 return actx;
429}
430
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100431void sgsn_apn_ctx_free(struct apn_ctx *actx)
432{
433 llist_del(&actx->list);
434 talloc_free(actx);
435}
436
437struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
438{
439 struct apn_ctx *actx;
440 struct apn_ctx *found_actx = NULL;
441 size_t imsi_prio = 0;
442 size_t name_prio = 0;
443 size_t name_req_len = strlen(name);
444
445 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
446 size_t name_ref_len, imsi_ref_len;
447 const char *name_ref_start, *name_match_start;
448
449 imsi_ref_len = strlen(actx->imsi_prefix);
450 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
451 continue;
452
453 if (imsi_ref_len < imsi_prio)
454 continue;
455
456 /* IMSI matches */
457
458 name_ref_start = &actx->name[0];
459 if (name_ref_start[0] == '*') {
460 /* Suffix match */
461 name_ref_start += 1;
462 name_ref_len = strlen(name_ref_start);
463 if (name_ref_len > name_req_len)
464 continue;
465 } else {
466 name_ref_len = strlen(name_ref_start);
467 if (name_ref_len != name_req_len)
468 continue;
469 }
470
471 name_match_start = name + (name_req_len - name_ref_len);
472 if (strcasecmp(name_match_start, name_ref_start) != 0)
473 continue;
474
475 /* IMSI and name match */
476
477 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
478 /* Lower priority, skip */
479 continue;
480
481 imsi_prio = imsi_ref_len;
482 name_prio = name_ref_len;
483 found_actx = actx;
484 }
485 return found_actx;
486}
487
488struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200489{
490 struct apn_ctx *actx;
491
492 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100493 if (strcasecmp(name, actx->name) == 0 &&
494 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200495 return actx;
496 }
497 return NULL;
498}
499
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100500struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200501{
502 struct apn_ctx *actx;
503
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100504 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200505 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100506 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200507
508 return actx;
509}
Harald Welte6463c072010-05-18 17:04:55 +0200510
511uint32_t sgsn_alloc_ptmsi(void)
512{
513 struct sgsn_mm_ctx *mm;
514 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100515 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200516
517restart:
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100518 ptmsi = rand();
519 /* Enforce that the 2 MSB are set without loosing the distance between
520 * identical values. Since rand() has no duplicate values within a
521 * period (because the size of the state is the same like the size of
522 * the random value), this leads to a distance of period/4 when the
523 * distribution of the 2 MSB is uniform. This approach fails with a
524 * probability of (3/4)^max_retries, only 1% of the approaches will
525 * need more than 16 numbers (even distribution assumed).
526 *
527 * Alternatively, a freeze list could be used if another PRNG is used
528 * or when this approach proves to be not sufficient.
529 */
530 if (ptmsi >= 0xC0000000) {
531 if (!max_retries--)
532 goto failed;
533 goto restart;
534 }
535 ptmsi |= 0xC0000000;
536
537 if (ptmsi == GSM_RESERVED_TMSI) {
538 if (!max_retries--)
539 goto failed;
540 goto restart;
541 }
542
Harald Welte6463c072010-05-18 17:04:55 +0200543 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200544 if (mm->p_tmsi == ptmsi) {
545 if (!max_retries--)
546 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200547 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200548 }
Harald Welte6463c072010-05-18 17:04:55 +0200549 }
550
551 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200552
553failed:
554 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
555 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200556}
Harald Weltea9b473a2010-12-24 21:13:26 +0100557
558static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
559{
560 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
561 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
562 else {
563 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200564 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100565 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200566 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100567 sgsn_pdp_ctx_free(pdp);
568 }
569}
570
571/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100572 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100573int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
574{
575 struct sgsn_mm_ctx *mm;
576 int num = 0;
577
578 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
579 struct sgsn_pdp_ctx *pdp;
580 llist_for_each_entry(pdp, &mm->pdp_list, list) {
581 if (pdp->ggsn == ggsn) {
582 drop_one_pdp(pdp);
583 num++;
584 }
585 }
586 }
587
588 return num;
589}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200590
591int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
592{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +0100593 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200594}
595
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100596void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200597{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100598 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100599 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100600
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100601 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200602}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100603
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400604static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
605{
606 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
607 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
608}
609
610/**
611 * The tlv_parsed tp parameter will be modified to insert a
612 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
613 * PDP context handling.
614 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100615struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
616 struct tlv_parsed *tp,
617 enum gsm48_gsm_cause *gsm_cause)
618{
619 char req_apn_str[GSM_APN_LENGTH] = {0};
620 const struct apn_ctx *apn_ctx = NULL;
621 const char *selected_apn_str = NULL;
622 struct sgsn_subscriber_pdp_data *pdp;
623 struct sgsn_ggsn_ctx *ggsn = NULL;
624 int allow_any_apn = 0;
625
626 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
627 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
628 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
629 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
630 return NULL;
631 }
632
633 gprs_apn_to_str(req_apn_str,
634 TLVP_VAL(tp, GSM48_IE_GSM_APN),
635 TLVP_LEN(tp, GSM48_IE_GSM_APN));
636
637 if (strcmp(req_apn_str, "*") == 0)
638 req_apn_str[0] = 0;
639 }
640
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800641 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100642 allow_any_apn = 1;
643
644 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
645 /* No specific APN requested, check for an APN that is both
646 * granted and configured */
647
648 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
649 if (strcmp(pdp->apn_str, "*") == 0)
650 {
651 allow_any_apn = 1;
652 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400653 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100654 continue;
655 }
656 if (!llist_empty(&sgsn_apn_ctxts)) {
657 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
658 /* Not configured */
659 if (apn_ctx == NULL)
660 continue;
661 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400662 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100663 selected_apn_str = pdp->apn_str;
664 break;
665 }
666 } else if (!allow_any_apn) {
667 /* Check whether the given APN is granted */
668 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
669 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400670 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100671 selected_apn_str = req_apn_str;
672 allow_any_apn = 1;
673 continue;
674 }
675 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400676 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100677 selected_apn_str = req_apn_str;
678 break;
679 }
680 }
681 } else if (strlen(req_apn_str) != 0) {
682 /* Any APN is allowed */
683 selected_apn_str = req_apn_str;
684 } else {
685 /* Prefer the GGSN associated with the wildcard APN */
686 selected_apn_str = "";
687 }
688
689 if (!allow_any_apn && selected_apn_str == NULL) {
690 /* Access not granted */
691 LOGMMCTXP(LOGL_NOTICE, mmctx,
692 "The requested APN '%s' is not allowed\n",
693 req_apn_str);
694 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
695 return NULL;
696 }
697
698 if (apn_ctx == NULL && selected_apn_str)
699 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
700
701 if (apn_ctx != NULL) {
702 ggsn = apn_ctx->ggsn;
703 } else if (llist_empty(&sgsn_apn_ctxts)) {
704 /* No configuration -> use GGSN 0 */
705 ggsn = sgsn_ggsn_ctx_by_id(0);
706 } else if (allow_any_apn &&
707 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
708 /* No APN given and no default configuration -> Use GGSN 0 */
709 ggsn = sgsn_ggsn_ctx_by_id(0);
710 } else {
711 /* No matching configuration found */
712 LOGMMCTXP(LOGL_NOTICE, mmctx,
713 "The selected APN '%s' has not been configured\n",
714 selected_apn_str);
715 *gsm_cause = GSM_CAUSE_MISSING_APN;
716 return NULL;
717 }
718
719 LOGMMCTXP(LOGL_INFO, mmctx,
720 "Found GGSN %d for APN '%s' (requested '%s')\n",
721 ggsn->id, selected_apn_str ? selected_apn_str : "---",
722 req_apn_str);
723
724 return ggsn;
725}
726
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100727static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
728{
729 struct sgsn_mm_ctx *mmctx = NULL;
730
731 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
732 if (llme == mmctx->llme) {
733 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
734 return;
735 }
736 }
737
738 /* No MM context found */
739 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
740 llme->tlli);
741 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
742}
743
744static void sgsn_llme_check_cb(void *data_)
745{
746 struct gprs_llc_llme *llme, *llme_tmp;
747 struct timespec now_tp;
748 time_t now, age;
749 time_t max_age = gprs_max_time_to_idle();
750
751 int rc;
752
753 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
754 OSMO_ASSERT(rc >= 0);
755 now = now_tp.tv_sec;
756
757 LOGP(DGPRS, LOGL_DEBUG,
758 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
759
760 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
761 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
762 llme->age_timestamp = now;
763
764 age = now - llme->age_timestamp;
765
766 if (age > max_age || age < 0) {
767 LOGP(DGPRS, LOGL_INFO,
768 "Inactivity timeout for TLLI 0x%08x, age %d\n",
769 llme->tlli, (int)age);
770 sgsn_llme_cleanup_free(llme);
771 }
772 }
773
774 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
775}
776
777void sgsn_inst_init()
778{
779 sgsn->llme_timer.cb = sgsn_llme_check_cb;
780 sgsn->llme_timer.data = NULL;
781
782 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
783}
784