blob: e44029c3066c669684e2b8878d2c3662e0c084fc [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>
Harald Welte52b1f982008-12-23 20:25:15 +000028
Sylvain Munaut1e245502010-12-01 21:07:29 +010029#include <osmocore/talloc.h>
30
Harald Welte8470bf22008-12-25 23:28:35 +000031#include <openbsc/gsm_subscriber.h>
Sylvain Munaut5a86e062010-12-01 22:38:03 +010032#include <openbsc/gsm_04_08.h>
Holger Freyther3e0ef7c2009-06-02 02:54:48 +000033#include <openbsc/debug.h>
Sylvain Munaut1e245502010-12-01 21:07:29 +010034#include <openbsc/paging.h>
Harald Welteea5cf302009-07-29 23:14:15 +020035#include <openbsc/signal.h>
Harald Welte75a983f2008-12-27 21:34:06 +000036#include <openbsc/db.h>
Harald Welte52b1f982008-12-23 20:25:15 +000037
Sylvain Munaut1e245502010-12-01 21:07:29 +010038void *tall_sub_req_ctx;
39
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +020040extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte75a983f2008-12-27 21:34:06 +000041
Sylvain Munaut5a86e062010-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 Munaut1e245502010-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 Munaut5a86e062010-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 Munaut1e245502010-12-01 21:07:29 +010072{
73 struct subscr_request *request;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010074 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut5a86e062010-12-01 22:38:03 +010075 struct gsm_subscriber *subscr = param;
Sylvain Munaut8a31a3f2010-12-01 23:04:03 +010076 struct paging_signal_data sig_data;
Sylvain Munaut1e245502010-12-01 21:07:29 +010077
78 /* There is no request anymore... */
79 if (llist_empty(&subscr->requests))
80 return -1;
81
Sylvain Munaut8a31a3f2010-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 Freytherd3baf412010-12-23 18:19:17 +010086 sig_data.paging_result = event;
Sylvain Munaut8a31a3f2010-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 Munaut1e245502010-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
106 subscr_put(subscr);
107 talloc_free(request);
108 return 0;
109}
110
Sylvain Munaut5a86e062010-12-01 22:38:03 +0100111static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
112 struct msgb *msg, void *data, void *param)
113{
114 int rc;
115
116 switch (event) {
117 case GSM_SECURITY_AUTH_FAILED:
118 /* Dispatch as paging failure */
119 rc = subscr_paging_dispatch(
120 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
121 msg, data, param);
122 break;
123
124 case GSM_SECURITY_NOAVAIL:
125 case GSM_SECURITY_SUCCEEDED:
126 /* Dispatch as paging failure */
127 rc = subscr_paging_dispatch(
128 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
129 msg, data, param);
130 break;
131
132 default:
133 rc = -EINVAL;
134 }
135
136 return rc;
137}
138
139static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
140 struct msgb *msg, void *data, void *param)
141{
142 struct gsm_subscriber_connection *conn = data;
143 struct gsm48_hdr *gh;
144 struct gsm48_pag_resp *pr;
145
146 /* Other cases mean problem, dispatch direclty */
147 if (event != GSM_PAGING_SUCCEEDED)
148 return subscr_paging_dispatch(hooknum, event, msg, data, param);
149
150 /* Get paging response */
151 gh = msgb_l3(msg);
152 pr = (struct gsm48_pag_resp *)gh->data;
153
154 /* We _really_ have a channel, secure it now ! */
155 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
156}
157
158
Sylvain Munaut1e245502010-12-01 21:07:29 +0100159static void subscr_send_paging_request(struct gsm_subscriber *subscr)
160{
161 struct subscr_request *request;
162 int rc;
163
164 assert(!llist_empty(&subscr->requests));
165
166 request = (struct subscr_request *)subscr->requests.next;
167 rc = paging_request(subscr->net, subscr, request->channel_type,
168 subscr_paging_cb, subscr);
169
170 /* paging failed, quit now */
171 if (rc <= 0) {
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +0100172 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY,
Sylvain Munaut1e245502010-12-01 21:07:29 +0100173 NULL, NULL, subscr);
174 }
175}
176
177void subscr_get_channel(struct gsm_subscriber *subscr,
178 int type, gsm_cbfn *cbfn, void *param)
179{
180 struct subscr_request *request;
181
182 request = talloc(tall_sub_req_ctx, struct subscr_request);
183 if (!request) {
184 if (cbfn)
185 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
186 NULL, NULL, param);
187 return;
188 }
189
190 memset(request, 0, sizeof(*request));
191 request->subscr = subscr_get(subscr);
192 request->channel_type = type;
193 request->cbfn = cbfn;
194 request->param = param;
195
196 /*
197 * FIXME: We might be able to assign more than one
198 * channel, e.g. voice and SMS submit at the same
199 * time.
200 */
201 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
202 /* add to the list, send a request */
203 llist_add_tail(&request->entry, &subscr->requests);
204 subscr_send_paging_request(subscr);
205 } else {
206 /* this will be picked up later, from subscr_put_channel */
207 llist_add_tail(&request->entry, &subscr->requests);
208 }
209}
210
211void subscr_put_channel(struct gsm_subscriber_connection *conn)
212{
213 /*
214 * FIXME: Continue with other requests now... by checking
215 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
216 * of the lchan after having asked the next requestee to handle
217 * the channel.
218 */
219 /*
220 * FIXME: is the lchan is of a different type we could still
221 * issue an immediate assignment for another channel and then
222 * close this one.
223 */
224 /*
225 * Currently we will drop the last ref of the lchan which
226 * will result in a channel release on RSL and we will start
227 * the paging. This should work most of the time as the MS
228 * will listen to the paging requests before we timeout
229 */
230
231 if (conn->subscr && !llist_empty(&conn->subscr->requests))
232 subscr_send_paging_request(conn->subscr);
233}
234
235
Harald Welte9176bd42009-07-23 18:46:00 +0200236struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200237 u_int32_t tmsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000238{
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200239 char tmsi_string[14];
Holger Freyther12aa50d2009-01-01 18:02:05 +0000240 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000241
Holger Freyther12aa50d2009-01-01 18:02:05 +0000242 /* we might have a record in memory already */
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200243 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200244 if (tmsi == subscr->tmsi)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000245 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000246 }
247
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200248 sprintf(tmsi_string, "%u", tmsi);
249 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte75a983f2008-12-27 21:34:06 +0000250}
251
Harald Welte9176bd42009-07-23 18:46:00 +0200252struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
253 const char *imsi)
Harald Welte75a983f2008-12-27 21:34:06 +0000254{
Holger Freyther12aa50d2009-01-01 18:02:05 +0000255 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000256
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200257 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther12aa50d2009-01-01 18:02:05 +0000258 if (strcmp(subscr->imsi, imsi) == 0)
259 return subscr_get(subscr);
Harald Welte75a983f2008-12-27 21:34:06 +0000260 }
261
Harald Welte9176bd42009-07-23 18:46:00 +0200262 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000263}
264
Harald Welte9176bd42009-07-23 18:46:00 +0200265struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
266 const char *ext)
Holger Freyther9c564b82009-02-09 23:39:20 +0000267{
268 struct gsm_subscriber *subscr;
269
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200270 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Freyther9c564b82009-02-09 23:39:20 +0000271 if (strcmp(subscr->extension, ext) == 0)
272 return subscr_get(subscr);
273 }
274
Harald Welte9176bd42009-07-23 18:46:00 +0200275 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Holger Freyther9c564b82009-02-09 23:39:20 +0000276}
277
Harald Welte76042182009-08-08 16:03:15 +0200278struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
279 unsigned long long id)
280{
281 struct gsm_subscriber *subscr;
282 char buf[32];
283 sprintf(buf, "%llu", id);
284
Holger Hans Peter Freyther857e5e62009-06-12 07:08:13 +0200285 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte76042182009-08-08 16:03:15 +0200286 if (subscr->id == id)
287 return subscr_get(subscr);
288 }
289
290 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
291}
292
293
Holger Freyther4a49e772009-04-12 05:37:29 +0000294int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
Harald Welte52b1f982008-12-23 20:25:15 +0000295{
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100296 int rc;
297
Holger Freyther4a49e772009-04-12 05:37:29 +0000298 /* FIXME: Migrate pending requests from one BSC to another */
299 switch (reason) {
300 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltedcaf5652009-07-23 18:56:43 +0200301 s->net = bts->network;
Holger Freyther6d5200b2009-06-02 02:54:38 +0000302 /* Indicate "attached to LAC" */
303 s->lac = bts->location_area_code;
Harald Welte77563da2009-12-24 13:48:14 +0100304 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte2e6d4682009-12-24 14:50:24 +0100305 subscr_name(s), s->lac);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100306 rc = db_sync_subscriber(s);
307 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200308 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000309 break;
310 case GSM_SUBSCRIBER_UPDATE_DETACHED:
Holger Freytherc3d4b2d2009-06-04 14:27:39 +0000311 /* Only detach if we are currently in this area */
312 if (bts->location_area_code == s->lac)
Holger Hans Peter Freythere48b9562009-10-01 04:07:15 +0200313 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte2e6d4682009-12-24 14:50:24 +0100314 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100315 rc = db_sync_subscriber(s);
316 db_subscriber_update(s);
Harald Welteea5cf302009-07-29 23:14:15 +0200317 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000318 break;
319 default:
320 fprintf(stderr, "subscr_update with unknown reason: %d\n",
321 reason);
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100322 rc = db_sync_subscriber(s);
323 db_subscriber_update(s);
Holger Freyther4a49e772009-04-12 05:37:29 +0000324 break;
325 };
Holger Hans Peter Freythere25445b2010-12-25 19:40:14 +0100326
327 return rc;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000328}
329
Holger Hans Peter Freytherabd0cac2010-12-22 18:12:11 +0100330void subscr_update_from_db(struct gsm_subscriber *sub)
331{
332 db_subscriber_update(sub);
333}