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