Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 1 | /* The concept of a subscriber for the MSC, roughly HLR/VLR functionality */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 2 | |
| 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 Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
Holger Freyther | 8bdf762 | 2009-04-18 13:48:55 +0200 | [diff] [blame] | 27 | #include <assert.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 28 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
| 30 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 31 | #include <openbsc/gsm_subscriber.h> |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 32 | #include <openbsc/gsm_04_08.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 34 | #include <openbsc/paging.h> |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 35 | #include <openbsc/signal.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | #include <openbsc/db.h> |
| 37 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 38 | void *tall_sub_req_ctx; |
| 39 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 40 | extern struct llist_head *subscr_bsc_active_subscriber(void); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 41 | |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 42 | int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq, |
| 43 | gsm_cbfn *cb, void *cb_data); |
| 44 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 45 | |
| 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 | */ |
| 52 | struct 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 Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 70 | static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event, |
| 71 | struct msgb *msg, void *data, void *param) |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 72 | { |
| 73 | struct subscr_request *request; |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 74 | struct gsm_subscriber_connection *conn = data; |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 75 | struct gsm_subscriber *subscr = param; |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 76 | struct paging_signal_data sig_data; |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 77 | |
| 78 | /* There is no request anymore... */ |
| 79 | if (llist_empty(&subscr->requests)) |
| 80 | return -1; |
| 81 | |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 82 | /* Dispatch signal */ |
| 83 | sig_data.subscr = subscr; |
| 84 | sig_data.bts = conn ? conn->bts : NULL; |
| 85 | sig_data.conn = conn; |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 86 | sig_data.paging_result = event; |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 87 | dispatch_signal( |
| 88 | SS_PAGING, |
| 89 | event == GSM_PAGING_SUCCEEDED ? |
| 90 | S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, |
| 91 | &sig_data |
| 92 | ); |
| 93 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 94 | /* |
| 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 Freyther | bffd485 | 2010-12-28 17:27:05 +0100 | [diff] [blame^] | 106 | if (event != GSM_PAGING_SUCCEEDED) |
| 107 | subscr_put_channel(subscr); |
| 108 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 109 | subscr_put(subscr); |
| 110 | talloc_free(request); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 114 | static 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 | |
| 142 | static 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 Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 162 | static 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 Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 175 | subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY, |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 176 | NULL, NULL, subscr); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void 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 Freyther | 000571f | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 214 | void subscr_put_channel(struct gsm_subscriber *subscr) |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 215 | { |
| 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 Freyther | 000571f | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 234 | if (subscr && !llist_empty(&subscr->requests)) |
| 235 | subscr_send_paging_request(subscr); |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 239 | struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net, |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 240 | u_int32_t tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 241 | { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 242 | char tmsi_string[14]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 243 | struct gsm_subscriber *subscr; |
| 244 | |
| 245 | /* we might have a record in memory already */ |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 246 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 247 | if (tmsi == subscr->tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 248 | return subscr_get(subscr); |
| 249 | } |
| 250 | |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 251 | sprintf(tmsi_string, "%u", tmsi); |
| 252 | return db_get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 253 | } |
| 254 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 255 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net, |
| 256 | const char *imsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 257 | { |
| 258 | struct gsm_subscriber *subscr; |
| 259 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 260 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 261 | if (strcmp(subscr->imsi, imsi) == 0) |
| 262 | return subscr_get(subscr); |
| 263 | } |
| 264 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 265 | return db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 266 | } |
| 267 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 268 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net, |
| 269 | const char *ext) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 270 | { |
| 271 | struct gsm_subscriber *subscr; |
| 272 | |
Holger Hans Peter Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 273 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 274 | if (strcmp(subscr->extension, ext) == 0) |
| 275 | return subscr_get(subscr); |
| 276 | } |
| 277 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 278 | return db_get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 281 | struct 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 Freyther | 9a23af6 | 2009-06-12 07:08:13 +0200 | [diff] [blame] | 288 | llist_for_each_entry(subscr, subscr_bsc_active_subscriber(), entry) { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 289 | 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 297 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
| 298 | { |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 299 | int rc; |
| 300 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 301 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 302 | switch (reason) { |
| 303 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Harald Welte | c2189a6 | 2009-07-23 18:56:43 +0200 | [diff] [blame] | 304 | s->net = bts->network; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 305 | /* Indicate "attached to LAC" */ |
| 306 | s->lac = bts->location_area_code; |
Harald Welte | f00c2ac | 2009-12-24 13:48:14 +0100 | [diff] [blame] | 307 | LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n", |
Harald Welte | 0747c6a | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 308 | subscr_name(s), s->lac); |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 309 | rc = db_sync_subscriber(s); |
| 310 | db_subscriber_update(s); |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 311 | dispatch_signal(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 312 | 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 Freyther | 6c6ab86 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 316 | s->lac = GSM_LAC_RESERVED_DETACHED; |
Harald Welte | 0747c6a | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 317 | LOGP(DMM, LOGL_INFO, "Subscriber %s DETACHED\n", subscr_name(s)); |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 318 | rc = db_sync_subscriber(s); |
| 319 | db_subscriber_update(s); |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 320 | dispatch_signal(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 321 | break; |
| 322 | default: |
| 323 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 324 | reason); |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 325 | rc = db_sync_subscriber(s); |
| 326 | db_subscriber_update(s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 327 | break; |
| 328 | }; |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 329 | |
| 330 | return rc; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 333 | void subscr_update_from_db(struct gsm_subscriber *sub) |
| 334 | { |
| 335 | db_subscriber_update(sub); |
| 336 | } |