blob: 9cd992b49fbf9edc2a3ec957c1180772060d7819 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS SGSN functionality */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080019 *
20 */
21
Harald Welteeaa614c2010-05-02 11:26:34 +020022#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080023
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010024#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/talloc.h>
26#include <osmocom/core/timer.h>
27#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010028#include <osmocom/core/stats.h>
Harald Weltefdf453c2012-07-14 12:15:19 +020029#include <osmocom/core/backtrace.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080030#include <osmocom/gprs/gprs_ns.h>
31#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte53373bc2016-04-20 17:11:43 +020032#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080033
Harald Welte9b455bf2010-03-14 15:45:01 +080034#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020035#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080036#include <openbsc/gprs_sgsn.h>
Harald Welteab1d5622010-05-18 19:58:38 +020037#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010038#include <openbsc/gprs_gmm.h>
Jacob Erlbeck277b71e2015-02-02 18:03:05 +010039#include <openbsc/gprs_utils.h>
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +020040#include <openbsc/signal.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +020041#include "openbsc/gprs_llc.h"
Harald Welteab1d5622010-05-18 19:58:38 +020042
Neels Hofmeyrf4daf162016-05-21 00:44:50 +020043#include <pdp.h>
44
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010045#include <time.h>
46
Daniel Willmann044ce5f2015-10-12 19:36:33 +020047#include <openssl/rand.h>
48
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010049#define GPRS_LLME_CHECK_TICK 30
50
Harald Welteab1d5622010-05-18 19:58:38 +020051extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080052
Harald Welted193cb32010-05-17 22:58:03 +020053LLIST_HEAD(sgsn_mm_ctxts);
54LLIST_HEAD(sgsn_ggsn_ctxts);
55LLIST_HEAD(sgsn_apn_ctxts);
56LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080057
Harald Welte8acd88f2010-05-18 10:57:45 +020058static const struct rate_ctr_desc mmctx_ctr_description[] = {
59 { "sign.packets.in", "Signalling Messages ( In)" },
60 { "sign.packets.out", "Signalling Messages (Out)" },
61 { "udata.packets.in", "User Data Messages ( In)" },
62 { "udata.packets.out", "User Data Messages (Out)" },
63 { "udata.bytes.in", "User Data Bytes ( In)" },
64 { "udata.bytes.out", "User Data Bytes (Out)" },
65 { "pdp_ctx_act", "PDP Context Activations " },
66 { "suspend", "SUSPEND Count " },
67 { "paging.ps", "Paging Packet Switched " },
68 { "paging.cs", "Paging Circuit Switched " },
69 { "ra_update", "Routing Area Update " },
70};
71
72static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
73 .group_name_prefix = "sgsn.mmctx",
74 .group_description = "SGSN MM Context Statistics",
75 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
76 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010077 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020078};
79
Harald Welteefbdee92010-06-10 00:20:12 +020080static const struct rate_ctr_desc pdpctx_ctr_description[] = {
81 { "udata.packets.in", "User Data Messages ( In)" },
82 { "udata.packets.out", "User Data Messages (Out)" },
83 { "udata.bytes.in", "User Data Bytes ( In)" },
84 { "udata.bytes.out", "User Data Bytes (Out)" },
85};
86
87static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
88 .group_name_prefix = "sgsn.pdpctx",
89 .group_description = "SGSN PDP Context Statistics",
90 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
91 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010092 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +020093};
94
Alexander Couzens14314bd2016-07-05 09:52:52 +020095static const struct rate_ctr_desc sgsn_ctr_description[] = {
Alexander Couzens4e699a92016-07-05 11:04:27 +020096 { "llc.dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" },
97 { "llc.ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" },
98 { "llc.dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" },
99 { "llc.ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" },
Alexander Couzens14314bd2016-07-05 09:52:52 +0200100 { "gprs.attach_requested", "Received attach requests" },
101 { "gprs.attach_accepted", "Sent attach accepts" },
102 { "gprs.attach_rejected", "Sent attach rejects" },
103 { "gprs.detach_requested", "Received detach requests" },
104 { "gprs.detach_acked", "Sent detach acks" },
105 { "gprs.routing_area_requested", "Received routing area requests" },
106 { "gprs.routing_area_requested", "Sent routing area acks" },
107 { "gprs.routing_area_requested", "Sent routing area rejects" },
108 { "pdp.activate_requested", "Received activate requests" },
109 { "pdp.activate_rejected", "Sent activate rejects" },
110 { "pdp.activate_accepted", "Sent activate accepts" },
111 { "pdp.request_activated", "unused" },
112 { "pdp.request_activate_rejected", "unused" },
113 { "pdp.modify_requested", "unused" },
114 { "pdp.modify_accepted", "unused" },
115 { "pdp.dl_deactivate_requested", "Sent deactivate requests" },
116 { "pdp.dl_deactivate_accepted", "Sent deactivate accepted" },
117 { "pdp.ul_deactivate_requested", "Received deactivate requests" },
118 { "pdp.ul_deactivate_accepted", "Received deactivate accepts" },
119};
120
121static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
122 "sgsn",
123 "SGSN Overall Statistics",
124 OSMO_STATS_CLASS_GLOBAL,
125 ARRAY_SIZE(sgsn_ctr_description),
126 sgsn_ctr_description,
127};
128
129void sgsn_rate_ctr_init() {
130 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0);
131}
132
Harald Welte9b455bf2010-03-14 15:45:01 +0800133/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200134struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800135 const struct gprs_ra_id *raid)
136{
137 struct sgsn_mm_ctx *ctx;
138
139 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100140 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100141 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800142 return ctx;
143 }
Harald Welteab1d5622010-05-18 19:58:38 +0200144
Harald Welte9b455bf2010-03-14 15:45:01 +0800145 return NULL;
146}
147
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100148struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
149 const struct gprs_ra_id *raid)
150{
151 struct sgsn_mm_ctx *ctx;
152 int tlli_type;
153
154 /* TODO: Also check the P_TMSI signature to be safe. That signature
155 * should be different (at least with a sufficiently high probability)
156 * after SGSN restarts and for multiple SGSN instances.
157 */
158
159 tlli_type = gprs_tlli_type(tlli);
160 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
161 return NULL;
162
163 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
164 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
165 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
166 gprs_ra_id_equals(raid, &ctx->ra))
167 return ctx;
168 }
169
170 return NULL;
171}
172
Harald Welteeaa614c2010-05-02 11:26:34 +0200173struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800174{
175 struct sgsn_mm_ctx *ctx;
176
177 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200178 if (p_tmsi == ctx->p_tmsi ||
179 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800180 return ctx;
181 }
182 return NULL;
183}
184
185struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
186{
187 struct sgsn_mm_ctx *ctx;
188
189 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
190 if (!strcmp(imsi, ctx->imsi))
191 return ctx;
192 }
193 return NULL;
194
195}
196
197/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200198struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800199 const struct gprs_ra_id *raid)
200{
Harald Welte2720e732010-05-17 00:44:57 +0200201 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800202
Harald Welte2720e732010-05-17 00:44:57 +0200203 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800204 if (!ctx)
205 return NULL;
206
207 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100208 ctx->ran_type = MM_CTX_T_GERAN_Gb;
209 ctx->gb.tlli = tlli;
Harald Welte9b455bf2010-03-14 15:45:01 +0800210 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100211 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Maxb997f842016-07-06 15:57:01 +0200212 ctx->ciph_algo = sgsn->cfg.cipher;
Harald Welte8acd88f2010-05-18 10:57:45 +0200213 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200214 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800215
216 llist_add(&ctx->list, &sgsn_mm_ctxts);
217
218 return ctx;
219}
Harald Welted193cb32010-05-17 22:58:03 +0200220
Harald Welte7b022ee2012-07-14 12:04:04 +0200221/* this is a hard _free_ function, it doesn't clean up the PDP contexts
222 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200223static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100224{
225 struct sgsn_pdp_ctx *pdp, *pdp2;
226
Jacob Erlbecke671d252015-01-26 14:43:07 +0100227 /* Unlink from global list of MM contexts */
228 llist_del(&mm->list);
229
230 /* Free all PDP contexts */
231 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
232 sgsn_pdp_ctx_free(pdp);
233
234 rate_ctr_group_free(mm->ctrg);
235
236 talloc_free(mm);
237}
238
239void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
240{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200241 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100242 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100243 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200244 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100245
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200246 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
247 llme = mm->gb.llme;
248 else
249 OSMO_ASSERT(mm->gb.llme == NULL);
250
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800251 /* Forget about ongoing look-ups */
252 if (mm->ggsn_lookup) {
253 LOGMMCTXP(LOGL_NOTICE, mm,
254 "Cleaning mmctx with on-going query.\n");
255 mm->ggsn_lookup->mmctx = NULL;
256 mm->ggsn_lookup = NULL;
257 }
258
Jacob Erlbecke671d252015-01-26 14:43:07 +0100259 /* delete all existing PDP contexts for this MS */
260 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
261 LOGMMCTXP(LOGL_NOTICE, mm,
262 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
263 sgsn_pdp_ctx_terminate(pdp);
264 }
265
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200266 if (osmo_timer_pending(&mm->timer)) {
267 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
268 osmo_timer_del(&mm->timer);
269 }
270
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200271 memset(&sig_data, 0, sizeof(sig_data));
272 sig_data.mm = mm;
273 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
274
275
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100276 /* Detach from subscriber which is possibly freed then */
277 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100278 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100279 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100280 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100281 }
282
Jacob Erlbecke671d252015-01-26 14:43:07 +0100283 sgsn_mm_ctx_free(mm);
284 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100285
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200286 if (llme) {
287 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200288 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200289 }
Harald Weltec728eea2010-12-24 23:07:18 +0100290}
Harald Welte77289c22010-05-18 14:32:29 +0200291
Jacob Erlbecke671d252015-01-26 14:43:07 +0100292
Harald Welte96df6062010-06-03 06:37:26 +0200293/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200294struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
295 uint8_t nsapi)
296{
297 struct sgsn_pdp_ctx *pdp;
298
299 llist_for_each_entry(pdp, &mm->pdp_list, list) {
300 if (pdp->nsapi == nsapi)
301 return pdp;
302 }
303 return NULL;
304}
305
Harald Welte96df6062010-06-03 06:37:26 +0200306/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200307struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
308 uint8_t tid)
309{
310 struct sgsn_pdp_ctx *pdp;
311
312 llist_for_each_entry(pdp, &mm->pdp_list, list) {
313 if (pdp->ti == tid)
314 return pdp;
315 }
316 return NULL;
317}
318
Harald Welte7b022ee2012-07-14 12:04:04 +0200319/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200320struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
321 uint8_t nsapi)
322{
323 struct sgsn_pdp_ctx *pdp;
324
325 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
326 if (pdp)
327 return NULL;
328
329 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
330 if (!pdp)
331 return NULL;
332
333 pdp->mm = mm;
334 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200335 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200336 llist_add(&pdp->list, &mm->pdp_list);
337 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
338
339 return pdp;
340}
341
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200342/*
343 * This function will not trigger any GSM DEACT PDP ACK messages, so you
344 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
345 * isn't detached already.
346 */
347void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
348{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200349 struct sgsn_signal_data sig_data;
350
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200351 OSMO_ASSERT(pdp->mm != NULL);
352
353 /* There might still be pending callbacks in libgtp. So the parts of
354 * this object relevant to GTP need to remain intact in this case. */
355
356 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
357
Daniel Willmannf9f43872016-05-20 22:36:23 +0200358 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
359 /* Force the deactivation of the SNDCP layer */
360 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
361 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200362
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200363 memset(&sig_data, 0, sizeof(sig_data));
364 sig_data.pdp = pdp;
365 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
366
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200367 /* Detach from MM context */
368 llist_del(&pdp->list);
369 pdp->mm = NULL;
370
371 sgsn_delete_pdp_ctx(pdp);
372}
373
374/*
375 * Don't call this function directly unless you know what you are doing.
376 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
377 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
378 */
Harald Welted193cb32010-05-17 22:58:03 +0200379void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
380{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200381 struct sgsn_signal_data sig_data;
382
383 memset(&sig_data, 0, sizeof(sig_data));
384 sig_data.pdp = pdp;
385 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
386
Harald Welte376d5e52010-06-28 18:57:21 +0200387 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200388 if (pdp->mm)
389 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200390 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200391
392 /* _if_ we still have a library handle, at least set it to NULL
393 * to avoid any dereferences of the now-deleted PDP context from
394 * sgsn_libgtp:cb_data_ind() */
395 if (pdp->lib) {
396 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200397 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200398 "has a libgtp handle attached to it, this shouldn't "
399 "happen!\n");
400 osmo_generate_backtrace();
401 lib->priv = NULL;
402 }
403
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800404 if (pdp->destroy_ggsn)
405 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200406 talloc_free(pdp);
407}
408
409/* GGSN contexts */
410
Harald Welte77289c22010-05-18 14:32:29 +0200411struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200412{
Harald Welte77289c22010-05-18 14:32:29 +0200413 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200414
Harald Welte77289c22010-05-18 14:32:29 +0200415 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200416 if (!ggc)
417 return NULL;
418
419 ggc->id = id;
420 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100421 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200422 /* if we are called from config file parse, this gsn doesn't exist yet */
423 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200424 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200425
426 return ggc;
427}
428
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100429void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
430{
431 llist_del(&ggc->list);
432 talloc_free(ggc);
433}
434
Harald Welte77289c22010-05-18 14:32:29 +0200435struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200436{
Harald Welte77289c22010-05-18 14:32:29 +0200437 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200438
439 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
440 if (id == ggc->id)
441 return ggc;
442 }
443 return NULL;
444}
445
Harald Weltea9b473a2010-12-24 21:13:26 +0100446struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
447{
448 struct sgsn_ggsn_ctx *ggc;
449
450 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
451 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
452 return ggc;
453 }
454 return NULL;
455}
456
457
Harald Welte77289c22010-05-18 14:32:29 +0200458struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200459{
Harald Welte77289c22010-05-18 14:32:29 +0200460 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200461
Harald Welte77289c22010-05-18 14:32:29 +0200462 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200463 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200464 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200465 return ggc;
466}
467
468/* APN contexts */
469
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100470static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200471{
472 struct apn_ctx *actx;
473
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100474 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200475 if (!actx)
476 return NULL;
477 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100478 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
479
480 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200481
482 return actx;
483}
484
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100485void sgsn_apn_ctx_free(struct apn_ctx *actx)
486{
487 llist_del(&actx->list);
488 talloc_free(actx);
489}
490
491struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
492{
493 struct apn_ctx *actx;
494 struct apn_ctx *found_actx = NULL;
495 size_t imsi_prio = 0;
496 size_t name_prio = 0;
497 size_t name_req_len = strlen(name);
498
499 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
500 size_t name_ref_len, imsi_ref_len;
501 const char *name_ref_start, *name_match_start;
502
503 imsi_ref_len = strlen(actx->imsi_prefix);
504 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
505 continue;
506
507 if (imsi_ref_len < imsi_prio)
508 continue;
509
510 /* IMSI matches */
511
512 name_ref_start = &actx->name[0];
513 if (name_ref_start[0] == '*') {
514 /* Suffix match */
515 name_ref_start += 1;
516 name_ref_len = strlen(name_ref_start);
517 if (name_ref_len > name_req_len)
518 continue;
519 } else {
520 name_ref_len = strlen(name_ref_start);
521 if (name_ref_len != name_req_len)
522 continue;
523 }
524
525 name_match_start = name + (name_req_len - name_ref_len);
526 if (strcasecmp(name_match_start, name_ref_start) != 0)
527 continue;
528
529 /* IMSI and name match */
530
531 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
532 /* Lower priority, skip */
533 continue;
534
535 imsi_prio = imsi_ref_len;
536 name_prio = name_ref_len;
537 found_actx = actx;
538 }
539 return found_actx;
540}
541
542struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200543{
544 struct apn_ctx *actx;
545
546 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100547 if (strcasecmp(name, actx->name) == 0 &&
548 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200549 return actx;
550 }
551 return NULL;
552}
553
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100554struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200555{
556 struct apn_ctx *actx;
557
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100558 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200559 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100560 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200561
562 return actx;
563}
Harald Welte6463c072010-05-18 17:04:55 +0200564
565uint32_t sgsn_alloc_ptmsi(void)
566{
567 struct sgsn_mm_ctx *mm;
568 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100569 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200570
571restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200572 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
573 goto failed;
574
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100575 /* Enforce that the 2 MSB are set without loosing the distance between
576 * identical values. Since rand() has no duplicate values within a
577 * period (because the size of the state is the same like the size of
578 * the random value), this leads to a distance of period/4 when the
579 * distribution of the 2 MSB is uniform. This approach fails with a
580 * probability of (3/4)^max_retries, only 1% of the approaches will
581 * need more than 16 numbers (even distribution assumed).
582 *
583 * Alternatively, a freeze list could be used if another PRNG is used
584 * or when this approach proves to be not sufficient.
585 */
586 if (ptmsi >= 0xC0000000) {
587 if (!max_retries--)
588 goto failed;
589 goto restart;
590 }
591 ptmsi |= 0xC0000000;
592
593 if (ptmsi == GSM_RESERVED_TMSI) {
594 if (!max_retries--)
595 goto failed;
596 goto restart;
597 }
598
Harald Welte6463c072010-05-18 17:04:55 +0200599 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200600 if (mm->p_tmsi == ptmsi) {
601 if (!max_retries--)
602 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200603 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200604 }
Harald Welte6463c072010-05-18 17:04:55 +0200605 }
606
607 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200608
609failed:
610 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
611 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200612}
Harald Weltea9b473a2010-12-24 21:13:26 +0100613
614static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
615{
616 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
617 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
618 else {
619 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200620 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100621 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200622 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100623 sgsn_pdp_ctx_free(pdp);
624 }
625}
626
627/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100628 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100629int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
630{
631 struct sgsn_mm_ctx *mm;
632 int num = 0;
633
634 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
635 struct sgsn_pdp_ctx *pdp;
636 llist_for_each_entry(pdp, &mm->pdp_list, list) {
637 if (pdp->ggsn == ggsn) {
638 drop_one_pdp(pdp);
639 num++;
640 }
641 }
642 }
643
644 return num;
645}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200646
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100647void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200648{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100649 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100650 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100651
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100652 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200653}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100654
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400655static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
656{
657 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
658 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
659}
660
661/**
662 * The tlv_parsed tp parameter will be modified to insert a
663 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
664 * PDP context handling.
665 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100666struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
667 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800668 enum gsm48_gsm_cause *gsm_cause,
669 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100670{
671 char req_apn_str[GSM_APN_LENGTH] = {0};
672 const struct apn_ctx *apn_ctx = NULL;
673 const char *selected_apn_str = NULL;
674 struct sgsn_subscriber_pdp_data *pdp;
675 struct sgsn_ggsn_ctx *ggsn = NULL;
676 int allow_any_apn = 0;
677
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800678 out_apn_str[0] = '\0';
679
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100680 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
681 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
682 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
683 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
684 return NULL;
685 }
686
687 gprs_apn_to_str(req_apn_str,
688 TLVP_VAL(tp, GSM48_IE_GSM_APN),
689 TLVP_LEN(tp, GSM48_IE_GSM_APN));
690
691 if (strcmp(req_apn_str, "*") == 0)
692 req_apn_str[0] = 0;
693 }
694
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800695 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100696 allow_any_apn = 1;
697
698 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
699 /* No specific APN requested, check for an APN that is both
700 * granted and configured */
701
702 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
703 if (strcmp(pdp->apn_str, "*") == 0)
704 {
705 allow_any_apn = 1;
706 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400707 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100708 continue;
709 }
710 if (!llist_empty(&sgsn_apn_ctxts)) {
711 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
712 /* Not configured */
713 if (apn_ctx == NULL)
714 continue;
715 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400716 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100717 selected_apn_str = pdp->apn_str;
718 break;
719 }
720 } else if (!allow_any_apn) {
721 /* Check whether the given APN is granted */
722 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
723 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400724 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100725 selected_apn_str = req_apn_str;
726 allow_any_apn = 1;
727 continue;
728 }
729 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400730 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100731 selected_apn_str = req_apn_str;
732 break;
733 }
734 }
735 } else if (strlen(req_apn_str) != 0) {
736 /* Any APN is allowed */
737 selected_apn_str = req_apn_str;
738 } else {
739 /* Prefer the GGSN associated with the wildcard APN */
740 selected_apn_str = "";
741 }
742
743 if (!allow_any_apn && selected_apn_str == NULL) {
744 /* Access not granted */
745 LOGMMCTXP(LOGL_NOTICE, mmctx,
746 "The requested APN '%s' is not allowed\n",
747 req_apn_str);
748 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
749 return NULL;
750 }
751
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800752 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200753 if (selected_apn_str)
754 strcpy(out_apn_str, selected_apn_str);
755 else
756 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800757
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100758 if (apn_ctx == NULL && selected_apn_str)
759 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
760
761 if (apn_ctx != NULL) {
762 ggsn = apn_ctx->ggsn;
763 } else if (llist_empty(&sgsn_apn_ctxts)) {
764 /* No configuration -> use GGSN 0 */
765 ggsn = sgsn_ggsn_ctx_by_id(0);
766 } else if (allow_any_apn &&
767 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
768 /* No APN given and no default configuration -> Use GGSN 0 */
769 ggsn = sgsn_ggsn_ctx_by_id(0);
770 } else {
771 /* No matching configuration found */
772 LOGMMCTXP(LOGL_NOTICE, mmctx,
773 "The selected APN '%s' has not been configured\n",
774 selected_apn_str);
775 *gsm_cause = GSM_CAUSE_MISSING_APN;
776 return NULL;
777 }
778
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800779 if (!ggsn) {
780 LOGMMCTXP(LOGL_NOTICE, mmctx,
781 "No static GGSN configured. Selected APN '%s'\n",
782 selected_apn_str);
783 return NULL;
784 }
785
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100786 LOGMMCTXP(LOGL_INFO, mmctx,
787 "Found GGSN %d for APN '%s' (requested '%s')\n",
788 ggsn->id, selected_apn_str ? selected_apn_str : "---",
789 req_apn_str);
790
791 return ggsn;
792}
793
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100794static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
795{
796 struct sgsn_mm_ctx *mmctx = NULL;
797
798 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100799 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100800 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
801 return;
802 }
803 }
804
805 /* No MM context found */
806 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
807 llme->tlli);
Max39550252016-06-28 17:39:20 +0200808 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100809}
810
811static void sgsn_llme_check_cb(void *data_)
812{
813 struct gprs_llc_llme *llme, *llme_tmp;
814 struct timespec now_tp;
815 time_t now, age;
816 time_t max_age = gprs_max_time_to_idle();
817
818 int rc;
819
820 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
821 OSMO_ASSERT(rc >= 0);
822 now = now_tp.tv_sec;
823
824 LOGP(DGPRS, LOGL_DEBUG,
825 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
826
827 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
828 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
829 llme->age_timestamp = now;
830
831 age = now - llme->age_timestamp;
832
833 if (age > max_age || age < 0) {
834 LOGP(DGPRS, LOGL_INFO,
835 "Inactivity timeout for TLLI 0x%08x, age %d\n",
836 llme->tlli, (int)age);
837 sgsn_llme_cleanup_free(llme);
838 }
839 }
840
841 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
842}
843
844void sgsn_inst_init()
845{
846 sgsn->llme_timer.cb = sgsn_llme_check_cb;
847 sgsn->llme_timer.data = NULL;
848
849 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
850}
851