blob: fe7bb6aca52a6aa3a0e33a9d62aa2b49495805b2 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte59b04682009-06-10 05:40:52 +080029
Harald Weltea8379772009-06-20 22:36:41 +020030#include <openbsc/talloc.h>
Harald Welte59b04682009-06-10 05:40:52 +080031#include <openbsc/gsm_subscriber.h>
Holger Freyther93795ac2009-03-31 04:35:19 +020032#include <openbsc/paging.h>
Harald Welte59b04682009-06-10 05:40:52 +080033#include <openbsc/debug.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020034#include <openbsc/paging.h>
Harald Welteb7799832009-07-29 23:14:15 +020035#include <openbsc/signal.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/db.h>
37
Harald Welte59b04682009-06-10 05:40:52 +080038LLIST_HEAD(active_subscribers);
Harald Weltea8379772009-06-20 22:36:41 +020039static void *tall_subscr_ctx;
40static void *tall_sub_req_ctx;
Harald Welte59b04682009-06-10 05:40:52 +080041
Holger Freyther8bdf7622009-04-18 13:48:55 +020042/*
43 * Struct for pending channel requests. This is managed in the
44 * llist_head requests of each subscriber. The reference counting
45 * should work in such a way that a subscriber with a pending request
46 * remains in memory.
47 */
48struct subscr_request {
49 struct llist_head entry;
50
51 /* back reference */
52 struct gsm_subscriber *subscr;
53
54 /* the requested channel type */
55 int channel_type;
56
57 /* the bts we have decided to use */
58 struct gsm_network *network;
59
60 /* the callback data */
61 gsm_cbfn *cbfn;
62 void *param;
63};
64
65/*
66 * We got the channel assigned and can now hand this channel
67 * over to one of our callbacks.
68 */
69static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
70 struct msgb *msg, void *data, void *param)
71{
72 struct subscr_request *request;
73 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
74
75 assert(!llist_empty(&subscr->requests));
76
77 /*
78 * FIXME: What to do with paging requests coming during
79 * this callback? We must be sure to not start paging when
80 * we have an active connection to a subscriber and to make
81 * the subscr_put_channel work as required...
82 */
83 request = (struct subscr_request *)subscr->requests.next;
84 llist_del(&request->entry);
85 subscr->in_callback = 1;
86 request->cbfn(hooknum, event, msg, data, request->param);
87 subscr->in_callback = 0;
88
Harald Weltea8379772009-06-20 22:36:41 +020089 talloc_free(request);
Holger Freyther8bdf7622009-04-18 13:48:55 +020090 return 0;
91}
92
93static void subscr_send_paging_request(struct gsm_subscriber *subscr)
94{
95 struct subscr_request *request;
96 assert(!llist_empty(&subscr->requests));
97
98 request = (struct subscr_request *)subscr->requests.next;
99 paging_request(request->network, subscr, request->channel_type,
100 subscr_paging_cb, subscr);
101}
102
Harald Welte59b04682009-06-10 05:40:52 +0800103struct gsm_subscriber *subscr_alloc(void)
104{
105 struct gsm_subscriber *s;
106
Harald Weltea8379772009-06-20 22:36:41 +0200107 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
Harald Welte59b04682009-06-10 05:40:52 +0800108 if (!s)
109 return NULL;
110
111 memset(s, 0, sizeof(*s));
112 llist_add_tail(&s->entry, &active_subscribers);
113 s->use_count = 1;
114
Holger Freyther93795ac2009-03-31 04:35:19 +0200115 INIT_LLIST_HEAD(&s->requests);
116
Harald Welte59b04682009-06-10 05:40:52 +0800117 return s;
118}
119
120static void subscr_free(struct gsm_subscriber *subscr)
121{
122 llist_del(&subscr->entry);
Harald Weltea8379772009-06-20 22:36:41 +0200123 talloc_free(subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800124}
125
Harald Welte75350412009-07-23 18:46:00 +0200126struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
127 const char *tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800128{
129 struct gsm_subscriber *subscr;
130
131 /* we might have a record in memory already */
132 llist_for_each_entry(subscr, &active_subscribers, entry) {
133 if (strcmp(subscr->tmsi, tmsi) == 0)
134 return subscr_get(subscr);
135 }
136
Harald Welte75350412009-07-23 18:46:00 +0200137 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800138}
139
Harald Welte75350412009-07-23 18:46:00 +0200140struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
141 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +0800142{
143 struct gsm_subscriber *subscr;
144
145 llist_for_each_entry(subscr, &active_subscribers, entry) {
146 if (strcmp(subscr->imsi, imsi) == 0)
147 return subscr_get(subscr);
148 }
149
Harald Welte75350412009-07-23 18:46:00 +0200150 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +0800151}
152
Harald Welte75350412009-07-23 18:46:00 +0200153struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
154 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +0800155{
156 struct gsm_subscriber *subscr;
157
158 llist_for_each_entry(subscr, &active_subscribers, entry) {
159 if (strcmp(subscr->extension, ext) == 0)
160 return subscr_get(subscr);
161 }
162
Harald Welte75350412009-07-23 18:46:00 +0200163 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +0800164}
165
166int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
167{
168 /* FIXME: Migrate pending requests from one BSC to another */
169 switch (reason) {
170 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +0200171 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800172 /* Indicate "attached to LAC" */
173 s->lac = bts->location_area_code;
Harald Welteb7799832009-07-29 23:14:15 +0200174 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800175 break;
176 case GSM_SUBSCRIBER_UPDATE_DETACHED:
177 /* Only detach if we are currently in this area */
178 if (bts->location_area_code == s->lac)
179 s->lac = 0;
Harald Welteb7799832009-07-29 23:14:15 +0200180 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800181 break;
182 default:
183 fprintf(stderr, "subscr_update with unknown reason: %d\n",
184 reason);
185 break;
186 };
187 return db_sync_subscriber(s);
188}
189
190struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
191{
192 subscr->use_count++;
193 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
194 subscr->extension, subscr->use_count);
195 return subscr;
196}
197
198struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
199{
200 subscr->use_count--;
201 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
202 subscr->extension, subscr->use_count);
203 if (subscr->use_count <= 0)
204 subscr_free(subscr);
205 return NULL;
206}
207
Holger Freyther93795ac2009-03-31 04:35:19 +0200208void subscr_get_channel(struct gsm_subscriber *subscr,
209 struct gsm_network *network, int type,
210 gsm_cbfn *cbfn, void *param)
211{
Holger Freyther8bdf7622009-04-18 13:48:55 +0200212 struct subscr_request *request;
213
Harald Weltea8379772009-06-20 22:36:41 +0200214 request = talloc(tall_sub_req_ctx, struct subscr_request);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200215 if (!request) {
216 if (cbfn)
217 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
218 NULL, NULL, param);
219 return;
220 }
221
222 memset(request, 0, sizeof(*request));
223 request->network = network;
224 request->subscr = subscr;
225 request->channel_type = type;
226 request->cbfn = cbfn;
227 request->param = param;
228
229 /*
230 * FIXME: We might be able to assign more than one
231 * channel, e.g. voice and SMS submit at the same
232 * time.
233 */
234 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
235 /* add to the list, send a request */
236 llist_add_tail(&request->entry, &subscr->requests);
237 subscr_send_paging_request(subscr);
238 } else {
239 /* this will be picked up later, from subscr_put_channel */
240 llist_add_tail(&request->entry, &subscr->requests);
241 }
Holger Freyther93795ac2009-03-31 04:35:19 +0200242}
243
Harald Welte59b04682009-06-10 05:40:52 +0800244void subscr_put_channel(struct gsm_lchan *lchan)
245{
246 /*
247 * FIXME: Continue with other requests now... by checking
248 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
249 * of the lchan after having asked the next requestee to handle
250 * the channel.
251 */
Holger Freyther8bdf7622009-04-18 13:48:55 +0200252 /*
253 * FIXME: is the lchan is of a different type we could still
254 * issue an immediate assignment for another channel and then
255 * close this one.
256 */
257 /*
258 * Currently we will drop the last ref of the lchan which
259 * will result in a channel release on RSL and we will start
260 * the paging. This should work most of the time as the MS
261 * will listen to the paging requests before we timeout
262 */
263
Harald Welte59b04682009-06-10 05:40:52 +0800264 put_lchan(lchan);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200265
266 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
267 subscr_send_paging_request(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800268}
Holger Freyther93795ac2009-03-31 04:35:19 +0200269
Harald Welte932e20d2009-07-28 00:41:45 +0200270
271static __attribute__((constructor)) void on_dso_load_subscr(void)
272{
273 tall_subscr_ctx = talloc_named_const(tall_bsc_ctx, 1, "subscriber");
274
275 tall_sub_req_ctx = talloc_named_const(tall_bsc_ctx, 1,
276 "subscr_request");
277}