blob: 2671796ab6c83955fb9a1936b7c2f737d91f5de0 [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
Harald Welteeb471c92010-05-18 14:32:29 +0200126
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200127struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
128 uint8_t nsapi)
129{
130 struct sgsn_pdp_ctx *pdp;
131
132 llist_for_each_entry(pdp, &mm->pdp_list, list) {
133 if (pdp->nsapi == nsapi)
134 return pdp;
135 }
136 return NULL;
137}
138
Harald Welteeb471c92010-05-18 14:32:29 +0200139struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
140 uint8_t tid)
141{
142 struct sgsn_pdp_ctx *pdp;
143
144 llist_for_each_entry(pdp, &mm->pdp_list, list) {
145 if (pdp->ti == tid)
146 return pdp;
147 }
148 return NULL;
149}
150
151
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200152struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
153 uint8_t nsapi)
154{
155 struct sgsn_pdp_ctx *pdp;
156
157 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
158 if (pdp)
159 return NULL;
160
161 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
162 if (!pdp)
163 return NULL;
164
165 pdp->mm = mm;
166 pdp->nsapi = nsapi;
167 llist_add(&pdp->list, &mm->pdp_list);
168 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
169
170 return pdp;
171}
172
173void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
174{
175 llist_del(&pdp->list);
176 llist_del(&pdp->g_list);
177 talloc_free(pdp);
178}
179
180/* GGSN contexts */
181
Harald Welteeb471c92010-05-18 14:32:29 +0200182struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200183{
Harald Welteeb471c92010-05-18 14:32:29 +0200184 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200185
Harald Welteeb471c92010-05-18 14:32:29 +0200186 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200187 if (!ggc)
188 return NULL;
189
190 ggc->id = id;
191 ggc->gtp_version = 1;
192
193 return ggc;
194}
195
Harald Welteeb471c92010-05-18 14:32:29 +0200196struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200197{
Harald Welteeb471c92010-05-18 14:32:29 +0200198 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200199
200 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
201 if (id == ggc->id)
202 return ggc;
203 }
204 return NULL;
205}
206
Harald Welteeb471c92010-05-18 14:32:29 +0200207struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200208{
Harald Welteeb471c92010-05-18 14:32:29 +0200209 struct sgsn_ggsn_ctx *ggc;
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200210
Harald Welteeb471c92010-05-18 14:32:29 +0200211 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200212 if (!ggc)
Harald Welteeb471c92010-05-18 14:32:29 +0200213 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Weltec1f6bfe2010-05-17 22:58:03 +0200214 return ggc;
215}
216
217/* APN contexts */
218
219#if 0
220struct apn_ctx *apn_ctx_alloc(const char *ap_name)
221{
222 struct apn_ctx *actx;
223
224 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
225 if (!actx)
226 return NULL;
227 actx->name = talloc_strdup(actx, ap_name);
228
229 return actx;
230}
231
232struct apn_ctx *apn_ctx_by_name(const char *name)
233{
234 struct apn_ctx *actx;
235
236 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
237 if (!strcmp(name, actx->name))
238 return actx;
239 }
240 return NULL;
241}
242
243struct apn_ctx *apn_ctx_find_alloc(const char *name)
244{
245 struct apn_ctx *actx;
246
247 actx = apn_ctx_by_name(name);
248 if (!actx)
249 actx = apn_ctx_alloc(name);
250
251 return actx;
252}
253#endif