blob: a7554a6efe3e59cf9e8b6017b31d870b2f0c4da2 [file] [log] [blame]
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001/* NS-over-IP proxy */
2
Harald Weltee5209642020-12-05 19:59:45 +01003/* (C) 2010-2020 by Harald Welte <laforge@gnumonks.org>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02004 * (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 Welted2fef952020-12-05 00:31:07 +010034#include <osmocom/core/hashtable.h>
Daniel Willmann8f407b12020-12-02 19:33:50 +010035#include <osmocom/core/logging.h>
Daniel Willmannee834af2020-12-14 16:22:39 +010036#include <osmocom/core/linuxlist.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020037#include <osmocom/core/talloc.h>
38#include <osmocom/core/select.h>
39#include <osmocom/core/rate_ctr.h>
Daniel Willmann1ac920b2021-02-11 23:51:49 +010040#include <osmocom/core/signal.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020041#include <osmocom/core/stats.h>
Daniel Willmannd4ab1f92020-12-21 18:53:55 +010042#include <osmocom/core/utils.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020043
Alexander Couzens951e1332020-09-22 13:21:46 +020044#include <osmocom/gprs/gprs_ns2.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020045#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte209dc9f2020-12-12 19:02:16 +010046#include <osmocom/gprs/gprs_bssgp2.h>
Alexander Couzens951e1332020-09-22 13:21:46 +020047#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Weltee5209642020-12-05 19:59:45 +010048#include <osmocom/gprs/bssgp_bvc_fsm.h>
Philipp Maier1c5766b2021-02-09 17:03:03 +010049#include <osmocom/gprs/protocol/gsm_08_18.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020050
Daniel Willmannd4ab1f92020-12-21 18:53:55 +010051#include <osmocom/gsm/gsm23236.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020052#include <osmocom/gsm/gsm_utils.h>
53
Oliver Smith29532c22021-01-29 11:13:00 +010054#include "debug.h"
Daniel Willmanna16ecc32021-03-10 09:57:12 +010055#include <osmocom/gbproxy/gb_proxy.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020056
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020057#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020058
59extern void *tall_sgsn_ctx;
60
61static const struct rate_ctr_desc global_ctr_description[] = {
Daniel Willmann99a46ff2021-09-27 15:39:44 +020062 [GBPROX_GLOB_CTR_INV_BVCI] = { "inv-bvci", "Invalid BVC Identifier " },
63 [GBPROX_GLOB_CTR_INV_LAI] = { "inv-lai", "Invalid Location Area Identifier" },
64 [GBPROX_GLOB_CTR_INV_RAI] = { "inv-rai", "Invalid Routing Area Identifier " },
65 [GBPROX_GLOB_CTR_INV_NSEI] = { "inv-nsei", "No BVC established for NSEI " },
66 [GBPROX_GLOB_CTR_PROTO_ERR_BSS] = { "proto-err:bss", "BSSGP protocol error (BSS )" },
67 [GBPROX_GLOB_CTR_PROTO_ERR_SGSN] = { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
68 [GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS] = { "not-supp:bss", "Feature not supported (BSS )" },
69 [GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN] = { "not-supp:sgsn", "Feature not supported (SGSN)" },
70 [GBPROX_GLOB_CTR_RESTART_RESET_SGSN] = { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
71 [GBPROX_GLOB_CTR_TX_ERR_SGSN] = { "tx-err:sgsn", "NS Transmission error (SGSN)" },
72 [GBPROX_GLOB_CTR_OTHER_ERR] = { "error", "Other error " },
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020073};
74
75static const struct rate_ctr_group_desc global_ctrg_desc = {
76 .group_name_prefix = "gbproxy:global",
77 .group_description = "GBProxy Global Statistics",
78 .num_ctr = ARRAY_SIZE(global_ctr_description),
79 .ctr_desc = global_ctr_description,
80 .class_id = OSMO_STATS_CLASS_GLOBAL,
81};
82
Daniel Willmannf8cba652021-02-12 04:59:47 +010083int tx_status(struct gbproxy_nse *nse, uint16_t ns_bvci, enum gprs_bssgp_cause cause, const uint16_t *bvci, const struct msgb *old_msg)
84{
85 int rc;
Harald Weltea0f70732020-12-05 17:50:23 +010086
Daniel Willmannf8cba652021-02-12 04:59:47 +010087 struct msgb *msg = bssgp2_enc_status(cause, bvci, old_msg, nse->max_sdu_len);
88 if (!msg) {
89 LOGPNSE(nse, LOGL_NOTICE, "Unable to encode STATUS message\n");
90 return -ENOMEM;
91 }
92
93 rc = bssgp2_nsi_tx_ptp(nse->cfg->nsi, nse->nsei, ns_bvci, msg, 0);
94 if (rc < 0)
95 LOGPNSE(nse, LOGL_NOTICE, "Unable to send STATUS message\n");
96 return rc;
97}
98
99/* generate BVC-STATUS mess
100age with cause value derived from TLV-parser error */
101static int tx_status_from_tlvp(struct gbproxy_nse *nse, enum osmo_tlv_parser_error tlv_p_err, struct msgb *orig_msg)
Harald Welteec0f8012020-12-06 16:32:01 +0100102{
103 uint8_t bssgp_cause;
104 switch (tlv_p_err) {
105 case OSMO_TLVP_ERR_MAND_IE_MISSING:
106 bssgp_cause = BSSGP_CAUSE_MISSING_MAND_IE;
107 break;
108 default:
109 bssgp_cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
110 }
Daniel Willmannf8cba652021-02-12 04:59:47 +0100111 return tx_status(nse, msgb_bvci(orig_msg), bssgp_cause, NULL, orig_msg);
Harald Welteec0f8012020-12-06 16:32:01 +0100112}
113
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200114/* strip off the NS header */
115static void strip_ns_hdr(struct msgb *msg)
116{
117 int strip_len = msgb_bssgph(msg) - msg->data;
118 msgb_pull(msg, strip_len);
119}
120
Harald Weltee5209642020-12-05 19:59:45 +0100121#if 0
Harald Welte560bdb32020-12-04 22:24:47 +0100122/* feed a message down the NS-VC associated with the specified bvc */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200123static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
124 uint16_t ns_bvci, uint16_t sgsn_nsei)
125{
126 /* create a copy of the message so the old one can
127 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200128 struct gprs_ns2_inst *nsi = cfg->nsi;
129 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200130 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
131 int rc;
132
Daniel Willmann3696dce2020-12-02 16:08:02 +0100133 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200134 msgb_nsei(msg), ns_bvci, sgsn_nsei);
135
Alexander Couzens951e1332020-09-22 13:21:46 +0200136 nsp.bvci = ns_bvci;
137 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200138
139 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200140 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
141 PRIM_OP_REQUEST, msg);
142 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200143 if (rc < 0)
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200144 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_TX_ERR_SGSN));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200145 return rc;
146}
Harald Weltee5209642020-12-05 19:59:45 +0100147#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200148
Harald Weltee30985e2021-01-28 19:13:19 +0100149/*! Determine the TLLI from the given BSSGP message.
150 * \param[in] bssgp pointer to start of BSSGP header
151 * \param[in] bssgp_len length of BSSGP message in octets
152 * \param[out] tlli TLLI (if any) in host byte order
153 * \returns 1 if TLLI found; 0 if none found; negative on parse error */
154int gprs_gb_parse_tlli(const uint8_t *bssgp, size_t bssgp_len, uint32_t *tlli)
155{
156 const struct bssgp_normal_hdr *bgph;
157 uint8_t pdu_type;
158
159 if (bssgp_len < sizeof(struct bssgp_normal_hdr))
160 return -EINVAL;
161
162 bgph = (struct bssgp_normal_hdr *)bssgp;
163 pdu_type = bgph->pdu_type;
164
165 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
166 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
167 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *)bssgp;
168 if (bssgp_len < sizeof(struct bssgp_ud_hdr))
169 return -EINVAL;
170 *tlli = osmo_load32be((const uint8_t *)&budh->tlli);
171 return 1;
172 } else {
173 const uint8_t *data = bgph->data;
174 size_t data_len = bssgp_len - sizeof(*bgph);
175 struct tlv_parsed tp;
176
177 if (bssgp_tlv_parse(&tp, data, data_len) < 0)
178 return -EINVAL;
179
180 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
181 *tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
182 return 1;
183 }
184 }
185
186 /* No TLLI present in message */
187 return 0;
188}
189
Daniel Willmann76205712020-11-30 17:08:58 +0100190/* feed a message down the NSE */
191static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100192 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200193{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100194 OSMO_ASSERT(nse);
195 OSMO_ASSERT(nse->cfg);
196
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200197 /* create a copy of the message so the old one can
198 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100199 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Daniel Willmann76205712020-11-30 17:08:58 +0100200 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Daniel Willmann44fa2012021-02-12 04:55:40 +0100201 uint32_t tlli = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200202 int rc;
203
Daniel Willmann98b1b452020-12-21 10:40:27 +0100204 DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg),
205 !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200206
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200207 /* Strip the old NS header, it will be replaced with a new one */
208 strip_ns_hdr(msg);
209
Harald Weltefe059582020-11-18 12:01:46 +0100210 /* TS 48.018 Section 5.4.2: The link selector parameter is
211 * defined in 3GPP TS 48.016. At one side of the Gb interface,
212 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
213 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
214 * underlying network service. */
Daniel Willmann44fa2012021-02-12 04:55:40 +0100215 gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli);
Harald Weltefe059582020-11-18 12:01:46 +0100216
Daniel Willmann44fa2012021-02-12 04:55:40 +0100217 rc = bssgp2_nsi_tx_ptp(nsi, nse->nsei, ns_bvci, msg, tlli);
Daniel Willmann76205712020-11-30 17:08:58 +0100218 /* FIXME: We need a counter group for gbproxy_nse */
219 //if (rc < 0)
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200220 // rate_ctr_inc(rate_ctr_group_get_ctr(bvc->ctrg, GBPROX_PEER_CTR_TX_ERR));
Daniel Willmann76205712020-11-30 17:08:58 +0100221
222 return rc;
223}
224
Harald Welte560bdb32020-12-04 22:24:47 +0100225/* feed a message down the NS-VC associated with the specified bvc */
226static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann76205712020-11-30 17:08:58 +0100227 uint16_t ns_bvci)
228{
229 int rc;
Harald Welte560bdb32020-12-04 22:24:47 +0100230 struct gbproxy_nse *nse = bvc->nse;
Daniel Willmann76205712020-11-30 17:08:58 +0100231 OSMO_ASSERT(nse);
232
233 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200234 if (rc < 0)
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200235 rate_ctr_inc(rate_ctr_group_get_ctr(bvc->ctrg, GBPROX_PEER_CTR_TX_ERR));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200236
237 return rc;
238}
239
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200240int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
241{
242 return 0;
243}
244
Harald Weltee5209642020-12-05 19:59:45 +0100245
246/***********************************************************************
247 * PTP BVC handling
248 ***********************************************************************/
249
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100250/* FIXME: Handle the tlli NULL case correctly,
251 * This function should take a generic selector
252 * and choose an sgsn based on that
253 */
254static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli)
255{
256 struct gbproxy_sgsn *sgsn = NULL;
257 struct gbproxy_sgsn *sgsn_avoid = NULL;
258
259 int tlli_type;
260 int16_t nri;
261 bool null_nri = false;
262
263 if (!tlli) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200264 sgsn = gbproxy_sgsn_by_available(cfg);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100265 if (!sgsn) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200266 LOGP(DGPRS, LOGL_ERROR, "Could not find any available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100267 return NULL;
268 }
Daniel Willmann37518b32021-05-27 18:13:36 +0200269 LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100270 return sgsn;
271 }
272
273 if (cfg->pool.nri_bitlen == 0) {
274 /* Pooling is disabled */
Daniel Willmann37518b32021-05-27 18:13:36 +0200275 sgsn = gbproxy_sgsn_by_available(cfg);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100276 if (!sgsn) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200277 LOGP(DGPRS, LOGL_ERROR, "Could not find any available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100278 return NULL;
279 }
280
Daniel Willmann33dcdad2022-10-10 16:29:52 +0200281 LOGPSGSN(sgsn, LOGL_DEBUG, "Pooling disabled, using first available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100282 } else {
283 /* Pooling is enabled, try to use the NRI for routing to an SGSN
284 * See 3GPP TS 23.236 Ch. 5.3.2 */
285 tlli_type = gprs_tlli_type(*tlli);
286 if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) {
287 /* Only get/use the NRI if tlli type is local */
288 osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen);
289 if (nri >= 0) {
290 /* Get the SGSN for the NRI */
291 sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri);
292 if (sgsn && !null_nri)
293 return sgsn;
294 /* If the NRI is the null NRI, we need to avoid the chosen SGSN */
295 if (null_nri && sgsn) {
296 sgsn_avoid = sgsn;
297 }
298 } else {
299 /* We couldn't get the NRI from the TLLI */
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100300 LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %08x\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100301 }
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100302 } else {
303 LOGP(DGPRS, LOGL_INFO, "TLLI %08x is neither local nor foreign, not routing by NRI\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100304 }
305 }
306
307 /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid
308 * NOTE: This function is not stable if the number of SGSNs or allow_attach changes
309 * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that
310 * we can just wait for the MS to reattempt the procedure.
311 */
312 if (!sgsn)
313 sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli);
314
315 if (!sgsn) {
316 LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli);
317 return NULL;
318 }
319
320 return sgsn;
321}
322
323/*! Find the correct gbproxy_bvc given a cell and an SGSN
324 * \param[in] cfg The gbproxy configuration
325 * \param[in] cell The cell the message belongs to
326 * \param[in] tlli An optional TLLI used for tracking
327 * \return Returns 0 on success, otherwise a negative value
328 */
329static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli)
330{
331 struct gbproxy_sgsn *sgsn;
332 struct gbproxy_bvc *sgsn_bvc = NULL;
Harald Welte02d7c482020-12-30 12:13:36 +0100333 int i;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100334
335 sgsn = gbproxy_select_sgsn(cfg, tlli);
336 if (!sgsn) {
337 LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
338 return NULL;
339 }
340
341 /* Get the BVC for this SGSN/NSE */
Harald Welte02d7c482020-12-30 12:13:36 +0100342 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100343 sgsn_bvc = cell->sgsn_bvc[i];
344 if (!sgsn_bvc)
345 continue;
346 if (sgsn->nse != sgsn_bvc->nse)
347 continue;
Daniel Willmann5ceeb2b2022-02-23 18:00:48 +0100348 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi))
349 continue;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100350
351 return sgsn_bvc;
352 }
353
354 /* This shouldn't happen */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100355 LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN %s, dropping message!\n", sgsn->name);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100356 return NULL;
357}
358
359/*! Send a message to the next SGSN, possibly ignoring the null SGSN
360 * route an uplink message on a PTP-BVC to a SGSN using the TLLI
361 * \param[in] cell The cell the message belongs to
362 * \param[in] msg The BSSGP message
363 * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI)
364 * \param[in] tlli An optional TLLI used for tracking
365 * \return Returns 0 on success, otherwise a negative value
366 */
367static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli,
Harald Weltee5209642020-12-05 19:59:45 +0100368 bool sig_bvci)
369{
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100370 struct gbproxy_config *cfg = cell->cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100371 struct gbproxy_bvc *sgsn_bvc;
Harald Weltee5209642020-12-05 19:59:45 +0100372
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100373 sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli);
374 if (!sgsn_bvc) {
375 LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli);
376 return -EINVAL;
Harald Weltee5209642020-12-05 19:59:45 +0100377 }
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100378
379 return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);
Harald Weltee5209642020-12-05 19:59:45 +0100380}
381
Daniel Willmann22311802021-11-02 11:54:27 +0100382static int gbproxy_decode_bssgp(const struct bssgp_normal_hdr *bgph, int msg_len, struct tlv_parsed *tp, const char *log_pfx)
383{
384 int rc;
385
386 /* UNITDATA PDUs have a different header than the other PDUs */
387 if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA || bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) {
388 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) bgph;
389 if (msg_len < sizeof(*budh))
Daniel Willmannf689a8a2021-11-18 15:27:37 +0100390 return OSMO_TLVP_ERR_MAND_IE_MISSING;
Daniel Willmann22311802021-11-02 11:54:27 +0100391 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, 1, budh->pdu_type, budh->data,
392 msg_len - sizeof(*budh), 0, 0, DGPRS, log_pfx);
393 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
394 * doesn't have to worry where the TLLI came from */
395 tp->lv[BSSGP_IE_TLLI].len = 4;
396 tp->lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
397 } else {
398 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, 1, bgph->pdu_type, bgph->data,
399 msg_len - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
400 }
401
402 return rc;
403}
404
Daniel Willmann5a148372021-10-29 18:28:13 +0200405static int gbproxy_tlli_from_status_pdu(struct tlv_parsed *tp, uint32_t *tlli, char *log_pfx);
406
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200407/* Receive an incoming PTP message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100408static int gbprox_rx_ptp_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200409{
Harald Welte278dd272020-12-06 13:35:24 +0100410 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100411 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
412 struct gbproxy_bvc *bss_bvc;
413 struct tlv_parsed tp;
414 char log_pfx[32];
415 uint32_t tlli;
416 int rc;
417
418 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
419
420 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200421
Daniel Willmann06331ac2020-12-10 17:59:46 +0100422 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100423 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100424 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100425 }
426
427 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100428 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100429 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100430 }
431
432 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100433 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100434 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100435 }
436
Harald Weltee5209642020-12-05 19:59:45 +0100437 bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
438 if (!bss_bvc) {
439 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n",
440 log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100441 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200442 }
443
Daniel Willmann22311802021-11-02 11:54:27 +0100444 rc = gbproxy_decode_bssgp(bgph, msgb_bssgp_len(msg), &tp, log_pfx);
Harald Weltee5209642020-12-05 19:59:45 +0100445 if (rc < 0) {
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200446 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100447 return tx_status_from_tlvp(nse, rc, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100448 }
Harald Welte85a40272020-12-08 21:43:22 +0100449 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
450 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200451
Harald Weltee5209642020-12-05 19:59:45 +0100452 switch (bgph->pdu_type) {
453 case BSSGP_PDUT_UL_UNITDATA:
454 case BSSGP_PDUT_RA_CAPA_UPDATE:
455 case BSSGP_PDUT_FLOW_CONTROL_MS:
456 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
457 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
458 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
459 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
460 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
461 case BSSGP_PDUT_FLOW_CONTROL_PFC:
462 case BSSGP_PDUT_DELETE_BSS_PFC_REQ:
463 case BSSGP_PDUT_PS_HO_REQUIRED:
464 case BSSGP_PDUT_PS_HO_REQUEST_ACK:
465 case BSSGP_PDUT_PS_HO_REQUEST_NACK:
466 case BSSGP_PDUT_PS_HO_COMPLETE:
467 case BSSGP_PDUT_PS_HO_CANCEL:
468 /* We can route based on TLLI-NRI */
469 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100470 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100471 break;
472 case BSSGP_PDUT_RADIO_STATUS:
473 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
474 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100475 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100476 } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {
477 /* we treat the TMSI like a TLLI and extract the NRI from it */
478 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Daniel Willmann8b3ed292021-01-21 18:46:51 +0100479 /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */
480 tlli = gprs_tmsi2tlli(tlli, TLLI_FOREIGN);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100481 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100482 } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {
Daniel Willmann5193f222021-01-11 05:00:46 +0100483 /* FIXME: Use the IMSI as selector? */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100484 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false);
Harald Weltee5209642020-12-05 19:59:45 +0100485 } else
486 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");
487 break;
488 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
489 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +0100490 {
491 /* Route according to IMSI<->NSE cache entry */
492 struct osmo_mobile_identity mi;
493 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
494 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
495 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +0200496 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann5614e572021-01-18 18:38:27 +0100497 if (nse) {
498 OSMO_ASSERT(nse->sgsn_facing);
499 rc = gbprox_relay2nse(msg, nse, ns_bvci);
500 } else {
Daniel Willmann82669182021-01-19 11:37:55 +0100501 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx unmatched %s with IMSI %s\n", pdut_name, mi.imsi);
Daniel Willmann5614e572021-01-18 18:38:27 +0100502 }
Harald Weltee5209642020-12-05 19:59:45 +0100503 break;
Daniel Willmann5614e572021-01-18 18:38:27 +0100504 }
Harald Weltee5209642020-12-05 19:59:45 +0100505 case BSSGP_PDUT_FLOW_CONTROL_BVC:
Harald Welte85a40272020-12-08 21:43:22 +0100506 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100507 break;
508 case BSSGP_PDUT_STATUS:
Daniel Willmann5a148372021-10-29 18:28:13 +0200509 {
510 struct gbproxy_sgsn *sgsn;
511 /* Check if the status needs to be terminated locally */
512 uint8_t cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
513
514 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s)\n", cause,
515 bssgp_cause_str(cause));
516
517 if (gbproxy_tlli_from_status_pdu(&tp, &tlli, log_pfx) == 0)
518 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
519 else
520 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
521
522 if (!sgsn) {
523 rc = -EINVAL;
Harald Weltee5209642020-12-05 19:59:45 +0100524 break;
Daniel Willmann5a148372021-10-29 18:28:13 +0200525 }
526
527 rc = gbprox_relay2nse(msg, sgsn->nse, ns_bvci);
Harald Weltee5209642020-12-05 19:59:45 +0100528 break;
529 }
Daniel Willmann5a148372021-10-29 18:28:13 +0200530 }
Harald Weltee5209642020-12-05 19:59:45 +0100531
Daniel Willmannd71aa6b2022-02-28 18:19:27 +0100532 if (rc < 0)
533 rate_ctr_inc(rate_ctr_group_get_ctr(bss_bvc->ctrg, GBPROX_PEER_CTR_FWD_FROM_BSS_ERR));
534
Harald Weltee5209642020-12-05 19:59:45 +0100535 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200536}
537
538/* Receive an incoming PTP message from a SGSN-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100539static int gbprox_rx_ptp_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200540{
Harald Welte278dd272020-12-06 13:35:24 +0100541 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100542 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
543 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte85a40272020-12-08 21:43:22 +0100544 struct tlv_parsed tp;
Harald Weltee5209642020-12-05 19:59:45 +0100545 char log_pfx[32];
Harald Welte85a40272020-12-08 21:43:22 +0100546 int rc;
Harald Weltee5209642020-12-05 19:59:45 +0100547
548 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
549
550 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200551
Daniel Willmann06331ac2020-12-10 17:59:46 +0100552 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100553 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100554 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100555 }
556
557 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100558 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100559 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100560 }
561
562 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100563 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100564 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100565 }
566
Harald Weltee5209642020-12-05 19:59:45 +0100567 sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
568 if (!sgsn_bvc) {
569 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n",
570 log_pfx, pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200571 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100572 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200573 }
574
Harald Weltee5209642020-12-05 19:59:45 +0100575 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) {
576 LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200577 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn_bvc->ctrg, GBPROX_PEER_CTR_DROPPED));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100578 return tx_status(nse, ns_bvci, BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200579 }
Harald Welte85a40272020-12-08 21:43:22 +0100580
Daniel Willmann22311802021-11-02 11:54:27 +0100581 rc = gbproxy_decode_bssgp(bgph, msgb_bssgp_len(msg), &tp, log_pfx);
Harald Welte85a40272020-12-08 21:43:22 +0100582 if (rc < 0) {
Daniel Willmanncfb6f172021-11-02 11:55:06 +0100583 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100584 return tx_status_from_tlvp(nse, rc, msg);
Harald Welte85a40272020-12-08 21:43:22 +0100585 }
586 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
587 msgb_bcid(msg) = (void *)&tp;
588
Harald Weltee5209642020-12-05 19:59:45 +0100589 OSMO_ASSERT(sgsn_bvc->cell);
590 bss_bvc = sgsn_bvc->cell->bss_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200591
Harald Welte85a40272020-12-08 21:43:22 +0100592 switch (bgph->pdu_type) {
593 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
594 return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg);
Daniel Willmann5614e572021-01-18 18:38:27 +0100595 case BSSGP_PDUT_DUMMY_PAGING_PS:
596 case BSSGP_PDUT_PAGING_PS:
597 {
598 /* Cache the IMSI<->NSE to route PAGING REJECT */
599 struct osmo_mobile_identity mi;
600 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
601 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
602 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +0200603 gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann5614e572021-01-18 18:38:27 +0100604 break;
Harald Welte85a40272020-12-08 21:43:22 +0100605 }
Daniel Willmann5614e572021-01-18 18:38:27 +0100606 default:
607 break;
608 }
Daniel Willmannd71aa6b2022-02-28 18:19:27 +0100609 rc = gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci);
Harald Welte85a40272020-12-08 21:43:22 +0100610
Daniel Willmannd71aa6b2022-02-28 18:19:27 +0100611 if (rc < 0)
612 rate_ctr_inc(rate_ctr_group_get_ctr(bss_bvc->ctrg, GBPROX_PEER_CTR_FWD_FROM_SGSN_ERR));
613
614 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200615}
616
Harald Weltee5209642020-12-05 19:59:45 +0100617/***********************************************************************
618 * BVC FSM call-backs
619 ***********************************************************************/
Harald Welte7df1e5a2020-12-02 22:53:26 +0100620
Harald Weltee5209642020-12-05 19:59:45 +0100621/* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */
622static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv)
623{
624 unsigned int i;
625
626 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
627 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i];
628 if (!sgsn_bvc)
629 continue;
630 osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv);
631 }
632}
633
634/* BVC FSM informs us about a BSS-side reset of the signaling BVC */
635static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
636 uint16_t cell_id, uint8_t cause, void *priv)
637{
638 struct gbproxy_bvc *sig_bvc = priv;
639 struct gbproxy_nse *nse = sig_bvc->nse;
640 struct gbproxy_bvc *ptp_bvc;
641 unsigned int i;
642
643 /* BLOCK all SGSN-side PTP BVC within this NSE */
644 hash_for_each(nse->bvcs, i, ptp_bvc, list) {
645 if (ptp_bvc == sig_bvc)
646 continue;
647 OSMO_ASSERT(ptp_bvc->cell);
648
649 dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Harald Weltef9e149b2020-12-02 23:29:38 +0100650 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100651
Harald Weltee5209642020-12-05 19:59:45 +0100652 /* Delete all BSS-side PTP BVC within this NSE */
653 gbproxy_cleanup_bvcs(nse, 0);
654
655 /* TODO: we keep the "CELL" around for now, re-connecting it to
656 * any (later) new PTP-BVC for that BVCI. Not sure if that's the
657 * best idea ? */
658}
659
660/* forward declaration */
661static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops;
662
663static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = {
664 .reset_notification = bss_sig_bvc_reset_notif,
665};
666
667/* BVC FSM informs us about a BSS-side reset of a PTP BVC */
668static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
669 uint16_t cell_id, uint8_t cause, void *priv)
670{
671 struct gbproxy_bvc *bvc = priv;
672 struct gbproxy_config *cfg = bvc->nse->cfg;
Harald Welte664c24e2020-12-12 15:01:17 +0100673 struct gbproxy_nse *sgsn_nse;
Harald Weltee5209642020-12-05 19:59:45 +0100674 unsigned int i;
675
676 OSMO_ASSERT(bvci != 0);
677
678 if (!bvc->cell) {
679 /* see if we have a CELL dangling around */
680 bvc->cell = gbproxy_cell_by_bvci(cfg, bvci);
681 if (bvc->cell) {
682 /* the CELL already exists. This means either it * was created before at an
683 * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a
684 * malconfiguration */
685 if (bvc->cell->bss_bvc) {
686 LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already "
687 "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei);
688 LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting "
689 "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei);
690 gbproxy_bvc_free(bvc->cell->bss_bvc);
Daniel Willmann28ec0e32021-12-06 16:47:22 +0100691 bvc->cell = NULL;
692 } else {
693 LOGPBVC(bvc, LOGL_ERROR, "Found cell without BSS BVC, this should not happen!");
Harald Weltee5209642020-12-05 19:59:45 +0100694 }
Harald Weltee5209642020-12-05 19:59:45 +0100695 }
696 }
697
698 if (!bvc->cell) {
Harald Weltee5209642020-12-05 19:59:45 +0100699 /* if we end up here, it means this is the first time we received a BVC-RESET
700 * for this BVC. We need to create the 'cell' data structure and the SGSN-side
701 * BVC counterparts */
702
Philipp Maiere4597ec2021-02-09 16:02:00 +0100703 bvc->cell = gbproxy_cell_alloc(cfg, bvci, ra_id, cell_id);
Harald Weltee5209642020-12-05 19:59:45 +0100704 OSMO_ASSERT(bvc->cell);
705
706 /* link us to the cell and vice-versa */
707 bvc->cell->bss_bvc = bvc;
Harald Welte664c24e2020-12-12 15:01:17 +0100708 }
Harald Weltee5209642020-12-05 19:59:45 +0100709
Daniel Willmann6701d272021-04-08 08:39:12 +0200710 /* Ensure we have the correct RA/CELL ID */
711 if (!gsm48_ra_equal(&bvc->cell->id.raid, ra_id)) {
712 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));
713 memcpy(&bvc->cell->id.raid, ra_id, sizeof(*ra_id));
714 }
715 if (bvc->cell->id.cid != cell_id) {
716 LOGPBVC(bvc, LOGL_NOTICE, "CellID changed from %05d to %05d, updating cell\n", bvc->cell->id.cid, cell_id);
717 bvc->cell->id.cid = cell_id;
718 }
719
720 /* Reallocate SGSN-side BVCs of the cell, and reset them
Daniel Willmannf9902c52021-11-12 15:52:03 +0100721 * Removing and reallocating is needed because the ra_id/cell_id might have changed */
Harald Welte664c24e2020-12-12 15:01:17 +0100722 hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) {
723 struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Daniel Willmanndc763fd2021-09-24 16:45:38 +0200724 if (!sgsn_bvc)
725 sgsn_bvc = gbproxy_bvc_by_bvci_inactive(sgsn_nse, bvci);
Harald Welte664c24e2020-12-12 15:01:17 +0100726 if (sgsn_bvc)
Daniel Willmann6701d272021-04-08 08:39:12 +0200727 gbproxy_bvc_free(sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100728
Daniel Willmann6701d272021-04-08 08:39:12 +0200729 sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci);
730 OSMO_ASSERT(sgsn_bvc);
731 sgsn_bvc->cell = bvc->cell;
Daniel Willmann6701d272021-04-08 08:39:12 +0200732 sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei,
733 bvci, ra_id, cell_id);
734 OSMO_ASSERT(sgsn_bvc->fi);
735 bssgp_bvc_fsm_set_max_pdu_len(sgsn_bvc->fi, sgsn_nse->max_sdu_len);
736 bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc);
737 gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100738 }
739
740 /* Trigger outbound BVC-RESET procedure toward each SGSN */
741 dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause);
742}
743
744/* BVC FSM informs us about a BSS-side FSM state change */
745static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv)
746{
747 struct gbproxy_bvc *bvc = priv;
748 struct gbproxy_cell *cell = bvc->cell;
749 uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi);
750
751 /* we have just been created but due to callback ordering the cell is not associated */
752 if (!cell)
753 return;
754
755 switch (state) {
756 case BSSGP_BVCFSM_S_BLOCKED:
757 /* block the corresponding SGSN-side PTP BVCs */
758 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
759 break;
760 case BSSGP_BVCFSM_S_UNBLOCKED:
761 /* unblock the corresponding SGSN-side PTP BVCs */
762 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL);
763 break;
764 }
765}
766
Harald Welte85a40272020-12-08 21:43:22 +0100767/* BVC FSM informs us about BVC-FC PDU receive */
768static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
769{
Harald Welte209dc9f2020-12-12 19:02:16 +0100770 struct bssgp2_flow_ctrl fc_reduced;
Harald Welte85a40272020-12-08 21:43:22 +0100771 struct gbproxy_bvc *bss_bvc = priv;
Harald Welte209dc9f2020-12-12 19:02:16 +0100772 struct gbproxy_cell *cell;
773 struct gbproxy_config *cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100774
Harald Welte209dc9f2020-12-12 19:02:16 +0100775 OSMO_ASSERT(bss_bvc);
776 OSMO_ASSERT(fc);
777
778 cell = bss_bvc->cell;
Harald Welte85a40272020-12-08 21:43:22 +0100779 if (!cell)
780 return;
781
Harald Welte209dc9f2020-12-12 19:02:16 +0100782 cfg = cell->cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100783
Harald Welte209dc9f2020-12-12 19:02:16 +0100784 /* reduce / scale according to configuration to make sure we only advertise a fraction
785 * of the capacity to each of the SGSNs in the pool */
786 fc_reduced = *fc;
787 fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
788 fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
789 /* we don't modify the per-MS related values as any single MS is only served by one SGSN */
790
791 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
Harald Welte85a40272020-12-08 21:43:22 +0100792}
793
Harald Weltee5209642020-12-05 19:59:45 +0100794static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
795 .reset_notification = bss_ptp_bvc_reset_notif,
796 .state_chg_notification = bss_ptp_bvc_state_chg_notif,
Harald Welte85a40272020-12-08 21:43:22 +0100797 .rx_fc_bvc = bss_ptp_bvc_fc_bvc,
Harald Weltee5209642020-12-05 19:59:45 +0100798};
799
800/* BVC FSM informs us about a SGSN-side reset of a PTP BVC */
801static void sgsn_ptp_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
806 if (!bvc->cell) {
807 LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n");
808 return;
809 }
810
811 OSMO_ASSERT(bvc->cell->bss_bvc);
812
813 /* request reset of BSS-facing PTP-BVC */
814 osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
815}
816
817static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = {
818 .reset_notification = sgsn_ptp_bvc_reset_notif,
819};
820
821/* BVC FSM informs us about a SGSN-side reset of the signaling BVC */
822static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
823 uint16_t cell_id, uint8_t cause, void *priv)
824{
825 struct gbproxy_bvc *bvc = priv;
826 struct gbproxy_config *cfg = bvc->nse->cfg;
827 struct gbproxy_nse *bss_nse;
828 unsigned int i;
829
830 /* delete all SGSN-side PTP BVC for this SGSN */
831 gbproxy_cleanup_bvcs(bvc->nse, 0);
832 /* FIXME: what to do about the cells? */
833 /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */
834
835 /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */
836 hash_for_each(cfg->bss_nses, i, bss_nse, list) {
837 struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0);
838 if (!bss_bvc) {
839 LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n");
840 continue;
841 }
842 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
843 }
844}
845
846const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = {
847 .reset_notification = sgsn_sig_bvc_reset_notif,
Daniel Willmann2f30a0c2022-03-30 11:31:48 +0200848 .reset_ack_notification = sgsn_sig_bvc_reset_notif,
Harald Weltee5209642020-12-05 19:59:45 +0100849};
850
851/***********************************************************************
852 * Signaling BVC handling
853 ***********************************************************************/
854
855/* process a BVC-RESET message from the BSS side */
856static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp)
857{
858 struct gbproxy_bvc *from_bvc = NULL;
859 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
860 uint32_t features = 0; // FIXME: make configurable
861
862 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci);
863
Harald Welte314647b2020-12-02 23:03:22 +0100864 if (bvci == 0) {
865 /* If we receive a BVC reset on the signalling endpoint, we
866 * don't want the SGSN to reset, as the signalling endpoint
867 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte324f0652020-12-02 23:06:37 +0100868
Harald Weltee5209642020-12-05 19:59:45 +0100869 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
Harald Welte560bdb32020-12-04 22:24:47 +0100870 if (!from_bvc) {
Harald Weltee5209642020-12-05 19:59:45 +0100871 from_bvc = gbproxy_bvc_alloc(nse, 0);
872 OSMO_ASSERT(from_bvc);
873 from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features);
874 if (!from_bvc->fi) {
875 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
876 gbproxy_bvc_free(from_bvc);
877 return -ENOMEM;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100878 }
Daniel Willmanna8b61652021-02-12 05:05:14 +0100879 bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len);
Harald Weltee5209642020-12-05 19:59:45 +0100880 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc);
881 }
882 } else {
883 from_bvc = gbproxy_bvc_by_bvci(nse, bvci);
884 if (!from_bvc) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100885 /* if a PTP-BVC is reset, and we don't know that
Harald Welte560bdb32020-12-04 22:24:47 +0100886 * PTP-BVCI yet, we should allocate a new bvc */
887 from_bvc = gbproxy_bvc_alloc(nse, bvci);
888 OSMO_ASSERT(from_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100889 from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi,
890 nse->nsei, bvci);
891 if (!from_bvc->fi) {
892 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
893 gbproxy_bvc_free(from_bvc);
894 return -ENOMEM;
895 }
Daniel Willmanna8b61652021-02-12 05:05:14 +0100896 bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len);
Harald Weltee5209642020-12-05 19:59:45 +0100897 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100898 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100899 }
Harald Weltee5209642020-12-05 19:59:45 +0100900 /* hand into FSM for further processing */
901 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
902 return 0;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100903}
904
Philipp Maier1c5766b2021-02-09 17:03:03 +0100905/* Receive an incoming RIM message from a BSS-side NS-VC */
906static int gbprox_rx_rim_from_bss(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
907 const char *pdut_name)
908{
909 struct gbproxy_sgsn *sgsn;
910 struct gbproxy_cell *dest_cell;
911 struct gbproxy_cell *src_cell;
912 struct bssgp_rim_routing_info dest_ri;
913 struct bssgp_rim_routing_info src_ri;
914 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +0100915 char ri_src_str[64];
916 char ri_dest_str[64];
Daniel Willmannf8cba652021-02-12 04:59:47 +0100917 uint16_t ns_bvci = msgb_bvci(msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100918
919 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
920 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
921 if (rc < 0) {
922 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100923 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100924 }
925 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
926 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
927 if (rc < 0) {
928 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100929 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100930 }
931
932 /* Since gbproxy is 2G only we do not expect to get RIM messages only from GERAN cells. */
933 if (src_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
934 LOGP(DGPRS, LOGL_ERROR, "%s %s source RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
935 bssgp_rim_ri_name(&src_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100936 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100937 }
938
939 /* Lookup source cell to make sure that the source RIM routing information actually belongs
940 * to a valid cell that we know */
941 src_cell = gbproxy_cell_by_cellid(nse->cfg, &src_ri.geran.raid, src_ri.geran.cid);
942 if (!src_cell) {
943 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for source RIM routing info (%s)\n", log_pfx,
944 pdut_name, bssgp_rim_ri_name(&src_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100945 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100946 }
947
948 /* TODO: Use bssgp_bvc_get_features_negotiated(src_cell->bss_bvc->fi) to check if the the BSS sided BVC actually
949 * did negotiate RIM support. If not we should respond with a BSSGP STATUS message. The cause code should be
950 * BSSGP_CAUSE_PDU_INCOMP_FEAT. */
951
952 /* If Destination is known by gbproxy, route directly */
953 if (dest_ri.discr == BSSGP_RIM_ROUTING_INFO_GERAN) {
954 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
955 if (dest_cell) {
956 /* TODO: Also check if dest_cell->bss_bvc is RIM-capable (see also above). If not we should
957 * respond with a BSSGP STATUS message as well because it also would make no sense to try
958 * routing the RIM message to the next RIM-capable SGSN. */
Philipp Maier4499cf42021-02-10 17:54:44 +0100959 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to peer (nsei=%u) RIM-PDU: src=%s, dest=%s\n",
960 log_pfx, pdut_name, dest_cell->bss_bvc->nse->nsei,
961 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
962 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100963 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
964 }
965 }
966
967 /* Otherwise pass on to a RIM-capable SGSN */
968 /* TODO: We need to extend gbproxy_select_sgsn() so that it selects a RIM-capable SGSN, at the moment we just
969 * get any SGSN and just assume that it is RIM-capable. */
970 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
971 if (!sgsn) {
972 LOGP(DGPRS, LOGL_NOTICE,
973 "%s %s cannot route RIM message (%s to %s) since no RIM capable SGSN is found!\n", log_pfx,
974 pdut_name, bssgp_rim_ri_name(&src_ri), bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100975 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100976 }
Philipp Maier4499cf42021-02-10 17:54:44 +0100977 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
978 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
979 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
980 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100981
982 return gbprox_relay2nse(msg, sgsn->nse, 0);
983}
984
Daniel Willmann6ec5f952021-10-28 16:13:03 +0200985/* Extract the TLLI from the PDU-in-error of the STATUS PDU (if available) */
986static int gbproxy_tlli_from_status_pdu(struct tlv_parsed *tp, uint32_t *tlli, char *log_pfx)
Daniel Willmann7d37cbb2021-09-29 11:51:51 +0200987{
988 int rc;
989 int pdu_len = TLVP_LEN(&tp[0], BSSGP_IE_PDU_IN_ERROR);
990 const uint8_t *pdu_data = TLVP_VAL(&tp[0], BSSGP_IE_PDU_IN_ERROR);
991 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *)pdu_data;
Daniel Willmann01416282021-11-02 11:55:57 +0100992 struct tlv_parsed tp_inner;
Daniel Willmann7d37cbb2021-09-29 11:51:51 +0200993
Daniel Willmann01416282021-11-02 11:55:57 +0100994 rc = gbproxy_decode_bssgp(bgph, pdu_len, &tp_inner, log_pfx);
Daniel Willmannaf2e9b32021-11-18 15:27:49 +0100995 /* Ignore decode failure due to truncated message */
996 if (rc < 0 && rc != OSMO_TLVP_ERR_OFS_BEYOND_BUFFER)
Daniel Willmann7d37cbb2021-09-29 11:51:51 +0200997 return rc;
998
Daniel Willmann01416282021-11-02 11:55:57 +0100999 if (TLVP_PRESENT(&tp_inner, BSSGP_IE_TLLI)) {
1000 *tlli = osmo_load32be(TLVP_VAL(&tp_inner, BSSGP_IE_TLLI));
1001 } else if (TLVP_PRESENT(&tp_inner, BSSGP_IE_TMSI)) {
Daniel Willmanncbfc7cf2021-09-29 11:51:56 +02001002 /* we treat the TMSI like a TLLI and extract the NRI from it */
Daniel Willmann01416282021-11-02 11:55:57 +01001003 *tlli = osmo_load32be(TLVP_VAL(&tp_inner, BSSGP_IE_TMSI));
Daniel Willmanncbfc7cf2021-09-29 11:51:56 +02001004 /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */
1005 *tlli = gprs_tmsi2tlli(*tlli, TLLI_FOREIGN);
1006 } else {
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001007 return -ENOENT;
Daniel Willmanncbfc7cf2021-09-29 11:51:56 +02001008 }
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001009
1010 return 0;
1011}
1012
Daniel Willmann3d8f5992021-10-27 16:05:37 +02001013/* Extract the BVCI from the PDU-in-error of the STATUS PDU (if available) */
1014static int gbproxy_bvci_from_status_pdu(struct tlv_parsed *tp, uint16_t *bvci, char *log_pfx)
1015{
1016 int rc;
1017 int pdu_len = TLVP_LEN(&tp[0], BSSGP_IE_PDU_IN_ERROR);
1018 const uint8_t *pdu_data = TLVP_VAL(&tp[0], BSSGP_IE_PDU_IN_ERROR);
1019 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *)pdu_data;
Daniel Willmann01416282021-11-02 11:55:57 +01001020 struct tlv_parsed tp_inner;
Daniel Willmann3d8f5992021-10-27 16:05:37 +02001021
Daniel Willmann01416282021-11-02 11:55:57 +01001022 rc = gbproxy_decode_bssgp(bgph, pdu_len, &tp_inner, log_pfx);
Daniel Willmannaf2e9b32021-11-18 15:27:49 +01001023 /* Ignore decode failure due to truncated message */
1024 if (rc < 0 && rc != OSMO_TLVP_ERR_OFS_BEYOND_BUFFER)
Daniel Willmann3d8f5992021-10-27 16:05:37 +02001025 return rc;
1026
Daniel Willmann01416282021-11-02 11:55:57 +01001027 if (TLVP_PRESENT(&tp_inner, BSSGP_IE_BVCI))
1028 *bvci = ntohs(tlvp_val16_unal(&tp_inner, BSSGP_IE_BVCI));
Daniel Willmann3d8f5992021-10-27 16:05:37 +02001029 else
1030 return -ENOENT;
1031
1032 return 0;
1033}
1034
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001035/* Receive an incoming signalling message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +01001036static int gbprox_rx_sig_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001037{
1038 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001039 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +01001040 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
Philipp Maier74882dc2021-02-04 16:31:46 +01001041 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001042 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte560bdb32020-12-04 22:24:47 +01001043 struct gbproxy_bvc *from_bvc = NULL;
Harald Welteec0f8012020-12-06 16:32:01 +01001044 char log_pfx[32];
Harald Weltee5209642020-12-05 19:59:45 +01001045 uint16_t ptp_bvci;
1046 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001047 int rc;
1048
Harald Weltee5209642020-12-05 19:59:45 +01001049 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
1050
1051 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001052
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001053 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Weltee5209642020-12-05 19:59:45 +01001054 LOGP(DGPRS, LOGL_NOTICE, "%s %s BVCI=%05u is not signalling\n", log_pfx, pdut_name, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001055 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001056 }
1057
Harald Welte278dd272020-12-06 13:35:24 +01001058 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001059 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001060 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001061 }
1062
1063 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001064 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001065 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001066 }
1067
Philipp Maier74882dc2021-02-04 16:31:46 +01001068 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, ARRAY_SIZE(tp), pdu_type, bgph->data, data_len, 0, 0,
Harald Welteec0f8012020-12-06 16:32:01 +01001069 DGPRS, log_pfx);
1070 if (rc < 0) {
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001071 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001072 return tx_status_from_tlvp(nse, rc, msg);
Harald Welteec0f8012020-12-06 16:32:01 +01001073 }
Harald Weltee5209642020-12-05 19:59:45 +01001074 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001075 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001076
Harald Weltee5209642020-12-05 19:59:45 +01001077 /* special case handling for some PDU types */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001078 switch (pdu_type) {
Harald Weltee5209642020-12-05 19:59:45 +01001079 case BSSGP_PDUT_BVC_RESET:
1080 /* resolve or create gbproxy_bvc + handlei n BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001081 return rx_bvc_reset_from_bss(nse, msg, &tp[0]);
Harald Weltee5209642020-12-05 19:59:45 +01001082 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001083 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001084 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
Harald Welte560bdb32020-12-04 22:24:47 +01001085 if (!from_bvc)
1086 goto err_no_bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001087 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1088 case BSSGP_PDUT_BVC_BLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001089 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001090 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1091 if (!from_bvc)
1092 goto err_no_bvc;
1093 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg);
1094 case BSSGP_PDUT_BVC_UNBLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001095 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001096 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1097 if (!from_bvc)
1098 goto err_no_bvc;
1099 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg);
1100 case BSSGP_PDUT_SUSPEND:
1101 case BSSGP_PDUT_RESUME:
Daniel Willmann77493b12020-12-29 21:13:31 +01001102 {
1103 struct gbproxy_sgsn *sgsn;
1104
Philipp Maier74882dc2021-02-04 16:31:46 +01001105 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001106 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
1107 if (!sgsn) {
1108 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN for TLLI, dropping message!\n");
1109 rc = -EINVAL;
1110 break;
1111 }
1112
1113 gbproxy_tlli_cache_update(nse, tlli);
1114
1115 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
Harald Weltee5209642020-12-05 19:59:45 +01001116#if 0
1117 /* TODO: Validate the RAI for consistency with the RAI
1118 * we expect for any of the BVC within this BSS side NSE */
Philipp Maier74882dc2021-02-04 16:31:46 +01001119 memcpy(ra, TLVP_VAL(&tp[0], BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra));
Harald Welte560bdb32020-12-04 22:24:47 +01001120 gsm48_parse_ra(&raid, from_bvc->ra);
Harald Weltee5209642020-12-05 19:59:45 +01001121#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001122 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001123 }
Harald Weltee5209642020-12-05 19:59:45 +01001124 case BSSGP_PDUT_STATUS:
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001125 {
1126 struct gbproxy_sgsn *sgsn;
1127 /* Check if the status needs to be terminated locally */
1128 uint8_t cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE);
Daniel Willmannc5dcebd2021-09-30 16:57:51 +02001129
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001130 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) {
1131 /* Log and handle locally */
1132 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Daniel Willmannc5dcebd2021-09-30 16:57:51 +02001133 LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s) for PtP-BVC %05u\n", cause,
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001134 bssgp_cause_str(cause), ptp_bvci);
1135 /* FIXME: Remove/block our BVC if present? */
1136 break;
1137 }
1138
Daniel Willmannc5dcebd2021-09-30 16:57:51 +02001139 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause,
1140 bssgp_cause_str(cause));
1141
Daniel Willmann6ec5f952021-10-28 16:13:03 +02001142 if (gbproxy_tlli_from_status_pdu(tp, &tlli, log_pfx) == 0)
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001143 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
1144 else
1145 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
1146
1147 if (!sgsn) {
1148 rc = -EINVAL;
1149 break;
1150 }
1151
1152 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
Harald Weltee5209642020-12-05 19:59:45 +01001153 break;
Daniel Willmann7d37cbb2021-09-29 11:51:51 +02001154 }
Harald Weltee5209642020-12-05 19:59:45 +01001155 case BSSGP_PDUT_RAN_INFO:
1156 case BSSGP_PDUT_RAN_INFO_REQ:
1157 case BSSGP_PDUT_RAN_INFO_ACK:
1158 case BSSGP_PDUT_RAN_INFO_ERROR:
1159 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001160 rc = gbprox_rx_rim_from_bss(tp, nse, msg, log_pfx, pdut_name);
Harald Weltee5209642020-12-05 19:59:45 +01001161 break;
1162 case BSSGP_PDUT_LLC_DISCARD:
Harald Weltee5209642020-12-05 19:59:45 +01001163 /* route based on BVCI + TLLI */
Philipp Maier74882dc2021-02-04 16:31:46 +01001164 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1165 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Harald Weltee5209642020-12-05 19:59:45 +01001166 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1167 if (!from_bvc)
1168 goto err_no_bvc;
Daniel Willmanne705b3f2021-11-25 22:04:56 +01001169 rc = gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true);
Harald Weltee5209642020-12-05 19:59:45 +01001170 break;
Daniel Willmanne705b3f2021-11-25 22:04:56 +01001171 case BSSGP_PDUT_FLUSH_LL_ACK:
1172 {
1173 /* Route based on TLLI */
1174 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
1175 struct gbproxy_sgsn *sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
1176 if (!sgsn) {
1177 rc = -EINVAL;
1178 break;
1179 }
1180
1181 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
1182 break;
1183 }
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001184 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +01001185 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001186 {
1187 /* Route according to IMSI<->NSE cache entry */
1188 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001189 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1190 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001191 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +02001192 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001193 if (!nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001194 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001195 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001196 OSMO_ASSERT(nse->sgsn_facing);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001197 rc = gbprox_relay2nse(msg, nse, 0);
1198 break;
1199 }
Daniel Willmannf024eeb2021-07-06 14:02:41 +02001200 case BSSGP_PDUT_MS_REGISTR_ENQ:
1201 {
1202 struct gbproxy_sgsn *sgsn;
1203 struct osmo_mobile_identity mi;
1204 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1205 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
1206 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1207
1208 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
1209 if (!sgsn) {
1210 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
1211 rc = -EINVAL;
1212 break;
1213 }
1214
1215 gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_MS_REG_ENQ);
1216
1217 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
1218 break;
1219 }
Harald Weltee5209642020-12-05 19:59:45 +01001220 default:
1221 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001222 break;
1223 }
1224
Harald Weltee5209642020-12-05 19:59:45 +01001225 return rc;
Harald Welte560bdb32020-12-04 22:24:47 +01001226err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001227 LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001228 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_NSEI));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001229 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001230}
1231
1232/* Receive paging request from SGSN, we need to relay to proper BSS */
Harald Weltedf690e82020-12-12 15:58:28 +01001233static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name,
Daniel Willmann5614e572021-01-18 18:38:27 +01001234 struct tlv_parsed *tp, uint16_t ns_bvci, bool broadcast)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001235{
Harald Weltedf690e82020-12-12 15:58:28 +01001236 struct gbproxy_config *cfg = sgsn_nse->cfg;
Harald Weltee5209642020-12-05 19:59:45 +01001237 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Weltedf690e82020-12-12 15:58:28 +01001238 struct gbproxy_nse *nse;
Daniel Willmann76205712020-11-30 17:08:58 +01001239 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001240 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte8b4c7942020-12-05 10:14:49 +01001241 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001242
Harald Welte173a1822020-12-03 15:36:59 +01001243 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001244 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001245 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Weltedf690e82020-12-12 15:58:28 +01001246 sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001247 if (!sgsn_bvc) {
Harald Weltedf690e82020-12-12 15:58:28 +01001248 LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n",
Harald Weltee5209642020-12-05 19:59:45 +01001249 pdut_name, bvci);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001250 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr));
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001251 return -EINVAL;
1252 }
Harald Weltee5209642020-12-05 19:59:45 +01001253 LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name);
1254 return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte173a1822020-12-03 15:36:59 +01001255 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001256 struct gprs_ra_id raid;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001257 errctr = GBPROX_GLOB_CTR_INV_RAI;
Philipp Maierda3af942021-02-04 21:54:09 +01001258 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte560bdb32020-12-04 22:24:47 +01001259 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001260 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001261 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Daniel Willmannf9902c52021-11-12 15:52:03 +01001262 /* Skip BVCs without a cell (e.g. signalling) */
1263 if (!bss_bvc->cell)
1264 continue;
1265
1266 if (gsm48_ra_equal(&bss_bvc->cell->id.raid, &raid)) {
Harald Weltee5209642020-12-05 19:59:45 +01001267 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n",
1268 pdut_name);
1269 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001270 n_nses++;
1271 /* Only send it once to each NSE */
1272 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001273 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001274 }
1275 }
Harald Welte173a1822020-12-03 15:36:59 +01001276 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001277 struct gsm48_ra_id lac;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001278 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte560bdb32020-12-04 22:24:47 +01001279 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001280 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001281 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Daniel Willmannf9902c52021-11-12 15:52:03 +01001282 /* Skip BVCs without a cell (e.g. signalling) */
1283 if (!bss_bvc->cell)
1284 continue;
1285
1286 gsm48_encode_ra(&lac, &bss_bvc->cell->id.raid);
Philipp Maierda3af942021-02-04 21:54:09 +01001287 if (!memcmp(&lac, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Harald Weltee5209642020-12-05 19:59:45 +01001288 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n",
1289 pdut_name);
1290 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001291 n_nses++;
1292 /* Only send it once to each NSE */
1293 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001294 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001295 }
1296 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001297 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1) || broadcast) {
Harald Welte560bdb32020-12-04 22:24:47 +01001298 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001299 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001300 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1301 LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name);
1302 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001303 n_nses++;
1304 /* Only send it once to each NSE */
1305 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001306 }
Harald Welte53ee2062020-11-24 11:31:13 +01001307 }
1308 } else {
Harald Weltedf690e82020-12-12 15:58:28 +01001309 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n");
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001310 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr));
Harald Welte53ee2062020-11-24 11:31:13 +01001311 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001312
Daniel Willmann76205712020-11-30 17:08:58 +01001313 if (n_nses == 0) {
Harald Weltedf690e82020-12-12 15:58:28 +01001314 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n");
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001315 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001316 return -EINVAL;
1317 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001318 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001319}
1320
1321/* Receive an incoming BVC-RESET message from the SGSN */
Harald Weltee5209642020-12-05 19:59:45 +01001322static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp,
1323 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001324{
Harald Weltee5209642020-12-05 19:59:45 +01001325 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1326 struct gbproxy_bvc *from_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001327
Harald Weltee5209642020-12-05 19:59:45 +01001328 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001329
Harald Weltee5209642020-12-05 19:59:45 +01001330 if (ptp_bvci == 0) {
1331 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
1332 OSMO_ASSERT(from_bvc);
1333 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
1334 } else {
1335 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1336 if (!from_bvc) {
1337 LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001338 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001339 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001340 }
Harald Weltee5209642020-12-05 19:59:45 +01001341 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001342 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001343
1344 return 0;
1345}
1346
Philipp Maier1c5766b2021-02-09 17:03:03 +01001347/* Receive an incoming RIM message from the SGSN-side NS-VC */
1348static int gbprox_rx_rim_from_sgsn(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
1349 const char *pdut_name)
1350{
1351 struct gbproxy_sgsn *sgsn;
1352 struct gbproxy_cell *dest_cell;
1353 struct bssgp_rim_routing_info dest_ri;
1354 struct bssgp_rim_routing_info src_ri;
1355 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +01001356 char ri_src_str[64];
1357 char ri_dest_str[64];
Daniel Willmannf8cba652021-02-12 04:59:47 +01001358 uint16_t ns_bvci = msgb_bvci(msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001359
1360 /* TODO: Reply with STATUS if BSSGP didn't negotiate RIM feature, see also comments in
1361 gbprox_rx_rim_from_bss() */
1362
1363 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
1364 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
1365 if (rc < 0) {
1366 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001367 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001368 }
1369 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
1370 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
1371 if (rc < 0) {
1372 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001373 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001374 }
1375
1376 /* Since gbproxy is 2G only we do not expect to get RIM messages that target non-GERAN cells. */
1377 if (dest_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
1378 LOGP(DGPRS, LOGL_ERROR, "%s %s destination RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
1379 bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001380 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001381 }
1382
1383 /* Lookup destination cell */
1384 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
1385 if (!dest_cell) {
1386 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for destination RIM routing info (%s)\n", log_pfx,
1387 pdut_name, bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001388 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001389 }
1390
1391 /* TODO: Check if the BVC of the destination cell actually did negotiate RIM support, see also comments
1392 * in gbprox_rx_rim_from_bss() */
1393 sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
1394 OSMO_ASSERT(sgsn);
1395
Philipp Maier4499cf42021-02-10 17:54:44 +01001396 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying from SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
1397 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
1398 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
1399 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +01001400
1401 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
1402}
1403
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001404/* Receive an incoming signalling message from the SGSN-side NS-VC */
Harald Weltedbef0aa2020-12-07 17:48:11 +01001405static int gbprox_rx_sig_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001406{
Harald Weltedbef0aa2020-12-07 17:48:11 +01001407 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001408 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +01001409 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
1410 struct gbproxy_config *cfg = nse->cfg;
1411 struct gbproxy_bvc *sgsn_bvc;
Philipp Maier74882dc2021-02-04 16:31:46 +01001412 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001413 int data_len;
Alexander Couzens96a44272022-09-28 16:28:46 +02001414 uint16_t bvci, bvci2;
1415 uint32_t tlli;
Harald Welteec0f8012020-12-06 16:32:01 +01001416 char log_pfx[32];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001417 int rc = 0;
Harald Welted2fef952020-12-05 00:31:07 +01001418 int i;
Daniel Willmann5614e572021-01-18 18:38:27 +01001419 bool paging_bc = false;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001420
Harald Weltee5209642020-12-05 19:59:45 +01001421 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
1422
1423 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001424
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001425 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welteec0f8012020-12-06 16:32:01 +01001426 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001427 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001428 }
1429
Harald Welte278dd272020-12-06 13:35:24 +01001430 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001431 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001432 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001433 }
1434
1435 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001436 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001437 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001438 }
1439
Harald Weltedbef0aa2020-12-07 17:48:11 +01001440 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welteec0f8012020-12-06 16:32:01 +01001441
Philipp Maier74882dc2021-02-04 16:31:46 +01001442 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, tp, ARRAY_SIZE(tp), pdu_type, bgph->data, data_len, 0, 0,
Harald Welteec0f8012020-12-06 16:32:01 +01001443 DGPRS, log_pfx);
1444 if (rc < 0) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001445 rc = tx_status_from_tlvp(nse, rc, msg);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001446 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN));
Harald Welteec0f8012020-12-06 16:32:01 +01001447 return rc;
1448 }
Harald Weltee5209642020-12-05 19:59:45 +01001449 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001450 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001451
1452 switch (pdu_type) {
1453 case BSSGP_PDUT_BVC_RESET:
Harald Weltee5209642020-12-05 19:59:45 +01001454 /* resolve or create ggbproxy_bvc + handle in BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001455 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1456 rc = rx_bvc_reset_from_sgsn(nse, msg, &tp[0], ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001457 break;
1458 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001459 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001460 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1461 if (!sgsn_bvc)
1462 goto err_no_bvc;
1463 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1464 break;
1465 case BSSGP_PDUT_BVC_BLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001466 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001467 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
Daniel Willmanndc763fd2021-09-24 16:45:38 +02001468 if (!sgsn_bvc) {
1469 /* Check if BVC was blocked before */
1470 sgsn_bvc = gbproxy_bvc_by_bvci_inactive(nse, bvci);
1471 if (!sgsn_bvc)
1472 goto err_no_bvc;
1473 }
Harald Weltee5209642020-12-05 19:59:45 +01001474 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg);
1475 break;
1476 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001477 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001478 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1479 if (!sgsn_bvc)
1480 goto err_no_bvc;
1481 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg);
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001482 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001483 case BSSGP_PDUT_FLUSH_LL:
Alexander Couzens96a44272022-09-28 16:28:46 +02001484 /* TODO: If we negotiate Inter-NSE re-routing or LCS-procedures we can also
Daniel Willmanne705b3f2021-11-25 22:04:56 +01001485 * add the NSEI TLV to trigger re-routing the PDUs */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001486 /* simple case: BVCI IE is mandatory */
Philipp Maier74882dc2021-02-04 16:31:46 +01001487 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001488 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1489 if (!sgsn_bvc)
1490 goto err_no_bvc;
Alexander Couzens96a44272022-09-28 16:28:46 +02001491
1492 /* bvc is not valid */
1493 if (!sgsn_bvc->cell || !sgsn_bvc->cell->bss_bvc) {
1494 rc = -EINVAL;
1495 break;
1496 }
1497
1498 /* When both the old and new BVCI is present:
1499 * If old & new BVCI is on the same NSEI, forward message as is,
1500 * otherwise remove new BVCI. */
1501 if (TLVP_PRESENT(&tp[1], BSSGP_IE_BVCI)) {
1502 struct gbproxy_bvc *bvc2;
1503 struct msgb *flush;
1504
1505 bvci2 = ntohs(tlvp_val16_unal(&tp[1], BSSGP_IE_BVCI));
1506 bvc2 = gbproxy_bvc_by_bvci(nse, bvci2);
1507
1508 /* it's the same NSEI, we can pass the message as is */
1509 if (bvc2->nse == sgsn_bvc->nse) {
1510 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
1511 break;
1512 }
1513
1514 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
1515 /* build a new message in order to drop the old one */
1516 flush = bssgp2_enc_flush_ll(tlli, bvci, NULL, NULL);
1517 if (!flush) {
1518 /* TODO: error message */
1519 rc = -ENOMEM;
1520 break;
1521 }
1522
1523 DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg),
1524 !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS");
1525 rc = bssgp2_nsi_tx_ptp(nse->cfg->nsi, nse->nsei, ns_bvci, flush, tlli);
1526 } else {
Harald Weltee5209642020-12-05 19:59:45 +01001527 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Alexander Couzens96a44272022-09-28 16:28:46 +02001528 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001529 break;
Daniel Willmann5614e572021-01-18 18:38:27 +01001530 case BSSGP_PDUT_DUMMY_PAGING_PS:
1531 /* Routing area is optional in dummy paging and we have nothing else to go by
1532 * so in case it is missing we need to broadcast the paging */
1533 paging_bc = true;
1534 /* fall through */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001535 case BSSGP_PDUT_PAGING_PS:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001536 {
1537 /* Cache the IMSI<->NSE to route PAGING REJECT */
1538 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001539 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1540 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001541 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +02001542 gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001543 /* fall through */
1544 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001545 case BSSGP_PDUT_PAGING_CS:
1546 /* process the paging request (LAI/RAI lookup) */
Philipp Maier74882dc2021-02-04 16:31:46 +01001547 rc = gbprox_rx_paging(nse, msg, pdut_name, &tp[0], ns_bvci, paging_bc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001548 break;
1549 case BSSGP_PDUT_STATUS:
Daniel Willmann885f4302021-09-30 16:58:46 +02001550 {
1551 struct gbproxy_nse *nse_peer;
1552 uint32_t tlli;
1553
1554 /* Check if the status needs to be terminated locally */
1555 uint8_t cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE);
1556
1557 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) {
1558 /* Log and handle locally, BVCI should be present for these causes */
1559 if (!TLVP_PRESENT(&tp[0], BSSGP_IE_BVCI)) {
1560 LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s), but BVCI is missing\n", cause,
1561 bssgp_cause_str(cause));
1562 break;
1563 }
1564 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1565 LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s) for PtP-BVC %05u\n", cause,
1566 bssgp_cause_str(cause), ptp_bvci);
1567 /* FIXME: Remove/block the other BSS/SGSN BVCs if present? */
1568 break;
1569 }
1570
1571 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s)\n", cause,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001572 bssgp_cause_str(cause));
Daniel Willmann885f4302021-09-30 16:58:46 +02001573
Daniel Willmann3d8f5992021-10-27 16:05:37 +02001574
1575 if (gbproxy_bvci_from_status_pdu(tp, &bvci, log_pfx) == 0 && bvci != 0) {
1576 struct gbproxy_cell *cell = gbproxy_cell_by_bvci(cfg, bvci);
1577
1578 if ((!cell || !cell->bss_bvc || !cell->bss_bvc->nse)) {
1579 LOGPNSE(nse, LOGL_ERROR, "Rx STATUS cause=0x%02x(%s), but can't find NSE for cell\n",
1580 cause, bssgp_cause_str(cause));
1581 break;
1582 }
1583
1584 return gbprox_relay2nse(msg, cell->bss_bvc->nse, 0);
1585 }
1586
Daniel Willmann6ec5f952021-10-28 16:13:03 +02001587 /* We can only forward this TLLI if it's in the cache (which only happens on suspend/resume) */
1588 if (gbproxy_tlli_from_status_pdu(tp, &tlli, log_pfx) == 0) {
Daniel Willmann885f4302021-09-30 16:58:46 +02001589 nse_peer = gbproxy_nse_by_tlli(cfg, tlli);
1590 if (nse_peer)
1591 return gbprox_relay2nse(msg, nse_peer, 0);
1592 }
1593
1594 LOGPNSE(nse, LOGL_ERROR, "Unable to handle STATUS cause=0x%02x(%s)\n", cause,
1595 bssgp_cause_str(cause));
1596
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001597 break;
Daniel Willmann885f4302021-09-30 16:58:46 +02001598 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001599 /* those only exist in the SGSN -> BSS direction */
1600 case BSSGP_PDUT_SUSPEND_ACK:
1601 case BSSGP_PDUT_SUSPEND_NACK:
1602 case BSSGP_PDUT_RESUME_ACK:
1603 case BSSGP_PDUT_RESUME_NACK:
Daniel Willmann77493b12020-12-29 21:13:31 +01001604 {
1605 struct gbproxy_nse *nse_peer;
Philipp Maier74882dc2021-02-04 16:31:46 +01001606 uint32_t tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001607
1608 nse_peer = gbproxy_nse_by_tlli(cfg, tlli);
1609 if (!nse_peer) {
1610 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1611 /* TODO: Counter */
Daniel Willmannf8cba652021-02-12 04:59:47 +01001612 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001613 }
1614 /* Delete the entry after we're done */
1615 gbproxy_tlli_cache_remove(cfg, tlli);
1616 LOGPNSE(nse_peer, LOGL_DEBUG, "Rx %s: forwarding\n", pdut_name);
1617 gbprox_relay2nse(msg, nse_peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001618 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001619 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001620 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte7479c4d2020-12-02 20:06:04 +01001621 case BSSGP_PDUT_OVERLOAD:
Harald Weltee5209642020-12-05 19:59:45 +01001622 LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name);
Harald Welte560bdb32020-12-04 22:24:47 +01001623 /* broadcast to all BSS-side bvcs */
Harald Welted2fef952020-12-05 00:31:07 +01001624 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte7479c4d2020-12-02 20:06:04 +01001625 gbprox_relay2nse(msg, nse, 0);
1626 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001627 break;
Harald Weltee5209642020-12-05 19:59:45 +01001628 case BSSGP_PDUT_RAN_INFO:
1629 case BSSGP_PDUT_RAN_INFO_REQ:
1630 case BSSGP_PDUT_RAN_INFO_ACK:
1631 case BSSGP_PDUT_RAN_INFO_ERROR:
1632 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001633 rc = gbprox_rx_rim_from_sgsn(tp, nse, msg, log_pfx, pdut_name);
Pau Espin Pedrola4296342021-05-07 13:33:34 +02001634 break;
Daniel Willmannf024eeb2021-07-06 14:02:41 +02001635 case BSSGP_PDUT_MS_REGISTR_ENQ_RESP:
1636 {
1637 struct gbproxy_nse *nse_peer;
1638 struct osmo_mobile_identity mi;
1639 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1640 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
1641 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1642 nse_peer = gbproxy_nse_by_imsi(cfg, mi.imsi, CACHE_USAGE_MS_REG_ENQ);
1643 if (!nse_peer) {
1644 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1645 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1646 } else if (nse_peer->sgsn_facing) {
1647 LOGPNSE(nse, LOGL_ERROR, "Forwarding %s failed: IMSI cache contains SGSN NSE", pdut_name);
1648 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1649 }
1650 gbproxy_imsi_cache_remove(cfg, mi.imsi, CACHE_USAGE_MS_REG_ENQ);
1651 gbprox_relay2nse(msg, nse_peer, ns_bvci);
1652 break;
1653 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001654 default:
Harald Weltee5209642020-12-05 19:59:45 +01001655 LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001656 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001657 rc = tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001658 break;
1659 }
1660
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001661 return rc;
Harald Weltee5209642020-12-05 19:59:45 +01001662
Harald Welte560bdb32020-12-04 22:24:47 +01001663err_no_bvc:
Daniel Willmann723bb362021-09-28 18:51:47 +02001664 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC %05u\n", pdut_name, bvci);
Daniel Willmannc4b913b2021-09-24 16:43:42 +02001665 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001666 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001667}
1668
Harald Weltee5209642020-12-05 19:59:45 +01001669
1670/***********************************************************************
1671 * libosmogb NS/BSSGP integration
1672 ***********************************************************************/
1673
Alexander Couzens951e1332020-09-22 13:21:46 +02001674int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001675{
1676 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001677 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1678 struct gprs_ns2_inst *nsi = cfg->nsi;
1679 struct osmo_gprs_ns2_prim nsp = {};
1680
1681 nsp.bvci = msgb_bvci(msg);
1682 nsp.nsei = msgb_nsei(msg);
1683
Alexander Couzens55c36f92021-01-27 20:56:55 +01001684 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001685 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1686
1687 return rc;
1688}
1689
1690/* Main input function for Gb proxy */
1691int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1692{
Alexander Couzens951e1332020-09-22 13:21:46 +02001693 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Harald Weltee5209642020-12-05 19:59:45 +01001694 uint16_t ns_bvci = msgb_bvci(msg);
1695 uint16_t nsei = msgb_nsei(msg);
1696 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001697
Harald Weltee5209642020-12-05 19:59:45 +01001698 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN);
1699 if (nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001700 /* ensure minimum length to decode PDU type */
1701 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1702 return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1703
Harald Weltee5209642020-12-05 19:59:45 +01001704 if (ns_bvci == 0 || ns_bvci == 1)
1705 return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001706 else
Harald Weltee5209642020-12-05 19:59:45 +01001707 return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001708 }
1709
Harald Weltee5209642020-12-05 19:59:45 +01001710 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS);
1711 if (!nse) {
1712 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei);
1713 nse = gbproxy_nse_alloc(cfg, nsei, false);
1714 }
1715 if (nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001716 /* ensure minimum length to decode PDU type */
1717 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1718 return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1719
Harald Weltee5209642020-12-05 19:59:45 +01001720 if (ns_bvci == 0 || ns_bvci == 1)
1721 return gbprox_rx_sig_from_bss(nse, msg, ns_bvci);
1722 else
1723 return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci);
1724 }
1725
1726 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001727}
1728
Alexander Couzens951e1332020-09-22 13:21:46 +02001729/* TODO: What about handling:
Alexander Couzens55c36f92021-01-27 20:56:55 +01001730 * GPRS_NS2_AFF_CAUSE_VC_FAILURE,
1731 GPRS_NS2_AFF_CAUSE_VC_RECOVERY,
Alexander Couzens951e1332020-09-22 13:21:46 +02001732 osmocom own causes
Alexander Couzens55c36f92021-01-27 20:56:55 +01001733 GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED,
1734 GPRS_NS2_AFF_CAUSE_SNS_FAILURE,
Alexander Couzens951e1332020-09-22 13:21:46 +02001735 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001736
Alexander Couzens951e1332020-09-22 13:21:46 +02001737void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001738{
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001739 int i;
Harald Welte560bdb32020-12-04 22:24:47 +01001740 struct gbproxy_bvc *bvc;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001741 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001742
Alexander Couzens951e1332020-09-22 13:21:46 +02001743 switch (nsp->u.status.cause) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001744 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
1745 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens951e1332020-09-22 13:21:46 +02001746 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001747
Alexander Couzens55c36f92021-01-27 20:56:55 +01001748 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Harald Welte9b367d22021-01-18 13:55:51 +01001749 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Daniel Willmannf96cac52021-03-09 16:14:18 +01001750 nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN);
1751 if (nse) {
Daniel Willmann37518b32021-05-27 18:13:36 +02001752 nse->alive = true;
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001753 // Update the NSE max SDU len
Daniel Willmannf96cac52021-03-09 16:14:18 +01001754 nse->max_sdu_len = nsp->u.status.mtu;
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001755
Harald Weltee5209642020-12-05 19:59:45 +01001756 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001757 bvc = gbproxy_bvc_by_bvci(nse, 0);
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001758 if (bvc) {
Daniel Willmannf96cac52021-03-09 16:14:18 +01001759 bssgp_bvc_fsm_set_max_pdu_len(bvc->fi, nse->max_sdu_len);
Daniel Willmann3ea37932021-02-10 13:41:14 +01001760 osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001761 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001762 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001763 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001764 case GPRS_NS2_AFF_CAUSE_FAILURE:
Daniel Willmannf96cac52021-03-09 16:14:18 +01001765 nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_BSS | NSE_F_SGSN);
1766 if (!nse) {
1767 LOGP(DGPRS, LOGL_ERROR, "Unknown NSE(%05d) became unavailable\n", nsp->nsei);
1768 break;
1769 }
Daniel Willmann37518b32021-05-27 18:13:36 +02001770
1771 nse->alive = false;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001772 if (nse->sgsn_facing) {
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001773 struct hlist_node *ntmp;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001774 /* SGSN */
1775 /* TODO: When to block all PtP towards bss? Only if all SGSN are down? */
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001776 hash_for_each_safe(nse->bvcs, i, ntmp, bvc, list) {
1777 if (bvc->bvci == 0)
1778 continue;
1779 gbproxy_bvc_free(bvc);
1780 }
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001781 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_RESTART_RESET_SGSN));
Alexander Couzens951e1332020-09-22 13:21:46 +02001782 } else {
Daniel Willmannf96cac52021-03-09 16:14:18 +01001783 /* BSS became unavailable
1784 * Block matching PtP-BVCs on SGSN-side */
1785 hash_for_each(nse->bvcs, i, bvc, list) {
1786 if (bvc->bvci == 0)
1787 continue;
1788 /* Get BVC for each SGSN and send block request */
1789 struct gbproxy_cell *cell = bvc->cell;
1790 for (int j = 0; j < GBPROXY_MAX_NR_SGSN; j++) {
1791 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[j];
1792 if (!sgsn_bvc)
1793 continue;
1794
1795 /* Block BVC, indicate BSS equipment failure */
1796 uint8_t cause = BSSGP_CAUSE_EQUIP_FAIL;
1797 osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Daniel Willmanndc763fd2021-09-24 16:45:38 +02001798 sgsn_bvc->inactive = true;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001799 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001800 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001801
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001802 /* This frees the BVCs for us as well */
1803 gbproxy_nse_free(nse);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001804 }
Harald Welte9b367d22021-01-18 13:55:51 +01001805 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Alexander Couzens951e1332020-09-22 13:21:46 +02001806 break;
1807 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001808 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001809 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001810 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001811 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001812}
1813
Alexander Couzens951e1332020-09-22 13:21:46 +02001814/* called by the ns layer */
1815int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1816{
1817 struct osmo_gprs_ns2_prim *nsp;
1818 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001819 uintptr_t bvci;
Alexander Couzens951e1332020-09-22 13:21:46 +02001820 int rc = 0;
1821
1822 if (oph->sap != SAP_NS)
1823 return 0;
1824
1825 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1826
1827 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte9b367d22021-01-18 13:55:51 +01001828 LOGP(DGPRS, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001829 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001830 return 0;
1831 }
1832
1833 switch (oph->primitive) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001834 case GPRS_NS2_PRIM_UNIT_DATA:
Daniel Willmann8f407b12020-12-02 19:33:50 +01001835
Alexander Couzens951e1332020-09-22 13:21:46 +02001836 /* hand the message into the BSSGP implementation */
1837 msgb_bssgph(oph->msg) = oph->msg->l3h;
1838 msgb_bvci(oph->msg) = nsp->bvci;
1839 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001840 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens951e1332020-09-22 13:21:46 +02001841
Daniel Willmann8f407b12020-12-02 19:33:50 +01001842 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens951e1332020-09-22 13:21:46 +02001843 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001844 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001845 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001846 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens951e1332020-09-22 13:21:46 +02001847 gprs_ns_prim_status_cb(cfg, nsp);
1848 break;
1849 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001850 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001851 gprs_ns2_prim_str(oph->primitive),
1852 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001853 break;
1854 }
1855
1856 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001857}
1858
1859void gbprox_reset(struct gbproxy_config *cfg)
1860{
Harald Welted2fef952020-12-05 00:31:07 +01001861 struct gbproxy_nse *nse;
1862 struct hlist_node *ntmp;
Harald Welte8b4c7942020-12-05 10:14:49 +01001863 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001864
Harald Welted2fef952020-12-05 00:31:07 +01001865 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte8b4c7942020-12-05 10:14:49 +01001866 struct gbproxy_bvc *bvc;
1867 struct hlist_node *tmp;
1868 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +01001869 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001870
1871 gbproxy_nse_free(nse);
1872 }
Harald Weltee5209642020-12-05 19:59:45 +01001873 /* FIXME: cells */
1874 /* FIXME: SGSN side BVCs (except signaling) */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001875
1876 rate_ctr_group_free(cfg->ctrg);
1877 gbproxy_init_config(cfg);
1878}
1879
Daniel Willmann77493b12020-12-29 21:13:31 +01001880static void tlli_cache_cleanup(void *data)
1881{
1882 struct gbproxy_config *cfg = data;
1883 gbproxy_tlli_cache_cleanup(cfg);
1884
1885 /* TODO: Disable timer when cache is empty */
1886 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1887}
1888
Daniel Willmannc8a50092021-01-17 13:11:41 +01001889static void imsi_cache_cleanup(void *data)
1890{
1891 struct gbproxy_config *cfg = data;
1892 gbproxy_imsi_cache_cleanup(cfg);
1893
1894 /* TODO: Disable timer when cache is empty */
1895 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1896}
1897
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001898int gbproxy_init_config(struct gbproxy_config *cfg)
1899{
1900 struct timespec tp;
1901
Harald Welte209dc9f2020-12-12 19:02:16 +01001902 /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
1903 cfg->pool.bvc_fc_ratio = 100;
Daniel Willmannee834af2020-12-14 16:22:39 +01001904 cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001905 /* TODO: Make configurable */
Daniel Willmannbd12f3f2021-01-13 18:16:04 +01001906 cfg->tlli_cache.timeout = 10;
Daniel Willmannc8a50092021-01-17 13:11:41 +01001907 cfg->imsi_cache.timeout = 10;
Daniel Willmannee834af2020-12-14 16:22:39 +01001908
Harald Welted2fef952020-12-05 00:31:07 +01001909 hash_init(cfg->bss_nses);
Daniel Willmann1e7be5d2020-12-21 18:08:21 +01001910 hash_init(cfg->sgsn_nses);
1911 hash_init(cfg->cells);
Daniel Willmann77493b12020-12-29 21:13:31 +01001912 hash_init(cfg->tlli_cache.entries);
Daniel Willmannee834af2020-12-14 16:22:39 +01001913 INIT_LLIST_HEAD(&cfg->sgsns);
1914
Daniel Willmann77493b12020-12-29 21:13:31 +01001915 osmo_timer_setup(&cfg->tlli_cache.timer, tlli_cache_cleanup, cfg);
1916 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1917
Daniel Willmannc8a50092021-01-17 13:11:41 +01001918 /* We could also combine both timers */
1919 osmo_timer_setup(&cfg->imsi_cache.timer, imsi_cache_cleanup, cfg);
1920 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1921
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001922 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1923 if (!cfg->ctrg) {
1924 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1925 return -1;
1926 }
1927 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Harald Weltec169de42020-12-07 13:12:13 +01001928 osmo_fsm_log_timeouts(true);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001929
1930 return 0;
Oliver Smith29532c22021-01-29 11:13:00 +01001931}