blob: 3e65176b11892308f07751419ee7e46c83f74790 [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>
Jan Luebbebfbdeec2012-12-27 00:27:16 +010028#include <time.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010031
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +010032#include <osmocom/vty/vty.h>
33
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010035#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000036#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010037#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020038#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000039#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000040
Sylvain Munaut1e245502010-12-01 21:07:29 +010041void *tall_sub_req_ctx;
42
Jan Luebbe6e300682012-12-25 18:58:34 +010043extern struct llist_head *subscr_bsc_active_subscribers(void);
Harald Welte75a983f2008-12-27 21:34:06 +000044
Sylvain Munaut5a86e062010-12-01 22:38:03 +010045int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
46 gsm_cbfn *cb, void *cb_data);
47
Sylvain Munaut1e245502010-12-01 21:07:29 +010048
49/*
50 * Struct for pending channel requests. This is managed in the
51 * llist_head requests of each subscriber. The reference counting
52 * should work in such a way that a subscriber with a pending request
53 * remains in memory.
54 */
55struct subscr_request {
56 struct llist_head entry;
57
58 /* back reference */
59 struct gsm_subscriber *subscr;
60
61 /* the requested channel type */
62 int channel_type;
63
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010064 /* what did we do */
65 int state;
66
Sylvain Munaut1e245502010-12-01 21:07:29 +010067 /* the callback data */
68 gsm_cbfn *cbfn;
69 void *param;
70};
71
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +010072enum {
73 REQ_STATE_INITIAL,
74 REQ_STATE_QUEUED,
75 REQ_STATE_PAGED,
76 REQ_STATE_FAILED_START,
77 REQ_STATE_DISPATCHED,
78};
79
Sylvain Munaut1e245502010-12-01 21:07:29 +010080/*
81 * We got the channel assigned and can now hand this channel
82 * over to one of our callbacks.
83 */
Sylvain Munaut5a86e062010-12-01 22:38:03 +010084static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
85 struct msgb *msg, void *data, void *param)
Sylvain Munaut1e245502010-12-01 21:07:29 +010086{
87 struct subscr_request *request;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010088 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut5a86e062010-12-01 22:38:03 +010089 struct gsm_subscriber *subscr = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010090 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +010091
92 /* There is no request anymore... */
93 if (llist_empty(&subscr->requests))
94 return -1;
95
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010096 /* Dispatch signal */
97 sig_data.subscr = subscr;
98 sig_data.bts = conn ? conn->bts : NULL;
99 sig_data.conn = conn;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100100 sig_data.paging_result = event;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200101 osmo_signal_dispatch(
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +0100102 SS_PAGING,
103 event == GSM_PAGING_SUCCEEDED ?
104 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
105 &sig_data
106 );
107
Sylvain Munaut1e245502010-12-01 21:07:29 +0100108 /*
109 * FIXME: What to do with paging requests coming during
110 * this callback? We must be sure to not start paging when
111 * we have an active connection to a subscriber and to make
112 * the subscr_put_channel work as required...
113 */
114 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100115 request->state = REQ_STATE_DISPATCHED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100116 llist_del(&request->entry);
117 subscr->in_callback = 1;
118 request->cbfn(hooknum, event, msg, data, request->param);
119 subscr->in_callback = 0;
120
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100121 if (event != GSM_PAGING_SUCCEEDED) {
122 /*
123 * This is a workaround for a bigger issue. We have
124 * issued paging that might involve multiple BTSes
125 * and one of them have failed now. We will stop the
126 * other paging requests as well as the next timeout
127 * would work on the next paging request and the queue
128 * will do bad things. This should be fixed by counting
129 * the outstanding results.
130 */
131 paging_request_stop(NULL, subscr, NULL, NULL);
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100132 subscr_put_channel(subscr);
Holger Hans Peter Freytherffccb772010-12-28 17:47:43 +0100133 }
Holger Hans Peter Freytherf72b3d52010-12-28 17:27:05 +0100134
Sylvain Munaut1e245502010-12-01 21:07:29 +0100135 subscr_put(subscr);
136 talloc_free(request);
137 return 0;
138}
139
Sylvain Munaut5a86e062010-12-01 22:38:03 +0100140static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
141 struct msgb *msg, void *data, void *param)
142{
143 int rc;
144
145 switch (event) {
146 case GSM_SECURITY_AUTH_FAILED:
147 /* Dispatch as paging failure */
148 rc = subscr_paging_dispatch(
149 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
150 msg, data, param);
151 break;
152
153 case GSM_SECURITY_NOAVAIL:
154 case GSM_SECURITY_SUCCEEDED:
155 /* Dispatch as paging failure */
156 rc = subscr_paging_dispatch(
157 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
158 msg, data, param);
159 break;
160
161 default:
162 rc = -EINVAL;
163 }
164
165 return rc;
166}
167
168static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
169 struct msgb *msg, void *data, void *param)
170{
171 struct gsm_subscriber_connection *conn = data;
172 struct gsm48_hdr *gh;
173 struct gsm48_pag_resp *pr;
174
175 /* Other cases mean problem, dispatch direclty */
176 if (event != GSM_PAGING_SUCCEEDED)
177 return subscr_paging_dispatch(hooknum, event, msg, data, param);
178
179 /* Get paging response */
180 gh = msgb_l3(msg);
181 pr = (struct gsm48_pag_resp *)gh->data;
182
183 /* We _really_ have a channel, secure it now ! */
184 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
185}
186
187
Sylvain Munaut1e245502010-12-01 21:07:29 +0100188static void subscr_send_paging_request(struct gsm_subscriber *subscr)
189{
190 struct subscr_request *request;
191 int rc;
192
193 assert(!llist_empty(&subscr->requests));
194
195 request = (struct subscr_request *)subscr->requests.next;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100196 request->state = REQ_STATE_PAGED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100197 rc = paging_request(subscr->net, subscr, request->channel_type,
198 subscr_paging_cb, subscr);
199
200 /* paging failed, quit now */
201 if (rc <= 0) {
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100202 request->state = REQ_STATE_FAILED_START;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100203 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100204 NULL, NULL, subscr);
205 }
206}
207
208void subscr_get_channel(struct gsm_subscriber *subscr,
209 int type, gsm_cbfn *cbfn, void *param)
210{
211 struct subscr_request *request;
212
213 request = talloc(tall_sub_req_ctx, struct subscr_request);
214 if (!request) {
215 if (cbfn)
216 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
217 NULL, NULL, param);
218 return;
219 }
220
221 memset(request, 0, sizeof(*request));
222 request->subscr = subscr_get(subscr);
223 request->channel_type = type;
224 request->cbfn = cbfn;
225 request->param = param;
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100226 request->state = REQ_STATE_INITIAL;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100227
228 /*
229 * FIXME: We might be able to assign more than one
230 * channel, e.g. voice and SMS submit at the same
231 * time.
232 */
233 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
234 /* add to the list, send a request */
235 llist_add_tail(&request->entry, &subscr->requests);
236 subscr_send_paging_request(subscr);
237 } else {
238 /* this will be picked up later, from subscr_put_channel */
239 llist_add_tail(&request->entry, &subscr->requests);
Holger Hans Peter Freyther68c3bf62010-12-29 09:33:34 +0100240 request->state = REQ_STATE_QUEUED;
Sylvain Munaut1e245502010-12-01 21:07:29 +0100241 }
242}
243
Holger Hans Peter Freyther725966d2010-12-27 22:43:28 +0100244void subscr_put_channel(struct gsm_subscriber *subscr)
Sylvain Munaut1e245502010-12-01 21:07:29 +0100245{
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 */
252 /*
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 Hans Peter Freyther725966d2010-12-27 22:43:28 +0100264 if (subscr && !llist_empty(&subscr->requests))
265 subscr_send_paging_request(subscr);
Sylvain Munaut1e245502010-12-01 21:07:29 +0100266}
267
268
Harald Welte9176bd42009-07-23 18:46:00 +0200269struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200270 uint32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000271{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200272 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +0000273 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000274
Holger Freyther12aa50d2009-01-01 18:02:05 +0000275 /* we might have a record in memory already */
Jan Luebbe6e300682012-12-25 18:58:34 +0100276 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200277 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000278 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000279 }
280
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200281 sprintf(tmsi_string, "%u", tmsi);
282 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +0000283}
284
Harald Welte9176bd42009-07-23 18:46:00 +0200285struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
286 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000287{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000288 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000289
Jan Luebbe6e300682012-12-25 18:58:34 +0100290 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +0000291 if (strcmp(subscr->imsi, imsi) == 0)
292 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000293 }
294
Harald Welte9176bd42009-07-23 18:46:00 +0200295 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000296}
297
Harald Welte9176bd42009-07-23 18:46:00 +0200298struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
299 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000300{
301 struct gsm_subscriber *subscr;
302
Jan Luebbe6e300682012-12-25 18:58:34 +0100303 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +0000304 if (strcmp(subscr->extension, ext) == 0)
305 return subscr_get(subscr);
306 }
307
Harald Welte9176bd42009-07-23 18:46:00 +0200308 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000309}
310
Harald Welte76042182009-08-08 16:03:15 +0200311struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
312 unsigned long long id)
313{
314 struct gsm_subscriber *subscr;
315 char buf[32];
316 sprintf(buf, "%llu", id);
317
Jan Luebbe6e300682012-12-25 18:58:34 +0100318 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Harald Welte76042182009-08-08 16:03:15 +0200319 if (subscr->id == id)
320 return subscr_get(subscr);
321 }
322
323 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
324}
325
326
Holger Freyther4a49e772009-04-12 05:37:29 +0000327int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000328{
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100329 int rc;
330
Holger Freyther4a49e772009-04-12 05:37:29 +0000331 /* FIXME: Migrate pending requests from one BSC to another */
332 switch (reason) {
333 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200334 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000335 /* Indicate "attached to LAC" */
336 s->lac = bts->location_area_code;
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100337
338 /* FIXME: We should allow 0 for T3212 as well to disable the
339 * location update period. In that case we will need a way to
340 * indicate that in the database and then reenable that value in
341 * VTY.
342 */
343
344 /* Table 10.5.33: The T3212 timeout value field is coded as the
345 * binary representation of the timeout value for
346 * periodic updating in decihours. Mark the subscriber as
347 * inactive if it missed two consecutive location updates.
348 * Timeout is twice the t3212 value plus one minute */
349 s->expire_lu = time(NULL) +
350 (bts->si_common.chan_desc.t3212 * 60 * 6 * 2) + 60;
351
Harald Welte77563da2009-12-24 13:48:14 +0100352 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte2e6d4682009-12-24 14:50:24 +0100353 subscr_name(s), s->lac);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100354 rc = db_sync_subscriber(s);
355 db_subscriber_update(s);
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200356 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000357 break;
358 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000359 /* Only detach if we are currently in this area */
360 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200361 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100362 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100363 rc = db_sync_subscriber(s);
364 db_subscriber_update(s);
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200365 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000366 break;
367 default:
368 fprintf(stderr, "subscr_update with unknown reason: %d\n",
369 reason);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100370 rc = db_sync_subscriber(s);
371 db_subscriber_update(s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000372 break;
373 };
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100374
375 return rc;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000376}
377
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100378void subscr_update_from_db(struct gsm_subscriber *sub)
379{
380 db_subscriber_update(sub);
381}
Holger Hans Peter Freytherebdd3cb2010-12-28 22:12:30 +0100382
Jan Luebbebfbdeec2012-12-27 00:27:16 +0100383static void subscr_expire_callback(void *data, long long unsigned int id)
384{
385 struct gsm_network *net = data;
386 struct gsm_subscriber *s =
387 subscr_get_by_id(net, id);
388
389 LOGP(DMM, LOGL_NOTICE, "Expiring inactive subscriber %s (ID %i)\n",
390 subscr_name(s), id);
391 s->lac = GSM_LAC_RESERVED_DETACHED;
392 db_sync_subscriber(s);
393
394 subscr_put(s);
395}
396
397void subscr_expire(struct gsm_network *net)
398{
399 db_subscriber_expire(net, subscr_expire_callback);
400}
401
Holger Hans Peter Freytherebdd3cb2010-12-28 22:12:30 +0100402int subscr_pending_requests(struct gsm_subscriber *sub)
403{
404 struct subscr_request *req;
405 int pending = 0;
406
407 llist_for_each_entry(req, &sub->requests, entry)
408 pending += 1;
409
410 return pending;
411}
Holger Hans Peter Freytherfc857412010-12-28 22:21:55 +0100412
413int subscr_pending_clear(struct gsm_subscriber *sub)
414{
415 int deleted = 0;
416 struct subscr_request *req, *tmp;
417
418 llist_for_each_entry_safe(req, tmp, &sub->requests, entry) {
419 subscr_put(req->subscr);
420 llist_del(&req->entry);
421 talloc_free(req);
422 deleted += 1;
423 }
424
425 return deleted;
426}
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100427
428int subscr_pending_dump(struct gsm_subscriber *sub, struct vty *vty)
429{
430 struct subscr_request *req;
431
432 vty_out(vty, "Pending Requests for Subscriber %llu.%s", sub->id, VTY_NEWLINE);
433 llist_for_each_entry(req, &sub->requests, entry) {
434 vty_out(vty, "Channel type: %d State: %d Sub: %llu.%s",
435 req->channel_type, req->state, req->subscr->id, VTY_NEWLINE);
436 }
437
438 return 0;
439}
Holger Hans Peter Freyther451eb292010-12-29 13:45:49 +0100440
441int subscr_pending_kick(struct gsm_subscriber *sub)
442{
443 subscr_put_channel(sub);
444 return 0;
445}