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