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> |
| 40 | #include <osmocom/core/stats.h> |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 41 | #include <osmocom/core/utils.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 42 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 43 | #include <osmocom/gprs/gprs_ns2.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 44 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 45 | #include <osmocom/gprs/gprs_bssgp2.h> |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 46 | #include <osmocom/gprs/gprs_bssgp_bss.h> |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 47 | #include <osmocom/gprs/bssgp_bvc_fsm.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 48 | |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 49 | #include <osmocom/gsm/gsm23236.h> |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 50 | #include <osmocom/gsm/gsm_utils.h> |
| 51 | |
| 52 | #include <osmocom/sgsn/signal.h> |
| 53 | #include <osmocom/sgsn/debug.h> |
| 54 | #include <osmocom/sgsn/gprs_gb_parse.h> |
| 55 | #include <osmocom/sgsn/gb_proxy.h> |
| 56 | |
| 57 | #include <osmocom/sgsn/gprs_llc.h> |
| 58 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
| 59 | #include <osmocom/sgsn/gprs_utils.h> |
| 60 | |
| 61 | extern void *tall_sgsn_ctx; |
| 62 | |
| 63 | static const struct rate_ctr_desc global_ctr_description[] = { |
| 64 | { "inv-bvci", "Invalid BVC Identifier " }, |
| 65 | { "inv-lai", "Invalid Location Area Identifier" }, |
| 66 | { "inv-rai", "Invalid Routing Area Identifier " }, |
| 67 | { "inv-nsei", "No BVC established for NSEI " }, |
| 68 | { "proto-err:bss", "BSSGP protocol error (BSS )" }, |
| 69 | { "proto-err:sgsn", "BSSGP protocol error (SGSN)" }, |
| 70 | { "not-supp:bss", "Feature not supported (BSS )" }, |
| 71 | { "not-supp:sgsn", "Feature not supported (SGSN)" }, |
| 72 | { "restart:sgsn", "Restarted RESET procedure (SGSN)" }, |
| 73 | { "tx-err:sgsn", "NS Transmission error (SGSN)" }, |
| 74 | { "error", "Other error " }, |
| 75 | { "mod-peer-err", "Patch error: no peer " }, |
| 76 | }; |
| 77 | |
| 78 | static const struct rate_ctr_group_desc global_ctrg_desc = { |
| 79 | .group_name_prefix = "gbproxy:global", |
| 80 | .group_description = "GBProxy Global Statistics", |
| 81 | .num_ctr = ARRAY_SIZE(global_ctr_description), |
| 82 | .ctr_desc = global_ctr_description, |
| 83 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 84 | }; |
| 85 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 86 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc, |
Daniel Willmann | 35f7d33 | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 87 | uint16_t ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 88 | |
Harald Welte | a0f7073 | 2020-12-05 17:50:23 +0100 | [diff] [blame] | 89 | |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 90 | /* generate BVC-STATUS message with cause value derived from TLV-parser error */ |
| 91 | static int tx_status_from_tlvp(enum osmo_tlv_parser_error tlv_p_err, struct msgb *orig_msg) |
| 92 | { |
| 93 | uint8_t bssgp_cause; |
| 94 | switch (tlv_p_err) { |
| 95 | case OSMO_TLVP_ERR_MAND_IE_MISSING: |
| 96 | bssgp_cause = BSSGP_CAUSE_MISSING_MAND_IE; |
| 97 | break; |
| 98 | default: |
| 99 | bssgp_cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC; |
| 100 | } |
| 101 | return bssgp_tx_status(bssgp_cause, NULL, orig_msg); |
| 102 | } |
| 103 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 104 | /* strip off the NS header */ |
| 105 | static void strip_ns_hdr(struct msgb *msg) |
| 106 | { |
| 107 | int strip_len = msgb_bssgph(msg) - msg->data; |
| 108 | msgb_pull(msg, strip_len); |
| 109 | } |
| 110 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 111 | #if 0 |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 112 | /* 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] | 113 | static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg, |
| 114 | uint16_t ns_bvci, uint16_t sgsn_nsei) |
| 115 | { |
| 116 | /* create a copy of the message so the old one can |
| 117 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 118 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 119 | struct osmo_gprs_ns2_prim nsp = {}; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 120 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn"); |
| 121 | int rc; |
| 122 | |
Daniel Willmann | 3696dce | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 123 | 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] | 124 | msgb_nsei(msg), ns_bvci, sgsn_nsei); |
| 125 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 126 | nsp.bvci = ns_bvci; |
| 127 | nsp.nsei = sgsn_nsei; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 128 | |
| 129 | strip_ns_hdr(msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 130 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 131 | PRIM_OP_REQUEST, msg); |
| 132 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 133 | if (rc < 0) |
| 134 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 135 | return rc; |
| 136 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 137 | #endif |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 138 | |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 139 | /* feed a message down the NSE */ |
| 140 | static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse, |
Daniel Willmann | 35f7d33 | 2020-11-03 21:11:45 +0100 | [diff] [blame] | 141 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 142 | { |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 143 | OSMO_ASSERT(nse); |
| 144 | OSMO_ASSERT(nse->cfg); |
| 145 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 146 | /* create a copy of the message so the old one can |
| 147 | * be free()d safely when we return from gbprox_rcvmsg() */ |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 148 | struct gprs_ns2_inst *nsi = nse->cfg->nsi; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 149 | struct osmo_gprs_ns2_prim nsp = {}; |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 150 | struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse"); |
Harald Welte | fe05958 | 2020-11-18 12:01:46 +0100 | [diff] [blame] | 151 | uint32_t tlli; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 152 | int rc; |
| 153 | |
Daniel Willmann | 98b1b45 | 2020-12-21 10:40:27 +0100 | [diff] [blame] | 154 | DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg), |
| 155 | !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] | 156 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 157 | nsp.bvci = ns_bvci; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 158 | nsp.nsei = nse->nsei; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 159 | |
| 160 | /* Strip the old NS header, it will be replaced with a new one */ |
| 161 | strip_ns_hdr(msg); |
| 162 | |
Harald Welte | fe05958 | 2020-11-18 12:01:46 +0100 | [diff] [blame] | 163 | /* TS 48.018 Section 5.4.2: The link selector parameter is |
| 164 | * defined in 3GPP TS 48.016. At one side of the Gb interface, |
| 165 | * all BSSGP UNITDATA PDUs related to an MS shall be passed with |
| 166 | * the same LSP, e.g. the LSP contains the MS's TLLI, to the |
| 167 | * underlying network service. */ |
| 168 | if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1) |
| 169 | nsp.u.unitdata.link_selector = tlli; |
| 170 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 171 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, |
| 172 | PRIM_OP_REQUEST, msg); |
| 173 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 174 | /* FIXME: We need a counter group for gbproxy_nse */ |
| 175 | //if (rc < 0) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 176 | // rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 177 | |
| 178 | return rc; |
| 179 | } |
| 180 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 181 | /* feed a message down the NS-VC associated with the specified bvc */ |
| 182 | static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc, |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 183 | uint16_t ns_bvci) |
| 184 | { |
| 185 | int rc; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 186 | struct gbproxy_nse *nse = bvc->nse; |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 187 | OSMO_ASSERT(nse); |
| 188 | |
| 189 | rc = gbprox_relay2nse(old_msg, nse, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 190 | if (rc < 0) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 191 | rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 192 | |
| 193 | return rc; |
| 194 | } |
| 195 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 196 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 201 | |
| 202 | /*********************************************************************** |
| 203 | * PTP BVC handling |
| 204 | ***********************************************************************/ |
| 205 | |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 206 | /* FIXME: Handle the tlli NULL case correctly, |
| 207 | * This function should take a generic selector |
| 208 | * and choose an sgsn based on that |
| 209 | */ |
| 210 | static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli) |
| 211 | { |
| 212 | struct gbproxy_sgsn *sgsn = NULL; |
| 213 | struct gbproxy_sgsn *sgsn_avoid = NULL; |
| 214 | |
| 215 | int tlli_type; |
| 216 | int16_t nri; |
| 217 | bool null_nri = false; |
| 218 | |
| 219 | if (!tlli) { |
| 220 | sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list); |
| 221 | if (!sgsn) { |
| 222 | return NULL; |
| 223 | } |
| 224 | LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first SGSN\n"); |
| 225 | return sgsn; |
| 226 | } |
| 227 | |
| 228 | if (cfg->pool.nri_bitlen == 0) { |
| 229 | /* Pooling is disabled */ |
| 230 | sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list); |
| 231 | if (!sgsn) { |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | LOGPSGSN(sgsn, LOGL_INFO, "Pooling disabled, using first configured SGSN\n"); |
| 236 | } else { |
| 237 | /* Pooling is enabled, try to use the NRI for routing to an SGSN |
| 238 | * See 3GPP TS 23.236 Ch. 5.3.2 */ |
| 239 | tlli_type = gprs_tlli_type(*tlli); |
| 240 | if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) { |
| 241 | /* Only get/use the NRI if tlli type is local */ |
| 242 | osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen); |
| 243 | if (nri >= 0) { |
| 244 | /* Get the SGSN for the NRI */ |
| 245 | sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri); |
| 246 | if (sgsn && !null_nri) |
| 247 | return sgsn; |
| 248 | /* If the NRI is the null NRI, we need to avoid the chosen SGSN */ |
| 249 | if (null_nri && sgsn) { |
| 250 | sgsn_avoid = sgsn; |
| 251 | } |
| 252 | } else { |
| 253 | /* We couldn't get the NRI from the TLLI */ |
| 254 | LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %u\n", *tlli); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid |
| 260 | * NOTE: This function is not stable if the number of SGSNs or allow_attach changes |
| 261 | * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that |
| 262 | * we can just wait for the MS to reattempt the procedure. |
| 263 | */ |
| 264 | if (!sgsn) |
| 265 | sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli); |
| 266 | |
| 267 | if (!sgsn) { |
| 268 | LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | return sgsn; |
| 273 | } |
| 274 | |
| 275 | /*! Find the correct gbproxy_bvc given a cell and an SGSN |
| 276 | * \param[in] cfg The gbproxy configuration |
| 277 | * \param[in] cell The cell the message belongs to |
| 278 | * \param[in] tlli An optional TLLI used for tracking |
| 279 | * \return Returns 0 on success, otherwise a negative value |
| 280 | */ |
| 281 | static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli) |
| 282 | { |
| 283 | struct gbproxy_sgsn *sgsn; |
| 284 | struct gbproxy_bvc *sgsn_bvc = NULL; |
Harald Welte | 02d7c48 | 2020-12-30 12:13:36 +0100 | [diff] [blame] | 285 | int i; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 286 | |
| 287 | sgsn = gbproxy_select_sgsn(cfg, tlli); |
| 288 | if (!sgsn) { |
| 289 | LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n"); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | /* Get the BVC for this SGSN/NSE */ |
Harald Welte | 02d7c48 | 2020-12-30 12:13:36 +0100 | [diff] [blame] | 294 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 295 | sgsn_bvc = cell->sgsn_bvc[i]; |
| 296 | if (!sgsn_bvc) |
| 297 | continue; |
| 298 | if (sgsn->nse != sgsn_bvc->nse) |
| 299 | continue; |
| 300 | |
| 301 | return sgsn_bvc; |
| 302 | } |
| 303 | |
| 304 | /* This shouldn't happen */ |
Daniel Willmann | a648f3c | 2020-12-28 18:07:27 +0100 | [diff] [blame] | 305 | 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] | 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | /*! Send a message to the next SGSN, possibly ignoring the null SGSN |
| 310 | * route an uplink message on a PTP-BVC to a SGSN using the TLLI |
| 311 | * \param[in] cell The cell the message belongs to |
| 312 | * \param[in] msg The BSSGP message |
| 313 | * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI) |
| 314 | * \param[in] tlli An optional TLLI used for tracking |
| 315 | * \return Returns 0 on success, otherwise a negative value |
| 316 | */ |
| 317 | 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] | 318 | bool sig_bvci) |
| 319 | { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 320 | struct gbproxy_config *cfg = cell->cfg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 321 | struct gbproxy_bvc *sgsn_bvc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 322 | |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 323 | sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli); |
| 324 | if (!sgsn_bvc) { |
| 325 | LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli); |
| 326 | return -EINVAL; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 327 | } |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 328 | |
| 329 | return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 332 | /* Receive an incoming PTP message from a BSS-side NS-VC */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 333 | 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] | 334 | { |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 335 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 336 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 337 | struct gbproxy_bvc *bss_bvc; |
| 338 | struct tlv_parsed tp; |
| 339 | char log_pfx[32]; |
| 340 | uint32_t tlli; |
| 341 | int rc; |
| 342 | |
| 343 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 344 | |
| 345 | 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] | 346 | |
Daniel Willmann | 06331ac | 2020-12-10 17:59:46 +0100 | [diff] [blame] | 347 | if (ns_bvci == 0 || ns_bvci == 1) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 348 | LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 349 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 350 | } |
| 351 | |
| 352 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 353 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 354 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 355 | } |
| 356 | |
| 357 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 358 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 359 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 360 | } |
| 361 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 362 | bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci); |
| 363 | if (!bss_bvc) { |
| 364 | LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n", |
| 365 | log_pfx, pdut_name); |
| 366 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 369 | /* UL_UNITDATA has a different header than all other uplink PDUs */ |
| 370 | if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA) { |
| 371 | const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 372 | if (msgb_bssgp_len(msg) < sizeof(*budh)) |
| 373 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
| 374 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data, |
| 375 | msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx); |
| 376 | /* populate TLLI from the fixed headser into the TLV-parsed array so later code |
| 377 | * doesn't have to worry where the TLLI came from */ |
| 378 | tp.lv[BSSGP_IE_TLLI].len = 4; |
| 379 | tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli; |
| 380 | } else { |
| 381 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data, |
| 382 | msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx); |
| 383 | } |
| 384 | if (rc < 0) { |
| 385 | rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
| 386 | return tx_status_from_tlvp(rc, msg); |
| 387 | } |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 388 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
| 389 | msgb_bcid(msg) = (void *)&tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 390 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 391 | switch (bgph->pdu_type) { |
| 392 | case BSSGP_PDUT_UL_UNITDATA: |
| 393 | case BSSGP_PDUT_RA_CAPA_UPDATE: |
| 394 | case BSSGP_PDUT_FLOW_CONTROL_MS: |
| 395 | case BSSGP_PDUT_DOWNLOAD_BSS_PFC: |
| 396 | case BSSGP_PDUT_CREATE_BSS_PFC_ACK: |
| 397 | case BSSGP_PDUT_CREATE_BSS_PFC_NACK: |
| 398 | case BSSGP_PDUT_MODIFY_BSS_PFC_ACK: |
| 399 | case BSSGP_PDUT_DELETE_BSS_PFC_ACK: |
| 400 | case BSSGP_PDUT_FLOW_CONTROL_PFC: |
| 401 | case BSSGP_PDUT_DELETE_BSS_PFC_REQ: |
| 402 | case BSSGP_PDUT_PS_HO_REQUIRED: |
| 403 | case BSSGP_PDUT_PS_HO_REQUEST_ACK: |
| 404 | case BSSGP_PDUT_PS_HO_REQUEST_NACK: |
| 405 | case BSSGP_PDUT_PS_HO_COMPLETE: |
| 406 | case BSSGP_PDUT_PS_HO_CANCEL: |
| 407 | /* We can route based on TLLI-NRI */ |
| 408 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI)); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 409 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 410 | break; |
| 411 | case BSSGP_PDUT_RADIO_STATUS: |
| 412 | if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) { |
| 413 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI)); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 414 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 415 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) { |
| 416 | /* we treat the TMSI like a TLLI and extract the NRI from it */ |
| 417 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI)); |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 418 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 419 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) { |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 420 | // FIXME: Use the IMSI as selector? |
| 421 | rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false); |
| 422 | //rc = gbprox_bss2sgsn_hashed(bss_bvc->cell, msg, NULL); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 423 | } else |
| 424 | LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n"); |
| 425 | break; |
| 426 | case BSSGP_PDUT_DUMMY_PAGING_PS_RESP: |
| 427 | case BSSGP_PDUT_PAGING_PS_REJECT: |
| 428 | /* TODO: Implement via state tracking of PAGING-PS + DUMMY_PAGING_PS */ |
| 429 | LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name); |
| 430 | break; |
| 431 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 432 | 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] | 433 | break; |
| 434 | case BSSGP_PDUT_STATUS: |
| 435 | /* TODO: Implement by inspecting the contained PDU */ |
| 436 | if (!TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR)) |
| 437 | break; |
| 438 | LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name); |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* Receive an incoming PTP message from a SGSN-side NS-VC */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 446 | 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] | 447 | { |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 448 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 449 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 450 | struct gbproxy_bvc *sgsn_bvc, *bss_bvc; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 451 | struct tlv_parsed tp; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 452 | char log_pfx[32]; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 453 | int rc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 454 | |
| 455 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 456 | |
| 457 | 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] | 458 | |
Daniel Willmann | 06331ac | 2020-12-10 17:59:46 +0100 | [diff] [blame] | 459 | if (ns_bvci == 0 || ns_bvci == 1) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 460 | LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 461 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 462 | } |
| 463 | |
| 464 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 465 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 466 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 467 | } |
| 468 | |
| 469 | if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 470 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 471 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 472 | } |
| 473 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 474 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci); |
| 475 | if (!sgsn_bvc) { |
| 476 | LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n", |
| 477 | log_pfx, pdut_name); |
| 478 | rate_ctr_inc(&nse->cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 479 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 482 | if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) { |
| 483 | LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name); |
| 484 | rate_ctr_inc(&sgsn_bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 485 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg); |
| 486 | } |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 487 | |
| 488 | /* DL_UNITDATA has a different header than all other uplink PDUs */ |
| 489 | if (bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 490 | const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 491 | if (msgb_bssgp_len(msg) < sizeof(*budh)) |
| 492 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
| 493 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data, |
| 494 | msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx); |
| 495 | /* populate TLLI from the fixed headser into the TLV-parsed array so later code |
| 496 | * doesn't have to worry where the TLLI came from */ |
| 497 | tp.lv[BSSGP_IE_TLLI].len = 4; |
| 498 | tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli; |
| 499 | } else { |
| 500 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data, |
| 501 | msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx); |
| 502 | } |
| 503 | if (rc < 0) { |
| 504 | rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
| 505 | return tx_status_from_tlvp(rc, msg); |
| 506 | } |
| 507 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
| 508 | msgb_bcid(msg) = (void *)&tp; |
| 509 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 510 | OSMO_ASSERT(sgsn_bvc->cell); |
| 511 | bss_bvc = sgsn_bvc->cell->bss_bvc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 512 | |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 513 | switch (bgph->pdu_type) { |
| 514 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 515 | return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg); |
| 516 | default: |
| 517 | return gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci); |
| 518 | } |
| 519 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 522 | /*********************************************************************** |
| 523 | * BVC FSM call-backs |
| 524 | ***********************************************************************/ |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 525 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 526 | /* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */ |
| 527 | static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv) |
| 528 | { |
| 529 | unsigned int i; |
| 530 | |
| 531 | for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) { |
| 532 | struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i]; |
| 533 | if (!sgsn_bvc) |
| 534 | continue; |
| 535 | osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* BVC FSM informs us about a BSS-side reset of the signaling BVC */ |
| 540 | static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 541 | uint16_t cell_id, uint8_t cause, void *priv) |
| 542 | { |
| 543 | struct gbproxy_bvc *sig_bvc = priv; |
| 544 | struct gbproxy_nse *nse = sig_bvc->nse; |
| 545 | struct gbproxy_bvc *ptp_bvc; |
| 546 | unsigned int i; |
| 547 | |
| 548 | /* BLOCK all SGSN-side PTP BVC within this NSE */ |
| 549 | hash_for_each(nse->bvcs, i, ptp_bvc, list) { |
| 550 | if (ptp_bvc == sig_bvc) |
| 551 | continue; |
| 552 | OSMO_ASSERT(ptp_bvc->cell); |
| 553 | |
| 554 | 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] | 555 | } |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 556 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 557 | /* Delete all BSS-side PTP BVC within this NSE */ |
| 558 | gbproxy_cleanup_bvcs(nse, 0); |
| 559 | |
| 560 | /* TODO: we keep the "CELL" around for now, re-connecting it to |
| 561 | * any (later) new PTP-BVC for that BVCI. Not sure if that's the |
| 562 | * best idea ? */ |
| 563 | } |
| 564 | |
| 565 | /* forward declaration */ |
| 566 | static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops; |
| 567 | |
| 568 | static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = { |
| 569 | .reset_notification = bss_sig_bvc_reset_notif, |
| 570 | }; |
| 571 | |
| 572 | /* BVC FSM informs us about a BSS-side reset of a PTP BVC */ |
| 573 | static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 574 | uint16_t cell_id, uint8_t cause, void *priv) |
| 575 | { |
| 576 | struct gbproxy_bvc *bvc = priv; |
| 577 | struct gbproxy_config *cfg = bvc->nse->cfg; |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 578 | struct gbproxy_nse *sgsn_nse; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 579 | unsigned int i; |
| 580 | |
| 581 | OSMO_ASSERT(bvci != 0); |
| 582 | |
| 583 | if (!bvc->cell) { |
| 584 | /* see if we have a CELL dangling around */ |
| 585 | bvc->cell = gbproxy_cell_by_bvci(cfg, bvci); |
| 586 | if (bvc->cell) { |
| 587 | /* the CELL already exists. This means either it * was created before at an |
| 588 | * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a |
| 589 | * malconfiguration */ |
| 590 | if (bvc->cell->bss_bvc) { |
| 591 | LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already " |
| 592 | "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei); |
| 593 | LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting " |
| 594 | "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei); |
| 595 | gbproxy_bvc_free(bvc->cell->bss_bvc); |
| 596 | } |
| 597 | bvc->cell->bss_bvc = bvc; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (!bvc->cell) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 602 | /* if we end up here, it means this is the first time we received a BVC-RESET |
| 603 | * for this BVC. We need to create the 'cell' data structure and the SGSN-side |
| 604 | * BVC counterparts */ |
| 605 | |
| 606 | bvc->cell = gbproxy_cell_alloc(cfg, bvci); |
| 607 | OSMO_ASSERT(bvc->cell); |
Harald Welte | ee0cc81 | 2020-12-12 14:22:32 +0100 | [diff] [blame] | 608 | memcpy(bvc->cell->ra, bvc->ra, sizeof(bvc->cell->ra)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 609 | |
| 610 | /* link us to the cell and vice-versa */ |
| 611 | bvc->cell->bss_bvc = bvc; |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 612 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 613 | |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 614 | /* allocate (any missing) SGSN-side BVCs within the cell, and reset them */ |
| 615 | hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) { |
| 616 | struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci); |
| 617 | if (sgsn_bvc) |
| 618 | OSMO_ASSERT(sgsn_bvc->cell == bvc->cell || !sgsn_bvc->cell); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 619 | |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 620 | if (!sgsn_bvc) { |
| 621 | sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci); |
| 622 | OSMO_ASSERT(sgsn_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 623 | |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 624 | sgsn_bvc->cell = bvc->cell; |
| 625 | memcpy(sgsn_bvc->ra, bvc->cell->ra, sizeof(sgsn_bvc->ra)); |
| 626 | sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei, |
| 627 | bvci, ra_id, cell_id); |
| 628 | OSMO_ASSERT(sgsn_bvc->fi); |
| 629 | bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 630 | |
Harald Welte | 664c24e | 2020-12-12 15:01:17 +0100 | [diff] [blame] | 631 | gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | /* Trigger outbound BVC-RESET procedure toward each SGSN */ |
| 636 | dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
| 637 | } |
| 638 | |
| 639 | /* BVC FSM informs us about a BSS-side FSM state change */ |
| 640 | static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv) |
| 641 | { |
| 642 | struct gbproxy_bvc *bvc = priv; |
| 643 | struct gbproxy_cell *cell = bvc->cell; |
| 644 | uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi); |
| 645 | |
| 646 | /* we have just been created but due to callback ordering the cell is not associated */ |
| 647 | if (!cell) |
| 648 | return; |
| 649 | |
| 650 | switch (state) { |
| 651 | case BSSGP_BVCFSM_S_BLOCKED: |
| 652 | /* block the corresponding SGSN-side PTP BVCs */ |
| 653 | dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause); |
| 654 | break; |
| 655 | case BSSGP_BVCFSM_S_UNBLOCKED: |
| 656 | /* unblock the corresponding SGSN-side PTP BVCs */ |
| 657 | dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL); |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 662 | /* BVC FSM informs us about BVC-FC PDU receive */ |
| 663 | static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv) |
| 664 | { |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 665 | struct bssgp2_flow_ctrl fc_reduced; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 666 | struct gbproxy_bvc *bss_bvc = priv; |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 667 | struct gbproxy_cell *cell; |
| 668 | struct gbproxy_config *cfg; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 669 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 670 | OSMO_ASSERT(bss_bvc); |
| 671 | OSMO_ASSERT(fc); |
| 672 | |
| 673 | cell = bss_bvc->cell; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 674 | if (!cell) |
| 675 | return; |
| 676 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 677 | cfg = cell->cfg; |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 678 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 679 | /* reduce / scale according to configuration to make sure we only advertise a fraction |
| 680 | * of the capacity to each of the SGSNs in the pool */ |
| 681 | fc_reduced = *fc; |
| 682 | fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100; |
| 683 | fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100; |
| 684 | /* we don't modify the per-MS related values as any single MS is only served by one SGSN */ |
| 685 | |
| 686 | 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] | 687 | } |
| 688 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 689 | static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = { |
| 690 | .reset_notification = bss_ptp_bvc_reset_notif, |
| 691 | .state_chg_notification = bss_ptp_bvc_state_chg_notif, |
Harald Welte | 85a4027 | 2020-12-08 21:43:22 +0100 | [diff] [blame] | 692 | .rx_fc_bvc = bss_ptp_bvc_fc_bvc, |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 693 | }; |
| 694 | |
| 695 | /* BVC FSM informs us about a SGSN-side reset of a PTP BVC */ |
| 696 | static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 697 | uint16_t cell_id, uint8_t cause, void *priv) |
| 698 | { |
| 699 | struct gbproxy_bvc *bvc = priv; |
| 700 | |
| 701 | if (!bvc->cell) { |
| 702 | LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n"); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | OSMO_ASSERT(bvc->cell->bss_bvc); |
| 707 | |
| 708 | /* request reset of BSS-facing PTP-BVC */ |
| 709 | osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
| 710 | } |
| 711 | |
| 712 | static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = { |
| 713 | .reset_notification = sgsn_ptp_bvc_reset_notif, |
| 714 | }; |
| 715 | |
| 716 | /* BVC FSM informs us about a SGSN-side reset of the signaling BVC */ |
| 717 | static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, |
| 718 | uint16_t cell_id, uint8_t cause, void *priv) |
| 719 | { |
| 720 | struct gbproxy_bvc *bvc = priv; |
| 721 | struct gbproxy_config *cfg = bvc->nse->cfg; |
| 722 | struct gbproxy_nse *bss_nse; |
| 723 | unsigned int i; |
| 724 | |
| 725 | /* delete all SGSN-side PTP BVC for this SGSN */ |
| 726 | gbproxy_cleanup_bvcs(bvc->nse, 0); |
| 727 | /* FIXME: what to do about the cells? */ |
| 728 | /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */ |
| 729 | |
| 730 | /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */ |
| 731 | hash_for_each(cfg->bss_nses, i, bss_nse, list) { |
| 732 | struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0); |
| 733 | if (!bss_bvc) { |
| 734 | LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n"); |
| 735 | continue; |
| 736 | } |
| 737 | osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = { |
| 742 | .reset_notification = sgsn_sig_bvc_reset_notif, |
| 743 | }; |
| 744 | |
| 745 | /*********************************************************************** |
| 746 | * Signaling BVC handling |
| 747 | ***********************************************************************/ |
| 748 | |
| 749 | /* process a BVC-RESET message from the BSS side */ |
| 750 | static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp) |
| 751 | { |
| 752 | struct gbproxy_bvc *from_bvc = NULL; |
| 753 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 754 | uint32_t features = 0; // FIXME: make configurable |
| 755 | |
| 756 | LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci); |
| 757 | |
Harald Welte | 314647b | 2020-12-02 23:03:22 +0100 | [diff] [blame] | 758 | if (bvci == 0) { |
| 759 | /* If we receive a BVC reset on the signalling endpoint, we |
| 760 | * don't want the SGSN to reset, as the signalling endpoint |
| 761 | * is common for all point-to-point BVCs (and thus all BTS) */ |
Harald Welte | 324f065 | 2020-12-02 23:06:37 +0100 | [diff] [blame] | 762 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 763 | from_bvc = gbproxy_bvc_by_bvci(nse, 0); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 764 | if (!from_bvc) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 765 | from_bvc = gbproxy_bvc_alloc(nse, 0); |
| 766 | OSMO_ASSERT(from_bvc); |
| 767 | from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features); |
| 768 | if (!from_bvc->fi) { |
| 769 | LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n"); |
| 770 | gbproxy_bvc_free(from_bvc); |
| 771 | return -ENOMEM; |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 772 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 773 | bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc); |
| 774 | } |
| 775 | } else { |
| 776 | from_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 777 | if (!from_bvc) { |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 778 | /* if a PTP-BVC is reset, and we don't know that |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 779 | * PTP-BVCI yet, we should allocate a new bvc */ |
| 780 | from_bvc = gbproxy_bvc_alloc(nse, bvci); |
| 781 | OSMO_ASSERT(from_bvc); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 782 | from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi, |
| 783 | nse->nsei, bvci); |
| 784 | if (!from_bvc->fi) { |
| 785 | LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n"); |
| 786 | gbproxy_bvc_free(from_bvc); |
| 787 | return -ENOMEM; |
| 788 | } |
| 789 | 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] | 790 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 791 | #if 0 |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 792 | /* Could have moved to a different NSE */ |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 793 | if (!check_bvc_nsei(from_bvc, nsei)) { |
| 794 | LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei); |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 795 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 796 | struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei, false); |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 797 | if (!nse_new) { |
| 798 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for " |
| 799 | "BVCI=%05u\n", bvci, nsei); |
| 800 | bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg); |
| 801 | return 0; |
| 802 | } |
| 803 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 804 | /* Move bvc to different NSE */ |
| 805 | gbproxy_bvc_move(from_bvc, nse_new); |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 806 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 807 | #endif |
| 808 | /* FIXME: do we need this, if it happens within FSM? */ |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 809 | if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) { |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 810 | struct gprs_ra_id raid; |
| 811 | /* We have a Cell Identifier present in this |
| 812 | * PDU, this means we can extend our local |
| 813 | * state information about this particular cell |
| 814 | * */ |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 815 | memcpy(from_bvc->ra, TLVP_VAL(tp, BSSGP_IE_CELL_ID), sizeof(from_bvc->ra)); |
| 816 | gsm48_parse_ra(&raid, from_bvc->ra); |
| 817 | LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid)); |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 818 | } |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 819 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 820 | /* hand into FSM for further processing */ |
| 821 | osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg); |
| 822 | return 0; |
Harald Welte | 7df1e5a | 2020-12-02 22:53:26 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 825 | /* Receive an incoming signalling message from a BSS-side NS-VC */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 826 | 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] | 827 | { |
| 828 | 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] | 829 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 830 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 831 | struct tlv_parsed tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 832 | int data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 833 | struct gbproxy_bvc *from_bvc = NULL; |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 834 | char log_pfx[32]; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 835 | uint16_t ptp_bvci; |
| 836 | uint32_t tlli; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 837 | int rc; |
| 838 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 839 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 840 | |
| 841 | LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 842 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 843 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 844 | LOGP(DGPRS, LOGL_NOTICE, "%s %s BVCI=%05u is not signalling\n", log_pfx, pdut_name, ns_bvci); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 845 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 848 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 849 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 850 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 851 | } |
| 852 | |
| 853 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 854 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 855 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 858 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, pdu_type, bgph->data, data_len, 0, 0, |
| 859 | DGPRS, log_pfx); |
| 860 | if (rc < 0) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 861 | rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 862 | return tx_status_from_tlvp(rc, msg); |
| 863 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 864 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
| 865 | msgb_bcid(msg) = (void *)&tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 866 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 867 | /* special case handling for some PDU types */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 868 | switch (pdu_type) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 869 | case BSSGP_PDUT_BVC_RESET: |
| 870 | /* resolve or create gbproxy_bvc + handlei n BVC-FSM */ |
| 871 | ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 872 | return rx_bvc_reset_from_bss(nse, msg, &tp); |
| 873 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 874 | ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 875 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 876 | if (!from_bvc) |
| 877 | goto err_no_bvc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 878 | return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg); |
| 879 | case BSSGP_PDUT_BVC_BLOCK: |
| 880 | ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 881 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 882 | if (!from_bvc) |
| 883 | goto err_no_bvc; |
| 884 | return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg); |
| 885 | case BSSGP_PDUT_BVC_UNBLOCK: |
| 886 | ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 887 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 888 | if (!from_bvc) |
| 889 | goto err_no_bvc; |
| 890 | return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg); |
| 891 | case BSSGP_PDUT_SUSPEND: |
| 892 | case BSSGP_PDUT_RESUME: |
| 893 | /* FIXME: Implement TLLI Cache. Every SUSPEND/RESUME we must |
| 894 | * take record of the TLLI->BVC mapping so we can map |
| 895 | * back from TLLI->BVC when the SUSPEND/RESUME-ACK |
| 896 | * arrives. Cache should have a timeout of 1-3 seconds |
| 897 | * and the ACK should explicitly delete entries. */ |
| 898 | #if 0 |
| 899 | /* TODO: Validate the RAI for consistency with the RAI |
| 900 | * we expect for any of the BVC within this BSS side NSE */ |
| 901 | memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra)); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 902 | gsm48_parse_ra(&raid, from_bvc->ra); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 903 | #endif |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 904 | break; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 905 | case BSSGP_PDUT_STATUS: |
| 906 | /* FIXME: inspect the erroneous PDU IE (if any) and check |
| 907 | * if we can extract a TLLI/RNI to route it to the correct SGSN */ |
| 908 | break; |
| 909 | case BSSGP_PDUT_RAN_INFO: |
| 910 | case BSSGP_PDUT_RAN_INFO_REQ: |
| 911 | case BSSGP_PDUT_RAN_INFO_ACK: |
| 912 | case BSSGP_PDUT_RAN_INFO_ERROR: |
| 913 | case BSSGP_PDUT_RAN_INFO_APP_ERROR: |
| 914 | /* FIXME: route based in RIM Routing IE */ |
| 915 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
| 916 | break; |
| 917 | case BSSGP_PDUT_LLC_DISCARD: |
| 918 | case BSSGP_PDUT_FLUSH_LL_ACK: |
| 919 | /* route based on BVCI + TLLI */ |
| 920 | ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 921 | tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI)); |
| 922 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 923 | if (!from_bvc) |
| 924 | goto err_no_bvc; |
Daniel Willmann | d4ab1f9 | 2020-12-21 18:53:55 +0100 | [diff] [blame] | 925 | gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 926 | break; |
| 927 | default: |
| 928 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 929 | break; |
| 930 | } |
| 931 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 932 | return rc; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 933 | err_no_bvc: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 934 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci); |
| 935 | rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 936 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | /* Receive paging request from SGSN, we need to relay to proper BSS */ |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 940 | static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name, |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 941 | struct tlv_parsed *tp, uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 942 | { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 943 | struct gbproxy_config *cfg = sgsn_nse->cfg; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 944 | struct gbproxy_bvc *sgsn_bvc, *bss_bvc; |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 945 | struct gbproxy_nse *nse; |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 946 | unsigned int n_nses = 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 947 | int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN; |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 948 | int i, j; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 949 | |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 950 | /* FIXME: Handle paging logic to only page each matching NSE */ |
| 951 | |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 952 | if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 953 | uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 954 | errctr = GBPROX_GLOB_CTR_OTHER_ERR; |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 955 | sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 956 | if (!sgsn_bvc) { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 957 | 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] | 958 | pdut_name, bvci); |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 959 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 960 | return -EINVAL; |
| 961 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 962 | LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name); |
| 963 | return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci); |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 964 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 965 | errctr = GBPROX_GLOB_CTR_INV_RAI; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 966 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 967 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 968 | hash_for_each(nse->bvcs, j, bss_bvc, list) { |
| 969 | if (!memcmp(bss_bvc->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) { |
| 970 | LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n", |
| 971 | pdut_name); |
| 972 | gbprox_relay2peer(msg, bss_bvc, ns_bvci); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 973 | n_nses++; |
| 974 | /* Only send it once to each NSE */ |
| 975 | break; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 976 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 977 | } |
| 978 | } |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 979 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 980 | errctr = GBPROX_GLOB_CTR_INV_LAI; |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 981 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 982 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 983 | hash_for_each(nse->bvcs, j, bss_bvc, list) { |
| 984 | if (!memcmp(bss_bvc->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) { |
| 985 | LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n", |
| 986 | pdut_name); |
| 987 | gbprox_relay2peer(msg, bss_bvc, ns_bvci); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 988 | n_nses++; |
| 989 | /* Only send it once to each NSE */ |
| 990 | break; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 991 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 992 | } |
| 993 | } |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 994 | } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1)) { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 995 | /* iterate over all bvcs and dispatch the paging to each matching one */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 996 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 997 | hash_for_each(nse->bvcs, j, bss_bvc, list) { |
| 998 | LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name); |
| 999 | gbprox_relay2peer(msg, bss_bvc, ns_bvci); |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1000 | n_nses++; |
| 1001 | /* Only send it once to each NSE */ |
| 1002 | break; |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1003 | } |
Harald Welte | 53ee206 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 1004 | } |
| 1005 | } else { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1006 | LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n"); |
Harald Welte | 53ee206 | 2020-11-24 11:31:13 +0100 | [diff] [blame] | 1007 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 1008 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1009 | |
Daniel Willmann | 7620571 | 2020-11-30 17:08:58 +0100 | [diff] [blame] | 1010 | if (n_nses == 0) { |
Harald Welte | df690e8 | 2020-12-12 15:58:28 +0100 | [diff] [blame] | 1011 | LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n"); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1012 | rate_ctr_inc(&cfg->ctrg->ctr[errctr]); |
| 1013 | return -EINVAL; |
| 1014 | } |
Harald Welte | 3d1bd4d | 2020-11-23 15:14:20 +0100 | [diff] [blame] | 1015 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* Receive an incoming BVC-RESET message from the SGSN */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1019 | static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp, |
| 1020 | uint16_t ns_bvci) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1021 | { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1022 | uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI)); |
| 1023 | struct gbproxy_bvc *from_bvc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1024 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1025 | 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] | 1026 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1027 | if (ptp_bvci == 0) { |
| 1028 | from_bvc = gbproxy_bvc_by_bvci(nse, 0); |
| 1029 | OSMO_ASSERT(from_bvc); |
| 1030 | osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg); |
| 1031 | } else { |
| 1032 | from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci); |
| 1033 | if (!from_bvc) { |
| 1034 | LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci); |
| 1035 | rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]); |
| 1036 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1037 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1038 | 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] | 1039 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | /* Receive an incoming signalling message from the SGSN-side NS-VC */ |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1045 | 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] | 1046 | { |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1047 | 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] | 1048 | uint8_t pdu_type = bgph->pdu_type; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1049 | const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type); |
| 1050 | struct gbproxy_config *cfg = nse->cfg; |
| 1051 | struct gbproxy_bvc *sgsn_bvc; |
| 1052 | struct tlv_parsed tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1053 | int data_len; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1054 | uint16_t bvci; |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1055 | char log_pfx[32]; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1056 | int rc = 0; |
| 1057 | int cause; |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1058 | int i; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1059 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1060 | snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci); |
| 1061 | |
| 1062 | LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1063 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1064 | if (ns_bvci != 0 && ns_bvci != 1) { |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1065 | LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci); |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1066 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1069 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1070 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name); |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1071 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) { |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1075 | LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name); |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1076 | return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1079 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1080 | |
| 1081 | rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, pdu_type, bgph->data, data_len, 0, 0, |
| 1082 | DGPRS, log_pfx); |
| 1083 | if (rc < 0) { |
| 1084 | rc = tx_status_from_tlvp(rc, msg); |
Harald Welte | ec0f801 | 2020-12-06 16:32:01 +0100 | [diff] [blame] | 1085 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
| 1086 | return rc; |
| 1087 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1088 | /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */ |
| 1089 | msgb_bcid(msg) = (void *)&tp; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1090 | |
| 1091 | switch (pdu_type) { |
| 1092 | case BSSGP_PDUT_BVC_RESET: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1093 | /* resolve or create ggbproxy_bvc + handle in BVC-FSM */ |
| 1094 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1095 | rc = rx_bvc_reset_from_sgsn(nse, msg, &tp, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1096 | break; |
| 1097 | case BSSGP_PDUT_BVC_RESET_ACK: |
Daniel Willmann | 8489e7a | 2020-11-03 21:12:42 +0100 | [diff] [blame] | 1098 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1099 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1100 | if (!sgsn_bvc) |
| 1101 | goto err_no_bvc; |
| 1102 | rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg); |
| 1103 | break; |
| 1104 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 1105 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1106 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1107 | if (!sgsn_bvc) |
| 1108 | goto err_no_bvc; |
| 1109 | rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg); |
| 1110 | break; |
| 1111 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 1112 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
| 1113 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1114 | if (!sgsn_bvc) |
| 1115 | goto err_no_bvc; |
| 1116 | 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] | 1117 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1118 | case BSSGP_PDUT_FLUSH_LL: |
| 1119 | /* simple case: BVCI IE is mandatory */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1120 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1121 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1122 | if (!sgsn_bvc) |
| 1123 | goto err_no_bvc; |
| 1124 | if (sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc) |
| 1125 | rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1126 | break; |
| 1127 | case BSSGP_PDUT_PAGING_PS: |
| 1128 | case BSSGP_PDUT_PAGING_CS: |
| 1129 | /* process the paging request (LAI/RAI lookup) */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1130 | rc = gbprox_rx_paging(nse, msg, pdut_name, &tp, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1131 | break; |
| 1132 | case BSSGP_PDUT_STATUS: |
| 1133 | /* Some exception has occurred */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1134 | cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1135 | LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause, |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1136 | bssgp_cause_str(cause)); |
Harald Welte | 173a182 | 2020-12-03 15:36:59 +0100 | [diff] [blame] | 1137 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) { |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1138 | bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI)); |
Daniel Willmann | 3696dce | 2020-12-02 16:08:02 +0100 | [diff] [blame] | 1139 | LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1140 | sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci); |
| 1141 | /* don't send STATUS in response to STATUS if !bvc */ |
| 1142 | if (sgsn_bvc && sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc) |
| 1143 | rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1144 | } else |
| 1145 | LOGPC(DGPRS, LOGL_NOTICE, "\n"); |
| 1146 | break; |
| 1147 | /* those only exist in the SGSN -> BSS direction */ |
| 1148 | case BSSGP_PDUT_SUSPEND_ACK: |
| 1149 | case BSSGP_PDUT_SUSPEND_NACK: |
| 1150 | case BSSGP_PDUT_RESUME_ACK: |
| 1151 | case BSSGP_PDUT_RESUME_NACK: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1152 | /* FIXME: handle based on TLLI cache. The RA-ID is not a unique |
| 1153 | * criterion, so we have to rely on the TLLI->BVC state created |
| 1154 | * while processing the SUSPEND/RESUME in uplink */ |
| 1155 | /* FIXME: route to SGSN baed on NRI derived from TLLI */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1156 | break; |
| 1157 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 7479c4d | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 1158 | case BSSGP_PDUT_OVERLOAD: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1159 | LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name); |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1160 | /* broadcast to all BSS-side bvcs */ |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1161 | hash_for_each(cfg->bss_nses, i, nse, list) { |
Harald Welte | 7479c4d | 2020-12-02 20:06:04 +0100 | [diff] [blame] | 1162 | gbprox_relay2nse(msg, nse, 0); |
| 1163 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1164 | break; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1165 | case BSSGP_PDUT_RAN_INFO: |
| 1166 | case BSSGP_PDUT_RAN_INFO_REQ: |
| 1167 | case BSSGP_PDUT_RAN_INFO_ACK: |
| 1168 | case BSSGP_PDUT_RAN_INFO_ERROR: |
| 1169 | case BSSGP_PDUT_RAN_INFO_APP_ERROR: |
| 1170 | /* FIXME: route based in RIM Routing IE */ |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1171 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1172 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1173 | default: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1174 | LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name); |
| 1175 | rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]); |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1176 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1177 | break; |
| 1178 | } |
| 1179 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1180 | return rc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1181 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1182 | err_no_bvc: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1183 | LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC\n", pdut_name); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1184 | rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]); |
Harald Welte | dbef0aa | 2020-12-07 17:48:11 +0100 | [diff] [blame] | 1185 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1188 | |
| 1189 | /*********************************************************************** |
| 1190 | * libosmogb NS/BSSGP integration |
| 1191 | ***********************************************************************/ |
| 1192 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1193 | int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg) |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1194 | { |
| 1195 | int rc; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1196 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
| 1197 | struct gprs_ns2_inst *nsi = cfg->nsi; |
| 1198 | struct osmo_gprs_ns2_prim nsp = {}; |
| 1199 | |
| 1200 | nsp.bvci = msgb_bvci(msg); |
| 1201 | nsp.nsei = msgb_nsei(msg); |
| 1202 | |
| 1203 | osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg); |
| 1204 | rc = gprs_ns2_recv_prim(nsi, &nsp.oph); |
| 1205 | |
| 1206 | return rc; |
| 1207 | } |
| 1208 | |
| 1209 | /* Main input function for Gb proxy */ |
| 1210 | int gbprox_rcvmsg(void *ctx, struct msgb *msg) |
| 1211 | { |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1212 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1213 | uint16_t ns_bvci = msgb_bvci(msg); |
| 1214 | uint16_t nsei = msgb_nsei(msg); |
| 1215 | struct gbproxy_nse *nse; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1216 | |
Harald Welte | 278dd27 | 2020-12-06 13:35:24 +0100 | [diff] [blame] | 1217 | /* ensure minimum length to decode PCU type */ |
| 1218 | if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr)) |
| 1219 | return bssgp_tx_status(BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg); |
| 1220 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1221 | nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN); |
| 1222 | if (nse) { |
| 1223 | if (ns_bvci == 0 || ns_bvci == 1) |
| 1224 | return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1225 | else |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1226 | return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1229 | nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS); |
| 1230 | if (!nse) { |
| 1231 | LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei); |
| 1232 | nse = gbproxy_nse_alloc(cfg, nsei, false); |
| 1233 | } |
| 1234 | if (nse) { |
| 1235 | if (ns_bvci == 0 || ns_bvci == 1) |
| 1236 | return gbprox_rx_sig_from_bss(nse, msg, ns_bvci); |
| 1237 | else |
| 1238 | return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci); |
| 1239 | } |
| 1240 | |
| 1241 | return 0; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1244 | /* TODO: What about handling: |
| 1245 | * NS_AFF_CAUSE_VC_FAILURE, |
| 1246 | NS_AFF_CAUSE_VC_RECOVERY, |
| 1247 | NS_AFF_CAUSE_FAILURE, |
| 1248 | NS_AFF_CAUSE_RECOVERY, |
| 1249 | osmocom own causes |
| 1250 | NS_AFF_CAUSE_SNS_CONFIGURED, |
| 1251 | NS_AFF_CAUSE_SNS_FAILURE, |
| 1252 | */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1253 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1254 | 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] | 1255 | { |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1256 | /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, bvc->bvci, 0); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1257 | * TODO: sgsn nsei available/unavailable |
| 1258 | */ |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1259 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1260 | struct gbproxy_bvc *bvc; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1261 | struct gbproxy_nse *sgsn_nse; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1262 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1263 | switch (nsp->u.status.cause) { |
| 1264 | case NS_AFF_CAUSE_SNS_FAILURE: |
| 1265 | case NS_AFF_CAUSE_SNS_CONFIGURED: |
| 1266 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1267 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1268 | case NS_AFF_CAUSE_RECOVERY: |
| 1269 | LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei); |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1270 | sgsn_nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN); |
| 1271 | if (sgsn_nse) { |
| 1272 | uint8_t cause = BSSGP_CAUSE_OML_INTERV; |
| 1273 | bvc = gbproxy_bvc_by_bvci(sgsn_nse, 0); |
| 1274 | if (bvc) |
| 1275 | osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1276 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1277 | break; |
| 1278 | case NS_AFF_CAUSE_FAILURE: |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1279 | #if 0 |
Harald Welte | a0f7073 | 2020-12-05 17:50:23 +0100 | [diff] [blame] | 1280 | if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) { |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1281 | /* sgsn */ |
| 1282 | /* TODO: BSVC: block all PtP towards bss */ |
| 1283 | rate_ctr_inc(&cfg->ctrg-> |
| 1284 | ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]); |
| 1285 | } else { |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1286 | /* bss became unavailable |
| 1287 | * TODO: Block all BVC belonging to that NSE */ |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1288 | bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei); |
| 1289 | if (!bvc) { |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1290 | /* TODO: use primitive name + status cause name */ |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1291 | LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n", |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1292 | nsp->u.status.cause, nsp->nsei); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1293 | break; |
| 1294 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1295 | |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1296 | if (!bvc->blocked) |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1297 | break; |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1298 | hash_for_each(cfg->sgsn_nses, _sgsn, sgsn_nse, list) { |
| 1299 | bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, sgsn_nse->nsei, bvc->bvci, 0); |
| 1300 | } |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1301 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1302 | #endif |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1303 | LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei); |
| 1304 | break; |
| 1305 | default: |
Harald Welte | 95cf9fb | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 1306 | LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n", |
| 1307 | gprs_ns2_aff_cause_prim_str(nsp->u.status.cause)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1308 | break; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1309 | } |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1310 | } |
| 1311 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1312 | /* called by the ns layer */ |
| 1313 | int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 1314 | { |
| 1315 | struct osmo_gprs_ns2_prim *nsp; |
| 1316 | struct gbproxy_config *cfg = (struct gbproxy_config *) ctx; |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1317 | uintptr_t bvci; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1318 | int rc = 0; |
| 1319 | |
| 1320 | if (oph->sap != SAP_NS) |
| 1321 | return 0; |
| 1322 | |
| 1323 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 1324 | |
| 1325 | if (oph->operation != PRIM_OP_INDICATION) { |
Harald Welte | 95cf9fb | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 1326 | LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n", |
| 1327 | get_value_string(osmo_prim_op_names, oph->operation)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | switch (oph->primitive) { |
| 1332 | case PRIM_NS_UNIT_DATA: |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1333 | |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1334 | /* hand the message into the BSSGP implementation */ |
| 1335 | msgb_bssgph(oph->msg) = oph->msg->l3h; |
| 1336 | msgb_bvci(oph->msg) = nsp->bvci; |
| 1337 | msgb_nsei(oph->msg) = nsp->nsei; |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1338 | bvci = nsp->bvci | BVC_LOG_CTX_FLAG; |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1339 | |
Daniel Willmann | 8f407b1 | 2020-12-02 19:33:50 +0100 | [diff] [blame] | 1340 | log_set_context(LOG_CTX_GB_BVC, (void *)bvci); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1341 | rc = gbprox_rcvmsg(cfg, oph->msg); |
Daniel Willmann | b655010 | 2020-11-04 17:32:56 +0100 | [diff] [blame] | 1342 | msgb_free(oph->msg); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1343 | break; |
| 1344 | case PRIM_NS_STATUS: |
| 1345 | gprs_ns_prim_status_cb(cfg, nsp); |
| 1346 | break; |
| 1347 | default: |
Harald Welte | 95cf9fb | 2020-11-30 10:55:22 +0100 | [diff] [blame] | 1348 | LOGP(DPCU, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n", |
| 1349 | gprs_ns2_prim_str(oph->primitive), |
| 1350 | get_value_string(osmo_prim_op_names, oph->operation)); |
Alexander Couzens | 951e133 | 2020-09-22 13:21:46 +0200 | [diff] [blame] | 1351 | break; |
| 1352 | } |
| 1353 | |
| 1354 | return rc; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | void gbprox_reset(struct gbproxy_config *cfg) |
| 1358 | { |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1359 | struct gbproxy_nse *nse; |
| 1360 | struct hlist_node *ntmp; |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 1361 | int i, j; |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1362 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1363 | hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) { |
Harald Welte | 8b4c794 | 2020-12-05 10:14:49 +0100 | [diff] [blame] | 1364 | struct gbproxy_bvc *bvc; |
| 1365 | struct hlist_node *tmp; |
| 1366 | hash_for_each_safe(nse->bvcs, j, tmp, bvc, list) |
Harald Welte | 560bdb3 | 2020-12-04 22:24:47 +0100 | [diff] [blame] | 1367 | gbproxy_bvc_free(bvc); |
Daniel Willmann | e50550e | 2020-11-26 18:19:21 +0100 | [diff] [blame] | 1368 | |
| 1369 | gbproxy_nse_free(nse); |
| 1370 | } |
Harald Welte | e520964 | 2020-12-05 19:59:45 +0100 | [diff] [blame] | 1371 | /* FIXME: cells */ |
| 1372 | /* FIXME: SGSN side BVCs (except signaling) */ |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1373 | |
| 1374 | rate_ctr_group_free(cfg->ctrg); |
| 1375 | gbproxy_init_config(cfg); |
| 1376 | } |
| 1377 | |
| 1378 | int gbproxy_init_config(struct gbproxy_config *cfg) |
| 1379 | { |
| 1380 | struct timespec tp; |
| 1381 | |
Harald Welte | 209dc9f | 2020-12-12 19:02:16 +0100 | [diff] [blame] | 1382 | /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */ |
| 1383 | cfg->pool.bvc_fc_ratio = 100; |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 1384 | cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg); |
| 1385 | |
Harald Welte | d2fef95 | 2020-12-05 00:31:07 +0100 | [diff] [blame] | 1386 | hash_init(cfg->bss_nses); |
Daniel Willmann | 1e7be5d | 2020-12-21 18:08:21 +0100 | [diff] [blame] | 1387 | hash_init(cfg->sgsn_nses); |
| 1388 | hash_init(cfg->cells); |
Daniel Willmann | ee834af | 2020-12-14 16:22:39 +0100 | [diff] [blame] | 1389 | INIT_LLIST_HEAD(&cfg->sgsns); |
| 1390 | |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1391 | cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0); |
| 1392 | if (!cfg->ctrg) { |
| 1393 | LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n"); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | osmo_clock_gettime(CLOCK_REALTIME, &tp); |
Harald Welte | c169de4 | 2020-12-07 13:12:13 +0100 | [diff] [blame] | 1397 | osmo_fsm_log_timeouts(true); |
Pau Espin Pedrol | 1ddefb1 | 2019-08-30 19:48:34 +0200 | [diff] [blame] | 1398 | |
| 1399 | return 0; |
| 1400 | } |