blob: 8bb68509ee16937bda896d2a7085c298dda039ee [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>
Harald Welte53373bc2016-04-20 17:11:43 +020032#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080033
Harald Welte9b455bf2010-03-14 15:45:01 +080034#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020035#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080036#include <openbsc/gprs_sgsn.h>
Harald Welteab1d5622010-05-18 19:58:38 +020037#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010038#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 +080093/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020094struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080095 const struct gprs_ra_id *raid)
96{
97 struct sgsn_mm_ctx *ctx;
98
99 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100100 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100101 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800102 return ctx;
103 }
Harald Welteab1d5622010-05-18 19:58:38 +0200104
Harald Welte9b455bf2010-03-14 15:45:01 +0800105 return NULL;
106}
107
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100108struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
109 const struct gprs_ra_id *raid)
110{
111 struct sgsn_mm_ctx *ctx;
112 int tlli_type;
113
114 /* TODO: Also check the P_TMSI signature to be safe. That signature
115 * should be different (at least with a sufficiently high probability)
116 * after SGSN restarts and for multiple SGSN instances.
117 */
118
119 tlli_type = gprs_tlli_type(tlli);
120 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
121 return NULL;
122
123 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
124 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
125 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
126 gprs_ra_id_equals(raid, &ctx->ra))
127 return ctx;
128 }
129
130 return NULL;
131}
132
Harald Welteeaa614c2010-05-02 11:26:34 +0200133struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800134{
135 struct sgsn_mm_ctx *ctx;
136
137 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200138 if (p_tmsi == ctx->p_tmsi ||
139 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800140 return ctx;
141 }
142 return NULL;
143}
144
145struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
146{
147 struct sgsn_mm_ctx *ctx;
148
149 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
150 if (!strcmp(imsi, ctx->imsi))
151 return ctx;
152 }
153 return NULL;
154
155}
156
157/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200158struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800159 const struct gprs_ra_id *raid)
160{
Harald Welte2720e732010-05-17 00:44:57 +0200161 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800162
Harald Welte2720e732010-05-17 00:44:57 +0200163 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800164 if (!ctx)
165 return NULL;
166
167 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100168 ctx->ran_type = MM_CTX_T_GERAN_Gb;
169 ctx->gb.tlli = tlli;
Harald Welte9b455bf2010-03-14 15:45:01 +0800170 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100171 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8acd88f2010-05-18 10:57:45 +0200172 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200173 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800174
175 llist_add(&ctx->list, &sgsn_mm_ctxts);
176
177 return ctx;
178}
Harald Welted193cb32010-05-17 22:58:03 +0200179
Harald Welte7b022ee2012-07-14 12:04:04 +0200180/* this is a hard _free_ function, it doesn't clean up the PDP contexts
181 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200182static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100183{
184 struct sgsn_pdp_ctx *pdp, *pdp2;
185
Jacob Erlbecke671d252015-01-26 14:43:07 +0100186 /* Unlink from global list of MM contexts */
187 llist_del(&mm->list);
188
189 /* Free all PDP contexts */
190 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
191 sgsn_pdp_ctx_free(pdp);
192
193 rate_ctr_group_free(mm->ctrg);
194
195 talloc_free(mm);
196}
197
198void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
199{
Harald Weltef97ee042015-12-25 19:12:21 +0100200 struct gprs_llc_llme *llme = mm->gb.llme;
201 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100202 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200203 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100204
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800205 /* Forget about ongoing look-ups */
206 if (mm->ggsn_lookup) {
207 LOGMMCTXP(LOGL_NOTICE, mm,
208 "Cleaning mmctx with on-going query.\n");
209 mm->ggsn_lookup->mmctx = NULL;
210 mm->ggsn_lookup = NULL;
211 }
212
Jacob Erlbecke671d252015-01-26 14:43:07 +0100213 /* delete all existing PDP contexts for this MS */
214 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
215 LOGMMCTXP(LOGL_NOTICE, mm,
216 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
217 sgsn_pdp_ctx_terminate(pdp);
218 }
219
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200220 if (osmo_timer_pending(&mm->timer)) {
221 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
222 osmo_timer_del(&mm->timer);
223 }
224
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200225 memset(&sig_data, 0, sizeof(sig_data));
226 sig_data.mm = mm;
227 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
228
229
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100230 /* Detach from subscriber which is possibly freed then */
231 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100232 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100233 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100234 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100235 }
236
Jacob Erlbecke671d252015-01-26 14:43:07 +0100237 sgsn_mm_ctx_free(mm);
238 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100239
Jacob Erlbecke671d252015-01-26 14:43:07 +0100240 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
241 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec728eea2010-12-24 23:07:18 +0100242}
Harald Welte77289c22010-05-18 14:32:29 +0200243
Jacob Erlbecke671d252015-01-26 14:43:07 +0100244
Harald Welte96df6062010-06-03 06:37:26 +0200245/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200246struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
247 uint8_t nsapi)
248{
249 struct sgsn_pdp_ctx *pdp;
250
251 llist_for_each_entry(pdp, &mm->pdp_list, list) {
252 if (pdp->nsapi == nsapi)
253 return pdp;
254 }
255 return NULL;
256}
257
Harald Welte96df6062010-06-03 06:37:26 +0200258/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200259struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
260 uint8_t tid)
261{
262 struct sgsn_pdp_ctx *pdp;
263
264 llist_for_each_entry(pdp, &mm->pdp_list, list) {
265 if (pdp->ti == tid)
266 return pdp;
267 }
268 return NULL;
269}
270
Harald Welte7b022ee2012-07-14 12:04:04 +0200271/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200272struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
273 uint8_t nsapi)
274{
275 struct sgsn_pdp_ctx *pdp;
276
277 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
278 if (pdp)
279 return NULL;
280
281 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
282 if (!pdp)
283 return NULL;
284
285 pdp->mm = mm;
286 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200287 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200288 llist_add(&pdp->list, &mm->pdp_list);
289 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
290
291 return pdp;
292}
293
Harald Weltefdf453c2012-07-14 12:15:19 +0200294#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200295/*
296 * This function will not trigger any GSM DEACT PDP ACK messages, so you
297 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
298 * isn't detached already.
299 */
300void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
301{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200302 struct sgsn_signal_data sig_data;
303
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200304 OSMO_ASSERT(pdp->mm != NULL);
305
306 /* There might still be pending callbacks in libgtp. So the parts of
307 * this object relevant to GTP need to remain intact in this case. */
308
309 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
310
311 /* Force the deactivation of the SNDCP layer */
Harald Weltef97ee042015-12-25 19:12:21 +0100312 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200313
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200314 memset(&sig_data, 0, sizeof(sig_data));
315 sig_data.pdp = pdp;
316 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
317
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200318 /* Detach from MM context */
319 llist_del(&pdp->list);
320 pdp->mm = NULL;
321
322 sgsn_delete_pdp_ctx(pdp);
323}
324
325/*
326 * Don't call this function directly unless you know what you are doing.
327 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
328 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
329 */
Harald Welted193cb32010-05-17 22:58:03 +0200330void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
331{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200332 struct sgsn_signal_data sig_data;
333
334 memset(&sig_data, 0, sizeof(sig_data));
335 sig_data.pdp = pdp;
336 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
337
Harald Welte376d5e52010-06-28 18:57:21 +0200338 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200339 if (pdp->mm)
340 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200341 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200342
343 /* _if_ we still have a library handle, at least set it to NULL
344 * to avoid any dereferences of the now-deleted PDP context from
345 * sgsn_libgtp:cb_data_ind() */
346 if (pdp->lib) {
347 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200348 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200349 "has a libgtp handle attached to it, this shouldn't "
350 "happen!\n");
351 osmo_generate_backtrace();
352 lib->priv = NULL;
353 }
354
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800355 if (pdp->destroy_ggsn)
356 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200357 talloc_free(pdp);
358}
359
360/* GGSN contexts */
361
Harald Welte77289c22010-05-18 14:32:29 +0200362struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200363{
Harald Welte77289c22010-05-18 14:32:29 +0200364 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200365
Harald Welte77289c22010-05-18 14:32:29 +0200366 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200367 if (!ggc)
368 return NULL;
369
370 ggc->id = id;
371 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100372 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200373 /* if we are called from config file parse, this gsn doesn't exist yet */
374 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200375 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200376
377 return ggc;
378}
379
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100380void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
381{
382 llist_del(&ggc->list);
383 talloc_free(ggc);
384}
385
Harald Welte77289c22010-05-18 14:32:29 +0200386struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200387{
Harald Welte77289c22010-05-18 14:32:29 +0200388 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200389
390 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
391 if (id == ggc->id)
392 return ggc;
393 }
394 return NULL;
395}
396
Harald Weltea9b473a2010-12-24 21:13:26 +0100397struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
398{
399 struct sgsn_ggsn_ctx *ggc;
400
401 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
402 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
403 return ggc;
404 }
405 return NULL;
406}
407
408
Harald Welte77289c22010-05-18 14:32:29 +0200409struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200410{
Harald Welte77289c22010-05-18 14:32:29 +0200411 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200412
Harald Welte77289c22010-05-18 14:32:29 +0200413 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200414 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200415 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200416 return ggc;
417}
418
419/* APN contexts */
420
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100421static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200422{
423 struct apn_ctx *actx;
424
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100425 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200426 if (!actx)
427 return NULL;
428 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100429 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
430
431 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200432
433 return actx;
434}
435
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100436void sgsn_apn_ctx_free(struct apn_ctx *actx)
437{
438 llist_del(&actx->list);
439 talloc_free(actx);
440}
441
442struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
443{
444 struct apn_ctx *actx;
445 struct apn_ctx *found_actx = NULL;
446 size_t imsi_prio = 0;
447 size_t name_prio = 0;
448 size_t name_req_len = strlen(name);
449
450 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
451 size_t name_ref_len, imsi_ref_len;
452 const char *name_ref_start, *name_match_start;
453
454 imsi_ref_len = strlen(actx->imsi_prefix);
455 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
456 continue;
457
458 if (imsi_ref_len < imsi_prio)
459 continue;
460
461 /* IMSI matches */
462
463 name_ref_start = &actx->name[0];
464 if (name_ref_start[0] == '*') {
465 /* Suffix match */
466 name_ref_start += 1;
467 name_ref_len = strlen(name_ref_start);
468 if (name_ref_len > name_req_len)
469 continue;
470 } else {
471 name_ref_len = strlen(name_ref_start);
472 if (name_ref_len != name_req_len)
473 continue;
474 }
475
476 name_match_start = name + (name_req_len - name_ref_len);
477 if (strcasecmp(name_match_start, name_ref_start) != 0)
478 continue;
479
480 /* IMSI and name match */
481
482 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
483 /* Lower priority, skip */
484 continue;
485
486 imsi_prio = imsi_ref_len;
487 name_prio = name_ref_len;
488 found_actx = actx;
489 }
490 return found_actx;
491}
492
493struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200494{
495 struct apn_ctx *actx;
496
497 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100498 if (strcasecmp(name, actx->name) == 0 &&
499 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200500 return actx;
501 }
502 return NULL;
503}
504
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100505struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200506{
507 struct apn_ctx *actx;
508
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100509 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200510 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100511 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200512
513 return actx;
514}
Harald Welte6463c072010-05-18 17:04:55 +0200515
516uint32_t sgsn_alloc_ptmsi(void)
517{
518 struct sgsn_mm_ctx *mm;
519 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100520 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200521
522restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200523 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
524 goto failed;
525
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100526 /* Enforce that the 2 MSB are set without loosing the distance between
527 * identical values. Since rand() has no duplicate values within a
528 * period (because the size of the state is the same like the size of
529 * the random value), this leads to a distance of period/4 when the
530 * distribution of the 2 MSB is uniform. This approach fails with a
531 * probability of (3/4)^max_retries, only 1% of the approaches will
532 * need more than 16 numbers (even distribution assumed).
533 *
534 * Alternatively, a freeze list could be used if another PRNG is used
535 * or when this approach proves to be not sufficient.
536 */
537 if (ptmsi >= 0xC0000000) {
538 if (!max_retries--)
539 goto failed;
540 goto restart;
541 }
542 ptmsi |= 0xC0000000;
543
544 if (ptmsi == GSM_RESERVED_TMSI) {
545 if (!max_retries--)
546 goto failed;
547 goto restart;
548 }
549
Harald Welte6463c072010-05-18 17:04:55 +0200550 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200551 if (mm->p_tmsi == ptmsi) {
552 if (!max_retries--)
553 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200554 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200555 }
Harald Welte6463c072010-05-18 17:04:55 +0200556 }
557
558 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200559
560failed:
561 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
562 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200563}
Harald Weltea9b473a2010-12-24 21:13:26 +0100564
565static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
566{
567 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
568 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
569 else {
570 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200571 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100572 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200573 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100574 sgsn_pdp_ctx_free(pdp);
575 }
576}
577
578/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100579 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100580int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
581{
582 struct sgsn_mm_ctx *mm;
583 int num = 0;
584
585 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
586 struct sgsn_pdp_ctx *pdp;
587 llist_for_each_entry(pdp, &mm->pdp_list, list) {
588 if (pdp->ggsn == ggsn) {
589 drop_one_pdp(pdp);
590 num++;
591 }
592 }
593 }
594
595 return num;
596}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200597
598int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
599{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +0100600 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200601}
602
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100603void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200604{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100605 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100606 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100607
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100608 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200609}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100610
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400611static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
612{
613 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
614 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
615}
616
617/**
618 * The tlv_parsed tp parameter will be modified to insert a
619 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
620 * PDP context handling.
621 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100622struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
623 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800624 enum gsm48_gsm_cause *gsm_cause,
625 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100626{
627 char req_apn_str[GSM_APN_LENGTH] = {0};
628 const struct apn_ctx *apn_ctx = NULL;
629 const char *selected_apn_str = NULL;
630 struct sgsn_subscriber_pdp_data *pdp;
631 struct sgsn_ggsn_ctx *ggsn = NULL;
632 int allow_any_apn = 0;
633
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800634 out_apn_str[0] = '\0';
635
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100636 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
637 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
638 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
639 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
640 return NULL;
641 }
642
643 gprs_apn_to_str(req_apn_str,
644 TLVP_VAL(tp, GSM48_IE_GSM_APN),
645 TLVP_LEN(tp, GSM48_IE_GSM_APN));
646
647 if (strcmp(req_apn_str, "*") == 0)
648 req_apn_str[0] = 0;
649 }
650
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800651 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100652 allow_any_apn = 1;
653
654 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
655 /* No specific APN requested, check for an APN that is both
656 * granted and configured */
657
658 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
659 if (strcmp(pdp->apn_str, "*") == 0)
660 {
661 allow_any_apn = 1;
662 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400663 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100664 continue;
665 }
666 if (!llist_empty(&sgsn_apn_ctxts)) {
667 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
668 /* Not configured */
669 if (apn_ctx == NULL)
670 continue;
671 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400672 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100673 selected_apn_str = pdp->apn_str;
674 break;
675 }
676 } else if (!allow_any_apn) {
677 /* Check whether the given APN is granted */
678 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
679 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400680 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100681 selected_apn_str = req_apn_str;
682 allow_any_apn = 1;
683 continue;
684 }
685 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400686 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100687 selected_apn_str = req_apn_str;
688 break;
689 }
690 }
691 } else if (strlen(req_apn_str) != 0) {
692 /* Any APN is allowed */
693 selected_apn_str = req_apn_str;
694 } else {
695 /* Prefer the GGSN associated with the wildcard APN */
696 selected_apn_str = "";
697 }
698
699 if (!allow_any_apn && selected_apn_str == NULL) {
700 /* Access not granted */
701 LOGMMCTXP(LOGL_NOTICE, mmctx,
702 "The requested APN '%s' is not allowed\n",
703 req_apn_str);
704 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
705 return NULL;
706 }
707
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800708 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200709 if (selected_apn_str)
710 strcpy(out_apn_str, selected_apn_str);
711 else
712 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800713
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100714 if (apn_ctx == NULL && selected_apn_str)
715 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
716
717 if (apn_ctx != NULL) {
718 ggsn = apn_ctx->ggsn;
719 } else if (llist_empty(&sgsn_apn_ctxts)) {
720 /* No configuration -> use GGSN 0 */
721 ggsn = sgsn_ggsn_ctx_by_id(0);
722 } else if (allow_any_apn &&
723 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
724 /* No APN given and no default configuration -> Use GGSN 0 */
725 ggsn = sgsn_ggsn_ctx_by_id(0);
726 } else {
727 /* No matching configuration found */
728 LOGMMCTXP(LOGL_NOTICE, mmctx,
729 "The selected APN '%s' has not been configured\n",
730 selected_apn_str);
731 *gsm_cause = GSM_CAUSE_MISSING_APN;
732 return NULL;
733 }
734
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800735 if (!ggsn) {
736 LOGMMCTXP(LOGL_NOTICE, mmctx,
737 "No static GGSN configured. Selected APN '%s'\n",
738 selected_apn_str);
739 return NULL;
740 }
741
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100742 LOGMMCTXP(LOGL_INFO, mmctx,
743 "Found GGSN %d for APN '%s' (requested '%s')\n",
744 ggsn->id, selected_apn_str ? selected_apn_str : "---",
745 req_apn_str);
746
747 return ggsn;
748}
749
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100750static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
751{
752 struct sgsn_mm_ctx *mmctx = NULL;
753
754 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100755 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100756 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
757 return;
758 }
759 }
760
761 /* No MM context found */
762 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
763 llme->tlli);
764 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
765}
766
767static void sgsn_llme_check_cb(void *data_)
768{
769 struct gprs_llc_llme *llme, *llme_tmp;
770 struct timespec now_tp;
771 time_t now, age;
772 time_t max_age = gprs_max_time_to_idle();
773
774 int rc;
775
776 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
777 OSMO_ASSERT(rc >= 0);
778 now = now_tp.tv_sec;
779
780 LOGP(DGPRS, LOGL_DEBUG,
781 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
782
783 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
784 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
785 llme->age_timestamp = now;
786
787 age = now - llme->age_timestamp;
788
789 if (age > max_age || age < 0) {
790 LOGP(DGPRS, LOGL_INFO,
791 "Inactivity timeout for TLLI 0x%08x, age %d\n",
792 llme->tlli, (int)age);
793 sgsn_llme_cleanup_free(llme);
794 }
795 }
796
797 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
798}
799
800void sgsn_inst_init()
801{
802 sgsn->llme_timer.cb = sgsn_llme_check_cb;
803 sgsn->llme_timer.data = NULL;
804
805 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
806}
807