blob: 038e6f0ebc7fa07a4eb6ca5c3a75e3deaaa3bac2 [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
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010043#include <time.h>
44
Daniel Willmann044ce5f2015-10-12 19:36:33 +020045#include <openssl/rand.h>
46
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010047#define GPRS_LLME_CHECK_TICK 30
48
Harald Welteab1d5622010-05-18 19:58:38 +020049extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080050
Harald Welted193cb32010-05-17 22:58:03 +020051LLIST_HEAD(sgsn_mm_ctxts);
52LLIST_HEAD(sgsn_ggsn_ctxts);
53LLIST_HEAD(sgsn_apn_ctxts);
54LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080055
Harald Welte8acd88f2010-05-18 10:57:45 +020056static const struct rate_ctr_desc mmctx_ctr_description[] = {
57 { "sign.packets.in", "Signalling Messages ( In)" },
58 { "sign.packets.out", "Signalling Messages (Out)" },
59 { "udata.packets.in", "User Data Messages ( In)" },
60 { "udata.packets.out", "User Data Messages (Out)" },
61 { "udata.bytes.in", "User Data Bytes ( In)" },
62 { "udata.bytes.out", "User Data Bytes (Out)" },
63 { "pdp_ctx_act", "PDP Context Activations " },
64 { "suspend", "SUSPEND Count " },
65 { "paging.ps", "Paging Packet Switched " },
66 { "paging.cs", "Paging Circuit Switched " },
67 { "ra_update", "Routing Area Update " },
68};
69
70static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
71 .group_name_prefix = "sgsn.mmctx",
72 .group_description = "SGSN MM Context Statistics",
73 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
74 .ctr_desc = mmctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010075 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8acd88f2010-05-18 10:57:45 +020076};
77
Harald Welteefbdee92010-06-10 00:20:12 +020078static const struct rate_ctr_desc pdpctx_ctr_description[] = {
79 { "udata.packets.in", "User Data Messages ( In)" },
80 { "udata.packets.out", "User Data Messages (Out)" },
81 { "udata.bytes.in", "User Data Bytes ( In)" },
82 { "udata.bytes.out", "User Data Bytes (Out)" },
83};
84
85static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
86 .group_name_prefix = "sgsn.pdpctx",
87 .group_description = "SGSN PDP Context Statistics",
88 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
89 .ctr_desc = pdpctx_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010090 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welteefbdee92010-06-10 00:20:12 +020091};
92
Alexander Couzens14314bd2016-07-05 09:52:52 +020093static const struct rate_ctr_desc sgsn_ctr_description[] = {
Alexander Couzens4e699a92016-07-05 11:04:27 +020094 { "llc.dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" },
95 { "llc.ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" },
96 { "llc.dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" },
97 { "llc.ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" },
Alexander Couzens14314bd2016-07-05 09:52:52 +020098 { "gprs.attach_requested", "Received attach requests" },
99 { "gprs.attach_accepted", "Sent attach accepts" },
100 { "gprs.attach_rejected", "Sent attach rejects" },
101 { "gprs.detach_requested", "Received detach requests" },
102 { "gprs.detach_acked", "Sent detach acks" },
103 { "gprs.routing_area_requested", "Received routing area requests" },
104 { "gprs.routing_area_requested", "Sent routing area acks" },
105 { "gprs.routing_area_requested", "Sent routing area rejects" },
106 { "pdp.activate_requested", "Received activate requests" },
107 { "pdp.activate_rejected", "Sent activate rejects" },
108 { "pdp.activate_accepted", "Sent activate accepts" },
109 { "pdp.request_activated", "unused" },
110 { "pdp.request_activate_rejected", "unused" },
111 { "pdp.modify_requested", "unused" },
112 { "pdp.modify_accepted", "unused" },
113 { "pdp.dl_deactivate_requested", "Sent deactivate requests" },
114 { "pdp.dl_deactivate_accepted", "Sent deactivate accepted" },
115 { "pdp.ul_deactivate_requested", "Received deactivate requests" },
116 { "pdp.ul_deactivate_accepted", "Received deactivate accepts" },
117};
118
119static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
120 "sgsn",
121 "SGSN Overall Statistics",
122 OSMO_STATS_CLASS_GLOBAL,
123 ARRAY_SIZE(sgsn_ctr_description),
124 sgsn_ctr_description,
125};
126
127void sgsn_rate_ctr_init() {
128 sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0);
129}
130
Harald Welte9b455bf2010-03-14 15:45:01 +0800131/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200132struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800133 const struct gprs_ra_id *raid)
134{
135 struct sgsn_mm_ctx *ctx;
136
137 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100138 if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) &&
Jacob Erlbecke7bcdc32016-01-04 18:43:34 +0100139 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte9b455bf2010-03-14 15:45:01 +0800140 return ctx;
141 }
Harald Welteab1d5622010-05-18 19:58:38 +0200142
Harald Welte9b455bf2010-03-14 15:45:01 +0800143 return NULL;
144}
145
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100146struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
147 const struct gprs_ra_id *raid)
148{
149 struct sgsn_mm_ctx *ctx;
150 int tlli_type;
151
152 /* TODO: Also check the P_TMSI signature to be safe. That signature
153 * should be different (at least with a sufficiently high probability)
154 * after SGSN restarts and for multiple SGSN instances.
155 */
156
157 tlli_type = gprs_tlli_type(tlli);
158 if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL)
159 return NULL;
160
161 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
162 if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli ||
163 gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) &&
164 gprs_ra_id_equals(raid, &ctx->ra))
165 return ctx;
166 }
167
168 return NULL;
169}
170
Harald Welteeaa614c2010-05-02 11:26:34 +0200171struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800172{
173 struct sgsn_mm_ctx *ctx;
174
175 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200176 if (p_tmsi == ctx->p_tmsi ||
177 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800178 return ctx;
179 }
180 return NULL;
181}
182
183struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
184{
185 struct sgsn_mm_ctx *ctx;
186
187 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
188 if (!strcmp(imsi, ctx->imsi))
189 return ctx;
190 }
191 return NULL;
192
193}
194
195/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200196struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800197 const struct gprs_ra_id *raid)
198{
Harald Welte2720e732010-05-17 00:44:57 +0200199 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800200
Harald Welte2720e732010-05-17 00:44:57 +0200201 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800202 if (!ctx)
203 return NULL;
204
205 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
Harald Weltef97ee042015-12-25 19:12:21 +0100206 ctx->ran_type = MM_CTX_T_GERAN_Gb;
207 ctx->gb.tlli = tlli;
Harald Welte9b455bf2010-03-14 15:45:01 +0800208 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100209 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Maxb997f842016-07-06 15:57:01 +0200210 ctx->ciph_algo = sgsn->cfg.cipher;
Harald Welte8acd88f2010-05-18 10:57:45 +0200211 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200212 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800213
214 llist_add(&ctx->list, &sgsn_mm_ctxts);
215
216 return ctx;
217}
Harald Welted193cb32010-05-17 22:58:03 +0200218
Harald Welte7b022ee2012-07-14 12:04:04 +0200219/* this is a hard _free_ function, it doesn't clean up the PDP contexts
220 * in libgtp! */
Holger Hans Peter Freytherb448dd82015-05-03 11:46:58 +0200221static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec728eea2010-12-24 23:07:18 +0100222{
223 struct sgsn_pdp_ctx *pdp, *pdp2;
224
Jacob Erlbecke671d252015-01-26 14:43:07 +0100225 /* Unlink from global list of MM contexts */
226 llist_del(&mm->list);
227
228 /* Free all PDP contexts */
229 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
230 sgsn_pdp_ctx_free(pdp);
231
232 rate_ctr_group_free(mm->ctrg);
233
234 talloc_free(mm);
235}
236
237void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
238{
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200239 struct gprs_llc_llme *llme = NULL;
Harald Weltef97ee042015-12-25 19:12:21 +0100240 uint32_t tlli = mm->gb.tlli;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100241 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200242 struct sgsn_signal_data sig_data;
Jacob Erlbecke671d252015-01-26 14:43:07 +0100243
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200244 if (mm->ran_type == MM_CTX_T_GERAN_Gb)
245 llme = mm->gb.llme;
246 else
247 OSMO_ASSERT(mm->gb.llme == NULL);
248
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800249 /* Forget about ongoing look-ups */
250 if (mm->ggsn_lookup) {
251 LOGMMCTXP(LOGL_NOTICE, mm,
252 "Cleaning mmctx with on-going query.\n");
253 mm->ggsn_lookup->mmctx = NULL;
254 mm->ggsn_lookup = NULL;
255 }
256
Jacob Erlbecke671d252015-01-26 14:43:07 +0100257 /* delete all existing PDP contexts for this MS */
258 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
259 LOGMMCTXP(LOGL_NOTICE, mm,
260 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
261 sgsn_pdp_ctx_terminate(pdp);
262 }
263
Jacob Erlbeckae20b4b2014-10-20 16:05:55 +0200264 if (osmo_timer_pending(&mm->timer)) {
265 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
266 osmo_timer_del(&mm->timer);
267 }
268
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200269 memset(&sig_data, 0, sizeof(sig_data));
270 sig_data.mm = mm;
271 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
272
273
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100274 /* Detach from subscriber which is possibly freed then */
275 if (mm->subscr) {
Jacob Erlbeck306bb992015-01-26 13:41:11 +0100276 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbeck3e4e58f2015-01-26 11:07:24 +0100277 gprs_subscr_cleanup(subscr);
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100278 subscr_put(subscr);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100279 }
280
Jacob Erlbecke671d252015-01-26 14:43:07 +0100281 sgsn_mm_ctx_free(mm);
282 mm = NULL;
Harald Weltec728eea2010-12-24 23:07:18 +0100283
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200284 if (llme) {
285 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
Max5aa51962016-07-06 11:33:04 +0200286 gprs_llgmm_assign(llme, tlli, 0xffffffff);
Daniel Willmann7ec8ca42016-05-21 00:48:49 +0200287 }
Harald Weltec728eea2010-12-24 23:07:18 +0100288}
Harald Welte77289c22010-05-18 14:32:29 +0200289
Jacob Erlbecke671d252015-01-26 14:43:07 +0100290
Harald Welte96df6062010-06-03 06:37:26 +0200291/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200292struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
293 uint8_t nsapi)
294{
295 struct sgsn_pdp_ctx *pdp;
296
297 llist_for_each_entry(pdp, &mm->pdp_list, list) {
298 if (pdp->nsapi == nsapi)
299 return pdp;
300 }
301 return NULL;
302}
303
Harald Welte96df6062010-06-03 06:37:26 +0200304/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200305struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
306 uint8_t tid)
307{
308 struct sgsn_pdp_ctx *pdp;
309
310 llist_for_each_entry(pdp, &mm->pdp_list, list) {
311 if (pdp->ti == tid)
312 return pdp;
313 }
314 return NULL;
315}
316
Harald Welte7b022ee2012-07-14 12:04:04 +0200317/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200318struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
319 uint8_t nsapi)
320{
321 struct sgsn_pdp_ctx *pdp;
322
323 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
324 if (pdp)
325 return NULL;
326
327 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
328 if (!pdp)
329 return NULL;
330
331 pdp->mm = mm;
332 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200333 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200334 llist_add(&pdp->list, &mm->pdp_list);
335 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
336
337 return pdp;
338}
339
Harald Weltefdf453c2012-07-14 12:15:19 +0200340#include <pdp.h>
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200341/*
342 * This function will not trigger any GSM DEACT PDP ACK messages, so you
343 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
344 * isn't detached already.
345 */
346void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
347{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200348 struct sgsn_signal_data sig_data;
349
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200350 OSMO_ASSERT(pdp->mm != NULL);
351
352 /* There might still be pending callbacks in libgtp. So the parts of
353 * this object relevant to GTP need to remain intact in this case. */
354
355 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
356
Daniel Willmannf9f43872016-05-20 22:36:23 +0200357 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
358 /* Force the deactivation of the SNDCP layer */
359 sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi);
360 }
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200361
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200362 memset(&sig_data, 0, sizeof(sig_data));
363 sig_data.pdp = pdp;
364 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
365
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200366 /* Detach from MM context */
367 llist_del(&pdp->list);
368 pdp->mm = NULL;
369
370 sgsn_delete_pdp_ctx(pdp);
371}
372
373/*
374 * Don't call this function directly unless you know what you are doing.
375 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
376 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
377 */
Harald Welted193cb32010-05-17 22:58:03 +0200378void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
379{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200380 struct sgsn_signal_data sig_data;
381
382 memset(&sig_data, 0, sizeof(sig_data));
383 sig_data.pdp = pdp;
384 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
385
Harald Welte376d5e52010-06-28 18:57:21 +0200386 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200387 if (pdp->mm)
388 llist_del(&pdp->list);
Harald Welted193cb32010-05-17 22:58:03 +0200389 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200390
391 /* _if_ we still have a library handle, at least set it to NULL
392 * to avoid any dereferences of the now-deleted PDP context from
393 * sgsn_libgtp:cb_data_ind() */
394 if (pdp->lib) {
395 struct pdp_t *lib = pdp->lib;
Daniel Willmann46553142014-09-03 17:46:44 +0200396 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Weltefdf453c2012-07-14 12:15:19 +0200397 "has a libgtp handle attached to it, this shouldn't "
398 "happen!\n");
399 osmo_generate_backtrace();
400 lib->priv = NULL;
401 }
402
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800403 if (pdp->destroy_ggsn)
404 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Welted193cb32010-05-17 22:58:03 +0200405 talloc_free(pdp);
406}
407
408/* GGSN contexts */
409
Harald Welte77289c22010-05-18 14:32:29 +0200410struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200411{
Harald Welte77289c22010-05-18 14:32:29 +0200412 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200413
Harald Welte77289c22010-05-18 14:32:29 +0200414 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200415 if (!ggc)
416 return NULL;
417
418 ggc->id = id;
419 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100420 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200421 /* if we are called from config file parse, this gsn doesn't exist yet */
422 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200423 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200424
425 return ggc;
426}
427
Jacob Erlbeckf3456122015-02-03 19:53:15 +0100428void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
429{
430 llist_del(&ggc->list);
431 talloc_free(ggc);
432}
433
Harald Welte77289c22010-05-18 14:32:29 +0200434struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200435{
Harald Welte77289c22010-05-18 14:32:29 +0200436 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200437
438 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
439 if (id == ggc->id)
440 return ggc;
441 }
442 return NULL;
443}
444
Harald Weltea9b473a2010-12-24 21:13:26 +0100445struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
446{
447 struct sgsn_ggsn_ctx *ggc;
448
449 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
450 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
451 return ggc;
452 }
453 return NULL;
454}
455
456
Harald Welte77289c22010-05-18 14:32:29 +0200457struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200458{
Harald Welte77289c22010-05-18 14:32:29 +0200459 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200460
Harald Welte77289c22010-05-18 14:32:29 +0200461 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200462 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200463 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200464 return ggc;
465}
466
467/* APN contexts */
468
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100469static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200470{
471 struct apn_ctx *actx;
472
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100473 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200474 if (!actx)
475 return NULL;
476 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100477 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
478
479 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200480
481 return actx;
482}
483
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100484void sgsn_apn_ctx_free(struct apn_ctx *actx)
485{
486 llist_del(&actx->list);
487 talloc_free(actx);
488}
489
490struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
491{
492 struct apn_ctx *actx;
493 struct apn_ctx *found_actx = NULL;
494 size_t imsi_prio = 0;
495 size_t name_prio = 0;
496 size_t name_req_len = strlen(name);
497
498 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
499 size_t name_ref_len, imsi_ref_len;
500 const char *name_ref_start, *name_match_start;
501
502 imsi_ref_len = strlen(actx->imsi_prefix);
503 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
504 continue;
505
506 if (imsi_ref_len < imsi_prio)
507 continue;
508
509 /* IMSI matches */
510
511 name_ref_start = &actx->name[0];
512 if (name_ref_start[0] == '*') {
513 /* Suffix match */
514 name_ref_start += 1;
515 name_ref_len = strlen(name_ref_start);
516 if (name_ref_len > name_req_len)
517 continue;
518 } else {
519 name_ref_len = strlen(name_ref_start);
520 if (name_ref_len != name_req_len)
521 continue;
522 }
523
524 name_match_start = name + (name_req_len - name_ref_len);
525 if (strcasecmp(name_match_start, name_ref_start) != 0)
526 continue;
527
528 /* IMSI and name match */
529
530 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
531 /* Lower priority, skip */
532 continue;
533
534 imsi_prio = imsi_ref_len;
535 name_prio = name_ref_len;
536 found_actx = actx;
537 }
538 return found_actx;
539}
540
541struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200542{
543 struct apn_ctx *actx;
544
545 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100546 if (strcasecmp(name, actx->name) == 0 &&
547 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Welted193cb32010-05-17 22:58:03 +0200548 return actx;
549 }
550 return NULL;
551}
552
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100553struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Welted193cb32010-05-17 22:58:03 +0200554{
555 struct apn_ctx *actx;
556
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100557 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200558 if (!actx)
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100559 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Welted193cb32010-05-17 22:58:03 +0200560
561 return actx;
562}
Harald Welte6463c072010-05-18 17:04:55 +0200563
564uint32_t sgsn_alloc_ptmsi(void)
565{
566 struct sgsn_mm_ctx *mm;
567 uint32_t ptmsi;
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100568 int max_retries = 100;
Harald Welte6463c072010-05-18 17:04:55 +0200569
570restart:
Daniel Willmann044ce5f2015-10-12 19:36:33 +0200571 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
572 goto failed;
573
Jacob Erlbeckd8a65532015-01-15 18:51:31 +0100574 /* Enforce that the 2 MSB are set without loosing the distance between
575 * identical values. Since rand() has no duplicate values within a
576 * period (because the size of the state is the same like the size of
577 * the random value), this leads to a distance of period/4 when the
578 * distribution of the 2 MSB is uniform. This approach fails with a
579 * probability of (3/4)^max_retries, only 1% of the approaches will
580 * need more than 16 numbers (even distribution assumed).
581 *
582 * Alternatively, a freeze list could be used if another PRNG is used
583 * or when this approach proves to be not sufficient.
584 */
585 if (ptmsi >= 0xC0000000) {
586 if (!max_retries--)
587 goto failed;
588 goto restart;
589 }
590 ptmsi |= 0xC0000000;
591
592 if (ptmsi == GSM_RESERVED_TMSI) {
593 if (!max_retries--)
594 goto failed;
595 goto restart;
596 }
597
Harald Welte6463c072010-05-18 17:04:55 +0200598 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200599 if (mm->p_tmsi == ptmsi) {
600 if (!max_retries--)
601 goto failed;
Harald Welte6463c072010-05-18 17:04:55 +0200602 goto restart;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200603 }
Harald Welte6463c072010-05-18 17:04:55 +0200604 }
605
606 return ptmsi;
Jacob Erlbeck08fbeb82014-09-19 09:28:42 +0200607
608failed:
609 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
610 return GSM_RESERVED_TMSI;
Harald Welte6463c072010-05-18 17:04:55 +0200611}
Harald Weltea9b473a2010-12-24 21:13:26 +0100612
613static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
614{
615 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
616 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
617 else {
618 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmann46553142014-09-03 17:46:44 +0200619 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Weltea9b473a2010-12-24 21:13:26 +0100620 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200621 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100622 sgsn_pdp_ctx_free(pdp);
623 }
624}
625
626/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freyther19e990d2014-10-27 10:24:37 +0100627 * otherwise lost state (recovery procedure) */
Harald Weltea9b473a2010-12-24 21:13:26 +0100628int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
629{
630 struct sgsn_mm_ctx *mm;
631 int num = 0;
632
633 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
634 struct sgsn_pdp_ctx *pdp;
635 llist_for_each_entry(pdp, &mm->pdp_list, list) {
636 if (pdp->ggsn == ggsn) {
637 drop_one_pdp(pdp);
638 num++;
639 }
640 }
641 }
642
643 return num;
644}
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200645
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100646void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200647{
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100648 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeckc9391962014-12-18 09:53:07 +0100649 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100650
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100651 sgsn_auth_update(mmctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200652}
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100653
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400654static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
655{
656 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
657 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
658}
659
660/**
661 * The tlv_parsed tp parameter will be modified to insert a
662 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
663 * PDP context handling.
664 */
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100665struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
666 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800667 enum gsm48_gsm_cause *gsm_cause,
668 char *out_apn_str)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100669{
670 char req_apn_str[GSM_APN_LENGTH] = {0};
671 const struct apn_ctx *apn_ctx = NULL;
672 const char *selected_apn_str = NULL;
673 struct sgsn_subscriber_pdp_data *pdp;
674 struct sgsn_ggsn_ctx *ggsn = NULL;
675 int allow_any_apn = 0;
676
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800677 out_apn_str[0] = '\0';
678
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100679 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
680 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
681 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
682 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
683 return NULL;
684 }
685
686 gprs_apn_to_str(req_apn_str,
687 TLVP_VAL(tp, GSM48_IE_GSM_APN),
688 TLVP_LEN(tp, GSM48_IE_GSM_APN));
689
690 if (strcmp(req_apn_str, "*") == 0)
691 req_apn_str[0] = 0;
692 }
693
Holger Hans Peter Freyther9270d992015-05-24 20:51:17 +0800694 if (mmctx->subscr == NULL)
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100695 allow_any_apn = 1;
696
697 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
698 /* No specific APN requested, check for an APN that is both
699 * granted and configured */
700
701 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
702 if (strcmp(pdp->apn_str, "*") == 0)
703 {
704 allow_any_apn = 1;
705 selected_apn_str = "";
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400706 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100707 continue;
708 }
709 if (!llist_empty(&sgsn_apn_ctxts)) {
710 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
711 /* Not configured */
712 if (apn_ctx == NULL)
713 continue;
714 }
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400715 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100716 selected_apn_str = pdp->apn_str;
717 break;
718 }
719 } else if (!allow_any_apn) {
720 /* Check whether the given APN is granted */
721 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
722 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400723 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100724 selected_apn_str = req_apn_str;
725 allow_any_apn = 1;
726 continue;
727 }
728 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther8cedded2015-04-23 11:33:35 -0400729 insert_qos(tp, pdp);
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100730 selected_apn_str = req_apn_str;
731 break;
732 }
733 }
734 } else if (strlen(req_apn_str) != 0) {
735 /* Any APN is allowed */
736 selected_apn_str = req_apn_str;
737 } else {
738 /* Prefer the GGSN associated with the wildcard APN */
739 selected_apn_str = "";
740 }
741
742 if (!allow_any_apn && selected_apn_str == NULL) {
743 /* Access not granted */
744 LOGMMCTXP(LOGL_NOTICE, mmctx,
745 "The requested APN '%s' is not allowed\n",
746 req_apn_str);
747 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
748 return NULL;
749 }
750
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800751 /* copy the selected apn_str */
Holger Hans Peter Freytherf2e114a2015-06-02 09:33:31 +0200752 if (selected_apn_str)
753 strcpy(out_apn_str, selected_apn_str);
754 else
755 out_apn_str[0] = '\0';
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800756
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100757 if (apn_ctx == NULL && selected_apn_str)
758 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
759
760 if (apn_ctx != NULL) {
761 ggsn = apn_ctx->ggsn;
762 } else if (llist_empty(&sgsn_apn_ctxts)) {
763 /* No configuration -> use GGSN 0 */
764 ggsn = sgsn_ggsn_ctx_by_id(0);
765 } else if (allow_any_apn &&
766 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
767 /* No APN given and no default configuration -> Use GGSN 0 */
768 ggsn = sgsn_ggsn_ctx_by_id(0);
769 } else {
770 /* No matching configuration found */
771 LOGMMCTXP(LOGL_NOTICE, mmctx,
772 "The selected APN '%s' has not been configured\n",
773 selected_apn_str);
774 *gsm_cause = GSM_CAUSE_MISSING_APN;
775 return NULL;
776 }
777
Holger Hans Peter Freyther08bb84b2015-05-25 14:35:10 +0800778 if (!ggsn) {
779 LOGMMCTXP(LOGL_NOTICE, mmctx,
780 "No static GGSN configured. Selected APN '%s'\n",
781 selected_apn_str);
782 return NULL;
783 }
784
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100785 LOGMMCTXP(LOGL_INFO, mmctx,
786 "Found GGSN %d for APN '%s' (requested '%s')\n",
787 ggsn->id, selected_apn_str ? selected_apn_str : "---",
788 req_apn_str);
789
790 return ggsn;
791}
792
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100793static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
794{
795 struct sgsn_mm_ctx *mmctx = NULL;
796
797 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
Harald Weltef97ee042015-12-25 19:12:21 +0100798 if (llme == mmctx->gb.llme) {
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100799 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
800 return;
801 }
802 }
803
804 /* No MM context found */
805 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
806 llme->tlli);
Max39550252016-06-28 17:39:20 +0200807 gprs_llgmm_unassign(llme);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100808}
809
810static void sgsn_llme_check_cb(void *data_)
811{
812 struct gprs_llc_llme *llme, *llme_tmp;
813 struct timespec now_tp;
814 time_t now, age;
815 time_t max_age = gprs_max_time_to_idle();
816
817 int rc;
818
819 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
820 OSMO_ASSERT(rc >= 0);
821 now = now_tp.tv_sec;
822
823 LOGP(DGPRS, LOGL_DEBUG,
824 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
825
826 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
827 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
828 llme->age_timestamp = now;
829
830 age = now - llme->age_timestamp;
831
832 if (age > max_age || age < 0) {
833 LOGP(DGPRS, LOGL_INFO,
834 "Inactivity timeout for TLLI 0x%08x, age %d\n",
835 llme->tlli, (int)age);
836 sgsn_llme_cleanup_free(llme);
837 }
838 }
839
840 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
841}
842
843void sgsn_inst_init()
844{
845 sgsn->llme_timer.cb = sgsn_llme_check_cb;
846 sgsn->llme_timer.data = NULL;
847
848 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
849}
850