blob: 9a5de4108af2a1cd9021baa2c8d6ab13ea6a5c34 [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>
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +010035#include <osmocom/gsm/gsup.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080036
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020037#include <osmocom/sgsn/gprs_subscriber.h>
38#include <osmocom/sgsn/debug.h>
39#include <osmocom/sgsn/gprs_sgsn.h>
40#include <osmocom/sgsn/sgsn.h>
41#include <osmocom/sgsn/gprs_gmm.h>
42#include <osmocom/sgsn/gprs_utils.h>
43#include <osmocom/sgsn/signal.h>
Alexander Couzensf7198d72018-05-22 18:29:14 +020044#include <osmocom/sgsn/gprs_gmm_attach.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020045#include <osmocom/sgsn/gprs_llc.h>
Harald Welteab1d5622010-05-18 19:58:38 +020046
Neels Hofmeyrf4daf162016-05-21 00:44:50 +020047#include <pdp.h>
48
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010049#include <time.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;
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010060extern void *tall_sgsn_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" },
Ruben Undheim55fcf112018-09-25 22:59:34 +0200106 { "llc:ul_bytes", "Count successful received LLC bytes (encrypt & fcs correct)" },
107 { "llc:dl_packets", "Count successful sent LLC packets before giving it to the bssgp layer" },
108 { "llc:ul_packets", "Count successful received LLC packets (encrypt & fcs correct)" },
Harald Welteb68413b2017-11-21 08:51:47 +0100109 { "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() {
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100139 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_sgsn_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
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100227 ctx = talloc_zero(tall_sgsn_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 }
Alexander Couzensf7198d72018-05-22 18:29:14 +0200246 ctx->gmm_att_req.fsm = osmo_fsm_inst_alloc(&gmm_attach_req_fsm, ctx, ctx, LOGL_DEBUG, "gb_gmm_req");
Harald Welte6ffbaab2010-05-18 12:44:45 +0200247 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800248
249 llist_add(&ctx->list, &sgsn_mm_ctxts);
250
251 return ctx;
252}
Harald Welted193cb32010-05-17 22:58:03 +0200253
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200254/* Allocate a new SGSN MM context */
255struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx)
256{
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200257#if BUILD_IU
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200258 struct sgsn_mm_ctx *ctx;
Max794693c2017-12-20 11:38:01 +0100259 struct ranap_ue_conn_ctx *ue_ctx = uectx;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200260
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100261 ctx = talloc_zero(tall_sgsn_ctx, struct sgsn_mm_ctx);
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200262 if (!ctx)
263 return NULL;
264
265 ctx->ran_type = MM_CTX_T_UTRAN_Iu;
Max794693c2017-12-20 11:38:01 +0100266 ctx->iu.ue_ctx = ue_ctx;
Neels Hofmeyr2188a772016-05-20 21:59:55 +0200267 ctx->iu.ue_ctx->rab_assign_addr_enc = sgsn->cfg.iu.rab_assign_addr_enc;
Daniel Willmann3ecfbbb2016-05-21 00:16:55 +0200268 ctx->iu.new_key = 1;
Alexander Couzens4f8da6d2017-01-31 15:34:26 +0100269 ctx->gmm_state = GMM_DEREGISTERED;
Daniel Willmann5b2363e2016-05-21 00:01:21 +0200270 ctx->pmm_state = PMM_DETACHED;
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200271 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Max794693c2017-12-20 11:38:01 +0100272 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, ue_ctx->conn_id);
Harald Welte26c14652017-07-12 00:25:51 +0200273 if (!ctx->ctrg) {
Max794693c2017-12-20 11:38:01 +0100274 LOGMMCTXP(LOGL_ERROR, ctx, "Cannot allocate counter group for %s.%u\n",
275 mmctx_ctrg_desc.group_name_prefix, ue_ctx->conn_id);
Harald Welte26c14652017-07-12 00:25:51 +0200276 talloc_free(ctx);
277 return NULL;
278 }
Alexander Couzensf7198d72018-05-22 18:29:14 +0200279 ctx->gmm_att_req.fsm = osmo_fsm_inst_alloc(&gmm_attach_req_fsm, ctx, ctx, LOGL_DEBUG, "gb_gmm_req");
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200280
281 /* Need to get RAID from IU conn */
282 ctx->ra = ctx->iu.ue_ctx->ra_id;
283
284 INIT_LLIST_HEAD(&ctx->pdp_list);
285
286 llist_add(&ctx->list, &sgsn_mm_ctxts);
287
288 return ctx;
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200289#else
290 return NULL;
291#endif
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200292}
293
294
Harald Welte7b022ee2012-07-14 12:04:04 +0200295/* this is a hard _free_ function, it doesn't clean up the PDP contexts
296 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200297static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100298{
299 struct sgsn_pdp_ctx *pdp, *pdp2;
300
Jacob Erlbecke671d252015-01-26 14:43:07 +0100301 /* Unlink from global list of MM contexts */
302 llist_del(&mm->list);
303
304 /* Free all PDP contexts */
305 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
306 sgsn_pdp_ctx_free(pdp);
307
308 rate_ctr_group_free(mm->ctrg);
309
310 talloc_free(mm);
311}
312
313void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
314{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200315 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100316 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100317 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200318 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100319
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200320 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
321 llme = mm->gb.llme;
322 else
323 OSMO_ASSERT(mm->gb.llme == NULL);
324
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800325 /* Forget about ongoing look-ups */
326 if (mm->ggsn_lookup) {
327 LOGMMCTXP(LOGL_NOTICE, mm,
328 "Cleaning mmctx with on-going query.\n");
329 mm->ggsn_lookup->mmctx = NULL;
330 mm->ggsn_lookup = NULL;
331 }
332
Jacob Erlbecke671d252015-01-26 14:43:07 +0100333 /* delete all existing PDP contexts for this MS */
334 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
335 LOGMMCTXP(LOGL_NOTICE, mm,
336 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
337 sgsn_pdp_ctx_terminate(pdp);
338 }
339
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200340 if (osmo_timer_pending(&mm->timer)) {
341 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
342 osmo_timer_del(&mm->timer);
343 }
344
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200345 memset(&sig_data, 0, sizeof(sig_data));
346 sig_data.mm = mm;
347 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
348
349
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100350 /* Detach from subscriber which is possibly freed then */
351 if (mm->subscr) {
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100352 struct gprs_subscr *subscr = gprs_subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100353 gprs_subscr_cleanup(subscr);
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100354 gprs_subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100355 }
356
Alexander Couzensf7198d72018-05-22 18:29:14 +0200357 if (mm->gmm_att_req.fsm)
358 gmm_att_req_free(mm);
359
Jacob Erlbecke671d252015-01-26 14:43:07 +0100360 sgsn_mm_ctx_free(mm);
361 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100362
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200363 if (llme) {
364 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200365 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200366 }
Harald Weltec728eea2010-12-24 23:07:18 +0100367}
Harald Welte77289c22010-05-18 14:32:29 +0200368
Jacob Erlbecke671d252015-01-26 14:43:07 +0100369
Harald Welte96df6062010-06-03 06:37:26 +0200370/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200371struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
372 uint8_t nsapi)
373{
374 struct sgsn_pdp_ctx *pdp;
375
376 llist_for_each_entry(pdp, &mm->pdp_list, list) {
377 if (pdp->nsapi == nsapi)
378 return pdp;
379 }
380 return NULL;
381}
382
Harald Welte96df6062010-06-03 06:37:26 +0200383/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200384struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
385 uint8_t tid)
386{
387 struct sgsn_pdp_ctx *pdp;
388
389 llist_for_each_entry(pdp, &mm->pdp_list, list) {
390 if (pdp->ti == tid)
391 return pdp;
392 }
393 return NULL;
394}
395
Harald Welte7b022ee2012-07-14 12:04:04 +0200396/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200397struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200398 struct sgsn_ggsn_ctx *ggsn,
Harald Welted193cb32010-05-17 22:58:03 +0200399 uint8_t nsapi)
400{
401 struct sgsn_pdp_ctx *pdp;
402
403 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
404 if (pdp)
405 return NULL;
406
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100407 pdp = talloc_zero(tall_sgsn_ctx, struct sgsn_pdp_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200408 if (!pdp)
409 return NULL;
410
411 pdp->mm = mm;
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200412 pdp->ggsn = ggsn;
Harald Welted193cb32010-05-17 22:58:03 +0200413 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200414 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welte26c14652017-07-12 00:25:51 +0200415 if (!pdp->ctrg) {
416 LOGPDPCTXP(LOGL_ERROR, pdp, "Error allocation counter group\n");
417 talloc_free(pdp);
418 return NULL;
419 }
Harald Welted193cb32010-05-17 22:58:03 +0200420 llist_add(&pdp->list, &mm->pdp_list);
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200421 sgsn_ggsn_ctx_add_pdp(pdp->ggsn, pdp);
Harald Welted193cb32010-05-17 22:58:03 +0200422 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
423
424 return pdp;
425}
426
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200427/*
428 * This function will not trigger any GSM DEACT PDP ACK messages, so you
429 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
430 * isn't detached already.
431 */
432void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
433{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200434 struct sgsn_signal_data sig_data;
435
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200436 OSMO_ASSERT(pdp->mm != NULL);
437
438 /* There might still be pending callbacks in libgtp. So the parts of
439 * this object relevant to GTP need to remain intact in this case. */
440
441 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
442
Daniel Willmannf9f43872016-05-20 22:36:23 +0200443 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
444 /* Force the deactivation of the SNDCP layer */
445 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
446 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200447
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200448 memset(&sig_data, 0, sizeof(sig_data));
449 sig_data.pdp = pdp;
450 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
451
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200452 /* Detach from MM context */
Pau Espin Pedrol8c7d2592018-07-09 20:44:17 +0200453 pdp_ctx_detach_mm_ctx(pdp);
Pau Espin Pedrola733a502018-07-13 16:37:22 +0200454 if (pdp->ggsn)
455 sgsn_delete_pdp_ctx(pdp);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200456}
457
458/*
459 * Don't call this function directly unless you know what you are doing.
460 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
461 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
462 */
Harald Welted193cb32010-05-17 22:58:03 +0200463void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
464{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200465 struct sgsn_signal_data sig_data;
466
467 memset(&sig_data, 0, sizeof(sig_data));
468 sig_data.pdp = pdp;
469 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
470
Harald Welte376d5e52010-06-28 18:57:21 +0200471 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200472 if (pdp->mm)
473 llist_del(&pdp->list);
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200474 if (pdp->ggsn)
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200475 sgsn_ggsn_ctx_remove_pdp(pdp->ggsn, pdp);
Harald Welted193cb32010-05-17 22:58:03 +0200476 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200477
478 /* _if_ we still have a library handle, at least set it to NULL
479 * to avoid any dereferences of the now-deleted PDP context from
480 * sgsn_libgtp:cb_data_ind() */
481 if (pdp->lib) {
482 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200483 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200484 "has a libgtp handle attached to it, this shouldn't "
485 "happen!\n");
486 osmo_generate_backtrace();
487 lib->priv = NULL;
488 }
489
Harald Welted193cb32010-05-17 22:58:03 +0200490 talloc_free(pdp);
491}
492
Alexander Couzens176a4d22018-09-18 20:07:37 +0200493void sgsn_ggsn_ctx_check_echo_timer(struct sgsn_ggsn_ctx *ggc)
494{
Pau Espin Pedrol1825ab52019-05-27 18:04:02 +0200495 bool pending = osmo_timer_pending(&ggc->echo_timer);
496
497 /* Only enable if allowed by policy and at least 1 pdp ctx exists against ggsn */
498 if (!llist_empty(&ggc->pdp_list) && ggc->echo_interval > 0) {
499 if (!pending)
Alexander Couzens176a4d22018-09-18 20:07:37 +0200500 osmo_timer_schedule(&ggc->echo_timer, ggc->echo_interval, 0);
Pau Espin Pedrol1825ab52019-05-27 18:04:02 +0200501 } else {
502 if (pending)
503 osmo_timer_del(&ggc->echo_timer);
Alexander Couzens176a4d22018-09-18 20:07:37 +0200504 }
505}
506
Harald Welted193cb32010-05-17 22:58:03 +0200507/* GGSN contexts */
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200508static void echo_timer_cb(void *data)
509{
510 struct sgsn_ggsn_ctx *ggc = (struct sgsn_ggsn_ctx *) data;
511 sgsn_ggsn_echo_req(ggc);
512 osmo_timer_schedule(&ggc->echo_timer, ggc->echo_interval, 0);
513}
Harald Welted193cb32010-05-17 22:58:03 +0200514
Harald Welte77289c22010-05-18 14:32:29 +0200515struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200516{
Harald Welte77289c22010-05-18 14:32:29 +0200517 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200518
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100519 ggc = talloc_zero(tall_sgsn_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200520 if (!ggc)
521 return NULL;
522
523 ggc->id = id;
524 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100525 ggc->remote_restart_ctr = -1;
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200526 ggc->echo_interval = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200527 /* if we are called from config file parse, this gsn doesn't exist yet */
528 ggc->gsn = sgsn->gsn;
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200529 INIT_LLIST_HEAD(&ggc->pdp_list);
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200530 osmo_timer_setup(&ggc->echo_timer, echo_timer_cb, ggc);
Harald Welte119c2ba2010-05-18 18:39:00 +0200531 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200532
533 return ggc;
534}
535
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100536void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
537{
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200538 OSMO_ASSERT(llist_empty(&ggc->pdp_list));
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100539 llist_del(&ggc->list);
540 talloc_free(ggc);
541}
542
Harald Welte77289c22010-05-18 14:32:29 +0200543struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200544{
Harald Welte77289c22010-05-18 14:32:29 +0200545 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200546
547 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
548 if (id == ggc->id)
549 return ggc;
550 }
551 return NULL;
552}
553
Harald Weltea9b473a2010-12-24 21:13:26 +0100554struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
555{
556 struct sgsn_ggsn_ctx *ggc;
557
558 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
559 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
560 return ggc;
561 }
562 return NULL;
563}
564
565
Harald Welte77289c22010-05-18 14:32:29 +0200566struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200567{
Harald Welte77289c22010-05-18 14:32:29 +0200568 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200569
Harald Welte77289c22010-05-18 14:32:29 +0200570 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200571 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200572 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200573 return ggc;
574}
575
576/* APN contexts */
577
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100578static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200579{
580 struct apn_ctx *actx;
581
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100582 actx = talloc_zero(tall_sgsn_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200583 if (!actx)
584 return NULL;
585 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100586 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
587
588 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200589
590 return actx;
591}
592
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100593void sgsn_apn_ctx_free(struct apn_ctx *actx)
594{
595 llist_del(&actx->list);
596 talloc_free(actx);
597}
598
599struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
600{
601 struct apn_ctx *actx;
602 struct apn_ctx *found_actx = NULL;
603 size_t imsi_prio = 0;
604 size_t name_prio = 0;
605 size_t name_req_len = strlen(name);
606
607 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
608 size_t name_ref_len, imsi_ref_len;
609 const char *name_ref_start, *name_match_start;
610
611 imsi_ref_len = strlen(actx->imsi_prefix);
612 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
613 continue;
614
615 if (imsi_ref_len < imsi_prio)
616 continue;
617
618 /* IMSI matches */
619
620 name_ref_start = &actx->name[0];
621 if (name_ref_start[0] == '*') {
622 /* Suffix match */
623 name_ref_start += 1;
624 name_ref_len = strlen(name_ref_start);
625 if (name_ref_len > name_req_len)
626 continue;
627 } else {
628 name_ref_len = strlen(name_ref_start);
629 if (name_ref_len != name_req_len)
630 continue;
631 }
632
633 name_match_start = name + (name_req_len - name_ref_len);
634 if (strcasecmp(name_match_start, name_ref_start) != 0)
635 continue;
636
637 /* IMSI and name match */
638
639 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
640 /* Lower priority, skip */
641 continue;
642
643 imsi_prio = imsi_ref_len;
644 name_prio = name_ref_len;
645 found_actx = actx;
646 }
647 return found_actx;
648}
649
650struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200651{
652 struct apn_ctx *actx;
653
654 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100655 if (strcasecmp(name, actx->name) == 0 &&
656 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200657 return actx;
658 }
659 return NULL;
660}
661
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100662struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200663{
664 struct apn_ctx *actx;
665
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100666 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200667 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100668 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200669
670 return actx;
671}
Harald Welte6463c072010-05-18 17:04:55 +0200672
673uint32_t sgsn_alloc_ptmsi(void)
674{
675 struct sgsn_mm_ctx *mm;
Alexander Couzens8a215c32017-02-03 23:22:18 +0100676 uint32_t ptmsi = 0xdeadbeef;
Max3b6332f2017-11-01 13:28:38 +0100677 int max_retries = 100, rc = 0;
Harald Welte6463c072010-05-18 17:04:55 +0200678
679restart:
Max3b6332f2017-11-01 13:28:38 +0100680 rc = osmo_get_rand_id((uint8_t *) &ptmsi, sizeof(ptmsi));
681 if (rc < 0)
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200682 goto failed;
683
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100684 /* Enforce that the 2 MSB are set without loosing the distance between
685 * identical values. Since rand() has no duplicate values within a
686 * period (because the size of the state is the same like the size of
687 * the random value), this leads to a distance of period/4 when the
688 * distribution of the 2 MSB is uniform. This approach fails with a
689 * probability of (3/4)^max_retries, only 1% of the approaches will
690 * need more than 16 numbers (even distribution assumed).
691 *
692 * Alternatively, a freeze list could be used if another PRNG is used
693 * or when this approach proves to be not sufficient.
694 */
Eric Wilddf9b3912019-06-17 12:13:28 +0200695 if (ptmsi >= GSM23003_TMSI_SGSN_MASK) {
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100696 if (!max_retries--)
697 goto failed;
698 goto restart;
699 }
Eric Wilddf9b3912019-06-17 12:13:28 +0200700 ptmsi |= GSM23003_TMSI_SGSN_MASK;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100701
702 if (ptmsi == GSM_RESERVED_TMSI) {
703 if (!max_retries--)
704 goto failed;
705 goto restart;
706 }
707
Harald Welte6463c072010-05-18 17:04:55 +0200708 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200709 if (mm->p_tmsi == ptmsi) {
710 if (!max_retries--)
711 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200712 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200713 }
Harald Welte6463c072010-05-18 17:04:55 +0200714 }
715
716 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200717
718failed:
Max3b6332f2017-11-01 13:28:38 +0100719 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI: %d (%s)\n", rc, strerror(-rc));
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200720 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200721}
Harald Weltea9b473a2010-12-24 21:13:26 +0100722
Pau Espin Pedrol2cf70e02018-07-16 11:31:23 +0200723void sgsn_ggsn_ctx_drop_pdp(struct sgsn_pdp_ctx *pctx)
Harald Weltea9b473a2010-12-24 21:13:26 +0100724{
Alexander Couzensb62653a2018-09-18 16:53:42 +0200725 /* the MM context can be deleted while the GGSN is not reachable or
726 * if has been crashed. */
727 if (pctx->mm && pctx->mm->gmm_state == GMM_REGISTERED_NORMAL) {
Pau Espin Pedrol2cf70e02018-07-16 11:31:23 +0200728 gsm48_tx_gsm_deact_pdp_req(pctx, GSM_CAUSE_NET_FAIL, true);
729 sgsn_ggsn_ctx_remove_pdp(pctx->ggsn, pctx);
Pau Espin Pedrola733a502018-07-13 16:37:22 +0200730 } else {
Harald Weltea9b473a2010-12-24 21:13:26 +0100731 /* FIXME: GPRS paging in case MS is SUSPENDED */
Pau Espin Pedrol2cf70e02018-07-16 11:31:23 +0200732 LOGPDPCTXP(LOGL_NOTICE, pctx, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100733 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200734 /* FIXME: how to tell this to libgtp? */
Pau Espin Pedrol2cf70e02018-07-16 11:31:23 +0200735 sgsn_pdp_ctx_free(pctx);
Harald Weltea9b473a2010-12-24 21:13:26 +0100736 }
737}
738
739/* High-level function to be called in case a GGSN has disappeared or
Pau Espin Pedrolef6d78f2018-07-17 15:56:53 +0200740 * otherwise lost state (recovery procedure). It will detach all related pdp ctx
741 * from a ggsn and communicate deact to MS. Optionally (!NULL), one pdp ctx can
742 * be kept alive to allow handling later message which contained the Recovery IE. */
743int sgsn_ggsn_ctx_drop_all_pdp_except(struct sgsn_ggsn_ctx *ggsn, struct sgsn_pdp_ctx *except)
Harald Weltea9b473a2010-12-24 21:13:26 +0100744{
Harald Weltea9b473a2010-12-24 21:13:26 +0100745 int num = 0;
746
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200747 struct sgsn_pdp_ctx *pdp, *pdp2;
748 llist_for_each_entry_safe(pdp, pdp2, &ggsn->pdp_list, ggsn_list) {
Pau Espin Pedrolef6d78f2018-07-17 15:56:53 +0200749 if (pdp == except)
750 continue;
Pau Espin Pedrol2cf70e02018-07-16 11:31:23 +0200751 sgsn_ggsn_ctx_drop_pdp(pdp);
Pau Espin Pedrola98fead2018-07-09 14:39:47 +0200752 num++;
Harald Weltea9b473a2010-12-24 21:13:26 +0100753 }
754
755 return num;
756}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200757
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200758void sgsn_ggsn_ctx_add_pdp(struct sgsn_ggsn_ctx *ggc, struct sgsn_pdp_ctx *pdp)
759{
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200760 llist_add(&pdp->ggsn_list, &ggc->pdp_list);
Pau Espin Pedrol1825ab52019-05-27 18:04:02 +0200761 sgsn_ggsn_ctx_check_echo_timer(ggc);
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200762}
763void sgsn_ggsn_ctx_remove_pdp(struct sgsn_ggsn_ctx *ggc, struct sgsn_pdp_ctx *pdp)
764{
765 llist_del(&pdp->ggsn_list);
Alexander Couzens176a4d22018-09-18 20:07:37 +0200766 sgsn_ggsn_ctx_check_echo_timer(ggc);
Pau Espin Pedrola733a502018-07-13 16:37:22 +0200767 if (pdp->destroy_ggsn)
768 sgsn_ggsn_ctx_free(pdp->ggsn);
769 pdp->ggsn = NULL;
770 /* Drop references to libgtp since the conn is down */
771 if (pdp->lib)
772 pdp_freepdp(pdp->lib);
773 pdp->lib = NULL;
Pau Espin Pedrolfa120102018-07-09 20:37:47 +0200774}
775
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100776void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200777{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100778 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100779 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100780
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100781 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200782}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100783
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200784static void insert_extra(struct tlv_parsed *tp,
785 struct sgsn_subscriber_data *data,
786 struct sgsn_subscriber_pdp_data *pdp)
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400787{
788 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
789 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200790
791 /* Prefer PDP charging characteristics of per subscriber one */
792 if (pdp->has_pdp_charg) {
793 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(pdp->pdp_charg);
794 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &pdp->pdp_charg[0];
795 } else if (data->has_pdp_charg) {
796 tp->lv[OSMO_IE_GSM_CHARG_CHAR].len = sizeof(data->pdp_charg);
797 tp->lv[OSMO_IE_GSM_CHARG_CHAR].val = &data->pdp_charg[0];
798 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400799}
800
801/**
802 * The tlv_parsed tp parameter will be modified to insert a
803 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
804 * PDP context handling.
805 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100806struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
807 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800808 enum gsm48_gsm_cause *gsm_cause,
809 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100810{
811 char req_apn_str[GSM_APN_LENGTH] = {0};
812 const struct apn_ctx *apn_ctx = NULL;
813 const char *selected_apn_str = NULL;
814 struct sgsn_subscriber_pdp_data *pdp;
815 struct sgsn_ggsn_ctx *ggsn = NULL;
816 int allow_any_apn = 0;
817
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800818 out_apn_str[0] = '\0';
819
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100820 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
821 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
822 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
823 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
824 return NULL;
825 }
826
Harald Welte7e82b742017-08-12 13:43:54 +0200827 osmo_apn_to_str(req_apn_str,
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100828 TLVP_VAL(tp, GSM48_IE_GSM_APN),
829 TLVP_LEN(tp, GSM48_IE_GSM_APN));
830
831 if (strcmp(req_apn_str, "*") == 0)
832 req_apn_str[0] = 0;
833 }
834
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800835 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100836 allow_any_apn = 1;
837
838 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
839 /* No specific APN requested, check for an APN that is both
840 * granted and configured */
841
842 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
843 if (strcmp(pdp->apn_str, "*") == 0)
844 {
845 allow_any_apn = 1;
846 selected_apn_str = "";
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200847 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100848 continue;
849 }
850 if (!llist_empty(&sgsn_apn_ctxts)) {
851 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
852 /* Not configured */
853 if (apn_ctx == NULL)
854 continue;
855 }
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200856 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100857 selected_apn_str = pdp->apn_str;
858 break;
859 }
860 } else if (!allow_any_apn) {
861 /* Check whether the given APN is granted */
862 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
863 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200864 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100865 selected_apn_str = req_apn_str;
866 allow_any_apn = 1;
867 continue;
868 }
869 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther5db68572017-07-09 13:18:17 +0200870 insert_extra(tp, mmctx->subscr->sgsn_data, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100871 selected_apn_str = req_apn_str;
872 break;
873 }
874 }
875 } else if (strlen(req_apn_str) != 0) {
876 /* Any APN is allowed */
877 selected_apn_str = req_apn_str;
878 } else {
879 /* Prefer the GGSN associated with the wildcard APN */
880 selected_apn_str = "";
881 }
882
883 if (!allow_any_apn && selected_apn_str == NULL) {
884 /* Access not granted */
885 LOGMMCTXP(LOGL_NOTICE, mmctx,
886 "The requested APN '%s' is not allowed\n",
887 req_apn_str);
888 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
889 return NULL;
890 }
891
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800892 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200893 if (selected_apn_str)
894 strcpy(out_apn_str, selected_apn_str);
895 else
896 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800897
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100898 if (apn_ctx == NULL && selected_apn_str)
899 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
900
901 if (apn_ctx != NULL) {
902 ggsn = apn_ctx->ggsn;
903 } else if (llist_empty(&sgsn_apn_ctxts)) {
904 /* No configuration -> use GGSN 0 */
905 ggsn = sgsn_ggsn_ctx_by_id(0);
906 } else if (allow_any_apn &&
907 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
908 /* No APN given and no default configuration -> Use GGSN 0 */
909 ggsn = sgsn_ggsn_ctx_by_id(0);
910 } else {
911 /* No matching configuration found */
912 LOGMMCTXP(LOGL_NOTICE, mmctx,
913 "The selected APN '%s' has not been configured\n",
914 selected_apn_str);
915 *gsm_cause = GSM_CAUSE_MISSING_APN;
916 return NULL;
917 }
918
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800919 if (!ggsn) {
920 LOGMMCTXP(LOGL_NOTICE, mmctx,
921 "No static GGSN configured. Selected APN '%s'\n",
922 selected_apn_str);
Vadim Yanitskiy4b8e3092019-07-18 15:04:46 +0700923 *gsm_cause = GSM_CAUSE_MISSING_APN;
924 return NULL;
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800925 }
926
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100927 LOGMMCTXP(LOGL_INFO, mmctx,
928 "Found GGSN %d for APN '%s' (requested '%s')\n",
929 ggsn->id, selected_apn_str ? selected_apn_str : "---",
930 req_apn_str);
931
932 return ggsn;
933}
934
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100935static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
936{
937 struct sgsn_mm_ctx *mmctx = NULL;
938
939 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100940 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100941 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
942 return;
943 }
944 }
945
946 /* No MM context found */
947 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
948 llme->tlli);
Max39550252016-06-28 17:39:20 +0200949 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100950}
951
952static void sgsn_llme_check_cb(void *data_)
953{
954 struct gprs_llc_llme *llme, *llme_tmp;
955 struct timespec now_tp;
956 time_t now, age;
957 time_t max_age = gprs_max_time_to_idle();
958
959 int rc;
960
Pau Espin Pedrol36abead2018-08-17 13:27:20 +0200961 rc = osmo_clock_gettime(CLOCK_MONOTONIC, &now_tp);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100962 OSMO_ASSERT(rc >= 0);
963 now = now_tp.tv_sec;
964
965 LOGP(DGPRS, LOGL_DEBUG,
966 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
967
968 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
969 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
970 llme->age_timestamp = now;
971
972 age = now - llme->age_timestamp;
973
974 if (age > max_age || age < 0) {
975 LOGP(DGPRS, LOGL_INFO,
976 "Inactivity timeout for TLLI 0x%08x, age %d\n",
977 llme->tlli, (int)age);
978 sgsn_llme_cleanup_free(llme);
979 }
980 }
981
982 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
983}
984
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100985struct sgsn_instance *sgsn_instance_alloc(void *talloc_ctx)
986{
987 struct sgsn_instance *inst;
988 inst = talloc_zero(talloc_ctx, struct sgsn_instance);
989 inst->cfg.gtp_statedir = talloc_strdup(inst, "./");
990 inst->cfg.auth_policy = SGSN_AUTH_POLICY_CLOSED;
Pau Espin Pedrold1463bc2019-06-13 19:03:25 +0200991 inst->cfg.require_authentication = true; /* only applies if auth_policy is REMOTE */
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100992 inst->cfg.gsup_server_port = OSMO_GSUP_PORT;
993 return inst;
994}
995
996void sgsn_inst_init(struct sgsn_instance *sgsn)
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100997{
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200998 osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100999 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
1000}