blob: f0372aff75e3f1efbeb25b848d8bd2565454a93e [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
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010029#include <osmocom/core/talloc.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010030
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +010031#include <osmocom/vty/vty.h>
32
Harald Welte8470bf22008-12-25 23:28:35 +000033#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010034#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000035#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010036#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020037#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000039
Sylvain Munaut1e245502010-12-01 21:07:29 +010040void *tall_sub_req_ctx;
41
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020042extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte75a983f2008-12-27 21:34:06 +000043
Sylvain Munaut5a86e062010-12-01 22:38:03 +010044int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
45 gsm_cbfn *cb, void *cb_data);
46
Sylvain Munaut1e245502010-12-01 21:07:29 +010047
48/*
49 * Struct for pending channel requests. This is managed in the
50 * llist_head requests of each subscriber. The reference counting
51 * should work in such a way that a subscriber with a pending request
52 * remains in memory.
53 */
54struct subscr_request {
55 struct llist_head entry;
56
57 /* back reference */
58 struct gsm_subscriber *subscr;
59
60 /* the requested channel type */
61 int channel_type;
62
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010063 /* what did we do */
64 int state;
65
Sylvain Munaut1e245502010-12-01 21:07:29 +010066 /* the callback data */
67 gsm_cbfn *cbfn;
68 void *param;
69};
70
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010071enum {
72 REQ_STATE_INITIAL,
73 REQ_STATE_QUEUED,
74 REQ_STATE_PAGED,
75 REQ_STATE_FAILED_START,
76 REQ_STATE_DISPATCHED,
77};
78
Sylvain Munaut1e245502010-12-01 21:07:29 +010079/*
80 * We got the channel assigned and can now hand this channel
81 * over to one of our callbacks.
82 */
Sylvain Munaut5a86e062010-12-01 22:38:03 +010083static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
84 struct msgb *msg, void *data, void *param)
Sylvain Munaut1e245502010-12-01 21:07:29 +010085{
86 struct subscr_request *request;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010087 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut5a86e062010-12-01 22:38:03 +010088 struct gsm_subscriber *subscr = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010089 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +010090
91 /* There is no request anymore... */
92 if (llist_empty(&subscr->requests))
93 return -1;
94
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010095 /* Dispatch signal */
96 sig_data.subscr = subscr;
97 sig_data.bts = conn ? conn->bts : NULL;
98 sig_data.conn = conn;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +010099 sig_data.paging_result = event;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +0100100 dispatch_signal(
101 SS_PAGING,
102 event == GSM_PAGING_SUCCEEDED ?
103 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
104 &sig_data
105 );
106
Sylvain Munaut1e245502010-12-01 21:07:29 +0100107 /*
108 * FIXME: What to do with paging requests coming during
109 * this callback? We must be sure to not start paging when
110 * we have an active connection to a subscriber and to make
111 * the subscr_put_channel work as required...
112 */
113 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100114 request->state = REQ_STATE_DISPATCHED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100115 llist_del(&request->entry);
116 subscr->in_callback = 1;
117 request->cbfn(hooknum, event, msg, data, request->param);
118 subscr->in_callback = 0;
119
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100120 if (event != GSM_PAGING_SUCCEEDED) {
121 /*
122 * This is a workaround for a bigger issue. We have
123 * issued paging that might involve multiple BTSes
124 * and one of them have failed now. We will stop the
125 * other paging requests as well as the next timeout
126 * would work on the next paging request and the queue
127 * will do bad things. This should be fixed by counting
128 * the outstanding results.
129 */
130 paging_request_stop(NULL, subscr, NULL, NULL);
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100131 subscr_put_channel(subscr);
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100132 }
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100133
Sylvain Munaut1e245502010-12-01 21:07:29 +0100134 subscr_put(subscr);
135 talloc_free(request);
136 return 0;
137}
138
Sylvain Munaut5a86e062010-12-01 22:38:03 +0100139static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
140 struct msgb *msg, void *data, void *param)
141{
142 int rc;
143
144 switch (event) {
145 case GSM_SECURITY_AUTH_FAILED:
146 /* Dispatch as paging failure */
147 rc = subscr_paging_dispatch(
148 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
149 msg, data, param);
150 break;
151
152 case GSM_SECURITY_NOAVAIL:
153 case GSM_SECURITY_SUCCEEDED:
154 /* Dispatch as paging failure */
155 rc = subscr_paging_dispatch(
156 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
157 msg, data, param);
158 break;
159
160 default:
161 rc = -EINVAL;
162 }
163
164 return rc;
165}
166
167static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
168 struct msgb *msg, void *data, void *param)
169{
170 struct gsm_subscriber_connection *conn = data;
171 struct gsm48_hdr *gh;
172 struct gsm48_pag_resp *pr;
173
174 /* Other cases mean problem, dispatch direclty */
175 if (event != GSM_PAGING_SUCCEEDED)
176 return subscr_paging_dispatch(hooknum, event, msg, data, param);
177
178 /* Get paging response */
179 gh = msgb_l3(msg);
180 pr = (struct gsm48_pag_resp *)gh->data;
181
182 /* We _really_ have a channel, secure it now ! */
183 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
184}
185
186
Sylvain Munaut1e245502010-12-01 21:07:29 +0100187static void subscr_send_paging_request(struct gsm_subscriber *subscr)
188{
189 struct subscr_request *request;
190 int rc;
191
192 assert(!llist_empty(&subscr->requests));
193
194 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100195 request->state = REQ_STATE_PAGED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100196 rc = paging_request(subscr->net, subscr, request->channel_type,
197 subscr_paging_cb, subscr);
198
199 /* paging failed, quit now */
200 if (rc <= 0) {
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100201 request->state = REQ_STATE_FAILED_START;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100202 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100203 NULL, NULL, subscr);
204 }
205}
206
207void subscr_get_channel(struct gsm_subscriber *subscr,
208 int type, gsm_cbfn *cbfn, void *param)
209{
210 struct subscr_request *request;
211
212 request = talloc(tall_sub_req_ctx, struct subscr_request);
213 if (!request) {
214 if (cbfn)
215 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
216 NULL, NULL, param);
217 return;
218 }
219
220 memset(request, 0, sizeof(*request));
221 request->subscr = subscr_get(subscr);
222 request->channel_type = type;
223 request->cbfn = cbfn;
224 request->param = param;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100225 request->state = REQ_STATE_INITIAL;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100226
227 /*
228 * FIXME: We might be able to assign more than one
229 * channel, e.g. voice and SMS submit at the same
230 * time.
231 */
232 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
233 /* add to the list, send a request */
234 llist_add_tail(&request->entry, &subscr->requests);
235 subscr_send_paging_request(subscr);
236 } else {
237 /* this will be picked up later, from subscr_put_channel */
238 llist_add_tail(&request->entry, &subscr->requests);
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100239 request->state = REQ_STATE_QUEUED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100240 }
241}
242
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100243void subscr_put_channel(struct gsm_subscriber *subscr)
Sylvain Munaut1e245502010-12-01 21:07:29 +0100244{
245 /*
246 * FIXME: Continue with other requests now... by checking
247 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
248 * of the lchan after having asked the next requestee to handle
249 * the channel.
250 */
251 /*
252 * FIXME: is the lchan is of a different type we could still
253 * issue an immediate assignment for another channel and then
254 * close this one.
255 */
256 /*
257 * Currently we will drop the last ref of the lchan which
258 * will result in a channel release on RSL and we will start
259 * the paging. This should work most of the time as the MS
260 * will listen to the paging requests before we timeout
261 */
262
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100263 if (subscr && !llist_empty(&subscr->requests))
264 subscr_send_paging_request(subscr);
Sylvain Munaut1e245502010-12-01 21:07:29 +0100265}
266
267
Harald Welte9176bd42009-07-23 18:46:00 +0200268struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200269 uint32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000270{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200271 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +0000272 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000273
Holger Freyther12aa50d2009-01-01 18:02:05 +0000274 /* we might have a record in memory already */
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200275 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200276 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000277 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000278 }
279
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200280 sprintf(tmsi_string, "%u", tmsi);
281 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +0000282}
283
Harald Welte9176bd42009-07-23 18:46:00 +0200284struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
285 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000286{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000287 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000288
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200289 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +0000290 if (strcmp(subscr->imsi, imsi) == 0)
291 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000292 }
293
Harald Welte9176bd42009-07-23 18:46:00 +0200294 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000295}
296
Harald Welte9176bd42009-07-23 18:46:00 +0200297struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
298 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000299{
300 struct gsm_subscriber *subscr;
301
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200302 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +0000303 if (strcmp(subscr->extension, ext) == 0)
304 return subscr_get(subscr);
305 }
306
Harald Welte9176bd42009-07-23 18:46:00 +0200307 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000308}
309
Harald Welte76042182009-08-08 16:03:15 +0200310struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
311 unsigned long long id)
312{
313 struct gsm_subscriber *subscr;
314 char buf[32];
315 sprintf(buf, "%llu", id);
316
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200317 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte76042182009-08-08 16:03:15 +0200318 if (subscr->id == id)
319 return subscr_get(subscr);
320 }
321
322 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
323}
324
325
Holger Freyther4a49e772009-04-12 05:37:29 +0000326int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000327{
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100328 int rc;
329
Holger Freyther4a49e772009-04-12 05:37:29 +0000330 /* FIXME: Migrate pending requests from one BSC to another */
331 switch (reason) {
332 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200333 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000334 /* Indicate "attached to LAC" */
335 s->lac = bts->location_area_code;
Harald Welte77563da2009-12-24 13:48:14 +0100336 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte2e6d4682009-12-24 14:50:24 +0100337 subscr_name(s), s->lac);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100338 rc = db_sync_subscriber(s);
339 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200340 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000341 break;
342 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000343 /* Only detach if we are currently in this area */
344 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200345 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100346 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100347 rc = db_sync_subscriber(s);
348 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200349 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000350 break;
351 default:
352 fprintf(stderr, "subscr_update with unknown reason: %d\n",
353 reason);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100354 rc = db_sync_subscriber(s);
355 db_subscriber_update(s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000356 break;
357 };
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100358
359 return rc;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000360}
361
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100362void subscr_update_from_db(struct gsm_subscriber *sub)
363{
364 db_subscriber_update(sub);
365}
Holger Hans Peter Freytherebdd3cb2010-12-28 22:12:30 +0100366
367int subscr_pending_requests(struct gsm_subscriber *sub)
368{
369 struct subscr_request *req;
370 int pending = 0;
371
372 llist_for_each_entry(req, &sub->requests, entry)
373 pending += 1;
374
375 return pending;
376}
Holger Hans Peter Freytherfc857412010-12-28 22:21:55 +0100377
378int subscr_pending_clear(struct gsm_subscriber *sub)
379{
380 int deleted = 0;
381 struct subscr_request *req, *tmp;
382
383 llist_for_each_entry_safe(req, tmp, &sub->requests, entry) {
384 subscr_put(req->subscr);
385 llist_del(&req->entry);
386 talloc_free(req);
387 deleted += 1;
388 }
389
390 return deleted;
391}
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100392
393int subscr_pending_dump(struct gsm_subscriber *sub, struct vty *vty)
394{
395 struct subscr_request *req;
396
397 vty_out(vty, "Pending Requests for Subscriber %llu.%s", sub->id, VTY_NEWLINE);
398 llist_for_each_entry(req, &sub->requests, entry) {
399 vty_out(vty, "Channel type: %d State: %d Sub: %llu.%s",
400 req->channel_type, req->state, req->subscr->id, VTY_NEWLINE);
401 }
402
403 return 0;
404}
Holger Hans Peter Freyther451eb292010-12-29 13:45:49 +0100405
406int subscr_pending_kick(struct gsm_subscriber *sub)
407{
408 subscr_put_channel(sub);
409 return 0;
410}