blob: 0bcbdf4021ab940f7649d700d65ddee47d253c02 [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 Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000036
Holger Freyther12aa50d2009-01-01 18:02:05 +000037LLIST_HEAD(active_subscribers);
Harald Welte2cf161b2009-06-20 22:36:41 +020038static void *tall_subscr_ctx;
39static void *tall_sub_req_ctx;
Holger Freyther12aa50d2009-01-01 18:02:05 +000040
Holger Freyther85a7b362009-04-18 13:48:55 +020041/*
42 * Struct for pending channel requests. This is managed in the
43 * llist_head requests of each subscriber. The reference counting
44 * should work in such a way that a subscriber with a pending request
45 * remains in memory.
46 */
47struct subscr_request {
48 struct llist_head entry;
49
50 /* back reference */
51 struct gsm_subscriber *subscr;
52
53 /* the requested channel type */
54 int channel_type;
55
56 /* the bts we have decided to use */
57 struct gsm_network *network;
58
59 /* the callback data */
60 gsm_cbfn *cbfn;
61 void *param;
62};
63
64/*
65 * We got the channel assigned and can now hand this channel
66 * over to one of our callbacks.
67 */
68static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
69 struct msgb *msg, void *data, void *param)
70{
71 struct subscr_request *request;
72 struct gsm_subscriber *subscr = (struct gsm_subscriber *)param;
73
74 assert(!llist_empty(&subscr->requests));
75
76 /*
77 * FIXME: What to do with paging requests coming during
78 * this callback? We must be sure to not start paging when
79 * we have an active connection to a subscriber and to make
80 * the subscr_put_channel work as required...
81 */
82 request = (struct subscr_request *)subscr->requests.next;
83 llist_del(&request->entry);
84 subscr->in_callback = 1;
85 request->cbfn(hooknum, event, msg, data, request->param);
86 subscr->in_callback = 0;
87
Harald Welte2cf161b2009-06-20 22:36:41 +020088 talloc_free(request);
Holger Freyther85a7b362009-04-18 13:48:55 +020089 return 0;
90}
91
92static void subscr_send_paging_request(struct gsm_subscriber *subscr)
93{
94 struct subscr_request *request;
95 assert(!llist_empty(&subscr->requests));
96
97 request = (struct subscr_request *)subscr->requests.next;
98 paging_request(request->network, subscr, request->channel_type,
99 subscr_paging_cb, subscr);
100}
101
Harald Welte75a983f2008-12-27 21:34:06 +0000102struct gsm_subscriber *subscr_alloc(void)
Harald Welte52b1f982008-12-23 20:25:15 +0000103{
Harald Welte75a983f2008-12-27 21:34:06 +0000104 struct gsm_subscriber *s;
105
Harald Welte2cf161b2009-06-20 22:36:41 +0200106 s = talloc(tall_subscr_ctx, struct gsm_subscriber);
Harald Welte75a983f2008-12-27 21:34:06 +0000107 if (!s)
108 return NULL;
109
110 memset(s, 0, sizeof(*s));
Holger Freyther12aa50d2009-01-01 18:02:05 +0000111 llist_add_tail(&s->entry, &active_subscribers);
112 s->use_count = 1;
Harald Welte75a983f2008-12-27 21:34:06 +0000113
Holger Freyther04866d42009-03-31 04:35:19 +0200114 INIT_LLIST_HEAD(&s->requests);
115
Harald Welte75a983f2008-12-27 21:34:06 +0000116 return s;
Harald Welte52b1f982008-12-23 20:25:15 +0000117}
Harald Welte75a983f2008-12-27 21:34:06 +0000118
Holger Freyther12aa50d2009-01-01 18:02:05 +0000119static void subscr_free(struct gsm_subscriber *subscr)
Harald Welte52b1f982008-12-23 20:25:15 +0000120{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000121 llist_del(&subscr->entry);
Harald Welte2cf161b2009-06-20 22:36:41 +0200122 talloc_free(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000123}
124
Harald Welte9176bd42009-07-23 18:46:00 +0200125struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
126 const char *tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000127{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000128 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000129
Holger Freyther12aa50d2009-01-01 18:02:05 +0000130 /* we might have a record in memory already */
131 llist_for_each_entry(subscr, &active_subscribers, entry) {
132 if (strcmp(subscr->tmsi, tmsi) == 0)
133 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000134 }
135
Harald Welte9176bd42009-07-23 18:46:00 +0200136 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi);
Harald Welte75a983f2008-12-27 21:34:06 +0000137}
138
Harald Welte9176bd42009-07-23 18:46:00 +0200139struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
140 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000141{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000142 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000143
Holger Freyther12aa50d2009-01-01 18:02:05 +0000144 llist_for_each_entry(subscr, &active_subscribers, entry) {
145 if (strcmp(subscr->imsi, imsi) == 0)
146 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000147 }
148
Harald Welte9176bd42009-07-23 18:46:00 +0200149 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000150}
151
Harald Welte9176bd42009-07-23 18:46:00 +0200152struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
153 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000154{
155 struct gsm_subscriber *subscr;
156
157 llist_for_each_entry(subscr, &active_subscribers, entry) {
158 if (strcmp(subscr->extension, ext) == 0)
159 return subscr_get(subscr);
160 }
161
Harald Welte9176bd42009-07-23 18:46:00 +0200162 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000163}
164
Holger Freyther4a49e772009-04-12 05:37:29 +0000165int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000166{
Holger Freyther4a49e772009-04-12 05:37:29 +0000167 /* FIXME: Migrate pending requests from one BSC to another */
168 switch (reason) {
169 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200170 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000171 /* Indicate "attached to LAC" */
172 s->lac = bts->location_area_code;
Holger Freyther4a49e772009-04-12 05:37:29 +0000173 break;
174 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000175 /* Only detach if we are currently in this area */
176 if (bts->location_area_code == s->lac)
Holger Freyther6d5200b2009-06-02 02:54:38 +0000177 s->lac = 0;
Holger Freyther4a49e772009-04-12 05:37:29 +0000178 break;
179 default:
180 fprintf(stderr, "subscr_update with unknown reason: %d\n",
181 reason);
182 break;
183 };
Holger Freyther12aa50d2009-01-01 18:02:05 +0000184 return db_sync_subscriber(s);
185}
186
187struct gsm_subscriber *subscr_get(struct gsm_subscriber *subscr)
188{
189 subscr->use_count++;
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000190 DEBUGP(DCC, "subscr %s usage increases usage to: %d\n",
191 subscr->extension, subscr->use_count);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000192 return subscr;
193}
194
195struct gsm_subscriber *subscr_put(struct gsm_subscriber *subscr)
196{
Holger Freyther3e0ef7c2009-06-02 02:54:48 +0000197 subscr->use_count--;
198 DEBUGP(DCC, "subscr %s usage decreased usage to: %d\n",
199 subscr->extension, subscr->use_count);
200 if (subscr->use_count <= 0)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000201 subscr_free(subscr);
202 return NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000203}
Holger Freythera1f92f02009-04-12 05:37:52 +0000204
Holger Freyther04866d42009-03-31 04:35:19 +0200205void subscr_get_channel(struct gsm_subscriber *subscr,
206 struct gsm_network *network, int type,
207 gsm_cbfn *cbfn, void *param)
208{
Holger Freyther85a7b362009-04-18 13:48:55 +0200209 struct subscr_request *request;
210
Harald Welte2cf161b2009-06-20 22:36:41 +0200211 request = talloc(tall_sub_req_ctx, struct subscr_request);
Holger Freyther85a7b362009-04-18 13:48:55 +0200212 if (!request) {
213 if (cbfn)
214 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
215 NULL, NULL, param);
216 return;
217 }
218
219 memset(request, 0, sizeof(*request));
220 request->network = network;
221 request->subscr = subscr;
222 request->channel_type = type;
223 request->cbfn = cbfn;
224 request->param = param;
225
226 /*
227 * FIXME: We might be able to assign more than one
228 * channel, e.g. voice and SMS submit at the same
229 * time.
230 */
231 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
232 /* add to the list, send a request */
233 llist_add_tail(&request->entry, &subscr->requests);
234 subscr_send_paging_request(subscr);
235 } else {
236 /* this will be picked up later, from subscr_put_channel */
237 llist_add_tail(&request->entry, &subscr->requests);
238 }
Holger Freyther04866d42009-03-31 04:35:19 +0200239}
240
Holger Freythera1f92f02009-04-12 05:37:52 +0000241void subscr_put_channel(struct gsm_lchan *lchan)
242{
243 /*
244 * FIXME: Continue with other requests now... by checking
245 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
246 * of the lchan after having asked the next requestee to handle
247 * the channel.
248 */
Holger Freyther85a7b362009-04-18 13:48:55 +0200249 /*
250 * FIXME: is the lchan is of a different type we could still
251 * issue an immediate assignment for another channel and then
252 * close this one.
253 */
254 /*
255 * Currently we will drop the last ref of the lchan which
256 * will result in a channel release on RSL and we will start
257 * the paging. This should work most of the time as the MS
258 * will listen to the paging requests before we timeout
259 */
260
Holger Freythera1f92f02009-04-12 05:37:52 +0000261 put_lchan(lchan);
Holger Freyther85a7b362009-04-18 13:48:55 +0200262
263 if (lchan->subscr && !llist_empty(&lchan->subscr->requests))
264 subscr_send_paging_request(lchan->subscr);
Holger Freythera1f92f02009-04-12 05:37:52 +0000265}
Holger Freyther04866d42009-03-31 04:35:19 +0200266
Harald Welte7bfc2672009-07-28 00:41:45 +0200267
268static __attribute__((constructor)) void on_dso_load_subscr(void)
269{
270 tall_subscr_ctx = talloc_named_const(tall_bsc_ctx, 1, "subscriber");
271
272 tall_sub_req_ctx = talloc_named_const(tall_bsc_ctx, 1,
273 "subscr_request");
274}