blob: 9b8adebaa96a44e643681ae072c20256c9ada268 [file] [log] [blame]
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +02001/* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */
Harald Welte52b1f982008-12-23 20:25:15 +00002
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
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000020 *
21 */
22
Harald Welte75a983f2008-12-27 21:34:06 +000023#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020027#include <assert.h>
Harald Welte52b1f982008-12-23 20:25:15 +000028
Sylvain Munaut1e245502010-12-01 21:07:29 +010029#include <osmocore/talloc.h>
30
Harald Welte8470bf22008-12-25 23:28:35 +000031#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010032#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000033#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010034#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
Sylvain Munaut1e245502010-12-01 21:07:29 +010038void *tall_sub_req_ctx;
39
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020040extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte75a983f2008-12-27 21:34:06 +000041
Sylvain Munaut5a86e062010-12-01 22:38:03 +010042int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
43 gsm_cbfn *cb, void *cb_data);
44
Sylvain Munaut1e245502010-12-01 21:07:29 +010045
46/*
47 * Struct for pending channel requests. This is managed in the
48 * llist_head requests of each subscriber. The reference counting
49 * should work in such a way that a subscriber with a pending request
50 * remains in memory.
51 */
52struct subscr_request {
53 struct llist_head entry;
54
55 /* back reference */
56 struct gsm_subscriber *subscr;
57
58 /* the requested channel type */
59 int channel_type;
60
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010061 /* what did we do */
62 int state;
63
Sylvain Munaut1e245502010-12-01 21:07:29 +010064 /* the callback data */
65 gsm_cbfn *cbfn;
66 void *param;
67};
68
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010069enum {
70 REQ_STATE_INITIAL,
71 REQ_STATE_QUEUED,
72 REQ_STATE_PAGED,
73 REQ_STATE_FAILED_START,
74 REQ_STATE_DISPATCHED,
75};
76
Sylvain Munaut1e245502010-12-01 21:07:29 +010077/*
78 * We got the channel assigned and can now hand this channel
79 * over to one of our callbacks.
80 */
Sylvain Munaut5a86e062010-12-01 22:38:03 +010081static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
82 struct msgb *msg, void *data, void *param)
Sylvain Munaut1e245502010-12-01 21:07:29 +010083{
84 struct subscr_request *request;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010085 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut5a86e062010-12-01 22:38:03 +010086 struct gsm_subscriber *subscr = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010087 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +010088
89 /* There is no request anymore... */
90 if (llist_empty(&subscr->requests))
91 return -1;
92
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010093 /* Dispatch signal */
94 sig_data.subscr = subscr;
95 sig_data.bts = conn ? conn->bts : NULL;
96 sig_data.conn = conn;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +010097 sig_data.paging_result = event;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010098 dispatch_signal(
99 SS_PAGING,
100 event == GSM_PAGING_SUCCEEDED ?
101 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
102 &sig_data
103 );
104
Sylvain Munaut1e245502010-12-01 21:07:29 +0100105 /*
106 * FIXME: What to do with paging requests coming during
107 * this callback? We must be sure to not start paging when
108 * we have an active connection to a subscriber and to make
109 * the subscr_put_channel work as required...
110 */
111 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100112 request->state = REQ_STATE_DISPATCHED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100113 llist_del(&request->entry);
114 subscr->in_callback = 1;
115 request->cbfn(hooknum, event, msg, data, request->param);
116 subscr->in_callback = 0;
117
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100118 if (event != GSM_PAGING_SUCCEEDED) {
119 /*
120 * This is a workaround for a bigger issue. We have
121 * issued paging that might involve multiple BTSes
122 * and one of them have failed now. We will stop the
123 * other paging requests as well as the next timeout
124 * would work on the next paging request and the queue
125 * will do bad things. This should be fixed by counting
126 * the outstanding results.
127 */
128 paging_request_stop(NULL, subscr, NULL, NULL);
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100129 subscr_put_channel(subscr);
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100130 }
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100131
Sylvain Munaut1e245502010-12-01 21:07:29 +0100132 subscr_put(subscr);
133 talloc_free(request);
134 return 0;
135}
136
Sylvain Munaut5a86e062010-12-01 22:38:03 +0100137static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
138 struct msgb *msg, void *data, void *param)
139{
140 int rc;
141
142 switch (event) {
143 case GSM_SECURITY_AUTH_FAILED:
144 /* Dispatch as paging failure */
145 rc = subscr_paging_dispatch(
146 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
147 msg, data, param);
148 break;
149
150 case GSM_SECURITY_NOAVAIL:
151 case GSM_SECURITY_SUCCEEDED:
152 /* Dispatch as paging failure */
153 rc = subscr_paging_dispatch(
154 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
155 msg, data, param);
156 break;
157
158 default:
159 rc = -EINVAL;
160 }
161
162 return rc;
163}
164
165static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
166 struct msgb *msg, void *data, void *param)
167{
168 struct gsm_subscriber_connection *conn = data;
169 struct gsm48_hdr *gh;
170 struct gsm48_pag_resp *pr;
171
172 /* Other cases mean problem, dispatch direclty */
173 if (event != GSM_PAGING_SUCCEEDED)
174 return subscr_paging_dispatch(hooknum, event, msg, data, param);
175
176 /* Get paging response */
177 gh = msgb_l3(msg);
178 pr = (struct gsm48_pag_resp *)gh->data;
179
180 /* We _really_ have a channel, secure it now ! */
181 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
182}
183
184
Sylvain Munaut1e245502010-12-01 21:07:29 +0100185static void subscr_send_paging_request(struct gsm_subscriber *subscr)
186{
187 struct subscr_request *request;
188 int rc;
189
190 assert(!llist_empty(&subscr->requests));
191
192 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100193 request->state = REQ_STATE_PAGED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100194 rc = paging_request(subscr->net, subscr, request->channel_type,
195 subscr_paging_cb, subscr);
196
197 /* paging failed, quit now */
198 if (rc <= 0) {
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100199 request->state = REQ_STATE_FAILED_START;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100200 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100201 NULL, NULL, subscr);
202 }
203}
204
205void subscr_get_channel(struct gsm_subscriber *subscr,
206 int type, gsm_cbfn *cbfn, void *param)
207{
208 struct subscr_request *request;
209
210 request = talloc(tall_sub_req_ctx, struct subscr_request);
211 if (!request) {
212 if (cbfn)
213 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
214 NULL, NULL, param);
215 return;
216 }
217
218 memset(request, 0, sizeof(*request));
219 request->subscr = subscr_get(subscr);
220 request->channel_type = type;
221 request->cbfn = cbfn;
222 request->param = param;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100223 request->state = REQ_STATE_INITIAL;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100224
225 /*
226 * FIXME: We might be able to assign more than one
227 * channel, e.g. voice and SMS submit at the same
228 * time.
229 */
230 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
231 /* add to the list, send a request */
232 llist_add_tail(&request->entry, &subscr->requests);
233 subscr_send_paging_request(subscr);
234 } else {
235 /* this will be picked up later, from subscr_put_channel */
236 llist_add_tail(&request->entry, &subscr->requests);
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100237 request->state = REQ_STATE_QUEUED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100238 }
239}
240
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100241void subscr_put_channel(struct gsm_subscriber *subscr)
Sylvain Munaut1e245502010-12-01 21:07:29 +0100242{
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 */
249 /*
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 Hans Peter Freyther725966d2010-12-27 22:43:28 +0100261 if (subscr && !llist_empty(&subscr->requests))
262 subscr_send_paging_request(subscr);
Sylvain Munaut1e245502010-12-01 21:07:29 +0100263}
264
265
Harald Welte9176bd42009-07-23 18:46:00 +0200266struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200267 u_int32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000268{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200269 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +0000270 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000271
Holger Freyther12aa50d2009-01-01 18:02:05 +0000272 /* we might have a record in memory already */
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200273 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200274 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000275 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000276 }
277
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200278 sprintf(tmsi_string, "%u", tmsi);
279 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +0000280}
281
Harald Welte9176bd42009-07-23 18:46:00 +0200282struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
283 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000284{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000285 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000286
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200287 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +0000288 if (strcmp(subscr->imsi, imsi) == 0)
289 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000290 }
291
Harald Welte9176bd42009-07-23 18:46:00 +0200292 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000293}
294
Harald Welte9176bd42009-07-23 18:46:00 +0200295struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
296 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000297{
298 struct gsm_subscriber *subscr;
299
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200300 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +0000301 if (strcmp(subscr->extension, ext) == 0)
302 return subscr_get(subscr);
303 }
304
Harald Welte9176bd42009-07-23 18:46:00 +0200305 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000306}
307
Harald Welte76042182009-08-08 16:03:15 +0200308struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
309 unsigned long long id)
310{
311 struct gsm_subscriber *subscr;
312 char buf[32];
313 sprintf(buf, "%llu", id);
314
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200315 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte76042182009-08-08 16:03:15 +0200316 if (subscr->id == id)
317 return subscr_get(subscr);
318 }
319
320 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
321}
322
323
Holger Freyther4a49e772009-04-12 05:37:29 +0000324int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000325{
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100326 int rc;
327
Holger Freyther4a49e772009-04-12 05:37:29 +0000328 /* FIXME: Migrate pending requests from one BSC to another */
329 switch (reason) {
330 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200331 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000332 /* Indicate "attached to LAC" */
333 s->lac = bts->location_area_code;
Harald Welte77563da2009-12-24 13:48:14 +0100334 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte2e6d4682009-12-24 14:50:24 +0100335 subscr_name(s), s->lac);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100336 rc = db_sync_subscriber(s);
337 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200338 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000339 break;
340 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000341 /* Only detach if we are currently in this area */
342 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200343 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100344 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100345 rc = db_sync_subscriber(s);
346 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200347 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000348 break;
349 default:
350 fprintf(stderr, "subscr_update with unknown reason: %d\n",
351 reason);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100352 rc = db_sync_subscriber(s);
353 db_subscriber_update(s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000354 break;
355 };
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100356
357 return rc;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000358}
359
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100360void subscr_update_from_db(struct gsm_subscriber *sub)
361{
362 db_subscriber_update(sub);
363}
Holger Hans Peter Freytherebdd3cb2010-12-28 22:12:30 +0100364
365int subscr_pending_requests(struct gsm_subscriber *sub)
366{
367 struct subscr_request *req;
368 int pending = 0;
369
370 llist_for_each_entry(req, &sub->requests, entry)
371 pending += 1;
372
373 return pending;
374}
Holger Hans Peter Freytherfc857412010-12-28 22:21:55 +0100375
376int subscr_pending_clear(struct gsm_subscriber *sub)
377{
378 int deleted = 0;
379 struct subscr_request *req, *tmp;
380
381 llist_for_each_entry_safe(req, tmp, &sub->requests, entry) {
382 subscr_put(req->subscr);
383 llist_del(&req->entry);
384 talloc_free(req);
385 deleted += 1;
386 }
387
388 return deleted;
389}