blob: dc7d638f18de3f4802384ae10c95f374d8582708 [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
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welte75a983f2008-12-27 21:34:06 +000024#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther85a7b362009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Sylvain Munaut1e245502010-12-01 21:07:29 +010030#include <osmocore/talloc.h>
31
Harald Welte8470bf22008-12-25 23:28:35 +000032#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010033#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000034#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010035#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020036#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000037#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000038
Sylvain Munaut1e245502010-12-01 21:07:29 +010039void *tall_sub_req_ctx;
40
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020041extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte75a983f2008-12-27 21:34:06 +000042
Sylvain Munaut5a86e062010-12-01 22:38:03 +010043int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
44 gsm_cbfn *cb, void *cb_data);
45
Sylvain Munaut1e245502010-12-01 21:07:29 +010046
47/*
48 * Struct for pending channel requests. This is managed in the
49 * llist_head requests of each subscriber. The reference counting
50 * should work in such a way that a subscriber with a pending request
51 * remains in memory.
52 */
53struct subscr_request {
54 struct llist_head entry;
55
56 /* back reference */
57 struct gsm_subscriber *subscr;
58
59 /* the requested channel type */
60 int channel_type;
61
62 /* the callback data */
63 gsm_cbfn *cbfn;
64 void *param;
65};
66
67/*
68 * We got the channel assigned and can now hand this channel
69 * over to one of our callbacks.
70 */
Sylvain Munaut5a86e062010-12-01 22:38:03 +010071static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
72 struct msgb *msg, void *data, void *param)
Sylvain Munaut1e245502010-12-01 21:07:29 +010073{
74 struct subscr_request *request;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010075 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut5a86e062010-12-01 22:38:03 +010076 struct gsm_subscriber *subscr = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010077 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +010078
79 /* There is no request anymore... */
80 if (llist_empty(&subscr->requests))
81 return -1;
82
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010083 /* Dispatch signal */
84 sig_data.subscr = subscr;
85 sig_data.bts = conn ? conn->bts : NULL;
86 sig_data.conn = conn;
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +010087 sig_data.paging_result = event;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010088 dispatch_signal(
89 SS_PAGING,
90 event == GSM_PAGING_SUCCEEDED ?
91 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
92 &sig_data
93 );
94
Sylvain Munaut1e245502010-12-01 21:07:29 +010095 /*
96 * FIXME: What to do with paging requests coming during
97 * this callback? We must be sure to not start paging when
98 * we have an active connection to a subscriber and to make
99 * the subscr_put_channel work as required...
100 */
101 request = (struct subscr_request *)subscr->requests.next;
102 llist_del(&request->entry);
103 subscr->in_callback = 1;
104 request->cbfn(hooknum, event, msg, data, request->param);
105 subscr->in_callback = 0;
106
107 subscr_put(subscr);
108 talloc_free(request);
109 return 0;
110}
111
Sylvain Munaut5a86e062010-12-01 22:38:03 +0100112static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
113 struct msgb *msg, void *data, void *param)
114{
115 int rc;
116
117 switch (event) {
118 case GSM_SECURITY_AUTH_FAILED:
119 /* Dispatch as paging failure */
120 rc = subscr_paging_dispatch(
121 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
122 msg, data, param);
123 break;
124
125 case GSM_SECURITY_NOAVAIL:
126 case GSM_SECURITY_SUCCEEDED:
127 /* Dispatch as paging failure */
128 rc = subscr_paging_dispatch(
129 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
130 msg, data, param);
131 break;
132
133 default:
134 rc = -EINVAL;
135 }
136
137 return rc;
138}
139
140static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
141 struct msgb *msg, void *data, void *param)
142{
143 struct gsm_subscriber_connection *conn = data;
144 struct gsm48_hdr *gh;
145 struct gsm48_pag_resp *pr;
146
147 /* Other cases mean problem, dispatch direclty */
148 if (event != GSM_PAGING_SUCCEEDED)
149 return subscr_paging_dispatch(hooknum, event, msg, data, param);
150
151 /* Get paging response */
152 gh = msgb_l3(msg);
153 pr = (struct gsm48_pag_resp *)gh->data;
154
155 /* We _really_ have a channel, secure it now ! */
156 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
157}
158
159
Sylvain Munaut1e245502010-12-01 21:07:29 +0100160static void subscr_send_paging_request(struct gsm_subscriber *subscr)
161{
162 struct subscr_request *request;
163 int rc;
164
165 assert(!llist_empty(&subscr->requests));
166
167 request = (struct subscr_request *)subscr->requests.next;
168 rc = paging_request(subscr->net, subscr, request->channel_type,
169 subscr_paging_cb, subscr);
170
171 /* paging failed, quit now */
172 if (rc <= 0) {
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100173 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100174 NULL, NULL, subscr);
175 }
176}
177
178void subscr_get_channel(struct gsm_subscriber *subscr,
179 int type, gsm_cbfn *cbfn, void *param)
180{
181 struct subscr_request *request;
182
183 request = talloc(tall_sub_req_ctx, struct subscr_request);
184 if (!request) {
185 if (cbfn)
186 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
187 NULL, NULL, param);
188 return;
189 }
190
191 memset(request, 0, sizeof(*request));
192 request->subscr = subscr_get(subscr);
193 request->channel_type = type;
194 request->cbfn = cbfn;
195 request->param = param;
196
197 /*
198 * FIXME: We might be able to assign more than one
199 * channel, e.g. voice and SMS submit at the same
200 * time.
201 */
202 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
203 /* add to the list, send a request */
204 llist_add_tail(&request->entry, &subscr->requests);
205 subscr_send_paging_request(subscr);
206 } else {
207 /* this will be picked up later, from subscr_put_channel */
208 llist_add_tail(&request->entry, &subscr->requests);
209 }
210}
211
212void subscr_put_channel(struct gsm_subscriber_connection *conn)
213{
214 /*
215 * FIXME: Continue with other requests now... by checking
216 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
217 * of the lchan after having asked the next requestee to handle
218 * the channel.
219 */
220 /*
221 * FIXME: is the lchan is of a different type we could still
222 * issue an immediate assignment for another channel and then
223 * close this one.
224 */
225 /*
226 * Currently we will drop the last ref of the lchan which
227 * will result in a channel release on RSL and we will start
228 * the paging. This should work most of the time as the MS
229 * will listen to the paging requests before we timeout
230 */
231
232 if (conn->subscr && !llist_empty(&conn->subscr->requests))
233 subscr_send_paging_request(conn->subscr);
234}
235
236
Harald Welte9176bd42009-07-23 18:46:00 +0200237struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200238 u_int32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000239{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200240 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +0000241 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000242
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243 /* we might have a record in memory already */
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200244 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200245 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000246 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000247 }
248
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200249 sprintf(tmsi_string, "%u", tmsi);
250 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +0000251}
252
Harald Welte9176bd42009-07-23 18:46:00 +0200253struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
254 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000255{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000257
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200258 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259 if (strcmp(subscr->imsi, imsi) == 0)
260 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000261 }
262
Harald Welte9176bd42009-07-23 18:46:00 +0200263 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000264}
265
Harald Welte9176bd42009-07-23 18:46:00 +0200266struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
267 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000268{
269 struct gsm_subscriber *subscr;
270
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200271 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +0000272 if (strcmp(subscr->extension, ext) == 0)
273 return subscr_get(subscr);
274 }
275
Harald Welte9176bd42009-07-23 18:46:00 +0200276 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000277}
278
Harald Welte76042182009-08-08 16:03:15 +0200279struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
280 unsigned long long id)
281{
282 struct gsm_subscriber *subscr;
283 char buf[32];
284 sprintf(buf, "%llu", id);
285
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200286 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte76042182009-08-08 16:03:15 +0200287 if (subscr->id == id)
288 return subscr_get(subscr);
289 }
290
291 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
292}
293
294
Holger Freyther4a49e772009-04-12 05:37:29 +0000295int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000296{
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100297 int rc;
298
Holger Freyther4a49e772009-04-12 05:37:29 +0000299 /* FIXME: Migrate pending requests from one BSC to another */
300 switch (reason) {
301 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200302 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000303 /* Indicate "attached to LAC" */
304 s->lac = bts->location_area_code;
Harald Welte77563da2009-12-24 13:48:14 +0100305 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte2e6d4682009-12-24 14:50:24 +0100306 subscr_name(s), s->lac);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100307 rc = db_sync_subscriber(s);
308 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200309 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000310 break;
311 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000312 /* Only detach if we are currently in this area */
313 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200314 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100315 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100316 rc = db_sync_subscriber(s);
317 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200318 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000319 break;
320 default:
321 fprintf(stderr, "subscr_update with unknown reason: %d\n",
322 reason);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100323 rc = db_sync_subscriber(s);
324 db_subscriber_update(s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000325 break;
326 };
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100327
328 return rc;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000329}
330
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100331void subscr_update_from_db(struct gsm_subscriber *sub)
332{
333 db_subscriber_update(sub);
334}