blob: f5580037ef2d2baa5c90b6c7e75626669eedec41 [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>
Harald Weltea9b473a2010-12-24 21:13:26 +010035#include <openbsc/gsm_04_08_gprs.h>
36#include <openbsc/gprs_gmm.h>
Harald Welteab1d5622010-05-18 19:58:38 +020037
38extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080039
Harald Welted193cb32010-05-17 22:58:03 +020040LLIST_HEAD(sgsn_mm_ctxts);
41LLIST_HEAD(sgsn_ggsn_ctxts);
42LLIST_HEAD(sgsn_apn_ctxts);
43LLIST_HEAD(sgsn_pdp_ctxts);
Harald Welte9b455bf2010-03-14 15:45:01 +080044
Harald Welte8acd88f2010-05-18 10:57:45 +020045static const struct rate_ctr_desc mmctx_ctr_description[] = {
46 { "sign.packets.in", "Signalling Messages ( In)" },
47 { "sign.packets.out", "Signalling Messages (Out)" },
48 { "udata.packets.in", "User Data Messages ( In)" },
49 { "udata.packets.out", "User Data Messages (Out)" },
50 { "udata.bytes.in", "User Data Bytes ( In)" },
51 { "udata.bytes.out", "User Data Bytes (Out)" },
52 { "pdp_ctx_act", "PDP Context Activations " },
53 { "suspend", "SUSPEND Count " },
54 { "paging.ps", "Paging Packet Switched " },
55 { "paging.cs", "Paging Circuit Switched " },
56 { "ra_update", "Routing Area Update " },
57};
58
59static const struct rate_ctr_group_desc mmctx_ctrg_desc = {
60 .group_name_prefix = "sgsn.mmctx",
61 .group_description = "SGSN MM Context Statistics",
62 .num_ctr = ARRAY_SIZE(mmctx_ctr_description),
63 .ctr_desc = mmctx_ctr_description,
64};
65
Harald Welteefbdee92010-06-10 00:20:12 +020066static const struct rate_ctr_desc pdpctx_ctr_description[] = {
67 { "udata.packets.in", "User Data Messages ( In)" },
68 { "udata.packets.out", "User Data Messages (Out)" },
69 { "udata.bytes.in", "User Data Bytes ( In)" },
70 { "udata.bytes.out", "User Data Bytes (Out)" },
71};
72
73static const struct rate_ctr_group_desc pdpctx_ctrg_desc = {
74 .group_name_prefix = "sgsn.pdpctx",
75 .group_description = "SGSN PDP Context Statistics",
76 .num_ctr = ARRAY_SIZE(pdpctx_ctr_description),
77 .ctr_desc = pdpctx_ctr_description,
78};
79
Harald Welte9b455bf2010-03-14 15:45:01 +080080static int ra_id_equals(const struct gprs_ra_id *id1,
81 const struct gprs_ra_id *id2)
82{
83 return (id1->mcc == id2->mcc && id1->mnc == id2->mnc &&
84 id1->lac == id2->lac && id1->rac == id2->rac);
85}
86
Harald Weltef6bd3402010-12-23 23:34:43 +010087/* See 03.02 Chapter 2.6 */
88static inline uint32_t tlli_foreign(uint32_t tlli)
89{
90 return ((tlli | 0x80000000) & ~0x40000000);
91}
92
Harald Welte9b455bf2010-03-14 15:45:01 +080093/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +020094struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +080095 const struct gprs_ra_id *raid)
96{
97 struct sgsn_mm_ctx *ctx;
Harald Welteab1d5622010-05-18 19:58:38 +020098 int tlli_type;
Harald Welte9b455bf2010-03-14 15:45:01 +080099
100 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
101 if (tlli == ctx->tlli &&
102 ra_id_equals(raid, &ctx->ra))
103 return ctx;
104 }
Harald Welteab1d5622010-05-18 19:58:38 +0200105
106 tlli_type = gprs_tlli_type(tlli);
Harald Weltef6bd3402010-12-23 23:34:43 +0100107 switch (tlli_type) {
108 case TLLI_LOCAL:
Harald Welteab1d5622010-05-18 19:58:38 +0200109 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200110 if ((ctx->p_tmsi | 0xC0000000) == tlli ||
111 (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) {
Harald Welteab1d5622010-05-18 19:58:38 +0200112 ctx->tlli = tlli;
113 return ctx;
114 }
115 }
Harald Weltef6bd3402010-12-23 23:34:43 +0100116 break;
117 case TLLI_FOREIGN:
118 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
119 if (tlli == tlli_foreign(ctx->tlli) &&
120 ra_id_equals(raid, &ctx->ra))
121 return ctx;
122 }
123 break;
124 default:
125 break;
Harald Welteab1d5622010-05-18 19:58:38 +0200126 }
127
Harald Welte9b455bf2010-03-14 15:45:01 +0800128 return NULL;
129}
130
Harald Welteeaa614c2010-05-02 11:26:34 +0200131struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi)
Harald Welte9b455bf2010-03-14 15:45:01 +0800132{
133 struct sgsn_mm_ctx *ctx;
134
135 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200136 if (p_tmsi == ctx->p_tmsi ||
137 (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi))
Harald Welte9b455bf2010-03-14 15:45:01 +0800138 return ctx;
139 }
140 return NULL;
141}
142
143struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi)
144{
145 struct sgsn_mm_ctx *ctx;
146
147 llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) {
148 if (!strcmp(imsi, ctx->imsi))
149 return ctx;
150 }
151 return NULL;
152
153}
154
155/* Allocate a new SGSN MM context */
Harald Welteeaa614c2010-05-02 11:26:34 +0200156struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800157 const struct gprs_ra_id *raid)
158{
Harald Welte2720e732010-05-17 00:44:57 +0200159 struct sgsn_mm_ctx *ctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800160
Harald Welte2720e732010-05-17 00:44:57 +0200161 ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800162 if (!ctx)
163 return NULL;
164
165 memcpy(&ctx->ra, raid, sizeof(ctx->ra));
166 ctx->tlli = tlli;
167 ctx->mm_state = GMM_DEREGISTERED;
Harald Welte8acd88f2010-05-18 10:57:45 +0200168 ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli);
Harald Welte6ffbaab2010-05-18 12:44:45 +0200169 INIT_LLIST_HEAD(&ctx->pdp_list);
Harald Welte9b455bf2010-03-14 15:45:01 +0800170
171 llist_add(&ctx->list, &sgsn_mm_ctxts);
172
173 return ctx;
174}
Harald Welted193cb32010-05-17 22:58:03 +0200175
Harald Welte77289c22010-05-18 14:32:29 +0200176
Harald Welte96df6062010-06-03 06:37:26 +0200177/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200178struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
179 uint8_t nsapi)
180{
181 struct sgsn_pdp_ctx *pdp;
182
183 llist_for_each_entry(pdp, &mm->pdp_list, list) {
184 if (pdp->nsapi == nsapi)
185 return pdp;
186 }
187 return NULL;
188}
189
Harald Welte96df6062010-06-03 06:37:26 +0200190/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200191struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
192 uint8_t tid)
193{
194 struct sgsn_pdp_ctx *pdp;
195
196 llist_for_each_entry(pdp, &mm->pdp_list, list) {
197 if (pdp->ti == tid)
198 return pdp;
199 }
200 return NULL;
201}
202
Harald Welted193cb32010-05-17 22:58:03 +0200203struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm,
204 uint8_t nsapi)
205{
206 struct sgsn_pdp_ctx *pdp;
207
208 pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi);
209 if (pdp)
210 return NULL;
211
212 pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx);
213 if (!pdp)
214 return NULL;
215
216 pdp->mm = mm;
217 pdp->nsapi = nsapi;
Harald Welteefbdee92010-06-10 00:20:12 +0200218 pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi);
Harald Welted193cb32010-05-17 22:58:03 +0200219 llist_add(&pdp->list, &mm->pdp_list);
220 llist_add(&pdp->g_list, &sgsn_pdp_ctxts);
221
222 return pdp;
223}
224
225void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp)
226{
Harald Welte376d5e52010-06-28 18:57:21 +0200227 rate_ctr_group_free(pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200228 llist_del(&pdp->list);
229 llist_del(&pdp->g_list);
230 talloc_free(pdp);
231}
232
233/* GGSN contexts */
234
Harald Welte77289c22010-05-18 14:32:29 +0200235struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200236{
Harald Welte77289c22010-05-18 14:32:29 +0200237 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200238
Harald Welte77289c22010-05-18 14:32:29 +0200239 ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx);
Harald Welted193cb32010-05-17 22:58:03 +0200240 if (!ggc)
241 return NULL;
242
243 ggc->id = id;
244 ggc->gtp_version = 1;
Harald Weltea9b473a2010-12-24 21:13:26 +0100245 ggc->remote_restart_ctr = -1;
Harald Welteab1d5622010-05-18 19:58:38 +0200246 /* if we are called from config file parse, this gsn doesn't exist yet */
247 ggc->gsn = sgsn->gsn;
Harald Welte119c2ba2010-05-18 18:39:00 +0200248 llist_add(&ggc->list, &sgsn_ggsn_ctxts);
Harald Welted193cb32010-05-17 22:58:03 +0200249
250 return ggc;
251}
252
Harald Welte77289c22010-05-18 14:32:29 +0200253struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200254{
Harald Welte77289c22010-05-18 14:32:29 +0200255 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200256
257 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
258 if (id == ggc->id)
259 return ggc;
260 }
261 return NULL;
262}
263
Harald Weltea9b473a2010-12-24 21:13:26 +0100264struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr)
265{
266 struct sgsn_ggsn_ctx *ggc;
267
268 llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) {
269 if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr)))
270 return ggc;
271 }
272 return NULL;
273}
274
275
Harald Welte77289c22010-05-18 14:32:29 +0200276struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id)
Harald Welted193cb32010-05-17 22:58:03 +0200277{
Harald Welte77289c22010-05-18 14:32:29 +0200278 struct sgsn_ggsn_ctx *ggc;
Harald Welted193cb32010-05-17 22:58:03 +0200279
Harald Welte77289c22010-05-18 14:32:29 +0200280 ggc = sgsn_ggsn_ctx_by_id(id);
Harald Welted193cb32010-05-17 22:58:03 +0200281 if (!ggc)
Harald Welte77289c22010-05-18 14:32:29 +0200282 ggc = sgsn_ggsn_ctx_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200283 return ggc;
284}
285
286/* APN contexts */
287
288#if 0
289struct apn_ctx *apn_ctx_alloc(const char *ap_name)
290{
291 struct apn_ctx *actx;
292
293 actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx);
294 if (!actx)
295 return NULL;
296 actx->name = talloc_strdup(actx, ap_name);
297
298 return actx;
299}
300
301struct apn_ctx *apn_ctx_by_name(const char *name)
302{
303 struct apn_ctx *actx;
304
305 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
306 if (!strcmp(name, actx->name))
307 return actx;
308 }
309 return NULL;
310}
311
312struct apn_ctx *apn_ctx_find_alloc(const char *name)
313{
314 struct apn_ctx *actx;
315
316 actx = apn_ctx_by_name(name);
317 if (!actx)
318 actx = apn_ctx_alloc(name);
319
320 return actx;
321}
322#endif
Harald Welte6463c072010-05-18 17:04:55 +0200323
324uint32_t sgsn_alloc_ptmsi(void)
325{
326 struct sgsn_mm_ctx *mm;
327 uint32_t ptmsi;
328
329restart:
330 ptmsi = rand();
331 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
332 if (mm->p_tmsi == ptmsi)
333 goto restart;
334 }
335
336 return ptmsi;
337}
Harald Weltea9b473a2010-12-24 21:13:26 +0100338
339static void drop_one_pdp(struct sgsn_pdp_ctx *pdp)
340{
341 if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL)
342 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
343 else {
344 /* FIXME: GPRS paging in case MS is SUSPENDED */
345 LOGP(DGPRS, LOGL_NOTICE, "Hard-dropping PDP ctx due to GGSN "
346 "recovery\n");
347 sgsn_pdp_ctx_free(pdp);
348 }
349}
350
351/* High-level function to be called in case a GGSN has disappeared or
352 * ottherwise lost state (recovery procedure) */
353int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn)
354{
355 struct sgsn_mm_ctx *mm;
356 int num = 0;
357
358 llist_for_each_entry(mm, &sgsn_mm_ctxts, list) {
359 struct sgsn_pdp_ctx *pdp;
360 llist_for_each_entry(pdp, &mm->pdp_list, list) {
361 if (pdp->ggsn == ggsn) {
362 drop_one_pdp(pdp);
363 num++;
364 }
365 }
366 }
367
368 return num;
369}