blob: 38056f0e2f684ffab5b7816131a30a6fbde8e544 [file] [log] [blame]
Harald Welte75bb8202010-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 Welted85d9a92010-05-02 11:26:34 +020023#include <stdint.h>
Harald Welte75bb8202010-03-14 15:45:01 +080024
25#include <osmocore/linuxlist.h>
26#include <osmocore/talloc.h>
27#include <osmocore/timer.h>
Harald Welte8a035af2010-05-18 10:57:45 +020028#include <osmocore/rate_ctr.h>
Harald Welte75bb8202010-03-14 15:45:01 +080029#include <openbsc/gsm_subscriber.h>
Harald Weltef67a5f92010-04-26 19:18:54 +020030#include <openbsc/debug.h>
Harald Welte75bb8202010-03-14 15:45:01 +080031#include <openbsc/gprs_sgsn.h>
Harald Weltef67a5f92010-04-26 19:18:54 +020032#include <openbsc/gprs_ns.h>
33#include <openbsc/gprs_bssgp.h>
Harald Welte75bb8202010-03-14 15:45:01 +080034
Harald Weltec1f6bfe2010-05-17 22:58:03 +020035LLIST_HEAD(sgsn_mm_ctxts);
36LLIST_HEAD(sgsn_ggsn_ctxts);
37LLIST_HEAD(sgsn_apn_ctxts);
38LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte75bb8202010-03-14 15:45:01 +080039
Harald Welte8a035af2010-05-18 10:57:45 +020040static const struct rate_ctr_desc mmctx_ctr_description[] = {
41 { "sign.packets.in", "Signalling Messages ( In)" },
42 { "sign.packets.out", "Signalling Messages (Out)" },
43 { "udata.packets.in", "User Data Messages ( In)" },
44 { "udata.packets.out", "User Data Messages (Out)" },
45 { "udata.bytes.in", "User Data Bytes ( In)" },
46 { "udata.bytes.out", "User Data Bytes (Out)" },
47 { "pdp_ctx_act", "PDP Context Activations " },
48 { "suspend", "SUSPEND Count " },
49 { "paging.ps", "Paging Packet Switched " },
50 { "paging.cs", "Paging Circuit Switched " },
51 { "ra_update", "Routing Area Update " },
52};
53
54static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
55 .group_name_prefix = "sgsn.mmctx",
56 .group_description = "SGSN MM Context Statistics",
57 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
58 .ctr_desc = mmctx_ctr_description,
59};
60
Harald Welte75bb8202010-03-14 15:45:01 +080061static int ra_id_equals(const struct gprs_ra_id *id1,
62 const struct gprs_ra_id *id2)
63{
64 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
65 id1->lac == id2->lac && id1->rac == id2->rac);
66}
67
68/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welted85d9a92010-05-02 11:26:34 +020069struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte75bb8202010-03-14 15:45:01 +080070 const struct gprs_ra_id *raid)
71{
72 struct sgsn_mm_ctx *ctx;
73
74 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
75 if (tlli == ctx->tlli &&
76 ra_id_equals(raid, &ctx->ra))
77 return ctx;
78 }
79 return NULL;
80}
81
Harald Welted85d9a92010-05-02 11:26:34 +020082struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte75bb8202010-03-14 15:45:01 +080083{
84 struct sgsn_mm_ctx *ctx;
85
86 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
87 if (p_tmsi == ctx->p_tmsi)
88 return ctx;
89 }
90 return NULL;
91}
92
93struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
94{
95 struct sgsn_mm_ctx *ctx;
96
97 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
98 if (!strcmp(imsi, ctx->imsi))
99 return ctx;
100 }
101 return NULL;
102
103}
104
105/* Allocate a new SGSN MM context */
Harald Welted85d9a92010-05-02 11:26:34 +0200106struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte75bb8202010-03-14 15:45:01 +0800107 const struct gprs_ra_id *raid)
108{
Harald Welte8f77f192010-05-17 00:44:57 +0200109 struct sgsn_mm_ctx *ctx;
Harald Welte75bb8202010-03-14 15:45:01 +0800110
Harald Welte8f77f192010-05-17 00:44:57 +0200111 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte75bb8202010-03-14 15:45:01 +0800112 if (!ctx)
113 return NULL;
114
115 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
116 ctx->tlli = tlli;
117 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8a035af2010-05-18 10:57:45 +0200118 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welteded83ec2010-05-18 12:44:45 +0200119 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte75bb8202010-03-14 15:45:01 +0800120
121 llist_add(&ctx->list, &sgsn_mm_ctxts);
122
123 return ctx;
124}
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200125
126struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
127 uint8_t nsapi)
128{
129 struct sgsn_pdp_ctx *pdp;
130
131 llist_for_each_entry(pdp, &mm->pdp_list, list) {
132 if (pdp->nsapi == nsapi)
133 return pdp;
134 }
135 return NULL;
136}
137
138struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
139 uint8_t nsapi)
140{
141 struct sgsn_pdp_ctx *pdp;
142
143 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
144 if (pdp)
145 return NULL;
146
147 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
148 if (!pdp)
149 return NULL;
150
151 pdp->mm = mm;
152 pdp->nsapi = nsapi;
153 llist_add(&pdp->list, &mm->pdp_list);
154 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
155
156 return pdp;
157}
158
159void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
160{
161 llist_del(&pdp->list);
162 llist_del(&pdp->g_list);
163 talloc_free(pdp);
164}
165
166/* GGSN contexts */
167
168struct ggsn_ctx *ggsn_ctx_alloc(uint32_t id)
169{
170 struct ggsn_ctx *ggc;
171
172 ggc = talloc_zero(tall_bsc_ctx, struct ggsn_ctx);
173 if (!ggc)
174 return NULL;
175
176 ggc->id = id;
177 ggc->gtp_version = 1;
178
179 return ggc;
180}
181
182struct ggsn_ctx *ggsn_ctx_by_id(uint32_t id)
183{
184 struct ggsn_ctx *ggc;
185
186 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
187 if (id == ggc->id)
188 return ggc;
189 }
190 return NULL;
191}
192
193struct ggsn_ctx *ggsn_ctx_find_alloc(uint32_t id)
194{
195 struct ggsn_ctx *ggc;
196
197 ggc = ggsn_ctx_by_id(id);
198 if (!ggc)
199 ggc = ggsn_ctx_alloc(id);
200 return ggc;
201}
202
203/* APN contexts */
204
205#if 0
206struct apn_ctx *apn_ctx_alloc(const char *ap_name)
207{
208 struct apn_ctx *actx;
209
210 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
211 if (!actx)
212 return NULL;
213 actx->name = talloc_strdup(actx, ap_name);
214
215 return actx;
216}
217
218struct apn_ctx *apn_ctx_by_name(const char *name)
219{
220 struct apn_ctx *actx;
221
222 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
223 if (!strcmp(name, actx->name))
224 return actx;
225 }
226 return NULL;
227}
228
229struct apn_ctx *apn_ctx_find_alloc(const char *name)
230{
231 struct apn_ctx *actx;
232
233 actx = apn_ctx_by_name(name);
234 if (!actx)
235 actx = apn_ctx_alloc(name);
236
237 return actx;
238}
239#endif