Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 90267a9 | 2013-10-23 11:24:17 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [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 | 9f75c35 | 2010-04-30 20:26:32 +0200 | [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 | 9f75c35 | 2010-04-30 20:26:32 +0200 | [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 | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
Harald Welte | 7fc9822 | 2010-05-11 10:15:42 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 32 | #include <time.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 33 | |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 34 | #include <osmocom/core/hashtable.h> |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 35 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 36 | #include <osmocom/core/talloc.h> |
| 37 | #include <osmocom/core/select.h> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 38 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 39 | #include <osmocom/core/stats.h> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 40 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 41 | #include <osmocom/gprs/gprs_ns2.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 42 | #include <osmocom/gprs/gprs_bssgp.h> |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 43 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 44 | |
Jacob Erlbeck | 7c101d9 | 2014-06-06 18:49:23 +0200 | [diff] [blame] | 45 | #include <osmocom/gsm/gsm_utils.h> |
| 46 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 47 | #include <osmocom/sgsn/signal.h> |
| 48 | #include <osmocom/sgsn/debug.h> |
| 49 | #include <osmocom/sgsn/gprs_gb_parse.h> |
| 50 | #include <osmocom/sgsn/gb_proxy.h> |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 51 | |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 52 | #include <osmocom/sgsn/gprs_llc.h> |
Harald Welte | 53373bc | 2016-04-20 17:11:43 +0200 | [diff] [blame] | 53 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 54 | #include <osmocom/sgsn/gprs_utils.h> |
Jacob Erlbeck | 67a4445 | 2014-05-19 10:14:58 +0200 | [diff] [blame] | 55 | |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 56 | extern void *tall_sgsn_ctx; |
Neels Hofmeyr | ee6cfdc | 2017-07-13 02:03:50 +0200 | [diff] [blame] | 57 | |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 58 | static const struct rate_ctr_desc global_ctr_description[] = { |
| 59 | { "inv-bvci", "Invalid BVC Identifier " }, |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 60 | { "inv-lai", "Invalid Location Area Identifier" }, |
| 61 | { "inv-rai", "Invalid Routing Area Identifier " }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 62 | { "inv-nsei", "No BVC established for NSEI " }, |
Pau Espin Pedrol | 437e209 | 2018-07-24 12:41:26 +0200 | [diff] [blame] | 63 | { "proto-err:bss", "BSSGP protocol error (BSS )" }, |
| 64 | { "proto-err:sgsn", "BSSGP protocol error (SGSN)" }, |
| 65 | { "not-supp:bss", "Feature not supported (BSS )" }, |
| 66 | { "not-supp:sgsn", "Feature not supported (SGSN)" }, |
| 67 | { "restart:sgsn", "Restarted RESET procedure (SGSN)" }, |
| 68 | { "tx-err:sgsn", "NS Transmission error (SGSN)" }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 69 | { "error", "Other error " }, |
Jacob Erlbeck | 006c038 | 2014-05-27 13:49:04 +0200 | [diff] [blame] | 70 | { "mod-peer-err", "Patch error: no peer " }, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
Max | 8a01a80 | 2017-12-20 13:10:11 +0100 | [diff] [blame] | 74 | .group_name_prefix = "gbproxy:global", |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 75 | .group_description = "GBProxy Global Statistics", |
| 76 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 77 | .ctr_desc = global_ctr_description, |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 78 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 81 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc, |
Daniel Willmann | 8bca8de | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 82 | uint16_t ns_bvci); |
Jacob Erlbeck | 5f4ef32 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 83 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
Jacob Erlbeck | f4d60c8 | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 84 | uint16_t ns_bvci, uint16_t sgsn_nsei); |
Jacob Erlbeck | 5f4ef32 | 2014-08-22 17:10:01 +0200 | [diff] [blame] | 85 | |
Harald Welte | 4bf53ef | 2020-12-05 17:50:23 +0100 | [diff] [blame] | 86 | |
| 87 | static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei) |
| 88 | { |
| 89 | return nsei == cfg->nsip_sgsn_nsei; |
| 90 | } |
| 91 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 92 | static int check_bvc_nsei(struct gbproxy_bvc *bvc, uint16_t nsei) |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 93 | { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 94 | OSMO_ASSERT(bvc); |
| 95 | OSMO_ASSERT(bvc->nse); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 96 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 97 | if (bvc->nse->nsei != nsei) { |
| 98 | LOGPBVC(bvc, LOGL_NOTICE, "Peer entry doesn't match current NSEI " |
Daniel Willmann | 6e8ed27 | 2020-12-02 13:54:21 +0100 | [diff] [blame] | 99 | "via NSE(%05u/BSS)\n", nsei); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 100 | rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]); |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 101 | return 0; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Jacob Erlbeck | 64cb924 | 2013-10-24 12:48:56 +0200 | [diff] [blame] | 104 | return 1; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 107 | /* strip off the NS header */ |
| 108 | static void strip_ns_hdr(struct msgb *msg) |
| 109 | { |
| 110 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 111 | msgb_pull(msg, strip_len); |
| 112 | } |
| 113 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 114 | /* feed a message down the NS-VC associated with the specified bvc */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 115 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
Jacob Erlbeck | f4d60c8 | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 116 | uint16_t ns_bvci, uint16_t sgsn_nsei) |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 117 | { |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 118 | /* create a copy of the message so the old one can |
| 119 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 120 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 121 | struct osmo_gprs_ns2_prim nsp = {}; |
Pau Espin Pedrol | 816a67d | 2018-08-15 13:53:48 +0200 | [diff] [blame] | 122 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn"); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 123 | int rc; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 124 | |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 125 | DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n", |
Jacob Erlbeck | f4d60c8 | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 126 | msgb_nsei(msg), ns_bvci, sgsn_nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 127 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 128 | nsp.bvci = ns_bvci; |
| 129 | nsp.nsei = sgsn_nsei; |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 130 | |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 131 | strip_ns_hdr(msg); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 132 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 133 | PRIM_OP_REQUEST, msg); |
| 134 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 135 | if (rc < 0) |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 136 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 137 | return rc; |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 140 | /* feed a message down the NSE */ |
| 141 | static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse, |
Daniel Willmann | 8bca8de | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 142 | uint16_t ns_bvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 143 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 144 | OSMO_ASSERT(nse); |
| 145 | OSMO_ASSERT(nse->cfg); |
| 146 | |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 147 | /* create a copy of the message so the old one can |
| 148 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 149 | struct gprs_ns2_inst *nsi = nse->cfg->nsi; |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 150 | struct osmo_gprs_ns2_prim nsp = {}; |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 151 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse"); |
Harald Welte | 2636e89 | 2020-11-18 12:01:46 +0100 | [diff] [blame] | 152 | uint32_t tlli; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 153 | int rc; |
Harald Welte | 39d0bb5 | 2010-05-12 18:10:25 +0000 | [diff] [blame] | 154 | |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 155 | DEBUGP(DGPRS, "NSE(%05u/SGSN)-BVC(%05u) proxying SGSN->BSS NSE(%05u/BSS)\n", |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 156 | msgb_nsei(msg), ns_bvci, nse->nsei); |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 157 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 158 | nsp.bvci = ns_bvci; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 159 | nsp.nsei = nse->nsei; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 160 | |
Harald Welte | 0ab535b | 2010-05-13 10:34:56 +0200 | [diff] [blame] | 161 | /* Strip the old NS header, it will be replaced with a new one */ |
Harald Welte | 69619e3 | 2010-05-03 19:05:10 +0200 | [diff] [blame] | 162 | strip_ns_hdr(msg); |
| 163 | |
Harald Welte | 2636e89 | 2020-11-18 12:01:46 +0100 | [diff] [blame] | 164 | /* TS 48.018 Section 5.4.2: The link selector parameter is |
| 165 | * defined in 3GPP TS 48.016. At one side of the Gb interface, |
| 166 | * all BSSGP UNITDATA PDUs related to an MS shall be passed with |
| 167 | * the same LSP, e.g. the LSP contains the MS's TLLI, to the |
| 168 | * underlying network service. */ |
| 169 | if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1) |
| 170 | nsp.u.unitdata.link_selector = tlli; |
| 171 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 172 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 173 | PRIM_OP_REQUEST, msg); |
| 174 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 175 | /* FIXME: We need a counter group for gbproxy_nse */ |
| 176 | //if (rc < 0) |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 177 | // rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 178 | |
| 179 | return rc; |
| 180 | } |
| 181 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 182 | /* feed a message down the NS-VC associated with the specified bvc */ |
| 183 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc, |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 184 | uint16_t ns_bvci) |
| 185 | { |
| 186 | int rc; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 187 | struct gbproxy_nse *nse = bvc->nse; |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 188 | OSMO_ASSERT(nse); |
| 189 | |
| 190 | rc = gbprox_relay2nse(old_msg, nse, ns_bvci); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 191 | if (rc < 0) |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 192 | rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 193 | |
| 194 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 197 | static int block_unblock_bvc(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type) |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 198 | { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 199 | struct gbproxy_bvc *bvc; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 200 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 201 | bvc = gbproxy_bvc_by_bvci(cfg, ptp_bvci); |
| 202 | if (!bvc) { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 203 | LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n", |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 204 | ptp_bvci); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 205 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 206 | return -ENOENT; |
| 207 | } |
| 208 | |
| 209 | switch (pdu_type) { |
| 210 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 211 | bvc->blocked = true; |
| 212 | rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 213 | break; |
| 214 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 215 | bvc->blocked = false; |
| 216 | rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 217 | break; |
| 218 | default: |
| 219 | break; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 224 | /* Send a message to a bvc identified by ptp_bvci but using ns_bvci |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 225 | * in the NS hdr */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 226 | static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 227 | uint16_t ns_bvci) |
| 228 | { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 229 | struct gbproxy_bvc *bvc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 230 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 231 | bvc = gbproxy_bvc_by_bvci(cfg, ptp_bvci); |
| 232 | if (!bvc) { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 233 | LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n", |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 234 | ptp_bvci); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 235 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 236 | return -ENOENT; |
Harald Welte | 1c77c6e | 2010-05-03 21:37:11 +0200 | [diff] [blame] | 237 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 238 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 239 | return gbprox_relay2peer(msg, bvc, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 242 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 247 | /* Receive an incoming PTP message from a BSS-side NS-VC */ |
| 248 | static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg, |
| 249 | struct msgb *msg, uint16_t nsei, |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 250 | uint16_t ns_bvci) |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 251 | { |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 252 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 253 | struct gbproxy_bvc *bvc; |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 254 | |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 255 | if (ns_bvci == 0 && ns_bvci == 1) { |
| 256 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) BVCI=%05u is not PTP\n", nsei, ns_bvci); |
| 257 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 258 | } |
| 259 | |
| 260 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) { |
| 261 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/%05u) %s not allowed in PTP BVC\n", |
| 262 | nsei, ns_bvci, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type)); |
| 263 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 264 | } |
| 265 | |
| 266 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) { |
| 267 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/%05u) %s not allowed in uplink direction\n", |
| 268 | nsei, ns_bvci, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type)); |
| 269 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 270 | } |
| 271 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 272 | bvc = gbproxy_bvc_by_bvci(cfg, ns_bvci); |
| 273 | if (!bvc) { |
| 274 | LOGP(DGPRS, LOGL_NOTICE, "BVC(%05u/??) Didn't find bvc " |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 275 | "for PTP message from NSE(%05u/BSS), " |
Jacob Erlbeck | 058ae12 | 2014-10-10 09:07:04 +0200 | [diff] [blame] | 276 | "discarding message\n", |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 277 | ns_bvci, nsei); |
Jacob Erlbeck | 058ae12 | 2014-10-10 09:07:04 +0200 | [diff] [blame] | 278 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 279 | &ns_bvci, msg); |
| 280 | } |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 281 | |
Daniel Willmann | 6626bbc | 2020-12-02 17:46:56 +0100 | [diff] [blame] | 282 | /* TODO: Should we discard this message if the check fails */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 283 | check_bvc_nsei(bvc, nsei); |
Jacob Erlbeck | 43b8f9f | 2014-10-02 14:56:44 +0200 | [diff] [blame] | 284 | |
Jacob Erlbeck | f4d60c8 | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 285 | return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei); |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* Receive an incoming PTP message from a SGSN-side NS-VC */ |
| 289 | static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg, |
| 290 | struct msgb *msg, uint16_t nsei, |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 291 | uint16_t ns_bvci) |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 292 | { |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 293 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 294 | struct gbproxy_bvc *bvc; |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 295 | |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 296 | if (ns_bvci == 0 && ns_bvci == 1) { |
| 297 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) BVCI=%05u is not PTP\n", nsei, ns_bvci); |
| 298 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 299 | } |
| 300 | |
| 301 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) { |
| 302 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/%05u) %s not allowed in PTP BVC\n", |
| 303 | nsei, ns_bvci, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type)); |
| 304 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 305 | } |
| 306 | |
| 307 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) { |
| 308 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/%05u) %s not allowed in downlink direction\n", |
| 309 | nsei, ns_bvci, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type)); |
| 310 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 311 | } |
| 312 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 313 | bvc = gbproxy_bvc_by_bvci(cfg, ns_bvci); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 314 | if (!bvc) { |
| 315 | LOGP(DGPRS, LOGL_INFO, "BVC(%05u/??) Didn't find bvc for " |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 316 | "for message from NSE(%05u/SGSN)\n", |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 317 | ns_bvci, nsei); |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 318 | rate_ctr_inc(&cfg->ctrg-> |
| 319 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 320 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
| 321 | &ns_bvci, msg); |
| 322 | } |
| 323 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 324 | if (bvc->blocked) { |
| 325 | LOGPBVC(bvc, LOGL_NOTICE, "Dropping PDU for " |
Daniel Willmann | 6e8ed27 | 2020-12-02 13:54:21 +0100 | [diff] [blame] | 326 | "blocked BVC via NSE(%05u/SGSN)\n", nsei); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 327 | rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Jacob Erlbeck | 17b42b8 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 328 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg); |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 331 | return gbprox_relay2peer(msg, bvc, ns_bvci); |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 334 | /* process a BVC-RESET message from the BSS side */ |
| 335 | static int gbprox_rx_bvc_reset_from_bss(struct gbproxy_config *cfg, struct msgb *msg, |
Harald Welte | 91bb720 | 2020-12-05 11:18:23 +0100 | [diff] [blame] | 336 | uint16_t nsei, struct tlv_parsed *tp) |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 337 | { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 338 | struct gbproxy_bvc *from_bvc = NULL; |
Harald Welte | 1239cf4 | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 339 | uint16_t bvci; |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 340 | |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 341 | if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) || !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) { |
Harald Welte | fb7f8c5 | 2020-12-02 23:29:38 +0100 | [diff] [blame] | 342 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
| 343 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 344 | } |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 345 | |
Harald Welte | 1239cf4 | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 346 | bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 347 | LOGP(DGPRS, LOGL_INFO, "NSE(%05u) Rx BVC RESET (BVCI=%05u)\n", nsei, bvci); |
| 348 | if (bvci == 0) { |
| 349 | /* If we receive a BVC reset on the signalling endpoint, we |
| 350 | * don't want the SGSN to reset, as the signalling endpoint |
| 351 | * is common for all point-to-point BVCs (and thus all BTS) */ |
Harald Welte | 5525371 | 2020-12-02 23:06:37 +0100 | [diff] [blame] | 352 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 353 | /* Ensure the NSE bvc is there and clear all PtP BVCs */ |
Harald Welte | 5525371 | 2020-12-02 23:06:37 +0100 | [diff] [blame] | 354 | struct gbproxy_nse *nse = gbproxy_nse_by_nsei_or_new(cfg, nsei); |
Harald Welte | 1239cf4 | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 355 | if (!nse) { |
| 356 | LOGP(DGPRS, LOGL_ERROR, "Could not create NSE(%05u)\n", nsei); |
| 357 | bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, 0, msg); |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 358 | return 0; |
| 359 | } |
Harald Welte | 1239cf4 | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 360 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 361 | gbproxy_cleanup_bvcs(cfg, nsei, 0); |
Harald Welte | 1239cf4 | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 362 | |
| 363 | /* FIXME: only do this if SGSN is alive! */ |
Harald Welte | 5525371 | 2020-12-02 23:06:37 +0100 | [diff] [blame] | 364 | LOGPNSE(nse, LOGL_INFO, "Tx fake BVC RESET ACK of BVCI=0\n"); |
Harald Welte | 1239cf4 | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 365 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, nsei, 0, 0); |
| 366 | return 0; |
| 367 | } else { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 368 | from_bvc = gbproxy_bvc_by_bvci(cfg, bvci); |
| 369 | if (!from_bvc) { |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 370 | struct gbproxy_nse *nse = gbproxy_nse_by_nsei(cfg, nsei); |
| 371 | if (!nse) { |
| 372 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for " |
| 373 | "BVCI=%05u\n", nsei, bvci); |
| 374 | bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg); |
| 375 | return 0; |
| 376 | } |
| 377 | /* if a PTP-BVC is reset, and we don't know that |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 378 | * PTP-BVCI yet, we should allocate a new bvc */ |
| 379 | from_bvc = gbproxy_bvc_alloc(nse, bvci); |
| 380 | OSMO_ASSERT(from_bvc); |
| 381 | LOGPBVC(from_bvc, LOGL_INFO, "Allocated new bvc\n"); |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Could have moved to a different NSE */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 385 | if (!check_bvc_nsei(from_bvc, nsei)) { |
| 386 | LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei); |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 387 | |
| 388 | struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei); |
| 389 | if (!nse_new) { |
| 390 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for " |
| 391 | "BVCI=%05u\n", bvci, nsei); |
| 392 | bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg); |
| 393 | return 0; |
| 394 | } |
| 395 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 396 | /* Move bvc to different NSE */ |
| 397 | gbproxy_bvc_move(from_bvc, nse_new); |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 400 | if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) { |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 401 | struct gprs_ra_id raid; |
| 402 | /* We have a Cell Identifier present in this |
| 403 | * PDU, this means we can extend our local |
| 404 | * state information about this particular cell |
| 405 | * */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 406 | memcpy(from_bvc->ra, TLVP_VAL(tp, BSSGP_IE_CELL_ID), sizeof(from_bvc->ra)); |
| 407 | gsm48_parse_ra(&raid, from_bvc->ra); |
| 408 | LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid)); |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 409 | } |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 410 | } |
| 411 | /* continue processing / relaying to SGSN[s] */ |
| 412 | return 1; |
| 413 | } |
| 414 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 415 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 416 | static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg, |
| 417 | struct msgb *msg, uint16_t nsei, |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 418 | uint16_t ns_bvci) |
| 419 | { |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 420 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 421 | struct tlv_parsed tp; |
| 422 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | ca3620a | 2010-05-03 16:30:59 +0200 | [diff] [blame] | 423 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 424 | struct gbproxy_bvc *from_bvc = NULL; |
Harald Welte | 70f38d2 | 2010-05-01 12:10:57 +0200 | [diff] [blame] | 425 | struct gprs_ra_id raid; |
Jacob Erlbeck | 5930064 | 2014-09-08 09:04:01 +0200 | [diff] [blame] | 426 | int rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 427 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 428 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 429 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) BVCI=%05u is not signalling\n", nsei, ns_bvci); |
| 430 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 433 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) { |
| 434 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) %s not allowed in signalling BVC\n", |
| 435 | nsei, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, pdu_type)); |
| 436 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 437 | } |
| 438 | |
| 439 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) { |
| 440 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) %s not allowed in uplink direction\n", |
| 441 | nsei, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, pdu_type)); |
| 442 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 446 | |
| 447 | switch (pdu_type) { |
| 448 | case BSSGP_PDUT_SUSPEND: |
| 449 | case BSSGP_PDUT_RESUME: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 450 | /* We implement RAI snooping during SUSPEND/RESUME, since it |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 451 | * establishes a relationsip between BVCI/bvc and the routeing |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 452 | * area identification. The snooped information is then used |
| 453 | * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct |
| 454 | * BSSGP */ |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 455 | if (!TLVP_PRES_LEN(&tp, BSSGP_IE_ROUTEING_AREA, 6)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 456 | goto err_mand_ie; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 457 | from_bvc = gbproxy_bvc_by_nsei(cfg, nsei); |
| 458 | if (!from_bvc) |
| 459 | goto err_no_bvc; |
| 460 | memcpy(from_bvc->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), |
| 461 | sizeof(from_bvc->ra)); |
| 462 | gsm48_parse_ra(&raid, from_bvc->ra); |
| 463 | LOGPBVC(from_bvc, LOGL_INFO, "BSSGP SUSPEND/RESUME " |
Daniel Willmann | 6e8ed27 | 2020-12-02 13:54:21 +0100 | [diff] [blame] | 464 | "RAI snooping: RAI %s\n", |
| 465 | osmo_rai_name(&raid)); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 466 | /* FIXME: This only supports one BSS per RA */ |
| 467 | break; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 468 | case BSSGP_PDUT_BVC_RESET: |
Harald Welte | 91bb720 | 2020-12-05 11:18:23 +0100 | [diff] [blame] | 469 | rc = gbprox_rx_bvc_reset_from_bss(cfg, msg, nsei, &tp); |
Harald Welte | ac44d6b | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 470 | /* if function retruns 0, we terminate processing here */ |
| 471 | if (rc == 0) |
| 472 | return 0; |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 473 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Jacob Erlbeck | f4d60c8 | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 476 | return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 477 | err_no_bvc: |
| 478 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) cannot find bvc based on NSEI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 479 | nsei); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 480 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Jacob Erlbeck | 17b42b8 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 481 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 482 | err_mand_ie: |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 483 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) missing mandatory RA IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 484 | nsei); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 485 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 486 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 490 | static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp, |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 491 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 492 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 493 | struct gbproxy_nse *nse; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 494 | struct gbproxy_bvc *bvc; |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 495 | unsigned int n_nses = 0; |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 496 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 497 | int i, j; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 498 | |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 499 | /* FIXME: Handle paging logic to only page each matching NSE */ |
| 500 | |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 501 | LOGP(DGPRS, LOGL_INFO, "NSE(%05u/SGSN) BSSGP PAGING\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 502 | nsei); |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 503 | if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) { |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 504 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 505 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 506 | bvc = gbproxy_bvc_by_bvci(cfg, bvci); |
| 507 | if (!bvc) { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 508 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PAGING: " |
| 509 | "unable to route: BVCI=%05u unknown\n", nsei, bvci); |
Harald Welte | 3375fa4 | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 510 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 511 | return -EINVAL; |
| 512 | } |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 513 | LOGPBVC(bvc, LOGL_INFO, "routing by BVCI\n"); |
| 514 | return gbprox_relay2peer(msg, bvc, ns_bvci); |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 515 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) { |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 516 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 517 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 518 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 519 | hash_for_each(nse->bvcs, j, bvc, list) { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 520 | if (!memcmp(bvc->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) { |
Daniel Willmann | bd356a6 | 2020-12-04 01:24:34 +0100 | [diff] [blame] | 521 | LOGPNSE(nse, LOGL_INFO, "routing to NSE (RAI match)\n"); |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 522 | gbprox_relay2nse(msg, nse, ns_bvci); |
| 523 | n_nses++; |
| 524 | /* Only send it once to each NSE */ |
| 525 | break; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 526 | } |
Harald Welte | 3375fa4 | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 527 | } |
| 528 | } |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 529 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) { |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 530 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 531 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 532 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 533 | hash_for_each(nse->bvcs, j, bvc, list) { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 534 | if (!memcmp(bvc->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) { |
Daniel Willmann | 6e8ed27 | 2020-12-02 13:54:21 +0100 | [diff] [blame] | 535 | LOGPNSE(nse, LOGL_INFO, "routing to NSE (LAI match)\n"); |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 536 | gbprox_relay2nse(msg, nse, ns_bvci); |
| 537 | n_nses++; |
| 538 | /* Only send it once to each NSE */ |
| 539 | break; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 540 | } |
Harald Welte | 3375fa4 | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 541 | } |
| 542 | } |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 543 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1)) { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 544 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 545 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 546 | hash_for_each(nse->bvcs, j, bvc, list) { |
Daniel Willmann | 6e8ed27 | 2020-12-02 13:54:21 +0100 | [diff] [blame] | 547 | LOGPNSE(nse, LOGL_INFO, "routing to NSE (broadcast)\n"); |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 548 | gbprox_relay2nse(msg, nse, ns_bvci); |
| 549 | n_nses++; |
| 550 | /* Only send it once to each NSE */ |
| 551 | break; |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 552 | } |
Harald Welte | 8553f55 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 553 | } |
| 554 | } else { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 555 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: " |
Harald Welte | 8553f55 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 556 | "unable to route, missing IE\n", nsei); |
| 557 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 558 | } |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 559 | |
Daniel Willmann | 5937dfd | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 560 | if (n_nses == 0) { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 561 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: " |
Harald Welte | 8553f55 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 562 | "unable to route, no destination found\n", nsei); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 563 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 564 | return -EINVAL; |
Harald Welte | 4cf12e9 | 2010-05-13 14:14:56 +0200 | [diff] [blame] | 565 | } |
Harald Welte | 3375fa4 | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 566 | return 0; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 569 | /* Receive an incoming BVC-RESET message from the SGSN */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 570 | static int rx_reset_from_sgsn(struct gbproxy_config *cfg, |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 571 | struct msgb *orig_msg, |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 572 | struct msgb *msg, struct tlv_parsed *tp, |
| 573 | uint32_t nsei, uint16_t ns_bvci) |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 574 | { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 575 | struct gbproxy_nse *nse; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 576 | struct gbproxy_bvc *bvc; |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 577 | uint16_t ptp_bvci; |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 578 | int i, j; |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 579 | |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 580 | if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) { |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 581 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 582 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 583 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 584 | NULL, orig_msg); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 585 | } |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 586 | ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 587 | |
| 588 | if (ptp_bvci >= 2) { |
| 589 | /* A reset for a PTP BVC was received, forward it to its |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 590 | * respective bvc */ |
| 591 | bvc = gbproxy_bvc_by_bvci(cfg, ptp_bvci); |
| 592 | if (!bvc) { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 593 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BVCI=%05u: Cannot find BSS\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 594 | nsei, ptp_bvci); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 595 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 596 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 597 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, |
Jacob Erlbeck | 17b42b8 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 598 | &ptp_bvci, orig_msg); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 599 | } |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 600 | return gbprox_relay2peer(msg, bvc, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* A reset for the Signalling entity has been received |
| 604 | * from the SGSN. As the signalling BVCI is shared |
| 605 | * among all the BSS's that we multiplex, it needs to |
| 606 | * be relayed */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 607 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 608 | hash_for_each(nse->bvcs, j, bvc, list) |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 609 | gbprox_relay2peer(msg, bvc, ns_bvci); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 610 | } |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 615 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 616 | static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg, |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 617 | struct msgb *orig_msg, uint32_t nsei, |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 618 | uint16_t ns_bvci) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 619 | { |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 620 | struct bssgp_normal_hdr *bgph = |
| 621 | (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 622 | struct tlv_parsed tp; |
| 623 | uint8_t pdu_type = bgph->pdu_type; |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 624 | int data_len; |
Harald Welte | c8d98ac | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 625 | struct gbproxy_nse *nse; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 626 | struct gbproxy_bvc *bvc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 627 | uint16_t bvci; |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 628 | struct msgb *msg; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 629 | int rc = 0; |
Jacob Erlbeck | 1e65b0e | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 630 | int cause; |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 631 | int i; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 632 | |
Harald Welte | c471d3d | 2011-02-06 17:13:12 +0100 | [diff] [blame] | 633 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 634 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BVCI=%05u is not signalling\n", nsei, ns_bvci); |
| 635 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 638 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) { |
| 639 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) %s not allowed in signalling BVC\n", |
| 640 | nsei, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, pdu_type)); |
| 641 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
| 642 | } |
| 643 | |
| 644 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) { |
| 645 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) %s not allowed in downlink direction\n", |
| 646 | nsei, osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, pdu_type)); |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 647 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Pau Espin Pedrol | 816a67d | 2018-08-15 13:53:48 +0200 | [diff] [blame] | 650 | msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn"); |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 651 | /* Update message info */ |
| 652 | bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 653 | data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 654 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 655 | |
| 656 | switch (pdu_type) { |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 657 | case BSSGP_PDUT_BVC_RESET: |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 658 | rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 659 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 660 | case BSSGP_PDUT_BVC_RESET_ACK: |
Daniel Willmann | 15c9da2 | 2020-11-03 21:12:42 +0100 | [diff] [blame] | 661 | /* simple case: BVCI IE is mandatory */ |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 662 | if (!TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) |
Daniel Willmann | 15c9da2 | 2020-11-03 21:12:42 +0100 | [diff] [blame] | 663 | goto err_mand_ie; |
| 664 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 665 | if (bvci == BVCI_SIGNALLING) { |
| 666 | /* TODO: Reset all PTP BVCIs */ |
| 667 | } else { |
| 668 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
| 669 | } |
| 670 | break; |
Jacob Erlbeck | c1c57d3 | 2014-08-28 13:20:39 +0200 | [diff] [blame] | 671 | case BSSGP_PDUT_FLUSH_LL: |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 672 | /* simple case: BVCI IE is mandatory */ |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 673 | if (!TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 674 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 675 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 676 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 677 | break; |
| 678 | case BSSGP_PDUT_PAGING_PS: |
| 679 | case BSSGP_PDUT_PAGING_CS: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 680 | /* process the paging request (LAI/RAI lookup) */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 681 | rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 682 | break; |
| 683 | case BSSGP_PDUT_STATUS: |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 684 | /* Some exception has occurred */ |
Harald Welte | 44c4830 | 2010-05-03 19:22:32 +0200 | [diff] [blame] | 685 | LOGP(DGPRS, LOGL_NOTICE, |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 686 | "NSE(%05u/SGSN) BSSGP STATUS ", nsei); |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 687 | if (!TLVP_PRES_LEN(&tp, BSSGP_IE_CAUSE, 1)) { |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 688 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 689 | goto err_mand_ie; |
| 690 | } |
Jacob Erlbeck | 1e65b0e | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 691 | cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 692 | LOGPC(DGPRS, LOGL_NOTICE, |
| 693 | "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE), |
Jacob Erlbeck | 1e65b0e | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 694 | bssgp_cause_str(cause)); |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 695 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) { |
Jacob Erlbeck | 1e65b0e | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 696 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 697 | LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci); |
Jacob Erlbeck | 1e65b0e | 2014-08-29 13:32:17 +0200 | [diff] [blame] | 698 | |
| 699 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI) |
| 700 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | 0a4050c | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 701 | } else |
| 702 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 703 | break; |
| 704 | /* those only exist in the SGSN -> BSS direction */ |
| 705 | case BSSGP_PDUT_SUSPEND_ACK: |
| 706 | case BSSGP_PDUT_SUSPEND_NACK: |
| 707 | case BSSGP_PDUT_RESUME_ACK: |
| 708 | case BSSGP_PDUT_RESUME_NACK: |
Jacob Erlbeck | 8f50359 | 2014-06-02 10:49:00 +0200 | [diff] [blame] | 709 | /* RAI IE is mandatory */ |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 710 | if (!TLVP_PRES_LEN(&tp, BSSGP_IE_ROUTEING_AREA, 6)) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 711 | goto err_mand_ie; |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 712 | bvc = gbproxy_bvc_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA)); |
| 713 | if (!bvc) |
| 714 | goto err_no_bvc; |
| 715 | rc = gbprox_relay2peer(msg, bvc, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 716 | break; |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 717 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 718 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
Harald Welte | 6c4c6f0 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 719 | if (!TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 720 | goto err_mand_ie; |
Holger Hans Peter Freyther | ffd6856 | 2012-09-04 20:42:20 +0200 | [diff] [blame] | 721 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 722 | if (bvci == 0) { |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 723 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP " |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 724 | "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei, |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 725 | pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":""); |
Daniel Willmann | 6626bbc | 2020-12-02 17:46:56 +0100 | [diff] [blame] | 726 | /* TODO: should we send STATUS ? */ |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 727 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 728 | ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 729 | } else { |
| 730 | /* Mark BVC as (un)blocked */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 731 | block_unblock_bvc(cfg, bvci, pdu_type); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 732 | } |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 733 | rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci); |
Harald Welte | 36f98d9 | 2011-02-06 13:09:29 +0100 | [diff] [blame] | 734 | break; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 735 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | c8d98ac | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 736 | case BSSGP_PDUT_OVERLOAD: |
| 737 | LOGP(DGPRS, LOGL_DEBUG, |
| 738 | "NSE(%05u/SGSN) BSSGP %s: broadcasting\n", nsei, bssgp_pdu_str(pdu_type)); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 739 | /* broadcast to all BSS-side bvcs */ |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 740 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | c8d98ac | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 741 | gbprox_relay2nse(msg, nse, 0); |
| 742 | } |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 743 | break; |
| 744 | default: |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 745 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PDU type %s not supported\n", nsei, |
| 746 | bssgp_pdu_str(pdu_type)); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 747 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 748 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 749 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 753 | msgb_free(msg); |
| 754 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 755 | return rc; |
| 756 | err_mand_ie: |
Daniel Willmann | 103a7ec | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 757 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) missing mandatory IE\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 758 | nsei); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 759 | rate_ctr_inc(&cfg->ctrg-> |
Jacob Erlbeck | bc55574 | 2013-10-18 14:34:55 +0200 | [diff] [blame] | 760 | ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 761 | msgb_free(msg); |
| 762 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg); |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 763 | err_no_bvc: |
| 764 | LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) cannot find bvc based on RAI\n", |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 765 | nsei); |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 766 | rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 767 | msgb_free(msg); |
Jacob Erlbeck | 17b42b8 | 2014-08-29 12:20:15 +0200 | [diff] [blame] | 768 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 771 | int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg) |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 772 | { |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 773 | int rc; |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 774 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 775 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 776 | struct osmo_gprs_ns2_prim nsp = {}; |
| 777 | |
| 778 | nsp.bvci = msgb_bvci(msg); |
| 779 | nsp.nsei = msgb_nsei(msg); |
| 780 | |
| 781 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg); |
| 782 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
| 783 | |
| 784 | return rc; |
| 785 | } |
| 786 | |
| 787 | /* Main input function for Gb proxy */ |
| 788 | int gbprox_rcvmsg(void *ctx, struct msgb *msg) |
| 789 | { |
| 790 | int rc; |
| 791 | uint16_t nsei = msgb_nsei(msg); |
| 792 | uint16_t ns_bvci = msgb_bvci(msg); |
| 793 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 794 | |
Jacob Erlbeck | f4d60c8 | 2014-08-26 14:47:15 +0200 | [diff] [blame] | 795 | int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 796 | |
Harald Welte | d651edc | 2020-12-06 13:35:24 +0100 | [diff] [blame^] | 797 | /* ensure minimum length to decode PCU type */ |
| 798 | if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr)) |
| 799 | return bssgp_tx_status(BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg); |
| 800 | |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 801 | /* Only BVCI=0 messages need special treatment */ |
| 802 | if (ns_bvci == 0 || ns_bvci == 1) { |
Jacob Erlbeck | e75fec6 | 2013-10-15 12:00:27 +0200 | [diff] [blame] | 803 | if (remote_end_is_sgsn) |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 804 | rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 805 | else |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 806 | rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci); |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 807 | } else { |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 808 | /* All other BVCI are PTP */ |
| 809 | if (remote_end_is_sgsn) |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 810 | rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei, |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 811 | ns_bvci); |
| 812 | else |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 813 | rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei, |
Jacob Erlbeck | 46f1d6f | 2014-08-21 13:45:04 +0200 | [diff] [blame] | 814 | ns_bvci); |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Harald Welte | 672f5c4 | 2010-05-03 18:54:58 +0200 | [diff] [blame] | 817 | return rc; |
Harald Welte | 9f75c35 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 818 | } |
Harald Welte | 85801d0 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 819 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 820 | /* TODO: What about handling: |
| 821 | * NS_AFF_CAUSE_VC_FAILURE, |
| 822 | NS_AFF_CAUSE_VC_RECOVERY, |
| 823 | NS_AFF_CAUSE_FAILURE, |
| 824 | NS_AFF_CAUSE_RECOVERY, |
| 825 | osmocom own causes |
| 826 | NS_AFF_CAUSE_SNS_CONFIGURED, |
| 827 | NS_AFF_CAUSE_SNS_FAILURE, |
| 828 | */ |
Harald Welte | 1ccbf44 | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 829 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 830 | void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp) |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 831 | { |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 832 | /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, bvc->bvci, 0); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 833 | * TODO: sgsn nsei available/unavailable |
| 834 | */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 835 | struct gbproxy_bvc *bvc; |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 836 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 837 | switch (nsp->u.status.cause) { |
| 838 | case NS_AFF_CAUSE_SNS_FAILURE: |
| 839 | case NS_AFF_CAUSE_SNS_CONFIGURED: |
| 840 | break; |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 841 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 842 | case NS_AFF_CAUSE_RECOVERY: |
| 843 | LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei); |
Harald Welte | 4bf53ef | 2020-12-05 17:50:23 +0100 | [diff] [blame] | 844 | if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) { |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 845 | /* look-up or create the BTS context for this BVC */ |
| 846 | struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei); |
| 847 | if (!bctx) |
| 848 | bctx = btsctx_alloc(nsp->bvci, nsp->nsei); |
Harald Welte | f69c059 | 2010-05-11 18:29:44 +0200 | [diff] [blame] | 849 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 850 | bssgp_tx_bvc_reset_nsei_bvci(cfg->nsip_sgsn_nsei, 0, BSSGP_CAUSE_OML_INTERV, NULL, 0); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 851 | } |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 852 | break; |
| 853 | case NS_AFF_CAUSE_FAILURE: |
Harald Welte | 4bf53ef | 2020-12-05 17:50:23 +0100 | [diff] [blame] | 854 | if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) { |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 855 | /* sgsn */ |
| 856 | /* TODO: BSVC: block all PtP towards bss */ |
| 857 | rate_ctr_inc(&cfg->ctrg-> |
| 858 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
| 859 | } else { |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 860 | /* bss became unavailable |
| 861 | * TODO: Block all BVC belonging to that NSE */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 862 | bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei); |
| 863 | if (!bvc) { |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 864 | /* TODO: use primitive name + status cause name */ |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 865 | LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n", |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 866 | nsp->u.status.cause, nsp->nsei); |
Harald Welte | 5e106d7 | 2011-02-06 16:33:29 +0100 | [diff] [blame] | 867 | break; |
| 868 | } |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 869 | |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 870 | if (!bvc->blocked) |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 871 | break; |
| 872 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei, |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 873 | bvc->bvci, 0); |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 874 | } |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 875 | LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei); |
| 876 | break; |
| 877 | default: |
Harald Welte | d97ff68 | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 878 | LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n", |
| 879 | gprs_ns2_aff_cause_prim_str(nsp->u.status.cause)); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 880 | break; |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 881 | } |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 884 | /* called by the ns layer */ |
| 885 | int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 886 | { |
| 887 | struct osmo_gprs_ns2_prim *nsp; |
| 888 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 889 | uintptr_t bvci; |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 890 | int rc = 0; |
| 891 | |
| 892 | if (oph->sap != SAP_NS) |
| 893 | return 0; |
| 894 | |
| 895 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 896 | |
| 897 | if (oph->operation != PRIM_OP_INDICATION) { |
Harald Welte | d97ff68 | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 898 | LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n", |
| 899 | get_value_string(osmo_prim_op_names, oph->operation)); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | switch (oph->primitive) { |
| 904 | case PRIM_NS_UNIT_DATA: |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 905 | |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 906 | /* hand the message into the BSSGP implementation */ |
| 907 | msgb_bssgph(oph->msg) = oph->msg->l3h; |
| 908 | msgb_bvci(oph->msg) = nsp->bvci; |
| 909 | msgb_nsei(oph->msg) = nsp->nsei; |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 910 | bvci = nsp->bvci | BVC_LOG_CTX_FLAG; |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 911 | |
Daniel Willmann | e8c8ec9 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 912 | log_set_context(LOG_CTX_GB_BVC, (void *)bvci); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 913 | rc = gbprox_rcvmsg(cfg, oph->msg); |
Daniel Willmann | 02f2c34 | 2020-11-04 17:32:56 +0100 | [diff] [blame] | 914 | msgb_free(oph->msg); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 915 | break; |
| 916 | case PRIM_NS_STATUS: |
| 917 | gprs_ns_prim_status_cb(cfg, nsp); |
| 918 | break; |
| 919 | default: |
Harald Welte | d97ff68 | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 920 | LOGP(DPCU, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n", |
| 921 | gprs_ns2_prim_str(oph->primitive), |
| 922 | get_value_string(osmo_prim_op_names, oph->operation)); |
Alexander Couzens | 82182d0 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 923 | break; |
| 924 | } |
| 925 | |
| 926 | return rc; |
Harald Welte | c1c1dd2 | 2010-05-11 06:34:24 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 929 | void gbprox_reset(struct gbproxy_config *cfg) |
Jacob Erlbeck | 72b401f | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 930 | { |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 931 | struct gbproxy_nse *nse; |
| 932 | struct hlist_node *ntmp; |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 933 | int i, j; |
Jacob Erlbeck | 72b401f | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 934 | |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 935 | hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) { |
Harald Welte | 993d3f4 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 936 | struct gbproxy_bvc *bvc; |
| 937 | struct hlist_node *tmp; |
| 938 | hash_for_each_safe(nse->bvcs, j, tmp, bvc, list) |
Harald Welte | 8cd7440 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 939 | gbproxy_bvc_free(bvc); |
Daniel Willmann | 447ad44 | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 940 | |
| 941 | gbproxy_nse_free(nse); |
| 942 | } |
Jacob Erlbeck | 72b401f | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 943 | |
Holger Hans Peter Freyther | eece627 | 2014-08-04 15:42:36 +0200 | [diff] [blame] | 944 | rate_ctr_group_free(cfg->ctrg); |
| 945 | gbproxy_init_config(cfg); |
Jacob Erlbeck | 72b401f | 2013-10-24 12:48:55 +0200 | [diff] [blame] | 946 | } |
| 947 | |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 948 | int gbproxy_init_config(struct gbproxy_config *cfg) |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 949 | { |
Jacob Erlbeck | 0d37671 | 2014-08-11 19:12:24 +0200 | [diff] [blame] | 950 | struct timespec tp; |
Jacob Erlbeck | 9a83d7a | 2014-09-25 11:17:31 +0200 | [diff] [blame] | 951 | |
Harald Welte | 78db244 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 952 | hash_init(cfg->bss_nses); |
Pau Espin Pedrol | b1d1c24 | 2018-10-30 17:27:59 +0100 | [diff] [blame] | 953 | cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0); |
Harald Welte | 26c1465 | 2017-07-12 00:25:51 +0200 | [diff] [blame] | 954 | if (!cfg->ctrg) { |
| 955 | LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n"); |
| 956 | return -1; |
| 957 | } |
Pau Espin Pedrol | 36abead | 2018-08-17 13:27:20 +0200 | [diff] [blame] | 958 | osmo_clock_gettime(CLOCK_REALTIME, &tp); |
Jacob Erlbeck | 9a83d7a | 2014-09-25 11:17:31 +0200 | [diff] [blame] | 959 | |
Holger Hans Peter Freyther | 18739ea | 2014-08-04 11:10:09 +0200 | [diff] [blame] | 960 | return 0; |
Jacob Erlbeck | 4211d79 | 2013-10-24 12:48:23 +0200 | [diff] [blame] | 961 | } |