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