blob: e3a04f343283733baa5125e17c57df18eae3f1df [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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <assert.h>
30
Harald Weltef4625b12010-02-20 16:24:02 +010031#include <osmocore/talloc.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020032#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020033#include <openbsc/debug.h>
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020034
35LLIST_HEAD(active_subscribers);
36void *tall_subscr_ctx;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020037
38/* for the gsm_subscriber.c */
39struct llist_head *subscr_bsc_active_subscriber(void)
40{
41 return &active_subscribers;
42}
43
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020044
Holger Hans Peter Freyther3ea77a52010-06-30 12:56:41 +080045char *subscr_name(struct gsm_subscriber *subscr)
46{
47 if (strlen(subscr->name))
48 return subscr->name;
49
50 return subscr->imsi;
51}
52
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020053struct gsm_subscriber *subscr_alloc(void)
54{
55 struct gsm_subscriber *s;
56
Holger Hans Peter Freythere4533482009-10-29 02:29:45 +010057 s = talloc_zero(tall_subscr_ctx, struct gsm_subscriber);
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020058 if (!s)
59 return NULL;
60
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020061 llist_add_tail(&s->entry, &active_subscribers);
62 s->use_count = 1;
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +020063 s->tmsi = GSM_RESERVED_TMSI;
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020064
65 INIT_LLIST_HEAD(&s->requests);
66
67 return s;
68}
69
70static void subscr_free(struct gsm_subscriber *subscr)
71{
72 llist_del(&subscr->entry);
73 talloc_free(subscr);
74}
75
76struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
77{
78 subscr->use_count++;
Harald Welte57fde552009-12-24 11:46:44 +010079 DEBUGP(DREF, "subscr %s usage increases usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020080 subscr->extension, subscr->use_count);
81 return subscr;
82}
83
84struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
85{
86 subscr->use_count--;
Harald Welte57fde552009-12-24 11:46:44 +010087 DEBUGP(DREF, "subscr %s usage decreased usage to: %d\n",
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020088 subscr->extension, subscr->use_count);
Holger Hans Peter Freyther0a7d4972010-12-22 12:34:39 +010089 if (subscr->use_count <= 0 && !subscr->net->keep_subscr)
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020090 subscr_free(subscr);
91 return NULL;
92}
93
Holger Hans Peter Freytherdcec9aa2010-11-15 09:15:01 +010094struct gsm_subscriber *subscr_get_or_create(struct gsm_network *net,
95 const char *imsi)
96{
97 struct gsm_subscriber *subscr;
98
99 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
100 if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net)
101 return subscr_get(subscr);
102 }
103
104 subscr = subscr_alloc();
105 if (!subscr)
106 return NULL;
107
108 strcpy(subscr->imsi, imsi);
109 subscr->net = net;
110 return subscr;
111}
Holger Hans Peter Freytherb0065af2010-11-15 13:32:33 +0100112
113struct gsm_subscriber *subscr_active_by_tmsi(struct gsm_network *net, uint32_t tmsi)
114{
115 struct gsm_subscriber *subscr;
116
117 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
118 if (subscr->tmsi == tmsi && subscr->net == net)
119 return subscr_get(subscr);
120 }
121
122 return NULL;
123}
124
125struct gsm_subscriber *subscr_active_by_imsi(struct gsm_network *net, const char *imsi)
126{
127 struct gsm_subscriber *subscr;
128
129 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
130 if (strcmp(subscr->imsi, imsi) == 0 && subscr->net == net)
131 return subscr_get(subscr);
132 }
133
134 return NULL;
135}