blob: 93b133f3b21ea506f79fa6411f95a7629dd28644 [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
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010034#include <openbsc/gprs_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"
Daniel Willmann6292c8d2016-05-21 17:35:57 +020042#include <openbsc/iu.h>
Harald Welteab1d5622010-05-18 19:58:38 +020043
Neels Hofmeyrf4daf162016-05-21 00:44:50 +020044#include <pdp.h>
45
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010046#include <time.h>
47
Daniel Willmann044ce5f2015-10-12 19:36:33 +020048#include <openssl/rand.h>
49
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010050#define GPRS_LLME_CHECK_TICK 30
51
Harald Welteab1d5622010-05-18 19:58:38 +020052extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080053
Harald Welted193cb32010-05-17 22:58:03 +020054LLIST_HEAD(sgsn_mm_ctxts);
55LLIST_HEAD(sgsn_ggsn_ctxts);
56LLIST_HEAD(sgsn_apn_ctxts);
57LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080058
Harald Welte8acd88f2010-05-18 10:57:45 +020059static const struct rate_ctr_desc mmctx_ctr_description[] = {
60 { "sign.packets.in", "Signalling Messages ( In)" },
61 { "sign.packets.out", "Signalling Messages (Out)" },
62 { "udata.packets.in", "User Data Messages ( In)" },
63 { "udata.packets.out", "User Data Messages (Out)" },
64 { "udata.bytes.in", "User Data Bytes ( In)" },
65 { "udata.bytes.out", "User Data Bytes (Out)" },
66 { "pdp_ctx_act", "PDP Context Activations " },
67 { "suspend", "SUSPEND Count " },
68 { "paging.ps", "Paging Packet Switched " },
69 { "paging.cs", "Paging Circuit Switched " },
70 { "ra_update", "Routing Area Update " },
71};
72
73static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
74 .group_name_prefix = "sgsn.mmctx",
75 .group_description = "SGSN MM Context Statistics",
76 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
77 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010078 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020079};
80
Harald Welteefbdee92010-06-10 00:20:12 +020081static const struct rate_ctr_desc pdpctx_ctr_description[] = {
82 { "udata.packets.in", "User Data Messages ( In)" },
83 { "udata.packets.out", "User Data Messages (Out)" },
84 { "udata.bytes.in", "User Data Bytes ( In)" },
85 { "udata.bytes.out", "User Data Bytes (Out)" },
86};
87
88static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
89 .group_name_prefix = "sgsn.pdpctx",
90 .group_description = "SGSN PDP Context Statistics",
91 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
92 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010093 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +020094};
95
Alexander Couzens14314bd2016-07-05 09:52:52 +020096static const struct rate_ctr_desc sgsn_ctr_description[] = {
Alexander Couzens4e699a92016-07-05 11:04:27 +020097 { "llc.dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" },
98 { "llc.ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" },
99 { "llc.dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" },
100 { "llc.ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" },
Alexander Couzens14314bd2016-07-05 09:52:52 +0200101 { "gprs.attach_requested", "Received attach requests" },
102 { "gprs.attach_accepted", "Sent attach accepts" },
103 { "gprs.attach_rejected", "Sent attach rejects" },
104 { "gprs.detach_requested", "Received detach requests" },
105 { "gprs.detach_acked", "Sent detach acks" },
106 { "gprs.routing_area_requested", "Received routing area requests" },
107 { "gprs.routing_area_requested", "Sent routing area acks" },
108 { "gprs.routing_area_requested", "Sent routing area rejects" },
109 { "pdp.activate_requested", "Received activate requests" },
110 { "pdp.activate_rejected", "Sent activate rejects" },
111 { "pdp.activate_accepted", "Sent activate accepts" },
112 { "pdp.request_activated", "unused" },
113 { "pdp.request_activate_rejected", "unused" },
114 { "pdp.modify_requested", "unused" },
115 { "pdp.modify_accepted", "unused" },
116 { "pdp.dl_deactivate_requested", "Sent deactivate requests" },
117 { "pdp.dl_deactivate_accepted", "Sent deactivate accepted" },
118 { "pdp.ul_deactivate_requested", "Received deactivate requests" },
119 { "pdp.ul_deactivate_accepted", "Received deactivate accepts" },
120};
121
122static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
123 "sgsn",
124 "SGSN Overall Statistics",
125 OSMO_STATS_CLASS_GLOBAL,
126 ARRAY_SIZE(sgsn_ctr_description),
127 sgsn_ctr_description,
128};
129
130void sgsn_rate_ctr_init() {
131 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0);
Harald Welte9282db82017-07-12 00:25:51 +0200132 OSMO_ASSERT(sgsn->rate_ctrs);
Alexander Couzens14314bd2016-07-05 09:52:52 +0200133}
134
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200135/* look-up an SGSN MM context based on Iu UE context (struct ue_conn_ctx)*/
136struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx)
137{
138 struct sgsn_mm_ctx *ctx;
139
140 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
141 if (ctx->ran_type == MM_CTX_T_UTRAN_Iu
142 && uectx == ctx->iu.ue_ctx)
143 return ctx;
144 }
145
146 return NULL;
147}
148
Harald Welte9b455bf2010-03-14 15:45:01 +0800149/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200150struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800151 const struct gprs_ra_id *raid)
152{
153 struct sgsn_mm_ctx *ctx;
154
155 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100156 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100157 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800158 return ctx;
159 }
Harald Welteab1d5622010-05-18 19:58:38 +0200160
Harald Welte9b455bf2010-03-14 15:45:01 +0800161 return NULL;
162}
163
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100164struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
165 const struct gprs_ra_id *raid)
166{
167 struct sgsn_mm_ctx *ctx;
168 int tlli_type;
169
170 /* TODO: Also check the P_TMSI signature to be safe. That signature
171 * should be different (at least with a sufficiently high probability)
172 * after SGSN restarts and for multiple SGSN instances.
173 */
174
175 tlli_type = gprs_tlli_type(tlli);
176 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
177 return NULL;
178
179 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
180 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
181 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
182 gprs_ra_id_equals(raid, &ctx->ra))
183 return ctx;
184 }
185
186 return NULL;
187}
188
Harald Welteeaa614c2010-05-02 11:26:34 +0200189struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800190{
191 struct sgsn_mm_ctx *ctx;
192
193 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200194 if (p_tmsi == ctx->p_tmsi ||
195 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800196 return ctx;
197 }
198 return NULL;
199}
200
201struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
202{
203 struct sgsn_mm_ctx *ctx;
204
205 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
206 if (!strcmp(imsi, ctx->imsi))
207 return ctx;
208 }
209 return NULL;
210
211}
212
Alexander Couzens2b5fb8e2017-02-04 06:01:00 +0100213/* Allocate a new SGSN MM context for GERAN_Gb */
214struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_gb(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800215 const struct gprs_ra_id *raid)
216{
Harald Welte2720e732010-05-17 00:44:57 +0200217 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800218
Harald Welte2720e732010-05-17 00:44:57 +0200219 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800220 if (!ctx)
221 return NULL;
222
223 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100224 ctx->ran_type = MM_CTX_T_GERAN_Gb;
225 ctx->gb.tlli = tlli;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100226 ctx->gmm_state = GMM_DEREGISTERED;
Alexander Couzens10135502017-02-04 05:53:07 +0100227 ctx->pmm_state = MM_IDLE;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100228 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Maxb997f842016-07-06 15:57:01 +0200229 ctx->ciph_algo = sgsn->cfg.cipher;
Max549ebc72016-11-18 14:07:04 +0100230 LOGMMCTXP(LOGL_DEBUG, ctx, "Allocated with %s cipher.\n",
231 get_value_string(gprs_cipher_names, ctx->ciph_algo));
Harald Welte8acd88f2010-05-18 10:57:45 +0200232 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte9282db82017-07-12 00:25:51 +0200233 if (!ctx->ctrg) {
234 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group\n");
235 talloc_free(ctx);
236 return NULL;
237 }
Harald Welte6ffbaab2010-05-18 12:44:45 +0200238 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800239
240 llist_add(&ctx->list, &sgsn_mm_ctxts);
241
242 return ctx;
243}
Harald Welted193cb32010-05-17 22:58:03 +0200244
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200245/* Allocate a new SGSN MM context */
246struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx)
247{
248 struct sgsn_mm_ctx *ctx;
249
250 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
251 if (!ctx)
252 return NULL;
253
254 ctx->ran_type = MM_CTX_T_UTRAN_Iu;
255 ctx->iu.ue_ctx = uectx;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200256 ctx->iu.ue_ctx->rab_assign_addr_enc = sgsn->cfg.iu.rab_assign_addr_enc;
Daniel Willmann3ecfbbb2016-05-21 00:16:55 +0200257 ctx->iu.new_key = 1;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100258 ctx->gmm_state = GMM_DEREGISTERED;
Daniel Willmann5b2363e2016-05-21 00:01:21 +0200259 ctx->pmm_state = PMM_DETACHED;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200260 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
261 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, 0);
Harald Welte9282db82017-07-12 00:25:51 +0200262 if (!ctx->ctrg) {
263 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group\n");
264 talloc_free(ctx);
265 return NULL;
266 }
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200267
268 /* Need to get RAID from IU conn */
269 ctx->ra = ctx->iu.ue_ctx->ra_id;
270
271 INIT_LLIST_HEAD(&ctx->pdp_list);
272
273 llist_add(&ctx->list, &sgsn_mm_ctxts);
274
275 return ctx;
276}
277
278
Harald Welte7b022ee2012-07-14 12:04:04 +0200279/* this is a hard _free_ function, it doesn't clean up the PDP contexts
280 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200281static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100282{
283 struct sgsn_pdp_ctx *pdp, *pdp2;
284
Jacob Erlbecke671d252015-01-26 14:43:07 +0100285 /* Unlink from global list of MM contexts */
286 llist_del(&mm->list);
287
288 /* Free all PDP contexts */
289 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
290 sgsn_pdp_ctx_free(pdp);
291
292 rate_ctr_group_free(mm->ctrg);
293
294 talloc_free(mm);
295}
296
297void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
298{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200299 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100300 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100301 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200302 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100303
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200304 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
305 llme = mm->gb.llme;
306 else
307 OSMO_ASSERT(mm->gb.llme == NULL);
308
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800309 /* Forget about ongoing look-ups */
310 if (mm->ggsn_lookup) {
311 LOGMMCTXP(LOGL_NOTICE, mm,
312 "Cleaning mmctx with on-going query.\n");
313 mm->ggsn_lookup->mmctx = NULL;
314 mm->ggsn_lookup = NULL;
315 }
316
Jacob Erlbecke671d252015-01-26 14:43:07 +0100317 /* delete all existing PDP contexts for this MS */
318 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
319 LOGMMCTXP(LOGL_NOTICE, mm,
320 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
321 sgsn_pdp_ctx_terminate(pdp);
322 }
323
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200324 if (osmo_timer_pending(&mm->timer)) {
325 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
326 osmo_timer_del(&mm->timer);
327 }
328
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200329 memset(&sig_data, 0, sizeof(sig_data));
330 sig_data.mm = mm;
331 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
332
333
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100334 /* Detach from subscriber which is possibly freed then */
335 if (mm->subscr) {
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100336 struct gprs_subscr *subscr = gprs_subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100337 gprs_subscr_cleanup(subscr);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100338 gprs_subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100339 }
340
Jacob Erlbecke671d252015-01-26 14:43:07 +0100341 sgsn_mm_ctx_free(mm);
342 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100343
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200344 if (llme) {
345 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200346 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200347 }
Harald Weltec728eea2010-12-24 23:07:18 +0100348}
Harald Welte77289c22010-05-18 14:32:29 +0200349
Jacob Erlbecke671d252015-01-26 14:43:07 +0100350
Harald Welte96df6062010-06-03 06:37:26 +0200351/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200352struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
353 uint8_t nsapi)
354{
355 struct sgsn_pdp_ctx *pdp;
356
357 llist_for_each_entry(pdp, &mm->pdp_list, list) {
358 if (pdp->nsapi == nsapi)
359 return pdp;
360 }
361 return NULL;
362}
363
Harald Welte96df6062010-06-03 06:37:26 +0200364/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200365struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
366 uint8_t tid)
367{
368 struct sgsn_pdp_ctx *pdp;
369
370 llist_for_each_entry(pdp, &mm->pdp_list, list) {
371 if (pdp->ti == tid)
372 return pdp;
373 }
374 return NULL;
375}
376
Harald Welte7b022ee2012-07-14 12:04:04 +0200377/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200378struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
379 uint8_t nsapi)
380{
381 struct sgsn_pdp_ctx *pdp;
382
383 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
384 if (pdp)
385 return NULL;
386
387 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
388 if (!pdp)
389 return NULL;
390
391 pdp->mm = mm;
392 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200393 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welte9282db82017-07-12 00:25:51 +0200394 if (!pdp->ctrg) {
395 LOGPDPCTXP(LOGL_ERROR, pdp, "Error allocation counter group\n");
396 talloc_free(pdp);
397 return NULL;
398 }
Harald Welted193cb32010-05-17 22:58:03 +0200399 llist_add(&pdp->list, &mm->pdp_list);
400 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
401
402 return pdp;
403}
404
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200405/*
406 * This function will not trigger any GSM DEACT PDP ACK messages, so you
407 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
408 * isn't detached already.
409 */
410void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
411{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200412 struct sgsn_signal_data sig_data;
413
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200414 OSMO_ASSERT(pdp->mm != NULL);
415
416 /* There might still be pending callbacks in libgtp. So the parts of
417 * this object relevant to GTP need to remain intact in this case. */
418
419 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
420
Daniel Willmannf9f43872016-05-20 22:36:23 +0200421 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
422 /* Force the deactivation of the SNDCP layer */
423 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
424 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200425
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200426 memset(&sig_data, 0, sizeof(sig_data));
427 sig_data.pdp = pdp;
428 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
429
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200430 /* Detach from MM context */
431 llist_del(&pdp->list);
432 pdp->mm = NULL;
433
434 sgsn_delete_pdp_ctx(pdp);
435}
436
437/*
438 * Don't call this function directly unless you know what you are doing.
439 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
440 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
441 */
Harald Welted193cb32010-05-17 22:58:03 +0200442void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
443{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200444 struct sgsn_signal_data sig_data;
445
446 memset(&sig_data, 0, sizeof(sig_data));
447 sig_data.pdp = pdp;
448 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
449
Harald Welte376d5e52010-06-28 18:57:21 +0200450 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200451 if (pdp->mm)
452 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200453 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200454
455 /* _if_ we still have a library handle, at least set it to NULL
456 * to avoid any dereferences of the now-deleted PDP context from
457 * sgsn_libgtp:cb_data_ind() */
458 if (pdp->lib) {
459 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200460 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200461 "has a libgtp handle attached to it, this shouldn't "
462 "happen!\n");
463 osmo_generate_backtrace();
464 lib->priv = NULL;
465 }
466
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800467 if (pdp->destroy_ggsn)
468 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200469 talloc_free(pdp);
470}
471
472/* GGSN contexts */
473
Harald Welte77289c22010-05-18 14:32:29 +0200474struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200475{
Harald Welte77289c22010-05-18 14:32:29 +0200476 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200477
Harald Welte77289c22010-05-18 14:32:29 +0200478 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200479 if (!ggc)
480 return NULL;
481
482 ggc->id = id;
483 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100484 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200485 /* if we are called from config file parse, this gsn doesn't exist yet */
486 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200487 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200488
489 return ggc;
490}
491
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100492void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
493{
494 llist_del(&ggc->list);
495 talloc_free(ggc);
496}
497
Harald Welte77289c22010-05-18 14:32:29 +0200498struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200499{
Harald Welte77289c22010-05-18 14:32:29 +0200500 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200501
502 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
503 if (id == ggc->id)
504 return ggc;
505 }
506 return NULL;
507}
508
Harald Weltea9b473a2010-12-24 21:13:26 +0100509struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
510{
511 struct sgsn_ggsn_ctx *ggc;
512
513 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
514 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
515 return ggc;
516 }
517 return NULL;
518}
519
520
Harald Welte77289c22010-05-18 14:32:29 +0200521struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200522{
Harald Welte77289c22010-05-18 14:32:29 +0200523 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200524
Harald Welte77289c22010-05-18 14:32:29 +0200525 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200526 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200527 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200528 return ggc;
529}
530
531/* APN contexts */
532
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100533static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200534{
535 struct apn_ctx *actx;
536
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100537 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200538 if (!actx)
539 return NULL;
540 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100541 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
542
543 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200544
545 return actx;
546}
547
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100548void sgsn_apn_ctx_free(struct apn_ctx *actx)
549{
550 llist_del(&actx->list);
551 talloc_free(actx);
552}
553
554struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
555{
556 struct apn_ctx *actx;
557 struct apn_ctx *found_actx = NULL;
558 size_t imsi_prio = 0;
559 size_t name_prio = 0;
560 size_t name_req_len = strlen(name);
561
562 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
563 size_t name_ref_len, imsi_ref_len;
564 const char *name_ref_start, *name_match_start;
565
566 imsi_ref_len = strlen(actx->imsi_prefix);
567 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
568 continue;
569
570 if (imsi_ref_len < imsi_prio)
571 continue;
572
573 /* IMSI matches */
574
575 name_ref_start = &actx->name[0];
576 if (name_ref_start[0] == '*') {
577 /* Suffix match */
578 name_ref_start += 1;
579 name_ref_len = strlen(name_ref_start);
580 if (name_ref_len > name_req_len)
581 continue;
582 } else {
583 name_ref_len = strlen(name_ref_start);
584 if (name_ref_len != name_req_len)
585 continue;
586 }
587
588 name_match_start = name + (name_req_len - name_ref_len);
589 if (strcasecmp(name_match_start, name_ref_start) != 0)
590 continue;
591
592 /* IMSI and name match */
593
594 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
595 /* Lower priority, skip */
596 continue;
597
598 imsi_prio = imsi_ref_len;
599 name_prio = name_ref_len;
600 found_actx = actx;
601 }
602 return found_actx;
603}
604
605struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200606{
607 struct apn_ctx *actx;
608
609 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100610 if (strcasecmp(name, actx->name) == 0 &&
611 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200612 return actx;
613 }
614 return NULL;
615}
616
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100617struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200618{
619 struct apn_ctx *actx;
620
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100621 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200622 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100623 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200624
625 return actx;
626}
Harald Welte6463c072010-05-18 17:04:55 +0200627
628uint32_t sgsn_alloc_ptmsi(void)
629{
630 struct sgsn_mm_ctx *mm;
Alexander Couzens8a215c32017-02-03 23:22:18 +0100631 uint32_t ptmsi = 0xdeadbeef;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100632 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200633
634restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200635 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
636 goto failed;
637
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100638 /* Enforce that the 2 MSB are set without loosing the distance between
639 * identical values. Since rand() has no duplicate values within a
640 * period (because the size of the state is the same like the size of
641 * the random value), this leads to a distance of period/4 when the
642 * distribution of the 2 MSB is uniform. This approach fails with a
643 * probability of (3/4)^max_retries, only 1% of the approaches will
644 * need more than 16 numbers (even distribution assumed).
645 *
646 * Alternatively, a freeze list could be used if another PRNG is used
647 * or when this approach proves to be not sufficient.
648 */
649 if (ptmsi >= 0xC0000000) {
650 if (!max_retries--)
651 goto failed;
652 goto restart;
653 }
654 ptmsi |= 0xC0000000;
655
656 if (ptmsi == GSM_RESERVED_TMSI) {
657 if (!max_retries--)
658 goto failed;
659 goto restart;
660 }
661
Harald Welte6463c072010-05-18 17:04:55 +0200662 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200663 if (mm->p_tmsi == ptmsi) {
664 if (!max_retries--)
665 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200666 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200667 }
Harald Welte6463c072010-05-18 17:04:55 +0200668 }
669
670 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200671
672failed:
673 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
674 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200675}
Harald Weltea9b473a2010-12-24 21:13:26 +0100676
677static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
678{
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100679 if (pdp->mm->gmm_state == GMM_REGISTERED_NORMAL)
Harald Weltea9b473a2010-12-24 21:13:26 +0100680 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
681 else {
682 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200683 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100684 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200685 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100686 sgsn_pdp_ctx_free(pdp);
687 }
688}
689
690/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100691 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100692int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
693{
694 struct sgsn_mm_ctx *mm;
695 int num = 0;
696
697 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
698 struct sgsn_pdp_ctx *pdp;
699 llist_for_each_entry(pdp, &mm->pdp_list, list) {
700 if (pdp->ggsn == ggsn) {
701 drop_one_pdp(pdp);
702 num++;
703 }
704 }
705 }
706
707 return num;
708}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200709
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100710void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200711{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100712 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100713 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100714
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100715 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200716}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100717
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200718static void insert_extra(struct tlv_parsed *tp,
719 struct sgsn_subscriber_data *data,
720 struct sgsn_subscriber_pdp_data *pdp)
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400721{
722 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
723 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200724
725 /* Prefer PDP charging characteristics of per subscriber one */
726 if (pdp->has_pdp_charg) {
727 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(pdp->pdp_charg);
728 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &pdp->pdp_charg[0];
729 } else if (data->has_pdp_charg) {
730 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(data->pdp_charg);
731 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &data->pdp_charg[0];
732 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400733}
734
735/**
736 * The tlv_parsed tp parameter will be modified to insert a
737 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
738 * PDP context handling.
739 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100740struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
741 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800742 enum gsm48_gsm_cause *gsm_cause,
743 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100744{
745 char req_apn_str[GSM_APN_LENGTH] = {0};
746 const struct apn_ctx *apn_ctx = NULL;
747 const char *selected_apn_str = NULL;
748 struct sgsn_subscriber_pdp_data *pdp;
749 struct sgsn_ggsn_ctx *ggsn = NULL;
750 int allow_any_apn = 0;
751
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800752 out_apn_str[0] = '\0';
753
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100754 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
755 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
756 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
757 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
758 return NULL;
759 }
760
761 gprs_apn_to_str(req_apn_str,
762 TLVP_VAL(tp, GSM48_IE_GSM_APN),
763 TLVP_LEN(tp, GSM48_IE_GSM_APN));
764
765 if (strcmp(req_apn_str, "*") == 0)
766 req_apn_str[0] = 0;
767 }
768
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800769 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100770 allow_any_apn = 1;
771
772 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
773 /* No specific APN requested, check for an APN that is both
774 * granted and configured */
775
776 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
777 if (strcmp(pdp->apn_str, "*") == 0)
778 {
779 allow_any_apn = 1;
780 selected_apn_str = "";
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200781 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100782 continue;
783 }
784 if (!llist_empty(&sgsn_apn_ctxts)) {
785 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
786 /* Not configured */
787 if (apn_ctx == NULL)
788 continue;
789 }
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200790 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100791 selected_apn_str = pdp->apn_str;
792 break;
793 }
794 } else if (!allow_any_apn) {
795 /* Check whether the given APN is granted */
796 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
797 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200798 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100799 selected_apn_str = req_apn_str;
800 allow_any_apn = 1;
801 continue;
802 }
803 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200804 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100805 selected_apn_str = req_apn_str;
806 break;
807 }
808 }
809 } else if (strlen(req_apn_str) != 0) {
810 /* Any APN is allowed */
811 selected_apn_str = req_apn_str;
812 } else {
813 /* Prefer the GGSN associated with the wildcard APN */
814 selected_apn_str = "";
815 }
816
817 if (!allow_any_apn && selected_apn_str == NULL) {
818 /* Access not granted */
819 LOGMMCTXP(LOGL_NOTICE, mmctx,
820 "The requested APN '%s' is not allowed\n",
821 req_apn_str);
822 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
823 return NULL;
824 }
825
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800826 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200827 if (selected_apn_str)
828 strcpy(out_apn_str, selected_apn_str);
829 else
830 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800831
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100832 if (apn_ctx == NULL && selected_apn_str)
833 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
834
835 if (apn_ctx != NULL) {
836 ggsn = apn_ctx->ggsn;
837 } else if (llist_empty(&sgsn_apn_ctxts)) {
838 /* No configuration -> use GGSN 0 */
839 ggsn = sgsn_ggsn_ctx_by_id(0);
840 } else if (allow_any_apn &&
841 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
842 /* No APN given and no default configuration -> Use GGSN 0 */
843 ggsn = sgsn_ggsn_ctx_by_id(0);
844 } else {
845 /* No matching configuration found */
846 LOGMMCTXP(LOGL_NOTICE, mmctx,
847 "The selected APN '%s' has not been configured\n",
848 selected_apn_str);
849 *gsm_cause = GSM_CAUSE_MISSING_APN;
850 return NULL;
851 }
852
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800853 if (!ggsn) {
854 LOGMMCTXP(LOGL_NOTICE, mmctx,
855 "No static GGSN configured. Selected APN '%s'\n",
856 selected_apn_str);
857 return NULL;
858 }
859
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100860 LOGMMCTXP(LOGL_INFO, mmctx,
861 "Found GGSN %d for APN '%s' (requested '%s')\n",
862 ggsn->id, selected_apn_str ? selected_apn_str : "---",
863 req_apn_str);
864
865 return ggsn;
866}
867
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100868static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
869{
870 struct sgsn_mm_ctx *mmctx = NULL;
871
872 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100873 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100874 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
875 return;
876 }
877 }
878
879 /* No MM context found */
880 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
881 llme->tlli);
Max39550252016-06-28 17:39:20 +0200882 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100883}
884
885static void sgsn_llme_check_cb(void *data_)
886{
887 struct gprs_llc_llme *llme, *llme_tmp;
888 struct timespec now_tp;
889 time_t now, age;
890 time_t max_age = gprs_max_time_to_idle();
891
892 int rc;
893
894 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
895 OSMO_ASSERT(rc >= 0);
896 now = now_tp.tv_sec;
897
898 LOGP(DGPRS, LOGL_DEBUG,
899 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
900
901 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
902 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
903 llme->age_timestamp = now;
904
905 age = now - llme->age_timestamp;
906
907 if (age > max_age || age < 0) {
908 LOGP(DGPRS, LOGL_INFO,
909 "Inactivity timeout for TLLI 0x%08x, age %d\n",
910 llme->tlli, (int)age);
911 sgsn_llme_cleanup_free(llme);
912 }
913 }
914
915 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
916}
917
918void sgsn_inst_init()
919{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200920 osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100921 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
922}
923