blob: 84bf512ae77bed22e274a38064dc16d3a400df91 [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>
Harald Weltefdf453c2012-07-14 12:15:19 +020028#include <osmocom/core/backtrace.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080029#include <osmocom/gprs/gprs_ns.h>
30#include <osmocom/gprs/gprs_bssgp.h>
31
Harald Welte9b455bf2010-03-14 15:45:01 +080032#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020033#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080034#include <openbsc/gprs_sgsn.h>
Harald Welteab1d5622010-05-18 19:58:38 +020035#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010036#include <openbsc/gsm_04_08_gprs.h>
37#include <openbsc/gprs_gmm.h>
Harald Welteab1d5622010-05-18 19:58:38 +020038
39extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080040
Harald Welted193cb32010-05-17 22:58:03 +020041LLIST_HEAD(sgsn_mm_ctxts);
42LLIST_HEAD(sgsn_ggsn_ctxts);
43LLIST_HEAD(sgsn_apn_ctxts);
44LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080045
Harald Welte8acd88f2010-05-18 10:57:45 +020046static const struct rate_ctr_desc mmctx_ctr_description[] = {
47 { "sign.packets.in", "Signalling Messages ( In)" },
48 { "sign.packets.out", "Signalling Messages (Out)" },
49 { "udata.packets.in", "User Data Messages ( In)" },
50 { "udata.packets.out", "User Data Messages (Out)" },
51 { "udata.bytes.in", "User Data Bytes ( In)" },
52 { "udata.bytes.out", "User Data Bytes (Out)" },
53 { "pdp_ctx_act", "PDP Context Activations " },
54 { "suspend", "SUSPEND Count " },
55 { "paging.ps", "Paging Packet Switched " },
56 { "paging.cs", "Paging Circuit Switched " },
57 { "ra_update", "Routing Area Update " },
58};
59
60static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
61 .group_name_prefix = "sgsn.mmctx",
62 .group_description = "SGSN MM Context Statistics",
63 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
64 .ctr_desc = mmctx_ctr_description,
65};
66
Harald Welteefbdee92010-06-10 00:20:12 +020067static const struct rate_ctr_desc pdpctx_ctr_description[] = {
68 { "udata.packets.in", "User Data Messages ( In)" },
69 { "udata.packets.out", "User Data Messages (Out)" },
70 { "udata.bytes.in", "User Data Bytes ( In)" },
71 { "udata.bytes.out", "User Data Bytes (Out)" },
72};
73
74static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
75 .group_name_prefix = "sgsn.pdpctx",
76 .group_description = "SGSN PDP Context Statistics",
77 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
78 .ctr_desc = pdpctx_ctr_description,
79};
80
Harald Welte9b455bf2010-03-14 15:45:01 +080081static int ra_id_equals(const struct gprs_ra_id *id1,
82 const struct gprs_ra_id *id2)
83{
84 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
85 id1->lac == id2->lac && id1->rac == id2->rac);
86}
87
Harald Weltef6bd3402010-12-23 23:34:43 +010088/* See 03.02 Chapter 2.6 */
89static inline uint32_t tlli_foreign(uint32_t tlli)
90{
91 return ((tlli | 0x80000000) & ~0x40000000);
92}
93
Harald Welte9b455bf2010-03-14 15:45:01 +080094/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020095struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080096 const struct gprs_ra_id *raid)
97{
98 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +020099 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800100
101 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
102 if (tlli == ctx->tlli &&
103 ra_id_equals(raid, &ctx->ra))
104 return ctx;
105 }
Harald Welteab1d5622010-05-18 19:58:38 +0200106
107 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100108 switch (tlli_type) {
109 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200110 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200111 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
112 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200113 ctx->tlli = tlli;
114 return ctx;
115 }
116 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100117 break;
118 case TLLI_FOREIGN:
119 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
120 if (tlli == tlli_foreign(ctx->tlli) &&
121 ra_id_equals(raid, &ctx->ra))
122 return ctx;
123 }
124 break;
125 default:
126 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200127 }
128
Harald Welte9b455bf2010-03-14 15:45:01 +0800129 return NULL;
130}
131
Harald Welteeaa614c2010-05-02 11:26:34 +0200132struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800133{
134 struct sgsn_mm_ctx *ctx;
135
136 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200137 if (p_tmsi == ctx->p_tmsi ||
138 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800139 return ctx;
140 }
141 return NULL;
142}
143
144struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
145{
146 struct sgsn_mm_ctx *ctx;
147
148 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
149 if (!strcmp(imsi, ctx->imsi))
150 return ctx;
151 }
152 return NULL;
153
154}
155
156/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200157struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800158 const struct gprs_ra_id *raid)
159{
Harald Welte2720e732010-05-17 00:44:57 +0200160 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800161
Harald Welte2720e732010-05-17 00:44:57 +0200162 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800163 if (!ctx)
164 return NULL;
165
166 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
167 ctx->tlli = tlli;
168 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200169 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200170 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800171
172 llist_add(&ctx->list, &sgsn_mm_ctxts);
173
174 return ctx;
175}
Harald Welted193cb32010-05-17 22:58:03 +0200176
Harald Welte7b022ee2012-07-14 12:04:04 +0200177/* this is a hard _free_ function, it doesn't clean up the PDP contexts
178 * in libgtp! */
Harald Weltec728eea2010-12-24 23:07:18 +0100179void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
180{
181 struct sgsn_pdp_ctx *pdp, *pdp2;
182
183 /* Unlink from global list of MM contexts */
184 llist_del(&mm->list);
185
186 /* Free all PDP contexts */
187 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
188 sgsn_pdp_ctx_free(pdp);
189
190 rate_ctr_group_free(mm->ctrg);
191
192 talloc_free(mm);
193}
Harald Welte77289c22010-05-18 14:32:29 +0200194
Harald Welte96df6062010-06-03 06:37:26 +0200195/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200196struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
197 uint8_t nsapi)
198{
199 struct sgsn_pdp_ctx *pdp;
200
201 llist_for_each_entry(pdp, &mm->pdp_list, list) {
202 if (pdp->nsapi == nsapi)
203 return pdp;
204 }
205 return NULL;
206}
207
Harald Welte96df6062010-06-03 06:37:26 +0200208/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200209struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
210 uint8_t tid)
211{
212 struct sgsn_pdp_ctx *pdp;
213
214 llist_for_each_entry(pdp, &mm->pdp_list, list) {
215 if (pdp->ti == tid)
216 return pdp;
217 }
218 return NULL;
219}
220
Harald Welte7b022ee2012-07-14 12:04:04 +0200221/* you don't want to use this directly, call sgsn_create_pdp_ctx() */
Harald Welted193cb32010-05-17 22:58:03 +0200222struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
223 uint8_t nsapi)
224{
225 struct sgsn_pdp_ctx *pdp;
226
227 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
228 if (pdp)
229 return NULL;
230
231 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
232 if (!pdp)
233 return NULL;
234
235 pdp->mm = mm;
236 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200237 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200238 llist_add(&pdp->list, &mm->pdp_list);
239 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
240
241 return pdp;
242}
243
Harald Weltefdf453c2012-07-14 12:15:19 +0200244#include <pdp.h>
Harald Welte7b022ee2012-07-14 12:04:04 +0200245/* you probably want to call sgsn_delete_pdp_ctx() instead */
Harald Welted193cb32010-05-17 22:58:03 +0200246void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
247{
Harald Welte376d5e52010-06-28 18:57:21 +0200248 rate_ctr_group_free(pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200249 llist_del(&pdp->list);
250 llist_del(&pdp->g_list);
Harald Weltefdf453c2012-07-14 12:15:19 +0200251
252 /* _if_ we still have a library handle, at least set it to NULL
253 * to avoid any dereferences of the now-deleted PDP context from
254 * sgsn_libgtp:cb_data_ind() */
255 if (pdp->lib) {
256 struct pdp_t *lib = pdp->lib;
257 LOGP(DGPRS, LOGL_NOTICE, "freeing PDP context that still "
258 "has a libgtp handle attached to it, this shouldn't "
259 "happen!\n");
260 osmo_generate_backtrace();
261 lib->priv = NULL;
262 }
263
Harald Welted193cb32010-05-17 22:58:03 +0200264 talloc_free(pdp);
265}
266
267/* GGSN contexts */
268
Harald Welte77289c22010-05-18 14:32:29 +0200269struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200270{
Harald Welte77289c22010-05-18 14:32:29 +0200271 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200272
Harald Welte77289c22010-05-18 14:32:29 +0200273 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200274 if (!ggc)
275 return NULL;
276
277 ggc->id = id;
278 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100279 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200280 /* if we are called from config file parse, this gsn doesn't exist yet */
281 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200282 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200283
284 return ggc;
285}
286
Harald Welte77289c22010-05-18 14:32:29 +0200287struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200288{
Harald Welte77289c22010-05-18 14:32:29 +0200289 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200290
291 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
292 if (id == ggc->id)
293 return ggc;
294 }
295 return NULL;
296}
297
Harald Weltea9b473a2010-12-24 21:13:26 +0100298struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
299{
300 struct sgsn_ggsn_ctx *ggc;
301
302 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
303 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
304 return ggc;
305 }
306 return NULL;
307}
308
309
Harald Welte77289c22010-05-18 14:32:29 +0200310struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200311{
Harald Welte77289c22010-05-18 14:32:29 +0200312 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200313
Harald Welte77289c22010-05-18 14:32:29 +0200314 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200315 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200316 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200317 return ggc;
318}
319
320/* APN contexts */
321
322#if 0
323struct apn_ctx *apn_ctx_alloc(const char *ap_name)
324{
325 struct apn_ctx *actx;
326
327 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
328 if (!actx)
329 return NULL;
330 actx->name = talloc_strdup(actx, ap_name);
331
332 return actx;
333}
334
335struct apn_ctx *apn_ctx_by_name(const char *name)
336{
337 struct apn_ctx *actx;
338
339 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
340 if (!strcmp(name, actx->name))
341 return actx;
342 }
343 return NULL;
344}
345
346struct apn_ctx *apn_ctx_find_alloc(const char *name)
347{
348 struct apn_ctx *actx;
349
350 actx = apn_ctx_by_name(name);
351 if (!actx)
352 actx = apn_ctx_alloc(name);
353
354 return actx;
355}
356#endif
Harald Welte6463c072010-05-18 17:04:55 +0200357
358uint32_t sgsn_alloc_ptmsi(void)
359{
360 struct sgsn_mm_ctx *mm;
361 uint32_t ptmsi;
362
363restart:
364 ptmsi = rand();
365 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
366 if (mm->p_tmsi == ptmsi)
367 goto restart;
368 }
369
370 return ptmsi;
371}
Harald Weltea9b473a2010-12-24 21:13:26 +0100372
373static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
374{
375 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
376 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
377 else {
378 /* FIXME: GPRS paging in case MS is SUSPENDED */
379 LOGP(DGPRS, LOGL_NOTICE, "Hard-dropping PDP ctx due to GGSN "
380 "recovery\n");
Harald Welte7b022ee2012-07-14 12:04:04 +0200381 /* FIXME: how to tell this to libgtp? */
Harald Weltea9b473a2010-12-24 21:13:26 +0100382 sgsn_pdp_ctx_free(pdp);
383 }
384}
385
386/* High-level function to be called in case a GGSN has disappeared or
387 * ottherwise lost state (recovery procedure) */
388int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
389{
390 struct sgsn_mm_ctx *mm;
391 int num = 0;
392
393 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
394 struct sgsn_pdp_ctx *pdp;
395 llist_for_each_entry(pdp, &mm->pdp_list, list) {
396 if (pdp->ggsn == ggsn) {
397 drop_one_pdp(pdp);
398 num++;
399 }
400 }
401 }
402
403 return num;
404}