blob: fe7bb6aca52a6aa3a0e33a9d62aa2b49495805b2 [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
Holger Freyther4a49e772009-04-12 05:37:29 +0000166int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000167{
Holger Freyther4a49e772009-04-12 05:37:29 +0000168 /* FIXME: Migrate pending requests from one BSC to another */
169 switch (reason) {
170 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200171 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000172 /* Indicate "attached to LAC" */
173 s->lac = bts->location_area_code;
Harald Welteea5cf302009-07-29 23:14:15 +0200174 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000175 break;
176 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000177 /* Only detach if we are currently in this area */
178 if (bts->location_area_code == s->lac)
Holger Freyther6d5200b2009-06-02 02:54:38 +0000179 s->lac = 0;
Harald Welteea5cf302009-07-29 23:14:15 +0200180 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000181 break;
182 default:
183 fprintf(stderr, "subscr_update with unknown reason: %d\n",
184 reason);
185 break;
186 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000187 return db_sync_subscriber(s);
188}
189
190struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
191{
192 subscr->use_count++;
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000193 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
194 subscr->extension, subscr->use_count);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000195 return subscr;
196}
197
198struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
199{
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000200 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)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000204 subscr_free(subscr);
205 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000206}
Holger Freythera1f92f02009-04-12 05:37:52 +0000207
Holger Freyther04866d42009-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 Freyther85a7b362009-04-18 13:48:55 +0200212 struct subscr_request *request;
213
Harald Welte2cf161b2009-06-20 22:36:41 +0200214 request = talloc(tall_sub_req_ctx, struct subscr_request);
Holger Freyther85a7b362009-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 Freyther04866d42009-03-31 04:35:19 +0200242}
243
Holger Freythera1f92f02009-04-12 05:37:52 +0000244void 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 Freyther85a7b362009-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
Holger Freythera1f92f02009-04-12 05:37:52 +0000264 put_lchan(lchan);
Holger Freyther85a7b362009-04-18 13:48:55 +0200265
266 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
267 subscr_send_paging_request(lchan->subscr);
Holger Freythera1f92f02009-04-12 05:37:52 +0000268}
Holger Freyther04866d42009-03-31 04:35:19 +0200269
Harald Welte7bfc2672009-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}