blob: e24f468c602a103cdba13b7b1fafc1ed23f0b5a6 [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[] = {
62 { "inv-bvci", "Invalid BVC Identifier " },
63 { "inv-lai", "Invalid Location Area Identifier" },
64 { "inv-rai", "Invalid Routing Area Identifier " },
65 { "inv-nsei", "No BVC established for NSEI " },
66 { "proto-err:bss", "BSSGP protocol error (BSS )" },
67 { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
68 { "not-supp:bss", "Feature not supported (BSS )" },
69 { "not-supp:sgsn", "Feature not supported (SGSN)" },
70 { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
71 { "tx-err:sgsn", "NS Transmission error (SGSN)" },
72 { "error", "Other error " },
73 { "mod-peer-err", "Patch error: no peer " },
74};
75
76static const struct rate_ctr_group_desc global_ctrg_desc = {
77 .group_name_prefix = "gbproxy:global",
78 .group_description = "GBProxy Global Statistics",
79 .num_ctr = ARRAY_SIZE(global_ctr_description),
80 .ctr_desc = global_ctr_description,
81 .class_id = OSMO_STATS_CLASS_GLOBAL,
82};
83
Harald Welte560bdb32020-12-04 22:24:47 +010084static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann35f7d332020-11-03 21:11:45 +010085 uint16_t ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020086
Daniel Willmannf8cba652021-02-12 04:59:47 +010087int tx_status(struct gbproxy_nse *nse, uint16_t ns_bvci, enum gprs_bssgp_cause cause, const uint16_t *bvci, const struct msgb *old_msg)
88{
89 int rc;
Harald Weltea0f70732020-12-05 17:50:23 +010090
Daniel Willmannf8cba652021-02-12 04:59:47 +010091 struct msgb *msg = bssgp2_enc_status(cause, bvci, old_msg, nse->max_sdu_len);
92 if (!msg) {
93 LOGPNSE(nse, LOGL_NOTICE, "Unable to encode STATUS message\n");
94 return -ENOMEM;
95 }
96
97 rc = bssgp2_nsi_tx_ptp(nse->cfg->nsi, nse->nsei, ns_bvci, msg, 0);
98 if (rc < 0)
99 LOGPNSE(nse, LOGL_NOTICE, "Unable to send STATUS message\n");
100 return rc;
101}
102
103/* generate BVC-STATUS mess
104age with cause value derived from TLV-parser error */
105static 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 +0100106{
107 uint8_t bssgp_cause;
108 switch (tlv_p_err) {
109 case OSMO_TLVP_ERR_MAND_IE_MISSING:
110 bssgp_cause = BSSGP_CAUSE_MISSING_MAND_IE;
111 break;
112 default:
113 bssgp_cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
114 }
Daniel Willmannf8cba652021-02-12 04:59:47 +0100115 return tx_status(nse, msgb_bvci(orig_msg), bssgp_cause, NULL, orig_msg);
Harald Welteec0f8012020-12-06 16:32:01 +0100116}
117
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200118/* strip off the NS header */
119static void strip_ns_hdr(struct msgb *msg)
120{
121 int strip_len = msgb_bssgph(msg) - msg->data;
122 msgb_pull(msg, strip_len);
123}
124
Harald Weltee5209642020-12-05 19:59:45 +0100125#if 0
Harald Welte560bdb32020-12-04 22:24:47 +0100126/* feed a message down the NS-VC associated with the specified bvc */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200127static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
128 uint16_t ns_bvci, uint16_t sgsn_nsei)
129{
130 /* create a copy of the message so the old one can
131 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200132 struct gprs_ns2_inst *nsi = cfg->nsi;
133 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200134 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
135 int rc;
136
Daniel Willmann3696dce2020-12-02 16:08:02 +0100137 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200138 msgb_nsei(msg), ns_bvci, sgsn_nsei);
139
Alexander Couzens951e1332020-09-22 13:21:46 +0200140 nsp.bvci = ns_bvci;
141 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200142
143 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200144 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
145 PRIM_OP_REQUEST, msg);
146 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200147 if (rc < 0)
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200148 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_TX_ERR_SGSN));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200149 return rc;
150}
Harald Weltee5209642020-12-05 19:59:45 +0100151#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200152
Harald Weltee30985e2021-01-28 19:13:19 +0100153/*! Determine the TLLI from the given BSSGP message.
154 * \param[in] bssgp pointer to start of BSSGP header
155 * \param[in] bssgp_len length of BSSGP message in octets
156 * \param[out] tlli TLLI (if any) in host byte order
157 * \returns 1 if TLLI found; 0 if none found; negative on parse error */
158int gprs_gb_parse_tlli(const uint8_t *bssgp, size_t bssgp_len, uint32_t *tlli)
159{
160 const struct bssgp_normal_hdr *bgph;
161 uint8_t pdu_type;
162
163 if (bssgp_len < sizeof(struct bssgp_normal_hdr))
164 return -EINVAL;
165
166 bgph = (struct bssgp_normal_hdr *)bssgp;
167 pdu_type = bgph->pdu_type;
168
169 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
170 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
171 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *)bssgp;
172 if (bssgp_len < sizeof(struct bssgp_ud_hdr))
173 return -EINVAL;
174 *tlli = osmo_load32be((const uint8_t *)&budh->tlli);
175 return 1;
176 } else {
177 const uint8_t *data = bgph->data;
178 size_t data_len = bssgp_len - sizeof(*bgph);
179 struct tlv_parsed tp;
180
181 if (bssgp_tlv_parse(&tp, data, data_len) < 0)
182 return -EINVAL;
183
184 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
185 *tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
186 return 1;
187 }
188 }
189
190 /* No TLLI present in message */
191 return 0;
192}
193
Daniel Willmann76205712020-11-30 17:08:58 +0100194/* feed a message down the NSE */
195static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100196 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200197{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100198 OSMO_ASSERT(nse);
199 OSMO_ASSERT(nse->cfg);
200
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200201 /* create a copy of the message so the old one can
202 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100203 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Daniel Willmann76205712020-11-30 17:08:58 +0100204 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Daniel Willmann44fa2012021-02-12 04:55:40 +0100205 uint32_t tlli = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200206 int rc;
207
Daniel Willmann98b1b452020-12-21 10:40:27 +0100208 DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg),
209 !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200210
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200211 /* Strip the old NS header, it will be replaced with a new one */
212 strip_ns_hdr(msg);
213
Harald Weltefe059582020-11-18 12:01:46 +0100214 /* TS 48.018 Section 5.4.2: The link selector parameter is
215 * defined in 3GPP TS 48.016. At one side of the Gb interface,
216 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
217 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
218 * underlying network service. */
Daniel Willmann44fa2012021-02-12 04:55:40 +0100219 gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli);
Harald Weltefe059582020-11-18 12:01:46 +0100220
Daniel Willmann44fa2012021-02-12 04:55:40 +0100221 rc = bssgp2_nsi_tx_ptp(nsi, nse->nsei, ns_bvci, msg, tlli);
Daniel Willmann76205712020-11-30 17:08:58 +0100222 /* FIXME: We need a counter group for gbproxy_nse */
223 //if (rc < 0)
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200224 // rate_ctr_inc(rate_ctr_group_get_ctr(bvc->ctrg, GBPROX_PEER_CTR_TX_ERR));
Daniel Willmann76205712020-11-30 17:08:58 +0100225
226 return rc;
227}
228
Harald Welte560bdb32020-12-04 22:24:47 +0100229/* feed a message down the NS-VC associated with the specified bvc */
230static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann76205712020-11-30 17:08:58 +0100231 uint16_t ns_bvci)
232{
233 int rc;
Harald Welte560bdb32020-12-04 22:24:47 +0100234 struct gbproxy_nse *nse = bvc->nse;
Daniel Willmann76205712020-11-30 17:08:58 +0100235 OSMO_ASSERT(nse);
236
237 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200238 if (rc < 0)
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200239 rate_ctr_inc(rate_ctr_group_get_ctr(bvc->ctrg, GBPROX_PEER_CTR_TX_ERR));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200240
241 return rc;
242}
243
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200244int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
245{
246 return 0;
247}
248
Harald Weltee5209642020-12-05 19:59:45 +0100249
250/***********************************************************************
251 * PTP BVC handling
252 ***********************************************************************/
253
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100254/* FIXME: Handle the tlli NULL case correctly,
255 * This function should take a generic selector
256 * and choose an sgsn based on that
257 */
258static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli)
259{
260 struct gbproxy_sgsn *sgsn = NULL;
261 struct gbproxy_sgsn *sgsn_avoid = NULL;
262
263 int tlli_type;
264 int16_t nri;
265 bool null_nri = false;
266
267 if (!tlli) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200268 sgsn = gbproxy_sgsn_by_available(cfg);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100269 if (!sgsn) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200270 LOGP(DGPRS, LOGL_ERROR, "Could not find any available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100271 return NULL;
272 }
Daniel Willmann37518b32021-05-27 18:13:36 +0200273 LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100274 return sgsn;
275 }
276
277 if (cfg->pool.nri_bitlen == 0) {
278 /* Pooling is disabled */
Daniel Willmann37518b32021-05-27 18:13:36 +0200279 sgsn = gbproxy_sgsn_by_available(cfg);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100280 if (!sgsn) {
Daniel Willmann37518b32021-05-27 18:13:36 +0200281 LOGP(DGPRS, LOGL_ERROR, "Could not find any available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100282 return NULL;
283 }
284
Daniel Willmann37518b32021-05-27 18:13:36 +0200285 LOGPSGSN(sgsn, LOGL_INFO, "Pooling disabled, using first available SGSN\n");
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100286 } else {
287 /* Pooling is enabled, try to use the NRI for routing to an SGSN
288 * See 3GPP TS 23.236 Ch. 5.3.2 */
289 tlli_type = gprs_tlli_type(*tlli);
290 if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) {
291 /* Only get/use the NRI if tlli type is local */
292 osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen);
293 if (nri >= 0) {
294 /* Get the SGSN for the NRI */
295 sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri);
296 if (sgsn && !null_nri)
297 return sgsn;
298 /* If the NRI is the null NRI, we need to avoid the chosen SGSN */
299 if (null_nri && sgsn) {
300 sgsn_avoid = sgsn;
301 }
302 } else {
303 /* We couldn't get the NRI from the TLLI */
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100304 LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %08x\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100305 }
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100306 } else {
307 LOGP(DGPRS, LOGL_INFO, "TLLI %08x is neither local nor foreign, not routing by NRI\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100308 }
309 }
310
311 /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid
312 * NOTE: This function is not stable if the number of SGSNs or allow_attach changes
313 * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that
314 * we can just wait for the MS to reattempt the procedure.
315 */
316 if (!sgsn)
317 sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli);
318
319 if (!sgsn) {
320 LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli);
321 return NULL;
322 }
323
324 return sgsn;
325}
326
327/*! Find the correct gbproxy_bvc given a cell and an SGSN
328 * \param[in] cfg The gbproxy configuration
329 * \param[in] cell The cell the message belongs to
330 * \param[in] tlli An optional TLLI used for tracking
331 * \return Returns 0 on success, otherwise a negative value
332 */
333static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli)
334{
335 struct gbproxy_sgsn *sgsn;
336 struct gbproxy_bvc *sgsn_bvc = NULL;
Harald Welte02d7c482020-12-30 12:13:36 +0100337 int i;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100338
339 sgsn = gbproxy_select_sgsn(cfg, tlli);
340 if (!sgsn) {
341 LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
342 return NULL;
343 }
344
345 /* Get the BVC for this SGSN/NSE */
Harald Welte02d7c482020-12-30 12:13:36 +0100346 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100347 sgsn_bvc = cell->sgsn_bvc[i];
348 if (!sgsn_bvc)
349 continue;
350 if (sgsn->nse != sgsn_bvc->nse)
351 continue;
352
353 return sgsn_bvc;
354 }
355
356 /* This shouldn't happen */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100357 LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN %s, dropping message!\n", sgsn->name);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100358 return NULL;
359}
360
361/*! Send a message to the next SGSN, possibly ignoring the null SGSN
362 * route an uplink message on a PTP-BVC to a SGSN using the TLLI
363 * \param[in] cell The cell the message belongs to
364 * \param[in] msg The BSSGP message
365 * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI)
366 * \param[in] tlli An optional TLLI used for tracking
367 * \return Returns 0 on success, otherwise a negative value
368 */
369static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli,
Harald Weltee5209642020-12-05 19:59:45 +0100370 bool sig_bvci)
371{
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100372 struct gbproxy_config *cfg = cell->cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100373 struct gbproxy_bvc *sgsn_bvc;
Harald Weltee5209642020-12-05 19:59:45 +0100374
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100375 sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli);
376 if (!sgsn_bvc) {
377 LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli);
378 return -EINVAL;
Harald Weltee5209642020-12-05 19:59:45 +0100379 }
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100380
381 return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);
Harald Weltee5209642020-12-05 19:59:45 +0100382}
383
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200384/* Receive an incoming PTP message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100385static 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 +0200386{
Harald Welte278dd272020-12-06 13:35:24 +0100387 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100388 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
389 struct gbproxy_bvc *bss_bvc;
390 struct tlv_parsed tp;
391 char log_pfx[32];
392 uint32_t tlli;
393 int rc;
394
395 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
396
397 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200398
Daniel Willmann06331ac2020-12-10 17:59:46 +0100399 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100400 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100401 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100402 }
403
404 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100405 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100406 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100407 }
408
409 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100410 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100411 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100412 }
413
Harald Weltee5209642020-12-05 19:59:45 +0100414 bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
415 if (!bss_bvc) {
416 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n",
417 log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100418 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200419 }
420
Harald Weltee5209642020-12-05 19:59:45 +0100421 /* UL_UNITDATA has a different header than all other uplink PDUs */
422 if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA) {
423 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
424 if (msgb_bssgp_len(msg) < sizeof(*budh))
Daniel Willmannf8cba652021-02-12 04:59:47 +0100425 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100426 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
427 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
428 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
429 * doesn't have to worry where the TLLI came from */
430 tp.lv[BSSGP_IE_TLLI].len = 4;
431 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
432 } else {
433 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
434 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
435 }
436 if (rc < 0) {
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200437 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100438 return tx_status_from_tlvp(nse, rc, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100439 }
Harald Welte85a40272020-12-08 21:43:22 +0100440 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
441 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200442
Harald Weltee5209642020-12-05 19:59:45 +0100443 switch (bgph->pdu_type) {
444 case BSSGP_PDUT_UL_UNITDATA:
445 case BSSGP_PDUT_RA_CAPA_UPDATE:
446 case BSSGP_PDUT_FLOW_CONTROL_MS:
447 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
448 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
449 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
450 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
451 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
452 case BSSGP_PDUT_FLOW_CONTROL_PFC:
453 case BSSGP_PDUT_DELETE_BSS_PFC_REQ:
454 case BSSGP_PDUT_PS_HO_REQUIRED:
455 case BSSGP_PDUT_PS_HO_REQUEST_ACK:
456 case BSSGP_PDUT_PS_HO_REQUEST_NACK:
457 case BSSGP_PDUT_PS_HO_COMPLETE:
458 case BSSGP_PDUT_PS_HO_CANCEL:
459 /* We can route based on TLLI-NRI */
460 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100461 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100462 break;
463 case BSSGP_PDUT_RADIO_STATUS:
464 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
465 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100466 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100467 } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {
468 /* we treat the TMSI like a TLLI and extract the NRI from it */
469 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Daniel Willmann8b3ed292021-01-21 18:46:51 +0100470 /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */
471 tlli = gprs_tmsi2tlli(tlli, TLLI_FOREIGN);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100472 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100473 } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {
Daniel Willmann5193f222021-01-11 05:00:46 +0100474 /* FIXME: Use the IMSI as selector? */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100475 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false);
Harald Weltee5209642020-12-05 19:59:45 +0100476 } else
477 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");
478 break;
479 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
480 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +0100481 {
482 /* Route according to IMSI<->NSE cache entry */
483 struct osmo_mobile_identity mi;
484 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
485 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
486 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +0200487 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann5614e572021-01-18 18:38:27 +0100488 if (nse) {
489 OSMO_ASSERT(nse->sgsn_facing);
490 rc = gbprox_relay2nse(msg, nse, ns_bvci);
491 } else {
Daniel Willmann82669182021-01-19 11:37:55 +0100492 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx unmatched %s with IMSI %s\n", pdut_name, mi.imsi);
Daniel Willmann5614e572021-01-18 18:38:27 +0100493 }
Harald Weltee5209642020-12-05 19:59:45 +0100494 break;
Daniel Willmann5614e572021-01-18 18:38:27 +0100495 }
Harald Weltee5209642020-12-05 19:59:45 +0100496 case BSSGP_PDUT_FLOW_CONTROL_BVC:
Harald Welte85a40272020-12-08 21:43:22 +0100497 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100498 break;
499 case BSSGP_PDUT_STATUS:
500 /* TODO: Implement by inspecting the contained PDU */
501 if (!TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR))
502 break;
503 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
504 break;
505 }
506
507 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200508}
509
510/* Receive an incoming PTP message from a SGSN-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100511static 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 +0200512{
Harald Welte278dd272020-12-06 13:35:24 +0100513 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100514 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
515 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte85a40272020-12-08 21:43:22 +0100516 struct tlv_parsed tp;
Harald Weltee5209642020-12-05 19:59:45 +0100517 char log_pfx[32];
Harald Welte85a40272020-12-08 21:43:22 +0100518 int rc;
Harald Weltee5209642020-12-05 19:59:45 +0100519
520 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
521
522 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200523
Daniel Willmann06331ac2020-12-10 17:59:46 +0100524 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100525 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100526 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100527 }
528
529 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100530 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100531 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100532 }
533
534 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100535 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100536 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100537 }
538
Harald Weltee5209642020-12-05 19:59:45 +0100539 sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
540 if (!sgsn_bvc) {
541 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n",
542 log_pfx, pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200543 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100544 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200545 }
546
Harald Weltee5209642020-12-05 19:59:45 +0100547 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) {
548 LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200549 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn_bvc->ctrg, GBPROX_PEER_CTR_DROPPED));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100550 return tx_status(nse, ns_bvci, BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200551 }
Harald Welte85a40272020-12-08 21:43:22 +0100552
553 /* DL_UNITDATA has a different header than all other uplink PDUs */
554 if (bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) {
555 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
556 if (msgb_bssgp_len(msg) < sizeof(*budh))
Daniel Willmannf8cba652021-02-12 04:59:47 +0100557 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte85a40272020-12-08 21:43:22 +0100558 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
559 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
560 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
561 * doesn't have to worry where the TLLI came from */
562 tp.lv[BSSGP_IE_TLLI].len = 4;
563 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
564 } else {
565 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
566 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
567 }
568 if (rc < 0) {
Pau Espin Pedrol56438362021-06-04 18:03:44 +0200569 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100570 return tx_status_from_tlvp(nse, rc, msg);
Harald Welte85a40272020-12-08 21:43:22 +0100571 }
572 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
573 msgb_bcid(msg) = (void *)&tp;
574
Harald Weltee5209642020-12-05 19:59:45 +0100575 OSMO_ASSERT(sgsn_bvc->cell);
576 bss_bvc = sgsn_bvc->cell->bss_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200577
Harald Welte85a40272020-12-08 21:43:22 +0100578 switch (bgph->pdu_type) {
579 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
580 return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg);
Daniel Willmann5614e572021-01-18 18:38:27 +0100581 case BSSGP_PDUT_DUMMY_PAGING_PS:
582 case BSSGP_PDUT_PAGING_PS:
583 {
584 /* Cache the IMSI<->NSE to route PAGING REJECT */
585 struct osmo_mobile_identity mi;
586 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
587 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
588 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +0200589 gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann5614e572021-01-18 18:38:27 +0100590 break;
Harald Welte85a40272020-12-08 21:43:22 +0100591 }
Daniel Willmann5614e572021-01-18 18:38:27 +0100592 default:
593 break;
594 }
595 return gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci);
Harald Welte85a40272020-12-08 21:43:22 +0100596
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200597}
598
Harald Weltee5209642020-12-05 19:59:45 +0100599/***********************************************************************
600 * BVC FSM call-backs
601 ***********************************************************************/
Harald Welte7df1e5a2020-12-02 22:53:26 +0100602
Harald Weltee5209642020-12-05 19:59:45 +0100603/* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */
604static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv)
605{
606 unsigned int i;
607
608 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
609 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i];
610 if (!sgsn_bvc)
611 continue;
612 osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv);
613 }
614}
615
616/* BVC FSM informs us about a BSS-side reset of the signaling BVC */
617static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
618 uint16_t cell_id, uint8_t cause, void *priv)
619{
620 struct gbproxy_bvc *sig_bvc = priv;
621 struct gbproxy_nse *nse = sig_bvc->nse;
622 struct gbproxy_bvc *ptp_bvc;
623 unsigned int i;
624
625 /* BLOCK all SGSN-side PTP BVC within this NSE */
626 hash_for_each(nse->bvcs, i, ptp_bvc, list) {
627 if (ptp_bvc == sig_bvc)
628 continue;
629 OSMO_ASSERT(ptp_bvc->cell);
630
631 dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Harald Weltef9e149b2020-12-02 23:29:38 +0100632 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100633
Harald Weltee5209642020-12-05 19:59:45 +0100634 /* Delete all BSS-side PTP BVC within this NSE */
635 gbproxy_cleanup_bvcs(nse, 0);
636
637 /* TODO: we keep the "CELL" around for now, re-connecting it to
638 * any (later) new PTP-BVC for that BVCI. Not sure if that's the
639 * best idea ? */
640}
641
642/* forward declaration */
643static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops;
644
645static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = {
646 .reset_notification = bss_sig_bvc_reset_notif,
647};
648
649/* BVC FSM informs us about a BSS-side reset of a PTP BVC */
650static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
651 uint16_t cell_id, uint8_t cause, void *priv)
652{
653 struct gbproxy_bvc *bvc = priv;
654 struct gbproxy_config *cfg = bvc->nse->cfg;
Harald Welte664c24e2020-12-12 15:01:17 +0100655 struct gbproxy_nse *sgsn_nse;
Harald Weltee5209642020-12-05 19:59:45 +0100656 unsigned int i;
657
658 OSMO_ASSERT(bvci != 0);
659
660 if (!bvc->cell) {
661 /* see if we have a CELL dangling around */
662 bvc->cell = gbproxy_cell_by_bvci(cfg, bvci);
663 if (bvc->cell) {
664 /* the CELL already exists. This means either it * was created before at an
665 * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a
666 * malconfiguration */
667 if (bvc->cell->bss_bvc) {
668 LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already "
669 "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei);
670 LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting "
671 "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei);
672 gbproxy_bvc_free(bvc->cell->bss_bvc);
673 }
674 bvc->cell->bss_bvc = bvc;
675 }
676 }
677
678 if (!bvc->cell) {
Harald Weltee5209642020-12-05 19:59:45 +0100679 /* if we end up here, it means this is the first time we received a BVC-RESET
680 * for this BVC. We need to create the 'cell' data structure and the SGSN-side
681 * BVC counterparts */
682
Philipp Maiere4597ec2021-02-09 16:02:00 +0100683 bvc->cell = gbproxy_cell_alloc(cfg, bvci, ra_id, cell_id);
Harald Weltee5209642020-12-05 19:59:45 +0100684 OSMO_ASSERT(bvc->cell);
685
686 /* link us to the cell and vice-versa */
687 bvc->cell->bss_bvc = bvc;
Harald Welte664c24e2020-12-12 15:01:17 +0100688 }
Harald Weltee5209642020-12-05 19:59:45 +0100689
Daniel Willmann6701d272021-04-08 08:39:12 +0200690 /* Ensure we have the correct RA/CELL ID */
691 if (!gsm48_ra_equal(&bvc->cell->id.raid, ra_id)) {
692 LOGPBVC(bvc, LOGL_NOTICE, "RAID changed from %s to %s, updating cell\n", osmo_rai_name(&bvc->cell->id.raid), osmo_rai_name(ra_id));
693 memcpy(&bvc->cell->id.raid, ra_id, sizeof(*ra_id));
694 }
695 if (bvc->cell->id.cid != cell_id) {
696 LOGPBVC(bvc, LOGL_NOTICE, "CellID changed from %05d to %05d, updating cell\n", bvc->cell->id.cid, cell_id);
697 bvc->cell->id.cid = cell_id;
698 }
699
700 /* Reallocate SGSN-side BVCs of the cell, and reset them
701 * Removing and reallocating is needed becaus the ra_id/cell_id might have changed */
Harald Welte664c24e2020-12-12 15:01:17 +0100702 hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) {
703 struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Daniel Willmanndc763fd2021-09-24 16:45:38 +0200704 if (!sgsn_bvc)
705 sgsn_bvc = gbproxy_bvc_by_bvci_inactive(sgsn_nse, bvci);
Harald Welte664c24e2020-12-12 15:01:17 +0100706 if (sgsn_bvc)
Daniel Willmann6701d272021-04-08 08:39:12 +0200707 gbproxy_bvc_free(sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100708
Daniel Willmann6701d272021-04-08 08:39:12 +0200709 sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci);
710 OSMO_ASSERT(sgsn_bvc);
711 sgsn_bvc->cell = bvc->cell;
712 memcpy(&sgsn_bvc->raid, &bvc->cell->id.raid, sizeof(sgsn_bvc->raid));
713 sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei,
714 bvci, ra_id, cell_id);
715 OSMO_ASSERT(sgsn_bvc->fi);
716 bssgp_bvc_fsm_set_max_pdu_len(sgsn_bvc->fi, sgsn_nse->max_sdu_len);
717 bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc);
718 gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100719 }
720
721 /* Trigger outbound BVC-RESET procedure toward each SGSN */
722 dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause);
723}
724
725/* BVC FSM informs us about a BSS-side FSM state change */
726static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv)
727{
728 struct gbproxy_bvc *bvc = priv;
729 struct gbproxy_cell *cell = bvc->cell;
730 uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi);
731
732 /* we have just been created but due to callback ordering the cell is not associated */
733 if (!cell)
734 return;
735
736 switch (state) {
737 case BSSGP_BVCFSM_S_BLOCKED:
738 /* block the corresponding SGSN-side PTP BVCs */
739 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
740 break;
741 case BSSGP_BVCFSM_S_UNBLOCKED:
742 /* unblock the corresponding SGSN-side PTP BVCs */
743 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL);
744 break;
745 }
746}
747
Harald Welte85a40272020-12-08 21:43:22 +0100748/* BVC FSM informs us about BVC-FC PDU receive */
749static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
750{
Harald Welte209dc9f2020-12-12 19:02:16 +0100751 struct bssgp2_flow_ctrl fc_reduced;
Harald Welte85a40272020-12-08 21:43:22 +0100752 struct gbproxy_bvc *bss_bvc = priv;
Harald Welte209dc9f2020-12-12 19:02:16 +0100753 struct gbproxy_cell *cell;
754 struct gbproxy_config *cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100755
Harald Welte209dc9f2020-12-12 19:02:16 +0100756 OSMO_ASSERT(bss_bvc);
757 OSMO_ASSERT(fc);
758
759 cell = bss_bvc->cell;
Harald Welte85a40272020-12-08 21:43:22 +0100760 if (!cell)
761 return;
762
Harald Welte209dc9f2020-12-12 19:02:16 +0100763 cfg = cell->cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100764
Harald Welte209dc9f2020-12-12 19:02:16 +0100765 /* reduce / scale according to configuration to make sure we only advertise a fraction
766 * of the capacity to each of the SGSNs in the pool */
767 fc_reduced = *fc;
768 fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
769 fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
770 /* we don't modify the per-MS related values as any single MS is only served by one SGSN */
771
772 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
Harald Welte85a40272020-12-08 21:43:22 +0100773}
774
Harald Weltee5209642020-12-05 19:59:45 +0100775static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
776 .reset_notification = bss_ptp_bvc_reset_notif,
777 .state_chg_notification = bss_ptp_bvc_state_chg_notif,
Harald Welte85a40272020-12-08 21:43:22 +0100778 .rx_fc_bvc = bss_ptp_bvc_fc_bvc,
Harald Weltee5209642020-12-05 19:59:45 +0100779};
780
781/* BVC FSM informs us about a SGSN-side reset of a PTP BVC */
782static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
783 uint16_t cell_id, uint8_t cause, void *priv)
784{
785 struct gbproxy_bvc *bvc = priv;
786
787 if (!bvc->cell) {
788 LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n");
789 return;
790 }
791
792 OSMO_ASSERT(bvc->cell->bss_bvc);
793
794 /* request reset of BSS-facing PTP-BVC */
795 osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
796}
797
798static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = {
799 .reset_notification = sgsn_ptp_bvc_reset_notif,
800};
801
802/* BVC FSM informs us about a SGSN-side reset of the signaling BVC */
803static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
804 uint16_t cell_id, uint8_t cause, void *priv)
805{
806 struct gbproxy_bvc *bvc = priv;
807 struct gbproxy_config *cfg = bvc->nse->cfg;
808 struct gbproxy_nse *bss_nse;
809 unsigned int i;
810
811 /* delete all SGSN-side PTP BVC for this SGSN */
812 gbproxy_cleanup_bvcs(bvc->nse, 0);
813 /* FIXME: what to do about the cells? */
814 /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */
815
816 /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */
817 hash_for_each(cfg->bss_nses, i, bss_nse, list) {
818 struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0);
819 if (!bss_bvc) {
820 LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n");
821 continue;
822 }
823 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
824 }
825}
826
827const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = {
828 .reset_notification = sgsn_sig_bvc_reset_notif,
829};
830
831/***********************************************************************
832 * Signaling BVC handling
833 ***********************************************************************/
834
835/* process a BVC-RESET message from the BSS side */
836static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp)
837{
838 struct gbproxy_bvc *from_bvc = NULL;
839 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
840 uint32_t features = 0; // FIXME: make configurable
841
842 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci);
843
Harald Welte314647b2020-12-02 23:03:22 +0100844 if (bvci == 0) {
845 /* If we receive a BVC reset on the signalling endpoint, we
846 * don't want the SGSN to reset, as the signalling endpoint
847 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte324f0652020-12-02 23:06:37 +0100848
Harald Weltee5209642020-12-05 19:59:45 +0100849 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
Harald Welte560bdb32020-12-04 22:24:47 +0100850 if (!from_bvc) {
Harald Weltee5209642020-12-05 19:59:45 +0100851 from_bvc = gbproxy_bvc_alloc(nse, 0);
852 OSMO_ASSERT(from_bvc);
853 from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features);
854 if (!from_bvc->fi) {
855 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
856 gbproxy_bvc_free(from_bvc);
857 return -ENOMEM;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100858 }
Daniel Willmanna8b61652021-02-12 05:05:14 +0100859 bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len);
Harald Weltee5209642020-12-05 19:59:45 +0100860 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc);
861 }
862 } else {
863 from_bvc = gbproxy_bvc_by_bvci(nse, bvci);
864 if (!from_bvc) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100865 /* if a PTP-BVC is reset, and we don't know that
Harald Welte560bdb32020-12-04 22:24:47 +0100866 * PTP-BVCI yet, we should allocate a new bvc */
867 from_bvc = gbproxy_bvc_alloc(nse, bvci);
868 OSMO_ASSERT(from_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100869 from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi,
870 nse->nsei, bvci);
871 if (!from_bvc->fi) {
872 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
873 gbproxy_bvc_free(from_bvc);
874 return -ENOMEM;
875 }
Daniel Willmanna8b61652021-02-12 05:05:14 +0100876 bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len);
Harald Weltee5209642020-12-05 19:59:45 +0100877 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100878 }
Harald Weltee5209642020-12-05 19:59:45 +0100879#if 0
Harald Welte7df1e5a2020-12-02 22:53:26 +0100880 /* Could have moved to a different NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100881 if (!check_bvc_nsei(from_bvc, nsei)) {
882 LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100883
Harald Weltee5209642020-12-05 19:59:45 +0100884 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei, false);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100885 if (!nse_new) {
886 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
887 "BVCI=%05u\n", bvci, nsei);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100888 tx_status(nse, ns_bvci, BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100889 return 0;
890 }
891
Harald Welte560bdb32020-12-04 22:24:47 +0100892 /* Move bvc to different NSE */
893 gbproxy_bvc_move(from_bvc, nse_new);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100894 }
Harald Weltee5209642020-12-05 19:59:45 +0100895#endif
896 /* FIXME: do we need this, if it happens within FSM? */
Harald Welte173a1822020-12-03 15:36:59 +0100897 if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100898 struct gprs_ra_id raid;
899 /* We have a Cell Identifier present in this
900 * PDU, this means we can extend our local
901 * state information about this particular cell
902 * */
Philipp Maierda3af942021-02-04 21:54:09 +0100903 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
904 memcpy(&from_bvc->raid, &raid, sizeof(from_bvc->raid));
Harald Welte560bdb32020-12-04 22:24:47 +0100905 LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welte7df1e5a2020-12-02 22:53:26 +0100906 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100907 }
Harald Weltee5209642020-12-05 19:59:45 +0100908 /* hand into FSM for further processing */
909 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
910 return 0;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100911}
912
Philipp Maier1c5766b2021-02-09 17:03:03 +0100913/* Receive an incoming RIM message from a BSS-side NS-VC */
914static int gbprox_rx_rim_from_bss(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
915 const char *pdut_name)
916{
917 struct gbproxy_sgsn *sgsn;
918 struct gbproxy_cell *dest_cell;
919 struct gbproxy_cell *src_cell;
920 struct bssgp_rim_routing_info dest_ri;
921 struct bssgp_rim_routing_info src_ri;
922 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +0100923 char ri_src_str[64];
924 char ri_dest_str[64];
Daniel Willmannf8cba652021-02-12 04:59:47 +0100925 uint16_t ns_bvci = msgb_bvci(msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100926
927 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
928 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
929 if (rc < 0) {
930 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100931 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100932 }
933 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
934 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
935 if (rc < 0) {
936 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100937 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100938 }
939
940 /* Since gbproxy is 2G only we do not expect to get RIM messages only from GERAN cells. */
941 if (src_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
942 LOGP(DGPRS, LOGL_ERROR, "%s %s source RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
943 bssgp_rim_ri_name(&src_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100944 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100945 }
946
947 /* Lookup source cell to make sure that the source RIM routing information actually belongs
948 * to a valid cell that we know */
949 src_cell = gbproxy_cell_by_cellid(nse->cfg, &src_ri.geran.raid, src_ri.geran.cid);
950 if (!src_cell) {
951 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for source RIM routing info (%s)\n", log_pfx,
952 pdut_name, bssgp_rim_ri_name(&src_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100953 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100954 }
955
956 /* TODO: Use bssgp_bvc_get_features_negotiated(src_cell->bss_bvc->fi) to check if the the BSS sided BVC actually
957 * did negotiate RIM support. If not we should respond with a BSSGP STATUS message. The cause code should be
958 * BSSGP_CAUSE_PDU_INCOMP_FEAT. */
959
960 /* If Destination is known by gbproxy, route directly */
961 if (dest_ri.discr == BSSGP_RIM_ROUTING_INFO_GERAN) {
962 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
963 if (dest_cell) {
964 /* TODO: Also check if dest_cell->bss_bvc is RIM-capable (see also above). If not we should
965 * respond with a BSSGP STATUS message as well because it also would make no sense to try
966 * routing the RIM message to the next RIM-capable SGSN. */
Philipp Maier4499cf42021-02-10 17:54:44 +0100967 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to peer (nsei=%u) RIM-PDU: src=%s, dest=%s\n",
968 log_pfx, pdut_name, dest_cell->bss_bvc->nse->nsei,
969 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
970 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100971 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
972 }
973 }
974
975 /* Otherwise pass on to a RIM-capable SGSN */
976 /* TODO: We need to extend gbproxy_select_sgsn() so that it selects a RIM-capable SGSN, at the moment we just
977 * get any SGSN and just assume that it is RIM-capable. */
978 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
979 if (!sgsn) {
980 LOGP(DGPRS, LOGL_NOTICE,
981 "%s %s cannot route RIM message (%s to %s) since no RIM capable SGSN is found!\n", log_pfx,
982 pdut_name, bssgp_rim_ri_name(&src_ri), bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100983 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100984 }
Philipp Maier4499cf42021-02-10 17:54:44 +0100985 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
986 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
987 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
988 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100989
990 return gbprox_relay2nse(msg, sgsn->nse, 0);
991}
992
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200993/* Receive an incoming signalling message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100994static 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 +0200995{
996 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200997 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +0100998 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
Philipp Maier74882dc2021-02-04 16:31:46 +0100999 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001000 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte560bdb32020-12-04 22:24:47 +01001001 struct gbproxy_bvc *from_bvc = NULL;
Harald Welteec0f8012020-12-06 16:32:01 +01001002 char log_pfx[32];
Harald Weltee5209642020-12-05 19:59:45 +01001003 uint16_t ptp_bvci;
1004 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001005 int rc;
1006
Harald Weltee5209642020-12-05 19:59:45 +01001007 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
1008
1009 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001010
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001011 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Weltee5209642020-12-05 19:59:45 +01001012 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 +01001013 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001014 }
1015
Harald Welte278dd272020-12-06 13:35:24 +01001016 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001017 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001018 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001019 }
1020
1021 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001022 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001023 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001024 }
1025
Philipp Maier74882dc2021-02-04 16:31:46 +01001026 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 +01001027 DGPRS, log_pfx);
1028 if (rc < 0) {
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001029 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_BSS));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001030 return tx_status_from_tlvp(nse, rc, msg);
Harald Welteec0f8012020-12-06 16:32:01 +01001031 }
Harald Weltee5209642020-12-05 19:59:45 +01001032 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001033 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001034
Harald Weltee5209642020-12-05 19:59:45 +01001035 /* special case handling for some PDU types */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001036 switch (pdu_type) {
Harald Weltee5209642020-12-05 19:59:45 +01001037 case BSSGP_PDUT_BVC_RESET:
1038 /* resolve or create gbproxy_bvc + handlei n BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001039 return rx_bvc_reset_from_bss(nse, msg, &tp[0]);
Harald Weltee5209642020-12-05 19:59:45 +01001040 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001041 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001042 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
Harald Welte560bdb32020-12-04 22:24:47 +01001043 if (!from_bvc)
1044 goto err_no_bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001045 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1046 case BSSGP_PDUT_BVC_BLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001047 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001048 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1049 if (!from_bvc)
1050 goto err_no_bvc;
1051 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg);
1052 case BSSGP_PDUT_BVC_UNBLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001053 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001054 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1055 if (!from_bvc)
1056 goto err_no_bvc;
1057 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg);
1058 case BSSGP_PDUT_SUSPEND:
1059 case BSSGP_PDUT_RESUME:
Daniel Willmann77493b12020-12-29 21:13:31 +01001060 {
1061 struct gbproxy_sgsn *sgsn;
1062
Philipp Maier74882dc2021-02-04 16:31:46 +01001063 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001064 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
1065 if (!sgsn) {
1066 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN for TLLI, dropping message!\n");
1067 rc = -EINVAL;
1068 break;
1069 }
1070
1071 gbproxy_tlli_cache_update(nse, tlli);
1072
1073 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
Harald Weltee5209642020-12-05 19:59:45 +01001074#if 0
1075 /* TODO: Validate the RAI for consistency with the RAI
1076 * we expect for any of the BVC within this BSS side NSE */
Philipp Maier74882dc2021-02-04 16:31:46 +01001077 memcpy(ra, TLVP_VAL(&tp[0], BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra));
Harald Welte560bdb32020-12-04 22:24:47 +01001078 gsm48_parse_ra(&raid, from_bvc->ra);
Harald Weltee5209642020-12-05 19:59:45 +01001079#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001080 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001081 }
Harald Weltee5209642020-12-05 19:59:45 +01001082 case BSSGP_PDUT_STATUS:
1083 /* FIXME: inspect the erroneous PDU IE (if any) and check
1084 * if we can extract a TLLI/RNI to route it to the correct SGSN */
1085 break;
1086 case BSSGP_PDUT_RAN_INFO:
1087 case BSSGP_PDUT_RAN_INFO_REQ:
1088 case BSSGP_PDUT_RAN_INFO_ACK:
1089 case BSSGP_PDUT_RAN_INFO_ERROR:
1090 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001091 rc = gbprox_rx_rim_from_bss(tp, nse, msg, log_pfx, pdut_name);
Harald Weltee5209642020-12-05 19:59:45 +01001092 break;
1093 case BSSGP_PDUT_LLC_DISCARD:
1094 case BSSGP_PDUT_FLUSH_LL_ACK:
1095 /* route based on BVCI + TLLI */
Philipp Maier74882dc2021-02-04 16:31:46 +01001096 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1097 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Harald Weltee5209642020-12-05 19:59:45 +01001098 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1099 if (!from_bvc)
1100 goto err_no_bvc;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +01001101 gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true);
Harald Weltee5209642020-12-05 19:59:45 +01001102 break;
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001103 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +01001104 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001105 {
1106 /* Route according to IMSI<->NSE cache entry */
1107 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001108 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1109 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001110 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +02001111 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001112 if (!nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001113 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001114 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001115 OSMO_ASSERT(nse->sgsn_facing);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001116 rc = gbprox_relay2nse(msg, nse, 0);
1117 break;
1118 }
Daniel Willmannf024eeb2021-07-06 14:02:41 +02001119 case BSSGP_PDUT_MS_REGISTR_ENQ:
1120 {
1121 struct gbproxy_sgsn *sgsn;
1122 struct osmo_mobile_identity mi;
1123 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1124 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
1125 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1126
1127 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
1128 if (!sgsn) {
1129 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
1130 rc = -EINVAL;
1131 break;
1132 }
1133
1134 gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_MS_REG_ENQ);
1135
1136 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
1137 break;
1138 }
Harald Weltee5209642020-12-05 19:59:45 +01001139 default:
1140 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001141 break;
1142 }
1143
Harald Weltee5209642020-12-05 19:59:45 +01001144 return rc;
Harald Welte560bdb32020-12-04 22:24:47 +01001145err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001146 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 +02001147 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_NSEI));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001148 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001149}
1150
1151/* Receive paging request from SGSN, we need to relay to proper BSS */
Harald Weltedf690e82020-12-12 15:58:28 +01001152static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name,
Daniel Willmann5614e572021-01-18 18:38:27 +01001153 struct tlv_parsed *tp, uint16_t ns_bvci, bool broadcast)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001154{
Harald Weltedf690e82020-12-12 15:58:28 +01001155 struct gbproxy_config *cfg = sgsn_nse->cfg;
Harald Weltee5209642020-12-05 19:59:45 +01001156 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Weltedf690e82020-12-12 15:58:28 +01001157 struct gbproxy_nse *nse;
Daniel Willmann76205712020-11-30 17:08:58 +01001158 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001159 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte8b4c7942020-12-05 10:14:49 +01001160 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001161
Harald Welte173a1822020-12-03 15:36:59 +01001162 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001163 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001164 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Weltedf690e82020-12-12 15:58:28 +01001165 sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001166 if (!sgsn_bvc) {
Harald Weltedf690e82020-12-12 15:58:28 +01001167 LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n",
Harald Weltee5209642020-12-05 19:59:45 +01001168 pdut_name, bvci);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001169 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr));
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001170 return -EINVAL;
1171 }
Harald Weltee5209642020-12-05 19:59:45 +01001172 LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name);
1173 return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte173a1822020-12-03 15:36:59 +01001174 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001175 struct gprs_ra_id raid;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001176 errctr = GBPROX_GLOB_CTR_INV_RAI;
Philipp Maierda3af942021-02-04 21:54:09 +01001177 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte560bdb32020-12-04 22:24:47 +01001178 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001179 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001180 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Philipp Maierda3af942021-02-04 21:54:09 +01001181 if (gsm48_ra_equal(&bss_bvc->raid, &raid)) {
Harald Weltee5209642020-12-05 19:59:45 +01001182 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n",
1183 pdut_name);
1184 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001185 n_nses++;
1186 /* Only send it once to each NSE */
1187 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001188 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001189 }
1190 }
Harald Welte173a1822020-12-03 15:36:59 +01001191 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001192 struct gsm48_ra_id lac;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001193 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte560bdb32020-12-04 22:24:47 +01001194 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001195 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001196 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Philipp Maierda3af942021-02-04 21:54:09 +01001197 gsm48_encode_ra(&lac, &bss_bvc->raid);
1198 if (!memcmp(&lac, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Harald Weltee5209642020-12-05 19:59:45 +01001199 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n",
1200 pdut_name);
1201 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001202 n_nses++;
1203 /* Only send it once to each NSE */
1204 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001205 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001206 }
1207 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001208 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1) || broadcast) {
Harald Welte560bdb32020-12-04 22:24:47 +01001209 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001210 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001211 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1212 LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name);
1213 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001214 n_nses++;
1215 /* Only send it once to each NSE */
1216 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001217 }
Harald Welte53ee2062020-11-24 11:31:13 +01001218 }
1219 } else {
Harald Weltedf690e82020-12-12 15:58:28 +01001220 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n");
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001221 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr));
Harald Welte53ee2062020-11-24 11:31:13 +01001222 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001223
Daniel Willmann76205712020-11-30 17:08:58 +01001224 if (n_nses == 0) {
Harald Weltedf690e82020-12-12 15:58:28 +01001225 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n");
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001226 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, errctr));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001227 return -EINVAL;
1228 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001229 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001230}
1231
1232/* Receive an incoming BVC-RESET message from the SGSN */
Harald Weltee5209642020-12-05 19:59:45 +01001233static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp,
1234 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001235{
Harald Weltee5209642020-12-05 19:59:45 +01001236 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1237 struct gbproxy_bvc *from_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001238
Harald Weltee5209642020-12-05 19:59:45 +01001239 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001240
Harald Weltee5209642020-12-05 19:59:45 +01001241 if (ptp_bvci == 0) {
1242 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
1243 OSMO_ASSERT(from_bvc);
1244 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
1245 } else {
1246 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1247 if (!from_bvc) {
1248 LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001249 rate_ctr_inc(rate_ctr_group_get_ctr(nse->cfg->ctrg, GBPROX_GLOB_CTR_INV_BVCI));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001250 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001251 }
Harald Weltee5209642020-12-05 19:59:45 +01001252 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001253 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001254
1255 return 0;
1256}
1257
Philipp Maier1c5766b2021-02-09 17:03:03 +01001258/* Receive an incoming RIM message from the SGSN-side NS-VC */
1259static int gbprox_rx_rim_from_sgsn(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
1260 const char *pdut_name)
1261{
1262 struct gbproxy_sgsn *sgsn;
1263 struct gbproxy_cell *dest_cell;
1264 struct bssgp_rim_routing_info dest_ri;
1265 struct bssgp_rim_routing_info src_ri;
1266 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +01001267 char ri_src_str[64];
1268 char ri_dest_str[64];
Daniel Willmannf8cba652021-02-12 04:59:47 +01001269 uint16_t ns_bvci = msgb_bvci(msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001270
1271 /* TODO: Reply with STATUS if BSSGP didn't negotiate RIM feature, see also comments in
1272 gbprox_rx_rim_from_bss() */
1273
1274 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
1275 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
1276 if (rc < 0) {
1277 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001278 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001279 }
1280 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
1281 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
1282 if (rc < 0) {
1283 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001284 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001285 }
1286
1287 /* Since gbproxy is 2G only we do not expect to get RIM messages that target non-GERAN cells. */
1288 if (dest_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
1289 LOGP(DGPRS, LOGL_ERROR, "%s %s destination RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
1290 bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001291 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001292 }
1293
1294 /* Lookup destination cell */
1295 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
1296 if (!dest_cell) {
1297 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for destination RIM routing info (%s)\n", log_pfx,
1298 pdut_name, bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001299 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001300 }
1301
1302 /* TODO: Check if the BVC of the destination cell actually did negotiate RIM support, see also comments
1303 * in gbprox_rx_rim_from_bss() */
1304 sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
1305 OSMO_ASSERT(sgsn);
1306
Philipp Maier4499cf42021-02-10 17:54:44 +01001307 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying from SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
1308 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
1309 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
1310 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +01001311
1312 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
1313}
1314
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001315/* Receive an incoming signalling message from the SGSN-side NS-VC */
Harald Weltedbef0aa2020-12-07 17:48:11 +01001316static 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 +02001317{
Harald Weltedbef0aa2020-12-07 17:48:11 +01001318 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001319 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +01001320 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
1321 struct gbproxy_config *cfg = nse->cfg;
1322 struct gbproxy_bvc *sgsn_bvc;
Philipp Maier74882dc2021-02-04 16:31:46 +01001323 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001324 int data_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001325 uint16_t bvci;
Harald Welteec0f8012020-12-06 16:32:01 +01001326 char log_pfx[32];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001327 int rc = 0;
1328 int cause;
Harald Welted2fef952020-12-05 00:31:07 +01001329 int i;
Daniel Willmann5614e572021-01-18 18:38:27 +01001330 bool paging_bc = false;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001331
Harald Weltee5209642020-12-05 19:59:45 +01001332 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
1333
1334 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001335
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001336 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welteec0f8012020-12-06 16:32:01 +01001337 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001338 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001339 }
1340
Harald Welte278dd272020-12-06 13:35:24 +01001341 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001342 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001343 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001344 }
1345
1346 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001347 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001348 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001349 }
1350
Harald Weltedbef0aa2020-12-07 17:48:11 +01001351 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welteec0f8012020-12-06 16:32:01 +01001352
Philipp Maier74882dc2021-02-04 16:31:46 +01001353 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 +01001354 DGPRS, log_pfx);
1355 if (rc < 0) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001356 rc = tx_status_from_tlvp(nse, rc, msg);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001357 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN));
Harald Welteec0f8012020-12-06 16:32:01 +01001358 return rc;
1359 }
Harald Weltee5209642020-12-05 19:59:45 +01001360 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001361 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001362
1363 switch (pdu_type) {
1364 case BSSGP_PDUT_BVC_RESET:
Harald Weltee5209642020-12-05 19:59:45 +01001365 /* resolve or create ggbproxy_bvc + handle in BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001366 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1367 rc = rx_bvc_reset_from_sgsn(nse, msg, &tp[0], ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001368 break;
1369 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001370 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001371 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1372 if (!sgsn_bvc)
1373 goto err_no_bvc;
1374 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1375 break;
1376 case BSSGP_PDUT_BVC_BLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001377 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001378 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
Daniel Willmanndc763fd2021-09-24 16:45:38 +02001379 if (!sgsn_bvc) {
1380 /* Check if BVC was blocked before */
1381 sgsn_bvc = gbproxy_bvc_by_bvci_inactive(nse, bvci);
1382 if (!sgsn_bvc)
1383 goto err_no_bvc;
1384 }
Harald Weltee5209642020-12-05 19:59:45 +01001385 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg);
1386 break;
1387 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001388 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001389 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1390 if (!sgsn_bvc)
1391 goto err_no_bvc;
1392 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg);
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001393 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001394 case BSSGP_PDUT_FLUSH_LL:
1395 /* simple case: BVCI IE is mandatory */
Philipp Maier74882dc2021-02-04 16:31:46 +01001396 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001397 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1398 if (!sgsn_bvc)
1399 goto err_no_bvc;
1400 if (sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1401 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001402 break;
Daniel Willmann5614e572021-01-18 18:38:27 +01001403 case BSSGP_PDUT_DUMMY_PAGING_PS:
1404 /* Routing area is optional in dummy paging and we have nothing else to go by
1405 * so in case it is missing we need to broadcast the paging */
1406 paging_bc = true;
1407 /* fall through */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001408 case BSSGP_PDUT_PAGING_PS:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001409 {
1410 /* Cache the IMSI<->NSE to route PAGING REJECT */
1411 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001412 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1413 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001414 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
Daniel Willmann361d0b52021-07-09 17:44:30 +02001415 gbproxy_imsi_cache_update(nse, mi.imsi, CACHE_USAGE_PAGING);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001416 /* fall through */
1417 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001418 case BSSGP_PDUT_PAGING_CS:
1419 /* process the paging request (LAI/RAI lookup) */
Philipp Maier74882dc2021-02-04 16:31:46 +01001420 rc = gbprox_rx_paging(nse, msg, pdut_name, &tp[0], ns_bvci, paging_bc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001421 break;
1422 case BSSGP_PDUT_STATUS:
1423 /* Some exception has occurred */
Philipp Maier74882dc2021-02-04 16:31:46 +01001424 cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE);
Harald Weltee5209642020-12-05 19:59:45 +01001425 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001426 bssgp_cause_str(cause));
Philipp Maier74882dc2021-02-04 16:31:46 +01001427 if (TLVP_PRES_LEN(&tp[0], BSSGP_IE_BVCI, 2)) {
1428 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001429 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001430 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1431 /* don't send STATUS in response to STATUS if !bvc */
1432 if (sgsn_bvc && sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1433 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001434 } else
1435 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1436 break;
1437 /* those only exist in the SGSN -> BSS direction */
1438 case BSSGP_PDUT_SUSPEND_ACK:
1439 case BSSGP_PDUT_SUSPEND_NACK:
1440 case BSSGP_PDUT_RESUME_ACK:
1441 case BSSGP_PDUT_RESUME_NACK:
Daniel Willmann77493b12020-12-29 21:13:31 +01001442 {
1443 struct gbproxy_nse *nse_peer;
Philipp Maier74882dc2021-02-04 16:31:46 +01001444 uint32_t tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001445
1446 nse_peer = gbproxy_nse_by_tlli(cfg, tlli);
1447 if (!nse_peer) {
1448 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1449 /* TODO: Counter */
Daniel Willmannf8cba652021-02-12 04:59:47 +01001450 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001451 }
1452 /* Delete the entry after we're done */
1453 gbproxy_tlli_cache_remove(cfg, tlli);
1454 LOGPNSE(nse_peer, LOGL_DEBUG, "Rx %s: forwarding\n", pdut_name);
1455 gbprox_relay2nse(msg, nse_peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001456 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001457 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001458 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte7479c4d2020-12-02 20:06:04 +01001459 case BSSGP_PDUT_OVERLOAD:
Harald Weltee5209642020-12-05 19:59:45 +01001460 LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name);
Harald Welte560bdb32020-12-04 22:24:47 +01001461 /* broadcast to all BSS-side bvcs */
Harald Welted2fef952020-12-05 00:31:07 +01001462 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte7479c4d2020-12-02 20:06:04 +01001463 gbprox_relay2nse(msg, nse, 0);
1464 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001465 break;
Harald Weltee5209642020-12-05 19:59:45 +01001466 case BSSGP_PDUT_RAN_INFO:
1467 case BSSGP_PDUT_RAN_INFO_REQ:
1468 case BSSGP_PDUT_RAN_INFO_ACK:
1469 case BSSGP_PDUT_RAN_INFO_ERROR:
1470 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001471 rc = gbprox_rx_rim_from_sgsn(tp, nse, msg, log_pfx, pdut_name);
Pau Espin Pedrola4296342021-05-07 13:33:34 +02001472 break;
Daniel Willmannf024eeb2021-07-06 14:02:41 +02001473 case BSSGP_PDUT_MS_REGISTR_ENQ_RESP:
1474 {
1475 struct gbproxy_nse *nse_peer;
1476 struct osmo_mobile_identity mi;
1477 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1478 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
1479 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1480 nse_peer = gbproxy_nse_by_imsi(cfg, mi.imsi, CACHE_USAGE_MS_REG_ENQ);
1481 if (!nse_peer) {
1482 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1483 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1484 } else if (nse_peer->sgsn_facing) {
1485 LOGPNSE(nse, LOGL_ERROR, "Forwarding %s failed: IMSI cache contains SGSN NSE", pdut_name);
1486 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1487 }
1488 gbproxy_imsi_cache_remove(cfg, mi.imsi, CACHE_USAGE_MS_REG_ENQ);
1489 gbprox_relay2nse(msg, nse_peer, ns_bvci);
1490 break;
1491 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001492 default:
Harald Weltee5209642020-12-05 19:59:45 +01001493 LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001494 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_PROTO_ERR_SGSN));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001495 rc = tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001496 break;
1497 }
1498
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001499 return rc;
Harald Weltee5209642020-12-05 19:59:45 +01001500
Harald Welte560bdb32020-12-04 22:24:47 +01001501err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001502 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC\n", pdut_name);
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001503 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_INV_RAI));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001504 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001505}
1506
Harald Weltee5209642020-12-05 19:59:45 +01001507
1508/***********************************************************************
1509 * libosmogb NS/BSSGP integration
1510 ***********************************************************************/
1511
Alexander Couzens951e1332020-09-22 13:21:46 +02001512int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001513{
1514 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001515 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1516 struct gprs_ns2_inst *nsi = cfg->nsi;
1517 struct osmo_gprs_ns2_prim nsp = {};
1518
1519 nsp.bvci = msgb_bvci(msg);
1520 nsp.nsei = msgb_nsei(msg);
1521
Alexander Couzens55c36f92021-01-27 20:56:55 +01001522 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001523 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1524
1525 return rc;
1526}
1527
1528/* Main input function for Gb proxy */
1529int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1530{
Alexander Couzens951e1332020-09-22 13:21:46 +02001531 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Harald Weltee5209642020-12-05 19:59:45 +01001532 uint16_t ns_bvci = msgb_bvci(msg);
1533 uint16_t nsei = msgb_nsei(msg);
1534 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001535
Harald Weltee5209642020-12-05 19:59:45 +01001536 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN);
1537 if (nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001538 /* ensure minimum length to decode PDU type */
1539 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1540 return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1541
Harald Weltee5209642020-12-05 19:59:45 +01001542 if (ns_bvci == 0 || ns_bvci == 1)
1543 return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001544 else
Harald Weltee5209642020-12-05 19:59:45 +01001545 return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001546 }
1547
Harald Weltee5209642020-12-05 19:59:45 +01001548 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS);
1549 if (!nse) {
1550 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei);
1551 nse = gbproxy_nse_alloc(cfg, nsei, false);
1552 }
1553 if (nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001554 /* ensure minimum length to decode PDU type */
1555 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1556 return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1557
Harald Weltee5209642020-12-05 19:59:45 +01001558 if (ns_bvci == 0 || ns_bvci == 1)
1559 return gbprox_rx_sig_from_bss(nse, msg, ns_bvci);
1560 else
1561 return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci);
1562 }
1563
1564 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001565}
1566
Alexander Couzens951e1332020-09-22 13:21:46 +02001567/* TODO: What about handling:
Alexander Couzens55c36f92021-01-27 20:56:55 +01001568 * GPRS_NS2_AFF_CAUSE_VC_FAILURE,
1569 GPRS_NS2_AFF_CAUSE_VC_RECOVERY,
Alexander Couzens951e1332020-09-22 13:21:46 +02001570 osmocom own causes
Alexander Couzens55c36f92021-01-27 20:56:55 +01001571 GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED,
1572 GPRS_NS2_AFF_CAUSE_SNS_FAILURE,
Alexander Couzens951e1332020-09-22 13:21:46 +02001573 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001574
Alexander Couzens951e1332020-09-22 13:21:46 +02001575void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001576{
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001577 int i;
Harald Welte560bdb32020-12-04 22:24:47 +01001578 struct gbproxy_bvc *bvc;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001579 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001580
Alexander Couzens951e1332020-09-22 13:21:46 +02001581 switch (nsp->u.status.cause) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001582 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
1583 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens951e1332020-09-22 13:21:46 +02001584 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001585
Alexander Couzens55c36f92021-01-27 20:56:55 +01001586 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Harald Welte9b367d22021-01-18 13:55:51 +01001587 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Daniel Willmannf96cac52021-03-09 16:14:18 +01001588 nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN);
1589 if (nse) {
Daniel Willmann37518b32021-05-27 18:13:36 +02001590 nse->alive = true;
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001591 // Update the NSE max SDU len
Daniel Willmannf96cac52021-03-09 16:14:18 +01001592 nse->max_sdu_len = nsp->u.status.mtu;
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001593
Harald Weltee5209642020-12-05 19:59:45 +01001594 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001595 bvc = gbproxy_bvc_by_bvci(nse, 0);
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001596 if (bvc) {
Daniel Willmannf96cac52021-03-09 16:14:18 +01001597 bssgp_bvc_fsm_set_max_pdu_len(bvc->fi, nse->max_sdu_len);
Daniel Willmann3ea37932021-02-10 13:41:14 +01001598 osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
Daniel Willmann38b9c9a2021-03-09 15:54:44 +01001599 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001600 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001601 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001602 case GPRS_NS2_AFF_CAUSE_FAILURE:
Daniel Willmannf96cac52021-03-09 16:14:18 +01001603 nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_BSS | NSE_F_SGSN);
1604 if (!nse) {
1605 LOGP(DGPRS, LOGL_ERROR, "Unknown NSE(%05d) became unavailable\n", nsp->nsei);
1606 break;
1607 }
Daniel Willmann37518b32021-05-27 18:13:36 +02001608
1609 nse->alive = false;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001610 if (nse->sgsn_facing) {
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001611 struct hlist_node *ntmp;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001612 /* SGSN */
1613 /* TODO: When to block all PtP towards bss? Only if all SGSN are down? */
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001614 hash_for_each_safe(nse->bvcs, i, ntmp, bvc, list) {
1615 if (bvc->bvci == 0)
1616 continue;
1617 gbproxy_bvc_free(bvc);
1618 }
Pau Espin Pedrol56438362021-06-04 18:03:44 +02001619 rate_ctr_inc(rate_ctr_group_get_ctr(cfg->ctrg, GBPROX_GLOB_CTR_RESTART_RESET_SGSN));
Alexander Couzens951e1332020-09-22 13:21:46 +02001620 } else {
Daniel Willmannf96cac52021-03-09 16:14:18 +01001621 /* BSS became unavailable
1622 * Block matching PtP-BVCs on SGSN-side */
1623 hash_for_each(nse->bvcs, i, bvc, list) {
1624 if (bvc->bvci == 0)
1625 continue;
1626 /* Get BVC for each SGSN and send block request */
1627 struct gbproxy_cell *cell = bvc->cell;
1628 for (int j = 0; j < GBPROXY_MAX_NR_SGSN; j++) {
1629 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[j];
1630 if (!sgsn_bvc)
1631 continue;
1632
1633 /* Block BVC, indicate BSS equipment failure */
1634 uint8_t cause = BSSGP_CAUSE_EQUIP_FAIL;
1635 osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Daniel Willmanndc763fd2021-09-24 16:45:38 +02001636 sgsn_bvc->inactive = true;
Daniel Willmannf96cac52021-03-09 16:14:18 +01001637 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001638 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001639
Daniel Willmann1f3470f2021-03-11 09:53:42 +01001640 /* This frees the BVCs for us as well */
1641 gbproxy_nse_free(nse);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001642 }
Harald Welte9b367d22021-01-18 13:55:51 +01001643 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Alexander Couzens951e1332020-09-22 13:21:46 +02001644 break;
1645 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001646 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001647 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001648 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001649 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001650}
1651
Alexander Couzens951e1332020-09-22 13:21:46 +02001652/* called by the ns layer */
1653int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1654{
1655 struct osmo_gprs_ns2_prim *nsp;
1656 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001657 uintptr_t bvci;
Alexander Couzens951e1332020-09-22 13:21:46 +02001658 int rc = 0;
1659
1660 if (oph->sap != SAP_NS)
1661 return 0;
1662
1663 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1664
1665 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte9b367d22021-01-18 13:55:51 +01001666 LOGP(DGPRS, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001667 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001668 return 0;
1669 }
1670
1671 switch (oph->primitive) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001672 case GPRS_NS2_PRIM_UNIT_DATA:
Daniel Willmann8f407b12020-12-02 19:33:50 +01001673
Alexander Couzens951e1332020-09-22 13:21:46 +02001674 /* hand the message into the BSSGP implementation */
1675 msgb_bssgph(oph->msg) = oph->msg->l3h;
1676 msgb_bvci(oph->msg) = nsp->bvci;
1677 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001678 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens951e1332020-09-22 13:21:46 +02001679
Daniel Willmann8f407b12020-12-02 19:33:50 +01001680 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens951e1332020-09-22 13:21:46 +02001681 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001682 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001683 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001684 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens951e1332020-09-22 13:21:46 +02001685 gprs_ns_prim_status_cb(cfg, nsp);
1686 break;
1687 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001688 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001689 gprs_ns2_prim_str(oph->primitive),
1690 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001691 break;
1692 }
1693
1694 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001695}
1696
1697void gbprox_reset(struct gbproxy_config *cfg)
1698{
Harald Welted2fef952020-12-05 00:31:07 +01001699 struct gbproxy_nse *nse;
1700 struct hlist_node *ntmp;
Harald Welte8b4c7942020-12-05 10:14:49 +01001701 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001702
Harald Welted2fef952020-12-05 00:31:07 +01001703 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte8b4c7942020-12-05 10:14:49 +01001704 struct gbproxy_bvc *bvc;
1705 struct hlist_node *tmp;
1706 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +01001707 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001708
1709 gbproxy_nse_free(nse);
1710 }
Harald Weltee5209642020-12-05 19:59:45 +01001711 /* FIXME: cells */
1712 /* FIXME: SGSN side BVCs (except signaling) */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001713
1714 rate_ctr_group_free(cfg->ctrg);
1715 gbproxy_init_config(cfg);
1716}
1717
Daniel Willmann77493b12020-12-29 21:13:31 +01001718static void tlli_cache_cleanup(void *data)
1719{
1720 struct gbproxy_config *cfg = data;
1721 gbproxy_tlli_cache_cleanup(cfg);
1722
1723 /* TODO: Disable timer when cache is empty */
1724 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1725}
1726
Daniel Willmannc8a50092021-01-17 13:11:41 +01001727static void imsi_cache_cleanup(void *data)
1728{
1729 struct gbproxy_config *cfg = data;
1730 gbproxy_imsi_cache_cleanup(cfg);
1731
1732 /* TODO: Disable timer when cache is empty */
1733 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1734}
1735
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001736int gbproxy_init_config(struct gbproxy_config *cfg)
1737{
1738 struct timespec tp;
1739
Harald Welte209dc9f2020-12-12 19:02:16 +01001740 /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
1741 cfg->pool.bvc_fc_ratio = 100;
Daniel Willmannee834af2020-12-14 16:22:39 +01001742 cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001743 /* TODO: Make configurable */
Daniel Willmannbd12f3f2021-01-13 18:16:04 +01001744 cfg->tlli_cache.timeout = 10;
Daniel Willmannc8a50092021-01-17 13:11:41 +01001745 cfg->imsi_cache.timeout = 10;
Daniel Willmannee834af2020-12-14 16:22:39 +01001746
Harald Welted2fef952020-12-05 00:31:07 +01001747 hash_init(cfg->bss_nses);
Daniel Willmann1e7be5d2020-12-21 18:08:21 +01001748 hash_init(cfg->sgsn_nses);
1749 hash_init(cfg->cells);
Daniel Willmann77493b12020-12-29 21:13:31 +01001750 hash_init(cfg->tlli_cache.entries);
Daniel Willmannee834af2020-12-14 16:22:39 +01001751 INIT_LLIST_HEAD(&cfg->sgsns);
1752
Daniel Willmann77493b12020-12-29 21:13:31 +01001753 osmo_timer_setup(&cfg->tlli_cache.timer, tlli_cache_cleanup, cfg);
1754 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1755
Daniel Willmannc8a50092021-01-17 13:11:41 +01001756 /* We could also combine both timers */
1757 osmo_timer_setup(&cfg->imsi_cache.timer, imsi_cache_cleanup, cfg);
1758 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1759
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001760 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1761 if (!cfg->ctrg) {
1762 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1763 return -1;
1764 }
1765 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Harald Weltec169de42020-12-07 13:12:13 +01001766 osmo_fsm_log_timeouts(true);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001767
1768 return 0;
Oliver Smith29532c22021-01-29 11:13:00 +01001769}