blob: 3e3e74a2b951dfb3775511f2bcf57462d9be7f3b [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>
Harald Welte59b04682009-06-10 05:40:52 +080028
Sylvain Munaut34619752010-12-01 21:07:29 +010029#include <osmocore/talloc.h>
30
Harald Welte59b04682009-06-10 05:40:52 +080031#include <openbsc/gsm_subscriber.h>
Sylvain Munaut65c51b62010-12-01 22:38:03 +010032#include <openbsc/gsm_04_08.h>
Harald Welte59b04682009-06-10 05:40:52 +080033#include <openbsc/debug.h>
Sylvain Munaut34619752010-12-01 21:07:29 +010034#include <openbsc/paging.h>
Harald Welteb7799832009-07-29 23:14:15 +020035#include <openbsc/signal.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/db.h>
37
Sylvain Munaut34619752010-12-01 21:07:29 +010038void *tall_sub_req_ctx;
39
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020040extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte59b04682009-06-10 05:40:52 +080041
Sylvain Munaut65c51b62010-12-01 22:38:03 +010042int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
43 gsm_cbfn *cb, void *cb_data);
44
Sylvain Munaut34619752010-12-01 21:07:29 +010045
46/*
47 * Struct for pending channel requests. This is managed in the
48 * llist_head requests of each subscriber. The reference counting
49 * should work in such a way that a subscriber with a pending request
50 * remains in memory.
51 */
52struct subscr_request {
53 struct llist_head entry;
54
55 /* back reference */
56 struct gsm_subscriber *subscr;
57
58 /* the requested channel type */
59 int channel_type;
60
61 /* the callback data */
62 gsm_cbfn *cbfn;
63 void *param;
64};
65
66/*
67 * We got the channel assigned and can now hand this channel
68 * over to one of our callbacks.
69 */
Sylvain Munaut65c51b62010-12-01 22:38:03 +010070static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event,
71 struct msgb *msg, void *data, void *param)
Sylvain Munaut34619752010-12-01 21:07:29 +010072{
73 struct subscr_request *request;
Sylvain Munaut7a8f8812010-12-01 23:04:03 +010074 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut65c51b62010-12-01 22:38:03 +010075 struct gsm_subscriber *subscr = param;
Sylvain Munaut7a8f8812010-12-01 23:04:03 +010076 struct paging_signal_data sig_data;
Sylvain Munaut34619752010-12-01 21:07:29 +010077
78 /* There is no request anymore... */
79 if (llist_empty(&subscr->requests))
80 return -1;
81
Sylvain Munaut7a8f8812010-12-01 23:04:03 +010082 /* Dispatch signal */
83 sig_data.subscr = subscr;
84 sig_data.bts = conn ? conn->bts : NULL;
85 sig_data.conn = conn;
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +010086 sig_data.paging_result = event;
Sylvain Munaut7a8f8812010-12-01 23:04:03 +010087 dispatch_signal(
88 SS_PAGING,
89 event == GSM_PAGING_SUCCEEDED ?
90 S_PAGING_SUCCEEDED : S_PAGING_EXPIRED,
91 &sig_data
92 );
93
Sylvain Munaut34619752010-12-01 21:07:29 +010094 /*
95 * FIXME: What to do with paging requests coming during
96 * this callback? We must be sure to not start paging when
97 * we have an active connection to a subscriber and to make
98 * the subscr_put_channel work as required...
99 */
100 request = (struct subscr_request *)subscr->requests.next;
101 llist_del(&request->entry);
102 subscr->in_callback = 1;
103 request->cbfn(hooknum, event, msg, data, request->param);
104 subscr->in_callback = 0;
105
Holger Hans Peter Freytherbffd4852010-12-28 17:27:05 +0100106 if (event != GSM_PAGING_SUCCEEDED)
107 subscr_put_channel(subscr);
108
Sylvain Munaut34619752010-12-01 21:07:29 +0100109 subscr_put(subscr);
110 talloc_free(request);
111 return 0;
112}
113
Sylvain Munaut65c51b62010-12-01 22:38:03 +0100114static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
115 struct msgb *msg, void *data, void *param)
116{
117 int rc;
118
119 switch (event) {
120 case GSM_SECURITY_AUTH_FAILED:
121 /* Dispatch as paging failure */
122 rc = subscr_paging_dispatch(
123 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
124 msg, data, param);
125 break;
126
127 case GSM_SECURITY_NOAVAIL:
128 case GSM_SECURITY_SUCCEEDED:
129 /* Dispatch as paging failure */
130 rc = subscr_paging_dispatch(
131 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
132 msg, data, param);
133 break;
134
135 default:
136 rc = -EINVAL;
137 }
138
139 return rc;
140}
141
142static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
143 struct msgb *msg, void *data, void *param)
144{
145 struct gsm_subscriber_connection *conn = data;
146 struct gsm48_hdr *gh;
147 struct gsm48_pag_resp *pr;
148
149 /* Other cases mean problem, dispatch direclty */
150 if (event != GSM_PAGING_SUCCEEDED)
151 return subscr_paging_dispatch(hooknum, event, msg, data, param);
152
153 /* Get paging response */
154 gh = msgb_l3(msg);
155 pr = (struct gsm48_pag_resp *)gh->data;
156
157 /* We _really_ have a channel, secure it now ! */
158 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
159}
160
161
Sylvain Munaut34619752010-12-01 21:07:29 +0100162static void subscr_send_paging_request(struct gsm_subscriber *subscr)
163{
164 struct subscr_request *request;
165 int rc;
166
167 assert(!llist_empty(&subscr->requests));
168
169 request = (struct subscr_request *)subscr->requests.next;
170 rc = paging_request(subscr->net, subscr, request->channel_type,
171 subscr_paging_cb, subscr);
172
173 /* paging failed, quit now */
174 if (rc <= 0) {
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +0100175 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut34619752010-12-01 21:07:29 +0100176 NULL, NULL, subscr);
177 }
178}
179
180void subscr_get_channel(struct gsm_subscriber *subscr,
181 int type, gsm_cbfn *cbfn, void *param)
182{
183 struct subscr_request *request;
184
185 request = talloc(tall_sub_req_ctx, struct subscr_request);
186 if (!request) {
187 if (cbfn)
188 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
189 NULL, NULL, param);
190 return;
191 }
192
193 memset(request, 0, sizeof(*request));
194 request->subscr = subscr_get(subscr);
195 request->channel_type = type;
196 request->cbfn = cbfn;
197 request->param = param;
198
199 /*
200 * FIXME: We might be able to assign more than one
201 * channel, e.g. voice and SMS submit at the same
202 * time.
203 */
204 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
205 /* add to the list, send a request */
206 llist_add_tail(&request->entry, &subscr->requests);
207 subscr_send_paging_request(subscr);
208 } else {
209 /* this will be picked up later, from subscr_put_channel */
210 llist_add_tail(&request->entry, &subscr->requests);
211 }
212}
213
Holger Hans Peter Freyther000571f2010-12-27 22:43:28 +0100214void subscr_put_channel(struct gsm_subscriber *subscr)
Sylvain Munaut34619752010-12-01 21:07:29 +0100215{
216 /*
217 * FIXME: Continue with other requests now... by checking
218 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
219 * of the lchan after having asked the next requestee to handle
220 * the channel.
221 */
222 /*
223 * FIXME: is the lchan is of a different type we could still
224 * issue an immediate assignment for another channel and then
225 * close this one.
226 */
227 /*
228 * Currently we will drop the last ref of the lchan which
229 * will result in a channel release on RSL and we will start
230 * the paging. This should work most of the time as the MS
231 * will listen to the paging requests before we timeout
232 */
233
Holger Hans Peter Freyther000571f2010-12-27 22:43:28 +0100234 if (subscr && !llist_empty(&subscr->requests))
235 subscr_send_paging_request(subscr);
Sylvain Munaut34619752010-12-01 21:07:29 +0100236}
237
238
Harald Welte75350412009-07-23 18:46:00 +0200239struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200240 u_int32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800241{
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200242 char tmsi_string[14];
Harald Welte59b04682009-06-10 05:40:52 +0800243 struct gsm_subscriber *subscr;
244
245 /* we might have a record in memory already */
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200246 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200247 if (tmsi == subscr->tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800248 return subscr_get(subscr);
249 }
250
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200251 sprintf(tmsi_string, "%u", tmsi);
252 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800253}
254
Harald Welte75350412009-07-23 18:46:00 +0200255struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
256 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +0800257{
258 struct gsm_subscriber *subscr;
259
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200260 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +0800261 if (strcmp(subscr->imsi, imsi) == 0)
262 return subscr_get(subscr);
263 }
264
Harald Welte75350412009-07-23 18:46:00 +0200265 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +0800266}
267
Harald Welte75350412009-07-23 18:46:00 +0200268struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
269 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +0800270{
271 struct gsm_subscriber *subscr;
272
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200273 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +0800274 if (strcmp(subscr->extension, ext) == 0)
275 return subscr_get(subscr);
276 }
277
Harald Welte75350412009-07-23 18:46:00 +0200278 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +0800279}
280
Harald Welte68b7df22009-08-08 16:03:15 +0200281struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
282 unsigned long long id)
283{
284 struct gsm_subscriber *subscr;
285 char buf[32];
286 sprintf(buf, "%llu", id);
287
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200288 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte68b7df22009-08-08 16:03:15 +0200289 if (subscr->id == id)
290 return subscr_get(subscr);
291 }
292
293 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
294}
295
296
Harald Welte59b04682009-06-10 05:40:52 +0800297int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
298{
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100299 int rc;
300
Harald Welte59b04682009-06-10 05:40:52 +0800301 /* FIXME: Migrate pending requests from one BSC to another */
302 switch (reason) {
303 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +0200304 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800305 /* Indicate "attached to LAC" */
306 s->lac = bts->location_area_code;
Harald Weltef00c2ac2009-12-24 13:48:14 +0100307 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte0747c6a2009-12-24 14:50:24 +0100308 subscr_name(s), s->lac);
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100309 rc = db_sync_subscriber(s);
310 db_subscriber_update(s);
Harald Welteb7799832009-07-29 23:14:15 +0200311 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800312 break;
313 case GSM_SUBSCRIBER_UPDATE_DETACHED:
314 /* Only detach if we are currently in this area */
315 if (bts->location_area_code == s->lac)
Holger Hans Peter Freyther6c6ab862009-10-01 04:07:15 +0200316 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte0747c6a2009-12-24 14:50:24 +0100317 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100318 rc = db_sync_subscriber(s);
319 db_subscriber_update(s);
Harald Welteb7799832009-07-29 23:14:15 +0200320 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800321 break;
322 default:
323 fprintf(stderr, "subscr_update with unknown reason: %d\n",
324 reason);
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100325 rc = db_sync_subscriber(s);
326 db_subscriber_update(s);
Harald Welte59b04682009-06-10 05:40:52 +0800327 break;
328 };
Holger Hans Peter Freyther52a63752010-12-25 19:40:14 +0100329
330 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800331}
332
Holger Hans Peter Freyther0c29e5f2010-12-22 18:12:11 +0100333void subscr_update_from_db(struct gsm_subscriber *sub)
334{
335 db_subscriber_update(sub);
336}