blob: f71066d8e1709863953355afe75ddc13a9e6abb0 [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 Welte75bb8202010-03-14 15:45:01 +080093/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welted85d9a92010-05-02 11:26:34 +020094struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte75bb8202010-03-14 15:45:01 +080095 const struct gprs_ra_id *raid)
96{
97 struct sgsn_mm_ctx *ctx;
98
99 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Jacob Erlbeckf963e7e2016-01-04 18:43:35 +0100100 if ((tlli == ctx->tlli || tlli == ctx->tlli_new) &&
Jacob Erlbeckd6466c92016-01-04 18:43:34 +0100101 gprs_ra_id_equals(raid, &ctx->ra))
Harald Welte75bb8202010-03-14 15:45:01 +0800102 return ctx;
103 }
Harald Welteebe8a6d2010-05-18 19:58:38 +0200104
Harald Welte75bb8202010-03-14 15:45:01 +0800105 return NULL;
106}
107
Harald Welted85d9a92010-05-02 11:26:34 +0200108struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte75bb8202010-03-14 15:45:01 +0800109{
110 struct sgsn_mm_ctx *ctx;
111
112 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Welte7e01d8e2010-05-31 20:23:38 +0200113 if (p_tmsi == ctx->p_tmsi ||
114 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte75bb8202010-03-14 15:45:01 +0800115 return ctx;
116 }
117 return NULL;
118}
119
120struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
121{
122 struct sgsn_mm_ctx *ctx;
123
124 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
125 if (!strcmp(imsi, ctx->imsi))
126 return ctx;
127 }
128 return NULL;
129
130}
131
132/* Allocate a new SGSN MM context */
Harald Welted85d9a92010-05-02 11:26:34 +0200133struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte75bb8202010-03-14 15:45:01 +0800134 const struct gprs_ra_id *raid)
135{
Harald Welte8f77f192010-05-17 00:44:57 +0200136 struct sgsn_mm_ctx *ctx;
Harald Welte75bb8202010-03-14 15:45:01 +0800137
Harald Welte8f77f192010-05-17 00:44:57 +0200138 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800139 if (!ctx)
140 return NULL;
141
142 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
143 ctx->tlli = tlli;
144 ctx->mm_state = GMM_DEREGISTERED;
Jacob Erlbeck4d127672014-12-01 12:33:33 +0100145 ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL;
Harald Welte8a035af2010-05-18 10:57:45 +0200146 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welteded83ec2010-05-18 12:44:45 +0200147 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte75bb8202010-03-14 15:45:01 +0800148
149 llist_add(&ctx->list, &sgsn_mm_ctxts);
150
151 return ctx;
152}
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200153
Harald Welted8765b92012-07-14 12:04:04 +0200154/* this is a hard _free_ function, it doesn't clean up the PDP contexts
155 * in libgtp! */
Holger Hans Peter Freytherea2825a2015-05-03 11:46:58 +0200156static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
Harald Weltec6e196f2010-12-24 23:07:18 +0100157{
158 struct sgsn_pdp_ctx *pdp, *pdp2;
159
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100160 /* Unlink from global list of MM contexts */
161 llist_del(&mm->list);
162
163 /* Free all PDP contexts */
164 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
165 sgsn_pdp_ctx_free(pdp);
166
167 rate_ctr_group_free(mm->ctrg);
168
169 talloc_free(mm);
170}
171
172void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm)
173{
174 struct gprs_llc_llme *llme = mm->llme;
175 uint32_t tlli = mm->tlli;
176 struct sgsn_pdp_ctx *pdp, *pdp2;
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200177 struct sgsn_signal_data sig_data;
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100178
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800179 /* Forget about ongoing look-ups */
180 if (mm->ggsn_lookup) {
181 LOGMMCTXP(LOGL_NOTICE, mm,
182 "Cleaning mmctx with on-going query.\n");
183 mm->ggsn_lookup->mmctx = NULL;
184 mm->ggsn_lookup = NULL;
185 }
186
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100187 /* delete all existing PDP contexts for this MS */
188 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) {
189 LOGMMCTXP(LOGL_NOTICE, mm,
190 "Dropping PDP context for NSAPI=%u\n", pdp->nsapi);
191 sgsn_pdp_ctx_terminate(pdp);
192 }
193
Jacob Erlbeck11493a22014-10-20 16:05:55 +0200194 if (osmo_timer_pending(&mm->timer)) {
195 LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T);
196 osmo_timer_del(&mm->timer);
197 }
198
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200199 memset(&sig_data, 0, sizeof(sig_data));
200 sig_data.mm = mm;
201 osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data);
202
203
Jacob Erlbeckd04f7cc2014-11-12 10:18:09 +0100204 /* Detach from subscriber which is possibly freed then */
205 if (mm->subscr) {
Jacob Erlbeck480e8202015-01-26 13:41:11 +0100206 struct gsm_subscriber *subscr = subscr_get(mm->subscr);
Jacob Erlbecke71ab2f2015-01-26 11:07:24 +0100207 gprs_subscr_cleanup(subscr);
Jacob Erlbeck7a7d8812015-01-23 13:52:55 +0100208 subscr_put(subscr);
Jacob Erlbeckd04f7cc2014-11-12 10:18:09 +0100209 }
210
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100211 sgsn_mm_ctx_free(mm);
212 mm = NULL;
Harald Weltec6e196f2010-12-24 23:07:18 +0100213
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100214 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
215 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
Harald Weltec6e196f2010-12-24 23:07:18 +0100216}
Harald Welteeb471c92010-05-18 14:32:29 +0200217
Jacob Erlbeck70c177a2015-01-26 14:43:07 +0100218
Harald Welteff70e0e2010-06-03 06:37:26 +0200219/* look up PDP context by MM context and NSAPI */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200220struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
221 uint8_t nsapi)
222{
223 struct sgsn_pdp_ctx *pdp;
224
225 llist_for_each_entry(pdp, &mm->pdp_list, list) {
226 if (pdp->nsapi == nsapi)
227 return pdp;
228 }
229 return NULL;
230}
231
Harald Welteff70e0e2010-06-03 06:37:26 +0200232/* look up PDP context by MM context and transaction ID */
Harald Welteeb471c92010-05-18 14:32:29 +0200233struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
234 uint8_t tid)
235{
236 struct sgsn_pdp_ctx *pdp;
237
238 llist_for_each_entry(pdp, &mm->pdp_list, list) {
239 if (pdp->ti == tid)
240 return pdp;
241 }
242 return NULL;
243}
244
Harald Welted8765b92012-07-14 12:04:04 +0200245/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200246struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
247 uint8_t nsapi)
248{
249 struct sgsn_pdp_ctx *pdp;
250
251 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
252 if (pdp)
253 return NULL;
254
255 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
256 if (!pdp)
257 return NULL;
258
259 pdp->mm = mm;
260 pdp->nsapi = nsapi;
Harald Welte0fe506b2010-06-10 00:20:12 +0200261 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200262 llist_add(&pdp->list, &mm->pdp_list);
263 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
264
265 return pdp;
266}
267
Harald Welte095b3de2012-07-14 12:15:19 +0200268#include <pdp.h>
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200269/*
270 * This function will not trigger any GSM DEACT PDP ACK messages, so you
271 * probably want to call sgsn_delete_pdp_ctx() instead if the connection
272 * isn't detached already.
273 */
274void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp)
275{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200276 struct sgsn_signal_data sig_data;
277
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200278 OSMO_ASSERT(pdp->mm != NULL);
279
280 /* There might still be pending callbacks in libgtp. So the parts of
281 * this object relevant to GTP need to remain intact in this case. */
282
283 LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n");
284
285 /* Force the deactivation of the SNDCP layer */
286 sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi);
287
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200288 memset(&sig_data, 0, sizeof(sig_data));
289 sig_data.pdp = pdp;
290 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data);
291
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200292 /* Detach from MM context */
293 llist_del(&pdp->list);
294 pdp->mm = NULL;
295
296 sgsn_delete_pdp_ctx(pdp);
297}
298
299/*
300 * Don't call this function directly unless you know what you are doing.
301 * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or
302 * implementation dependent abnormal ones sgsn_pdp_ctx_terminate.
303 */
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200304void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
305{
Holger Hans Peter Freytherf14098a2015-05-02 19:55:38 +0200306 struct sgsn_signal_data sig_data;
307
308 memset(&sig_data, 0, sizeof(sig_data));
309 sig_data.pdp = pdp;
310 osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data);
311
Harald Welte17a40a62010-06-28 18:57:21 +0200312 rate_ctr_group_free(pdp->ctrg);
Jacob Erlbeckd781c7a2014-10-13 10:32:00 +0200313 if (pdp->mm)
314 llist_del(&pdp->list);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200315 llist_del(&pdp->g_list);
Harald Welte095b3de2012-07-14 12:15:19 +0200316
317 /* _if_ we still have a library handle, at least set it to NULL
318 * to avoid any dereferences of the now-deleted PDP context from
319 * sgsn_libgtp:cb_data_ind() */
320 if (pdp->lib) {
321 struct pdp_t *lib = pdp->lib;
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200322 LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still "
Harald Welte095b3de2012-07-14 12:15:19 +0200323 "has a libgtp handle attached to it, this shouldn't "
324 "happen!\n");
325 osmo_generate_backtrace();
326 lib->priv = NULL;
327 }
328
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800329 if (pdp->destroy_ggsn)
330 sgsn_ggsn_ctx_free(pdp->ggsn);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200331 talloc_free(pdp);
332}
333
334/* GGSN contexts */
335
Harald Welteeb471c92010-05-18 14:32:29 +0200336struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200337{
Harald Welteeb471c92010-05-18 14:32:29 +0200338 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200339
Harald Welteeb471c92010-05-18 14:32:29 +0200340 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200341 if (!ggc)
342 return NULL;
343
344 ggc->id = id;
345 ggc->gtp_version = 1;
Harald Welte94ecef32010-12-24 21:13:26 +0100346 ggc->remote_restart_ctr = -1;
Harald Welteebe8a6d2010-05-18 19:58:38 +0200347 /* if we are called from config file parse, this gsn doesn't exist yet */
348 ggc->gsn = sgsn->gsn;
Harald Weltee58eee52010-05-18 18:39:00 +0200349 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200350
351 return ggc;
352}
353
Jacob Erlbecke3c935d2015-02-03 19:53:15 +0100354void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc)
355{
356 llist_del(&ggc->list);
357 talloc_free(ggc);
358}
359
Harald Welteeb471c92010-05-18 14:32:29 +0200360struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200361{
Harald Welteeb471c92010-05-18 14:32:29 +0200362 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200363
364 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
365 if (id == ggc->id)
366 return ggc;
367 }
368 return NULL;
369}
370
Harald Welte94ecef32010-12-24 21:13:26 +0100371struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
372{
373 struct sgsn_ggsn_ctx *ggc;
374
375 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
376 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
377 return ggc;
378 }
379 return NULL;
380}
381
382
Harald Welteeb471c92010-05-18 14:32:29 +0200383struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200384{
Harald Welteeb471c92010-05-18 14:32:29 +0200385 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200386
Harald Welteeb471c92010-05-18 14:32:29 +0200387 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200388 if (!ggc)
Harald Welteeb471c92010-05-18 14:32:29 +0200389 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200390 return ggc;
391}
392
393/* APN contexts */
394
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100395static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200396{
397 struct apn_ctx *actx;
398
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100399 actx = talloc_zero(tall_bsc_ctx, struct apn_ctx);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200400 if (!actx)
401 return NULL;
402 actx->name = talloc_strdup(actx, ap_name);
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100403 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
404
405 llist_add_tail(&actx->list, &sgsn_apn_ctxts);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200406
407 return actx;
408}
409
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100410void sgsn_apn_ctx_free(struct apn_ctx *actx)
411{
412 llist_del(&actx->list);
413 talloc_free(actx);
414}
415
416struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
417{
418 struct apn_ctx *actx;
419 struct apn_ctx *found_actx = NULL;
420 size_t imsi_prio = 0;
421 size_t name_prio = 0;
422 size_t name_req_len = strlen(name);
423
424 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
425 size_t name_ref_len, imsi_ref_len;
426 const char *name_ref_start, *name_match_start;
427
428 imsi_ref_len = strlen(actx->imsi_prefix);
429 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
430 continue;
431
432 if (imsi_ref_len < imsi_prio)
433 continue;
434
435 /* IMSI matches */
436
437 name_ref_start = &actx->name[0];
438 if (name_ref_start[0] == '*') {
439 /* Suffix match */
440 name_ref_start += 1;
441 name_ref_len = strlen(name_ref_start);
442 if (name_ref_len > name_req_len)
443 continue;
444 } else {
445 name_ref_len = strlen(name_ref_start);
446 if (name_ref_len != name_req_len)
447 continue;
448 }
449
450 name_match_start = name + (name_req_len - name_ref_len);
451 if (strcasecmp(name_match_start, name_ref_start) != 0)
452 continue;
453
454 /* IMSI and name match */
455
456 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
457 /* Lower priority, skip */
458 continue;
459
460 imsi_prio = imsi_ref_len;
461 name_prio = name_ref_len;
462 found_actx = actx;
463 }
464 return found_actx;
465}
466
467struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200468{
469 struct apn_ctx *actx;
470
471 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100472 if (strcasecmp(name, actx->name) == 0 &&
473 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200474 return actx;
475 }
476 return NULL;
477}
478
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100479struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200480{
481 struct apn_ctx *actx;
482
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100483 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200484 if (!actx)
Jacob Erlbeck9b3ca642015-02-03 13:47:53 +0100485 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200486
487 return actx;
488}
Harald Welte64df8ed2010-05-18 17:04:55 +0200489
490uint32_t sgsn_alloc_ptmsi(void)
491{
492 struct sgsn_mm_ctx *mm;
493 uint32_t ptmsi;
Jacob Erlbecke3aeb1e2015-01-15 18:51:31 +0100494 int max_retries = 100;
Harald Welte64df8ed2010-05-18 17:04:55 +0200495
496restart:
Daniel Willmannd7f6cdf2015-10-12 19:36:33 +0200497 if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1)
498 goto failed;
499
Jacob Erlbecke3aeb1e2015-01-15 18:51:31 +0100500 /* Enforce that the 2 MSB are set without loosing the distance between
501 * identical values. Since rand() has no duplicate values within a
502 * period (because the size of the state is the same like the size of
503 * the random value), this leads to a distance of period/4 when the
504 * distribution of the 2 MSB is uniform. This approach fails with a
505 * probability of (3/4)^max_retries, only 1% of the approaches will
506 * need more than 16 numbers (even distribution assumed).
507 *
508 * Alternatively, a freeze list could be used if another PRNG is used
509 * or when this approach proves to be not sufficient.
510 */
511 if (ptmsi >= 0xC0000000) {
512 if (!max_retries--)
513 goto failed;
514 goto restart;
515 }
516 ptmsi |= 0xC0000000;
517
518 if (ptmsi == GSM_RESERVED_TMSI) {
519 if (!max_retries--)
520 goto failed;
521 goto restart;
522 }
523
Harald Welte64df8ed2010-05-18 17:04:55 +0200524 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200525 if (mm->p_tmsi == ptmsi) {
526 if (!max_retries--)
527 goto failed;
Harald Welte64df8ed2010-05-18 17:04:55 +0200528 goto restart;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200529 }
Harald Welte64df8ed2010-05-18 17:04:55 +0200530 }
531
532 return ptmsi;
Jacob Erlbeck788de3e2014-09-19 09:28:42 +0200533
534failed:
535 LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n");
536 return GSM_RESERVED_TMSI;
Harald Welte64df8ed2010-05-18 17:04:55 +0200537}
Harald Welte94ecef32010-12-24 21:13:26 +0100538
539static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
540{
541 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
542 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
543 else {
544 /* FIXME: GPRS paging in case MS is SUSPENDED */
Daniel Willmannc9ac9732014-09-03 17:46:44 +0200545 LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN "
Harald Welte94ecef32010-12-24 21:13:26 +0100546 "recovery\n");
Harald Welted8765b92012-07-14 12:04:04 +0200547 /* FIXME: how to tell this to libgtp? */
Harald Welte94ecef32010-12-24 21:13:26 +0100548 sgsn_pdp_ctx_free(pdp);
549 }
550}
551
552/* High-level function to be called in case a GGSN has disappeared or
Holger Hans Peter Freytheredcefd52014-10-27 10:24:37 +0100553 * otherwise lost state (recovery procedure) */
Harald Welte94ecef32010-12-24 21:13:26 +0100554int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
555{
556 struct sgsn_mm_ctx *mm;
557 int num = 0;
558
559 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
560 struct sgsn_pdp_ctx *pdp;
561 llist_for_each_entry(pdp, &mm->pdp_list, list) {
562 if (pdp->ggsn == ggsn) {
563 drop_one_pdp(pdp);
564 num++;
565 }
566 }
567 }
568
569 return num;
570}
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200571
572int sgsn_force_reattach_oldmsg(struct msgb *oldmsg)
573{
Jacob Erlbeck6e15b732014-10-31 12:20:49 +0100574 return gsm0408_gprs_force_reattach_oldmsg(oldmsg);
Jacob Erlbeckccaa22a2014-09-05 14:32:36 +0200575}
576
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100577void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx)
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200578{
Jacob Erlbeck428f1ec2015-01-26 13:52:42 +0100579 OSMO_ASSERT(mmctx != NULL);
Jacob Erlbeck0939c0f2014-12-18 09:53:07 +0100580 LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n");
Jacob Erlbeckd04f7cc2014-11-12 10:18:09 +0100581
Jacob Erlbeckb2acd742014-11-13 10:48:39 +0100582 sgsn_auth_update(mmctx);
Jacob Erlbeckc64af7a2014-10-24 18:09:54 +0200583}
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100584
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400585static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp)
586{
587 tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len;
588 tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed;
589}
590
591/**
592 * The tlv_parsed tp parameter will be modified to insert a
593 * OSMO_IE_GSM_SUB_QOS in case the data is available in the
594 * PDP context handling.
595 */
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100596struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
597 struct tlv_parsed *tp,
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800598 enum gsm48_gsm_cause *gsm_cause,
599 char *out_apn_str)
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100600{
601 char req_apn_str[GSM_APN_LENGTH] = {0};
602 const struct apn_ctx *apn_ctx = NULL;
603 const char *selected_apn_str = NULL;
604 struct sgsn_subscriber_pdp_data *pdp;
605 struct sgsn_ggsn_ctx *ggsn = NULL;
606 int allow_any_apn = 0;
607
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800608 out_apn_str[0] = '\0';
609
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100610 if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) {
611 if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) {
612 LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n");
613 *gsm_cause = GSM_CAUSE_INV_MAND_INFO;
614 return NULL;
615 }
616
617 gprs_apn_to_str(req_apn_str,
618 TLVP_VAL(tp, GSM48_IE_GSM_APN),
619 TLVP_LEN(tp, GSM48_IE_GSM_APN));
620
621 if (strcmp(req_apn_str, "*") == 0)
622 req_apn_str[0] = 0;
623 }
624
Holger Hans Peter Freytherab9422e2015-05-24 20:51:17 +0800625 if (mmctx->subscr == NULL)
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100626 allow_any_apn = 1;
627
628 if (strlen(req_apn_str) == 0 && !allow_any_apn) {
629 /* No specific APN requested, check for an APN that is both
630 * granted and configured */
631
632 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
633 if (strcmp(pdp->apn_str, "*") == 0)
634 {
635 allow_any_apn = 1;
636 selected_apn_str = "";
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400637 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100638 continue;
639 }
640 if (!llist_empty(&sgsn_apn_ctxts)) {
641 apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi);
642 /* Not configured */
643 if (apn_ctx == NULL)
644 continue;
645 }
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400646 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100647 selected_apn_str = pdp->apn_str;
648 break;
649 }
650 } else if (!allow_any_apn) {
651 /* Check whether the given APN is granted */
652 llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) {
653 if (strcmp(pdp->apn_str, "*") == 0) {
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400654 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100655 selected_apn_str = req_apn_str;
656 allow_any_apn = 1;
657 continue;
658 }
659 if (strcasecmp(pdp->apn_str, req_apn_str) == 0) {
Holger Hans Peter Freyther532b09d2015-04-23 11:33:35 -0400660 insert_qos(tp, pdp);
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100661 selected_apn_str = req_apn_str;
662 break;
663 }
664 }
665 } else if (strlen(req_apn_str) != 0) {
666 /* Any APN is allowed */
667 selected_apn_str = req_apn_str;
668 } else {
669 /* Prefer the GGSN associated with the wildcard APN */
670 selected_apn_str = "";
671 }
672
673 if (!allow_any_apn && selected_apn_str == NULL) {
674 /* Access not granted */
675 LOGMMCTXP(LOGL_NOTICE, mmctx,
676 "The requested APN '%s' is not allowed\n",
677 req_apn_str);
678 *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB;
679 return NULL;
680 }
681
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800682 /* copy the selected apn_str */
Holger Hans Peter Freyther3332d0c2015-06-02 09:33:31 +0200683 if (selected_apn_str)
684 strcpy(out_apn_str, selected_apn_str);
685 else
686 out_apn_str[0] = '\0';
Holger Hans Peter Freyther2b1d49a2015-05-25 12:26:49 +0800687
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100688 if (apn_ctx == NULL && selected_apn_str)
689 apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi);
690
691 if (apn_ctx != NULL) {
692 ggsn = apn_ctx->ggsn;
693 } else if (llist_empty(&sgsn_apn_ctxts)) {
694 /* No configuration -> use GGSN 0 */
695 ggsn = sgsn_ggsn_ctx_by_id(0);
696 } else if (allow_any_apn &&
697 (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) {
698 /* No APN given and no default configuration -> Use GGSN 0 */
699 ggsn = sgsn_ggsn_ctx_by_id(0);
700 } else {
701 /* No matching configuration found */
702 LOGMMCTXP(LOGL_NOTICE, mmctx,
703 "The selected APN '%s' has not been configured\n",
704 selected_apn_str);
705 *gsm_cause = GSM_CAUSE_MISSING_APN;
706 return NULL;
707 }
708
Holger Hans Peter Freythereaa58512015-05-25 14:35:10 +0800709 if (!ggsn) {
710 LOGMMCTXP(LOGL_NOTICE, mmctx,
711 "No static GGSN configured. Selected APN '%s'\n",
712 selected_apn_str);
713 return NULL;
714 }
715
Jacob Erlbeck3400f112015-02-02 18:03:05 +0100716 LOGMMCTXP(LOGL_INFO, mmctx,
717 "Found GGSN %d for APN '%s' (requested '%s')\n",
718 ggsn->id, selected_apn_str ? selected_apn_str : "---",
719 req_apn_str);
720
721 return ggsn;
722}
723
Jacob Erlbecke6140d02015-01-23 11:33:51 +0100724static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme)
725{
726 struct sgsn_mm_ctx *mmctx = NULL;
727
728 llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) {
729 if (llme == mmctx->llme) {
730 gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE);
731 return;
732 }
733 }
734
735 /* No MM context found */
736 LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n",
737 llme->tlli);
738 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
739}
740
741static void sgsn_llme_check_cb(void *data_)
742{
743 struct gprs_llc_llme *llme, *llme_tmp;
744 struct timespec now_tp;
745 time_t now, age;
746 time_t max_age = gprs_max_time_to_idle();
747
748 int rc;
749
750 rc = clock_gettime(CLOCK_MONOTONIC, &now_tp);
751 OSMO_ASSERT(rc >= 0);
752 now = now_tp.tv_sec;
753
754 LOGP(DGPRS, LOGL_DEBUG,
755 "Checking for inactive LLMEs, time = %u\n", (unsigned)now);
756
757 llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) {
758 if (llme->age_timestamp == GPRS_LLME_RESET_AGE)
759 llme->age_timestamp = now;
760
761 age = now - llme->age_timestamp;
762
763 if (age > max_age || age < 0) {
764 LOGP(DGPRS, LOGL_INFO,
765 "Inactivity timeout for TLLI 0x%08x, age %d\n",
766 llme->tlli, (int)age);
767 sgsn_llme_cleanup_free(llme);
768 }
769 }
770
771 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
772}
773
774void sgsn_inst_init()
775{
776 sgsn->llme_timer.cb = sgsn_llme_check_cb;
777 sgsn->llme_timer.data = NULL;
778
779 osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0);
780}
781