blob: bd76faec7f78ee31fbab3f508662e5ce25e14996 [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
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
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
Holger Freyther8bdf7622009-04-18 13:48:55 +020028#include <assert.h>
Harald Welte59b04682009-06-10 05:40:52 +080029
Sylvain Munaut34619752010-12-01 21:07:29 +010030#include <osmocore/talloc.h>
31
Harald Welte59b04682009-06-10 05:40:52 +080032#include <openbsc/gsm_subscriber.h>
Sylvain Munaut65c51b62010-12-01 22:38:03 +010033#include <openbsc/gsm_04_08.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/debug.h>
Sylvain Munaut34619752010-12-01 21:07:29 +010035#include <openbsc/paging.h>
Harald Welteb7799832009-07-29 23:14:15 +020036#include <openbsc/signal.h>
Harald Welte59b04682009-06-10 05:40:52 +080037#include <openbsc/db.h>
38
Sylvain Munaut34619752010-12-01 21:07:29 +010039void *tall_sub_req_ctx;
40
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +020041extern struct llist_head *subscr_bsc_active_subscriber(void);
Harald Welte59b04682009-06-10 05:40:52 +080042
Sylvain Munaut65c51b62010-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 Munaut34619752010-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 Munaut65c51b62010-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 Munaut34619752010-12-01 21:07:29 +010073{
74 struct subscr_request *request;
Sylvain Munaut65c51b62010-12-01 22:38:03 +010075 struct gsm_subscriber *subscr = param;
Sylvain Munaut34619752010-12-01 21:07:29 +010076
77 /* There is no request anymore... */
78 if (llist_empty(&subscr->requests))
79 return -1;
80
81 /*
82 * FIXME: What to do with paging requests coming during
83 * this callback? We must be sure to not start paging when
84 * we have an active connection to a subscriber and to make
85 * the subscr_put_channel work as required...
86 */
87 request = (struct subscr_request *)subscr->requests.next;
88 llist_del(&request->entry);
89 subscr->in_callback = 1;
90 request->cbfn(hooknum, event, msg, data, request->param);
91 subscr->in_callback = 0;
92
93 subscr_put(subscr);
94 talloc_free(request);
95 return 0;
96}
97
Sylvain Munaut65c51b62010-12-01 22:38:03 +010098static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event,
99 struct msgb *msg, void *data, void *param)
100{
101 int rc;
102
103 switch (event) {
104 case GSM_SECURITY_AUTH_FAILED:
105 /* Dispatch as paging failure */
106 rc = subscr_paging_dispatch(
107 GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
108 msg, data, param);
109 break;
110
111 case GSM_SECURITY_NOAVAIL:
112 case GSM_SECURITY_SUCCEEDED:
113 /* Dispatch as paging failure */
114 rc = subscr_paging_dispatch(
115 GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED,
116 msg, data, param);
117 break;
118
119 default:
120 rc = -EINVAL;
121 }
122
123 return rc;
124}
125
126static int subscr_paging_cb(unsigned int hooknum, unsigned int event,
127 struct msgb *msg, void *data, void *param)
128{
129 struct gsm_subscriber_connection *conn = data;
130 struct gsm48_hdr *gh;
131 struct gsm48_pag_resp *pr;
132
133 /* Other cases mean problem, dispatch direclty */
134 if (event != GSM_PAGING_SUCCEEDED)
135 return subscr_paging_dispatch(hooknum, event, msg, data, param);
136
137 /* Get paging response */
138 gh = msgb_l3(msg);
139 pr = (struct gsm48_pag_resp *)gh->data;
140
141 /* We _really_ have a channel, secure it now ! */
142 return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param);
143}
144
145
Sylvain Munaut34619752010-12-01 21:07:29 +0100146static void subscr_send_paging_request(struct gsm_subscriber *subscr)
147{
148 struct subscr_request *request;
149 int rc;
150
151 assert(!llist_empty(&subscr->requests));
152
153 request = (struct subscr_request *)subscr->requests.next;
154 rc = paging_request(subscr->net, subscr, request->channel_type,
155 subscr_paging_cb, subscr);
156
157 /* paging failed, quit now */
158 if (rc <= 0) {
159 subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED,
160 NULL, NULL, subscr);
161 }
162}
163
164void subscr_get_channel(struct gsm_subscriber *subscr,
165 int type, gsm_cbfn *cbfn, void *param)
166{
167 struct subscr_request *request;
168
169 request = talloc(tall_sub_req_ctx, struct subscr_request);
170 if (!request) {
171 if (cbfn)
172 cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM,
173 NULL, NULL, param);
174 return;
175 }
176
177 memset(request, 0, sizeof(*request));
178 request->subscr = subscr_get(subscr);
179 request->channel_type = type;
180 request->cbfn = cbfn;
181 request->param = param;
182
183 /*
184 * FIXME: We might be able to assign more than one
185 * channel, e.g. voice and SMS submit at the same
186 * time.
187 */
188 if (!subscr->in_callback && llist_empty(&subscr->requests)) {
189 /* add to the list, send a request */
190 llist_add_tail(&request->entry, &subscr->requests);
191 subscr_send_paging_request(subscr);
192 } else {
193 /* this will be picked up later, from subscr_put_channel */
194 llist_add_tail(&request->entry, &subscr->requests);
195 }
196}
197
198void subscr_put_channel(struct gsm_subscriber_connection *conn)
199{
200 /*
201 * FIXME: Continue with other requests now... by checking
202 * the gsm_subscriber inside the gsm_lchan. Drop the ref count
203 * of the lchan after having asked the next requestee to handle
204 * the channel.
205 */
206 /*
207 * FIXME: is the lchan is of a different type we could still
208 * issue an immediate assignment for another channel and then
209 * close this one.
210 */
211 /*
212 * Currently we will drop the last ref of the lchan which
213 * will result in a channel release on RSL and we will start
214 * the paging. This should work most of the time as the MS
215 * will listen to the paging requests before we timeout
216 */
217
218 if (conn->subscr && !llist_empty(&conn->subscr->requests))
219 subscr_send_paging_request(conn->subscr);
220}
221
222
Harald Welte75350412009-07-23 18:46:00 +0200223struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net,
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200224 u_int32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800225{
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200226 char tmsi_string[14];
Harald Welte59b04682009-06-10 05:40:52 +0800227 struct gsm_subscriber *subscr;
228
229 /* we might have a record in memory already */
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200230 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200231 if (tmsi == subscr->tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800232 return subscr_get(subscr);
233 }
234
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200235 sprintf(tmsi_string, "%u", tmsi);
236 return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800237}
238
Harald Welte75350412009-07-23 18:46:00 +0200239struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net,
240 const char *imsi)
Harald Welte59b04682009-06-10 05:40:52 +0800241{
242 struct gsm_subscriber *subscr;
243
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200244 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +0800245 if (strcmp(subscr->imsi, imsi) == 0)
246 return subscr_get(subscr);
247 }
248
Harald Welte75350412009-07-23 18:46:00 +0200249 return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Harald Welte59b04682009-06-10 05:40:52 +0800250}
251
Harald Welte75350412009-07-23 18:46:00 +0200252struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net,
253 const char *ext)
Harald Welte59b04682009-06-10 05:40:52 +0800254{
255 struct gsm_subscriber *subscr;
256
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200257 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte59b04682009-06-10 05:40:52 +0800258 if (strcmp(subscr->extension, ext) == 0)
259 return subscr_get(subscr);
260 }
261
Harald Welte75350412009-07-23 18:46:00 +0200262 return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext);
Harald Welte59b04682009-06-10 05:40:52 +0800263}
264
Harald Welte68b7df22009-08-08 16:03:15 +0200265struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net,
266 unsigned long long id)
267{
268 struct gsm_subscriber *subscr;
269 char buf[32];
270 sprintf(buf, "%llu", id);
271
Holger Hans Peter Freyther9a23af62009-06-12 07:08:13 +0200272 llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) {
Harald Welte68b7df22009-08-08 16:03:15 +0200273 if (subscr->id == id)
274 return subscr_get(subscr);
275 }
276
277 return db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
278}
279
280
Harald Welte59b04682009-06-10 05:40:52 +0800281int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason)
282{
283 /* FIXME: Migrate pending requests from one BSC to another */
284 switch (reason) {
285 case GSM_SUBSCRIBER_UPDATE_ATTACHED:
Harald Weltec2189a62009-07-23 18:56:43 +0200286 s->net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800287 /* Indicate "attached to LAC" */
288 s->lac = bts->location_area_code;
Harald Weltef00c2ac2009-12-24 13:48:14 +0100289 LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n",
Harald Welte0747c6a2009-12-24 14:50:24 +0100290 subscr_name(s), s->lac);
Harald Welteb7799832009-07-29 23:14:15 +0200291 dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800292 break;
293 case GSM_SUBSCRIBER_UPDATE_DETACHED:
294 /* Only detach if we are currently in this area */
295 if (bts->location_area_code == s->lac)
Holger Hans Peter Freyther6c6ab862009-10-01 04:07:15 +0200296 s->lac = GSM_LAC_RESERVED_DETACHED;
Harald Welte0747c6a2009-12-24 14:50:24 +0100297 LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s));
Harald Welteb7799832009-07-29 23:14:15 +0200298 dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s);
Harald Welte59b04682009-06-10 05:40:52 +0800299 break;
300 default:
301 fprintf(stderr, "subscr_update with unknown reason: %d\n",
302 reason);
303 break;
304 };
305 return db_sync_subscriber(s);
306}
307
Harald Welte59b04682009-06-10 05:40:52 +0800308