blob: 48a00b822cb4cbbbec13699af6436a8abc7c6a36 [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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welteeaa614c2010-05-02 11:26:34 +020023#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080024
25#include <osmocore/linuxlist.h>
26#include <osmocore/talloc.h>
27#include <osmocore/timer.h>
Harald Welte8acd88f2010-05-18 10:57:45 +020028#include <osmocore/rate_ctr.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080029#include <openbsc/gsm_subscriber.h>
Harald Weltecb991632010-04-26 19:18:54 +020030#include <openbsc/debug.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080031#include <openbsc/gprs_sgsn.h>
Harald Weltecb991632010-04-26 19:18:54 +020032#include <openbsc/gprs_ns.h>
33#include <openbsc/gprs_bssgp.h>
Harald Welteab1d5622010-05-18 19:58:38 +020034#include <openbsc/sgsn.h>
35
36extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080037
Harald Welted193cb32010-05-17 22:58:03 +020038LLIST_HEAD(sgsn_mm_ctxts);
39LLIST_HEAD(sgsn_ggsn_ctxts);
40LLIST_HEAD(sgsn_apn_ctxts);
41LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080042
Harald Welte8acd88f2010-05-18 10:57:45 +020043static const struct rate_ctr_desc mmctx_ctr_description[] = {
44 { "sign.packets.in", "Signalling Messages ( In)" },
45 { "sign.packets.out", "Signalling Messages (Out)" },
46 { "udata.packets.in", "User Data Messages ( In)" },
47 { "udata.packets.out", "User Data Messages (Out)" },
48 { "udata.bytes.in", "User Data Bytes ( In)" },
49 { "udata.bytes.out", "User Data Bytes (Out)" },
50 { "pdp_ctx_act", "PDP Context Activations " },
51 { "suspend", "SUSPEND Count " },
52 { "paging.ps", "Paging Packet Switched " },
53 { "paging.cs", "Paging Circuit Switched " },
54 { "ra_update", "Routing Area Update " },
55};
56
57static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
58 .group_name_prefix = "sgsn.mmctx",
59 .group_description = "SGSN MM Context Statistics",
60 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
61 .ctr_desc = mmctx_ctr_description,
62};
63
Harald Welteefbdee92010-06-10 00:20:12 +020064static const struct rate_ctr_desc pdpctx_ctr_description[] = {
65 { "udata.packets.in", "User Data Messages ( In)" },
66 { "udata.packets.out", "User Data Messages (Out)" },
67 { "udata.bytes.in", "User Data Bytes ( In)" },
68 { "udata.bytes.out", "User Data Bytes (Out)" },
69};
70
71static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
72 .group_name_prefix = "sgsn.pdpctx",
73 .group_description = "SGSN PDP Context Statistics",
74 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
75 .ctr_desc = pdpctx_ctr_description,
76};
77
Harald Welte9b455bf2010-03-14 15:45:01 +080078static int ra_id_equals(const struct gprs_ra_id *id1,
79 const struct gprs_ra_id *id2)
80{
81 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
82 id1->lac == id2->lac && id1->rac == id2->rac);
83}
84
Harald Weltef6bd3402010-12-23 23:34:43 +010085/* See 03.02 Chapter 2.6 */
86static inline uint32_t tlli_foreign(uint32_t tlli)
87{
88 return ((tlli | 0x80000000) & ~0x40000000);
89}
90
Harald Welte9b455bf2010-03-14 15:45:01 +080091/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020092struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080093 const struct gprs_ra_id *raid)
94{
95 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +020096 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +080097
98 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
99 if (tlli == ctx->tlli &&
100 ra_id_equals(raid, &ctx->ra))
101 return ctx;
102 }
Harald Welteab1d5622010-05-18 19:58:38 +0200103
104 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100105 switch (tlli_type) {
106 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200107 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200108 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
109 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200110 ctx->tlli = tlli;
111 return ctx;
112 }
113 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100114 break;
115 case TLLI_FOREIGN:
116 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
117 if (tlli == tlli_foreign(ctx->tlli) &&
118 ra_id_equals(raid, &ctx->ra))
119 return ctx;
120 }
121 break;
122 default:
123 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200124 }
125
Harald Welte9b455bf2010-03-14 15:45:01 +0800126 return NULL;
127}
128
Harald Welteeaa614c2010-05-02 11:26:34 +0200129struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800130{
131 struct sgsn_mm_ctx *ctx;
132
133 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200134 if (p_tmsi == ctx->p_tmsi ||
135 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800136 return ctx;
137 }
138 return NULL;
139}
140
141struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
142{
143 struct sgsn_mm_ctx *ctx;
144
145 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
146 if (!strcmp(imsi, ctx->imsi))
147 return ctx;
148 }
149 return NULL;
150
151}
152
153/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200154struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800155 const struct gprs_ra_id *raid)
156{
Harald Welte2720e732010-05-17 00:44:57 +0200157 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800158
Harald Welte2720e732010-05-17 00:44:57 +0200159 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800160 if (!ctx)
161 return NULL;
162
163 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
164 ctx->tlli = tlli;
165 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200166 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200167 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800168
169 llist_add(&ctx->list, &sgsn_mm_ctxts);
170
171 return ctx;
172}
Harald Welted193cb32010-05-17 22:58:03 +0200173
Harald Welte77289c22010-05-18 14:32:29 +0200174
Harald Welte96df6062010-06-03 06:37:26 +0200175/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200176struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
177 uint8_t nsapi)
178{
179 struct sgsn_pdp_ctx *pdp;
180
181 llist_for_each_entry(pdp, &mm->pdp_list, list) {
182 if (pdp->nsapi == nsapi)
183 return pdp;
184 }
185 return NULL;
186}
187
Harald Welte96df6062010-06-03 06:37:26 +0200188/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200189struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
190 uint8_t tid)
191{
192 struct sgsn_pdp_ctx *pdp;
193
194 llist_for_each_entry(pdp, &mm->pdp_list, list) {
195 if (pdp->ti == tid)
196 return pdp;
197 }
198 return NULL;
199}
200
Harald Welted193cb32010-05-17 22:58:03 +0200201struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
202 uint8_t nsapi)
203{
204 struct sgsn_pdp_ctx *pdp;
205
206 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
207 if (pdp)
208 return NULL;
209
210 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
211 if (!pdp)
212 return NULL;
213
214 pdp->mm = mm;
215 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200216 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200217 llist_add(&pdp->list, &mm->pdp_list);
218 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
219
220 return pdp;
221}
222
223void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
224{
Harald Welte376d5e52010-06-28 18:57:21 +0200225 rate_ctr_group_free(pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200226 llist_del(&pdp->list);
227 llist_del(&pdp->g_list);
228 talloc_free(pdp);
229}
230
231/* GGSN contexts */
232
Harald Welte77289c22010-05-18 14:32:29 +0200233struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200234{
Harald Welte77289c22010-05-18 14:32:29 +0200235 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200236
Harald Welte77289c22010-05-18 14:32:29 +0200237 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200238 if (!ggc)
239 return NULL;
240
241 ggc->id = id;
242 ggc->gtp_version = 1;
Harald Welteab1d5622010-05-18 19:58:38 +0200243 /* if we are called from config file parse, this gsn doesn't exist yet */
244 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200245 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200246
247 return ggc;
248}
249
Harald Welte77289c22010-05-18 14:32:29 +0200250struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200251{
Harald Welte77289c22010-05-18 14:32:29 +0200252 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200253
254 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
255 if (id == ggc->id)
256 return ggc;
257 }
258 return NULL;
259}
260
Harald Welte77289c22010-05-18 14:32:29 +0200261struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200262{
Harald Welte77289c22010-05-18 14:32:29 +0200263 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200264
Harald Welte77289c22010-05-18 14:32:29 +0200265 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200266 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200267 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200268 return ggc;
269}
270
271/* APN contexts */
272
273#if 0
274struct apn_ctx *apn_ctx_alloc(const char *ap_name)
275{
276 struct apn_ctx *actx;
277
278 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
279 if (!actx)
280 return NULL;
281 actx->name = talloc_strdup(actx, ap_name);
282
283 return actx;
284}
285
286struct apn_ctx *apn_ctx_by_name(const char *name)
287{
288 struct apn_ctx *actx;
289
290 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
291 if (!strcmp(name, actx->name))
292 return actx;
293 }
294 return NULL;
295}
296
297struct apn_ctx *apn_ctx_find_alloc(const char *name)
298{
299 struct apn_ctx *actx;
300
301 actx = apn_ctx_by_name(name);
302 if (!actx)
303 actx = apn_ctx_alloc(name);
304
305 return actx;
306}
307#endif
Harald Welte6463c072010-05-18 17:04:55 +0200308
309uint32_t sgsn_alloc_ptmsi(void)
310{
311 struct sgsn_mm_ctx *mm;
312 uint32_t ptmsi;
313
314restart:
315 ptmsi = rand();
316 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
317 if (mm->p_tmsi == ptmsi)
318 goto restart;
319 }
320
321 return ptmsi;
322}