blob: 3e65176b11892308f07751419ee7e46c83f74790 [file] [log] [blame]
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +02001/* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */
Harald Welte59b04682009-06-10 05:40:52 +08002
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080011 * (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 Welte0e3e88e2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080017 *
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080020 *
21 */
22
23#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020027#include <assert.h>
Jan Luebbefe7cc1e2012-12-27 00:27:16 +010028#include <time.h>
Harald Welte59b04682009-06-10 05:40:52 +080029
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Sylvain Munaut34619752010-12-01 21:07:29 +010031
Holger Hans Peter Freyther0fe0c8c2010-12-29 11:07:22 +010032#include <osmocom/vty/vty.h>
33
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/gsm_subscriber.h>
Sylvain Munaut65c51b62010-12-01 22:38:03 +010035#include <openbsc/gsm_04_08.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/debug.h>
Sylvain Munaut34619752010-12-01 21:07:29 +010037#include <openbsc/paging.h>
Harald Welteb7799832009-07-29 23:14:15 +020038#include <openbsc/signal.h>
Harald Welte59b04682009-06-10 05:40:52 +080039#include <openbsc/db.h>
40
Sylvain Munaut34619752010-12-01 21:07:29 +010041void *tall_sub_req_ctx;
42
Jan Luebbe8f7f1d12012-12-25 18:58:34 +010043extern struct llist_head *subscr_bsc_active_subscribers(void);
Harald Welte59b04682009-06-10 05:40:52 +080044
Sylvain Munaut65c51b62010-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 Munaut34619752010-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 Freytherc45f0a62010-12-29 09:33:34 +010064 /* what did we do */
65 int state;
66
Sylvain Munaut34619752010-12-01 21:07:29 +010067 /* the callback data */
68 gsm_cbfn *cbfn;
69 void *param;
70};
71
Holger Hans Peter Freytherc45f0a62010-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 Munaut34619752010-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 Munaut65c51b62010-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 Munaut34619752010-12-01 21:07:29 +010086{
87 struct subscr_request *request;
Sylvain Munaut7a8f8812010-12-01 23:04:03 +010088 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut65c51b62010-12-01 22:38:03 +010089 struct gsm_subscriber *subscr = param;
Sylvain Munaut7a8f8812010-12-01 23:04:03 +010090 struct paging_signal_data sig_data;
Sylvain Munaut34619752010-12-01 21:07:29 +010091
92 /* There is no request anymore... */
93 if (llist_empty(&subscr->requests))
94 return -1;
95
Sylvain Munaut7a8f8812010-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 Freytherab2a9332010-12-23 18:19:17 +0100100 sig_data.paging_result = event;
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200101 osmo_signal_dispatch(
Sylvain Munaut7a8f8812010-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 Munaut34619752010-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 Freytherc45f0a62010-12-29 09:33:34 +0100115 request->state = REQ_STATE_DISPATCHED;
Sylvain Munaut34619752010-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 Freythera32e9c12010-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 Freytherbffd4852010-12-28 17:27:05 +0100132 subscr_put_channel(subscr);
Holger Hans Peter Freythera32e9c12010-12-28 17:47:43 +0100133 }
Holger Hans Peter Freytherbffd4852010-12-28 17:27:05 +0100134
Sylvain Munaut34619752010-12-01 21:07:29 +0100135 subscr_put(subscr);
136 talloc_free(request);
137 return 0;
138}
139
Sylvain Munaut65c51b62010-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 Munaut34619752010-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 Freytherc45f0a62010-12-29 09:33:34 +0100196 request->state = REQ_STATE_PAGED;
Sylvain Munaut34619752010-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 Freytherc45f0a62010-12-29 09:33:34 +0100202 request->state = REQ_STATE_FAILED_START;
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +0100203 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut34619752010-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 Freytherc45f0a62010-12-29 09:33:34 +0100226 request->state = REQ_STATE_INITIAL;
Sylvain Munaut34619752010-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 Freytherc45f0a62010-12-29 09:33:34 +0100240 request->state = REQ_STATE_QUEUED;
Sylvain Munaut34619752010-12-01 21:07:29 +0100241 }
242}
243
Holger Hans Peter Freyther000571f2010-12-27 22:43:28 +0100244void subscr_put_channel(struct gsm_subscriber *subscr)
Sylvain Munaut34619752010-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 Freyther000571f2010-12-27 22:43:28 +0100264 if (subscr && !llist_empty(&subscr->requests))
265 subscr_send_paging_request(subscr);
Sylvain Munaut34619752010-12-01 21:07:29 +0100266}
267
268
Harald Welte75350412009-07-23 18:46:00 +0200269struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200270 uint32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800271{
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200272 char tmsi_string[14];
Harald Welte59b04682009-06-10 05:40:52 +0800273 struct gsm_subscriber *subscr;
274
275 /* we might have a record in memory already */
Jan Luebbe8f7f1d12012-12-25 18:58:34 +0100276 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200277 if (tmsi == subscr->tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800278 return subscr_get(subscr);
279 }
280
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200281 sprintf(tmsi_string, "%u", tmsi);
282 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800283}
284
Harald Welte75350412009-07-23 18:46:00 +0200285struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
286 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +0800287{
288 struct gsm_subscriber *subscr;
289
Jan Luebbe8f7f1d12012-12-25 18:58:34 +0100290 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +0800291 if (strcmp(subscr->imsi, imsi) == 0)
292 return subscr_get(subscr);
293 }
294
Harald Welte75350412009-07-23 18:46:00 +0200295 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +0800296}
297
Harald Welte75350412009-07-23 18:46:00 +0200298struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
299 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +0800300{
301 struct gsm_subscriber *subscr;
302
Jan Luebbe8f7f1d12012-12-25 18:58:34 +0100303 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +0800304 if (strcmp(subscr->extension, ext) == 0)
305 return subscr_get(subscr);
306 }
307
Harald Welte75350412009-07-23 18:46:00 +0200308 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +0800309}
310
Harald Welte68b7df22009-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 Luebbe8f7f1d12012-12-25 18:58:34 +0100318 llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) {
Harald Welte68b7df22009-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
Harald Welte59b04682009-06-10 05:40:52 +0800327int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
328{
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100329 int rc;
330
Harald Welte59b04682009-06-10 05:40:52 +0800331 /* FIXME: Migrate pending requests from one BSC to another */
332 switch (reason) {
333 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +0200334 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800335 /* Indicate "attached to LAC" */
336 s->lac = bts->location_area_code;
Jan Luebbefe7cc1e2012-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 Weltef00c2ac2009-12-24 13:48:14 +0100352 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte0747c6a2009-12-24 14:50:24 +0100353 subscr_name(s), s->lac);
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100354 rc = db_sync_subscriber(s);
355 db_subscriber_update(s);
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200356 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800357 break;
358 case GSM_SUBSCRIBER_UPDATE_DETACHED:
359 /* Only detach if we are currently in this area */
360 if (bts->location_area_code == s->lac)
Holger Hans Peter Freyther6c6ab862009-10-01 04:07:15 +0200361 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte0747c6a2009-12-24 14:50:24 +0100362 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100363 rc = db_sync_subscriber(s);
364 db_subscriber_update(s);
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200365 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800366 break;
367 default:
368 fprintf(stderr, "subscr_update with unknown reason: %d\n",
369 reason);
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100370 rc = db_sync_subscriber(s);
371 db_subscriber_update(s);
Harald Welte59b04682009-06-10 05:40:52 +0800372 break;
373 };
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100374
375 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800376}
377
Holger Hans Peter Freyther0c29e5f2010-12-22 18:12:11 +0100378void subscr_update_from_db(struct gsm_subscriber *sub)
379{
380 db_subscriber_update(sub);
381}
Holger Hans Peter Freyther5b72b302010-12-28 22:12:30 +0100382
Jan Luebbefe7cc1e2012-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 Freyther5b72b302010-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 Freytherce69aec2010-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 Freyther0fe0c8c2010-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 Freythere5d806e2010-12-29 13:45:49 +0100440
441int subscr_pending_kick(struct gsm_subscriber *sub)
442{
443 subscr_put_channel(sub);
444 return 0;
445}