blob: d91298ee64d8dec8921eb6dcd6b90af36005ebc9 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* Dummy implementation of a subscriber database, roghly HLR/VLR functionality */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00005 *
Harald Welte52b1f982008-12-23 20:25:15 +00006 * 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
Harald Welte75a983f2008-12-27 21:34:06 +000024#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Harald Welte2cf161b2009-06-20 22:36:41 +020030#include <openbsc/talloc.h>
Harald Welte8470bf22008-12-25 23:28:35 +000031#include <openbsc/gsm_subscriber.h>
Holger Freyther04866d42009-03-31 04:35:19 +020032#include <openbsc/paging.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000033#include <openbsc/debug.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020034#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020035#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000036#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000037
Holger Freyther12aa50d2009-01-01 18:02:05 +000038LLIST_HEAD(active_subscribers);
Harald Welte2cf161b2009-06-20 22:36:41 +020039static void *tall_subscr_ctx;
40static void *tall_sub_req_ctx;
Holger Freyther12aa50d2009-01-01 18:02:05 +000041
Holger Freyther85a7b362009-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 Welte2cf161b2009-06-20 22:36:41 +020089 talloc_free(request);
Holger Freyther85a7b362009-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 Welte75a983f2008-12-27 21:34:06 +0000103struct gsm_subscriber *subscr_alloc(void)
Harald Welte52b1f982008-12-23 20:25:15 +0000104{
Harald Welte75a983f2008-12-27 21:34:06 +0000105 struct gsm_subscriber *s;
106
Harald Welte2cf161b2009-06-20 22:36:41 +0200107 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
Harald Welte75a983f2008-12-27 21:34:06 +0000108 if (!s)
109 return NULL;
110
111 memset(s, 0, sizeof(*s));
Holger Freyther12aa50d2009-01-01 18:02:05 +0000112 llist_add_tail(&s->entry, &active_subscribers);
113 s->use_count = 1;
Harald Welte75a983f2008-12-27 21:34:06 +0000114
Holger Freyther04866d42009-03-31 04:35:19 +0200115 INIT_LLIST_HEAD(&s->requests);
116
Harald Welte75a983f2008-12-27 21:34:06 +0000117 return s;
Harald Welte52b1f982008-12-23 20:25:15 +0000118}
Harald Welte75a983f2008-12-27 21:34:06 +0000119
Holger Freyther12aa50d2009-01-01 18:02:05 +0000120static void subscr_free(struct gsm_subscriber *subscr)
Harald Welte52b1f982008-12-23 20:25:15 +0000121{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000122 llist_del(&subscr->entry);
Harald Welte2cf161b2009-06-20 22:36:41 +0200123 talloc_free(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000124}
125
Harald Welte9176bd42009-07-23 18:46:00 +0200126struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
127 const char *tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000128{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000129 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000130
Holger Freyther12aa50d2009-01-01 18:02:05 +0000131 /* 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);
Harald Welte75a983f2008-12-27 21:34:06 +0000135 }
136
Harald Welte9176bd42009-07-23 18:46:00 +0200137 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte75a983f2008-12-27 21:34:06 +0000138}
139
Harald Welte9176bd42009-07-23 18:46:00 +0200140struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
141 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000142{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000143 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000144
Holger Freyther12aa50d2009-01-01 18:02:05 +0000145 llist_for_each_entry(subscr, &active_subscribers, entry) {
146 if (strcmp(subscr->imsi, imsi) == 0)
147 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000148 }
149
Harald Welte9176bd42009-07-23 18:46:00 +0200150 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000151}
152
Harald Welte9176bd42009-07-23 18:46:00 +0200153struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
154 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000155{
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 Welte9176bd42009-07-23 18:46:00 +0200163 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000164}
165
Harald Welte76042182009-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
Holger Freyther4a49e772009-04-12 05:37:29 +0000182int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000183{
Holger Freyther4a49e772009-04-12 05:37:29 +0000184 /* FIXME: Migrate pending requests from one BSC to another */
185 switch (reason) {
186 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200187 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000188 /* Indicate "attached to LAC" */
189 s->lac = bts->location_area_code;
Harald Welteea5cf302009-07-29 23:14:15 +0200190 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000191 break;
192 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000193 /* Only detach if we are currently in this area */
194 if (bts->location_area_code == s->lac)
Holger Freyther6d5200b2009-06-02 02:54:38 +0000195 s->lac = 0;
Harald Welteea5cf302009-07-29 23:14:15 +0200196 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000197 break;
198 default:
199 fprintf(stderr, "subscr_update with unknown reason: %d\n",
200 reason);
201 break;
202 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000203 return db_sync_subscriber(s);
204}
205
206struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
207{
208 subscr->use_count++;
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000209 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
210 subscr->extension, subscr->use_count);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000211 return subscr;
212}
213
214struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
215{
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000216 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)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000220 subscr_free(subscr);
221 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000222}
Holger Freythera1f92f02009-04-12 05:37:52 +0000223
Holger Freyther04866d42009-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 Freyther85a7b362009-04-18 13:48:55 +0200228 struct subscr_request *request;
229
Harald Welte2cf161b2009-06-20 22:36:41 +0200230 request = talloc(tall_sub_req_ctx, struct subscr_request);
Holger Freyther85a7b362009-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 Freyther04866d42009-03-31 04:35:19 +0200258}
259
Holger Freythera1f92f02009-04-12 05:37:52 +0000260void 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 Freyther85a7b362009-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
Holger Freythera1f92f02009-04-12 05:37:52 +0000280 put_lchan(lchan);
Holger Freyther85a7b362009-04-18 13:48:55 +0200281
282 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
283 subscr_send_paging_request(lchan->subscr);
Holger Freythera1f92f02009-04-12 05:37:52 +0000284}
Holger Freyther04866d42009-03-31 04:35:19 +0200285
Harald Welte7bfc2672009-07-28 00:41:45 +0200286
287static __attribute__((constructor)) void on_dso_load_subscr(void)
288{
289 tall_subscr_ctx = talloc_named_const(tall_bsc_ctx, 1, "subscriber");
290
291 tall_sub_req_ctx = talloc_named_const(tall_bsc_ctx, 1,
292 "subscr_request");
293}