blob: bab34d8d738f3c81f7d4a7eacf3fdce5e32caa92 [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) {
Harald Weltec2e8cc42010-05-31 20:23:38 +020087 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
88 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +020089 ctx->tlli = tlli;
90 return ctx;
91 }
92 }
93 }
94
Harald Welte9b455bf2010-03-14 15:45:01 +080095 return NULL;
96}
97
Harald Welteeaa614c2010-05-02 11:26:34 +020098struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +080099{
100 struct sgsn_mm_ctx *ctx;
101
102 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200103 if (p_tmsi == ctx->p_tmsi ||
104 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800105 return ctx;
106 }
107 return NULL;
108}
109
110struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
111{
112 struct sgsn_mm_ctx *ctx;
113
114 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
115 if (!strcmp(imsi, ctx->imsi))
116 return ctx;
117 }
118 return NULL;
119
120}
121
122/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200123struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800124 const struct gprs_ra_id *raid)
125{
Harald Welte2720e732010-05-17 00:44:57 +0200126 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800127
Harald Welte2720e732010-05-17 00:44:57 +0200128 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800129 if (!ctx)
130 return NULL;
131
132 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
133 ctx->tlli = tlli;
134 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200135 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200136 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800137
138 llist_add(&ctx->list, &sgsn_mm_ctxts);
139
140 return ctx;
141}
Harald Welted193cb32010-05-17 22:58:03 +0200142
Harald Welte77289c22010-05-18 14:32:29 +0200143
Harald Welted193cb32010-05-17 22:58:03 +0200144struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
145 uint8_t nsapi)
146{
147 struct sgsn_pdp_ctx *pdp;
148
149 llist_for_each_entry(pdp, &mm->pdp_list, list) {
150 if (pdp->nsapi == nsapi)
151 return pdp;
152 }
153 return NULL;
154}
155
Harald Welte77289c22010-05-18 14:32:29 +0200156struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
157 uint8_t tid)
158{
159 struct sgsn_pdp_ctx *pdp;
160
161 llist_for_each_entry(pdp, &mm->pdp_list, list) {
162 if (pdp->ti == tid)
163 return pdp;
164 }
165 return NULL;
166}
167
168
Harald Welted193cb32010-05-17 22:58:03 +0200169struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
170 uint8_t nsapi)
171{
172 struct sgsn_pdp_ctx *pdp;
173
174 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
175 if (pdp)
176 return NULL;
177
178 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
179 if (!pdp)
180 return NULL;
181
182 pdp->mm = mm;
183 pdp->nsapi = nsapi;
184 llist_add(&pdp->list, &mm->pdp_list);
185 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
186
187 return pdp;
188}
189
190void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
191{
192 llist_del(&pdp->list);
193 llist_del(&pdp->g_list);
194 talloc_free(pdp);
195}
196
197/* GGSN contexts */
198
Harald Welte77289c22010-05-18 14:32:29 +0200199struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200200{
Harald Welte77289c22010-05-18 14:32:29 +0200201 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200202
Harald Welte77289c22010-05-18 14:32:29 +0200203 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200204 if (!ggc)
205 return NULL;
206
207 ggc->id = id;
208 ggc->gtp_version = 1;
Harald Welteab1d5622010-05-18 19:58:38 +0200209 /* if we are called from config file parse, this gsn doesn't exist yet */
210 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200211 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200212
213 return ggc;
214}
215
Harald Welte77289c22010-05-18 14:32:29 +0200216struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200217{
Harald Welte77289c22010-05-18 14:32:29 +0200218 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200219
220 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
221 if (id == ggc->id)
222 return ggc;
223 }
224 return NULL;
225}
226
Harald Welte77289c22010-05-18 14:32:29 +0200227struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200228{
Harald Welte77289c22010-05-18 14:32:29 +0200229 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200230
Harald Welte77289c22010-05-18 14:32:29 +0200231 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200232 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200233 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200234 return ggc;
235}
236
237/* APN contexts */
238
239#if 0
240struct apn_ctx *apn_ctx_alloc(const char *ap_name)
241{
242 struct apn_ctx *actx;
243
244 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
245 if (!actx)
246 return NULL;
247 actx->name = talloc_strdup(actx, ap_name);
248
249 return actx;
250}
251
252struct apn_ctx *apn_ctx_by_name(const char *name)
253{
254 struct apn_ctx *actx;
255
256 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
257 if (!strcmp(name, actx->name))
258 return actx;
259 }
260 return NULL;
261}
262
263struct apn_ctx *apn_ctx_find_alloc(const char *name)
264{
265 struct apn_ctx *actx;
266
267 actx = apn_ctx_by_name(name);
268 if (!actx)
269 actx = apn_ctx_alloc(name);
270
271 return actx;
272}
273#endif
Harald Welte6463c072010-05-18 17:04:55 +0200274
275uint32_t sgsn_alloc_ptmsi(void)
276{
277 struct sgsn_mm_ctx *mm;
278 uint32_t ptmsi;
279
280restart:
281 ptmsi = rand();
282 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
283 if (mm->p_tmsi == ptmsi)
284 goto restart;
285 }
286
287 return ptmsi;
288}