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> |
Max | e6052c4 | 2016-06-30 10:25:49 +0200 | [diff] [blame] | 29 | #include <stdbool.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 30 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | 763b42a | 2010-12-29 11:07:22 +0100 | [diff] [blame] | 33 | #include <osmocom/vty/vty.h> |
| 34 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 35 | #include <openbsc/gsm_subscriber.h> |
Sylvain Munaut | 5a86e06 | 2010-12-01 22:38:03 +0100 | [diff] [blame] | 36 | #include <openbsc/gsm_04_08.h> |
Holger Freyther | 3e0ef7c | 2009-06-02 02:54:48 +0000 | [diff] [blame] | 37 | #include <openbsc/debug.h> |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 38 | #include <openbsc/paging.h> |
Harald Welte | ea5cf30 | 2009-07-29 23:14:15 +0200 | [diff] [blame] | 39 | #include <openbsc/signal.h> |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 40 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 41 | #include <openbsc/chan_alloc.h> |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 42 | #include <openbsc/vlr.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 43 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 44 | void *tall_sub_req_ctx; |
| 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 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 64 | static struct bsc_subscr *vlr_subscr_to_bsc_sub(struct llist_head *bsc_subscribers, |
| 65 | struct vlr_subscr *vsub) |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 66 | { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 67 | struct bsc_subscr *sub; |
| 68 | /* TODO MSC split -- creating a BSC subscriber directly from MSC data |
| 69 | * structures in RAM. At some point the MSC will send a message to the |
| 70 | * BSC instead. */ |
| 71 | sub = bsc_subscr_find_or_create_by_imsi(bsc_subscribers, vsub->imsi); |
| 72 | sub->tmsi = vsub->tmsi; |
| 73 | sub->lac = vsub->lac; |
| 74 | return sub; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 77 | /* |
| 78 | * We got the channel assigned and can now hand this channel |
| 79 | * over to one of our callbacks. |
| 80 | */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 81 | int subscr_paging_dispatch(unsigned int hooknum, unsigned int event, |
| 82 | struct msgb *msg, void *data, void *param) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 83 | { |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 84 | struct subscr_request *request, *tmp; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 85 | struct gsm_subscriber_connection *conn = data; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 86 | struct vlr_subscr *vsub = param; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 87 | struct paging_signal_data sig_data; |
Neels Hofmeyr | 6d804b1 | 2017-02-18 22:20:46 +0100 | [diff] [blame] | 88 | struct bsc_subscr *bsub; |
| 89 | struct gsm_network *net; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 90 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 91 | OSMO_ASSERT(vsub && vsub->cs.is_paging); |
| 92 | net = vsub->vlr->user_ctx; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | 778c7d1 | 2015-08-03 11:18:23 +0200 | [diff] [blame] | 94 | /* |
| 95 | * Stop paging on all other BTS. E.g. if this is |
| 96 | * the first timeout on a BTS then the others will |
| 97 | * timeout soon as well. Let's just stop everything |
| 98 | * and forget we wanted to page. |
| 99 | */ |
Neels Hofmeyr | 6d804b1 | 2017-02-18 22:20:46 +0100 | [diff] [blame] | 100 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 101 | bsub = vlr_subscr_to_bsc_sub(conn->network->bsc_subscribers, vsub); |
Neels Hofmeyr | 6d804b1 | 2017-02-18 22:20:46 +0100 | [diff] [blame] | 102 | paging_request_stop(&net->bts_list, NULL, bsub, NULL, NULL); |
| 103 | bsc_subscr_put(bsub); |
Holger Hans Peter Freyther | 778c7d1 | 2015-08-03 11:18:23 +0200 | [diff] [blame] | 104 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 105 | /* Inform parts of the system we don't know */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 106 | sig_data.vsub = vsub; |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 107 | sig_data.bts = conn ? conn->bts : NULL; |
| 108 | sig_data.conn = conn; |
Holger Hans Peter Freyther | d3baf41 | 2010-12-23 18:19:17 +0100 | [diff] [blame] | 109 | sig_data.paging_result = event; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 110 | osmo_signal_dispatch( |
Sylvain Munaut | 8a31a3f | 2010-12-01 23:04:03 +0100 | [diff] [blame] | 111 | SS_PAGING, |
| 112 | event == GSM_PAGING_SUCCEEDED ? |
| 113 | S_PAGING_SUCCEEDED : S_PAGING_EXPIRED, |
| 114 | &sig_data |
| 115 | ); |
| 116 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 117 | llist_for_each_entry_safe(request, tmp, &vsub->cs.requests, entry) { |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 118 | llist_del(&request->entry); |
| 119 | request->cbfn(hooknum, event, msg, data, request->param); |
| 120 | talloc_free(request); |
Holger Hans Peter Freyther | ffccb77 | 2010-12-28 17:47:43 +0100 | [diff] [blame] | 121 | } |
Holger Hans Peter Freyther | f72b3d5 | 2010-12-28 17:27:05 +0100 | [diff] [blame] | 122 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 123 | /* balanced with the moment we start paging */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 124 | vsub->cs.is_paging = false; |
| 125 | vlr_subscr_put(vsub); |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 129 | struct subscr_request *subscr_request_channel(struct vlr_subscr *vsub, |
| 130 | int channel_type, |
| 131 | gsm_cbfn *cbfn, void *param) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 132 | { |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 133 | int rc; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 134 | struct subscr_request *request; |
Neels Hofmeyr | 6d804b1 | 2017-02-18 22:20:46 +0100 | [diff] [blame] | 135 | struct bsc_subscr *bsub; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 136 | struct gsm_network *net = vsub->vlr->user_ctx; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 137 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 138 | /* Start paging.. we know it is async so we can do it before */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 139 | if (!vsub->cs.is_paging) { |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 140 | LOGP(DMM, LOGL_DEBUG, "Subscriber %s not paged yet.\n", |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 141 | vlr_subscr_name(vsub)); |
| 142 | bsub = vlr_subscr_to_bsc_sub(net->bsc_subscribers, vsub); |
| 143 | rc = paging_request(net, bsub, channel_type, NULL, NULL); |
Neels Hofmeyr | 6d804b1 | 2017-02-18 22:20:46 +0100 | [diff] [blame] | 144 | bsc_subscr_put(bsub); |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 145 | if (rc <= 0) { |
| 146 | LOGP(DMM, LOGL_ERROR, "Subscriber %s paging failed: %d\n", |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 147 | vlr_subscr_name(vsub), rc); |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 148 | return NULL; |
| 149 | } |
| 150 | /* reduced on the first paging callback */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 151 | vlr_subscr_get(vsub); |
| 152 | vsub->cs.is_paging = true; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 155 | /* TODO: Stop paging in case of memory allocation failure */ |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 156 | request = talloc_zero(vsub, struct subscr_request); |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 157 | if (!request) |
| 158 | return NULL; |
| 159 | |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 160 | request->cbfn = cbfn; |
| 161 | request->param = param; |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 162 | llist_add_tail(&request->entry, &vsub->cs.requests); |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 163 | return request; |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 166 | void subscr_remove_request(struct subscr_request *request) |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 167 | { |
Holger Hans Peter Freyther | d6d7aff | 2015-04-06 12:03:45 +0200 | [diff] [blame] | 168 | llist_del(&request->entry); |
| 169 | talloc_free(request); |
Sylvain Munaut | 1e24550 | 2010-12-01 21:07:29 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 172 | struct gsm_subscriber_connection *connection_for_subscr(struct vlr_subscr *vsub) |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 173 | { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 174 | struct gsm_network *net = vsub->vlr->user_ctx; |
Neels Hofmeyr | 1a60644 | 2016-05-10 13:56:43 +0200 | [diff] [blame] | 175 | struct gsm_subscriber_connection *conn; |
| 176 | |
| 177 | llist_for_each_entry(conn, &net->subscr_conns, entry) { |
Harald Welte | 7b423ed | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 178 | if (conn->vsub == vsub) |
Neels Hofmeyr | 1a60644 | 2016-05-10 13:56:43 +0200 | [diff] [blame] | 179 | return conn; |
| 180 | } |
| 181 | |
| 182 | return NULL; |
| 183 | } |