blob: a58e504c5ee741fddc60e347341b7686cfd86195 [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
Neels Hofmeyra7a39472017-07-05 15:19:52 +020049#include "../../bscconfig.h"
50
51#if BUILD_IU
52#include <osmocom/ranap/iu_client.h>
53#endif
54
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010055#define GPRS_LLME_CHECK_TICK 30
56
Harald Welteab1d5622010-05-18 19:58:38 +020057extern struct sgsn_instance *sgsn;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020058extern void *tall_bsc_ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +080059
Harald Welted193cb32010-05-17 22:58:03 +020060LLIST_HEAD(sgsn_mm_ctxts);
61LLIST_HEAD(sgsn_ggsn_ctxts);
62LLIST_HEAD(sgsn_apn_ctxts);
63LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080064
Harald Welte8acd88f2010-05-18 10:57:45 +020065static const struct rate_ctr_desc mmctx_ctr_description[] = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010066 { "sign:packets:in", "Signalling Messages ( In)" },
67 { "sign:packets:out", "Signalling Messages (Out)" },
68 { "udata:packets:in", "User Data Messages ( In)" },
69 { "udata:packets:out", "User Data Messages (Out)" },
70 { "udata:bytes:in", "User Data Bytes ( In)" },
71 { "udata:bytes:out", "User Data Bytes (Out)" },
Harald Welte8acd88f2010-05-18 10:57:45 +020072 { "pdp_ctx_act", "PDP Context Activations " },
73 { "suspend", "SUSPEND Count " },
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010074 { "paging:ps", "Paging Packet Switched " },
75 { "paging:cs", "Paging Circuit Switched " },
Harald Welte8acd88f2010-05-18 10:57:45 +020076 { "ra_update", "Routing Area Update " },
77};
78
79static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010080 .group_name_prefix = "sgsn:mmctx",
Harald Welte8acd88f2010-05-18 10:57:45 +020081 .group_description = "SGSN MM Context Statistics",
82 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
83 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010084 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020085};
86
Harald Welteefbdee92010-06-10 00:20:12 +020087static const struct rate_ctr_desc pdpctx_ctr_description[] = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010088 { "udata:packets:in", "User Data Messages ( In)" },
89 { "udata:packets:out", "User Data Messages (Out)" },
90 { "udata:bytes:in", "User Data Bytes ( In)" },
91 { "udata:bytes:out", "User Data Bytes (Out)" },
Harald Welteefbdee92010-06-10 00:20:12 +020092};
93
94static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
Pau Espin Pedroldc730a32017-11-28 19:40:34 +010095 .group_name_prefix = "sgsn:pdpctx",
Harald Welteefbdee92010-06-10 00:20:12 +020096 .group_description = "SGSN PDP Context Statistics",
97 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
98 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010099 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +0200100};
101
Alexander Couzens14314bd2016-07-05 09:52:52 +0200102static const struct rate_ctr_desc sgsn_ctr_description[] = {
Harald Welteb68413b2017-11-21 08:51:47 +0100103 { "llc:dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" },
104 { "llc:ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" },
105 { "llc:dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" },
106 { "llc:ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" },
107 { "gprs:attach_requested", "Received attach requests" },
108 { "gprs:attach_accepted", "Sent attach accepts" },
109 { "gprs:attach_rejected", "Sent attach rejects" },
110 { "gprs:detach_requested", "Received detach requests" },
111 { "gprs:detach_acked", "Sent detach acks" },
112 { "gprs:routing_area_requested", "Received routing area requests" },
113 { "gprs:routing_area_requested", "Sent routing area acks" },
114 { "gprs:routing_area_requested", "Sent routing area rejects" },
115 { "pdp:activate_requested", "Received activate requests" },
116 { "pdp:activate_rejected", "Sent activate rejects" },
117 { "pdp:activate_accepted", "Sent activate accepts" },
118 { "pdp:request_activated", "unused" },
119 { "pdp:request_activate_rejected", "unused" },
120 { "pdp:modify_requested", "unused" },
121 { "pdp:modify_accepted", "unused" },
122 { "pdp:dl_deactivate_requested", "Sent deactivate requests" },
123 { "pdp:dl_deactivate_accepted", "Sent deactivate accepted" },
124 { "pdp:ul_deactivate_requested", "Received deactivate requests" },
125 { "pdp:ul_deactivate_accepted", "Received deactivate accepts" },
Alexander Couzens14314bd2016-07-05 09:52:52 +0200126};
127
128static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
129 "sgsn",
130 "SGSN Overall Statistics",
131 OSMO_STATS_CLASS_GLOBAL,
132 ARRAY_SIZE(sgsn_ctr_description),
133 sgsn_ctr_description,
134};
135
136void sgsn_rate_ctr_init() {
137 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0);
Harald Welte26c14652017-07-12 00:25:51 +0200138 OSMO_ASSERT(sgsn->rate_ctrs);
Alexander Couzens14314bd2016-07-05 09:52:52 +0200139}
140
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200141/* look-up an SGSN MM context based on Iu UE context (struct ue_conn_ctx)*/
142struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx)
143{
144 struct sgsn_mm_ctx *ctx;
145
146 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
147 if (ctx->ran_type == MM_CTX_T_UTRAN_Iu
148 && uectx == ctx->iu.ue_ctx)
149 return ctx;
150 }
151
152 return NULL;
153}
154
Harald Welte9b455bf2010-03-14 15:45:01 +0800155/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200156struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800157 const struct gprs_ra_id *raid)
158{
159 struct sgsn_mm_ctx *ctx;
160
161 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100162 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100163 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800164 return ctx;
165 }
Harald Welteab1d5622010-05-18 19:58:38 +0200166
Harald Welte9b455bf2010-03-14 15:45:01 +0800167 return NULL;
168}
169
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100170struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
171 const struct gprs_ra_id *raid)
172{
173 struct sgsn_mm_ctx *ctx;
174 int tlli_type;
175
176 /* TODO: Also check the P_TMSI signature to be safe. That signature
177 * should be different (at least with a sufficiently high probability)
178 * after SGSN restarts and for multiple SGSN instances.
179 */
180
181 tlli_type = gprs_tlli_type(tlli);
182 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
183 return NULL;
184
185 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
186 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
187 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
188 gprs_ra_id_equals(raid, &ctx->ra))
189 return ctx;
190 }
191
192 return NULL;
193}
194
Harald Welteeaa614c2010-05-02 11:26:34 +0200195struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800196{
197 struct sgsn_mm_ctx *ctx;
198
199 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200200 if (p_tmsi == ctx->p_tmsi ||
201 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800202 return ctx;
203 }
204 return NULL;
205}
206
207struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
208{
209 struct sgsn_mm_ctx *ctx;
210
211 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
212 if (!strcmp(imsi, ctx->imsi))
213 return ctx;
214 }
215 return NULL;
216
217}
218
Alexander Couzens2b5fb8e2017-02-04 06:01:00 +0100219/* Allocate a new SGSN MM context for GERAN_Gb */
220struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_gb(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800221 const struct gprs_ra_id *raid)
222{
Harald Welte2720e732010-05-17 00:44:57 +0200223 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800224
Harald Welte2720e732010-05-17 00:44:57 +0200225 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800226 if (!ctx)
227 return NULL;
228
229 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100230 ctx->ran_type = MM_CTX_T_GERAN_Gb;
231 ctx->gb.tlli = tlli;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100232 ctx->gmm_state = GMM_DEREGISTERED;
Alexander Couzens10135502017-02-04 05:53:07 +0100233 ctx->pmm_state = MM_IDLE;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100234 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Maxb997f842016-07-06 15:57:01 +0200235 ctx->ciph_algo = sgsn->cfg.cipher;
Max549ebc72016-11-18 14:07:04 +0100236 LOGMMCTXP(LOGL_DEBUG, ctx, "Allocated with %s cipher.\n",
237 get_value_string(gprs_cipher_names, ctx->ciph_algo));
Harald Welte8acd88f2010-05-18 10:57:45 +0200238 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte26c14652017-07-12 00:25:51 +0200239 if (!ctx->ctrg) {
240 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group\n");
241 talloc_free(ctx);
242 return NULL;
243 }
Harald Welte6ffbaab2010-05-18 12:44:45 +0200244 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800245
246 llist_add(&ctx->list, &sgsn_mm_ctxts);
247
248 return ctx;
249}
Harald Welted193cb32010-05-17 22:58:03 +0200250
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200251/* Allocate a new SGSN MM context */
252struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx)
253{
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200254#if BUILD_IU
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200255 struct sgsn_mm_ctx *ctx;
Max794693c2017-12-20 11:38:01 +0100256 struct ranap_ue_conn_ctx *ue_ctx = uectx;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200257
258 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
259 if (!ctx)
260 return NULL;
261
262 ctx->ran_type = MM_CTX_T_UTRAN_Iu;
Max794693c2017-12-20 11:38:01 +0100263 ctx->iu.ue_ctx = ue_ctx;
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200264 ctx->iu.ue_ctx->rab_assign_addr_enc = sgsn->cfg.iu.rab_assign_addr_enc;
Daniel Willmann3ecfbbb2016-05-21 00:16:55 +0200265 ctx->iu.new_key = 1;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100266 ctx->gmm_state = GMM_DEREGISTERED;
Daniel Willmann5b2363e2016-05-21 00:01:21 +0200267 ctx->pmm_state = PMM_DETACHED;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200268 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Max794693c2017-12-20 11:38:01 +0100269 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, ue_ctx->conn_id);
Harald Welte26c14652017-07-12 00:25:51 +0200270 if (!ctx->ctrg) {
Max794693c2017-12-20 11:38:01 +0100271 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group for %s.%u\n",
272 mmctx_ctrg_desc.group_name_prefix, ue_ctx->conn_id);
Harald Welte26c14652017-07-12 00:25:51 +0200273 talloc_free(ctx);
274 return NULL;
275 }
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200276
277 /* Need to get RAID from IU conn */
278 ctx->ra = ctx->iu.ue_ctx->ra_id;
279
280 INIT_LLIST_HEAD(&ctx->pdp_list);
281
282 llist_add(&ctx->list, &sgsn_mm_ctxts);
283
284 return ctx;
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200285#else
286 return NULL;
287#endif
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200288}
289
290
Harald Welte7b022ee2012-07-14 12:04:04 +0200291/* this is a hard _free_ function, it doesn't clean up the PDP contexts
292 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200293static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100294{
295 struct sgsn_pdp_ctx *pdp, *pdp2;
296
Jacob Erlbecke671d252015-01-26 14:43:07 +0100297 /* Unlink from global list of MM contexts */
298 llist_del(&mm->list);
299
300 /* Free all PDP contexts */
301 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
302 sgsn_pdp_ctx_free(pdp);
303
304 rate_ctr_group_free(mm->ctrg);
305
306 talloc_free(mm);
307}
308
309void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
310{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200311 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100312 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100313 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200314 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100315
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200316 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
317 llme = mm->gb.llme;
318 else
319 OSMO_ASSERT(mm->gb.llme == NULL);
320
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800321 /* Forget about ongoing look-ups */
322 if (mm->ggsn_lookup) {
323 LOGMMCTXP(LOGL_NOTICE, mm,
324 "Cleaning mmctx with on-going query.\n");
325 mm->ggsn_lookup->mmctx = NULL;
326 mm->ggsn_lookup = NULL;
327 }
328
Jacob Erlbecke671d252015-01-26 14:43:07 +0100329 /* delete all existing PDP contexts for this MS */
330 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
331 LOGMMCTXP(LOGL_NOTICE, mm,
332 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
333 sgsn_pdp_ctx_terminate(pdp);
334 }
335
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200336 if (osmo_timer_pending(&mm->timer)) {
337 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
338 osmo_timer_del(&mm->timer);
339 }
340
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200341 memset(&sig_data, 0, sizeof(sig_data));
342 sig_data.mm = mm;
343 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
344
345
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100346 /* Detach from subscriber which is possibly freed then */
347 if (mm->subscr) {
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100348 struct gprs_subscr *subscr = gprs_subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100349 gprs_subscr_cleanup(subscr);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100350 gprs_subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100351 }
352
Jacob Erlbecke671d252015-01-26 14:43:07 +0100353 sgsn_mm_ctx_free(mm);
354 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100355
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200356 if (llme) {
357 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200358 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200359 }
Harald Weltec728eea2010-12-24 23:07:18 +0100360}
Harald Welte77289c22010-05-18 14:32:29 +0200361
Jacob Erlbecke671d252015-01-26 14:43:07 +0100362
Harald Welte96df6062010-06-03 06:37:26 +0200363/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200364struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
365 uint8_t nsapi)
366{
367 struct sgsn_pdp_ctx *pdp;
368
369 llist_for_each_entry(pdp, &mm->pdp_list, list) {
370 if (pdp->nsapi == nsapi)
371 return pdp;
372 }
373 return NULL;
374}
375
Harald Welte96df6062010-06-03 06:37:26 +0200376/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200377struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
378 uint8_t tid)
379{
380 struct sgsn_pdp_ctx *pdp;
381
382 llist_for_each_entry(pdp, &mm->pdp_list, list) {
383 if (pdp->ti == tid)
384 return pdp;
385 }
386 return NULL;
387}
388
Harald Welte7b022ee2012-07-14 12:04:04 +0200389/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200390struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200391 struct sgsn_ggsn_ctx *ggsn,
Harald Welted193cb32010-05-17 22:58:03 +0200392 uint8_t nsapi)
393{
394 struct sgsn_pdp_ctx *pdp;
395
396 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
397 if (pdp)
398 return NULL;
399
400 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
401 if (!pdp)
402 return NULL;
403
404 pdp->mm = mm;
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200405 pdp->ggsn = ggsn;
Harald Welted193cb32010-05-17 22:58:03 +0200406 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);
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200414 llist_add(&pdp->ggsn_list, &ggsn->pdp_list);
Harald Welted193cb32010-05-17 22:58:03 +0200415 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
416
417 return pdp;
418}
419
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200420/*
421 * This function will not trigger any GSM DEACT PDP ACK messages, so you
422 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
423 * isn't detached already.
424 */
425void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
426{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200427 struct sgsn_signal_data sig_data;
428
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200429 OSMO_ASSERT(pdp->mm != NULL);
430
431 /* There might still be pending callbacks in libgtp. So the parts of
432 * this object relevant to GTP need to remain intact in this case. */
433
434 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
435
Daniel Willmannf9f43872016-05-20 22:36:23 +0200436 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
437 /* Force the deactivation of the SNDCP layer */
438 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
439 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200440
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200441 memset(&sig_data, 0, sizeof(sig_data));
442 sig_data.pdp = pdp;
443 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
444
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200445 /* Detach from MM context */
446 llist_del(&pdp->list);
447 pdp->mm = NULL;
448
449 sgsn_delete_pdp_ctx(pdp);
450}
451
452/*
453 * Don't call this function directly unless you know what you are doing.
454 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
455 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
456 */
Harald Welted193cb32010-05-17 22:58:03 +0200457void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
458{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200459 struct sgsn_signal_data sig_data;
460
461 memset(&sig_data, 0, sizeof(sig_data));
462 sig_data.pdp = pdp;
463 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
464
Harald Welte376d5e52010-06-28 18:57:21 +0200465 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200466 if (pdp->mm)
467 llist_del(&pdp->list);
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200468 if (pdp->ggsn)
469 llist_del(&pdp->ggsn_list);
Harald Welted193cb32010-05-17 22:58:03 +0200470 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200471
472 /* _if_ we still have a library handle, at least set it to NULL
473 * to avoid any dereferences of the now-deleted PDP context from
474 * sgsn_libgtp:cb_data_ind() */
475 if (pdp->lib) {
476 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200477 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200478 "has a libgtp handle attached to it, this shouldn't "
479 "happen!\n");
480 osmo_generate_backtrace();
481 lib->priv = NULL;
482 }
483
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800484 if (pdp->destroy_ggsn)
485 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200486 talloc_free(pdp);
487}
488
489/* GGSN contexts */
490
Harald Welte77289c22010-05-18 14:32:29 +0200491struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200492{
Harald Welte77289c22010-05-18 14:32:29 +0200493 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200494
Harald Welte77289c22010-05-18 14:32:29 +0200495 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200496 if (!ggc)
497 return NULL;
498
499 ggc->id = id;
500 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100501 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200502 /* if we are called from config file parse, this gsn doesn't exist yet */
503 ggc->gsn = sgsn->gsn;
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200504 INIT_LLIST_HEAD(&ggc->pdp_list);
Harald Welte119c2ba2010-05-18 18:39:00 +0200505 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200506
507 return ggc;
508}
509
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100510void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
511{
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200512 OSMO_ASSERT(llist_empty(&ggc->pdp_list));
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100513 llist_del(&ggc->list);
514 talloc_free(ggc);
515}
516
Harald Welte77289c22010-05-18 14:32:29 +0200517struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200518{
Harald Welte77289c22010-05-18 14:32:29 +0200519 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200520
521 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
522 if (id == ggc->id)
523 return ggc;
524 }
525 return NULL;
526}
527
Harald Weltea9b473a2010-12-24 21:13:26 +0100528struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
529{
530 struct sgsn_ggsn_ctx *ggc;
531
532 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
533 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
534 return ggc;
535 }
536 return NULL;
537}
538
539
Harald Welte77289c22010-05-18 14:32:29 +0200540struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200541{
Harald Welte77289c22010-05-18 14:32:29 +0200542 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200543
Harald Welte77289c22010-05-18 14:32:29 +0200544 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200545 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200546 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200547 return ggc;
548}
549
550/* APN contexts */
551
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100552static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200553{
554 struct apn_ctx *actx;
555
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100556 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200557 if (!actx)
558 return NULL;
559 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100560 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
561
562 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200563
564 return actx;
565}
566
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100567void sgsn_apn_ctx_free(struct apn_ctx *actx)
568{
569 llist_del(&actx->list);
570 talloc_free(actx);
571}
572
573struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
574{
575 struct apn_ctx *actx;
576 struct apn_ctx *found_actx = NULL;
577 size_t imsi_prio = 0;
578 size_t name_prio = 0;
579 size_t name_req_len = strlen(name);
580
581 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
582 size_t name_ref_len, imsi_ref_len;
583 const char *name_ref_start, *name_match_start;
584
585 imsi_ref_len = strlen(actx->imsi_prefix);
586 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
587 continue;
588
589 if (imsi_ref_len < imsi_prio)
590 continue;
591
592 /* IMSI matches */
593
594 name_ref_start = &actx->name[0];
595 if (name_ref_start[0] == '*') {
596 /* Suffix match */
597 name_ref_start += 1;
598 name_ref_len = strlen(name_ref_start);
599 if (name_ref_len > name_req_len)
600 continue;
601 } else {
602 name_ref_len = strlen(name_ref_start);
603 if (name_ref_len != name_req_len)
604 continue;
605 }
606
607 name_match_start = name + (name_req_len - name_ref_len);
608 if (strcasecmp(name_match_start, name_ref_start) != 0)
609 continue;
610
611 /* IMSI and name match */
612
613 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
614 /* Lower priority, skip */
615 continue;
616
617 imsi_prio = imsi_ref_len;
618 name_prio = name_ref_len;
619 found_actx = actx;
620 }
621 return found_actx;
622}
623
624struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200625{
626 struct apn_ctx *actx;
627
628 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100629 if (strcasecmp(name, actx->name) == 0 &&
630 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200631 return actx;
632 }
633 return NULL;
634}
635
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100636struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200637{
638 struct apn_ctx *actx;
639
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100640 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200641 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100642 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200643
644 return actx;
645}
Harald Welte6463c072010-05-18 17:04:55 +0200646
647uint32_t sgsn_alloc_ptmsi(void)
648{
649 struct sgsn_mm_ctx *mm;
Alexander Couzens8a215c32017-02-03 23:22:18 +0100650 uint32_t ptmsi = 0xdeadbeef;
Max3b6332f2017-11-01 13:28:38 +0100651 int max_retries = 100, rc = 0;
Harald Welte6463c072010-05-18 17:04:55 +0200652
653restart:
Max3b6332f2017-11-01 13:28:38 +0100654 rc = osmo_get_rand_id((uint8_t *) &ptmsi, sizeof(ptmsi));
655 if (rc < 0)
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200656 goto failed;
657
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100658 /* Enforce that the 2 MSB are set without loosing the distance between
659 * identical values. Since rand() has no duplicate values within a
660 * period (because the size of the state is the same like the size of
661 * the random value), this leads to a distance of period/4 when the
662 * distribution of the 2 MSB is uniform. This approach fails with a
663 * probability of (3/4)^max_retries, only 1% of the approaches will
664 * need more than 16 numbers (even distribution assumed).
665 *
666 * Alternatively, a freeze list could be used if another PRNG is used
667 * or when this approach proves to be not sufficient.
668 */
669 if (ptmsi >= 0xC0000000) {
670 if (!max_retries--)
671 goto failed;
672 goto restart;
673 }
674 ptmsi |= 0xC0000000;
675
676 if (ptmsi == GSM_RESERVED_TMSI) {
677 if (!max_retries--)
678 goto failed;
679 goto restart;
680 }
681
Harald Welte6463c072010-05-18 17:04:55 +0200682 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200683 if (mm->p_tmsi == ptmsi) {
684 if (!max_retries--)
685 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200686 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200687 }
Harald Welte6463c072010-05-18 17:04:55 +0200688 }
689
690 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200691
692failed:
Max3b6332f2017-11-01 13:28:38 +0100693 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI: %d (%s)\n", rc, strerror(-rc));
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200694 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200695}
Harald Weltea9b473a2010-12-24 21:13:26 +0100696
697static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
698{
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100699 if (pdp->mm->gmm_state == GMM_REGISTERED_NORMAL)
Harald Weltea9b473a2010-12-24 21:13:26 +0100700 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
701 else {
702 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200703 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100704 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200705 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100706 sgsn_pdp_ctx_free(pdp);
707 }
708}
709
710/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100711 * otherwise lost state (recovery procedure) */
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200712int sgsn_ggsn_ctx_drop_all_pdp(struct sgsn_ggsn_ctx *ggsn)
Harald Weltea9b473a2010-12-24 21:13:26 +0100713{
Harald Weltea9b473a2010-12-24 21:13:26 +0100714 int num = 0;
715
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200716 struct sgsn_pdp_ctx *pdp, *pdp2;
717 llist_for_each_entry_safe(pdp, pdp2, &ggsn->pdp_list, ggsn_list) {
718 drop_one_pdp(pdp);
719 num++;
Harald Weltea9b473a2010-12-24 21:13:26 +0100720 }
721
722 return num;
723}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200724
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100725void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200726{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100727 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100728 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100729
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100730 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200731}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100732
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200733static void insert_extra(struct tlv_parsed *tp,
734 struct sgsn_subscriber_data *data,
735 struct sgsn_subscriber_pdp_data *pdp)
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400736{
737 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
738 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200739
740 /* Prefer PDP charging characteristics of per subscriber one */
741 if (pdp->has_pdp_charg) {
742 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(pdp->pdp_charg);
743 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &pdp->pdp_charg[0];
744 } else if (data->has_pdp_charg) {
745 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(data->pdp_charg);
746 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &data->pdp_charg[0];
747 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400748}
749
750/**
751 * The tlv_parsed tp parameter will be modified to insert a
752 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
753 * PDP context handling.
754 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100755struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
756 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800757 enum gsm48_gsm_cause *gsm_cause,
758 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100759{
760 char req_apn_str[GSM_APN_LENGTH] = {0};
761 const struct apn_ctx *apn_ctx = NULL;
762 const char *selected_apn_str = NULL;
763 struct sgsn_subscriber_pdp_data *pdp;
764 struct sgsn_ggsn_ctx *ggsn = NULL;
765 int allow_any_apn = 0;
766
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800767 out_apn_str[0] = '\0';
768
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100769 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
770 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
771 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
772 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
773 return NULL;
774 }
775
Harald Welte7e82b742017-08-12 13:43:54 +0200776 osmo_apn_to_str(req_apn_str,
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100777 TLVP_VAL(tp, GSM48_IE_GSM_APN),
778 TLVP_LEN(tp, GSM48_IE_GSM_APN));
779
780 if (strcmp(req_apn_str, "*") == 0)
781 req_apn_str[0] = 0;
782 }
783
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800784 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100785 allow_any_apn = 1;
786
787 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
788 /* No specific APN requested, check for an APN that is both
789 * granted and configured */
790
791 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
792 if (strcmp(pdp->apn_str, "*") == 0)
793 {
794 allow_any_apn = 1;
795 selected_apn_str = "";
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200796 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100797 continue;
798 }
799 if (!llist_empty(&sgsn_apn_ctxts)) {
800 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
801 /* Not configured */
802 if (apn_ctx == NULL)
803 continue;
804 }
Holger Hans Peter Freyther5db68572017-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 = pdp->apn_str;
807 break;
808 }
809 } else if (!allow_any_apn) {
810 /* Check whether the given APN is granted */
811 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
812 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200813 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100814 selected_apn_str = req_apn_str;
815 allow_any_apn = 1;
816 continue;
817 }
818 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200819 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100820 selected_apn_str = req_apn_str;
821 break;
822 }
823 }
824 } else if (strlen(req_apn_str) != 0) {
825 /* Any APN is allowed */
826 selected_apn_str = req_apn_str;
827 } else {
828 /* Prefer the GGSN associated with the wildcard APN */
829 selected_apn_str = "";
830 }
831
832 if (!allow_any_apn && selected_apn_str == NULL) {
833 /* Access not granted */
834 LOGMMCTXP(LOGL_NOTICE, mmctx,
835 "The requested APN '%s' is not allowed\n",
836 req_apn_str);
837 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
838 return NULL;
839 }
840
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800841 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200842 if (selected_apn_str)
843 strcpy(out_apn_str, selected_apn_str);
844 else
845 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800846
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100847 if (apn_ctx == NULL && selected_apn_str)
848 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
849
850 if (apn_ctx != NULL) {
851 ggsn = apn_ctx->ggsn;
852 } else if (llist_empty(&sgsn_apn_ctxts)) {
853 /* No configuration -> use GGSN 0 */
854 ggsn = sgsn_ggsn_ctx_by_id(0);
855 } else if (allow_any_apn &&
856 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
857 /* No APN given and no default configuration -> Use GGSN 0 */
858 ggsn = sgsn_ggsn_ctx_by_id(0);
859 } else {
860 /* No matching configuration found */
861 LOGMMCTXP(LOGL_NOTICE, mmctx,
862 "The selected APN '%s' has not been configured\n",
863 selected_apn_str);
864 *gsm_cause = GSM_CAUSE_MISSING_APN;
865 return NULL;
866 }
867
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800868 if (!ggsn) {
869 LOGMMCTXP(LOGL_NOTICE, mmctx,
870 "No static GGSN configured. Selected APN '%s'\n",
871 selected_apn_str);
872 return NULL;
873 }
874
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100875 LOGMMCTXP(LOGL_INFO, mmctx,
876 "Found GGSN %d for APN '%s' (requested '%s')\n",
877 ggsn->id, selected_apn_str ? selected_apn_str : "---",
878 req_apn_str);
879
880 return ggsn;
881}
882
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100883static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
884{
885 struct sgsn_mm_ctx *mmctx = NULL;
886
887 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100888 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100889 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
890 return;
891 }
892 }
893
894 /* No MM context found */
895 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
896 llme->tlli);
Max39550252016-06-28 17:39:20 +0200897 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100898}
899
900static void sgsn_llme_check_cb(void *data_)
901{
902 struct gprs_llc_llme *llme, *llme_tmp;
903 struct timespec now_tp;
904 time_t now, age;
905 time_t max_age = gprs_max_time_to_idle();
906
907 int rc;
908
909 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
910 OSMO_ASSERT(rc >= 0);
911 now = now_tp.tv_sec;
912
913 LOGP(DGPRS, LOGL_DEBUG,
914 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
915
916 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
917 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
918 llme->age_timestamp = now;
919
920 age = now - llme->age_timestamp;
921
922 if (age > max_age || age < 0) {
923 LOGP(DGPRS, LOGL_INFO,
924 "Inactivity timeout for TLLI 0x%08x, age %d\n",
925 llme->tlli, (int)age);
926 sgsn_llme_cleanup_free(llme);
927 }
928 }
929
930 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
931}
932
933void sgsn_inst_init()
934{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200935 osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100936 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
937}
938