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> |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 4 | * (C) 2009,2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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> |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 28 | #include <time.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 30 | #include <osmocom/core/talloc.h> |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | 0fe0c8c | 2010-12-29 11:07:22 +0100 | [diff] [blame] | 32 | #include <osmocom/vty/vty.h> |
| 33 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 34 | #include <openbsc/gsm_subscriber.h> |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 35 | #include <openbsc/gsm_04_08.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | #include <openbsc/debug.h> |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 37 | #include <openbsc/paging.h> |
Harald Welte | b779983 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 38 | #include <openbsc/signal.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 39 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 40 | #include <openbsc/chan_alloc.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 41 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 42 | void *tall_sub_req_ctx; |
| 43 | |
Jan Luebbe | 8f7f1d1 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 44 | extern struct llist_head *subscr_bsc_active_subscribers(void); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 45 | |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 46 | int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq, |
| 47 | gsm_cbfn *cb, void *cb_data); |
| 48 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Struct for pending channel requests. This is managed in the |
| 52 | * llist_head requests of each subscriber. The reference counting |
| 53 | * should work in such a way that a subscriber with a pending request |
| 54 | * remains in memory. |
| 55 | */ |
| 56 | struct subscr_request { |
| 57 | struct llist_head entry; |
| 58 | |
| 59 | /* back reference */ |
| 60 | struct gsm_subscriber *subscr; |
| 61 | |
| 62 | /* the requested channel type */ |
| 63 | int channel_type; |
| 64 | |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 65 | /* what did we do */ |
| 66 | int state; |
| 67 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 68 | /* the callback data */ |
| 69 | gsm_cbfn *cbfn; |
| 70 | void *param; |
| 71 | }; |
| 72 | |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 73 | enum { |
| 74 | REQ_STATE_INITIAL, |
| 75 | REQ_STATE_QUEUED, |
| 76 | REQ_STATE_PAGED, |
| 77 | REQ_STATE_FAILED_START, |
| 78 | REQ_STATE_DISPATCHED, |
| 79 | }; |
| 80 | |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 81 | static struct gsm_subscriber *get_subscriber(struct gsm_network *net, |
| 82 | int type, const char *ident) |
| 83 | { |
| 84 | struct gsm_subscriber *subscr = db_get_subscriber(type, ident); |
| 85 | if (subscr) |
| 86 | subscr->net = net; |
| 87 | return subscr; |
| 88 | } |
| 89 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 90 | /* |
| 91 | * We got the channel assigned and can now hand this channel |
| 92 | * over to one of our callbacks. |
| 93 | */ |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 94 | static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event, |
| 95 | struct msgb *msg, void *data, void *param) |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 96 | { |
| 97 | struct subscr_request *request; |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 98 | struct gsm_subscriber_connection *conn = data; |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 99 | struct gsm_subscriber *subscr = param; |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 100 | struct paging_signal_data sig_data; |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 101 | |
| 102 | /* There is no request anymore... */ |
| 103 | if (llist_empty(&subscr->requests)) |
| 104 | return -1; |
| 105 | |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 106 | /* Dispatch signal */ |
| 107 | sig_data.subscr = subscr; |
| 108 | sig_data.bts = conn ? conn->bts : NULL; |
| 109 | sig_data.conn = conn; |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 110 | sig_data.paging_result = event; |
Pablo Neira Ayuso | ef717c6 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 111 | osmo_signal_dispatch( |
Sylvain Munaut | 7a8f881 | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 112 | SS_PAGING, |
| 113 | event == GSM_PAGING_SUCCEEDED ? |
| 114 | S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, |
| 115 | &sig_data |
| 116 | ); |
| 117 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 118 | /* |
| 119 | * FIXME: What to do with paging requests coming during |
| 120 | * this callback? We must be sure to not start paging when |
| 121 | * we have an active connection to a subscriber and to make |
| 122 | * the subscr_put_channel work as required... |
| 123 | */ |
| 124 | request = (struct subscr_request *)subscr->requests.next; |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 125 | request->state = REQ_STATE_DISPATCHED; |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 126 | llist_del(&request->entry); |
| 127 | subscr->in_callback = 1; |
| 128 | request->cbfn(hooknum, event, msg, data, request->param); |
| 129 | subscr->in_callback = 0; |
| 130 | |
Holger Hans Peter Freyther | a32e9c1 | 2010-12-28 17:47:43 +0100 | [diff] [blame] | 131 | if (event != GSM_PAGING_SUCCEEDED) { |
| 132 | /* |
| 133 | * This is a workaround for a bigger issue. We have |
| 134 | * issued paging that might involve multiple BTSes |
| 135 | * and one of them have failed now. We will stop the |
| 136 | * other paging requests as well as the next timeout |
| 137 | * would work on the next paging request and the queue |
| 138 | * will do bad things. This should be fixed by counting |
| 139 | * the outstanding results. |
| 140 | */ |
| 141 | paging_request_stop(NULL, subscr, NULL, NULL); |
Holger Hans Peter Freyther | bffd485 | 2010-12-28 17:27:05 +0100 | [diff] [blame] | 142 | subscr_put_channel(subscr); |
Holger Hans Peter Freyther | a32e9c1 | 2010-12-28 17:47:43 +0100 | [diff] [blame] | 143 | } |
Holger Hans Peter Freyther | bffd485 | 2010-12-28 17:27:05 +0100 | [diff] [blame] | 144 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 145 | subscr_put(subscr); |
| 146 | talloc_free(request); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Sylvain Munaut | 65c51b6 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 150 | static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event, |
| 151 | struct msgb *msg, void *data, void *param) |
| 152 | { |
| 153 | int rc; |
| 154 | |
| 155 | switch (event) { |
| 156 | case GSM_SECURITY_AUTH_FAILED: |
| 157 | /* Dispatch as paging failure */ |
| 158 | rc = subscr_paging_dispatch( |
| 159 | GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, |
| 160 | msg, data, param); |
| 161 | break; |
| 162 | |
| 163 | case GSM_SECURITY_NOAVAIL: |
| 164 | case GSM_SECURITY_SUCCEEDED: |
| 165 | /* Dispatch as paging failure */ |
| 166 | rc = subscr_paging_dispatch( |
| 167 | GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED, |
| 168 | msg, data, param); |
| 169 | break; |
| 170 | |
| 171 | default: |
| 172 | rc = -EINVAL; |
| 173 | } |
| 174 | |
| 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | static int subscr_paging_cb(unsigned int hooknum, unsigned int event, |
| 179 | struct msgb *msg, void *data, void *param) |
| 180 | { |
| 181 | struct gsm_subscriber_connection *conn = data; |
| 182 | struct gsm48_hdr *gh; |
| 183 | struct gsm48_pag_resp *pr; |
| 184 | |
| 185 | /* Other cases mean problem, dispatch direclty */ |
| 186 | if (event != GSM_PAGING_SUCCEEDED) |
| 187 | return subscr_paging_dispatch(hooknum, event, msg, data, param); |
| 188 | |
| 189 | /* Get paging response */ |
| 190 | gh = msgb_l3(msg); |
| 191 | pr = (struct gsm48_pag_resp *)gh->data; |
| 192 | |
| 193 | /* We _really_ have a channel, secure it now ! */ |
| 194 | return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param); |
| 195 | } |
| 196 | |
| 197 | |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 198 | static void subscr_send_paging_request(struct gsm_subscriber *subscr) |
| 199 | { |
| 200 | struct subscr_request *request; |
| 201 | int rc; |
| 202 | |
| 203 | assert(!llist_empty(&subscr->requests)); |
| 204 | |
| 205 | request = (struct subscr_request *)subscr->requests.next; |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 206 | request->state = REQ_STATE_PAGED; |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 207 | rc = paging_request(subscr->net, subscr, request->channel_type, |
| 208 | subscr_paging_cb, subscr); |
| 209 | |
| 210 | /* paging failed, quit now */ |
| 211 | if (rc <= 0) { |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 212 | request->state = REQ_STATE_FAILED_START; |
Holger Hans Peter Freyther | ab2a933 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 213 | subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY, |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 214 | NULL, NULL, subscr); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void subscr_get_channel(struct gsm_subscriber *subscr, |
| 219 | int type, gsm_cbfn *cbfn, void *param) |
| 220 | { |
| 221 | struct subscr_request *request; |
| 222 | |
| 223 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
| 224 | if (!request) { |
| 225 | if (cbfn) |
| 226 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 227 | NULL, NULL, param); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | memset(request, 0, sizeof(*request)); |
| 232 | request->subscr = subscr_get(subscr); |
| 233 | request->channel_type = type; |
| 234 | request->cbfn = cbfn; |
| 235 | request->param = param; |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 236 | request->state = REQ_STATE_INITIAL; |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * FIXME: We might be able to assign more than one |
| 240 | * channel, e.g. voice and SMS submit at the same |
| 241 | * time. |
| 242 | */ |
| 243 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 244 | /* add to the list, send a request */ |
| 245 | llist_add_tail(&request->entry, &subscr->requests); |
| 246 | subscr_send_paging_request(subscr); |
| 247 | } else { |
| 248 | /* this will be picked up later, from subscr_put_channel */ |
| 249 | llist_add_tail(&request->entry, &subscr->requests); |
Holger Hans Peter Freyther | c45f0a6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 250 | request->state = REQ_STATE_QUEUED; |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Holger Hans Peter Freyther | 000571f | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 254 | void subscr_put_channel(struct gsm_subscriber *subscr) |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 255 | { |
| 256 | /* |
| 257 | * FIXME: Continue with other requests now... by checking |
| 258 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 259 | * of the lchan after having asked the next requestee to handle |
| 260 | * the channel. |
| 261 | */ |
| 262 | /* |
| 263 | * FIXME: is the lchan is of a different type we could still |
| 264 | * issue an immediate assignment for another channel and then |
| 265 | * close this one. |
| 266 | */ |
| 267 | /* |
| 268 | * Currently we will drop the last ref of the lchan which |
| 269 | * will result in a channel release on RSL and we will start |
| 270 | * the paging. This should work most of the time as the MS |
| 271 | * will listen to the paging requests before we timeout |
| 272 | */ |
| 273 | |
Holger Hans Peter Freyther | 000571f | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 274 | if (subscr && !llist_empty(&subscr->requests)) |
| 275 | subscr_send_paging_request(subscr); |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 278 | struct gsm_subscriber *subscr_create_subscriber(struct gsm_network *net, |
| 279 | const char *imsi) |
| 280 | { |
| 281 | struct gsm_subscriber *subscr = db_create_subscriber(imsi); |
| 282 | if (subscr) |
| 283 | subscr->net = net; |
| 284 | return subscr; |
| 285 | } |
Sylvain Munaut | 3461975 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 286 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 287 | struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_network *net, |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 288 | uint32_t tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 289 | { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 290 | char tmsi_string[14]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 291 | struct gsm_subscriber *subscr; |
| 292 | |
| 293 | /* we might have a record in memory already */ |
Jan Luebbe | 8f7f1d1 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 294 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 295 | if (tmsi == subscr->tmsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 296 | return subscr_get(subscr); |
| 297 | } |
| 298 | |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 299 | sprintf(tmsi_string, "%u", tmsi); |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 300 | return get_subscriber(net, GSM_SUBSCRIBER_TMSI, tmsi_string); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 301 | } |
| 302 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 303 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_network *net, |
| 304 | const char *imsi) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 305 | { |
| 306 | struct gsm_subscriber *subscr; |
| 307 | |
Jan Luebbe | 8f7f1d1 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 308 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 309 | if (strcmp(subscr->imsi, imsi) == 0) |
| 310 | return subscr_get(subscr); |
| 311 | } |
| 312 | |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 313 | return get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 314 | } |
| 315 | |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 316 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_network *net, |
| 317 | const char *ext) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 318 | { |
| 319 | struct gsm_subscriber *subscr; |
| 320 | |
Jan Luebbe | 8f7f1d1 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 321 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 322 | if (strcmp(subscr->extension, ext) == 0) |
| 323 | return subscr_get(subscr); |
| 324 | } |
| 325 | |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 326 | return get_subscriber(net, GSM_SUBSCRIBER_EXTENSION, ext); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 327 | } |
| 328 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 329 | struct gsm_subscriber *subscr_get_by_id(struct gsm_network *net, |
| 330 | unsigned long long id) |
| 331 | { |
| 332 | struct gsm_subscriber *subscr; |
| 333 | char buf[32]; |
| 334 | sprintf(buf, "%llu", id); |
| 335 | |
Jan Luebbe | 8f7f1d1 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 336 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 337 | if (subscr->id == id) |
| 338 | return subscr_get(subscr); |
| 339 | } |
| 340 | |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 341 | return get_subscriber(net, GSM_SUBSCRIBER_ID, buf); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 344 | int subscr_update_expire_lu(struct gsm_subscriber *s, struct gsm_bts *bts) |
| 345 | { |
| 346 | int rc; |
| 347 | |
| 348 | /* Table 10.5.33: The T3212 timeout value field is coded as the |
| 349 | * binary representation of the timeout value for |
| 350 | * periodic updating in decihours. Mark the subscriber as |
| 351 | * inactive if it missed two consecutive location updates. |
| 352 | * Timeout is twice the t3212 value plus one minute */ |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 353 | |
| 354 | /* Is expiration handling enabled? */ |
| 355 | if (bts->si_common.chan_desc.t3212 == 0) |
| 356 | s->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION; |
| 357 | else |
| 358 | s->expire_lu = time(NULL) + |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 359 | (bts->si_common.chan_desc.t3212 * 60 * 6 * 2) + 60; |
Holger Hans Peter Freyther | 2fb8ebf | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 360 | |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 361 | rc = db_sync_subscriber(s); |
| 362 | db_subscriber_update(s); |
| 363 | return rc; |
| 364 | } |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 365 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 366 | int subscr_update(struct gsm_subscriber *s, struct gsm_bts *bts, int reason) |
| 367 | { |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 368 | int rc; |
| 369 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 370 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 371 | switch (reason) { |
| 372 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Harald Welte | c2189a6 | 2009-07-23 18:56:43 +0200 | [diff] [blame] | 373 | s->net = bts->network; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 374 | /* Indicate "attached to LAC" */ |
| 375 | s->lac = bts->location_area_code; |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 376 | |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 377 | LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n", |
| 378 | subscr_name(s), s->lac); |
| 379 | |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 380 | /* |
| 381 | * The below will set a new expire_lu but as a side-effect |
| 382 | * the new lac will be saved in the database. |
| 383 | */ |
| 384 | rc = subscr_update_expire_lu(s, bts); |
Pablo Neira Ayuso | ef717c6 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 385 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 386 | break; |
| 387 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
| 388 | /* Only detach if we are currently in this area */ |
| 389 | if (bts->location_area_code == s->lac) |
Holger Hans Peter Freyther | 6c6ab86 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 390 | s->lac = GSM_LAC_RESERVED_DETACHED; |
Harald Welte | 0747c6a | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 391 | 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] | 392 | rc = db_sync_subscriber(s); |
| 393 | db_subscriber_update(s); |
Pablo Neira Ayuso | ef717c6 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 394 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 395 | break; |
| 396 | default: |
| 397 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 398 | reason); |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 399 | rc = db_sync_subscriber(s); |
| 400 | db_subscriber_update(s); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 401 | break; |
| 402 | }; |
Holger Hans Peter Freyther | 52a6375 | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 403 | |
| 404 | return rc; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 405 | } |
| 406 | |
Holger Hans Peter Freyther | 0c29e5f | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 407 | void subscr_update_from_db(struct gsm_subscriber *sub) |
| 408 | { |
| 409 | db_subscriber_update(sub); |
| 410 | } |
Holger Hans Peter Freyther | 5b72b30 | 2010-12-28 22:12:30 +0100 | [diff] [blame] | 411 | |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 412 | static void subscr_expire_callback(void *data, long long unsigned int id) |
| 413 | { |
| 414 | struct gsm_network *net = data; |
Holger Hans Peter Freyther | c25021f | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 415 | struct gsm_subscriber *s = subscr_get_by_id(net, id); |
| 416 | struct gsm_subscriber_connection *conn = connection_for_subscr(s); |
| 417 | |
| 418 | /* |
| 419 | * The subscriber is active and the phone stopped the timer. As |
| 420 | * we don't want to periodically update the database for active |
| 421 | * subscribers we will just do it when the subscriber was selected |
| 422 | * for expiration. This way on the next around another subscriber |
| 423 | * will be selected. |
| 424 | */ |
| 425 | if (conn && conn->expire_timer_stopped) { |
| 426 | LOGP(DMM, LOGL_DEBUG, "Not expiring subscriber %s (ID %llu)\n", |
| 427 | subscr_name(s), id); |
| 428 | subscr_update_expire_lu(s, conn->bts); |
| 429 | return; |
| 430 | } |
| 431 | |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 432 | |
Holger Hans Peter Freyther | 274e93e | 2013-07-04 20:22:27 +0200 | [diff] [blame] | 433 | LOGP(DMM, LOGL_NOTICE, "Expiring inactive subscriber %s (ID %llu)\n", |
Jan Luebbe | fe7cc1e | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 434 | subscr_name(s), id); |
| 435 | s->lac = GSM_LAC_RESERVED_DETACHED; |
| 436 | db_sync_subscriber(s); |
| 437 | |
| 438 | subscr_put(s); |
| 439 | } |
| 440 | |
| 441 | void subscr_expire(struct gsm_network *net) |
| 442 | { |
| 443 | db_subscriber_expire(net, subscr_expire_callback); |
| 444 | } |
| 445 | |
Holger Hans Peter Freyther | 5b72b30 | 2010-12-28 22:12:30 +0100 | [diff] [blame] | 446 | int subscr_pending_requests(struct gsm_subscriber *sub) |
| 447 | { |
| 448 | struct subscr_request *req; |
| 449 | int pending = 0; |
| 450 | |
| 451 | llist_for_each_entry(req, &sub->requests, entry) |
| 452 | pending += 1; |
| 453 | |
| 454 | return pending; |
| 455 | } |
Holger Hans Peter Freyther | ce69aec | 2010-12-28 22:21:55 +0100 | [diff] [blame] | 456 | |
| 457 | int subscr_pending_clear(struct gsm_subscriber *sub) |
| 458 | { |
| 459 | int deleted = 0; |
| 460 | struct subscr_request *req, *tmp; |
| 461 | |
| 462 | llist_for_each_entry_safe(req, tmp, &sub->requests, entry) { |
| 463 | subscr_put(req->subscr); |
| 464 | llist_del(&req->entry); |
| 465 | talloc_free(req); |
| 466 | deleted += 1; |
| 467 | } |
| 468 | |
| 469 | return deleted; |
| 470 | } |
Holger Hans Peter Freyther | 0fe0c8c | 2010-12-29 11:07:22 +0100 | [diff] [blame] | 471 | |
| 472 | int subscr_pending_dump(struct gsm_subscriber *sub, struct vty *vty) |
| 473 | { |
| 474 | struct subscr_request *req; |
| 475 | |
| 476 | vty_out(vty, "Pending Requests for Subscriber %llu.%s", sub->id, VTY_NEWLINE); |
| 477 | llist_for_each_entry(req, &sub->requests, entry) { |
| 478 | vty_out(vty, "Channel type: %d State: %d Sub: %llu.%s", |
| 479 | req->channel_type, req->state, req->subscr->id, VTY_NEWLINE); |
| 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | } |
Holger Hans Peter Freyther | e5d806e | 2010-12-29 13:45:49 +0100 | [diff] [blame] | 484 | |
| 485 | int subscr_pending_kick(struct gsm_subscriber *sub) |
| 486 | { |
| 487 | subscr_put_channel(sub); |
| 488 | return 0; |
| 489 | } |