blob: ae18ea0fa7fe2657367f6f896b95d0b0c8eb25a5 [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
85/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020086struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080087 const struct gprs_ra_id *raid)
88{
89 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +020090 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +080091
92 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
93 if (tlli == ctx->tlli &&
94 ra_id_equals(raid, &ctx->ra))
95 return ctx;
96 }
Harald Welteab1d5622010-05-18 19:58:38 +020097
98 tlli_type = gprs_tlli_type(tlli);
99 if (tlli_type == TLLI_LOCAL) {
100 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200101 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
102 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200103 ctx->tlli = tlli;
104 return ctx;
105 }
106 }
107 }
108
Harald Welte9b455bf2010-03-14 15:45:01 +0800109 return NULL;
110}
111
Harald Welteeaa614c2010-05-02 11:26:34 +0200112struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800113{
114 struct sgsn_mm_ctx *ctx;
115
116 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200117 if (p_tmsi == ctx->p_tmsi ||
118 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800119 return ctx;
120 }
121 return NULL;
122}
123
124struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
125{
126 struct sgsn_mm_ctx *ctx;
127
128 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
129 if (!strcmp(imsi, ctx->imsi))
130 return ctx;
131 }
132 return NULL;
133
134}
135
136/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200137struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800138 const struct gprs_ra_id *raid)
139{
Harald Welte2720e732010-05-17 00:44:57 +0200140 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800141
Harald Welte2720e732010-05-17 00:44:57 +0200142 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800143 if (!ctx)
144 return NULL;
145
146 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
147 ctx->tlli = tlli;
148 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200149 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200150 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800151
152 llist_add(&ctx->list, &sgsn_mm_ctxts);
153
154 return ctx;
155}
Harald Welted193cb32010-05-17 22:58:03 +0200156
Harald Welte77289c22010-05-18 14:32:29 +0200157
Harald Welte96df6062010-06-03 06:37:26 +0200158/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200159struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
160 uint8_t nsapi)
161{
162 struct sgsn_pdp_ctx *pdp;
163
164 llist_for_each_entry(pdp, &mm->pdp_list, list) {
165 if (pdp->nsapi == nsapi)
166 return pdp;
167 }
168 return NULL;
169}
170
Harald Welte96df6062010-06-03 06:37:26 +0200171/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200172struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
173 uint8_t tid)
174{
175 struct sgsn_pdp_ctx *pdp;
176
177 llist_for_each_entry(pdp, &mm->pdp_list, list) {
178 if (pdp->ti == tid)
179 return pdp;
180 }
181 return NULL;
182}
183
Harald Welted193cb32010-05-17 22:58:03 +0200184struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
185 uint8_t nsapi)
186{
187 struct sgsn_pdp_ctx *pdp;
188
189 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
190 if (pdp)
191 return NULL;
192
193 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
194 if (!pdp)
195 return NULL;
196
197 pdp->mm = mm;
198 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200199 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200200 llist_add(&pdp->list, &mm->pdp_list);
201 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
202
203 return pdp;
204}
205
206void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
207{
Harald Welte376d5e52010-06-28 18:57:21 +0200208 rate_ctr_group_free(pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200209 llist_del(&pdp->list);
210 llist_del(&pdp->g_list);
211 talloc_free(pdp);
212}
213
214/* GGSN contexts */
215
Harald Welte77289c22010-05-18 14:32:29 +0200216struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(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
Harald Welte77289c22010-05-18 14:32:29 +0200220 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200221 if (!ggc)
222 return NULL;
223
224 ggc->id = id;
225 ggc->gtp_version = 1;
Harald Welteab1d5622010-05-18 19:58:38 +0200226 /* if we are called from config file parse, this gsn doesn't exist yet */
227 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200228 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200229
230 return ggc;
231}
232
Harald Welte77289c22010-05-18 14:32:29 +0200233struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(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
237 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
238 if (id == ggc->id)
239 return ggc;
240 }
241 return NULL;
242}
243
Harald Welte77289c22010-05-18 14:32:29 +0200244struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200245{
Harald Welte77289c22010-05-18 14:32:29 +0200246 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200247
Harald Welte77289c22010-05-18 14:32:29 +0200248 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200249 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200250 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200251 return ggc;
252}
253
254/* APN contexts */
255
256#if 0
257struct apn_ctx *apn_ctx_alloc(const char *ap_name)
258{
259 struct apn_ctx *actx;
260
261 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
262 if (!actx)
263 return NULL;
264 actx->name = talloc_strdup(actx, ap_name);
265
266 return actx;
267}
268
269struct apn_ctx *apn_ctx_by_name(const char *name)
270{
271 struct apn_ctx *actx;
272
273 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
274 if (!strcmp(name, actx->name))
275 return actx;
276 }
277 return NULL;
278}
279
280struct apn_ctx *apn_ctx_find_alloc(const char *name)
281{
282 struct apn_ctx *actx;
283
284 actx = apn_ctx_by_name(name);
285 if (!actx)
286 actx = apn_ctx_alloc(name);
287
288 return actx;
289}
290#endif
Harald Welte6463c072010-05-18 17:04:55 +0200291
292uint32_t sgsn_alloc_ptmsi(void)
293{
294 struct sgsn_mm_ctx *mm;
295 uint32_t ptmsi;
296
297restart:
298 ptmsi = rand();
299 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
300 if (mm->p_tmsi == ptmsi)
301 goto restart;
302 }
303
304 return ptmsi;
305}