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