blob: 8f8bdc68e7fb36ed66d89a07403e67f50a231911 [file] [log] [blame]
Harald Welte75bb8202010-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 Welte0e3e88e2011-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 Welte75bb8202010-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 Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte75bb8202010-03-14 15:45:01 +080016 *
Harald Welte0e3e88e2011-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 Welte75bb8202010-03-14 15:45:01 +080019 *
20 */
21
Harald Welted85d9a92010-05-02 11:26:34 +020022#include <stdint.h>
Harald Welte75bb8202010-03-14 15:45:01 +080023
Pablo Neira Ayusodd5fff42011-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 Erlbeckd31596b2015-11-02 15:15:38 +010028#include <osmocom/core/stats.h>
Harald Welte095b3de2012-07-14 12:15:19 +020029#include <osmocom/core/backtrace.h>
Harald Weltecfb6b282012-06-16 14:59:56 +080030#include <osmocom/gprs/gprs_ns.h>
31#include <osmocom/gprs/gprs_bssgp.h>
32
Harald Welte75bb8202010-03-14 15:45:01 +080033#include <openbsc/gsm_subscriber.h>
Harald Weltef67a5f92010-04-26 19:18:54 +020034#include <openbsc/debug.h>
Harald Welte75bb8202010-03-14 15:45:01 +080035#include <openbsc/gprs_sgsn.h>
Harald Welteebe8a6d2010-05-18 19:58:38 +020036#include <openbsc/sgsn.h>
Harald Welte94ecef32010-12-24 21:13:26 +010037#include <openbsc/gsm_04_08_gprs.h>
38#include <openbsc/gprs_gmm.h>
Jacob Erlbeck3400f112015-02-02 18:03:05 +010039#include <openbsc/gprs_utils.h>
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +020040#include <openbsc/signal.h>
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +020041#include "openbsc/gprs_llc.h"
Harald Welteebe8a6d2010-05-18 19:58:38 +020042
Jacob Erlbecke6140d02015-01-23 11:33:51 +010043#include <time.h>
44
Daniel Willmannd7f6cdf2015-10-12 19:36:33 +020045#include <openssl/rand.h>
46
Jacob Erlbecke6140d02015-01-23 11:33:51 +010047#define GPRS_LLME_CHECK_TICK 30
48
Harald Welteebe8a6d2010-05-18 19:58:38 +020049extern struct sgsn_instance *sgsn;
Harald Welte75bb8202010-03-14 15:45:01 +080050
Harald Weltec1f6bfe2010-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 Welte75bb8202010-03-14 15:45:01 +080055
Harald Welte8a035af2010-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 Erlbeckd31596b2015-11-02 15:15:38 +010075 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte8a035af2010-05-18 10:57:45 +020076};
77
Harald Welte0fe506b2010-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 Erlbeckd31596b2015-11-02 15:15:38 +010090 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
Harald Welte0fe506b2010-06-10 00:20:12 +020091};
92
Harald Welte51f78ee2010-12-23 23:34:43 +010093/* See 03.02 Chapter 2.6 */
94static inline uint32_t tlli_foreign(uint32_t tlli)
95{
96 return ((tlli | 0x80000000) & ~0x40000000);
97}
98
Harald Welte75bb8202010-03-14 15:45:01 +080099/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welted85d9a92010-05-02 11:26:34 +0200100struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte75bb8202010-03-14 15:45:01 +0800101 const struct gprs_ra_id *raid)
102{
103 struct sgsn_mm_ctx *ctx;
Harald Welteebe8a6d2010-05-18 19:58:38 +0200104 int tlli_type;
Harald Welte75bb8202010-03-14 15:45:01 +0800105
106 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
107 if (tlli == ctx->tlli &&
Jacob Erlbeckd6466c92016-01-04 18:43:34 +0100108 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte75bb8202010-03-14 15:45:01 +0800109 return ctx;
110 }
Harald Welteebe8a6d2010-05-18 19:58:38 +0200111
112 tlli_type = gprs_tlli_type(tlli);
Harald Welte51f78ee2010-12-23 23:34:43 +0100113 switch (tlli_type) {
114 case TLLI_LOCAL:
Harald Welteebe8a6d2010-05-18 19:58:38 +0200115 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200116 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
117 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteebe8a6d2010-05-18 19:58:38 +0200118 ctx->tlli = tlli;
119 return ctx;
120 }
121 }
Harald Welte51f78ee2010-12-23 23:34:43 +0100122 break;
123 case TLLI_FOREIGN:
124 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
125 if (tlli == tlli_foreign(ctx->tlli) &&
Jacob Erlbeckd6466c92016-01-04 18:43:34 +0100126 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte51f78ee2010-12-23 23:34:43 +0100127 return ctx;
128 }
129 break;
130 default:
131 break;
Harald Welteebe8a6d2010-05-18 19:58:38 +0200132 }
133
Harald Welte75bb8202010-03-14 15:45:01 +0800134 return NULL;
135}
136
Harald Welted85d9a92010-05-02 11:26:34 +0200137struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte75bb8202010-03-14 15:45:01 +0800138{
139 struct sgsn_mm_ctx *ctx;
140
141 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200142 if (p_tmsi == ctx->p_tmsi ||
143 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte75bb8202010-03-14 15:45:01 +0800144 return ctx;
145 }
146 return NULL;
147}
148
149struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
150{
151 struct sgsn_mm_ctx *ctx;
152
153 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
154 if (!strcmp(imsi, ctx->imsi))
155 return ctx;
156 }
157 return NULL;
158
159}
160
161/* Allocate a new SGSN MM context */
Harald Welted85d9a92010-05-02 11:26:34 +0200162struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte75bb8202010-03-14 15:45:01 +0800163 const struct gprs_ra_id *raid)
164{
Harald Welte8f77f192010-05-17 00:44:57 +0200165 struct sgsn_mm_ctx *ctx;
Harald Welte75bb8202010-03-14 15:45:01 +0800166
Harald Welte8f77f192010-05-17 00:44:57 +0200167 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800168 if (!ctx)
169 return NULL;
170
171 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
172 ctx->tlli = tlli;
173 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeck4d127672014-12-01 12:33:33 +0100174 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8a035af2010-05-18 10:57:45 +0200175 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welteded83ec2010-05-18 12:44:45 +0200176 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte75bb8202010-03-14 15:45:01 +0800177
178 llist_add(&ctx->list, &sgsn_mm_ctxts);
179
180 return ctx;
181}
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200182
Harald Welted8765b92012-07-14 12:04:04 +0200183/* this is a hard _free_ function, it doesn't clean up the PDP contexts
184 * in libgtp! */
Holger Hans Peter Freytherea2825a2015-05-03 11:46:58 +0200185static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec6e196f2010-12-24 23:07:18 +0100186{
187 struct sgsn_pdp_ctx *pdp, *pdp2;
188
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100189 /* Unlink from global list of MM contexts */
190 llist_del(&mm->list);
191
192 /* Free all PDP contexts */
193 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
194 sgsn_pdp_ctx_free(pdp);
195
196 rate_ctr_group_free(mm->ctrg);
197
198 talloc_free(mm);
199}
200
201void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
202{
203 struct gprs_llc_llme *llme = mm->llme;
204 uint32_t tlli = mm->tlli;
205 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200206 struct sgsn_signal_data sig_data;
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100207
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800208 /* Forget about ongoing look-ups */
209 if (mm->ggsn_lookup) {
210 LOGMMCTXP(LOGL_NOTICE, mm,
211 "Cleaning mmctx with on-going query.\n");
212 mm->ggsn_lookup->mmctx = NULL;
213 mm->ggsn_lookup = NULL;
214 }
215
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100216 /* delete all existing PDP contexts for this MS */
217 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
218 LOGMMCTXP(LOGL_NOTICE, mm,
219 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
220 sgsn_pdp_ctx_terminate(pdp);
221 }
222
Jacob Erlbeck11493a22014-10-20 16:05:55 +0200223 if (osmo_timer_pending(&mm->timer)) {
224 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
225 osmo_timer_del(&mm->timer);
226 }
227
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200228 memset(&sig_data, 0, sizeof(sig_data));
229 sig_data.mm = mm;
230 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
231
232
Jacob Erlbeckd04f7cc2014-11-12 10:18:09 +0100233 /* Detach from subscriber which is possibly freed then */
234 if (mm->subscr) {
Jacob Erlbeck480e8202015-01-26 13:41:11 +0100235 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbecke71ab2f2015-01-26 11:07:24 +0100236 gprs_subscr_cleanup(subscr);
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100237 subscr_put(subscr);
Jacob Erlbeckd04f7cc2014-11-12 10:18:09 +0100238 }
239
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100240 sgsn_mm_ctx_free(mm);
241 mm = NULL;
Harald Weltec6e196f2010-12-24 23:07:18 +0100242
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100243 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
244 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec6e196f2010-12-24 23:07:18 +0100245}
Harald Welteeb471c92010-05-18 14:32:29 +0200246
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100247
Harald Welteff70e0e2010-06-03 06:37:26 +0200248/* look up PDP context by MM context and NSAPI */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200249struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
250 uint8_t nsapi)
251{
252 struct sgsn_pdp_ctx *pdp;
253
254 llist_for_each_entry(pdp, &mm->pdp_list, list) {
255 if (pdp->nsapi == nsapi)
256 return pdp;
257 }
258 return NULL;
259}
260
Harald Welteff70e0e2010-06-03 06:37:26 +0200261/* look up PDP context by MM context and transaction ID */
Harald Welteeb471c92010-05-18 14:32:29 +0200262struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
263 uint8_t tid)
264{
265 struct sgsn_pdp_ctx *pdp;
266
267 llist_for_each_entry(pdp, &mm->pdp_list, list) {
268 if (pdp->ti == tid)
269 return pdp;
270 }
271 return NULL;
272}
273
Harald Welted8765b92012-07-14 12:04:04 +0200274/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200275struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
276 uint8_t nsapi)
277{
278 struct sgsn_pdp_ctx *pdp;
279
280 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
281 if (pdp)
282 return NULL;
283
284 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
285 if (!pdp)
286 return NULL;
287
288 pdp->mm = mm;
289 pdp->nsapi = nsapi;
Harald Welte0fe506b2010-06-10 00:20:12 +0200290 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200291 llist_add(&pdp->list, &mm->pdp_list);
292 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
293
294 return pdp;
295}
296
Harald Welte095b3de2012-07-14 12:15:19 +0200297#include <pdp.h>
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200298/*
299 * This function will not trigger any GSM DEACT PDP ACK messages, so you
300 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
301 * isn't detached already.
302 */
303void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
304{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200305 struct sgsn_signal_data sig_data;
306
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200307 OSMO_ASSERT(pdp->mm != NULL);
308
309 /* There might still be pending callbacks in libgtp. So the parts of
310 * this object relevant to GTP need to remain intact in this case. */
311
312 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
313
314 /* Force the deactivation of the SNDCP layer */
315 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
316
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200317 memset(&sig_data, 0, sizeof(sig_data));
318 sig_data.pdp = pdp;
319 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
320
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200321 /* Detach from MM context */
322 llist_del(&pdp->list);
323 pdp->mm = NULL;
324
325 sgsn_delete_pdp_ctx(pdp);
326}
327
328/*
329 * Don't call this function directly unless you know what you are doing.
330 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
331 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
332 */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200333void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
334{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200335 struct sgsn_signal_data sig_data;
336
337 memset(&sig_data, 0, sizeof(sig_data));
338 sig_data.pdp = pdp;
339 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
340
Harald Welte17a40a62010-06-28 18:57:21 +0200341 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200342 if (pdp->mm)
343 llist_del(&pdp->list);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200344 llist_del(&pdp->g_list);
Harald Welte095b3de2012-07-14 12:15:19 +0200345
346 /* _if_ we still have a library handle, at least set it to NULL
347 * to avoid any dereferences of the now-deleted PDP context from
348 * sgsn_libgtp:cb_data_ind() */
349 if (pdp->lib) {
350 struct pdp_t *lib = pdp->lib;
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200351 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Welte095b3de2012-07-14 12:15:19 +0200352 "has a libgtp handle attached to it, this shouldn't "
353 "happen!\n");
354 osmo_generate_backtrace();
355 lib->priv = NULL;
356 }
357
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800358 if (pdp->destroy_ggsn)
359 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200360 talloc_free(pdp);
361}
362
363/* GGSN contexts */
364
Harald Welteeb471c92010-05-18 14:32:29 +0200365struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200366{
Harald Welteeb471c92010-05-18 14:32:29 +0200367 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200368
Harald Welteeb471c92010-05-18 14:32:29 +0200369 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200370 if (!ggc)
371 return NULL;
372
373 ggc->id = id;
374 ggc->gtp_version = 1;
Harald Welte94ecef32010-12-24 21:13:26 +0100375 ggc->remote_restart_ctr = -1;
Harald Welteebe8a6d2010-05-18 19:58:38 +0200376 /* if we are called from config file parse, this gsn doesn't exist yet */
377 ggc->gsn = sgsn->gsn;
Harald Weltee58eee52010-05-18 18:39:00 +0200378 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200379
380 return ggc;
381}
382
Jacob Erlbecke3c935d2015-02-03 19:53:15 +0100383void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
384{
385 llist_del(&ggc->list);
386 talloc_free(ggc);
387}
388
Harald Welteeb471c92010-05-18 14:32:29 +0200389struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200390{
Harald Welteeb471c92010-05-18 14:32:29 +0200391 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200392
393 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
394 if (id == ggc->id)
395 return ggc;
396 }
397 return NULL;
398}
399
Harald Welte94ecef32010-12-24 21:13:26 +0100400struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
401{
402 struct sgsn_ggsn_ctx *ggc;
403
404 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
405 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
406 return ggc;
407 }
408 return NULL;
409}
410
411
Harald Welteeb471c92010-05-18 14:32:29 +0200412struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200413{
Harald Welteeb471c92010-05-18 14:32:29 +0200414 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200415
Harald Welteeb471c92010-05-18 14:32:29 +0200416 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200417 if (!ggc)
Harald Welteeb471c92010-05-18 14:32:29 +0200418 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200419 return ggc;
420}
421
422/* APN contexts */
423
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100424static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200425{
426 struct apn_ctx *actx;
427
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100428 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200429 if (!actx)
430 return NULL;
431 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100432 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
433
434 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200435
436 return actx;
437}
438
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100439void sgsn_apn_ctx_free(struct apn_ctx *actx)
440{
441 llist_del(&actx->list);
442 talloc_free(actx);
443}
444
445struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
446{
447 struct apn_ctx *actx;
448 struct apn_ctx *found_actx = NULL;
449 size_t imsi_prio = 0;
450 size_t name_prio = 0;
451 size_t name_req_len = strlen(name);
452
453 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
454 size_t name_ref_len, imsi_ref_len;
455 const char *name_ref_start, *name_match_start;
456
457 imsi_ref_len = strlen(actx->imsi_prefix);
458 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
459 continue;
460
461 if (imsi_ref_len < imsi_prio)
462 continue;
463
464 /* IMSI matches */
465
466 name_ref_start = &actx->name[0];
467 if (name_ref_start[0] == '*') {
468 /* Suffix match */
469 name_ref_start += 1;
470 name_ref_len = strlen(name_ref_start);
471 if (name_ref_len > name_req_len)
472 continue;
473 } else {
474 name_ref_len = strlen(name_ref_start);
475 if (name_ref_len != name_req_len)
476 continue;
477 }
478
479 name_match_start = name + (name_req_len - name_ref_len);
480 if (strcasecmp(name_match_start, name_ref_start) != 0)
481 continue;
482
483 /* IMSI and name match */
484
485 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
486 /* Lower priority, skip */
487 continue;
488
489 imsi_prio = imsi_ref_len;
490 name_prio = name_ref_len;
491 found_actx = actx;
492 }
493 return found_actx;
494}
495
496struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200497{
498 struct apn_ctx *actx;
499
500 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100501 if (strcasecmp(name, actx->name) == 0 &&
502 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200503 return actx;
504 }
505 return NULL;
506}
507
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100508struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200509{
510 struct apn_ctx *actx;
511
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100512 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200513 if (!actx)
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100514 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200515
516 return actx;
517}
Harald Welte64df8ed2010-05-18 17:04:55 +0200518
519uint32_t sgsn_alloc_ptmsi(void)
520{
521 struct sgsn_mm_ctx *mm;
522 uint32_t ptmsi;
Jacob Erlbecke3aeb1e2015-01-15 18:51:31 +0100523 int max_retries = 100;
Harald Welte64df8ed2010-05-18 17:04:55 +0200524
525restart:
Daniel Willmannd7f6cdf2015-10-12 19:36:33 +0200526 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
527 goto failed;
528
Jacob Erlbecke3aeb1e2015-01-15 18:51:31 +0100529 /* Enforce that the 2 MSB are set without loosing the distance between
530 * identical values. Since rand() has no duplicate values within a
531 * period (because the size of the state is the same like the size of
532 * the random value), this leads to a distance of period/4 when the
533 * distribution of the 2 MSB is uniform. This approach fails with a
534 * probability of (3/4)^max_retries, only 1% of the approaches will
535 * need more than 16 numbers (even distribution assumed).
536 *
537 * Alternatively, a freeze list could be used if another PRNG is used
538 * or when this approach proves to be not sufficient.
539 */
540 if (ptmsi >= 0xC0000000) {
541 if (!max_retries--)
542 goto failed;
543 goto restart;
544 }
545 ptmsi |= 0xC0000000;
546
547 if (ptmsi == GSM_RESERVED_TMSI) {
548 if (!max_retries--)
549 goto failed;
550 goto restart;
551 }
552
Harald Welte64df8ed2010-05-18 17:04:55 +0200553 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200554 if (mm->p_tmsi == ptmsi) {
555 if (!max_retries--)
556 goto failed;
Harald Welte64df8ed2010-05-18 17:04:55 +0200557 goto restart;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200558 }
Harald Welte64df8ed2010-05-18 17:04:55 +0200559 }
560
561 return ptmsi;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200562
563failed:
564 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
565 return GSM_RESERVED_TMSI;
Harald Welte64df8ed2010-05-18 17:04:55 +0200566}
Harald Welte94ecef32010-12-24 21:13:26 +0100567
568static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
569{
570 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
571 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
572 else {
573 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200574 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Welte94ecef32010-12-24 21:13:26 +0100575 "recovery\n");
Harald Welted8765b92012-07-14 12:04:04 +0200576 /* FIXME: how to tell this to libgtp? */
Harald Welte94ecef32010-12-24 21:13:26 +0100577 sgsn_pdp_ctx_free(pdp);
578 }
579}
580
581/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freytheredcefd52014-10-27 10:24:37 +0100582 * otherwise lost state (recovery procedure) */
Harald Welte94ecef32010-12-24 21:13:26 +0100583int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
584{
585 struct sgsn_mm_ctx *mm;
586 int num = 0;
587
588 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
589 struct sgsn_pdp_ctx *pdp;
590 llist_for_each_entry(pdp, &mm->pdp_list, list) {
591 if (pdp->ggsn == ggsn) {
592 drop_one_pdp(pdp);
593 num++;
594 }
595 }
596 }
597
598 return num;
599}
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200600
601int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
602{
Jacob Erlbeck6e15b732014-10-31 12:20:49 +0100603 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200604}
605
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100606void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200607{
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100608 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeck0939c0f2014-12-18 09:53:07 +0100609 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckd04f7cc2014-11-12 10:18:09 +0100610
Jacob Erlbeckb2acd742014-11-13 10:48:39 +0100611 sgsn_auth_update(mmctx);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200612}
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100613
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400614static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
615{
616 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
617 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
618}
619
620/**
621 * The tlv_parsed tp parameter will be modified to insert a
622 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
623 * PDP context handling.
624 */
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100625struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
626 struct tlv_parsed *tp,
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800627 enum gsm48_gsm_cause *gsm_cause,
628 char *out_apn_str)
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100629{
630 char req_apn_str[GSM_APN_LENGTH] = {0};
631 const struct apn_ctx *apn_ctx = NULL;
632 const char *selected_apn_str = NULL;
633 struct sgsn_subscriber_pdp_data *pdp;
634 struct sgsn_ggsn_ctx *ggsn = NULL;
635 int allow_any_apn = 0;
636
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800637 out_apn_str[0] = '\0';
638
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100639 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
640 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
641 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
642 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
643 return NULL;
644 }
645
646 gprs_apn_to_str(req_apn_str,
647 TLVP_VAL(tp, GSM48_IE_GSM_APN),
648 TLVP_LEN(tp, GSM48_IE_GSM_APN));
649
650 if (strcmp(req_apn_str, "*") == 0)
651 req_apn_str[0] = 0;
652 }
653
Holger Hans Peter Freytherab9422e2015-05-24 20:51:17 +0800654 if (mmctx->subscr == NULL)
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100655 allow_any_apn = 1;
656
657 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
658 /* No specific APN requested, check for an APN that is both
659 * granted and configured */
660
661 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
662 if (strcmp(pdp->apn_str, "*") == 0)
663 {
664 allow_any_apn = 1;
665 selected_apn_str = "";
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400666 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100667 continue;
668 }
669 if (!llist_empty(&sgsn_apn_ctxts)) {
670 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
671 /* Not configured */
672 if (apn_ctx == NULL)
673 continue;
674 }
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400675 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100676 selected_apn_str = pdp->apn_str;
677 break;
678 }
679 } else if (!allow_any_apn) {
680 /* Check whether the given APN is granted */
681 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
682 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400683 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100684 selected_apn_str = req_apn_str;
685 allow_any_apn = 1;
686 continue;
687 }
688 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400689 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100690 selected_apn_str = req_apn_str;
691 break;
692 }
693 }
694 } else if (strlen(req_apn_str) != 0) {
695 /* Any APN is allowed */
696 selected_apn_str = req_apn_str;
697 } else {
698 /* Prefer the GGSN associated with the wildcard APN */
699 selected_apn_str = "";
700 }
701
702 if (!allow_any_apn && selected_apn_str == NULL) {
703 /* Access not granted */
704 LOGMMCTXP(LOGL_NOTICE, mmctx,
705 "The requested APN '%s' is not allowed\n",
706 req_apn_str);
707 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
708 return NULL;
709 }
710
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800711 /* copy the selected apn_str */
Holger Hans Peter Freyther3332d0c2015-06-02 09:33:31 +0200712 if (selected_apn_str)
713 strcpy(out_apn_str, selected_apn_str);
714 else
715 out_apn_str[0] = '\0';
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800716
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100717 if (apn_ctx == NULL && selected_apn_str)
718 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
719
720 if (apn_ctx != NULL) {
721 ggsn = apn_ctx->ggsn;
722 } else if (llist_empty(&sgsn_apn_ctxts)) {
723 /* No configuration -> use GGSN 0 */
724 ggsn = sgsn_ggsn_ctx_by_id(0);
725 } else if (allow_any_apn &&
726 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
727 /* No APN given and no default configuration -> Use GGSN 0 */
728 ggsn = sgsn_ggsn_ctx_by_id(0);
729 } else {
730 /* No matching configuration found */
731 LOGMMCTXP(LOGL_NOTICE, mmctx,
732 "The selected APN '%s' has not been configured\n",
733 selected_apn_str);
734 *gsm_cause = GSM_CAUSE_MISSING_APN;
735 return NULL;
736 }
737
Holger Hans Peter Freythereaa58512015-05-25 14:35:10 +0800738 if (!ggsn) {
739 LOGMMCTXP(LOGL_NOTICE, mmctx,
740 "No static GGSN configured. Selected APN '%s'\n",
741 selected_apn_str);
742 return NULL;
743 }
744
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100745 LOGMMCTXP(LOGL_INFO, mmctx,
746 "Found GGSN %d for APN '%s' (requested '%s')\n",
747 ggsn->id, selected_apn_str ? selected_apn_str : "---",
748 req_apn_str);
749
750 return ggsn;
751}
752
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100753static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
754{
755 struct sgsn_mm_ctx *mmctx = NULL;
756
757 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
758 if (llme == mmctx->llme) {
759 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
760 return;
761 }
762 }
763
764 /* No MM context found */
765 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
766 llme->tlli);
767 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
768}
769
770static void sgsn_llme_check_cb(void *data_)
771{
772 struct gprs_llc_llme *llme, *llme_tmp;
773 struct timespec now_tp;
774 time_t now, age;
775 time_t max_age = gprs_max_time_to_idle();
776
777 int rc;
778
779 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
780 OSMO_ASSERT(rc >= 0);
781 now = now_tp.tv_sec;
782
783 LOGP(DGPRS, LOGL_DEBUG,
784 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
785
786 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
787 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
788 llme->age_timestamp = now;
789
790 age = now - llme->age_timestamp;
791
792 if (age > max_age || age < 0) {
793 LOGP(DGPRS, LOGL_INFO,
794 "Inactivity timeout for TLLI 0x%08x, age %d\n",
795 llme->tlli, (int)age);
796 sgsn_llme_cleanup_free(llme);
797 }
798 }
799
800 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
801}
802
803void sgsn_inst_init()
804{
805 sgsn->llme_timer.cb = sgsn_llme_check_cb;
806 sgsn->llme_timer.data = NULL;
807
808 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
809}
810