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