blob: c4321f91ccc92a97bd3ffb81778c758879f17e99 [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 Welte7e82b742017-08-12 13:43:54 +020033#include <osmocom/gsm/apn.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020034#include <osmocom/gsm/gsm_utils.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080035
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020036#include <osmocom/sgsn/gprs_subscriber.h>
37#include <osmocom/sgsn/debug.h>
38#include <osmocom/sgsn/gprs_sgsn.h>
39#include <osmocom/sgsn/sgsn.h>
40#include <osmocom/sgsn/gprs_gmm.h>
41#include <osmocom/sgsn/gprs_utils.h>
42#include <osmocom/sgsn/signal.h>
43#include <osmocom/sgsn/gprs_llc.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
Neels Hofmeyra7a39472017-07-05 15:19:52 +020051#include "../../bscconfig.h"
52
53#if BUILD_IU
54#include <osmocom/ranap/iu_client.h>
55#endif
56
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010057#define GPRS_LLME_CHECK_TICK 30
58
Harald Welteab1d5622010-05-18 19:58:38 +020059extern struct sgsn_instance *sgsn;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020060extern void *tall_bsc_ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +080061
Harald Welted193cb32010-05-17 22:58:03 +020062LLIST_HEAD(sgsn_mm_ctxts);
63LLIST_HEAD(sgsn_ggsn_ctxts);
64LLIST_HEAD(sgsn_apn_ctxts);
65LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080066
Harald Welte8acd88f2010-05-18 10:57:45 +020067static const struct rate_ctr_desc mmctx_ctr_description[] = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010068 { "sign:packets:in", "Signalling Messages ( In)" },
69 { "sign:packets:out", "Signalling Messages (Out)" },
70 { "udata:packets:in", "User Data Messages ( In)" },
71 { "udata:packets:out", "User Data Messages (Out)" },
72 { "udata:bytes:in", "User Data Bytes ( In)" },
73 { "udata:bytes:out", "User Data Bytes (Out)" },
Harald Welte8acd88f2010-05-18 10:57:45 +020074 { "pdp_ctx_act", "PDP Context Activations " },
75 { "suspend", "SUSPEND Count " },
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010076 { "paging:ps", "Paging Packet Switched " },
77 { "paging:cs", "Paging Circuit Switched " },
Harald Welte8acd88f2010-05-18 10:57:45 +020078 { "ra_update", "Routing Area Update " },
79};
80
81static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010082 .group_name_prefix = "sgsn:mmctx",
Harald Welte8acd88f2010-05-18 10:57:45 +020083 .group_description = "SGSN MM Context Statistics",
84 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
85 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010086 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020087};
88
Harald Welteefbdee92010-06-10 00:20:12 +020089static const struct rate_ctr_desc pdpctx_ctr_description[] = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010090 { "udata:packets:in", "User Data Messages ( In)" },
91 { "udata:packets:out", "User Data Messages (Out)" },
92 { "udata:bytes:in", "User Data Bytes ( In)" },
93 { "udata:bytes:out", "User Data Bytes (Out)" },
Harald Welteefbdee92010-06-10 00:20:12 +020094};
95
96static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010097 .group_name_prefix = "sgsn:pdpctx",
Harald Welteefbdee92010-06-10 00:20:12 +020098 .group_description = "SGSN PDP Context Statistics",
99 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
100 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +0100101 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +0200102};
103
Alexander Couzens14314bd2016-07-05 09:52:52 +0200104static const struct rate_ctr_desc sgsn_ctr_description[] = {
Harald Welteb68413b2017-11-21 08:51:47 +0100105 { "llc:dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" },
106 { "llc:ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" },
107 { "llc:dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" },
108 { "llc:ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" },
109 { "gprs:attach_requested", "Received attach requests" },
110 { "gprs:attach_accepted", "Sent attach accepts" },
111 { "gprs:attach_rejected", "Sent attach rejects" },
112 { "gprs:detach_requested", "Received detach requests" },
113 { "gprs:detach_acked", "Sent detach acks" },
114 { "gprs:routing_area_requested", "Received routing area requests" },
115 { "gprs:routing_area_requested", "Sent routing area acks" },
116 { "gprs:routing_area_requested", "Sent routing area rejects" },
117 { "pdp:activate_requested", "Received activate requests" },
118 { "pdp:activate_rejected", "Sent activate rejects" },
119 { "pdp:activate_accepted", "Sent activate accepts" },
120 { "pdp:request_activated", "unused" },
121 { "pdp:request_activate_rejected", "unused" },
122 { "pdp:modify_requested", "unused" },
123 { "pdp:modify_accepted", "unused" },
124 { "pdp:dl_deactivate_requested", "Sent deactivate requests" },
125 { "pdp:dl_deactivate_accepted", "Sent deactivate accepted" },
126 { "pdp:ul_deactivate_requested", "Received deactivate requests" },
127 { "pdp:ul_deactivate_accepted", "Received deactivate accepts" },
Alexander Couzens14314bd2016-07-05 09:52:52 +0200128};
129
130static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
131 "sgsn",
132 "SGSN Overall Statistics",
133 OSMO_STATS_CLASS_GLOBAL,
134 ARRAY_SIZE(sgsn_ctr_description),
135 sgsn_ctr_description,
136};
137
138void sgsn_rate_ctr_init() {
139 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0);
Harald Welte26c14652017-07-12 00:25:51 +0200140 OSMO_ASSERT(sgsn->rate_ctrs);
Alexander Couzens14314bd2016-07-05 09:52:52 +0200141}
142
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200143/* look-up an SGSN MM context based on Iu UE context (struct ue_conn_ctx)*/
144struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx)
145{
146 struct sgsn_mm_ctx *ctx;
147
148 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
149 if (ctx->ran_type == MM_CTX_T_UTRAN_Iu
150 && uectx == ctx->iu.ue_ctx)
151 return ctx;
152 }
153
154 return NULL;
155}
156
Harald Welte9b455bf2010-03-14 15:45:01 +0800157/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200158struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800159 const struct gprs_ra_id *raid)
160{
161 struct sgsn_mm_ctx *ctx;
162
163 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100164 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100165 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800166 return ctx;
167 }
Harald Welteab1d5622010-05-18 19:58:38 +0200168
Harald Welte9b455bf2010-03-14 15:45:01 +0800169 return NULL;
170}
171
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100172struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
173 const struct gprs_ra_id *raid)
174{
175 struct sgsn_mm_ctx *ctx;
176 int tlli_type;
177
178 /* TODO: Also check the P_TMSI signature to be safe. That signature
179 * should be different (at least with a sufficiently high probability)
180 * after SGSN restarts and for multiple SGSN instances.
181 */
182
183 tlli_type = gprs_tlli_type(tlli);
184 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
185 return NULL;
186
187 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
188 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
189 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
190 gprs_ra_id_equals(raid, &ctx->ra))
191 return ctx;
192 }
193
194 return NULL;
195}
196
Harald Welteeaa614c2010-05-02 11:26:34 +0200197struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800198{
199 struct sgsn_mm_ctx *ctx;
200
201 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200202 if (p_tmsi == ctx->p_tmsi ||
203 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800204 return ctx;
205 }
206 return NULL;
207}
208
209struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
210{
211 struct sgsn_mm_ctx *ctx;
212
213 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
214 if (!strcmp(imsi, ctx->imsi))
215 return ctx;
216 }
217 return NULL;
218
219}
220
Alexander Couzens2b5fb8e2017-02-04 06:01:00 +0100221/* Allocate a new SGSN MM context for GERAN_Gb */
222struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_gb(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800223 const struct gprs_ra_id *raid)
224{
Harald Welte2720e732010-05-17 00:44:57 +0200225 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800226
Harald Welte2720e732010-05-17 00:44:57 +0200227 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800228 if (!ctx)
229 return NULL;
230
231 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100232 ctx->ran_type = MM_CTX_T_GERAN_Gb;
233 ctx->gb.tlli = tlli;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100234 ctx->gmm_state = GMM_DEREGISTERED;
Alexander Couzens10135502017-02-04 05:53:07 +0100235 ctx->pmm_state = MM_IDLE;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100236 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Maxb997f842016-07-06 15:57:01 +0200237 ctx->ciph_algo = sgsn->cfg.cipher;
Max549ebc72016-11-18 14:07:04 +0100238 LOGMMCTXP(LOGL_DEBUG, ctx, "Allocated with %s cipher.\n",
239 get_value_string(gprs_cipher_names, ctx->ciph_algo));
Harald Welte8acd88f2010-05-18 10:57:45 +0200240 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte26c14652017-07-12 00:25:51 +0200241 if (!ctx->ctrg) {
242 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group\n");
243 talloc_free(ctx);
244 return NULL;
245 }
Harald Welte6ffbaab2010-05-18 12:44:45 +0200246 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800247
248 llist_add(&ctx->list, &sgsn_mm_ctxts);
249
250 return ctx;
251}
Harald Welted193cb32010-05-17 22:58:03 +0200252
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200253/* Allocate a new SGSN MM context */
254struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx)
255{
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200256#if BUILD_IU
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200257 struct sgsn_mm_ctx *ctx;
Max794693c2017-12-20 11:38:01 +0100258 struct ranap_ue_conn_ctx *ue_ctx = uectx;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200259
260 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
261 if (!ctx)
262 return NULL;
263
264 ctx->ran_type = MM_CTX_T_UTRAN_Iu;
Max794693c2017-12-20 11:38:01 +0100265 ctx->iu.ue_ctx = ue_ctx;
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200266 ctx->iu.ue_ctx->rab_assign_addr_enc = sgsn->cfg.iu.rab_assign_addr_enc;
Daniel Willmann3ecfbbb2016-05-21 00:16:55 +0200267 ctx->iu.new_key = 1;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100268 ctx->gmm_state = GMM_DEREGISTERED;
Daniel Willmann5b2363e2016-05-21 00:01:21 +0200269 ctx->pmm_state = PMM_DETACHED;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200270 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Max794693c2017-12-20 11:38:01 +0100271 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, ue_ctx->conn_id);
Harald Welte26c14652017-07-12 00:25:51 +0200272 if (!ctx->ctrg) {
Max794693c2017-12-20 11:38:01 +0100273 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group for %s.%u\n",
274 mmctx_ctrg_desc.group_name_prefix, ue_ctx->conn_id);
Harald Welte26c14652017-07-12 00:25:51 +0200275 talloc_free(ctx);
276 return NULL;
277 }
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200278
279 /* Need to get RAID from IU conn */
280 ctx->ra = ctx->iu.ue_ctx->ra_id;
281
282 INIT_LLIST_HEAD(&ctx->pdp_list);
283
284 llist_add(&ctx->list, &sgsn_mm_ctxts);
285
286 return ctx;
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200287#else
288 return NULL;
289#endif
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200290}
291
292
Harald Welte7b022ee2012-07-14 12:04:04 +0200293/* this is a hard _free_ function, it doesn't clean up the PDP contexts
294 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200295static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100296{
297 struct sgsn_pdp_ctx *pdp, *pdp2;
298
Jacob Erlbecke671d252015-01-26 14:43:07 +0100299 /* Unlink from global list of MM contexts */
300 llist_del(&mm->list);
301
302 /* Free all PDP contexts */
303 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
304 sgsn_pdp_ctx_free(pdp);
305
306 rate_ctr_group_free(mm->ctrg);
307
308 talloc_free(mm);
309}
310
311void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
312{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200313 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100314 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100315 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200316 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100317
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200318 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
319 llme = mm->gb.llme;
320 else
321 OSMO_ASSERT(mm->gb.llme == NULL);
322
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800323 /* Forget about ongoing look-ups */
324 if (mm->ggsn_lookup) {
325 LOGMMCTXP(LOGL_NOTICE, mm,
326 "Cleaning mmctx with on-going query.\n");
327 mm->ggsn_lookup->mmctx = NULL;
328 mm->ggsn_lookup = NULL;
329 }
330
Jacob Erlbecke671d252015-01-26 14:43:07 +0100331 /* delete all existing PDP contexts for this MS */
332 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
333 LOGMMCTXP(LOGL_NOTICE, mm,
334 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
335 sgsn_pdp_ctx_terminate(pdp);
336 }
337
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200338 if (osmo_timer_pending(&mm->timer)) {
339 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
340 osmo_timer_del(&mm->timer);
341 }
342
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200343 memset(&sig_data, 0, sizeof(sig_data));
344 sig_data.mm = mm;
345 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
346
347
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100348 /* Detach from subscriber which is possibly freed then */
349 if (mm->subscr) {
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100350 struct gprs_subscr *subscr = gprs_subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100351 gprs_subscr_cleanup(subscr);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100352 gprs_subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100353 }
354
Jacob Erlbecke671d252015-01-26 14:43:07 +0100355 sgsn_mm_ctx_free(mm);
356 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100357
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200358 if (llme) {
359 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200360 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200361 }
Harald Weltec728eea2010-12-24 23:07:18 +0100362}
Harald Welte77289c22010-05-18 14:32:29 +0200363
Jacob Erlbecke671d252015-01-26 14:43:07 +0100364
Harald Welte96df6062010-06-03 06:37:26 +0200365/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200366struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
367 uint8_t nsapi)
368{
369 struct sgsn_pdp_ctx *pdp;
370
371 llist_for_each_entry(pdp, &mm->pdp_list, list) {
372 if (pdp->nsapi == nsapi)
373 return pdp;
374 }
375 return NULL;
376}
377
Harald Welte96df6062010-06-03 06:37:26 +0200378/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200379struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
380 uint8_t tid)
381{
382 struct sgsn_pdp_ctx *pdp;
383
384 llist_for_each_entry(pdp, &mm->pdp_list, list) {
385 if (pdp->ti == tid)
386 return pdp;
387 }
388 return NULL;
389}
390
Harald Welte7b022ee2012-07-14 12:04:04 +0200391/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200392struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
393 uint8_t nsapi)
394{
395 struct sgsn_pdp_ctx *pdp;
396
397 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
398 if (pdp)
399 return NULL;
400
401 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
402 if (!pdp)
403 return NULL;
404
405 pdp->mm = mm;
406 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200407 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welte26c14652017-07-12 00:25:51 +0200408 if (!pdp->ctrg) {
409 LOGPDPCTXP(LOGL_ERROR, pdp, "Error allocation counter group\n");
410 talloc_free(pdp);
411 return NULL;
412 }
Harald Welted193cb32010-05-17 22:58:03 +0200413 llist_add(&pdp->list, &mm->pdp_list);
414 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
415
416 return pdp;
417}
418
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200419/*
420 * This function will not trigger any GSM DEACT PDP ACK messages, so you
421 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
422 * isn't detached already.
423 */
424void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
425{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200426 struct sgsn_signal_data sig_data;
427
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200428 OSMO_ASSERT(pdp->mm != NULL);
429
430 /* There might still be pending callbacks in libgtp. So the parts of
431 * this object relevant to GTP need to remain intact in this case. */
432
433 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
434
Daniel Willmannf9f43872016-05-20 22:36:23 +0200435 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
436 /* Force the deactivation of the SNDCP layer */
437 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
438 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200439
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200440 memset(&sig_data, 0, sizeof(sig_data));
441 sig_data.pdp = pdp;
442 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
443
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200444 /* Detach from MM context */
445 llist_del(&pdp->list);
446 pdp->mm = NULL;
447
448 sgsn_delete_pdp_ctx(pdp);
449}
450
451/*
452 * Don't call this function directly unless you know what you are doing.
453 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
454 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
455 */
Harald Welted193cb32010-05-17 22:58:03 +0200456void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
457{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200458 struct sgsn_signal_data sig_data;
459
460 memset(&sig_data, 0, sizeof(sig_data));
461 sig_data.pdp = pdp;
462 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
463
Harald Welte376d5e52010-06-28 18:57:21 +0200464 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200465 if (pdp->mm)
466 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200467 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200468
469 /* _if_ we still have a library handle, at least set it to NULL
470 * to avoid any dereferences of the now-deleted PDP context from
471 * sgsn_libgtp:cb_data_ind() */
472 if (pdp->lib) {
473 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200474 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200475 "has a libgtp handle attached to it, this shouldn't "
476 "happen!\n");
477 osmo_generate_backtrace();
478 lib->priv = NULL;
479 }
480
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800481 if (pdp->destroy_ggsn)
482 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200483 talloc_free(pdp);
484}
485
486/* GGSN contexts */
487
Harald Welte77289c22010-05-18 14:32:29 +0200488struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200489{
Harald Welte77289c22010-05-18 14:32:29 +0200490 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200491
Harald Welte77289c22010-05-18 14:32:29 +0200492 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200493 if (!ggc)
494 return NULL;
495
496 ggc->id = id;
497 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100498 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200499 /* if we are called from config file parse, this gsn doesn't exist yet */
500 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200501 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200502
503 return ggc;
504}
505
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100506void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
507{
508 llist_del(&ggc->list);
509 talloc_free(ggc);
510}
511
Harald Welte77289c22010-05-18 14:32:29 +0200512struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200513{
Harald Welte77289c22010-05-18 14:32:29 +0200514 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200515
516 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
517 if (id == ggc->id)
518 return ggc;
519 }
520 return NULL;
521}
522
Harald Weltea9b473a2010-12-24 21:13:26 +0100523struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
524{
525 struct sgsn_ggsn_ctx *ggc;
526
527 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
528 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
529 return ggc;
530 }
531 return NULL;
532}
533
534
Harald Welte77289c22010-05-18 14:32:29 +0200535struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200536{
Harald Welte77289c22010-05-18 14:32:29 +0200537 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200538
Harald Welte77289c22010-05-18 14:32:29 +0200539 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200540 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200541 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200542 return ggc;
543}
544
545/* APN contexts */
546
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100547static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200548{
549 struct apn_ctx *actx;
550
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100551 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200552 if (!actx)
553 return NULL;
554 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100555 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
556
557 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200558
559 return actx;
560}
561
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100562void sgsn_apn_ctx_free(struct apn_ctx *actx)
563{
564 llist_del(&actx->list);
565 talloc_free(actx);
566}
567
568struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
569{
570 struct apn_ctx *actx;
571 struct apn_ctx *found_actx = NULL;
572 size_t imsi_prio = 0;
573 size_t name_prio = 0;
574 size_t name_req_len = strlen(name);
575
576 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
577 size_t name_ref_len, imsi_ref_len;
578 const char *name_ref_start, *name_match_start;
579
580 imsi_ref_len = strlen(actx->imsi_prefix);
581 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
582 continue;
583
584 if (imsi_ref_len < imsi_prio)
585 continue;
586
587 /* IMSI matches */
588
589 name_ref_start = &actx->name[0];
590 if (name_ref_start[0] == '*') {
591 /* Suffix match */
592 name_ref_start += 1;
593 name_ref_len = strlen(name_ref_start);
594 if (name_ref_len > name_req_len)
595 continue;
596 } else {
597 name_ref_len = strlen(name_ref_start);
598 if (name_ref_len != name_req_len)
599 continue;
600 }
601
602 name_match_start = name + (name_req_len - name_ref_len);
603 if (strcasecmp(name_match_start, name_ref_start) != 0)
604 continue;
605
606 /* IMSI and name match */
607
608 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
609 /* Lower priority, skip */
610 continue;
611
612 imsi_prio = imsi_ref_len;
613 name_prio = name_ref_len;
614 found_actx = actx;
615 }
616 return found_actx;
617}
618
619struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200620{
621 struct apn_ctx *actx;
622
623 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100624 if (strcasecmp(name, actx->name) == 0 &&
625 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200626 return actx;
627 }
628 return NULL;
629}
630
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100631struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200632{
633 struct apn_ctx *actx;
634
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100635 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200636 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100637 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200638
639 return actx;
640}
Harald Welte6463c072010-05-18 17:04:55 +0200641
642uint32_t sgsn_alloc_ptmsi(void)
643{
644 struct sgsn_mm_ctx *mm;
Alexander Couzens8a215c32017-02-03 23:22:18 +0100645 uint32_t ptmsi = 0xdeadbeef;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100646 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200647
648restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200649 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
650 goto failed;
651
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100652 /* Enforce that the 2 MSB are set without loosing the distance between
653 * identical values. Since rand() has no duplicate values within a
654 * period (because the size of the state is the same like the size of
655 * the random value), this leads to a distance of period/4 when the
656 * distribution of the 2 MSB is uniform. This approach fails with a
657 * probability of (3/4)^max_retries, only 1% of the approaches will
658 * need more than 16 numbers (even distribution assumed).
659 *
660 * Alternatively, a freeze list could be used if another PRNG is used
661 * or when this approach proves to be not sufficient.
662 */
663 if (ptmsi >= 0xC0000000) {
664 if (!max_retries--)
665 goto failed;
666 goto restart;
667 }
668 ptmsi |= 0xC0000000;
669
670 if (ptmsi == GSM_RESERVED_TMSI) {
671 if (!max_retries--)
672 goto failed;
673 goto restart;
674 }
675
Harald Welte6463c072010-05-18 17:04:55 +0200676 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200677 if (mm->p_tmsi == ptmsi) {
678 if (!max_retries--)
679 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200680 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200681 }
Harald Welte6463c072010-05-18 17:04:55 +0200682 }
683
684 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200685
686failed:
687 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
688 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200689}
Harald Weltea9b473a2010-12-24 21:13:26 +0100690
691static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
692{
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100693 if (pdp->mm->gmm_state == GMM_REGISTERED_NORMAL)
Harald Weltea9b473a2010-12-24 21:13:26 +0100694 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
695 else {
696 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200697 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100698 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200699 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100700 sgsn_pdp_ctx_free(pdp);
701 }
702}
703
704/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100705 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100706int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
707{
708 struct sgsn_mm_ctx *mm;
709 int num = 0;
710
711 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
712 struct sgsn_pdp_ctx *pdp;
713 llist_for_each_entry(pdp, &mm->pdp_list, list) {
714 if (pdp->ggsn == ggsn) {
715 drop_one_pdp(pdp);
716 num++;
717 }
718 }
719 }
720
721 return num;
722}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200723
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100724void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200725{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100726 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100727 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100728
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100729 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200730}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100731
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200732static void insert_extra(struct tlv_parsed *tp,
733 struct sgsn_subscriber_data *data,
734 struct sgsn_subscriber_pdp_data *pdp)
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400735{
736 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
737 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200738
739 /* Prefer PDP charging characteristics of per subscriber one */
740 if (pdp->has_pdp_charg) {
741 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(pdp->pdp_charg);
742 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &pdp->pdp_charg[0];
743 } else if (data->has_pdp_charg) {
744 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(data->pdp_charg);
745 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &data->pdp_charg[0];
746 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400747}
748
749/**
750 * The tlv_parsed tp parameter will be modified to insert a
751 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
752 * PDP context handling.
753 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100754struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
755 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800756 enum gsm48_gsm_cause *gsm_cause,
757 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100758{
759 char req_apn_str[GSM_APN_LENGTH] = {0};
760 const struct apn_ctx *apn_ctx = NULL;
761 const char *selected_apn_str = NULL;
762 struct sgsn_subscriber_pdp_data *pdp;
763 struct sgsn_ggsn_ctx *ggsn = NULL;
764 int allow_any_apn = 0;
765
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800766 out_apn_str[0] = '\0';
767
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100768 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
769 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
770 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
771 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
772 return NULL;
773 }
774
Harald Welte7e82b742017-08-12 13:43:54 +0200775 osmo_apn_to_str(req_apn_str,
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100776 TLVP_VAL(tp, GSM48_IE_GSM_APN),
777 TLVP_LEN(tp, GSM48_IE_GSM_APN));
778
779 if (strcmp(req_apn_str, "*") == 0)
780 req_apn_str[0] = 0;
781 }
782
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800783 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100784 allow_any_apn = 1;
785
786 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
787 /* No specific APN requested, check for an APN that is both
788 * granted and configured */
789
790 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
791 if (strcmp(pdp->apn_str, "*") == 0)
792 {
793 allow_any_apn = 1;
794 selected_apn_str = "";
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200795 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100796 continue;
797 }
798 if (!llist_empty(&sgsn_apn_ctxts)) {
799 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
800 /* Not configured */
801 if (apn_ctx == NULL)
802 continue;
803 }
Holger Hans Peter Freyther5db68572017-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 = pdp->apn_str;
806 break;
807 }
808 } else if (!allow_any_apn) {
809 /* Check whether the given APN is granted */
810 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
811 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200812 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100813 selected_apn_str = req_apn_str;
814 allow_any_apn = 1;
815 continue;
816 }
817 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200818 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100819 selected_apn_str = req_apn_str;
820 break;
821 }
822 }
823 } else if (strlen(req_apn_str) != 0) {
824 /* Any APN is allowed */
825 selected_apn_str = req_apn_str;
826 } else {
827 /* Prefer the GGSN associated with the wildcard APN */
828 selected_apn_str = "";
829 }
830
831 if (!allow_any_apn && selected_apn_str == NULL) {
832 /* Access not granted */
833 LOGMMCTXP(LOGL_NOTICE, mmctx,
834 "The requested APN '%s' is not allowed\n",
835 req_apn_str);
836 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
837 return NULL;
838 }
839
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800840 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200841 if (selected_apn_str)
842 strcpy(out_apn_str, selected_apn_str);
843 else
844 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800845
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100846 if (apn_ctx == NULL && selected_apn_str)
847 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
848
849 if (apn_ctx != NULL) {
850 ggsn = apn_ctx->ggsn;
851 } else if (llist_empty(&sgsn_apn_ctxts)) {
852 /* No configuration -> use GGSN 0 */
853 ggsn = sgsn_ggsn_ctx_by_id(0);
854 } else if (allow_any_apn &&
855 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
856 /* No APN given and no default configuration -> Use GGSN 0 */
857 ggsn = sgsn_ggsn_ctx_by_id(0);
858 } else {
859 /* No matching configuration found */
860 LOGMMCTXP(LOGL_NOTICE, mmctx,
861 "The selected APN '%s' has not been configured\n",
862 selected_apn_str);
863 *gsm_cause = GSM_CAUSE_MISSING_APN;
864 return NULL;
865 }
866
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800867 if (!ggsn) {
868 LOGMMCTXP(LOGL_NOTICE, mmctx,
869 "No static GGSN configured. Selected APN '%s'\n",
870 selected_apn_str);
871 return NULL;
872 }
873
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100874 LOGMMCTXP(LOGL_INFO, mmctx,
875 "Found GGSN %d for APN '%s' (requested '%s')\n",
876 ggsn->id, selected_apn_str ? selected_apn_str : "---",
877 req_apn_str);
878
879 return ggsn;
880}
881
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100882static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
883{
884 struct sgsn_mm_ctx *mmctx = NULL;
885
886 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100887 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100888 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
889 return;
890 }
891 }
892
893 /* No MM context found */
894 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
895 llme->tlli);
Max39550252016-06-28 17:39:20 +0200896 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100897}
898
899static void sgsn_llme_check_cb(void *data_)
900{
901 struct gprs_llc_llme *llme, *llme_tmp;
902 struct timespec now_tp;
903 time_t now, age;
904 time_t max_age = gprs_max_time_to_idle();
905
906 int rc;
907
908 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
909 OSMO_ASSERT(rc >= 0);
910 now = now_tp.tv_sec;
911
912 LOGP(DGPRS, LOGL_DEBUG,
913 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
914
915 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
916 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
917 llme->age_timestamp = now;
918
919 age = now - llme->age_timestamp;
920
921 if (age > max_age || age < 0) {
922 LOGP(DGPRS, LOGL_INFO,
923 "Inactivity timeout for TLLI 0x%08x, age %d\n",
924 llme->tlli, (int)age);
925 sgsn_llme_cleanup_free(llme);
926 }
927 }
928
929 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
930}
931
932void sgsn_inst_init()
933{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200934 osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100935 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
936}
937