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 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 81 | static struct gsm_subscriber *get_subscriber(struct gsm_subscriber_group *sgrp, |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 82 | int type, const char *ident) |
| 83 | { |
| 84 | struct gsm_subscriber *subscr = db_get_subscriber(type, ident); |
| 85 | if (subscr) |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 86 | subscr->group = sgrp; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 87 | return subscr; |
| 88 | } |
| 89 | |
Sylvain Munaut | 1e24550 | 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 | 5a86e06 | 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 | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 96 | { |
| 97 | struct subscr_request *request; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 98 | struct gsm_subscriber_connection *conn = data; |
Sylvain Munaut | 5a86e06 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 99 | struct gsm_subscriber *subscr = param; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 100 | struct paging_signal_data sig_data; |
Sylvain Munaut | 1e24550 | 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 | 8a31a3f | 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 | d3baf41 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 110 | sig_data.paging_result = event; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 111 | osmo_signal_dispatch( |
Sylvain Munaut | 8a31a3f | 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 | 1e24550 | 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 | 68c3bf6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 125 | request->state = REQ_STATE_DISPATCHED; |
Sylvain Munaut | 1e24550 | 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 | ffccb77 | 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 | f72b3d5 | 2010-12-28 17:27:05 +0100 | [diff] [blame] | 142 | subscr_put_channel(subscr); |
Holger Hans Peter Freyther | ffccb77 | 2010-12-28 17:47:43 +0100 | [diff] [blame] | 143 | } |
Holger Hans Peter Freyther | f72b3d5 | 2010-12-28 17:27:05 +0100 | [diff] [blame] | 144 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 145 | subscr_put(subscr); |
| 146 | talloc_free(request); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Sylvain Munaut | 5a86e06 | 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 | 1e24550 | 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; |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 202 | struct gsm_network *net = subscr->group->net; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 203 | |
| 204 | assert(!llist_empty(&subscr->requests)); |
| 205 | |
| 206 | request = (struct subscr_request *)subscr->requests.next; |
Holger Hans Peter Freyther | 68c3bf6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 207 | request->state = REQ_STATE_PAGED; |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 208 | rc = paging_request(net, subscr, request->channel_type, |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 209 | subscr_paging_cb, subscr); |
| 210 | |
| 211 | /* paging failed, quit now */ |
| 212 | if (rc <= 0) { |
Holger Hans Peter Freyther | 68c3bf6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 213 | request->state = REQ_STATE_FAILED_START; |
Holger Hans Peter Freyther | d3baf41 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 214 | subscr_paging_cb(GSM_HOOK_RR_PAGING, GSM_PAGING_BUSY, |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 215 | NULL, NULL, subscr); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void subscr_get_channel(struct gsm_subscriber *subscr, |
| 220 | int type, gsm_cbfn *cbfn, void *param) |
| 221 | { |
| 222 | struct subscr_request *request; |
| 223 | |
| 224 | request = talloc(tall_sub_req_ctx, struct subscr_request); |
| 225 | if (!request) { |
| 226 | if (cbfn) |
| 227 | cbfn(GSM_HOOK_RR_PAGING, GSM_PAGING_OOM, |
| 228 | NULL, NULL, param); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | memset(request, 0, sizeof(*request)); |
| 233 | request->subscr = subscr_get(subscr); |
| 234 | request->channel_type = type; |
| 235 | request->cbfn = cbfn; |
| 236 | request->param = param; |
Holger Hans Peter Freyther | 68c3bf6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 237 | request->state = REQ_STATE_INITIAL; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * FIXME: We might be able to assign more than one |
| 241 | * channel, e.g. voice and SMS submit at the same |
| 242 | * time. |
| 243 | */ |
| 244 | if (!subscr->in_callback && llist_empty(&subscr->requests)) { |
| 245 | /* add to the list, send a request */ |
| 246 | llist_add_tail(&request->entry, &subscr->requests); |
| 247 | subscr_send_paging_request(subscr); |
| 248 | } else { |
| 249 | /* this will be picked up later, from subscr_put_channel */ |
| 250 | llist_add_tail(&request->entry, &subscr->requests); |
Holger Hans Peter Freyther | 68c3bf6 | 2010-12-29 09:33:34 +0100 | [diff] [blame] | 251 | request->state = REQ_STATE_QUEUED; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Holger Hans Peter Freyther | 725966d | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 255 | void subscr_put_channel(struct gsm_subscriber *subscr) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 256 | { |
| 257 | /* |
| 258 | * FIXME: Continue with other requests now... by checking |
| 259 | * the gsm_subscriber inside the gsm_lchan. Drop the ref count |
| 260 | * of the lchan after having asked the next requestee to handle |
| 261 | * the channel. |
| 262 | */ |
| 263 | /* |
| 264 | * FIXME: is the lchan is of a different type we could still |
| 265 | * issue an immediate assignment for another channel and then |
| 266 | * close this one. |
| 267 | */ |
| 268 | /* |
| 269 | * Currently we will drop the last ref of the lchan which |
| 270 | * will result in a channel release on RSL and we will start |
| 271 | * the paging. This should work most of the time as the MS |
| 272 | * will listen to the paging requests before we timeout |
| 273 | */ |
| 274 | |
Holger Hans Peter Freyther | 725966d | 2010-12-27 22:43:28 +0100 | [diff] [blame] | 275 | if (subscr && !llist_empty(&subscr->requests)) |
| 276 | subscr_send_paging_request(subscr); |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 279 | struct gsm_subscriber *subscr_create_subscriber(struct gsm_subscriber_group *sgrp, |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 280 | const char *imsi) |
| 281 | { |
| 282 | struct gsm_subscriber *subscr = db_create_subscriber(imsi); |
| 283 | if (subscr) |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 284 | subscr->group = sgrp; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 285 | return subscr; |
| 286 | } |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 287 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 288 | struct gsm_subscriber *subscr_get_by_tmsi(struct gsm_subscriber_group *sgrp, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 289 | uint32_t tmsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 290 | { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 291 | char tmsi_string[14]; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 292 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 293 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 294 | /* we might have a record in memory already */ |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 295 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 296 | if (tmsi == subscr->tmsi) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 297 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 300 | sprintf(tmsi_string, "%u", tmsi); |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 301 | return get_subscriber(sgrp, GSM_SUBSCRIBER_TMSI, tmsi_string); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 304 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_subscriber_group *sgrp, |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 305 | const char *imsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 306 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 307 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 308 | |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 309 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 310 | if (strcmp(subscr->imsi, imsi) == 0) |
| 311 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 314 | return get_subscriber(sgrp, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 317 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_subscriber_group *sgrp, |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 318 | const char *ext) |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 319 | { |
| 320 | struct gsm_subscriber *subscr; |
| 321 | |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 322 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 323 | if (strcmp(subscr->extension, ext) == 0) |
| 324 | return subscr_get(subscr); |
| 325 | } |
| 326 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 327 | return get_subscriber(sgrp, GSM_SUBSCRIBER_EXTENSION, ext); |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 330 | struct gsm_subscriber *subscr_get_by_id(struct gsm_subscriber_group *sgrp, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 331 | unsigned long long id) |
| 332 | { |
| 333 | struct gsm_subscriber *subscr; |
| 334 | char buf[32]; |
| 335 | sprintf(buf, "%llu", id); |
| 336 | |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 337 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 338 | if (subscr->id == id) |
| 339 | return subscr_get(subscr); |
| 340 | } |
| 341 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 342 | return get_subscriber(sgrp, GSM_SUBSCRIBER_ID, buf); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 345 | int subscr_update_expire_lu(struct gsm_subscriber *s, struct gsm_bts *bts) |
| 346 | { |
| 347 | int rc; |
| 348 | |
| 349 | /* Table 10.5.33: The T3212 timeout value field is coded as the |
| 350 | * binary representation of the timeout value for |
| 351 | * periodic updating in decihours. Mark the subscriber as |
| 352 | * inactive if it missed two consecutive location updates. |
| 353 | * Timeout is twice the t3212 value plus one minute */ |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 354 | |
| 355 | /* Is expiration handling enabled? */ |
| 356 | if (bts->si_common.chan_desc.t3212 == 0) |
| 357 | s->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION; |
| 358 | else |
| 359 | s->expire_lu = time(NULL) + |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 360 | (bts->si_common.chan_desc.t3212 * 60 * 6 * 2) + 60; |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 361 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 362 | rc = db_sync_subscriber(s); |
| 363 | db_subscriber_update(s); |
| 364 | return rc; |
| 365 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 366 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 367 | 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] | 368 | { |
Holger Hans Peter Freyther | e25445b | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 369 | int rc; |
| 370 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 371 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 372 | switch (reason) { |
| 373 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 374 | s->group = bts->network->subscr_group; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 375 | /* Indicate "attached to LAC" */ |
| 376 | s->lac = bts->location_area_code; |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 377 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 378 | LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n", |
| 379 | subscr_name(s), s->lac); |
| 380 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 381 | /* |
| 382 | * The below will set a new expire_lu but as a side-effect |
| 383 | * the new lac will be saved in the database. |
| 384 | */ |
| 385 | rc = subscr_update_expire_lu(s, bts); |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 386 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 387 | break; |
| 388 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
Holger Freyther | c3d4b2d | 2009-06-04 14:27:39 +0000 | [diff] [blame] | 389 | /* Only detach if we are currently in this area */ |
| 390 | if (bts->location_area_code == s->lac) |
Holger Hans Peter Freyther | e48b956 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 391 | s->lac = GSM_LAC_RESERVED_DETACHED; |
Harald Welte | 2e6d468 | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 392 | 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] | 393 | rc = db_sync_subscriber(s); |
| 394 | db_subscriber_update(s); |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 395 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 396 | break; |
| 397 | default: |
| 398 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 399 | reason); |
Holger Hans Peter Freyther | e25445b | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 400 | rc = db_sync_subscriber(s); |
| 401 | db_subscriber_update(s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 402 | break; |
| 403 | }; |
Holger Hans Peter Freyther | e25445b | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 404 | |
| 405 | return rc; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 408 | void subscr_update_from_db(struct gsm_subscriber *sub) |
| 409 | { |
| 410 | db_subscriber_update(sub); |
| 411 | } |
Holger Hans Peter Freyther | ebdd3cb | 2010-12-28 22:12:30 +0100 | [diff] [blame] | 412 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 413 | static void subscr_expire_callback(void *data, long long unsigned int id) |
| 414 | { |
| 415 | struct gsm_network *net = data; |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 416 | struct gsm_subscriber *s = subscr_get_by_id(net->subscr_group, id); |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 417 | struct gsm_subscriber_connection *conn = connection_for_subscr(s); |
| 418 | |
| 419 | /* |
| 420 | * The subscriber is active and the phone stopped the timer. As |
| 421 | * we don't want to periodically update the database for active |
| 422 | * subscribers we will just do it when the subscriber was selected |
| 423 | * for expiration. This way on the next around another subscriber |
| 424 | * will be selected. |
| 425 | */ |
| 426 | if (conn && conn->expire_timer_stopped) { |
| 427 | LOGP(DMM, LOGL_DEBUG, "Not expiring subscriber %s (ID %llu)\n", |
| 428 | subscr_name(s), id); |
| 429 | subscr_update_expire_lu(s, conn->bts); |
| 430 | return; |
| 431 | } |
| 432 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 433 | |
Holger Hans Peter Freyther | 81cff91 | 2013-07-04 20:22:27 +0200 | [diff] [blame] | 434 | LOGP(DMM, LOGL_NOTICE, "Expiring inactive subscriber %s (ID %llu)\n", |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 435 | subscr_name(s), id); |
| 436 | s->lac = GSM_LAC_RESERVED_DETACHED; |
| 437 | db_sync_subscriber(s); |
| 438 | |
| 439 | subscr_put(s); |
| 440 | } |
| 441 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 442 | void subscr_expire(struct gsm_subscriber_group *sgrp) |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 443 | { |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 444 | db_subscriber_expire(sgrp->net, subscr_expire_callback); |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Holger Hans Peter Freyther | ebdd3cb | 2010-12-28 22:12:30 +0100 | [diff] [blame] | 447 | int subscr_pending_requests(struct gsm_subscriber *sub) |
| 448 | { |
| 449 | struct subscr_request *req; |
| 450 | int pending = 0; |
| 451 | |
| 452 | llist_for_each_entry(req, &sub->requests, entry) |
| 453 | pending += 1; |
| 454 | |
| 455 | return pending; |
| 456 | } |
Holger Hans Peter Freyther | fc85741 | 2010-12-28 22:21:55 +0100 | [diff] [blame] | 457 | |
| 458 | int subscr_pending_clear(struct gsm_subscriber *sub) |
| 459 | { |
| 460 | int deleted = 0; |
| 461 | struct subscr_request *req, *tmp; |
| 462 | |
| 463 | llist_for_each_entry_safe(req, tmp, &sub->requests, entry) { |
| 464 | subscr_put(req->subscr); |
| 465 | llist_del(&req->entry); |
| 466 | talloc_free(req); |
| 467 | deleted += 1; |
| 468 | } |
| 469 | |
| 470 | return deleted; |
| 471 | } |
Holger Hans Peter Freyther | 763b42a | 2010-12-29 11:07:22 +0100 | [diff] [blame] | 472 | |
| 473 | int subscr_pending_dump(struct gsm_subscriber *sub, struct vty *vty) |
| 474 | { |
| 475 | struct subscr_request *req; |
| 476 | |
| 477 | vty_out(vty, "Pending Requests for Subscriber %llu.%s", sub->id, VTY_NEWLINE); |
| 478 | llist_for_each_entry(req, &sub->requests, entry) { |
| 479 | vty_out(vty, "Channel type: %d State: %d Sub: %llu.%s", |
| 480 | req->channel_type, req->state, req->subscr->id, VTY_NEWLINE); |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
Holger Hans Peter Freyther | 451eb29 | 2010-12-29 13:45:49 +0100 | [diff] [blame] | 485 | |
| 486 | int subscr_pending_kick(struct gsm_subscriber *sub) |
| 487 | { |
| 488 | subscr_put_channel(sub); |
| 489 | return 0; |
| 490 | } |