blob: d104c4e8102f6784913651240c87fadc51389545 [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 Welte96df6062010-06-03 06:37:26 +0200144/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200145struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
146 uint8_t nsapi)
147{
148 struct sgsn_pdp_ctx *pdp;
149
150 llist_for_each_entry(pdp, &mm->pdp_list, list) {
151 if (pdp->nsapi == nsapi)
152 return pdp;
153 }
154 return NULL;
155}
156
Harald Welte96df6062010-06-03 06:37:26 +0200157/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200158struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
159 uint8_t tid)
160{
161 struct sgsn_pdp_ctx *pdp;
162
163 llist_for_each_entry(pdp, &mm->pdp_list, list) {
164 if (pdp->ti == tid)
165 return pdp;
166 }
167 return NULL;
168}
169
Harald Welted193cb32010-05-17 22:58:03 +0200170struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
171 uint8_t nsapi)
172{
173 struct sgsn_pdp_ctx *pdp;
174
175 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
176 if (pdp)
177 return NULL;
178
179 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
180 if (!pdp)
181 return NULL;
182
183 pdp->mm = mm;
184 pdp->nsapi = nsapi;
185 llist_add(&pdp->list, &mm->pdp_list);
186 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
187
188 return pdp;
189}
190
191void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
192{
193 llist_del(&pdp->list);
194 llist_del(&pdp->g_list);
195 talloc_free(pdp);
196}
197
198/* GGSN contexts */
199
Harald Welte77289c22010-05-18 14:32:29 +0200200struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200201{
Harald Welte77289c22010-05-18 14:32:29 +0200202 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200203
Harald Welte77289c22010-05-18 14:32:29 +0200204 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200205 if (!ggc)
206 return NULL;
207
208 ggc->id = id;
209 ggc->gtp_version = 1;
Harald Welteab1d5622010-05-18 19:58:38 +0200210 /* if we are called from config file parse, this gsn doesn't exist yet */
211 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200212 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200213
214 return ggc;
215}
216
Harald Welte77289c22010-05-18 14:32:29 +0200217struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200218{
Harald Welte77289c22010-05-18 14:32:29 +0200219 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200220
221 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
222 if (id == ggc->id)
223 return ggc;
224 }
225 return NULL;
226}
227
Harald Welte77289c22010-05-18 14:32:29 +0200228struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200229{
Harald Welte77289c22010-05-18 14:32:29 +0200230 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200231
Harald Welte77289c22010-05-18 14:32:29 +0200232 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200233 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200234 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200235 return ggc;
236}
237
238/* APN contexts */
239
240#if 0
241struct apn_ctx *apn_ctx_alloc(const char *ap_name)
242{
243 struct apn_ctx *actx;
244
245 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
246 if (!actx)
247 return NULL;
248 actx->name = talloc_strdup(actx, ap_name);
249
250 return actx;
251}
252
253struct apn_ctx *apn_ctx_by_name(const char *name)
254{
255 struct apn_ctx *actx;
256
257 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
258 if (!strcmp(name, actx->name))
259 return actx;
260 }
261 return NULL;
262}
263
264struct apn_ctx *apn_ctx_find_alloc(const char *name)
265{
266 struct apn_ctx *actx;
267
268 actx = apn_ctx_by_name(name);
269 if (!actx)
270 actx = apn_ctx_alloc(name);
271
272 return actx;
273}
274#endif
Harald Welte6463c072010-05-18 17:04:55 +0200275
276uint32_t sgsn_alloc_ptmsi(void)
277{
278 struct sgsn_mm_ctx *mm;
279 uint32_t ptmsi;
280
281restart:
282 ptmsi = rand();
283 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
284 if (mm->p_tmsi == ptmsi)
285 goto restart;
286 }
287
288 return ptmsi;
289}