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