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