blob: 297b8c8f6eeb35778df0c100de3ace34d1acb8cb [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 Welte (local)8751ee92009-08-15 02:30:58 +020039void *tall_subscr_ctx;
40void *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
Harald Welte68b7df22009-08-08 16:03:15 +0200166struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
167 unsigned long long id)
168{
169 struct gsm_subscriber *subscr;
170 char buf[32];
171 sprintf(buf, "%llu", id);
172
173 llist_for_each_entry(subscr, &active_subscribers, entry) {
174 if (subscr->id == id)
175 return subscr_get(subscr);
176 }
177
178 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
179}
180
181
Harald Welte59b04682009-06-10 05:40:52 +0800182int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
183{
184 /* FIXME: Migrate pending requests from one BSC to another */
185 switch (reason) {
186 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +0200187 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800188 /* Indicate "attached to LAC" */
189 s->lac = bts->location_area_code;
Harald Welteb7799832009-07-29 23:14:15 +0200190 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800191 break;
192 case GSM_SUBSCRIBER_UPDATE_DETACHED:
193 /* Only detach if we are currently in this area */
194 if (bts->location_area_code == s->lac)
195 s->lac = 0;
Harald Welteb7799832009-07-29 23:14:15 +0200196 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800197 break;
198 default:
199 fprintf(stderr, "subscr_update with unknown reason: %d\n",
200 reason);
201 break;
202 };
203 return db_sync_subscriber(s);
204}
205
206struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
207{
208 subscr->use_count++;
209 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
210 subscr->extension, subscr->use_count);
211 return subscr;
212}
213
214struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
215{
216 subscr->use_count--;
217 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
218 subscr->extension, subscr->use_count);
219 if (subscr->use_count <= 0)
220 subscr_free(subscr);
221 return NULL;
222}
223
Holger Freyther93795ac2009-03-31 04:35:19 +0200224void subscr_get_channel(struct gsm_subscriber *subscr,
225 struct gsm_network *network, int type,
226 gsm_cbfn *cbfn, void *param)
227{
Holger Freyther8bdf7622009-04-18 13:48:55 +0200228 struct subscr_request *request;
229
Harald Weltea8379772009-06-20 22:36:41 +0200230 request = talloc(tall_sub_req_ctx, struct subscr_request);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200231 if (!request) {
232 if (cbfn)
233 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
234 NULL, NULL, param);
235 return;
236 }
237
238 memset(request, 0, sizeof(*request));
239 request->network = network;
240 request->subscr = subscr;
241 request->channel_type = type;
242 request->cbfn = cbfn;
243 request->param = param;
244
245 /*
246 * FIXME: We might be able to assign more than one
247 * channel, e.g. voice and SMS submit at the same
248 * time.
249 */
250 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
251 /* add to the list, send a request */
252 llist_add_tail(&request->entry, &subscr->requests);
253 subscr_send_paging_request(subscr);
254 } else {
255 /* this will be picked up later, from subscr_put_channel */
256 llist_add_tail(&request->entry, &subscr->requests);
257 }
Holger Freyther93795ac2009-03-31 04:35:19 +0200258}
259
Harald Welte59b04682009-06-10 05:40:52 +0800260void subscr_put_channel(struct gsm_lchan *lchan)
261{
262 /*
263 * FIXME: Continue with other requests now... by checking
264 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
265 * of the lchan after having asked the next requestee to handle
266 * the channel.
267 */
Holger Freyther8bdf7622009-04-18 13:48:55 +0200268 /*
269 * FIXME: is the lchan is of a different type we could still
270 * issue an immediate assignment for another channel and then
271 * close this one.
272 */
273 /*
274 * Currently we will drop the last ref of the lchan which
275 * will result in a channel release on RSL and we will start
276 * the paging. This should work most of the time as the MS
277 * will listen to the paging requests before we timeout
278 */
279
Harald Welte59b04682009-06-10 05:40:52 +0800280 put_lchan(lchan);
Holger Freyther8bdf7622009-04-18 13:48:55 +0200281
282 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
283 subscr_send_paging_request(lchan->subscr);
Harald Welte59b04682009-06-10 05:40:52 +0800284}