2009-06-12 05:08:13 +00:00
|
|
|
/* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */
|
2008-12-23 20:25:15 +00:00
|
|
|
|
|
|
|
/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
|
2009-01-01 18:02:05 +00:00
|
|
|
* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2008-12-25 23:28:35 +00:00
|
|
|
*
|
2008-12-23 20:25:15 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2011-01-01 14:25:50 +00:00
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2008-12-23 20:25:15 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-01-01 14:25:50 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
2008-12-23 20:25:15 +00:00
|
|
|
*
|
2011-01-01 14:25:50 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-23 20:25:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-12-27 21:34:06 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2009-04-18 11:48:55 +00:00
|
|
|
#include <assert.h>
|
2008-12-23 20:25:15 +00:00
|
|
|
|
2010-12-01 20:07:29 +00:00
|
|
|
#include <osmocore/talloc.h>
|
|
|
|
|
2008-12-25 23:28:35 +00:00
|
|
|
#include <openbsc/gsm_subscriber.h>
|
2010-12-01 21:38:03 +00:00
|
|
|
#include <openbsc/gsm_04_08.h>
|
2009-06-02 02:54:48 +00:00
|
|
|
#include <openbsc/debug.h>
|
2010-12-01 20:07:29 +00:00
|
|
|
#include <openbsc/paging.h>
|
2009-07-29 21:14:15 +00:00
|
|
|
#include <openbsc/signal.h>
|
2008-12-27 21:34:06 +00:00
|
|
|
#include <openbsc/db.h>
|
2008-12-23 20:25:15 +00:00
|
|
|
|
2010-12-01 20:07:29 +00:00
|
|
|
void *tall_sub_req_ctx;
|
|
|
|
|
2009-06-12 05:08:13 +00:00
|
|
|
extern struct llist_head *subscr_bsc_active_subscriber(void);
|
2008-12-27 21:34:06 +00:00
|
|
|
|
2010-12-01 21:38:03 +00:00
|
|
|
int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
|
|
|
|
gsm_cbfn *cb, void *cb_data);
|
|
|
|
|
2010-12-01 20:07:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Struct for pending channel requests. This is managed in the
|
|
|
|
* llist_head requests of each subscriber. The reference counting
|
|
|
|
* should work in such a way that a subscriber with a pending request
|
|
|
|
* remains in memory.
|
|
|
|
*/
|
|
|
|
struct subscr_request {
|
|
|
|
struct llist_head entry;
|
|
|
|
|
|
|
|
/* back reference */
|
|
|
|
struct gsm_subscriber *subscr;
|
|
|
|
|
|
|
|
/* the requested channel type */
|
|
|
|
int channel_type;
|
|
|
|
|
|
|
|
/* the callback data */
|
|
|
|
gsm_cbfn *cbfn;
|
|
|
|
void *param;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We got the channel assigned and can now hand this channel
|
|
|
|
* over to one of our callbacks.
|
|
|
|
*/
|
2010-12-01 21:38:03 +00:00
|
|
|
static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
|
|
|
|
struct msgb *msg, void *data, void *param)
|
2010-12-01 20:07:29 +00:00
|
|
|
{
|
|
|
|
struct subscr_request *request;
|
2010-12-01 22:04:03 +00:00
|
|
|
struct gsm_subscriber_connection *conn = data;
|
2010-12-01 21:38:03 +00:00
|
|
|
struct gsm_subscriber *subscr = param;
|
2010-12-01 22:04:03 +00:00
|
|
|
struct paging_signal_data sig_data;
|
2010-12-01 20:07:29 +00:00
|
|
|
|
|
|
|
/* There is no request anymore... */
|
|
|
|
if (llist_empty(&subscr->requests))
|
|
|
|
return -1;
|
|
|
|
|
2010-12-01 22:04:03 +00:00
|
|
|
/* Dispatch signal */
|
|
|
|
sig_data.subscr = subscr;
|
|
|
|
sig_data.bts = conn ? conn->bts : NULL;
|
|
|
|
sig_data.conn = conn;
|
2010-12-23 17:19:17 +00:00
|
|
|
sig_data.paging_result = event;
|
2010-12-01 22:04:03 +00:00
|
|
|
dispatch_signal(
|
|
|
|
SS_PAGING,
|
|
|
|
event == GSM_PAGING_SUCCEEDED ?
|
|
|
|
S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
|
|
|
|
&sig_data
|
|
|
|
);
|
|
|
|
|
2010-12-01 20:07:29 +00:00
|
|
|
/*
|
|
|
|
* FIXME: What to do with paging requests coming during
|
|
|
|
* this callback? We must be sure to not start paging when
|
|
|
|
* we have an active connection to a subscriber and to make
|
|
|
|
* the subscr_put_channel work as required...
|
|
|
|
*/
|
|
|
|
request = (struct subscr_request *)subscr->requests.next;
|
|
|
|
llist_del(&request->entry);
|
|
|
|
subscr->in_callback = 1;
|
|
|
|
request->cbfn(hooknum, event, msg, data, request->param);
|
|
|
|
subscr->in_callback = 0;
|
|
|
|
|
2010-12-28 16:27:05 +00:00
|
|
|
if (event != GSM_PAGING_SUCCEEDED)
|
|
|
|
subscr_put_channel(subscr);
|
|
|
|
|
2010-12-01 20:07:29 +00:00
|
|
|
subscr_put(subscr);
|
|
|
|
talloc_free(request);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-01 21:38:03 +00:00
|
|
|
static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
|
|
|
|
struct msgb *msg, void *data, void *param)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case GSM_SECURITY_AUTH_FAILED:
|
|
|
|
/* Dispatch as paging failure */
|
|
|
|
rc = subscr_paging_dispatch(
|
|
|
|
GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
|
|
|
|
msg, data, param);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GSM_SECURITY_NOAVAIL:
|
|
|
|
case GSM_SECURITY_SUCCEEDED:
|
|
|
|
/* Dispatch as paging failure */
|
|
|
|
rc = subscr_paging_dispatch(
|
|
|
|
GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
|
|
|
|
msg, data, param);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
rc = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
|
|
|
|
struct msgb *msg, void *data, void *param)
|
|
|
|
{
|
|
|
|
struct gsm_subscriber_connection *conn = data;
|
|
|
|
struct gsm48_hdr *gh;
|
|
|
|
struct gsm48_pag_resp *pr;
|
|
|
|
|
|
|
|
/* Other cases mean problem, dispatch direclty */
|
|
|
|
if (event != GSM_PAGING_SUCCEEDED)
|
|
|
|
return subscr_paging_dispatch(hooknum, event, msg, data, param);
|
|
|
|
|
|
|
|
/* Get paging response */
|
|
|
|
gh = msgb_l3(msg);
|
|
|
|
pr = (struct gsm48_pag_resp *)gh->data;
|
|
|
|
|
|
|
|
/* We _really_ have a channel, secure it now ! */
|
|
|
|
return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 20:07:29 +00:00
|
|
|
static void subscr_send_paging_request(struct gsm_subscriber *subscr)
|
|
|
|
{
|
|
|
|
struct subscr_request *request;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(!llist_empty(&subscr->requests));
|
|
|
|
|
|
|
|
request = (struct subscr_request *)subscr->requests.next;
|
|
|
|
rc = paging_request(subscr->net, subscr, request->channel_type,
|
|
|
|
subscr_paging_cb, subscr);
|
|
|
|
|
|
|
|
/* paging failed, quit now */
|
|
|
|
if (rc <= 0) {
|
2010-12-23 17:19:17 +00:00
|
|
|
subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
|
2010-12-01 20:07:29 +00:00
|
|
|
NULL, NULL, subscr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void subscr_get_channel(struct gsm_subscriber *subscr,
|
|
|
|
int type, gsm_cbfn *cbfn, void *param)
|
|
|
|
{
|
|
|
|
struct subscr_request *request;
|
|
|
|
|
|
|
|
request = talloc(tall_sub_req_ctx, struct subscr_request);
|
|
|
|
if (!request) {
|
|
|
|
if (cbfn)
|
|
|
|
cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
|
|
|
|
NULL, NULL, param);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(request, 0, sizeof(*request));
|
|
|
|
request->subscr = subscr_get(subscr);
|
|
|
|
request->channel_type = type;
|
|
|
|
request->cbfn = cbfn;
|
|
|
|
request->param = param;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: We might be able to assign more than one
|
|
|
|
* channel, e.g. voice and SMS submit at the same
|
|
|
|
* time.
|
|
|
|
*/
|
|
|
|
if (!subscr->in_callback && llist_empty(&subscr->requests)) {
|
|
|
|
/* add to the list, send a request */
|
|
|
|
llist_add_tail(&request->entry, &subscr->requests);
|
|
|
|
subscr_send_paging_request(subscr);
|
|
|
|
} else {
|
|
|
|
/* this will be picked up later, from subscr_put_channel */
|
|
|
|
llist_add_tail(&request->entry, &subscr->requests);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-27 21:43:28 +00:00
|
|
|
void subscr_put_channel(struct gsm_subscriber *subscr)
|
2010-12-01 20:07:29 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* FIXME: Continue with other requests now... by checking
|
|
|
|
* the gsm_subscriber inside the gsm_lchan. Drop the ref count
|
|
|
|
* of the lchan after having asked the next requestee to handle
|
|
|
|
* the channel.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* FIXME: is the lchan is of a different type we could still
|
|
|
|
* issue an immediate assignment for another channel and then
|
|
|
|
* close this one.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Currently we will drop the last ref of the lchan which
|
|
|
|
* will result in a channel release on RSL and we will start
|
|
|
|
* the paging. This should work most of the time as the MS
|
|
|
|
* will listen to the paging requests before we timeout
|
|
|
|
*/
|
|
|
|
|
2010-12-27 21:43:28 +00:00
|
|
|
if (subscr && !llist_empty(&subscr->requests))
|
|
|
|
subscr_send_paging_request(subscr);
|
2010-12-01 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-23 16:46:00 +00:00
|
|
|
struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
|
2009-08-19 10:53:57 +00:00
|
|
|
u_int32_t tmsi)
|
2008-12-23 20:25:15 +00:00
|
|
|
{
|
2009-08-19 10:53:57 +00:00
|
|
|
char tmsi_string[14];
|
2009-01-01 18:02:05 +00:00
|
|
|
struct gsm_subscriber *subscr;
|
2008-12-27 21:34:06 +00:00
|
|
|
|
2009-01-01 18:02:05 +00:00
|
|
|
/* we might have a record in memory already */
|
2009-06-12 05:08:13 +00:00
|
|
|
llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
|
2009-08-19 10:53:57 +00:00
|
|
|
if (tmsi == subscr->tmsi)
|
2009-01-01 18:02:05 +00:00
|
|
|
return subscr_get(subscr);
|
2008-12-27 21:34:06 +00:00
|
|
|
}
|
|
|
|
|
2009-08-19 10:53:57 +00:00
|
|
|
sprintf(tmsi_string, "%u", tmsi);
|
|
|
|
return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
|
2008-12-27 21:34:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 16:46:00 +00:00
|
|
|
struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
|
|
|
|
const char *imsi)
|
2008-12-27 21:34:06 +00:00
|
|
|
{
|
2009-01-01 18:02:05 +00:00
|
|
|
struct gsm_subscriber *subscr;
|
2008-12-27 21:34:06 +00:00
|
|
|
|
2009-06-12 05:08:13 +00:00
|
|
|
llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
|
2009-01-01 18:02:05 +00:00
|
|
|
if (strcmp(subscr->imsi, imsi) == 0)
|
|
|
|
return subscr_get(subscr);
|
2008-12-27 21:34:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 16:46:00 +00:00
|
|
|
return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
|
2008-12-23 20:25:15 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 16:46:00 +00:00
|
|
|
struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
|
|
|
|
const char *ext)
|
2009-02-09 23:39:20 +00:00
|
|
|
{
|
|
|
|
struct gsm_subscriber *subscr;
|
|
|
|
|
2009-06-12 05:08:13 +00:00
|
|
|
llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
|
2009-02-09 23:39:20 +00:00
|
|
|
if (strcmp(subscr->extension, ext) == 0)
|
|
|
|
return subscr_get(subscr);
|
|
|
|
}
|
|
|
|
|
2009-07-23 16:46:00 +00:00
|
|
|
return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
|
2009-02-09 23:39:20 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 14:03:15 +00:00
|
|
|
struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
|
|
|
|
unsigned long long id)
|
|
|
|
{
|
|
|
|
struct gsm_subscriber *subscr;
|
|
|
|
char buf[32];
|
|
|
|
sprintf(buf, "%llu", id);
|
|
|
|
|
2009-06-12 05:08:13 +00:00
|
|
|
llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
|
2009-08-08 14:03:15 +00:00
|
|
|
if (subscr->id == id)
|
|
|
|
return subscr_get(subscr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-12 05:37:29 +00:00
|
|
|
int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
|
2008-12-23 20:25:15 +00:00
|
|
|
{
|
2010-12-25 18:40:14 +00:00
|
|
|
int rc;
|
|
|
|
|
2009-04-12 05:37:29 +00:00
|
|
|
/* FIXME: Migrate pending requests from one BSC to another */
|
|
|
|
switch (reason) {
|
|
|
|
case GSM_SUBSCRIBER_UPDATE_ATTACHED:
|
2009-07-23 16:56:43 +00:00
|
|
|
s->net = bts->network;
|
2009-06-02 02:54:38 +00:00
|
|
|
/* Indicate "attached to LAC" */
|
|
|
|
s->lac = bts->location_area_code;
|
2009-12-24 12:48:14 +00:00
|
|
|
LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
|
2009-12-24 13:50:24 +00:00
|
|
|
subscr_name(s), s->lac);
|
2010-12-25 18:40:14 +00:00
|
|
|
rc = db_sync_subscriber(s);
|
|
|
|
db_subscriber_update(s);
|
2009-07-29 21:14:15 +00:00
|
|
|
dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
|
2009-04-12 05:37:29 +00:00
|
|
|
break;
|
|
|
|
case GSM_SUBSCRIBER_UPDATE_DETACHED:
|
2009-06-04 14:27:39 +00:00
|
|
|
/* Only detach if we are currently in this area */
|
|
|
|
if (bts->location_area_code == s->lac)
|
2009-10-01 02:07:15 +00:00
|
|
|
s->lac = GSM_LAC_RESERVED_DETACHED;
|
2009-12-24 13:50:24 +00:00
|
|
|
LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
|
2010-12-25 18:40:14 +00:00
|
|
|
rc = db_sync_subscriber(s);
|
|
|
|
db_subscriber_update(s);
|
2009-07-29 21:14:15 +00:00
|
|
|
dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
|
2009-04-12 05:37:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "subscr_update with unknown reason: %d\n",
|
|
|
|
reason);
|
2010-12-25 18:40:14 +00:00
|
|
|
rc = db_sync_subscriber(s);
|
|
|
|
db_subscriber_update(s);
|
2009-04-12 05:37:29 +00:00
|
|
|
break;
|
|
|
|
};
|
2010-12-25 18:40:14 +00:00
|
|
|
|
|
|
|
return rc;
|
2009-01-01 18:02:05 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 17:12:11 +00:00
|
|
|
void subscr_update_from_db(struct gsm_subscriber *sub)
|
|
|
|
{
|
|
|
|
db_subscriber_update(sub);
|
|
|
|
}
|