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 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 59 | /* the callback data */ |
| 60 | gsm_cbfn *cbfn; |
| 61 | void *param; |
| 62 | }; |
| 63 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 64 | 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] | 65 | int type, const char *ident) |
| 66 | { |
| 67 | struct gsm_subscriber *subscr = db_get_subscriber(type, ident); |
| 68 | if (subscr) |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 69 | subscr->group = sgrp; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 70 | return subscr; |
| 71 | } |
| 72 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 73 | /* |
| 74 | * We got the channel assigned and can now hand this channel |
| 75 | * over to one of our callbacks. |
| 76 | */ |
Sylvain Munaut | 5a86e06 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 77 | static int subscr_paging_dispatch(unsigned int hooknum, unsigned int event, |
| 78 | struct msgb *msg, void *data, void *param) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 79 | { |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 80 | struct subscr_request *request, *tmp; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 81 | struct gsm_subscriber_connection *conn = data; |
Sylvain Munaut | 5a86e06 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 82 | struct gsm_subscriber *subscr = param; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 83 | struct paging_signal_data sig_data; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 84 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 85 | OSMO_ASSERT(subscr->is_paging); |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 86 | |
Holger Hans Peter Freyther | 778c7d1 | 2015-08-03 11:18:23 +0200 | [diff] [blame] | 87 | /* |
| 88 | * Stop paging on all other BTS. E.g. if this is |
| 89 | * the first timeout on a BTS then the others will |
| 90 | * timeout soon as well. Let's just stop everything |
| 91 | * and forget we wanted to page. |
| 92 | */ |
| 93 | paging_request_stop(NULL, subscr, NULL, NULL); |
Holger Hans Peter Freyther | 778c7d1 | 2015-08-03 11:18:23 +0200 | [diff] [blame] | 94 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 95 | /* Inform parts of the system we don't know */ |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 96 | sig_data.subscr = subscr; |
| 97 | sig_data.bts = conn ? conn->bts : NULL; |
| 98 | sig_data.conn = conn; |
Holger Hans Peter Freyther | d3baf41 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 99 | sig_data.paging_result = event; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 100 | osmo_signal_dispatch( |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 101 | SS_PAGING, |
| 102 | event == GSM_PAGING_SUCCEEDED ? |
| 103 | S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, |
| 104 | &sig_data |
| 105 | ); |
| 106 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 107 | llist_for_each_entry_safe(request, tmp, &subscr->requests, entry) { |
| 108 | llist_del(&request->entry); |
| 109 | request->cbfn(hooknum, event, msg, data, request->param); |
| 110 | talloc_free(request); |
Holger Hans Peter Freyther | ffccb77 | 2010-12-28 17:47:43 +0100 | [diff] [blame] | 111 | } |
Holger Hans Peter Freyther | f72b3d5 | 2010-12-28 17:27:05 +0100 | [diff] [blame] | 112 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 113 | /* balanced with the moment we start paging */ |
Holger Hans Peter Freyther | eb48caf | 2015-08-03 11:34:35 +0200 | [diff] [blame] | 114 | subscr->is_paging = 0; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 115 | subscr_put(subscr); |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Sylvain Munaut | 5a86e06 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 119 | static int subscr_paging_sec_cb(unsigned int hooknum, unsigned int event, |
| 120 | struct msgb *msg, void *data, void *param) |
| 121 | { |
| 122 | int rc; |
| 123 | |
| 124 | switch (event) { |
| 125 | case GSM_SECURITY_AUTH_FAILED: |
| 126 | /* Dispatch as paging failure */ |
| 127 | rc = subscr_paging_dispatch( |
| 128 | GSM_HOOK_RR_PAGING, GSM_PAGING_EXPIRED, |
| 129 | msg, data, param); |
| 130 | break; |
| 131 | |
| 132 | case GSM_SECURITY_NOAVAIL: |
| 133 | case GSM_SECURITY_SUCCEEDED: |
| 134 | /* Dispatch as paging failure */ |
| 135 | rc = subscr_paging_dispatch( |
| 136 | GSM_HOOK_RR_PAGING, GSM_PAGING_SUCCEEDED, |
| 137 | msg, data, param); |
| 138 | break; |
| 139 | |
| 140 | default: |
| 141 | rc = -EINVAL; |
| 142 | } |
| 143 | |
| 144 | return rc; |
| 145 | } |
| 146 | |
| 147 | static int subscr_paging_cb(unsigned int hooknum, unsigned int event, |
| 148 | struct msgb *msg, void *data, void *param) |
| 149 | { |
| 150 | struct gsm_subscriber_connection *conn = data; |
| 151 | struct gsm48_hdr *gh; |
| 152 | struct gsm48_pag_resp *pr; |
| 153 | |
| 154 | /* Other cases mean problem, dispatch direclty */ |
| 155 | if (event != GSM_PAGING_SUCCEEDED) |
| 156 | return subscr_paging_dispatch(hooknum, event, msg, data, param); |
| 157 | |
| 158 | /* Get paging response */ |
| 159 | gh = msgb_l3(msg); |
| 160 | pr = (struct gsm48_pag_resp *)gh->data; |
| 161 | |
| 162 | /* We _really_ have a channel, secure it now ! */ |
| 163 | return gsm48_secure_channel(conn, pr->key_seq, subscr_paging_sec_cb, param); |
| 164 | } |
| 165 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 166 | struct subscr_request *subscr_request_channel(struct gsm_subscriber *subscr, |
| 167 | int channel_type, gsm_cbfn *cbfn, void *param) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 168 | { |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 169 | int rc; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 170 | struct subscr_request *request; |
| 171 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 172 | /* Start paging.. we know it is async so we can do it before */ |
| 173 | if (!subscr->is_paging) { |
| 174 | LOGP(DMM, LOGL_DEBUG, "Subscriber %s not paged yet.\n", |
| 175 | subscr_name(subscr)); |
| 176 | rc = paging_request(subscr->group->net, subscr, channel_type, |
| 177 | subscr_paging_cb, subscr); |
| 178 | if (rc <= 0) { |
| 179 | LOGP(DMM, LOGL_ERROR, "Subscriber %s paging failed: %d\n", |
| 180 | subscr_name(subscr), rc); |
| 181 | return NULL; |
| 182 | } |
| 183 | /* reduced on the first paging callback */ |
| 184 | subscr_get(subscr); |
| 185 | subscr->is_paging = 1; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 188 | /* TODO: Stop paging in case of memory allocation failure */ |
| 189 | request = talloc_zero(subscr, struct subscr_request); |
| 190 | if (!request) |
| 191 | return NULL; |
| 192 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 193 | request->cbfn = cbfn; |
| 194 | request->param = param; |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 195 | llist_add_tail(&request->entry, &subscr->requests); |
| 196 | return request; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 199 | void subscr_remove_request(struct subscr_request *request) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 200 | { |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 201 | llist_del(&request->entry); |
| 202 | talloc_free(request); |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 205 | struct gsm_subscriber *subscr_create_subscriber(struct gsm_subscriber_group *sgrp, |
Max | 0fcd2e2 | 2016-06-07 15:32:16 +0200 | [diff] [blame^] | 206 | const char *imsi, uint64_t smin, |
| 207 | uint64_t smax) |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 208 | { |
Max | 0fcd2e2 | 2016-06-07 15:32:16 +0200 | [diff] [blame^] | 209 | struct gsm_subscriber *subscr = db_create_subscriber(imsi, smin, smax); |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 210 | if (subscr) |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 211 | subscr->group = sgrp; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 212 | return subscr; |
| 213 | } |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 214 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 215 | 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] | 216 | uint32_t tmsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 217 | { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 218 | char tmsi_string[14]; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 219 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 220 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 221 | /* we might have a record in memory already */ |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 222 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 223 | if (tmsi == subscr->tmsi) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 224 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 227 | sprintf(tmsi_string, "%u", tmsi); |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 228 | return get_subscriber(sgrp, GSM_SUBSCRIBER_TMSI, tmsi_string); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 231 | struct gsm_subscriber *subscr_get_by_imsi(struct gsm_subscriber_group *sgrp, |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 232 | const char *imsi) |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 233 | { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 234 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 235 | |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 236 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 237 | if (strcmp(subscr->imsi, imsi) == 0) |
| 238 | return subscr_get(subscr); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 241 | return get_subscriber(sgrp, GSM_SUBSCRIBER_IMSI, imsi); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 244 | struct gsm_subscriber *subscr_get_by_extension(struct gsm_subscriber_group *sgrp, |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 245 | const char *ext) |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 246 | { |
| 247 | struct gsm_subscriber *subscr; |
| 248 | |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 249 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 250 | if (strcmp(subscr->extension, ext) == 0) |
| 251 | return subscr_get(subscr); |
| 252 | } |
| 253 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 254 | return get_subscriber(sgrp, GSM_SUBSCRIBER_EXTENSION, ext); |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 257 | struct gsm_subscriber *subscr_get_by_id(struct gsm_subscriber_group *sgrp, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 258 | unsigned long long id) |
| 259 | { |
| 260 | struct gsm_subscriber *subscr; |
| 261 | char buf[32]; |
| 262 | sprintf(buf, "%llu", id); |
| 263 | |
Jan Luebbe | 6e30068 | 2012-12-25 18:58:34 +0100 | [diff] [blame] | 264 | llist_for_each_entry(subscr, subscr_bsc_active_subscribers(), entry) { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 265 | if (subscr->id == id) |
| 266 | return subscr_get(subscr); |
| 267 | } |
| 268 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 269 | return get_subscriber(sgrp, GSM_SUBSCRIBER_ID, buf); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 272 | int subscr_update_expire_lu(struct gsm_subscriber *s, struct gsm_bts *bts) |
| 273 | { |
| 274 | int rc; |
| 275 | |
| 276 | /* Table 10.5.33: The T3212 timeout value field is coded as the |
| 277 | * binary representation of the timeout value for |
| 278 | * periodic updating in decihours. Mark the subscriber as |
| 279 | * inactive if it missed two consecutive location updates. |
| 280 | * Timeout is twice the t3212 value plus one minute */ |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 281 | |
| 282 | /* Is expiration handling enabled? */ |
| 283 | if (bts->si_common.chan_desc.t3212 == 0) |
| 284 | s->expire_lu = GSM_SUBSCRIBER_NO_EXPIRATION; |
| 285 | else |
| 286 | s->expire_lu = time(NULL) + |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 287 | (bts->si_common.chan_desc.t3212 * 60 * 6 * 2) + 60; |
Holger Hans Peter Freyther | c63f6f1 | 2013-07-27 21:07:57 +0200 | [diff] [blame] | 288 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 289 | rc = db_sync_subscriber(s); |
| 290 | db_subscriber_update(s); |
| 291 | return rc; |
| 292 | } |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 293 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 294 | 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] | 295 | { |
Holger Hans Peter Freyther | e25445b | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 296 | int rc; |
| 297 | |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 298 | /* FIXME: Migrate pending requests from one BSC to another */ |
| 299 | switch (reason) { |
| 300 | case GSM_SUBSCRIBER_UPDATE_ATTACHED: |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 301 | s->group = bts->network->subscr_group; |
Holger Freyther | 6d5200b | 2009-06-02 02:54:38 +0000 | [diff] [blame] | 302 | /* Indicate "attached to LAC" */ |
| 303 | s->lac = bts->location_area_code; |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 304 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 305 | LOGP(DMM, LOGL_INFO, "Subscriber %s ATTACHED LAC=%u\n", |
| 306 | subscr_name(s), s->lac); |
| 307 | |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 308 | /* |
| 309 | * The below will set a new expire_lu but as a side-effect |
| 310 | * the new lac will be saved in the database. |
| 311 | */ |
| 312 | rc = subscr_update_expire_lu(s, bts); |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 313 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 314 | break; |
| 315 | case GSM_SUBSCRIBER_UPDATE_DETACHED: |
Holger Freyther | c3d4b2d | 2009-06-04 14:27:39 +0000 | [diff] [blame] | 316 | /* Only detach if we are currently in this area */ |
| 317 | if (bts->location_area_code == s->lac) |
Holger Hans Peter Freyther | e48b956 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 318 | s->lac = GSM_LAC_RESERVED_DETACHED; |
Harald Welte | 2e6d468 | 2009-12-24 14:50:24 +0100 | [diff] [blame] | 319 | 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] | 320 | rc = db_sync_subscriber(s); |
| 321 | db_subscriber_update(s); |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 322 | osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 323 | break; |
| 324 | default: |
| 325 | fprintf(stderr, "subscr_update with unknown reason: %d\n", |
| 326 | reason); |
Holger Hans Peter Freyther | e25445b | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 327 | rc = db_sync_subscriber(s); |
| 328 | db_subscriber_update(s); |
Holger Freyther | 4a49e77 | 2009-04-12 05:37:29 +0000 | [diff] [blame] | 329 | break; |
| 330 | }; |
Holger Hans Peter Freyther | e25445b | 2010-12-25 19:40:14 +0100 | [diff] [blame] | 331 | |
| 332 | return rc; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Holger Hans Peter Freyther | abd0cac | 2010-12-22 18:12:11 +0100 | [diff] [blame] | 335 | void subscr_update_from_db(struct gsm_subscriber *sub) |
| 336 | { |
| 337 | db_subscriber_update(sub); |
| 338 | } |
Holger Hans Peter Freyther | ebdd3cb | 2010-12-28 22:12:30 +0100 | [diff] [blame] | 339 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 340 | static void subscr_expire_callback(void *data, long long unsigned int id) |
| 341 | { |
| 342 | struct gsm_network *net = data; |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 343 | 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] | 344 | struct gsm_subscriber_connection *conn = connection_for_subscr(s); |
| 345 | |
| 346 | /* |
| 347 | * The subscriber is active and the phone stopped the timer. As |
| 348 | * we don't want to periodically update the database for active |
| 349 | * subscribers we will just do it when the subscriber was selected |
| 350 | * for expiration. This way on the next around another subscriber |
| 351 | * will be selected. |
| 352 | */ |
| 353 | if (conn && conn->expire_timer_stopped) { |
| 354 | LOGP(DMM, LOGL_DEBUG, "Not expiring subscriber %s (ID %llu)\n", |
| 355 | subscr_name(s), id); |
| 356 | subscr_update_expire_lu(s, conn->bts); |
Holger Hans Peter Freyther | 8159455 | 2015-08-04 11:37:30 +0200 | [diff] [blame] | 357 | subscr_put(s); |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 361 | |
Holger Hans Peter Freyther | 81cff91 | 2013-07-04 20:22:27 +0200 | [diff] [blame] | 362 | LOGP(DMM, LOGL_NOTICE, "Expiring inactive subscriber %s (ID %llu)\n", |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 363 | subscr_name(s), id); |
| 364 | s->lac = GSM_LAC_RESERVED_DETACHED; |
| 365 | db_sync_subscriber(s); |
| 366 | |
| 367 | subscr_put(s); |
| 368 | } |
| 369 | |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 370 | void subscr_expire(struct gsm_subscriber_group *sgrp) |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 371 | { |
Jacob Erlbeck | 1e30a28 | 2014-12-03 09:28:24 +0100 | [diff] [blame] | 372 | db_subscriber_expire(sgrp->net, subscr_expire_callback); |
Jan Luebbe | bfbdeec | 2012-12-27 00:27:16 +0100 | [diff] [blame] | 373 | } |