Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 3 | /* (C) 2010-2020 by Harald Welte <laforge@gnumonks.org> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by On-Waves |
| 5 | * (C) 2013 by Holger Hans Peter Freyther |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 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 |
| 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 |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 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/>. |
| 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> |
| 31 | #include <arpa/inet.h> |
| 32 | #include <time.h> |
| 33 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 34 | #include <osmocom/core/hashtable.h> |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 35 | #include <osmocom/core/logging.h> |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 36 | #include <osmocom/core/linuxlist.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
| 38 | #include <osmocom/core/select.h> |
| 39 | #include <osmocom/core/rate_ctr.h> |
Daniel Willmann | 1ac920b | 2021-02-11 23:51:49 +0100 | [diff] [blame] | 40 | #include <osmocom/core/signal.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 41 | #include <osmocom/core/stats.h> |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 42 | #include <osmocom/core/utils.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 43 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 44 | #include <osmocom/gprs/gprs_ns2.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 45 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 46 | #include <osmocom/gprs/gprs_bssgp2.h> |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 47 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 48 | #include <osmocom/gprs/bssgp_bvc_fsm.h> |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 49 | #include <osmocom/gprs/protocol/gsm_08_18.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 50 | |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 51 | #include <osmocom/gsm/gsm23236.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 52 | #include <osmocom/gsm/gsm_utils.h> |
| 53 | |
Oliver Smith | 29532c2 | 2021-01-29 11:13:00 +0100 | [diff] [blame] | 54 | #include "debug.h" |
Daniel Willmann | a16ecc3 | 2021-03-10 09:57:12 +0100 | [diff] [blame] | 55 | #include <osmocom/gbproxy/gb_proxy.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 56 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 57 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 58 | |
| 59 | extern void *tall_sgsn_ctx; |
| 60 | |
| 61 | static const struct rate_ctr_desc global_ctr_description[] = { |
Daniel Willmann | 99a46ff | 2021-09-27 15:39:44 +0200 | [diff] [blame] | 62 | [GBPROX_GLOB_CTR_INV_BVCI] = { "inv-bvci", "Invalid BVC Identifier " }, |
| 63 | [GBPROX_GLOB_CTR_INV_LAI] = { "inv-lai", "Invalid Location Area Identifier" }, |
| 64 | [GBPROX_GLOB_CTR_INV_RAI] = { "inv-rai", "Invalid Routing Area Identifier " }, |
| 65 | [GBPROX_GLOB_CTR_INV_NSEI] = { "inv-nsei", "No BVC established for NSEI " }, |
| 66 | [GBPROX_GLOB_CTR_PROTO_ERR_BSS] = { "proto-err:bss", "BSSGP protocol error (BSS )" }, |
| 67 | [GBPROX_GLOB_CTR_PROTO_ERR_SGSN] = { "proto-err:sgsn", "BSSGP protocol error (SGSN)" }, |
| 68 | [GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS] = { "not-supp:bss", "Feature not supported (BSS )" }, |
| 69 | [GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN] = { "not-supp:sgsn", "Feature not supported (SGSN)" }, |
| 70 | [GBPROX_GLOB_CTR_RESTART_RESET_SGSN] = { "restart:sgsn", "Restarted RESET procedure (SGSN)" }, |
| 71 | [GBPROX_GLOB_CTR_TX_ERR_SGSN] = { "tx-err:sgsn", "NS Transmission error (SGSN)" }, |
| 72 | [GBPROX_GLOB_CTR_OTHER_ERR] = { "error", "Other error " }, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
| 76 | .group_name_prefix = "gbproxy:global", |
| 77 | .group_description = "GBProxy Global Statistics", |
| 78 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 79 | .ctr_desc = global_ctr_description, |
| 80 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 81 | }; |
| 82 | |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 83 | int tx_status(struct gbproxy_nse *nse, uint16_t ns_bvci, enum gprs_bssgp_cause cause, const uint16_t *bvci, const struct msgb *old_msg) |
| 84 | { |
| 85 | int rc; |
Harald Welte | a0f7073 | 2020-12-05 17:50:23 +0100 | [diff] [blame] | 86 | |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 87 | struct msgb *msg = bssgp2_enc_status(cause, bvci, old_msg, nse->max_sdu_len); |
| 88 | if (!msg) { |
| 89 | LOGPNSE(nse, LOGL_NOTICE, "Unable to encode STATUS message\n"); |
| 90 | return -ENOMEM; |
| 91 | } |
| 92 | |
| 93 | rc = bssgp2_nsi_tx_ptp(nse->cfg->nsi, nse->nsei, ns_bvci, msg, 0); |
| 94 | if (rc < 0) |
| 95 | LOGPNSE(nse, LOGL_NOTICE, "Unable to send STATUS message\n"); |
| 96 | return rc; |
| 97 | } |
| 98 | |
| 99 | /* generate BVC-STATUS mess |
| 100 | age with cause value derived from TLV-parser error */ |
| 101 | static int tx_status_from_tlvp(struct gbproxy_nse *nse, enum osmo_tlv_parser_error tlv_p_err, struct msgb *orig_msg) |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 102 | { |
| 103 | uint8_t bssgp_cause; |
| 104 | switch (tlv_p_err) { |
| 105 | case OSMO_TLVP_ERR_MAND_IE_MISSING: |
| 106 | bssgp_cause = BSSGP_CAUSE_MISSING_MAND_IE; |
| 107 | break; |
| 108 | default: |
| 109 | bssgp_cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC; |
| 110 | } |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 111 | return tx_status(nse, msgb_bvci(orig_msg), bssgp_cause, NULL, orig_msg); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 114 | /* strip off the NS header */ |
| 115 | static void strip_ns_hdr(struct msgb *msg) |
| 116 | { |
| 117 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 118 | msgb_pull(msg, strip_len); |
| 119 | } |
| 120 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 121 | #if 0 |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 122 | /* feed a message down the NS-VC associated with the specified bvc */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 123 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
| 124 | uint16_t ns_bvci, uint16_t sgsn_nsei) |
| 125 | { |
| 126 | /* create a copy of the message so the old one can |
| 127 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 128 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 129 | struct osmo_gprs_ns2_prim nsp = {}; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 130 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn"); |
| 131 | int rc; |
| 132 | |
Daniel Willmann | 3696dce | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 133 | DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n", |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 134 | msgb_nsei(msg), ns_bvci, sgsn_nsei); |
| 135 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 136 | nsp.bvci = ns_bvci; |
| 137 | nsp.nsei = sgsn_nsei; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 138 | |
| 139 | strip_ns_hdr(msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 140 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 141 | PRIM_OP_REQUEST, msg); |
| 142 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 143 | if (rc < 0) |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 144 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_TX_ERR_SGSN)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 145 | return rc; |
| 146 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 147 | #endif |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 148 | |
Harald Welte | e30985e | 2021-01-28 19:13:19 +0100 | [diff] [blame] | 149 | /*! Determine the TLLI from the given BSSGP message. |
| 150 | * \param[in] bssgp pointer to start of BSSGP header |
| 151 | * \param[in] bssgp_len length of BSSGP message in octets |
| 152 | * \param[out] tlli TLLI (if any) in host byte order |
| 153 | * \returns 1 if TLLI found; 0 if none found; negative on parse error */ |
| 154 | int gprs_gb_parse_tlli(const uint8_t *bssgp, size_t bssgp_len, uint32_t *tlli) |
| 155 | { |
| 156 | const struct bssgp_normal_hdr *bgph; |
| 157 | uint8_t pdu_type; |
| 158 | |
| 159 | if (bssgp_len < sizeof(struct bssgp_normal_hdr)) |
| 160 | return -EINVAL; |
| 161 | |
| 162 | bgph = (struct bssgp_normal_hdr *)bssgp; |
| 163 | pdu_type = bgph->pdu_type; |
| 164 | |
| 165 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 166 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 167 | const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *)bssgp; |
| 168 | if (bssgp_len < sizeof(struct bssgp_ud_hdr)) |
| 169 | return -EINVAL; |
| 170 | *tlli = osmo_load32be((const uint8_t *)&budh->tlli); |
| 171 | return 1; |
| 172 | } else { |
| 173 | const uint8_t *data = bgph->data; |
| 174 | size_t data_len = bssgp_len - sizeof(*bgph); |
| 175 | struct tlv_parsed tp; |
| 176 | |
| 177 | if (bssgp_tlv_parse(&tp, data, data_len) < 0) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) { |
| 181 | *tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI)); |
| 182 | return 1; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* No TLLI present in message */ |
| 187 | return 0; |
| 188 | } |
| 189 | |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 190 | /* feed a message down the NSE */ |
| 191 | static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse, |
Daniel Willmann | 35f7d33 | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 192 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 193 | { |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 194 | OSMO_ASSERT(nse); |
| 195 | OSMO_ASSERT(nse->cfg); |
| 196 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 197 | /* create a copy of the message so the old one can |
| 198 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 199 | struct gprs_ns2_inst *nsi = nse->cfg->nsi; |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 200 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse"); |
Daniel Willmann | 44fa201 | 2021-02-12 04:55:40 +0100 | [diff] [blame] | 201 | uint32_t tlli = 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 202 | int rc; |
| 203 | |
Daniel Willmann | 98b1b45 | 2020-12-21 10:40:27 +0100 | [diff] [blame] | 204 | DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg), |
| 205 | !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS"); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 206 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 207 | /* Strip the old NS header, it will be replaced with a new one */ |
| 208 | strip_ns_hdr(msg); |
| 209 | |
Harald Welte | fe05958 | 2020-11-18 12:01:46 +0100 | [diff] [blame] | 210 | /* TS 48.018 Section 5.4.2: The link selector parameter is |
| 211 | * defined in 3GPP TS 48.016. At one side of the Gb interface, |
| 212 | * all BSSGP UNITDATA PDUs related to an MS shall be passed with |
| 213 | * the same LSP, e.g. the LSP contains the MS's TLLI, to the |
| 214 | * underlying network service. */ |
Daniel Willmann | 44fa201 | 2021-02-12 04:55:40 +0100 | [diff] [blame] | 215 | gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli); |
Harald Welte | fe05958 | 2020-11-18 12:01:46 +0100 | [diff] [blame] | 216 | |
Daniel Willmann | 44fa201 | 2021-02-12 04:55:40 +0100 | [diff] [blame] | 217 | rc = bssgp2_nsi_tx_ptp(nsi, nse->nsei, ns_bvci, msg, tlli); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 218 | /* FIXME: We need a counter group for gbproxy_nse */ |
| 219 | //if (rc < 0) |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 220 | // rate_ctr_inc(rate_ctr_group_get_ctr(bvc->ctrg, GBPROX_PEER_CTR_TX_ERR)); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 221 | |
| 222 | return rc; |
| 223 | } |
| 224 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 225 | /* feed a message down the NS-VC associated with the specified bvc */ |
| 226 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc, |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 227 | uint16_t ns_bvci) |
| 228 | { |
| 229 | int rc; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 230 | struct gbproxy_nse *nse = bvc->nse; |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 231 | OSMO_ASSERT(nse); |
| 232 | |
| 233 | rc = gbprox_relay2nse(old_msg, nse, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 234 | if (rc < 0) |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 235 | rate_ctr_inc(rate_ctr_group_get_ctr(bvc->ctrg, GBPROX_PEER_CTR_TX_ERR)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 236 | |
| 237 | return rc; |
| 238 | } |
| 239 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 240 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 241 | { |
| 242 | return 0; |
| 243 | } |
| 244 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 245 | |
| 246 | /*********************************************************************** |
| 247 | * PTP BVC handling |
| 248 | ***********************************************************************/ |
| 249 | |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 250 | /* FIXME: Handle the tlli NULL case correctly, |
| 251 | * This function should take a generic selector |
| 252 | * and choose an sgsn based on that |
| 253 | */ |
| 254 | static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli) |
| 255 | { |
| 256 | struct gbproxy_sgsn *sgsn = NULL; |
| 257 | struct gbproxy_sgsn *sgsn_avoid = NULL; |
| 258 | |
| 259 | int tlli_type; |
| 260 | int16_t nri; |
| 261 | bool null_nri = false; |
| 262 | |
| 263 | if (!tlli) { |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 264 | sgsn = gbproxy_sgsn_by_available(cfg); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 265 | if (!sgsn) { |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 266 | LOGP(DGPRS, LOGL_ERROR, "Could not find any available SGSN\n"); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 269 | LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first available SGSN\n"); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 270 | return sgsn; |
| 271 | } |
| 272 | |
| 273 | if (cfg->pool.nri_bitlen == 0) { |
| 274 | /* Pooling is disabled */ |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 275 | sgsn = gbproxy_sgsn_by_available(cfg); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 276 | if (!sgsn) { |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 277 | LOGP(DGPRS, LOGL_ERROR, "Could not find any available SGSN\n"); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 278 | return NULL; |
| 279 | } |
| 280 | |
Daniel Willmann | 33dcdad | 2022-10-10 16:29:52 +0200 | [diff] [blame] | 281 | LOGPSGSN(sgsn, LOGL_DEBUG, "Pooling disabled, using first available SGSN\n"); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 282 | } else { |
| 283 | /* Pooling is enabled, try to use the NRI for routing to an SGSN |
| 284 | * See 3GPP TS 23.236 Ch. 5.3.2 */ |
| 285 | tlli_type = gprs_tlli_type(*tlli); |
| 286 | if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) { |
| 287 | /* Only get/use the NRI if tlli type is local */ |
| 288 | osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen); |
| 289 | if (nri >= 0) { |
| 290 | /* Get the SGSN for the NRI */ |
| 291 | sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri); |
| 292 | if (sgsn && !null_nri) |
| 293 | return sgsn; |
| 294 | /* If the NRI is the null NRI, we need to avoid the chosen SGSN */ |
| 295 | if (null_nri && sgsn) { |
| 296 | sgsn_avoid = sgsn; |
| 297 | } |
| 298 | } else { |
| 299 | /* We couldn't get the NRI from the TLLI */ |
Daniel Willmann | cd21afe | 2021-01-21 18:44:51 +0100 | [diff] [blame] | 300 | LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %08x\n", *tlli); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 301 | } |
Daniel Willmann | cd21afe | 2021-01-21 18:44:51 +0100 | [diff] [blame] | 302 | } else { |
| 303 | LOGP(DGPRS, LOGL_INFO, "TLLI %08x is neither local nor foreign, not routing by NRI\n", *tlli); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid |
| 308 | * NOTE: This function is not stable if the number of SGSNs or allow_attach changes |
| 309 | * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that |
| 310 | * we can just wait for the MS to reattempt the procedure. |
| 311 | */ |
| 312 | if (!sgsn) |
| 313 | sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli); |
| 314 | |
| 315 | if (!sgsn) { |
| 316 | LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli); |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | return sgsn; |
| 321 | } |
| 322 | |
| 323 | /*! Find the correct gbproxy_bvc given a cell and an SGSN |
| 324 | * \param[in] cfg The gbproxy configuration |
| 325 | * \param[in] cell The cell the message belongs to |
| 326 | * \param[in] tlli An optional TLLI used for tracking |
| 327 | * \return Returns 0 on success, otherwise a negative value |
| 328 | */ |
| 329 | static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli) |
| 330 | { |
| 331 | struct gbproxy_sgsn *sgsn; |
| 332 | struct gbproxy_bvc *sgsn_bvc = NULL; |
Harald Welte | 02d7c48 | 2020-12-30 12:13:36 +0100 | [diff] [blame] | 333 | int i; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 334 | |
| 335 | sgsn = gbproxy_select_sgsn(cfg, tlli); |
| 336 | if (!sgsn) { |
| 337 | LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n"); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | /* Get the BVC for this SGSN/NSE */ |
Harald Welte | 02d7c48 | 2020-12-30 12:13:36 +0100 | [diff] [blame] | 342 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 343 | sgsn_bvc = cell->sgsn_bvc[i]; |
| 344 | if (!sgsn_bvc) |
| 345 | continue; |
| 346 | if (sgsn->nse != sgsn_bvc->nse) |
| 347 | continue; |
Daniel Willmann | 5ceeb2b | 2022-02-23 18:00:48 +0100 | [diff] [blame] | 348 | if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) |
| 349 | continue; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 350 | |
| 351 | return sgsn_bvc; |
| 352 | } |
| 353 | |
| 354 | /* This shouldn't happen */ |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 355 | LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN %s, dropping message!\n", sgsn->name); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | /*! Send a message to the next SGSN, possibly ignoring the null SGSN |
| 360 | * route an uplink message on a PTP-BVC to a SGSN using the TLLI |
| 361 | * \param[in] cell The cell the message belongs to |
| 362 | * \param[in] msg The BSSGP message |
| 363 | * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI) |
| 364 | * \param[in] tlli An optional TLLI used for tracking |
| 365 | * \return Returns 0 on success, otherwise a negative value |
| 366 | */ |
| 367 | static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli, |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 368 | bool sig_bvci) |
| 369 | { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 370 | struct gbproxy_config *cfg = cell->cfg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 371 | struct gbproxy_bvc *sgsn_bvc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 372 | |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 373 | sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli); |
| 374 | if (!sgsn_bvc) { |
| 375 | LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli); |
| 376 | return -EINVAL; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 377 | } |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 378 | |
| 379 | return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Daniel Willmann | 2231180 | 2021-11-02 11:54:27 +0100 | [diff] [blame] | 382 | static int gbproxy_decode_bssgp(const struct bssgp_normal_hdr *bgph, int msg_len, struct tlv_parsed *tp, const char *log_pfx) |
| 383 | { |
| 384 | int rc; |
| 385 | |
| 386 | /* UNITDATA PDUs have a different header than the other PDUs */ |
| 387 | if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA || bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 388 | const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) bgph; |
| 389 | if (msg_len < sizeof(*budh)) |
Daniel Willmann | f689a8a | 2021-11-18 15:27:37 +0100 | [diff] [blame] | 390 | return OSMO_TLVP_ERR_MAND_IE_MISSING; |
Daniel Willmann | 2231180 | 2021-11-02 11:54:27 +0100 | [diff] [blame] | 391 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, 1, budh->pdu_type, budh->data, |
| 392 | msg_len - sizeof(*budh), 0, 0, DGPRS, log_pfx); |
| 393 | /* populate TLLI from the fixed headser into the TLV-parsed array so later code |
| 394 | * doesn't have to worry where the TLLI came from */ |
| 395 | tp->lv[BSSGP_IE_TLLI].len = 4; |
| 396 | tp->lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli; |
| 397 | } else { |
| 398 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, 1, bgph->pdu_type, bgph->data, |
| 399 | msg_len - sizeof(*bgph), 0, 0, DGPRS, log_pfx); |
| 400 | } |
| 401 | |
| 402 | return rc; |
| 403 | } |
| 404 | |
Daniel Willmann | 5a14837 | 2021-10-29 18:28:13 +0200 | [diff] [blame] | 405 | static int gbproxy_tlli_from_status_pdu(struct tlv_parsed *tp, uint32_t *tlli, char *log_pfx); |
| 406 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 407 | /* Receive an incoming PTP message from a BSS-side NS-VC */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 408 | static int gbprox_rx_ptp_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 409 | { |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 410 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 411 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 412 | struct gbproxy_bvc *bss_bvc; |
| 413 | struct tlv_parsed tp; |
| 414 | char log_pfx[32]; |
| 415 | uint32_t tlli; |
| 416 | int rc; |
| 417 | |
| 418 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 419 | |
| 420 | LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 421 | |
Daniel Willmann | 06331ac | 2020-12-10 17:59:46 +0100 | [diff] [blame] | 422 | if (ns_bvci == 0 || ns_bvci == 1) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 423 | LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 424 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 428 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 429 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 433 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 434 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 437 | bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci); |
| 438 | if (!bss_bvc) { |
| 439 | LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n", |
| 440 | log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 441 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Daniel Willmann | 2231180 | 2021-11-02 11:54:27 +0100 | [diff] [blame] | 444 | rc = gbproxy_decode_bssgp(bgph, msgb_bssgp_len(msg), &tp, log_pfx); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 445 | if (rc < 0) { |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 446 | rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 447 | return tx_status_from_tlvp(nse, rc, msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 448 | } |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 449 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
| 450 | msgb_bcid(msg) = (void *)&tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 451 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 452 | switch (bgph->pdu_type) { |
| 453 | case BSSGP_PDUT_UL_UNITDATA: |
| 454 | case BSSGP_PDUT_RA_CAPA_UPDATE: |
| 455 | case BSSGP_PDUT_FLOW_CONTROL_MS: |
| 456 | case BSSGP_PDUT_DOWNLOAD_BSS_PFC: |
| 457 | case BSSGP_PDUT_CREATE_BSS_PFC_ACK: |
| 458 | case BSSGP_PDUT_CREATE_BSS_PFC_NACK: |
| 459 | case BSSGP_PDUT_MODIFY_BSS_PFC_ACK: |
| 460 | case BSSGP_PDUT_DELETE_BSS_PFC_ACK: |
| 461 | case BSSGP_PDUT_FLOW_CONTROL_PFC: |
| 462 | case BSSGP_PDUT_DELETE_BSS_PFC_REQ: |
| 463 | case BSSGP_PDUT_PS_HO_REQUIRED: |
| 464 | case BSSGP_PDUT_PS_HO_REQUEST_ACK: |
| 465 | case BSSGP_PDUT_PS_HO_REQUEST_NACK: |
| 466 | case BSSGP_PDUT_PS_HO_COMPLETE: |
| 467 | case BSSGP_PDUT_PS_HO_CANCEL: |
| 468 | /* We can route based on TLLI-NRI */ |
| 469 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI)); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 470 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 471 | break; |
| 472 | case BSSGP_PDUT_RADIO_STATUS: |
| 473 | if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) { |
| 474 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI)); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 475 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 476 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) { |
| 477 | /* we treat the TMSI like a TLLI and extract the NRI from it */ |
| 478 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI)); |
Daniel Willmann | 8b3ed29 | 2021-01-21 18:46:51 +0100 | [diff] [blame] | 479 | /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */ |
| 480 | tlli = gprs_tmsi2tlli(tlli, TLLI_FOREIGN); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 481 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 482 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) { |
Daniel Willmann | 5193f22 | 2021-01-11 05:00:46 +0100 | [diff] [blame] | 483 | /* FIXME: Use the IMSI as selector? */ |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 484 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 485 | } else |
| 486 | LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n"); |
| 487 | break; |
| 488 | case BSSGP_PDUT_DUMMY_PAGING_PS_RESP: |
| 489 | case BSSGP_PDUT_PAGING_PS_REJECT: |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 490 | { |
| 491 | /* Route according to IMSI<->NSE cache entry */ |
| 492 | struct osmo_mobile_identity mi; |
| 493 | const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI); |
| 494 | uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI); |
| 495 | osmo_mobile_identity_decode(&mi, mi_data, mi_len, false); |
Daniel Willmann | 361d0b5 | 2021-07-09 17:44:30 +0200 | [diff] [blame] | 496 | nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi, CACHE_USAGE_PAGING); |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 497 | if (nse) { |
| 498 | OSMO_ASSERT(nse->sgsn_facing); |
| 499 | rc = gbprox_relay2nse(msg, nse, ns_bvci); |
| 500 | } else { |
Daniel Willmann | 8266918 | 2021-01-19 11:37:55 +0100 | [diff] [blame] | 501 | LOGPBVC(bss_bvc, LOGL_ERROR, "Rx unmatched %s with IMSI %s\n", pdut_name, mi.imsi); |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 502 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 503 | break; |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 504 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 505 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 506 | osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 507 | break; |
| 508 | case BSSGP_PDUT_STATUS: |
Daniel Willmann | 5a14837 | 2021-10-29 18:28:13 +0200 | [diff] [blame] | 509 | { |
| 510 | struct gbproxy_sgsn *sgsn; |
| 511 | /* Check if the status needs to be terminated locally */ |
| 512 | uint8_t cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE); |
| 513 | |
| 514 | LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s)\n", cause, |
| 515 | bssgp_cause_str(cause)); |
| 516 | |
| 517 | if (gbproxy_tlli_from_status_pdu(&tp, &tlli, log_pfx) == 0) |
| 518 | sgsn = gbproxy_select_sgsn(nse->cfg, &tlli); |
| 519 | else |
| 520 | sgsn = gbproxy_select_sgsn(nse->cfg, NULL); |
| 521 | |
| 522 | if (!sgsn) { |
| 523 | rc = -EINVAL; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 524 | break; |
Daniel Willmann | 5a14837 | 2021-10-29 18:28:13 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | rc = gbprox_relay2nse(msg, sgsn->nse, ns_bvci); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 528 | break; |
| 529 | } |
Daniel Willmann | 5a14837 | 2021-10-29 18:28:13 +0200 | [diff] [blame] | 530 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 531 | |
Daniel Willmann | d71aa6b | 2022-02-28 18:19:27 +0100 | [diff] [blame] | 532 | if (rc < 0) |
| 533 | rate_ctr_inc(rate_ctr_group_get_ctr(bss_bvc->ctrg, GBPROX_PEER_CTR_FWD_FROM_BSS_ERR)); |
| 534 | |
Oliver Smith | c44b50c | 2023-07-13 13:23:27 +0200 | [diff] [blame^] | 535 | /* avoid a dangling pointer from heap to stack (see above "hack" to set it to &tp) */ |
| 536 | msgb_bcid(msg) = 0; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 537 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* Receive an incoming PTP message from a SGSN-side NS-VC */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 541 | static int gbprox_rx_ptp_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 542 | { |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 543 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 544 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 545 | struct gbproxy_bvc *sgsn_bvc, *bss_bvc; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 546 | struct tlv_parsed tp; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 547 | char log_pfx[32]; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 548 | int rc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 549 | |
| 550 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 551 | |
| 552 | LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 553 | |
Daniel Willmann | 06331ac | 2020-12-10 17:59:46 +0100 | [diff] [blame] | 554 | if (ns_bvci == 0 || ns_bvci == 1) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 555 | LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 556 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 560 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 561 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 565 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 566 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 569 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci); |
| 570 | if (!sgsn_bvc) { |
| 571 | LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n", |
| 572 | log_pfx, pdut_name); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 573 | rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 574 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 577 | if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) { |
| 578 | LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 579 | rate_ctr_inc(rate_ctr_group_get_ctr(sgsn_bvc->ctrg, GBPROX_PEER_CTR_DROPPED)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 580 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 581 | } |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 582 | |
Daniel Willmann | 2231180 | 2021-11-02 11:54:27 +0100 | [diff] [blame] | 583 | rc = gbproxy_decode_bssgp(bgph, msgb_bssgp_len(msg), &tp, log_pfx); |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 584 | if (rc < 0) { |
Daniel Willmann | cfb6f17 | 2021-11-02 11:55:06 +0100 | [diff] [blame] | 585 | rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 586 | return tx_status_from_tlvp(nse, rc, msg); |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 587 | } |
| 588 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
| 589 | msgb_bcid(msg) = (void *)&tp; |
| 590 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 591 | OSMO_ASSERT(sgsn_bvc->cell); |
| 592 | bss_bvc = sgsn_bvc->cell->bss_bvc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 593 | |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 594 | switch (bgph->pdu_type) { |
| 595 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 596 | return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg); |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 597 | case BSSGP_PDUT_DUMMY_PAGING_PS: |
| 598 | case BSSGP_PDUT_PAGING_PS: |
| 599 | { |
| 600 | /* Cache the IMSI<->NSE to route PAGING REJECT */ |
| 601 | struct osmo_mobile_identity mi; |
| 602 | const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI); |
| 603 | uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI); |
| 604 | osmo_mobile_identity_decode(&mi, mi_data, mi_len, false); |
Daniel Willmann | 361d0b5 | 2021-07-09 17:44:30 +0200 | [diff] [blame] | 605 | gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_PAGING); |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 606 | break; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 607 | } |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 608 | default: |
| 609 | break; |
| 610 | } |
Daniel Willmann | d71aa6b | 2022-02-28 18:19:27 +0100 | [diff] [blame] | 611 | rc = gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci); |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 612 | |
Daniel Willmann | d71aa6b | 2022-02-28 18:19:27 +0100 | [diff] [blame] | 613 | if (rc < 0) |
| 614 | rate_ctr_inc(rate_ctr_group_get_ctr(bss_bvc->ctrg, GBPROX_PEER_CTR_FWD_FROM_SGSN_ERR)); |
| 615 | |
| 616 | return rc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 619 | /*********************************************************************** |
| 620 | * BVC FSM call-backs |
| 621 | ***********************************************************************/ |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 622 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 623 | /* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */ |
| 624 | static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv) |
| 625 | { |
| 626 | unsigned int i; |
| 627 | |
| 628 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 629 | struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i]; |
| 630 | if (!sgsn_bvc) |
| 631 | continue; |
| 632 | osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* BVC FSM informs us about a BSS-side reset of the signaling BVC */ |
| 637 | static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 638 | uint16_t cell_id, uint8_t cause, void *priv) |
| 639 | { |
| 640 | struct gbproxy_bvc *sig_bvc = priv; |
| 641 | struct gbproxy_nse *nse = sig_bvc->nse; |
| 642 | struct gbproxy_bvc *ptp_bvc; |
| 643 | unsigned int i; |
| 644 | |
| 645 | /* BLOCK all SGSN-side PTP BVC within this NSE */ |
| 646 | hash_for_each(nse->bvcs, i, ptp_bvc, list) { |
| 647 | if (ptp_bvc == sig_bvc) |
| 648 | continue; |
| 649 | OSMO_ASSERT(ptp_bvc->cell); |
| 650 | |
| 651 | dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause); |
Harald Welte | f9e149b | 2020-12-02 23:29:38 +0100 | [diff] [blame] | 652 | } |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 653 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 654 | /* Delete all BSS-side PTP BVC within this NSE */ |
| 655 | gbproxy_cleanup_bvcs(nse, 0); |
| 656 | |
| 657 | /* TODO: we keep the "CELL" around for now, re-connecting it to |
| 658 | * any (later) new PTP-BVC for that BVCI. Not sure if that's the |
| 659 | * best idea ? */ |
| 660 | } |
| 661 | |
| 662 | /* forward declaration */ |
| 663 | static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops; |
| 664 | |
| 665 | static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = { |
| 666 | .reset_notification = bss_sig_bvc_reset_notif, |
| 667 | }; |
| 668 | |
| 669 | /* BVC FSM informs us about a BSS-side reset of a PTP BVC */ |
| 670 | static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 671 | uint16_t cell_id, uint8_t cause, void *priv) |
| 672 | { |
| 673 | struct gbproxy_bvc *bvc = priv; |
| 674 | struct gbproxy_config *cfg = bvc->nse->cfg; |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 675 | struct gbproxy_nse *sgsn_nse; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 676 | unsigned int i; |
| 677 | |
| 678 | OSMO_ASSERT(bvci != 0); |
| 679 | |
| 680 | if (!bvc->cell) { |
| 681 | /* see if we have a CELL dangling around */ |
| 682 | bvc->cell = gbproxy_cell_by_bvci(cfg, bvci); |
| 683 | if (bvc->cell) { |
| 684 | /* the CELL already exists. This means either it * was created before at an |
| 685 | * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a |
| 686 | * malconfiguration */ |
| 687 | if (bvc->cell->bss_bvc) { |
| 688 | LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already " |
| 689 | "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei); |
| 690 | LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting " |
| 691 | "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei); |
| 692 | gbproxy_bvc_free(bvc->cell->bss_bvc); |
Daniel Willmann | 28ec0e3 | 2021-12-06 16:47:22 +0100 | [diff] [blame] | 693 | bvc->cell = NULL; |
| 694 | } else { |
| 695 | LOGPBVC(bvc, LOGL_ERROR, "Found cell without BSS BVC, this should not happen!"); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 696 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | if (!bvc->cell) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 701 | /* if we end up here, it means this is the first time we received a BVC-RESET |
| 702 | * for this BVC. We need to create the 'cell' data structure and the SGSN-side |
| 703 | * BVC counterparts */ |
| 704 | |
Philipp Maier | e4597ec | 2021-02-09 16:02:00 +0100 | [diff] [blame] | 705 | bvc->cell = gbproxy_cell_alloc(cfg, bvci, ra_id, cell_id); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 706 | OSMO_ASSERT(bvc->cell); |
| 707 | |
| 708 | /* link us to the cell and vice-versa */ |
| 709 | bvc->cell->bss_bvc = bvc; |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 710 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 711 | |
Daniel Willmann | 6701d27 | 2021-04-08 08:39:12 +0200 | [diff] [blame] | 712 | /* Ensure we have the correct RA/CELL ID */ |
| 713 | if (!gsm48_ra_equal(&bvc->cell->id.raid, ra_id)) { |
| 714 | LOGPBVC(bvc, LOGL_NOTICE, "RAID changed from %s to %s, updating cell\n", osmo_rai_name(&bvc->cell->id.raid), osmo_rai_name(ra_id)); |
| 715 | memcpy(&bvc->cell->id.raid, ra_id, sizeof(*ra_id)); |
| 716 | } |
| 717 | if (bvc->cell->id.cid != cell_id) { |
| 718 | LOGPBVC(bvc, LOGL_NOTICE, "CellID changed from %05d to %05d, updating cell\n", bvc->cell->id.cid, cell_id); |
| 719 | bvc->cell->id.cid = cell_id; |
| 720 | } |
| 721 | |
| 722 | /* Reallocate SGSN-side BVCs of the cell, and reset them |
Daniel Willmann | f9902c5 | 2021-11-12 15:52:03 +0100 | [diff] [blame] | 723 | * Removing and reallocating is needed because the ra_id/cell_id might have changed */ |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 724 | hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) { |
| 725 | struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci); |
Daniel Willmann | dc763fd | 2021-09-24 16:45:38 +0200 | [diff] [blame] | 726 | if (!sgsn_bvc) |
| 727 | sgsn_bvc = gbproxy_bvc_by_bvci_inactive(sgsn_nse, bvci); |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 728 | if (sgsn_bvc) |
Daniel Willmann | 6701d27 | 2021-04-08 08:39:12 +0200 | [diff] [blame] | 729 | gbproxy_bvc_free(sgsn_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 730 | |
Daniel Willmann | 6701d27 | 2021-04-08 08:39:12 +0200 | [diff] [blame] | 731 | sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci); |
| 732 | OSMO_ASSERT(sgsn_bvc); |
| 733 | sgsn_bvc->cell = bvc->cell; |
Daniel Willmann | 6701d27 | 2021-04-08 08:39:12 +0200 | [diff] [blame] | 734 | sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei, |
| 735 | bvci, ra_id, cell_id); |
| 736 | OSMO_ASSERT(sgsn_bvc->fi); |
| 737 | bssgp_bvc_fsm_set_max_pdu_len(sgsn_bvc->fi, sgsn_nse->max_sdu_len); |
| 738 | bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc); |
| 739 | gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* Trigger outbound BVC-RESET procedure toward each SGSN */ |
| 743 | dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
| 744 | } |
| 745 | |
| 746 | /* BVC FSM informs us about a BSS-side FSM state change */ |
| 747 | static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv) |
| 748 | { |
| 749 | struct gbproxy_bvc *bvc = priv; |
| 750 | struct gbproxy_cell *cell = bvc->cell; |
| 751 | uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi); |
| 752 | |
| 753 | /* we have just been created but due to callback ordering the cell is not associated */ |
| 754 | if (!cell) |
| 755 | return; |
| 756 | |
| 757 | switch (state) { |
| 758 | case BSSGP_BVCFSM_S_BLOCKED: |
| 759 | /* block the corresponding SGSN-side PTP BVCs */ |
| 760 | dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause); |
| 761 | break; |
| 762 | case BSSGP_BVCFSM_S_UNBLOCKED: |
| 763 | /* unblock the corresponding SGSN-side PTP BVCs */ |
| 764 | dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL); |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 769 | /* BVC FSM informs us about BVC-FC PDU receive */ |
| 770 | static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv) |
| 771 | { |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 772 | struct bssgp2_flow_ctrl fc_reduced; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 773 | struct gbproxy_bvc *bss_bvc = priv; |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 774 | struct gbproxy_cell *cell; |
| 775 | struct gbproxy_config *cfg; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 776 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 777 | OSMO_ASSERT(bss_bvc); |
| 778 | OSMO_ASSERT(fc); |
| 779 | |
| 780 | cell = bss_bvc->cell; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 781 | if (!cell) |
| 782 | return; |
| 783 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 784 | cfg = cell->cfg; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 785 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 786 | /* reduce / scale according to configuration to make sure we only advertise a fraction |
| 787 | * of the capacity to each of the SGSNs in the pool */ |
| 788 | fc_reduced = *fc; |
| 789 | fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100; |
| 790 | fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100; |
| 791 | /* we don't modify the per-MS related values as any single MS is only served by one SGSN */ |
| 792 | |
| 793 | dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced); |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 794 | } |
| 795 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 796 | static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = { |
| 797 | .reset_notification = bss_ptp_bvc_reset_notif, |
| 798 | .state_chg_notification = bss_ptp_bvc_state_chg_notif, |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 799 | .rx_fc_bvc = bss_ptp_bvc_fc_bvc, |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | /* BVC FSM informs us about a SGSN-side reset of a PTP BVC */ |
| 803 | static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 804 | uint16_t cell_id, uint8_t cause, void *priv) |
| 805 | { |
| 806 | struct gbproxy_bvc *bvc = priv; |
| 807 | |
| 808 | if (!bvc->cell) { |
| 809 | LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n"); |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | OSMO_ASSERT(bvc->cell->bss_bvc); |
| 814 | |
| 815 | /* request reset of BSS-facing PTP-BVC */ |
| 816 | osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
| 817 | } |
| 818 | |
| 819 | static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = { |
| 820 | .reset_notification = sgsn_ptp_bvc_reset_notif, |
| 821 | }; |
| 822 | |
| 823 | /* BVC FSM informs us about a SGSN-side reset of the signaling BVC */ |
| 824 | static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 825 | uint16_t cell_id, uint8_t cause, void *priv) |
| 826 | { |
| 827 | struct gbproxy_bvc *bvc = priv; |
| 828 | struct gbproxy_config *cfg = bvc->nse->cfg; |
| 829 | struct gbproxy_nse *bss_nse; |
| 830 | unsigned int i; |
| 831 | |
| 832 | /* delete all SGSN-side PTP BVC for this SGSN */ |
| 833 | gbproxy_cleanup_bvcs(bvc->nse, 0); |
| 834 | /* FIXME: what to do about the cells? */ |
| 835 | /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */ |
| 836 | |
| 837 | /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */ |
| 838 | hash_for_each(cfg->bss_nses, i, bss_nse, list) { |
| 839 | struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0); |
| 840 | if (!bss_bvc) { |
| 841 | LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n"); |
| 842 | continue; |
| 843 | } |
| 844 | osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = { |
| 849 | .reset_notification = sgsn_sig_bvc_reset_notif, |
Daniel Willmann | 2f30a0c | 2022-03-30 11:31:48 +0200 | [diff] [blame] | 850 | .reset_ack_notification = sgsn_sig_bvc_reset_notif, |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 851 | }; |
| 852 | |
| 853 | /*********************************************************************** |
| 854 | * Signaling BVC handling |
| 855 | ***********************************************************************/ |
| 856 | |
| 857 | /* process a BVC-RESET message from the BSS side */ |
| 858 | static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp) |
| 859 | { |
| 860 | struct gbproxy_bvc *from_bvc = NULL; |
| 861 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 862 | uint32_t features = 0; // FIXME: make configurable |
| 863 | |
| 864 | LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci); |
| 865 | |
Harald Welte | 314647b | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 866 | if (bvci == 0) { |
| 867 | /* If we receive a BVC reset on the signalling endpoint, we |
| 868 | * don't want the SGSN to reset, as the signalling endpoint |
| 869 | * is common for all point-to-point BVCs (and thus all BTS) */ |
Harald Welte | 324f065 | 2020-12-02 23:06:37 +0100 | [diff] [blame] | 870 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 871 | from_bvc = gbproxy_bvc_by_bvci(nse, 0); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 872 | if (!from_bvc) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 873 | from_bvc = gbproxy_bvc_alloc(nse, 0); |
| 874 | OSMO_ASSERT(from_bvc); |
| 875 | from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features); |
| 876 | if (!from_bvc->fi) { |
| 877 | LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n"); |
| 878 | gbproxy_bvc_free(from_bvc); |
| 879 | return -ENOMEM; |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 880 | } |
Daniel Willmann | a8b6165 | 2021-02-12 05:05:14 +0100 | [diff] [blame] | 881 | bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 882 | bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc); |
| 883 | } |
| 884 | } else { |
| 885 | from_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 886 | if (!from_bvc) { |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 887 | /* if a PTP-BVC is reset, and we don't know that |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 888 | * PTP-BVCI yet, we should allocate a new bvc */ |
| 889 | from_bvc = gbproxy_bvc_alloc(nse, bvci); |
| 890 | OSMO_ASSERT(from_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 891 | from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi, |
| 892 | nse->nsei, bvci); |
| 893 | if (!from_bvc->fi) { |
| 894 | LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n"); |
| 895 | gbproxy_bvc_free(from_bvc); |
| 896 | return -ENOMEM; |
| 897 | } |
Daniel Willmann | a8b6165 | 2021-02-12 05:05:14 +0100 | [diff] [blame] | 898 | bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 899 | bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc); |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 900 | } |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 901 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 902 | /* hand into FSM for further processing */ |
| 903 | osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg); |
| 904 | return 0; |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 907 | /* Receive an incoming RIM message from a BSS-side NS-VC */ |
| 908 | static int gbprox_rx_rim_from_bss(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx, |
| 909 | const char *pdut_name) |
| 910 | { |
| 911 | struct gbproxy_sgsn *sgsn; |
| 912 | struct gbproxy_cell *dest_cell; |
| 913 | struct gbproxy_cell *src_cell; |
| 914 | struct bssgp_rim_routing_info dest_ri; |
| 915 | struct bssgp_rim_routing_info src_ri; |
| 916 | int rc; |
Philipp Maier | 4499cf4 | 2021-02-10 17:54:44 +0100 | [diff] [blame] | 917 | char ri_src_str[64]; |
| 918 | char ri_dest_str[64]; |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 919 | uint16_t ns_bvci = msgb_bvci(msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 920 | |
| 921 | rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO), |
| 922 | TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO)); |
| 923 | if (rc < 0) { |
| 924 | LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 925 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 926 | } |
| 927 | rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO), |
| 928 | TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO)); |
| 929 | if (rc < 0) { |
| 930 | LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 931 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* Since gbproxy is 2G only we do not expect to get RIM messages only from GERAN cells. */ |
| 935 | if (src_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) { |
| 936 | LOGP(DGPRS, LOGL_ERROR, "%s %s source RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name, |
| 937 | bssgp_rim_ri_name(&src_ri)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 938 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* Lookup source cell to make sure that the source RIM routing information actually belongs |
| 942 | * to a valid cell that we know */ |
| 943 | src_cell = gbproxy_cell_by_cellid(nse->cfg, &src_ri.geran.raid, src_ri.geran.cid); |
| 944 | if (!src_cell) { |
| 945 | LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for source RIM routing info (%s)\n", log_pfx, |
| 946 | pdut_name, bssgp_rim_ri_name(&src_ri)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 947 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* TODO: Use bssgp_bvc_get_features_negotiated(src_cell->bss_bvc->fi) to check if the the BSS sided BVC actually |
| 951 | * did negotiate RIM support. If not we should respond with a BSSGP STATUS message. The cause code should be |
| 952 | * BSSGP_CAUSE_PDU_INCOMP_FEAT. */ |
| 953 | |
| 954 | /* If Destination is known by gbproxy, route directly */ |
| 955 | if (dest_ri.discr == BSSGP_RIM_ROUTING_INFO_GERAN) { |
| 956 | dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid); |
| 957 | if (dest_cell) { |
| 958 | /* TODO: Also check if dest_cell->bss_bvc is RIM-capable (see also above). If not we should |
| 959 | * respond with a BSSGP STATUS message as well because it also would make no sense to try |
| 960 | * routing the RIM message to the next RIM-capable SGSN. */ |
Philipp Maier | 4499cf4 | 2021-02-10 17:54:44 +0100 | [diff] [blame] | 961 | LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to peer (nsei=%u) RIM-PDU: src=%s, dest=%s\n", |
| 962 | log_pfx, pdut_name, dest_cell->bss_bvc->nse->nsei, |
| 963 | bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri), |
| 964 | bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri)); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 965 | return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /* Otherwise pass on to a RIM-capable SGSN */ |
| 970 | /* TODO: We need to extend gbproxy_select_sgsn() so that it selects a RIM-capable SGSN, at the moment we just |
| 971 | * get any SGSN and just assume that it is RIM-capable. */ |
| 972 | sgsn = gbproxy_select_sgsn(nse->cfg, NULL); |
| 973 | if (!sgsn) { |
| 974 | LOGP(DGPRS, LOGL_NOTICE, |
| 975 | "%s %s cannot route RIM message (%s to %s) since no RIM capable SGSN is found!\n", log_pfx, |
| 976 | pdut_name, bssgp_rim_ri_name(&src_ri), bssgp_rim_ri_name(&dest_ri)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 977 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 978 | } |
Philipp Maier | 4499cf4 | 2021-02-10 17:54:44 +0100 | [diff] [blame] | 979 | LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n", |
| 980 | log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name, |
| 981 | bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri), |
| 982 | bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri)); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 983 | |
| 984 | return gbprox_relay2nse(msg, sgsn->nse, 0); |
| 985 | } |
| 986 | |
Daniel Willmann | 6ec5f95 | 2021-10-28 16:13:03 +0200 | [diff] [blame] | 987 | /* Extract the TLLI from the PDU-in-error of the STATUS PDU (if available) */ |
| 988 | static int gbproxy_tlli_from_status_pdu(struct tlv_parsed *tp, uint32_t *tlli, char *log_pfx) |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 989 | { |
| 990 | int rc; |
| 991 | int pdu_len = TLVP_LEN(&tp[0], BSSGP_IE_PDU_IN_ERROR); |
| 992 | const uint8_t *pdu_data = TLVP_VAL(&tp[0], BSSGP_IE_PDU_IN_ERROR); |
| 993 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *)pdu_data; |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 994 | struct tlv_parsed tp_inner; |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 995 | |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 996 | rc = gbproxy_decode_bssgp(bgph, pdu_len, &tp_inner, log_pfx); |
Daniel Willmann | af2e9b3 | 2021-11-18 15:27:49 +0100 | [diff] [blame] | 997 | /* Ignore decode failure due to truncated message */ |
| 998 | if (rc < 0 && rc != OSMO_TLVP_ERR_OFS_BEYOND_BUFFER) |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 999 | return rc; |
| 1000 | |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 1001 | if (TLVP_PRESENT(&tp_inner, BSSGP_IE_TLLI)) { |
| 1002 | *tlli = osmo_load32be(TLVP_VAL(&tp_inner, BSSGP_IE_TLLI)); |
| 1003 | } else if (TLVP_PRESENT(&tp_inner, BSSGP_IE_TMSI)) { |
Daniel Willmann | cbfc7cf | 2021-09-29 11:51:56 +0200 | [diff] [blame] | 1004 | /* we treat the TMSI like a TLLI and extract the NRI from it */ |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 1005 | *tlli = osmo_load32be(TLVP_VAL(&tp_inner, BSSGP_IE_TMSI)); |
Daniel Willmann | cbfc7cf | 2021-09-29 11:51:56 +0200 | [diff] [blame] | 1006 | /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */ |
| 1007 | *tlli = gprs_tmsi2tlli(*tlli, TLLI_FOREIGN); |
| 1008 | } else { |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1009 | return -ENOENT; |
Daniel Willmann | cbfc7cf | 2021-09-29 11:51:56 +0200 | [diff] [blame] | 1010 | } |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Daniel Willmann | 3d8f599 | 2021-10-27 16:05:37 +0200 | [diff] [blame] | 1015 | /* Extract the BVCI from the PDU-in-error of the STATUS PDU (if available) */ |
| 1016 | static int gbproxy_bvci_from_status_pdu(struct tlv_parsed *tp, uint16_t *bvci, char *log_pfx) |
| 1017 | { |
| 1018 | int rc; |
| 1019 | int pdu_len = TLVP_LEN(&tp[0], BSSGP_IE_PDU_IN_ERROR); |
| 1020 | const uint8_t *pdu_data = TLVP_VAL(&tp[0], BSSGP_IE_PDU_IN_ERROR); |
| 1021 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *)pdu_data; |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 1022 | struct tlv_parsed tp_inner; |
Daniel Willmann | 3d8f599 | 2021-10-27 16:05:37 +0200 | [diff] [blame] | 1023 | |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 1024 | rc = gbproxy_decode_bssgp(bgph, pdu_len, &tp_inner, log_pfx); |
Daniel Willmann | af2e9b3 | 2021-11-18 15:27:49 +0100 | [diff] [blame] | 1025 | /* Ignore decode failure due to truncated message */ |
| 1026 | if (rc < 0 && rc != OSMO_TLVP_ERR_OFS_BEYOND_BUFFER) |
Daniel Willmann | 3d8f599 | 2021-10-27 16:05:37 +0200 | [diff] [blame] | 1027 | return rc; |
| 1028 | |
Daniel Willmann | 0141628 | 2021-11-02 11:55:57 +0100 | [diff] [blame] | 1029 | if (TLVP_PRESENT(&tp_inner, BSSGP_IE_BVCI)) |
| 1030 | *bvci = ntohs(tlvp_val16_unal(&tp_inner, BSSGP_IE_BVCI)); |
Daniel Willmann | 3d8f599 | 2021-10-27 16:05:37 +0200 | [diff] [blame] | 1031 | else |
| 1032 | return -ENOENT; |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1037 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1038 | static int gbprox_rx_sig_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1039 | { |
| 1040 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1041 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1042 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1043 | struct tlv_parsed tp[2]; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1044 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1045 | struct gbproxy_bvc *from_bvc = NULL; |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1046 | char log_pfx[32]; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1047 | uint16_t ptp_bvci; |
| 1048 | uint32_t tlli; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1049 | int rc; |
| 1050 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1051 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 1052 | |
| 1053 | LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1054 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1055 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1056 | LOGP(DGPRS, LOGL_NOTICE, "%s %s BVCI=%05u is not signalling\n", log_pfx, pdut_name, ns_bvci); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1057 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1060 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1061 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1062 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1066 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1067 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1068 | } |
| 1069 | |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1070 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, ARRAY_SIZE(tp), pdu_type, bgph->data, data_len, 0, 0, |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1071 | DGPRS, log_pfx); |
| 1072 | if (rc < 0) { |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1073 | rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1074 | return tx_status_from_tlvp(nse, rc, msg); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1075 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1076 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1077 | msgb_bcid(msg) = (void *)tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1078 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1079 | /* special case handling for some PDU types */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1080 | switch (pdu_type) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1081 | case BSSGP_PDUT_BVC_RESET: |
| 1082 | /* resolve or create gbproxy_bvc + handlei n BVC-FSM */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1083 | return rx_bvc_reset_from_bss(nse, msg, &tp[0]); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1084 | case BSSGP_PDUT_BVC_RESET_ACK: |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1085 | ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1086 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1087 | if (!from_bvc) |
| 1088 | goto err_no_bvc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1089 | return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg); |
| 1090 | case BSSGP_PDUT_BVC_BLOCK: |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1091 | ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1092 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 1093 | if (!from_bvc) |
| 1094 | goto err_no_bvc; |
| 1095 | return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg); |
| 1096 | case BSSGP_PDUT_BVC_UNBLOCK: |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1097 | ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1098 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 1099 | if (!from_bvc) |
| 1100 | goto err_no_bvc; |
| 1101 | return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg); |
| 1102 | case BSSGP_PDUT_SUSPEND: |
| 1103 | case BSSGP_PDUT_RESUME: |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1104 | { |
| 1105 | struct gbproxy_sgsn *sgsn; |
| 1106 | |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1107 | tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI)); |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1108 | sgsn = gbproxy_select_sgsn(nse->cfg, &tlli); |
| 1109 | if (!sgsn) { |
| 1110 | LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN for TLLI, dropping message!\n"); |
| 1111 | rc = -EINVAL; |
| 1112 | break; |
| 1113 | } |
| 1114 | |
| 1115 | gbproxy_tlli_cache_update(nse, tlli); |
| 1116 | |
| 1117 | rc = gbprox_relay2nse(msg, sgsn->nse, 0); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1118 | #if 0 |
| 1119 | /* TODO: Validate the RAI for consistency with the RAI |
| 1120 | * we expect for any of the BVC within this BSS side NSE */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1121 | memcpy(ra, TLVP_VAL(&tp[0], BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra)); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1122 | gsm48_parse_ra(&raid, from_bvc->ra); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1123 | #endif |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1124 | break; |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1125 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1126 | case BSSGP_PDUT_STATUS: |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1127 | { |
| 1128 | struct gbproxy_sgsn *sgsn; |
| 1129 | /* Check if the status needs to be terminated locally */ |
| 1130 | uint8_t cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE); |
Daniel Willmann | c5dcebd | 2021-09-30 16:57:51 +0200 | [diff] [blame] | 1131 | |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1132 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) { |
| 1133 | /* Log and handle locally */ |
| 1134 | ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Daniel Willmann | c5dcebd | 2021-09-30 16:57:51 +0200 | [diff] [blame] | 1135 | LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s) for PtP-BVC %05u\n", cause, |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1136 | bssgp_cause_str(cause), ptp_bvci); |
| 1137 | /* FIXME: Remove/block our BVC if present? */ |
| 1138 | break; |
| 1139 | } |
| 1140 | |
Daniel Willmann | c5dcebd | 2021-09-30 16:57:51 +0200 | [diff] [blame] | 1141 | LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause, |
| 1142 | bssgp_cause_str(cause)); |
| 1143 | |
Daniel Willmann | 6ec5f95 | 2021-10-28 16:13:03 +0200 | [diff] [blame] | 1144 | if (gbproxy_tlli_from_status_pdu(tp, &tlli, log_pfx) == 0) |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1145 | sgsn = gbproxy_select_sgsn(nse->cfg, &tlli); |
| 1146 | else |
| 1147 | sgsn = gbproxy_select_sgsn(nse->cfg, NULL); |
| 1148 | |
| 1149 | if (!sgsn) { |
| 1150 | rc = -EINVAL; |
| 1151 | break; |
| 1152 | } |
| 1153 | |
| 1154 | rc = gbprox_relay2nse(msg, sgsn->nse, 0); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1155 | break; |
Daniel Willmann | 7d37cbb | 2021-09-29 11:51:51 +0200 | [diff] [blame] | 1156 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1157 | case BSSGP_PDUT_RAN_INFO: |
| 1158 | case BSSGP_PDUT_RAN_INFO_REQ: |
| 1159 | case BSSGP_PDUT_RAN_INFO_ACK: |
| 1160 | case BSSGP_PDUT_RAN_INFO_ERROR: |
| 1161 | case BSSGP_PDUT_RAN_INFO_APP_ERROR: |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1162 | rc = gbprox_rx_rim_from_bss(tp, nse, msg, log_pfx, pdut_name); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1163 | break; |
| 1164 | case BSSGP_PDUT_LLC_DISCARD: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1165 | /* route based on BVCI + TLLI */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1166 | ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
| 1167 | tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1168 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 1169 | if (!from_bvc) |
| 1170 | goto err_no_bvc; |
Daniel Willmann | e705b3f | 2021-11-25 22:04:56 +0100 | [diff] [blame] | 1171 | rc = gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1172 | break; |
Daniel Willmann | e705b3f | 2021-11-25 22:04:56 +0100 | [diff] [blame] | 1173 | case BSSGP_PDUT_FLUSH_LL_ACK: |
| 1174 | { |
| 1175 | /* Route based on TLLI */ |
| 1176 | tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI)); |
| 1177 | struct gbproxy_sgsn *sgsn = gbproxy_select_sgsn(nse->cfg, &tlli); |
| 1178 | if (!sgsn) { |
| 1179 | rc = -EINVAL; |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | rc = gbprox_relay2nse(msg, sgsn->nse, 0); |
| 1184 | break; |
| 1185 | } |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1186 | case BSSGP_PDUT_PAGING_PS_REJECT: |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 1187 | case BSSGP_PDUT_DUMMY_PAGING_PS_RESP: |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1188 | { |
| 1189 | /* Route according to IMSI<->NSE cache entry */ |
| 1190 | struct osmo_mobile_identity mi; |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1191 | const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI); |
| 1192 | uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI); |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1193 | osmo_mobile_identity_decode(&mi, mi_data, mi_len, false); |
Daniel Willmann | 361d0b5 | 2021-07-09 17:44:30 +0200 | [diff] [blame] | 1194 | nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi, CACHE_USAGE_PAGING); |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1195 | if (!nse) { |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1196 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1197 | } |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 1198 | OSMO_ASSERT(nse->sgsn_facing); |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1199 | rc = gbprox_relay2nse(msg, nse, 0); |
| 1200 | break; |
| 1201 | } |
Daniel Willmann | f024eeb | 2021-07-06 14:02:41 +0200 | [diff] [blame] | 1202 | case BSSGP_PDUT_MS_REGISTR_ENQ: |
| 1203 | { |
| 1204 | struct gbproxy_sgsn *sgsn; |
| 1205 | struct osmo_mobile_identity mi; |
| 1206 | const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI); |
| 1207 | uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI); |
| 1208 | osmo_mobile_identity_decode(&mi, mi_data, mi_len, false); |
| 1209 | |
| 1210 | sgsn = gbproxy_select_sgsn(nse->cfg, NULL); |
| 1211 | if (!sgsn) { |
| 1212 | LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN, dropping message!\n"); |
| 1213 | rc = -EINVAL; |
| 1214 | break; |
| 1215 | } |
| 1216 | |
| 1217 | gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_MS_REG_ENQ); |
| 1218 | |
| 1219 | rc = gbprox_relay2nse(msg, sgsn->nse, 0); |
| 1220 | break; |
| 1221 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1222 | default: |
| 1223 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1224 | break; |
| 1225 | } |
| 1226 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1227 | return rc; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1228 | err_no_bvc: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1229 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1230 | rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_NSEI)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1231 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1235 | static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name, |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 1236 | struct tlv_parsed *tp, uint16_t ns_bvci, bool broadcast) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1237 | { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1238 | struct gbproxy_config *cfg = sgsn_nse->cfg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1239 | struct gbproxy_bvc *sgsn_bvc, *bss_bvc; |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1240 | struct gbproxy_nse *nse; |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1241 | unsigned int n_nses = 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1242 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 1243 | int i, j; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1244 | |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 1245 | if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1246 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1247 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1248 | sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1249 | if (!sgsn_bvc) { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1250 | LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n", |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1251 | pdut_name, bvci); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1252 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr)); |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 1253 | return -EINVAL; |
| 1254 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1255 | LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name); |
| 1256 | return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci); |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 1257 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) { |
Philipp Maier | da3af94 | 2021-02-04 21:54:09 +0100 | [diff] [blame] | 1258 | struct gprs_ra_id raid; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1259 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Philipp Maier | da3af94 | 2021-02-04 21:54:09 +0100 | [diff] [blame] | 1260 | gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA)); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1261 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1262 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1263 | hash_for_each(nse->bvcs, j, bss_bvc, list) { |
Daniel Willmann | f9902c5 | 2021-11-12 15:52:03 +0100 | [diff] [blame] | 1264 | /* Skip BVCs without a cell (e.g. signalling) */ |
| 1265 | if (!bss_bvc->cell) |
| 1266 | continue; |
| 1267 | |
| 1268 | if (gsm48_ra_equal(&bss_bvc->cell->id.raid, &raid)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1269 | LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n", |
| 1270 | pdut_name); |
| 1271 | gbprox_relay2peer(msg, bss_bvc, ns_bvci); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1272 | n_nses++; |
| 1273 | /* Only send it once to each NSE */ |
| 1274 | break; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1275 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 1276 | } |
| 1277 | } |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 1278 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) { |
Philipp Maier | da3af94 | 2021-02-04 21:54:09 +0100 | [diff] [blame] | 1279 | struct gsm48_ra_id lac; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1280 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1281 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1282 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1283 | hash_for_each(nse->bvcs, j, bss_bvc, list) { |
Daniel Willmann | f9902c5 | 2021-11-12 15:52:03 +0100 | [diff] [blame] | 1284 | /* Skip BVCs without a cell (e.g. signalling) */ |
| 1285 | if (!bss_bvc->cell) |
| 1286 | continue; |
| 1287 | |
| 1288 | gsm48_encode_ra(&lac, &bss_bvc->cell->id.raid); |
Philipp Maier | da3af94 | 2021-02-04 21:54:09 +0100 | [diff] [blame] | 1289 | if (!memcmp(&lac, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1290 | LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n", |
| 1291 | pdut_name); |
| 1292 | gbprox_relay2peer(msg, bss_bvc, ns_bvci); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1293 | n_nses++; |
| 1294 | /* Only send it once to each NSE */ |
| 1295 | break; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1296 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 1297 | } |
| 1298 | } |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 1299 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1) || broadcast) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1300 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1301 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1302 | hash_for_each(nse->bvcs, j, bss_bvc, list) { |
| 1303 | LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name); |
| 1304 | gbprox_relay2peer(msg, bss_bvc, ns_bvci); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1305 | n_nses++; |
| 1306 | /* Only send it once to each NSE */ |
| 1307 | break; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1308 | } |
Harald Welte | 53ee206 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 1309 | } |
| 1310 | } else { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1311 | LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n"); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1312 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr)); |
Harald Welte | 53ee206 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 1313 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1314 | |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1315 | if (n_nses == 0) { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1316 | LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n"); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1317 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1318 | return -EINVAL; |
| 1319 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 1320 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /* Receive an incoming BVC-RESET message from the SGSN */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1324 | static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp, |
| 1325 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1326 | { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1327 | uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 1328 | struct gbproxy_bvc *from_bvc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1329 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1330 | LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1331 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1332 | if (ptp_bvci == 0) { |
| 1333 | from_bvc = gbproxy_bvc_by_bvci(nse, 0); |
| 1334 | OSMO_ASSERT(from_bvc); |
| 1335 | osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg); |
| 1336 | } else { |
| 1337 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 1338 | if (!from_bvc) { |
| 1339 | LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1340 | rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1341 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1342 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1343 | osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1344 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1349 | /* Receive an incoming RIM message from the SGSN-side NS-VC */ |
| 1350 | static int gbprox_rx_rim_from_sgsn(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx, |
| 1351 | const char *pdut_name) |
| 1352 | { |
| 1353 | struct gbproxy_sgsn *sgsn; |
| 1354 | struct gbproxy_cell *dest_cell; |
| 1355 | struct bssgp_rim_routing_info dest_ri; |
| 1356 | struct bssgp_rim_routing_info src_ri; |
| 1357 | int rc; |
Philipp Maier | 4499cf4 | 2021-02-10 17:54:44 +0100 | [diff] [blame] | 1358 | char ri_src_str[64]; |
| 1359 | char ri_dest_str[64]; |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1360 | uint16_t ns_bvci = msgb_bvci(msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1361 | |
| 1362 | /* TODO: Reply with STATUS if BSSGP didn't negotiate RIM feature, see also comments in |
| 1363 | gbprox_rx_rim_from_bss() */ |
| 1364 | |
| 1365 | rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO), |
| 1366 | TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO)); |
| 1367 | if (rc < 0) { |
| 1368 | LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1369 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1370 | } |
| 1371 | rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO), |
| 1372 | TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO)); |
| 1373 | if (rc < 0) { |
| 1374 | LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1375 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* Since gbproxy is 2G only we do not expect to get RIM messages that target non-GERAN cells. */ |
| 1379 | if (dest_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) { |
| 1380 | LOGP(DGPRS, LOGL_ERROR, "%s %s destination RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name, |
| 1381 | bssgp_rim_ri_name(&dest_ri)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1382 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /* Lookup destination cell */ |
| 1386 | dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid); |
| 1387 | if (!dest_cell) { |
| 1388 | LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for destination RIM routing info (%s)\n", log_pfx, |
| 1389 | pdut_name, bssgp_rim_ri_name(&dest_ri)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1390 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /* TODO: Check if the BVC of the destination cell actually did negotiate RIM support, see also comments |
| 1394 | * in gbprox_rx_rim_from_bss() */ |
| 1395 | sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei); |
| 1396 | OSMO_ASSERT(sgsn); |
| 1397 | |
Philipp Maier | 4499cf4 | 2021-02-10 17:54:44 +0100 | [diff] [blame] | 1398 | LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying from SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n", |
| 1399 | log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name, |
| 1400 | bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri), |
| 1401 | bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri)); |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1402 | |
| 1403 | return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0); |
| 1404 | } |
| 1405 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1406 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1407 | static int gbprox_rx_sig_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1408 | { |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1409 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1410 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1411 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 1412 | struct gbproxy_config *cfg = nse->cfg; |
| 1413 | struct gbproxy_bvc *sgsn_bvc; |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1414 | struct tlv_parsed tp[2]; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1415 | int data_len; |
Alexander Couzens | 96a4427 | 2022-09-28 16:28:46 +0200 | [diff] [blame] | 1416 | uint16_t bvci, bvci2; |
| 1417 | uint32_t tlli; |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1418 | char log_pfx[32]; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1419 | int rc = 0; |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1420 | int i; |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 1421 | bool paging_bc = false; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1422 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1423 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 1424 | |
| 1425 | LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1426 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1427 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1428 | LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1429 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1432 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1433 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1434 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1438 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1439 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1442 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1443 | |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1444 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, ARRAY_SIZE(tp), pdu_type, bgph->data, data_len, 0, 0, |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1445 | DGPRS, log_pfx); |
| 1446 | if (rc < 0) { |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1447 | rc = tx_status_from_tlvp(nse, rc, msg); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1448 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN)); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1449 | return rc; |
| 1450 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1451 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1452 | msgb_bcid(msg) = (void *)tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1453 | |
| 1454 | switch (pdu_type) { |
| 1455 | case BSSGP_PDUT_BVC_RESET: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1456 | /* resolve or create ggbproxy_bvc + handle in BVC-FSM */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1457 | bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
| 1458 | rc = rx_bvc_reset_from_sgsn(nse, msg, &tp[0], ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1459 | break; |
| 1460 | case BSSGP_PDUT_BVC_RESET_ACK: |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1461 | bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1462 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1463 | if (!sgsn_bvc) |
| 1464 | goto err_no_bvc; |
| 1465 | rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg); |
| 1466 | break; |
| 1467 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1468 | bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1469 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
Daniel Willmann | dc763fd | 2021-09-24 16:45:38 +0200 | [diff] [blame] | 1470 | if (!sgsn_bvc) { |
| 1471 | /* Check if BVC was blocked before */ |
| 1472 | sgsn_bvc = gbproxy_bvc_by_bvci_inactive(nse, bvci); |
| 1473 | if (!sgsn_bvc) |
| 1474 | goto err_no_bvc; |
| 1475 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1476 | rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg); |
| 1477 | break; |
| 1478 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1479 | bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1480 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1481 | if (!sgsn_bvc) |
| 1482 | goto err_no_bvc; |
| 1483 | rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg); |
Daniel Willmann | 8489e7a | 2020-11-03 21:12:42 +0100 | [diff] [blame] | 1484 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1485 | case BSSGP_PDUT_FLUSH_LL: |
Alexander Couzens | 96a4427 | 2022-09-28 16:28:46 +0200 | [diff] [blame] | 1486 | /* TODO: If we negotiate Inter-NSE re-routing or LCS-procedures we can also |
Daniel Willmann | e705b3f | 2021-11-25 22:04:56 +0100 | [diff] [blame] | 1487 | * add the NSEI TLV to trigger re-routing the PDUs */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1488 | /* simple case: BVCI IE is mandatory */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1489 | bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1490 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1491 | if (!sgsn_bvc) |
| 1492 | goto err_no_bvc; |
Alexander Couzens | 96a4427 | 2022-09-28 16:28:46 +0200 | [diff] [blame] | 1493 | |
| 1494 | /* bvc is not valid */ |
| 1495 | if (!sgsn_bvc->cell || !sgsn_bvc->cell->bss_bvc) { |
| 1496 | rc = -EINVAL; |
| 1497 | break; |
| 1498 | } |
| 1499 | |
| 1500 | /* When both the old and new BVCI is present: |
| 1501 | * If old & new BVCI is on the same NSEI, forward message as is, |
| 1502 | * otherwise remove new BVCI. */ |
| 1503 | if (TLVP_PRESENT(&tp[1], BSSGP_IE_BVCI)) { |
| 1504 | struct gbproxy_bvc *bvc2; |
| 1505 | struct msgb *flush; |
| 1506 | |
| 1507 | bvci2 = ntohs(tlvp_val16_unal(&tp[1], BSSGP_IE_BVCI)); |
| 1508 | bvc2 = gbproxy_bvc_by_bvci(nse, bvci2); |
| 1509 | |
| 1510 | /* it's the same NSEI, we can pass the message as is */ |
| 1511 | if (bvc2->nse == sgsn_bvc->nse) { |
| 1512 | rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci); |
| 1513 | break; |
| 1514 | } |
| 1515 | |
| 1516 | tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI)); |
| 1517 | /* build a new message in order to drop the old one */ |
| 1518 | flush = bssgp2_enc_flush_ll(tlli, bvci, NULL, NULL); |
| 1519 | if (!flush) { |
| 1520 | /* TODO: error message */ |
| 1521 | rc = -ENOMEM; |
| 1522 | break; |
| 1523 | } |
| 1524 | |
| 1525 | DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg), |
| 1526 | !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS"); |
| 1527 | rc = bssgp2_nsi_tx_ptp(nse->cfg->nsi, nse->nsei, ns_bvci, flush, tlli); |
| 1528 | } else { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1529 | rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci); |
Alexander Couzens | 96a4427 | 2022-09-28 16:28:46 +0200 | [diff] [blame] | 1530 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1531 | break; |
Daniel Willmann | 5614e57 | 2021-01-18 18:38:27 +0100 | [diff] [blame] | 1532 | case BSSGP_PDUT_DUMMY_PAGING_PS: |
| 1533 | /* Routing area is optional in dummy paging and we have nothing else to go by |
| 1534 | * so in case it is missing we need to broadcast the paging */ |
| 1535 | paging_bc = true; |
| 1536 | /* fall through */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1537 | case BSSGP_PDUT_PAGING_PS: |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1538 | { |
| 1539 | /* Cache the IMSI<->NSE to route PAGING REJECT */ |
| 1540 | struct osmo_mobile_identity mi; |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1541 | const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI); |
| 1542 | uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI); |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1543 | osmo_mobile_identity_decode(&mi, mi_data, mi_len, false); |
Daniel Willmann | 361d0b5 | 2021-07-09 17:44:30 +0200 | [diff] [blame] | 1544 | gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_PAGING); |
Daniel Willmann | 8613c9d | 2021-01-17 13:40:38 +0100 | [diff] [blame] | 1545 | /* fall through */ |
| 1546 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1547 | case BSSGP_PDUT_PAGING_CS: |
| 1548 | /* process the paging request (LAI/RAI lookup) */ |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1549 | rc = gbprox_rx_paging(nse, msg, pdut_name, &tp[0], ns_bvci, paging_bc); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1550 | break; |
| 1551 | case BSSGP_PDUT_STATUS: |
Daniel Willmann | 885f430 | 2021-09-30 16:58:46 +0200 | [diff] [blame] | 1552 | { |
| 1553 | struct gbproxy_nse *nse_peer; |
| 1554 | uint32_t tlli; |
| 1555 | |
| 1556 | /* Check if the status needs to be terminated locally */ |
| 1557 | uint8_t cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE); |
| 1558 | |
| 1559 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) { |
| 1560 | /* Log and handle locally, BVCI should be present for these causes */ |
| 1561 | if (!TLVP_PRESENT(&tp[0], BSSGP_IE_BVCI)) { |
| 1562 | LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s), but BVCI is missing\n", cause, |
| 1563 | bssgp_cause_str(cause)); |
| 1564 | break; |
| 1565 | } |
| 1566 | uint16_t ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI)); |
| 1567 | LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s) for PtP-BVC %05u\n", cause, |
| 1568 | bssgp_cause_str(cause), ptp_bvci); |
| 1569 | /* FIXME: Remove/block the other BSS/SGSN BVCs if present? */ |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s)\n", cause, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1574 | bssgp_cause_str(cause)); |
Daniel Willmann | 885f430 | 2021-09-30 16:58:46 +0200 | [diff] [blame] | 1575 | |
Daniel Willmann | 3d8f599 | 2021-10-27 16:05:37 +0200 | [diff] [blame] | 1576 | |
| 1577 | if (gbproxy_bvci_from_status_pdu(tp, &bvci, log_pfx) == 0 && bvci != 0) { |
| 1578 | struct gbproxy_cell *cell = gbproxy_cell_by_bvci(cfg, bvci); |
| 1579 | |
| 1580 | if ((!cell || !cell->bss_bvc || !cell->bss_bvc->nse)) { |
| 1581 | LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s), but can't find NSE for cell\n", |
| 1582 | cause, bssgp_cause_str(cause)); |
| 1583 | break; |
| 1584 | } |
| 1585 | |
| 1586 | return gbprox_relay2nse(msg, cell->bss_bvc->nse, 0); |
| 1587 | } |
| 1588 | |
Daniel Willmann | 6ec5f95 | 2021-10-28 16:13:03 +0200 | [diff] [blame] | 1589 | /* We can only forward this TLLI if it's in the cache (which only happens on suspend/resume) */ |
| 1590 | if (gbproxy_tlli_from_status_pdu(tp, &tlli, log_pfx) == 0) { |
Daniel Willmann | 885f430 | 2021-09-30 16:58:46 +0200 | [diff] [blame] | 1591 | nse_peer = gbproxy_nse_by_tlli(cfg, tlli); |
| 1592 | if (nse_peer) |
| 1593 | return gbprox_relay2nse(msg, nse_peer, 0); |
| 1594 | } |
| 1595 | |
| 1596 | LOGPNSE(nse, LOGL_ERROR, "Unable to handle STATUS cause=0x%02x(%s)\n", cause, |
| 1597 | bssgp_cause_str(cause)); |
| 1598 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1599 | break; |
Daniel Willmann | 885f430 | 2021-09-30 16:58:46 +0200 | [diff] [blame] | 1600 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1601 | /* those only exist in the SGSN -> BSS direction */ |
| 1602 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1603 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1604 | case BSSGP_PDUT_RESUME_ACK: |
| 1605 | case BSSGP_PDUT_RESUME_NACK: |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1606 | { |
| 1607 | struct gbproxy_nse *nse_peer; |
Philipp Maier | 74882dc | 2021-02-04 16:31:46 +0100 | [diff] [blame] | 1608 | uint32_t tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI)); |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1609 | |
| 1610 | nse_peer = gbproxy_nse_by_tlli(cfg, tlli); |
| 1611 | if (!nse_peer) { |
| 1612 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name); |
| 1613 | /* TODO: Counter */ |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1614 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1615 | } |
| 1616 | /* Delete the entry after we're done */ |
| 1617 | gbproxy_tlli_cache_remove(cfg, tlli); |
| 1618 | LOGPNSE(nse_peer, LOGL_DEBUG, "Rx %s: forwarding\n", pdut_name); |
| 1619 | gbprox_relay2nse(msg, nse_peer, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1620 | break; |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1621 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1622 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 7479c4d | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 1623 | case BSSGP_PDUT_OVERLOAD: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1624 | LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1625 | /* broadcast to all BSS-side bvcs */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1626 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | 7479c4d | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 1627 | gbprox_relay2nse(msg, nse, 0); |
| 1628 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1629 | break; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1630 | case BSSGP_PDUT_RAN_INFO: |
| 1631 | case BSSGP_PDUT_RAN_INFO_REQ: |
| 1632 | case BSSGP_PDUT_RAN_INFO_ACK: |
| 1633 | case BSSGP_PDUT_RAN_INFO_ERROR: |
| 1634 | case BSSGP_PDUT_RAN_INFO_APP_ERROR: |
Philipp Maier | 1c5766b | 2021-02-09 17:03:03 +0100 | [diff] [blame] | 1635 | rc = gbprox_rx_rim_from_sgsn(tp, nse, msg, log_pfx, pdut_name); |
Pau Espin Pedrol | a429634 | 2021-05-07 13:33:34 +0200 | [diff] [blame] | 1636 | break; |
Daniel Willmann | f024eeb | 2021-07-06 14:02:41 +0200 | [diff] [blame] | 1637 | case BSSGP_PDUT_MS_REGISTR_ENQ_RESP: |
| 1638 | { |
| 1639 | struct gbproxy_nse *nse_peer; |
| 1640 | struct osmo_mobile_identity mi; |
| 1641 | const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI); |
| 1642 | uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI); |
| 1643 | osmo_mobile_identity_decode(&mi, mi_data, mi_len, false); |
| 1644 | nse_peer = gbproxy_nse_by_imsi(cfg, mi.imsi, CACHE_USAGE_MS_REG_ENQ); |
| 1645 | if (!nse_peer) { |
| 1646 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name); |
| 1647 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
| 1648 | } else if (nse_peer->sgsn_facing) { |
| 1649 | LOGPNSE(nse, LOGL_ERROR, "Forwarding %s failed: IMSI cache contains SGSN NSE", pdut_name); |
| 1650 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 1651 | } |
| 1652 | gbproxy_imsi_cache_remove(cfg, mi.imsi, CACHE_USAGE_MS_REG_ENQ); |
| 1653 | gbprox_relay2nse(msg, nse_peer, ns_bvci); |
| 1654 | break; |
| 1655 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1656 | default: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1657 | LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name); |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1658 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1659 | rc = tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1660 | break; |
| 1661 | } |
| 1662 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1663 | return rc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1664 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1665 | err_no_bvc: |
Daniel Willmann | 723bb36 | 2021-09-28 18:51:47 +0200 | [diff] [blame] | 1666 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC %05u\n", pdut_name, bvci); |
Daniel Willmann | c4b913b | 2021-09-24 16:43:42 +0200 | [diff] [blame] | 1667 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI)); |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1668 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1669 | } |
| 1670 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1671 | |
| 1672 | /*********************************************************************** |
| 1673 | * libosmogb NS/BSSGP integration |
| 1674 | ***********************************************************************/ |
| 1675 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1676 | int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1677 | { |
| 1678 | int rc; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1679 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 1680 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 1681 | struct osmo_gprs_ns2_prim nsp = {}; |
| 1682 | |
| 1683 | nsp.bvci = msgb_bvci(msg); |
| 1684 | nsp.nsei = msgb_nsei(msg); |
| 1685 | |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1686 | osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1687 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
| 1688 | |
| 1689 | return rc; |
| 1690 | } |
| 1691 | |
| 1692 | /* Main input function for Gb proxy */ |
| 1693 | int gbprox_rcvmsg(void *ctx, struct msgb *msg) |
| 1694 | { |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1695 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1696 | uint16_t ns_bvci = msgb_bvci(msg); |
| 1697 | uint16_t nsei = msgb_nsei(msg); |
| 1698 | struct gbproxy_nse *nse; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1699 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1700 | nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN); |
| 1701 | if (nse) { |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1702 | /* ensure minimum length to decode PDU type */ |
| 1703 | if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr)) |
| 1704 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg); |
| 1705 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1706 | if (ns_bvci == 0 || ns_bvci == 1) |
| 1707 | return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1708 | else |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1709 | return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1712 | nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS); |
| 1713 | if (!nse) { |
| 1714 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei); |
| 1715 | nse = gbproxy_nse_alloc(cfg, nsei, false); |
| 1716 | } |
| 1717 | if (nse) { |
Daniel Willmann | f8cba65 | 2021-02-12 04:59:47 +0100 | [diff] [blame] | 1718 | /* ensure minimum length to decode PDU type */ |
| 1719 | if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr)) |
| 1720 | return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg); |
| 1721 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1722 | if (ns_bvci == 0 || ns_bvci == 1) |
| 1723 | return gbprox_rx_sig_from_bss(nse, msg, ns_bvci); |
| 1724 | else |
| 1725 | return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci); |
| 1726 | } |
| 1727 | |
| 1728 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1729 | } |
| 1730 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1731 | /* TODO: What about handling: |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1732 | * GPRS_NS2_AFF_CAUSE_VC_FAILURE, |
| 1733 | GPRS_NS2_AFF_CAUSE_VC_RECOVERY, |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1734 | osmocom own causes |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1735 | GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED, |
| 1736 | GPRS_NS2_AFF_CAUSE_SNS_FAILURE, |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1737 | */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1738 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1739 | void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1740 | { |
Daniel Willmann | 1f3470f | 2021-03-11 09:53:42 +0100 | [diff] [blame] | 1741 | int i; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1742 | struct gbproxy_bvc *bvc; |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1743 | struct gbproxy_nse *nse; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1744 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1745 | switch (nsp->u.status.cause) { |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1746 | case GPRS_NS2_AFF_CAUSE_SNS_FAILURE: |
| 1747 | case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED: |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1748 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1749 | |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1750 | case GPRS_NS2_AFF_CAUSE_RECOVERY: |
Harald Welte | 9b367d2 | 2021-01-18 13:55:51 +0100 | [diff] [blame] | 1751 | LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei); |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1752 | nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN); |
| 1753 | if (nse) { |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 1754 | nse->alive = true; |
Daniel Willmann | 38b9c9a | 2021-03-09 15:54:44 +0100 | [diff] [blame] | 1755 | // Update the NSE max SDU len |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1756 | nse->max_sdu_len = nsp->u.status.mtu; |
Daniel Willmann | 38b9c9a | 2021-03-09 15:54:44 +0100 | [diff] [blame] | 1757 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1758 | uint8_t cause = BSSGP_CAUSE_OML_INTERV; |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1759 | bvc = gbproxy_bvc_by_bvci(nse, 0); |
Daniel Willmann | 38b9c9a | 2021-03-09 15:54:44 +0100 | [diff] [blame] | 1760 | if (bvc) { |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1761 | bssgp_bvc_fsm_set_max_pdu_len(bvc->fi, nse->max_sdu_len); |
Daniel Willmann | 3ea3793 | 2021-02-10 13:41:14 +0100 | [diff] [blame] | 1762 | osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
Daniel Willmann | 38b9c9a | 2021-03-09 15:54:44 +0100 | [diff] [blame] | 1763 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1764 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1765 | break; |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1766 | case GPRS_NS2_AFF_CAUSE_FAILURE: |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1767 | nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_BSS | NSE_F_SGSN); |
| 1768 | if (!nse) { |
| 1769 | LOGP(DGPRS, LOGL_ERROR, "Unknown NSE(%05d) became unavailable\n", nsp->nsei); |
| 1770 | break; |
| 1771 | } |
Daniel Willmann | 37518b3 | 2021-05-27 18:13:36 +0200 | [diff] [blame] | 1772 | |
| 1773 | nse->alive = false; |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1774 | if (nse->sgsn_facing) { |
Daniel Willmann | 1f3470f | 2021-03-11 09:53:42 +0100 | [diff] [blame] | 1775 | struct hlist_node *ntmp; |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1776 | /* SGSN */ |
| 1777 | /* TODO: When to block all PtP towards bss? Only if all SGSN are down? */ |
Daniel Willmann | 1f3470f | 2021-03-11 09:53:42 +0100 | [diff] [blame] | 1778 | hash_for_each_safe(nse->bvcs, i, ntmp, bvc, list) { |
| 1779 | if (bvc->bvci == 0) |
| 1780 | continue; |
| 1781 | gbproxy_bvc_free(bvc); |
| 1782 | } |
Pau Espin Pedrol | 5643836 | 2021-06-04 18:03:44 +0200 | [diff] [blame] | 1783 | rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_RESTART_RESET_SGSN)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1784 | } else { |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1785 | /* BSS became unavailable |
| 1786 | * Block matching PtP-BVCs on SGSN-side */ |
| 1787 | hash_for_each(nse->bvcs, i, bvc, list) { |
| 1788 | if (bvc->bvci == 0) |
| 1789 | continue; |
| 1790 | /* Get BVC for each SGSN and send block request */ |
| 1791 | struct gbproxy_cell *cell = bvc->cell; |
| 1792 | for (int j = 0; j < GBPROXY_MAX_NR_SGSN; j++) { |
| 1793 | struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[j]; |
| 1794 | if (!sgsn_bvc) |
| 1795 | continue; |
| 1796 | |
| 1797 | /* Block BVC, indicate BSS equipment failure */ |
| 1798 | uint8_t cause = BSSGP_CAUSE_EQUIP_FAIL; |
| 1799 | osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_REQ_BLOCK, &cause); |
Daniel Willmann | dc763fd | 2021-09-24 16:45:38 +0200 | [diff] [blame] | 1800 | sgsn_bvc->inactive = true; |
Daniel Willmann | f96cac5 | 2021-03-09 16:14:18 +0100 | [diff] [blame] | 1801 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1802 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1803 | |
Daniel Willmann | 1f3470f | 2021-03-11 09:53:42 +0100 | [diff] [blame] | 1804 | /* This frees the BVCs for us as well */ |
| 1805 | gbproxy_nse_free(nse); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1806 | } |
Harald Welte | 9b367d2 | 2021-01-18 13:55:51 +0100 | [diff] [blame] | 1807 | LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1808 | break; |
| 1809 | default: |
Harald Welte | 9b367d2 | 2021-01-18 13:55:51 +0100 | [diff] [blame] | 1810 | LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n", |
Harald Welte | 95cf9fb | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 1811 | gprs_ns2_aff_cause_prim_str(nsp->u.status.cause)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1812 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1813 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1816 | /* called by the ns layer */ |
| 1817 | int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 1818 | { |
| 1819 | struct osmo_gprs_ns2_prim *nsp; |
| 1820 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1821 | uintptr_t bvci; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1822 | int rc = 0; |
| 1823 | |
| 1824 | if (oph->sap != SAP_NS) |
| 1825 | return 0; |
| 1826 | |
| 1827 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 1828 | |
| 1829 | if (oph->operation != PRIM_OP_INDICATION) { |
Harald Welte | 9b367d2 | 2021-01-18 13:55:51 +0100 | [diff] [blame] | 1830 | LOGP(DGPRS, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n", |
Harald Welte | 95cf9fb | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 1831 | get_value_string(osmo_prim_op_names, oph->operation)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1832 | return 0; |
| 1833 | } |
| 1834 | |
| 1835 | switch (oph->primitive) { |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1836 | case GPRS_NS2_PRIM_UNIT_DATA: |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1837 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1838 | /* hand the message into the BSSGP implementation */ |
| 1839 | msgb_bssgph(oph->msg) = oph->msg->l3h; |
| 1840 | msgb_bvci(oph->msg) = nsp->bvci; |
| 1841 | msgb_nsei(oph->msg) = nsp->nsei; |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1842 | bvci = nsp->bvci | BVC_LOG_CTX_FLAG; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1843 | |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1844 | log_set_context(LOG_CTX_GB_BVC, (void *)bvci); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1845 | rc = gbprox_rcvmsg(cfg, oph->msg); |
Daniel Willmann | b655010 | 2020-11-04 17:32:56 +0100 | [diff] [blame] | 1846 | msgb_free(oph->msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1847 | break; |
Alexander Couzens | 55c36f9 | 2021-01-27 20:56:55 +0100 | [diff] [blame] | 1848 | case GPRS_NS2_PRIM_STATUS: |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1849 | gprs_ns_prim_status_cb(cfg, nsp); |
| 1850 | break; |
| 1851 | default: |
Harald Welte | 9b367d2 | 2021-01-18 13:55:51 +0100 | [diff] [blame] | 1852 | LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n", |
Harald Welte | 95cf9fb | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 1853 | gprs_ns2_prim_str(oph->primitive), |
| 1854 | get_value_string(osmo_prim_op_names, oph->operation)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | |
| 1858 | return rc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | void gbprox_reset(struct gbproxy_config *cfg) |
| 1862 | { |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1863 | struct gbproxy_nse *nse; |
| 1864 | struct hlist_node *ntmp; |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 1865 | int i, j; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1866 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1867 | hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) { |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 1868 | struct gbproxy_bvc *bvc; |
| 1869 | struct hlist_node *tmp; |
| 1870 | hash_for_each_safe(nse->bvcs, j, tmp, bvc, list) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1871 | gbproxy_bvc_free(bvc); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1872 | |
| 1873 | gbproxy_nse_free(nse); |
| 1874 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1875 | /* FIXME: cells */ |
| 1876 | /* FIXME: SGSN side BVCs (except signaling) */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1877 | |
| 1878 | rate_ctr_group_free(cfg->ctrg); |
| 1879 | gbproxy_init_config(cfg); |
| 1880 | } |
| 1881 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1882 | static void tlli_cache_cleanup(void *data) |
| 1883 | { |
| 1884 | struct gbproxy_config *cfg = data; |
| 1885 | gbproxy_tlli_cache_cleanup(cfg); |
| 1886 | |
| 1887 | /* TODO: Disable timer when cache is empty */ |
| 1888 | osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0); |
| 1889 | } |
| 1890 | |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 1891 | static void imsi_cache_cleanup(void *data) |
| 1892 | { |
| 1893 | struct gbproxy_config *cfg = data; |
| 1894 | gbproxy_imsi_cache_cleanup(cfg); |
| 1895 | |
| 1896 | /* TODO: Disable timer when cache is empty */ |
| 1897 | osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0); |
| 1898 | } |
| 1899 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1900 | int gbproxy_init_config(struct gbproxy_config *cfg) |
| 1901 | { |
| 1902 | struct timespec tp; |
| 1903 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 1904 | /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */ |
| 1905 | cfg->pool.bvc_fc_ratio = 100; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 1906 | cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg); |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1907 | /* TODO: Make configurable */ |
Daniel Willmann | bd12f3f | 2021-01-13 18:16:04 +0100 | [diff] [blame] | 1908 | cfg->tlli_cache.timeout = 10; |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 1909 | cfg->imsi_cache.timeout = 10; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 1910 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1911 | hash_init(cfg->bss_nses); |
Daniel Willmann | 1e7be5d | 2020-12-21 18:08:21 +0100 | [diff] [blame] | 1912 | hash_init(cfg->sgsn_nses); |
| 1913 | hash_init(cfg->cells); |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1914 | hash_init(cfg->tlli_cache.entries); |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 1915 | INIT_LLIST_HEAD(&cfg->sgsns); |
| 1916 | |
Daniel Willmann | 77493b1 | 2020-12-29 21:13:31 +0100 | [diff] [blame] | 1917 | osmo_timer_setup(&cfg->tlli_cache.timer, tlli_cache_cleanup, cfg); |
| 1918 | osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0); |
| 1919 | |
Daniel Willmann | c8a5009 | 2021-01-17 13:11:41 +0100 | [diff] [blame] | 1920 | /* We could also combine both timers */ |
| 1921 | osmo_timer_setup(&cfg->imsi_cache.timer, imsi_cache_cleanup, cfg); |
| 1922 | osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0); |
| 1923 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1924 | cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0); |
| 1925 | if (!cfg->ctrg) { |
| 1926 | LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n"); |
| 1927 | return -1; |
| 1928 | } |
| 1929 | osmo_clock_gettime(CLOCK_REALTIME, &tp); |
Harald Welte | c169de4 | 2020-12-07 13:12:13 +0100 | [diff] [blame] | 1930 | osmo_fsm_log_timeouts(true); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1931 | |
| 1932 | return 0; |
Oliver Smith | 29532c2 | 2021-01-29 11:13:00 +0100 | [diff] [blame] | 1933 | } |