blob: caf84e7bb755746f7d3acf4447cb4a59a41e5ad2 [file] [log] [blame]
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +02001/* The concept of a subscriber as seen by the BSC */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther108d58a2010-06-30 09:22:31 +08004 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther51808442010-10-06 20:37:09 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +02006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020018 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020021 *
22 */
23
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <assert.h>
29
Harald Weltef4625b12010-02-20 16:24:02 +010030#include <osmocore/talloc.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020031#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020032#include <openbsc/debug.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020033
34LLIST_HEAD(active_subscribers);
35void *tall_subscr_ctx;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020036
37/* for the gsm_subscriber.c */
38struct llist_head *subscr_bsc_active_subscriber(void)
39{
40 return &active_subscribers;
41}
42
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020043
Holger Hans Peter Freyther3ea77a52010-06-30 12:56:41 +080044char *subscr_name(struct gsm_subscriber *subscr)
45{
46 if (strlen(subscr->name))
47 return subscr->name;
48
49 return subscr->imsi;
50}
51
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020052struct gsm_subscriber *subscr_alloc(void)
53{
54 struct gsm_subscriber *s;
55
Holger Hans Peter Freythere4533482009-10-29 02:29:45 +010056 s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020057 if (!s)
58 return NULL;
59
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020060 llist_add_tail(&s->entry, &active_subscribers);
61 s->use_count = 1;
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +020062 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020063
64 INIT_LLIST_HEAD(&s->requests);
65
66 return s;
67}
68
69static void subscr_free(struct gsm_subscriber *subscr)
70{
71 llist_del(&subscr->entry);
72 talloc_free(subscr);
73}
74
75struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
76{
77 subscr->use_count++;
Harald Welte57fde552009-12-24 11:46:44 +010078 DEBUGP(DREF, "subscr %s usage increases usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020079 subscr->extension, subscr->use_count);
80 return subscr;
81}
82
83struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
84{
85 subscr->use_count--;
Harald Welte57fde552009-12-24 11:46:44 +010086 DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020087 subscr->extension, subscr->use_count);
Holger Hans Peter Freyther0a7d4972010-12-22 12:34:39 +010088 if (subscr->use_count <= 0 && !subscr->net->keep_subscr)
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020089 subscr_free(subscr);
90 return NULL;
91}
92
Holger Hans Peter Freytherdcec9aa2010-11-15 09:15:01 +010093struct gsm_subscriber *subscr_get_or_create(struct gsm_network *net,
94 const char *imsi)
95{
96 struct gsm_subscriber *subscr;
97
98 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
99 if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net)
100 return subscr_get(subscr);
101 }
102
103 subscr = subscr_alloc();
104 if (!subscr)
105 return NULL;
106
107 strcpy(subscr->imsi, imsi);
108 subscr->net = net;
109 return subscr;
110}
Holger Hans Peter Freytherb0065af2010-11-15 13:32:33 +0100111
112struct gsm_subscriber *subscr_active_by_tmsi(struct gsm_network *net, uint32_t tmsi)
113{
114 struct gsm_subscriber *subscr;
115
116 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
117 if (subscr->tmsi == tmsi && subscr->net == net)
118 return subscr_get(subscr);
119 }
120
121 return NULL;
122}
123
124struct gsm_subscriber *subscr_active_by_imsi(struct gsm_network *net, const char *imsi)
125{
126 struct gsm_subscriber *subscr;
127
128 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
129 if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net)
130 return subscr_get(subscr);
131 }
132
133 return NULL;
134}
Holger Hans Peter Freytherec4fa9e2010-12-22 14:31:34 +0100135
136int subscr_purge_inactive(struct gsm_network *net)
137{
138 struct gsm_subscriber *subscr, *tmp;
139 int purged = 0;
140
141 llist_for_each_entry_safe(subscr, tmp, subscr_bsc_active_subscriber(), entry) {
142 if (subscr->net == net && subscr->use_count <= 0) {
143 subscr_free(subscr);
144 purged += 1;
145 }
146 }
147
148 return purged;
149}