blob: 443655418a66d789d42b1e6868d65c349578593f [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
24#include <osmocore/linuxlist.h>
25#include <osmocore/talloc.h>
26#include <osmocore/timer.h>
Harald Welte8acd88f2010-05-18 10:57:45 +020027#include <osmocore/rate_ctr.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080028#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020029#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080030#include <openbsc/gprs_sgsn.h>
Harald Weltecb991632010-04-26 19:18:54 +020031#include <openbsc/gprs_ns.h>
32#include <openbsc/gprs_bssgp.h>
Harald Welteab1d5622010-05-18 19:58:38 +020033#include <openbsc/sgsn.h>
Harald Weltea9b473a2010-12-24 21:13:26 +010034#include <openbsc/gsm_04_08_gprs.h>
35#include <openbsc/gprs_gmm.h>
Harald Welteab1d5622010-05-18 19:58:38 +020036
37extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080038
Harald Welted193cb32010-05-17 22:58:03 +020039LLIST_HEAD(sgsn_mm_ctxts);
40LLIST_HEAD(sgsn_ggsn_ctxts);
41LLIST_HEAD(sgsn_apn_ctxts);
42LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080043
Harald Welte8acd88f2010-05-18 10:57:45 +020044static const struct rate_ctr_desc mmctx_ctr_description[] = {
45 { "sign.packets.in", "Signalling Messages ( In)" },
46 { "sign.packets.out", "Signalling Messages (Out)" },
47 { "udata.packets.in", "User Data Messages ( In)" },
48 { "udata.packets.out", "User Data Messages (Out)" },
49 { "udata.bytes.in", "User Data Bytes ( In)" },
50 { "udata.bytes.out", "User Data Bytes (Out)" },
51 { "pdp_ctx_act", "PDP Context Activations " },
52 { "suspend", "SUSPEND Count " },
53 { "paging.ps", "Paging Packet Switched " },
54 { "paging.cs", "Paging Circuit Switched " },
55 { "ra_update", "Routing Area Update " },
56};
57
58static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
59 .group_name_prefix = "sgsn.mmctx",
60 .group_description = "SGSN MM Context Statistics",
61 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
62 .ctr_desc = mmctx_ctr_description,
63};
64
Harald Welteefbdee92010-06-10 00:20:12 +020065static const struct rate_ctr_desc pdpctx_ctr_description[] = {
66 { "udata.packets.in", "User Data Messages ( In)" },
67 { "udata.packets.out", "User Data Messages (Out)" },
68 { "udata.bytes.in", "User Data Bytes ( In)" },
69 { "udata.bytes.out", "User Data Bytes (Out)" },
70};
71
72static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
73 .group_name_prefix = "sgsn.pdpctx",
74 .group_description = "SGSN PDP Context Statistics",
75 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
76 .ctr_desc = pdpctx_ctr_description,
77};
78
Harald Welte9b455bf2010-03-14 15:45:01 +080079static int ra_id_equals(const struct gprs_ra_id *id1,
80 const struct gprs_ra_id *id2)
81{
82 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
83 id1->lac == id2->lac && id1->rac == id2->rac);
84}
85
Harald Weltef6bd3402010-12-23 23:34:43 +010086/* See 03.02 Chapter 2.6 */
87static inline uint32_t tlli_foreign(uint32_t tlli)
88{
89 return ((tlli | 0x80000000) & ~0x40000000);
90}
91
Harald Welte9b455bf2010-03-14 15:45:01 +080092/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020093struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080094 const struct gprs_ra_id *raid)
95{
96 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +020097 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +080098
99 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
100 if (tlli == ctx->tlli &&
101 ra_id_equals(raid, &ctx->ra))
102 return ctx;
103 }
Harald Welteab1d5622010-05-18 19:58:38 +0200104
105 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100106 switch (tlli_type) {
107 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200108 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200109 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
110 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200111 ctx->tlli = tlli;
112 return ctx;
113 }
114 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100115 break;
116 case TLLI_FOREIGN:
117 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
118 if (tlli == tlli_foreign(ctx->tlli) &&
119 ra_id_equals(raid, &ctx->ra))
120 return ctx;
121 }
122 break;
123 default:
124 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200125 }
126
Harald Welte9b455bf2010-03-14 15:45:01 +0800127 return NULL;
128}
129
Harald Welteeaa614c2010-05-02 11:26:34 +0200130struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800131{
132 struct sgsn_mm_ctx *ctx;
133
134 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200135 if (p_tmsi == ctx->p_tmsi ||
136 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800137 return ctx;
138 }
139 return NULL;
140}
141
142struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
143{
144 struct sgsn_mm_ctx *ctx;
145
146 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
147 if (!strcmp(imsi, ctx->imsi))
148 return ctx;
149 }
150 return NULL;
151
152}
153
154/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200155struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800156 const struct gprs_ra_id *raid)
157{
Harald Welte2720e732010-05-17 00:44:57 +0200158 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800159
Harald Welte2720e732010-05-17 00:44:57 +0200160 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800161 if (!ctx)
162 return NULL;
163
164 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
165 ctx->tlli = tlli;
166 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200167 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200168 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800169
170 llist_add(&ctx->list, &sgsn_mm_ctxts);
171
172 return ctx;
173}
Harald Welted193cb32010-05-17 22:58:03 +0200174
Harald Weltec728eea2010-12-24 23:07:18 +0100175void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm)
176{
177 struct sgsn_pdp_ctx *pdp, *pdp2;
178
179 /* Unlink from global list of MM contexts */
180 llist_del(&mm->list);
181
182 /* Free all PDP contexts */
183 llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list)
184 sgsn_pdp_ctx_free(pdp);
185
186 rate_ctr_group_free(mm->ctrg);
187
188 talloc_free(mm);
189}
Harald Welte77289c22010-05-18 14:32:29 +0200190
Harald Welte96df6062010-06-03 06:37:26 +0200191/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200192struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
193 uint8_t nsapi)
194{
195 struct sgsn_pdp_ctx *pdp;
196
197 llist_for_each_entry(pdp, &mm->pdp_list, list) {
198 if (pdp->nsapi == nsapi)
199 return pdp;
200 }
201 return NULL;
202}
203
Harald Welte96df6062010-06-03 06:37:26 +0200204/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200205struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
206 uint8_t tid)
207{
208 struct sgsn_pdp_ctx *pdp;
209
210 llist_for_each_entry(pdp, &mm->pdp_list, list) {
211 if (pdp->ti == tid)
212 return pdp;
213 }
214 return NULL;
215}
216
Harald Welted193cb32010-05-17 22:58:03 +0200217struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
218 uint8_t nsapi)
219{
220 struct sgsn_pdp_ctx *pdp;
221
222 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
223 if (pdp)
224 return NULL;
225
226 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
227 if (!pdp)
228 return NULL;
229
230 pdp->mm = mm;
231 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200232 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200233 llist_add(&pdp->list, &mm->pdp_list);
234 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
235
236 return pdp;
237}
238
239void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
240{
Harald Welte376d5e52010-06-28 18:57:21 +0200241 rate_ctr_group_free(pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200242 llist_del(&pdp->list);
243 llist_del(&pdp->g_list);
244 talloc_free(pdp);
245}
246
247/* GGSN contexts */
248
Harald Welte77289c22010-05-18 14:32:29 +0200249struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200250{
Harald Welte77289c22010-05-18 14:32:29 +0200251 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200252
Harald Welte77289c22010-05-18 14:32:29 +0200253 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200254 if (!ggc)
255 return NULL;
256
257 ggc->id = id;
258 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100259 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200260 /* if we are called from config file parse, this gsn doesn't exist yet */
261 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200262 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200263
264 return ggc;
265}
266
Harald Welte77289c22010-05-18 14:32:29 +0200267struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200268{
Harald Welte77289c22010-05-18 14:32:29 +0200269 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200270
271 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
272 if (id == ggc->id)
273 return ggc;
274 }
275 return NULL;
276}
277
Harald Weltea9b473a2010-12-24 21:13:26 +0100278struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
279{
280 struct sgsn_ggsn_ctx *ggc;
281
282 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
283 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
284 return ggc;
285 }
286 return NULL;
287}
288
289
Harald Welte77289c22010-05-18 14:32:29 +0200290struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200291{
Harald Welte77289c22010-05-18 14:32:29 +0200292 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200293
Harald Welte77289c22010-05-18 14:32:29 +0200294 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200295 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200296 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200297 return ggc;
298}
299
300/* APN contexts */
301
302#if 0
303struct apn_ctx *apn_ctx_alloc(const char *ap_name)
304{
305 struct apn_ctx *actx;
306
307 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
308 if (!actx)
309 return NULL;
310 actx->name = talloc_strdup(actx, ap_name);
311
312 return actx;
313}
314
315struct apn_ctx *apn_ctx_by_name(const char *name)
316{
317 struct apn_ctx *actx;
318
319 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
320 if (!strcmp(name, actx->name))
321 return actx;
322 }
323 return NULL;
324}
325
326struct apn_ctx *apn_ctx_find_alloc(const char *name)
327{
328 struct apn_ctx *actx;
329
330 actx = apn_ctx_by_name(name);
331 if (!actx)
332 actx = apn_ctx_alloc(name);
333
334 return actx;
335}
336#endif
Harald Welte6463c072010-05-18 17:04:55 +0200337
338uint32_t sgsn_alloc_ptmsi(void)
339{
340 struct sgsn_mm_ctx *mm;
341 uint32_t ptmsi;
342
343restart:
344 ptmsi = rand();
345 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
346 if (mm->p_tmsi == ptmsi)
347 goto restart;
348 }
349
350 return ptmsi;
351}
Harald Weltea9b473a2010-12-24 21:13:26 +0100352
353static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
354{
355 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
356 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
357 else {
358 /* FIXME: GPRS paging in case MS is SUSPENDED */
359 LOGP(DGPRS, LOGL_NOTICE, "Hard-dropping PDP ctx due to GGSN "
360 "recovery\n");
361 sgsn_pdp_ctx_free(pdp);
362 }
363}
364
365/* High-level function to be called in case a GGSN has disappeared or
366 * ottherwise lost state (recovery procedure) */
367int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
368{
369 struct sgsn_mm_ctx *mm;
370 int num = 0;
371
372 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
373 struct sgsn_pdp_ctx *pdp;
374 llist_for_each_entry(pdp, &mm->pdp_list, list) {
375 if (pdp->ggsn == ggsn) {
376 drop_one_pdp(pdp);
377 num++;
378 }
379 }
380 }
381
382 return num;
383}