blob: 43eeaaa9b53d2c1672ae2e03ad5844a037043901 [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 Welted0d62492017-08-12 13:43:54 +020033#include <osmocom/gsm/apn.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080034
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010035#include <openbsc/gprs_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020036#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080037#include <openbsc/gprs_sgsn.h>
Harald Welteab1d5622010-05-18 19:58:38 +020038#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010039#include <openbsc/gprs_gmm.h>
Jacob Erlbeck277b71e2015-02-02 18:03:05 +010040#include <openbsc/gprs_utils.h>
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +020041#include <openbsc/signal.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +020042#include "openbsc/gprs_llc.h"
Daniel Willmann6292c8d2016-05-21 17:35:57 +020043#include <openbsc/iu.h>
Harald Welteab1d5622010-05-18 19:58:38 +020044
Neels Hofmeyrf4daf162016-05-21 00:44:50 +020045#include <pdp.h>
46
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010047#include <time.h>
48
Daniel Willmann044ce5f2015-10-12 19:36:33 +020049#include <openssl/rand.h>
50
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010051#define GPRS_LLME_CHECK_TICK 30
52
Harald Welteab1d5622010-05-18 19:58:38 +020053extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080054
Harald Welted193cb32010-05-17 22:58:03 +020055LLIST_HEAD(sgsn_mm_ctxts);
56LLIST_HEAD(sgsn_ggsn_ctxts);
57LLIST_HEAD(sgsn_apn_ctxts);
58LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080059
Harald Welte8acd88f2010-05-18 10:57:45 +020060static const struct rate_ctr_desc mmctx_ctr_description[] = {
61 { "sign.packets.in", "Signalling Messages ( In)" },
62 { "sign.packets.out", "Signalling Messages (Out)" },
63 { "udata.packets.in", "User Data Messages ( In)" },
64 { "udata.packets.out", "User Data Messages (Out)" },
65 { "udata.bytes.in", "User Data Bytes ( In)" },
66 { "udata.bytes.out", "User Data Bytes (Out)" },
67 { "pdp_ctx_act", "PDP Context Activations " },
68 { "suspend", "SUSPEND Count " },
69 { "paging.ps", "Paging Packet Switched " },
70 { "paging.cs", "Paging Circuit Switched " },
71 { "ra_update", "Routing Area Update " },
72};
73
74static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
75 .group_name_prefix = "sgsn.mmctx",
76 .group_description = "SGSN MM Context Statistics",
77 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
78 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010079 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020080};
81
Harald Welteefbdee92010-06-10 00:20:12 +020082static const struct rate_ctr_desc pdpctx_ctr_description[] = {
83 { "udata.packets.in", "User Data Messages ( In)" },
84 { "udata.packets.out", "User Data Messages (Out)" },
85 { "udata.bytes.in", "User Data Bytes ( In)" },
86 { "udata.bytes.out", "User Data Bytes (Out)" },
87};
88
89static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
90 .group_name_prefix = "sgsn.pdpctx",
91 .group_description = "SGSN PDP Context Statistics",
92 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
93 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010094 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +020095};
96
Alexander Couzens14314bd2016-07-05 09:52:52 +020097static const struct rate_ctr_desc sgsn_ctr_description[] = {
Alexander Couzens4e699a92016-07-05 11:04:27 +020098 { "llc.dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" },
99 { "llc.ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" },
100 { "llc.dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" },
101 { "llc.ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" },
Alexander Couzens14314bd2016-07-05 09:52:52 +0200102 { "gprs.attach_requested", "Received attach requests" },
103 { "gprs.attach_accepted", "Sent attach accepts" },
104 { "gprs.attach_rejected", "Sent attach rejects" },
105 { "gprs.detach_requested", "Received detach requests" },
106 { "gprs.detach_acked", "Sent detach acks" },
107 { "gprs.routing_area_requested", "Received routing area requests" },
108 { "gprs.routing_area_requested", "Sent routing area acks" },
109 { "gprs.routing_area_requested", "Sent routing area rejects" },
110 { "pdp.activate_requested", "Received activate requests" },
111 { "pdp.activate_rejected", "Sent activate rejects" },
112 { "pdp.activate_accepted", "Sent activate accepts" },
113 { "pdp.request_activated", "unused" },
114 { "pdp.request_activate_rejected", "unused" },
115 { "pdp.modify_requested", "unused" },
116 { "pdp.modify_accepted", "unused" },
117 { "pdp.dl_deactivate_requested", "Sent deactivate requests" },
118 { "pdp.dl_deactivate_accepted", "Sent deactivate accepted" },
119 { "pdp.ul_deactivate_requested", "Received deactivate requests" },
120 { "pdp.ul_deactivate_accepted", "Received deactivate accepts" },
121};
122
123static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
124 "sgsn",
125 "SGSN Overall Statistics",
126 OSMO_STATS_CLASS_GLOBAL,
127 ARRAY_SIZE(sgsn_ctr_description),
128 sgsn_ctr_description,
129};
130
131void sgsn_rate_ctr_init() {
132 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0);
Harald Welte9282db82017-07-12 00:25:51 +0200133 OSMO_ASSERT(sgsn->rate_ctrs);
Alexander Couzens14314bd2016-07-05 09:52:52 +0200134}
135
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200136/* look-up an SGSN MM context based on Iu UE context (struct ue_conn_ctx)*/
137struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx)
138{
139 struct sgsn_mm_ctx *ctx;
140
141 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
142 if (ctx->ran_type == MM_CTX_T_UTRAN_Iu
143 && uectx == ctx->iu.ue_ctx)
144 return ctx;
145 }
146
147 return NULL;
148}
149
Harald Welte9b455bf2010-03-14 15:45:01 +0800150/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200151struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800152 const struct gprs_ra_id *raid)
153{
154 struct sgsn_mm_ctx *ctx;
155
156 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100157 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100158 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800159 return ctx;
160 }
Harald Welteab1d5622010-05-18 19:58:38 +0200161
Harald Welte9b455bf2010-03-14 15:45:01 +0800162 return NULL;
163}
164
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100165struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
166 const struct gprs_ra_id *raid)
167{
168 struct sgsn_mm_ctx *ctx;
169 int tlli_type;
170
171 /* TODO: Also check the P_TMSI signature to be safe. That signature
172 * should be different (at least with a sufficiently high probability)
173 * after SGSN restarts and for multiple SGSN instances.
174 */
175
176 tlli_type = gprs_tlli_type(tlli);
177 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
178 return NULL;
179
180 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
181 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
182 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
183 gprs_ra_id_equals(raid, &ctx->ra))
184 return ctx;
185 }
186
187 return NULL;
188}
189
Harald Welteeaa614c2010-05-02 11:26:34 +0200190struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800191{
192 struct sgsn_mm_ctx *ctx;
193
194 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200195 if (p_tmsi == ctx->p_tmsi ||
196 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800197 return ctx;
198 }
199 return NULL;
200}
201
202struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
203{
204 struct sgsn_mm_ctx *ctx;
205
206 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
207 if (!strcmp(imsi, ctx->imsi))
208 return ctx;
209 }
210 return NULL;
211
212}
213
Alexander Couzens2b5fb8e2017-02-04 06:01:00 +0100214/* Allocate a new SGSN MM context for GERAN_Gb */
215struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_gb(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800216 const struct gprs_ra_id *raid)
217{
Harald Welte2720e732010-05-17 00:44:57 +0200218 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800219
Harald Welte2720e732010-05-17 00:44:57 +0200220 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800221 if (!ctx)
222 return NULL;
223
224 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100225 ctx->ran_type = MM_CTX_T_GERAN_Gb;
226 ctx->gb.tlli = tlli;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100227 ctx->gmm_state = GMM_DEREGISTERED;
Alexander Couzens10135502017-02-04 05:53:07 +0100228 ctx->pmm_state = MM_IDLE;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100229 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Maxb997f842016-07-06 15:57:01 +0200230 ctx->ciph_algo = sgsn->cfg.cipher;
Max549ebc72016-11-18 14:07:04 +0100231 LOGMMCTXP(LOGL_DEBUG, ctx, "Allocated with %s cipher.\n",
232 get_value_string(gprs_cipher_names, ctx->ciph_algo));
Harald Welte8acd88f2010-05-18 10:57:45 +0200233 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte9282db82017-07-12 00:25:51 +0200234 if (!ctx->ctrg) {
235 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group\n");
236 talloc_free(ctx);
237 return NULL;
238 }
Harald Welte6ffbaab2010-05-18 12:44:45 +0200239 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800240
241 llist_add(&ctx->list, &sgsn_mm_ctxts);
242
243 return ctx;
244}
Harald Welted193cb32010-05-17 22:58:03 +0200245
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200246/* Allocate a new SGSN MM context */
247struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx)
248{
249 struct sgsn_mm_ctx *ctx;
250
251 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
252 if (!ctx)
253 return NULL;
254
255 ctx->ran_type = MM_CTX_T_UTRAN_Iu;
256 ctx->iu.ue_ctx = uectx;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200257 ctx->iu.ue_ctx->rab_assign_addr_enc = sgsn->cfg.iu.rab_assign_addr_enc;
Daniel Willmann3ecfbbb2016-05-21 00:16:55 +0200258 ctx->iu.new_key = 1;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100259 ctx->gmm_state = GMM_DEREGISTERED;
Daniel Willmann5b2363e2016-05-21 00:01:21 +0200260 ctx->pmm_state = PMM_DETACHED;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200261 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
262 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, 0);
Harald Welte9282db82017-07-12 00:25:51 +0200263 if (!ctx->ctrg) {
264 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group\n");
265 talloc_free(ctx);
266 return NULL;
267 }
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200268
269 /* Need to get RAID from IU conn */
270 ctx->ra = ctx->iu.ue_ctx->ra_id;
271
272 INIT_LLIST_HEAD(&ctx->pdp_list);
273
274 llist_add(&ctx->list, &sgsn_mm_ctxts);
275
276 return ctx;
277}
278
279
Harald Welte7b022ee2012-07-14 12:04:04 +0200280/* this is a hard _free_ function, it doesn't clean up the PDP contexts
281 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200282static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100283{
284 struct sgsn_pdp_ctx *pdp, *pdp2;
285
Jacob Erlbecke671d252015-01-26 14:43:07 +0100286 /* Unlink from global list of MM contexts */
287 llist_del(&mm->list);
288
289 /* Free all PDP contexts */
290 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
291 sgsn_pdp_ctx_free(pdp);
292
293 rate_ctr_group_free(mm->ctrg);
294
295 talloc_free(mm);
296}
297
298void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
299{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200300 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100301 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100302 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200303 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100304
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200305 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
306 llme = mm->gb.llme;
307 else
308 OSMO_ASSERT(mm->gb.llme == NULL);
309
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800310 /* Forget about ongoing look-ups */
311 if (mm->ggsn_lookup) {
312 LOGMMCTXP(LOGL_NOTICE, mm,
313 "Cleaning mmctx with on-going query.\n");
314 mm->ggsn_lookup->mmctx = NULL;
315 mm->ggsn_lookup = NULL;
316 }
317
Jacob Erlbecke671d252015-01-26 14:43:07 +0100318 /* delete all existing PDP contexts for this MS */
319 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
320 LOGMMCTXP(LOGL_NOTICE, mm,
321 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
322 sgsn_pdp_ctx_terminate(pdp);
323 }
324
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200325 if (osmo_timer_pending(&mm->timer)) {
326 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
327 osmo_timer_del(&mm->timer);
328 }
329
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200330 memset(&sig_data, 0, sizeof(sig_data));
331 sig_data.mm = mm;
332 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
333
334
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100335 /* Detach from subscriber which is possibly freed then */
336 if (mm->subscr) {
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100337 struct gprs_subscr *subscr = gprs_subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100338 gprs_subscr_cleanup(subscr);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100339 gprs_subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100340 }
341
Jacob Erlbecke671d252015-01-26 14:43:07 +0100342 sgsn_mm_ctx_free(mm);
343 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100344
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200345 if (llme) {
346 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200347 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200348 }
Harald Weltec728eea2010-12-24 23:07:18 +0100349}
Harald Welte77289c22010-05-18 14:32:29 +0200350
Jacob Erlbecke671d252015-01-26 14:43:07 +0100351
Harald Welte96df6062010-06-03 06:37:26 +0200352/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200353struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
354 uint8_t nsapi)
355{
356 struct sgsn_pdp_ctx *pdp;
357
358 llist_for_each_entry(pdp, &mm->pdp_list, list) {
359 if (pdp->nsapi == nsapi)
360 return pdp;
361 }
362 return NULL;
363}
364
Harald Welte96df6062010-06-03 06:37:26 +0200365/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200366struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
367 uint8_t tid)
368{
369 struct sgsn_pdp_ctx *pdp;
370
371 llist_for_each_entry(pdp, &mm->pdp_list, list) {
372 if (pdp->ti == tid)
373 return pdp;
374 }
375 return NULL;
376}
377
Harald Welte7b022ee2012-07-14 12:04:04 +0200378/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200379struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
380 uint8_t nsapi)
381{
382 struct sgsn_pdp_ctx *pdp;
383
384 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
385 if (pdp)
386 return NULL;
387
388 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
389 if (!pdp)
390 return NULL;
391
392 pdp->mm = mm;
393 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200394 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welte9282db82017-07-12 00:25:51 +0200395 if (!pdp->ctrg) {
396 LOGPDPCTXP(LOGL_ERROR, pdp, "Error allocation counter group\n");
397 talloc_free(pdp);
398 return NULL;
399 }
Harald Welted193cb32010-05-17 22:58:03 +0200400 llist_add(&pdp->list, &mm->pdp_list);
401 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
402
403 return pdp;
404}
405
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200406/*
407 * This function will not trigger any GSM DEACT PDP ACK messages, so you
408 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
409 * isn't detached already.
410 */
411void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
412{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200413 struct sgsn_signal_data sig_data;
414
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200415 OSMO_ASSERT(pdp->mm != NULL);
416
417 /* There might still be pending callbacks in libgtp. So the parts of
418 * this object relevant to GTP need to remain intact in this case. */
419
420 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
421
Daniel Willmannf9f43872016-05-20 22:36:23 +0200422 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
423 /* Force the deactivation of the SNDCP layer */
424 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
425 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200426
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200427 memset(&sig_data, 0, sizeof(sig_data));
428 sig_data.pdp = pdp;
429 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
430
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200431 /* Detach from MM context */
432 llist_del(&pdp->list);
433 pdp->mm = NULL;
434
435 sgsn_delete_pdp_ctx(pdp);
436}
437
438/*
439 * Don't call this function directly unless you know what you are doing.
440 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
441 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
442 */
Harald Welted193cb32010-05-17 22:58:03 +0200443void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
444{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200445 struct sgsn_signal_data sig_data;
446
447 memset(&sig_data, 0, sizeof(sig_data));
448 sig_data.pdp = pdp;
449 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
450
Harald Welte376d5e52010-06-28 18:57:21 +0200451 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200452 if (pdp->mm)
453 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200454 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200455
456 /* _if_ we still have a library handle, at least set it to NULL
457 * to avoid any dereferences of the now-deleted PDP context from
458 * sgsn_libgtp:cb_data_ind() */
459 if (pdp->lib) {
460 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200461 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200462 "has a libgtp handle attached to it, this shouldn't "
463 "happen!\n");
464 osmo_generate_backtrace();
465 lib->priv = NULL;
466 }
467
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800468 if (pdp->destroy_ggsn)
469 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200470 talloc_free(pdp);
471}
472
473/* GGSN contexts */
474
Harald Welte77289c22010-05-18 14:32:29 +0200475struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200476{
Harald Welte77289c22010-05-18 14:32:29 +0200477 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200478
Harald Welte77289c22010-05-18 14:32:29 +0200479 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200480 if (!ggc)
481 return NULL;
482
483 ggc->id = id;
484 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100485 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200486 /* if we are called from config file parse, this gsn doesn't exist yet */
487 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200488 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200489
490 return ggc;
491}
492
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100493void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
494{
495 llist_del(&ggc->list);
496 talloc_free(ggc);
497}
498
Harald Welte77289c22010-05-18 14:32:29 +0200499struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200500{
Harald Welte77289c22010-05-18 14:32:29 +0200501 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200502
503 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
504 if (id == ggc->id)
505 return ggc;
506 }
507 return NULL;
508}
509
Harald Weltea9b473a2010-12-24 21:13:26 +0100510struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
511{
512 struct sgsn_ggsn_ctx *ggc;
513
514 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
515 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
516 return ggc;
517 }
518 return NULL;
519}
520
521
Harald Welte77289c22010-05-18 14:32:29 +0200522struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200523{
Harald Welte77289c22010-05-18 14:32:29 +0200524 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200525
Harald Welte77289c22010-05-18 14:32:29 +0200526 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200527 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200528 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200529 return ggc;
530}
531
532/* APN contexts */
533
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100534static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200535{
536 struct apn_ctx *actx;
537
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100538 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200539 if (!actx)
540 return NULL;
541 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100542 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
543
544 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200545
546 return actx;
547}
548
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100549void sgsn_apn_ctx_free(struct apn_ctx *actx)
550{
551 llist_del(&actx->list);
552 talloc_free(actx);
553}
554
555struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
556{
557 struct apn_ctx *actx;
558 struct apn_ctx *found_actx = NULL;
559 size_t imsi_prio = 0;
560 size_t name_prio = 0;
561 size_t name_req_len = strlen(name);
562
563 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
564 size_t name_ref_len, imsi_ref_len;
565 const char *name_ref_start, *name_match_start;
566
567 imsi_ref_len = strlen(actx->imsi_prefix);
568 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
569 continue;
570
571 if (imsi_ref_len < imsi_prio)
572 continue;
573
574 /* IMSI matches */
575
576 name_ref_start = &actx->name[0];
577 if (name_ref_start[0] == '*') {
578 /* Suffix match */
579 name_ref_start += 1;
580 name_ref_len = strlen(name_ref_start);
581 if (name_ref_len > name_req_len)
582 continue;
583 } else {
584 name_ref_len = strlen(name_ref_start);
585 if (name_ref_len != name_req_len)
586 continue;
587 }
588
589 name_match_start = name + (name_req_len - name_ref_len);
590 if (strcasecmp(name_match_start, name_ref_start) != 0)
591 continue;
592
593 /* IMSI and name match */
594
595 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
596 /* Lower priority, skip */
597 continue;
598
599 imsi_prio = imsi_ref_len;
600 name_prio = name_ref_len;
601 found_actx = actx;
602 }
603 return found_actx;
604}
605
606struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200607{
608 struct apn_ctx *actx;
609
610 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100611 if (strcasecmp(name, actx->name) == 0 &&
612 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200613 return actx;
614 }
615 return NULL;
616}
617
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100618struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200619{
620 struct apn_ctx *actx;
621
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100622 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200623 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100624 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200625
626 return actx;
627}
Harald Welte6463c072010-05-18 17:04:55 +0200628
629uint32_t sgsn_alloc_ptmsi(void)
630{
631 struct sgsn_mm_ctx *mm;
Alexander Couzens8a215c32017-02-03 23:22:18 +0100632 uint32_t ptmsi = 0xdeadbeef;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100633 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200634
635restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200636 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
637 goto failed;
638
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100639 /* Enforce that the 2 MSB are set without loosing the distance between
640 * identical values. Since rand() has no duplicate values within a
641 * period (because the size of the state is the same like the size of
642 * the random value), this leads to a distance of period/4 when the
643 * distribution of the 2 MSB is uniform. This approach fails with a
644 * probability of (3/4)^max_retries, only 1% of the approaches will
645 * need more than 16 numbers (even distribution assumed).
646 *
647 * Alternatively, a freeze list could be used if another PRNG is used
648 * or when this approach proves to be not sufficient.
649 */
650 if (ptmsi >= 0xC0000000) {
651 if (!max_retries--)
652 goto failed;
653 goto restart;
654 }
655 ptmsi |= 0xC0000000;
656
657 if (ptmsi == GSM_RESERVED_TMSI) {
658 if (!max_retries--)
659 goto failed;
660 goto restart;
661 }
662
Harald Welte6463c072010-05-18 17:04:55 +0200663 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200664 if (mm->p_tmsi == ptmsi) {
665 if (!max_retries--)
666 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200667 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200668 }
Harald Welte6463c072010-05-18 17:04:55 +0200669 }
670
671 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200672
673failed:
674 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
675 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200676}
Harald Weltea9b473a2010-12-24 21:13:26 +0100677
678static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
679{
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100680 if (pdp->mm->gmm_state == GMM_REGISTERED_NORMAL)
Harald Weltea9b473a2010-12-24 21:13:26 +0100681 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
682 else {
683 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200684 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100685 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200686 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100687 sgsn_pdp_ctx_free(pdp);
688 }
689}
690
691/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100692 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100693int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
694{
695 struct sgsn_mm_ctx *mm;
696 int num = 0;
697
698 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
699 struct sgsn_pdp_ctx *pdp;
700 llist_for_each_entry(pdp, &mm->pdp_list, list) {
701 if (pdp->ggsn == ggsn) {
702 drop_one_pdp(pdp);
703 num++;
704 }
705 }
706 }
707
708 return num;
709}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200710
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100711void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200712{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100713 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100714 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100715
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100716 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200717}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100718
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200719static void insert_extra(struct tlv_parsed *tp,
720 struct sgsn_subscriber_data *data,
721 struct sgsn_subscriber_pdp_data *pdp)
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400722{
723 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
724 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200725
726 /* Prefer PDP charging characteristics of per subscriber one */
727 if (pdp->has_pdp_charg) {
728 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(pdp->pdp_charg);
729 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &pdp->pdp_charg[0];
730 } else if (data->has_pdp_charg) {
731 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(data->pdp_charg);
732 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &data->pdp_charg[0];
733 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400734}
735
736/**
737 * The tlv_parsed tp parameter will be modified to insert a
738 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
739 * PDP context handling.
740 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100741struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
742 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800743 enum gsm48_gsm_cause *gsm_cause,
744 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100745{
746 char req_apn_str[GSM_APN_LENGTH] = {0};
747 const struct apn_ctx *apn_ctx = NULL;
748 const char *selected_apn_str = NULL;
749 struct sgsn_subscriber_pdp_data *pdp;
750 struct sgsn_ggsn_ctx *ggsn = NULL;
751 int allow_any_apn = 0;
752
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800753 out_apn_str[0] = '\0';
754
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100755 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
756 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
757 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
758 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
759 return NULL;
760 }
761
Harald Welted0d62492017-08-12 13:43:54 +0200762 osmo_apn_to_str(req_apn_str,
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100763 TLVP_VAL(tp, GSM48_IE_GSM_APN),
764 TLVP_LEN(tp, GSM48_IE_GSM_APN));
765
766 if (strcmp(req_apn_str, "*") == 0)
767 req_apn_str[0] = 0;
768 }
769
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800770 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100771 allow_any_apn = 1;
772
773 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
774 /* No specific APN requested, check for an APN that is both
775 * granted and configured */
776
777 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
778 if (strcmp(pdp->apn_str, "*") == 0)
779 {
780 allow_any_apn = 1;
781 selected_apn_str = "";
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200782 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100783 continue;
784 }
785 if (!llist_empty(&sgsn_apn_ctxts)) {
786 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
787 /* Not configured */
788 if (apn_ctx == NULL)
789 continue;
790 }
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200791 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100792 selected_apn_str = pdp->apn_str;
793 break;
794 }
795 } else if (!allow_any_apn) {
796 /* Check whether the given APN is granted */
797 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
798 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200799 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100800 selected_apn_str = req_apn_str;
801 allow_any_apn = 1;
802 continue;
803 }
804 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freytherc3bd4662017-07-09 13:18:17 +0200805 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100806 selected_apn_str = req_apn_str;
807 break;
808 }
809 }
810 } else if (strlen(req_apn_str) != 0) {
811 /* Any APN is allowed */
812 selected_apn_str = req_apn_str;
813 } else {
814 /* Prefer the GGSN associated with the wildcard APN */
815 selected_apn_str = "";
816 }
817
818 if (!allow_any_apn && selected_apn_str == NULL) {
819 /* Access not granted */
820 LOGMMCTXP(LOGL_NOTICE, mmctx,
821 "The requested APN '%s' is not allowed\n",
822 req_apn_str);
823 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
824 return NULL;
825 }
826
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800827 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200828 if (selected_apn_str)
829 strcpy(out_apn_str, selected_apn_str);
830 else
831 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800832
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100833 if (apn_ctx == NULL && selected_apn_str)
834 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
835
836 if (apn_ctx != NULL) {
837 ggsn = apn_ctx->ggsn;
838 } else if (llist_empty(&sgsn_apn_ctxts)) {
839 /* No configuration -> use GGSN 0 */
840 ggsn = sgsn_ggsn_ctx_by_id(0);
841 } else if (allow_any_apn &&
842 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
843 /* No APN given and no default configuration -> Use GGSN 0 */
844 ggsn = sgsn_ggsn_ctx_by_id(0);
845 } else {
846 /* No matching configuration found */
847 LOGMMCTXP(LOGL_NOTICE, mmctx,
848 "The selected APN '%s' has not been configured\n",
849 selected_apn_str);
850 *gsm_cause = GSM_CAUSE_MISSING_APN;
851 return NULL;
852 }
853
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800854 if (!ggsn) {
855 LOGMMCTXP(LOGL_NOTICE, mmctx,
856 "No static GGSN configured. Selected APN '%s'\n",
857 selected_apn_str);
858 return NULL;
859 }
860
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100861 LOGMMCTXP(LOGL_INFO, mmctx,
862 "Found GGSN %d for APN '%s' (requested '%s')\n",
863 ggsn->id, selected_apn_str ? selected_apn_str : "---",
864 req_apn_str);
865
866 return ggsn;
867}
868
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100869static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
870{
871 struct sgsn_mm_ctx *mmctx = NULL;
872
873 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100874 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100875 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
876 return;
877 }
878 }
879
880 /* No MM context found */
881 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
882 llme->tlli);
Max39550252016-06-28 17:39:20 +0200883 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100884}
885
886static void sgsn_llme_check_cb(void *data_)
887{
888 struct gprs_llc_llme *llme, *llme_tmp;
889 struct timespec now_tp;
890 time_t now, age;
891 time_t max_age = gprs_max_time_to_idle();
892
893 int rc;
894
895 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
896 OSMO_ASSERT(rc >= 0);
897 now = now_tp.tv_sec;
898
899 LOGP(DGPRS, LOGL_DEBUG,
900 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
901
902 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
903 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
904 llme->age_timestamp = now;
905
906 age = now - llme->age_timestamp;
907
908 if (age > max_age || age < 0) {
909 LOGP(DGPRS, LOGL_INFO,
910 "Inactivity timeout for TLLI 0x%08x, age %d\n",
911 llme->tlli, (int)age);
912 sgsn_llme_cleanup_free(llme);
913 }
914 }
915
916 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
917}
918
919void sgsn_inst_init()
920{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200921 osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100922 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
923}
924