blob: 0e87e62e055c3a782b9f8cfa42523a3ee87637db [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 Welte9b455bf2010-03-14 15:45:01 +080064static int ra_id_equals(const struct gprs_ra_id *id1,
65 const struct gprs_ra_id *id2)
66{
67 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
68 id1->lac == id2->lac && id1->rac == id2->rac);
69}
70
71/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020072struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080073 const struct gprs_ra_id *raid)
74{
75 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +020076 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +080077
78 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
79 if (tlli == ctx->tlli &&
80 ra_id_equals(raid, &ctx->ra))
81 return ctx;
82 }
Harald Welteab1d5622010-05-18 19:58:38 +020083
84 tlli_type = gprs_tlli_type(tlli);
85 if (tlli_type == TLLI_LOCAL) {
86 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
87 if ((ctx->p_tmsi | 0xC0000000) == tlli) {
88 ctx->tlli = tlli;
89 return ctx;
90 }
91 }
92 }
93
Harald Welte9b455bf2010-03-14 15:45:01 +080094 return NULL;
95}
96
Harald Welteeaa614c2010-05-02 11:26:34 +020097struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +080098{
99 struct sgsn_mm_ctx *ctx;
100
101 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
102 if (p_tmsi == ctx->p_tmsi)
103 return ctx;
104 }
105 return NULL;
106}
107
108struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
109{
110 struct sgsn_mm_ctx *ctx;
111
112 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
113 if (!strcmp(imsi, ctx->imsi))
114 return ctx;
115 }
116 return NULL;
117
118}
119
120/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200121struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800122 const struct gprs_ra_id *raid)
123{
Harald Welte2720e732010-05-17 00:44:57 +0200124 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800125
Harald Welte2720e732010-05-17 00:44:57 +0200126 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800127 if (!ctx)
128 return NULL;
129
130 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
131 ctx->tlli = tlli;
132 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200133 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200134 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800135
136 llist_add(&ctx->list, &sgsn_mm_ctxts);
137
138 return ctx;
139}
Harald Welted193cb32010-05-17 22:58:03 +0200140
Harald Welte77289c22010-05-18 14:32:29 +0200141
Harald Welted193cb32010-05-17 22:58:03 +0200142struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
143 uint8_t nsapi)
144{
145 struct sgsn_pdp_ctx *pdp;
146
147 llist_for_each_entry(pdp, &mm->pdp_list, list) {
148 if (pdp->nsapi == nsapi)
149 return pdp;
150 }
151 return NULL;
152}
153
Harald Welte77289c22010-05-18 14:32:29 +0200154struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
155 uint8_t tid)
156{
157 struct sgsn_pdp_ctx *pdp;
158
159 llist_for_each_entry(pdp, &mm->pdp_list, list) {
160 if (pdp->ti == tid)
161 return pdp;
162 }
163 return NULL;
164}
165
166
Harald Welted193cb32010-05-17 22:58:03 +0200167struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
168 uint8_t nsapi)
169{
170 struct sgsn_pdp_ctx *pdp;
171
172 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
173 if (pdp)
174 return NULL;
175
176 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
177 if (!pdp)
178 return NULL;
179
180 pdp->mm = mm;
181 pdp->nsapi = nsapi;
182 llist_add(&pdp->list, &mm->pdp_list);
183 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
184
185 return pdp;
186}
187
188void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
189{
190 llist_del(&pdp->list);
191 llist_del(&pdp->g_list);
192 talloc_free(pdp);
193}
194
195/* GGSN contexts */
196
Harald Welte77289c22010-05-18 14:32:29 +0200197struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200198{
Harald Welte77289c22010-05-18 14:32:29 +0200199 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200200
Harald Welte77289c22010-05-18 14:32:29 +0200201 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200202 if (!ggc)
203 return NULL;
204
205 ggc->id = id;
206 ggc->gtp_version = 1;
Harald Welteab1d5622010-05-18 19:58:38 +0200207 /* if we are called from config file parse, this gsn doesn't exist yet */
208 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200209 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200210
211 return ggc;
212}
213
Harald Welte77289c22010-05-18 14:32:29 +0200214struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200215{
Harald Welte77289c22010-05-18 14:32:29 +0200216 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200217
218 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
219 if (id == ggc->id)
220 return ggc;
221 }
222 return NULL;
223}
224
Harald Welte77289c22010-05-18 14:32:29 +0200225struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200226{
Harald Welte77289c22010-05-18 14:32:29 +0200227 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200228
Harald Welte77289c22010-05-18 14:32:29 +0200229 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200230 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200231 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200232 return ggc;
233}
234
235/* APN contexts */
236
237#if 0
238struct apn_ctx *apn_ctx_alloc(const char *ap_name)
239{
240 struct apn_ctx *actx;
241
242 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
243 if (!actx)
244 return NULL;
245 actx->name = talloc_strdup(actx, ap_name);
246
247 return actx;
248}
249
250struct apn_ctx *apn_ctx_by_name(const char *name)
251{
252 struct apn_ctx *actx;
253
254 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
255 if (!strcmp(name, actx->name))
256 return actx;
257 }
258 return NULL;
259}
260
261struct apn_ctx *apn_ctx_find_alloc(const char *name)
262{
263 struct apn_ctx *actx;
264
265 actx = apn_ctx_by_name(name);
266 if (!actx)
267 actx = apn_ctx_alloc(name);
268
269 return actx;
270}
271#endif
Harald Welte6463c072010-05-18 17:04:55 +0200272
273uint32_t sgsn_alloc_ptmsi(void)
274{
275 struct sgsn_mm_ctx *mm;
276 uint32_t ptmsi;
277
278restart:
279 ptmsi = rand();
280 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
281 if (mm->p_tmsi == ptmsi)
282 goto restart;
283 }
284
285 return ptmsi;
286}