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