blob: 78e1e1de63cb27e34c20b78fb6ff6ce5d080e6c1 [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"
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020055#include <osmocom/sgsn/gb_proxy.h>
56
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)
148 rate_ctr_inc(&cfg->ctrg->ctr[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)
Harald Welte560bdb32020-12-04 22:24:47 +0100224 // rate_ctr_inc(&bvc->ctrg->ctr[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)
Harald Welte560bdb32020-12-04 22:24:47 +0100239 rate_ctr_inc(&bvc->ctrg->ctr[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) {
268 sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);
269 if (!sgsn) {
270 return NULL;
271 }
272 LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first SGSN\n");
273 return sgsn;
274 }
275
276 if (cfg->pool.nri_bitlen == 0) {
277 /* Pooling is disabled */
278 sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);
279 if (!sgsn) {
280 return NULL;
281 }
282
283 LOGPSGSN(sgsn, LOGL_INFO, "Pooling disabled, using first configured SGSN\n");
284 } else {
285 /* Pooling is enabled, try to use the NRI for routing to an SGSN
286 * See 3GPP TS 23.236 Ch. 5.3.2 */
287 tlli_type = gprs_tlli_type(*tlli);
288 if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) {
289 /* Only get/use the NRI if tlli type is local */
290 osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen);
291 if (nri >= 0) {
292 /* Get the SGSN for the NRI */
293 sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri);
294 if (sgsn && !null_nri)
295 return sgsn;
296 /* If the NRI is the null NRI, we need to avoid the chosen SGSN */
297 if (null_nri && sgsn) {
298 sgsn_avoid = sgsn;
299 }
300 } else {
301 /* We couldn't get the NRI from the TLLI */
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100302 LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %08x\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100303 }
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100304 } else {
305 LOGP(DGPRS, LOGL_INFO, "TLLI %08x is neither local nor foreign, not routing by NRI\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100306 }
307 }
308
309 /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid
310 * NOTE: This function is not stable if the number of SGSNs or allow_attach changes
311 * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that
312 * we can just wait for the MS to reattempt the procedure.
313 */
314 if (!sgsn)
315 sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli);
316
317 if (!sgsn) {
318 LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli);
319 return NULL;
320 }
321
322 return sgsn;
323}
324
325/*! Find the correct gbproxy_bvc given a cell and an SGSN
326 * \param[in] cfg The gbproxy configuration
327 * \param[in] cell The cell the message belongs to
328 * \param[in] tlli An optional TLLI used for tracking
329 * \return Returns 0 on success, otherwise a negative value
330 */
331static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli)
332{
333 struct gbproxy_sgsn *sgsn;
334 struct gbproxy_bvc *sgsn_bvc = NULL;
Harald Welte02d7c482020-12-30 12:13:36 +0100335 int i;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100336
337 sgsn = gbproxy_select_sgsn(cfg, tlli);
338 if (!sgsn) {
339 LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
340 return NULL;
341 }
342
343 /* Get the BVC for this SGSN/NSE */
Harald Welte02d7c482020-12-30 12:13:36 +0100344 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100345 sgsn_bvc = cell->sgsn_bvc[i];
346 if (!sgsn_bvc)
347 continue;
348 if (sgsn->nse != sgsn_bvc->nse)
349 continue;
350
351 return sgsn_bvc;
352 }
353
354 /* This shouldn't happen */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100355 LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN %s, dropping message!\n", sgsn->name);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100356 return NULL;
357}
358
359/*! Send a message to the next SGSN, possibly ignoring the null SGSN
360 * route an uplink message on a PTP-BVC to a SGSN using the TLLI
361 * \param[in] cell The cell the message belongs to
362 * \param[in] msg The BSSGP message
363 * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI)
364 * \param[in] tlli An optional TLLI used for tracking
365 * \return Returns 0 on success, otherwise a negative value
366 */
367static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli,
Harald Weltee5209642020-12-05 19:59:45 +0100368 bool sig_bvci)
369{
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100370 struct gbproxy_config *cfg = cell->cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100371 struct gbproxy_bvc *sgsn_bvc;
Harald Weltee5209642020-12-05 19:59:45 +0100372
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100373 sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli);
374 if (!sgsn_bvc) {
375 LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli);
376 return -EINVAL;
Harald Weltee5209642020-12-05 19:59:45 +0100377 }
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100378
379 return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);
Harald Weltee5209642020-12-05 19:59:45 +0100380}
381
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200382/* Receive an incoming PTP message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100383static 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 +0200384{
Harald Welte278dd272020-12-06 13:35:24 +0100385 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100386 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
387 struct gbproxy_bvc *bss_bvc;
388 struct tlv_parsed tp;
389 char log_pfx[32];
390 uint32_t tlli;
391 int rc;
392
393 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
394
395 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200396
Daniel Willmann06331ac2020-12-10 17:59:46 +0100397 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100398 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100399 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100400 }
401
402 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100403 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100404 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100405 }
406
407 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100408 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100409 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100410 }
411
Harald Weltee5209642020-12-05 19:59:45 +0100412 bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
413 if (!bss_bvc) {
414 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n",
415 log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100416 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200417 }
418
Harald Weltee5209642020-12-05 19:59:45 +0100419 /* UL_UNITDATA has a different header than all other uplink PDUs */
420 if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA) {
421 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
422 if (msgb_bssgp_len(msg) < sizeof(*budh))
Daniel Willmannf8cba652021-02-12 04:59:47 +0100423 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100424 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
425 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
426 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
427 * doesn't have to worry where the TLLI came from */
428 tp.lv[BSSGP_IE_TLLI].len = 4;
429 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
430 } else {
431 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
432 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
433 }
434 if (rc < 0) {
435 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100436 return tx_status_from_tlvp(nse, rc, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100437 }
Harald Welte85a40272020-12-08 21:43:22 +0100438 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
439 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200440
Harald Weltee5209642020-12-05 19:59:45 +0100441 switch (bgph->pdu_type) {
442 case BSSGP_PDUT_UL_UNITDATA:
443 case BSSGP_PDUT_RA_CAPA_UPDATE:
444 case BSSGP_PDUT_FLOW_CONTROL_MS:
445 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
446 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
447 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
448 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
449 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
450 case BSSGP_PDUT_FLOW_CONTROL_PFC:
451 case BSSGP_PDUT_DELETE_BSS_PFC_REQ:
452 case BSSGP_PDUT_PS_HO_REQUIRED:
453 case BSSGP_PDUT_PS_HO_REQUEST_ACK:
454 case BSSGP_PDUT_PS_HO_REQUEST_NACK:
455 case BSSGP_PDUT_PS_HO_COMPLETE:
456 case BSSGP_PDUT_PS_HO_CANCEL:
457 /* We can route based on TLLI-NRI */
458 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100459 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100460 break;
461 case BSSGP_PDUT_RADIO_STATUS:
462 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
463 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100464 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100465 } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {
466 /* we treat the TMSI like a TLLI and extract the NRI from it */
467 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Daniel Willmann8b3ed292021-01-21 18:46:51 +0100468 /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */
469 tlli = gprs_tmsi2tlli(tlli, TLLI_FOREIGN);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100470 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100471 } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {
Daniel Willmann5193f222021-01-11 05:00:46 +0100472 /* FIXME: Use the IMSI as selector? */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100473 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false);
Harald Weltee5209642020-12-05 19:59:45 +0100474 } else
475 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");
476 break;
477 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
478 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +0100479 {
480 /* Route according to IMSI<->NSE cache entry */
481 struct osmo_mobile_identity mi;
482 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
483 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
484 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
485 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi);
486 if (nse) {
487 OSMO_ASSERT(nse->sgsn_facing);
488 rc = gbprox_relay2nse(msg, nse, ns_bvci);
489 } else {
Daniel Willmann82669182021-01-19 11:37:55 +0100490 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx unmatched %s with IMSI %s\n", pdut_name, mi.imsi);
Daniel Willmann5614e572021-01-18 18:38:27 +0100491 }
Harald Weltee5209642020-12-05 19:59:45 +0100492 break;
Daniel Willmann5614e572021-01-18 18:38:27 +0100493 }
Harald Weltee5209642020-12-05 19:59:45 +0100494 case BSSGP_PDUT_FLOW_CONTROL_BVC:
Harald Welte85a40272020-12-08 21:43:22 +0100495 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100496 break;
497 case BSSGP_PDUT_STATUS:
498 /* TODO: Implement by inspecting the contained PDU */
499 if (!TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR))
500 break;
501 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
502 break;
503 }
504
505 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200506}
507
508/* Receive an incoming PTP message from a SGSN-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100509static 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 +0200510{
Harald Welte278dd272020-12-06 13:35:24 +0100511 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100512 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
513 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte85a40272020-12-08 21:43:22 +0100514 struct tlv_parsed tp;
Harald Weltee5209642020-12-05 19:59:45 +0100515 char log_pfx[32];
Harald Welte85a40272020-12-08 21:43:22 +0100516 int rc;
Harald Weltee5209642020-12-05 19:59:45 +0100517
518 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
519
520 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200521
Daniel Willmann06331ac2020-12-10 17:59:46 +0100522 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100523 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100524 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100525 }
526
527 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100528 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100529 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100530 }
531
532 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100533 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100534 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +0100535 }
536
Harald Weltee5209642020-12-05 19:59:45 +0100537 sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
538 if (!sgsn_bvc) {
539 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n",
540 log_pfx, pdut_name);
541 rate_ctr_inc(&nse->cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100542 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200543 }
544
Harald Weltee5209642020-12-05 19:59:45 +0100545 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) {
546 LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name);
547 rate_ctr_inc(&sgsn_bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100548 return tx_status(nse, ns_bvci, BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200549 }
Harald Welte85a40272020-12-08 21:43:22 +0100550
551 /* DL_UNITDATA has a different header than all other uplink PDUs */
552 if (bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) {
553 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
554 if (msgb_bssgp_len(msg) < sizeof(*budh))
Daniel Willmannf8cba652021-02-12 04:59:47 +0100555 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte85a40272020-12-08 21:43:22 +0100556 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
557 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
558 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
559 * doesn't have to worry where the TLLI came from */
560 tp.lv[BSSGP_IE_TLLI].len = 4;
561 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
562 } else {
563 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
564 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
565 }
566 if (rc < 0) {
567 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100568 return tx_status_from_tlvp(nse, rc, msg);
Harald Welte85a40272020-12-08 21:43:22 +0100569 }
570 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
571 msgb_bcid(msg) = (void *)&tp;
572
Harald Weltee5209642020-12-05 19:59:45 +0100573 OSMO_ASSERT(sgsn_bvc->cell);
574 bss_bvc = sgsn_bvc->cell->bss_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200575
Harald Welte85a40272020-12-08 21:43:22 +0100576 switch (bgph->pdu_type) {
577 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
578 return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg);
Daniel Willmann5614e572021-01-18 18:38:27 +0100579 case BSSGP_PDUT_DUMMY_PAGING_PS:
580 case BSSGP_PDUT_PAGING_PS:
581 {
582 /* Cache the IMSI<->NSE to route PAGING REJECT */
583 struct osmo_mobile_identity mi;
584 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
585 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
586 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
587 gbproxy_imsi_cache_update(nse, mi.imsi);
588 break;
Harald Welte85a40272020-12-08 21:43:22 +0100589 }
Daniel Willmann5614e572021-01-18 18:38:27 +0100590 default:
591 break;
592 }
593 return gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci);
Harald Welte85a40272020-12-08 21:43:22 +0100594
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200595}
596
Harald Weltee5209642020-12-05 19:59:45 +0100597/***********************************************************************
598 * BVC FSM call-backs
599 ***********************************************************************/
Harald Welte7df1e5a2020-12-02 22:53:26 +0100600
Harald Weltee5209642020-12-05 19:59:45 +0100601/* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */
602static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv)
603{
604 unsigned int i;
605
606 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
607 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i];
608 if (!sgsn_bvc)
609 continue;
610 osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv);
611 }
612}
613
614/* BVC FSM informs us about a BSS-side reset of the signaling BVC */
615static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
616 uint16_t cell_id, uint8_t cause, void *priv)
617{
618 struct gbproxy_bvc *sig_bvc = priv;
619 struct gbproxy_nse *nse = sig_bvc->nse;
620 struct gbproxy_bvc *ptp_bvc;
621 unsigned int i;
622
623 /* BLOCK all SGSN-side PTP BVC within this NSE */
624 hash_for_each(nse->bvcs, i, ptp_bvc, list) {
625 if (ptp_bvc == sig_bvc)
626 continue;
627 OSMO_ASSERT(ptp_bvc->cell);
628
629 dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Harald Weltef9e149b2020-12-02 23:29:38 +0100630 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100631
Harald Weltee5209642020-12-05 19:59:45 +0100632 /* Delete all BSS-side PTP BVC within this NSE */
633 gbproxy_cleanup_bvcs(nse, 0);
634
635 /* TODO: we keep the "CELL" around for now, re-connecting it to
636 * any (later) new PTP-BVC for that BVCI. Not sure if that's the
637 * best idea ? */
638}
639
640/* forward declaration */
641static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops;
642
643static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = {
644 .reset_notification = bss_sig_bvc_reset_notif,
645};
646
647/* BVC FSM informs us about a BSS-side reset of a PTP BVC */
648static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
649 uint16_t cell_id, uint8_t cause, void *priv)
650{
651 struct gbproxy_bvc *bvc = priv;
652 struct gbproxy_config *cfg = bvc->nse->cfg;
Harald Welte664c24e2020-12-12 15:01:17 +0100653 struct gbproxy_nse *sgsn_nse;
Harald Weltee5209642020-12-05 19:59:45 +0100654 unsigned int i;
655
656 OSMO_ASSERT(bvci != 0);
657
658 if (!bvc->cell) {
659 /* see if we have a CELL dangling around */
660 bvc->cell = gbproxy_cell_by_bvci(cfg, bvci);
661 if (bvc->cell) {
662 /* the CELL already exists. This means either it * was created before at an
663 * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a
664 * malconfiguration */
665 if (bvc->cell->bss_bvc) {
666 LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already "
667 "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei);
668 LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting "
669 "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei);
670 gbproxy_bvc_free(bvc->cell->bss_bvc);
671 }
672 bvc->cell->bss_bvc = bvc;
673 }
674 }
675
676 if (!bvc->cell) {
Harald Weltee5209642020-12-05 19:59:45 +0100677 /* if we end up here, it means this is the first time we received a BVC-RESET
678 * for this BVC. We need to create the 'cell' data structure and the SGSN-side
679 * BVC counterparts */
680
Philipp Maiere4597ec2021-02-09 16:02:00 +0100681 bvc->cell = gbproxy_cell_alloc(cfg, bvci, ra_id, cell_id);
Harald Weltee5209642020-12-05 19:59:45 +0100682 OSMO_ASSERT(bvc->cell);
683
684 /* link us to the cell and vice-versa */
685 bvc->cell->bss_bvc = bvc;
Harald Welte664c24e2020-12-12 15:01:17 +0100686 }
Harald Weltee5209642020-12-05 19:59:45 +0100687
Harald Welte664c24e2020-12-12 15:01:17 +0100688 /* allocate (any missing) SGSN-side BVCs within the cell, and reset them */
689 hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) {
690 struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
691 if (sgsn_bvc)
692 OSMO_ASSERT(sgsn_bvc->cell == bvc->cell || !sgsn_bvc->cell);
Harald Weltee5209642020-12-05 19:59:45 +0100693
Harald Welte664c24e2020-12-12 15:01:17 +0100694 if (!sgsn_bvc) {
695 sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci);
696 OSMO_ASSERT(sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100697
Harald Welte664c24e2020-12-12 15:01:17 +0100698 sgsn_bvc->cell = bvc->cell;
Philipp Maierda3af942021-02-04 21:54:09 +0100699 memcpy(&sgsn_bvc->raid, &bvc->cell->id.raid, sizeof(sgsn_bvc->raid));
Harald Welte664c24e2020-12-12 15:01:17 +0100700 sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei,
701 bvci, ra_id, cell_id);
702 OSMO_ASSERT(sgsn_bvc->fi);
Daniel Willmanna8b61652021-02-12 05:05:14 +0100703 bssgp_bvc_fsm_set_max_pdu_len(sgsn_bvc->fi, sgsn_nse->max_sdu_len);
Harald Welte664c24e2020-12-12 15:01:17 +0100704 bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100705
Harald Welte664c24e2020-12-12 15:01:17 +0100706 gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100707 }
708 }
709
710 /* Trigger outbound BVC-RESET procedure toward each SGSN */
711 dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause);
712}
713
714/* BVC FSM informs us about a BSS-side FSM state change */
715static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv)
716{
717 struct gbproxy_bvc *bvc = priv;
718 struct gbproxy_cell *cell = bvc->cell;
719 uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi);
720
721 /* we have just been created but due to callback ordering the cell is not associated */
722 if (!cell)
723 return;
724
725 switch (state) {
726 case BSSGP_BVCFSM_S_BLOCKED:
727 /* block the corresponding SGSN-side PTP BVCs */
728 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
729 break;
730 case BSSGP_BVCFSM_S_UNBLOCKED:
731 /* unblock the corresponding SGSN-side PTP BVCs */
732 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL);
733 break;
734 }
735}
736
Harald Welte85a40272020-12-08 21:43:22 +0100737/* BVC FSM informs us about BVC-FC PDU receive */
738static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
739{
Harald Welte209dc9f2020-12-12 19:02:16 +0100740 struct bssgp2_flow_ctrl fc_reduced;
Harald Welte85a40272020-12-08 21:43:22 +0100741 struct gbproxy_bvc *bss_bvc = priv;
Harald Welte209dc9f2020-12-12 19:02:16 +0100742 struct gbproxy_cell *cell;
743 struct gbproxy_config *cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100744
Harald Welte209dc9f2020-12-12 19:02:16 +0100745 OSMO_ASSERT(bss_bvc);
746 OSMO_ASSERT(fc);
747
748 cell = bss_bvc->cell;
Harald Welte85a40272020-12-08 21:43:22 +0100749 if (!cell)
750 return;
751
Harald Welte209dc9f2020-12-12 19:02:16 +0100752 cfg = cell->cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100753
Harald Welte209dc9f2020-12-12 19:02:16 +0100754 /* reduce / scale according to configuration to make sure we only advertise a fraction
755 * of the capacity to each of the SGSNs in the pool */
756 fc_reduced = *fc;
757 fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
758 fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
759 /* we don't modify the per-MS related values as any single MS is only served by one SGSN */
760
761 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
Harald Welte85a40272020-12-08 21:43:22 +0100762}
763
Harald Weltee5209642020-12-05 19:59:45 +0100764static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
765 .reset_notification = bss_ptp_bvc_reset_notif,
766 .state_chg_notification = bss_ptp_bvc_state_chg_notif,
Harald Welte85a40272020-12-08 21:43:22 +0100767 .rx_fc_bvc = bss_ptp_bvc_fc_bvc,
Harald Weltee5209642020-12-05 19:59:45 +0100768};
769
770/* BVC FSM informs us about a SGSN-side reset of a PTP BVC */
771static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
772 uint16_t cell_id, uint8_t cause, void *priv)
773{
774 struct gbproxy_bvc *bvc = priv;
775
776 if (!bvc->cell) {
777 LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n");
778 return;
779 }
780
781 OSMO_ASSERT(bvc->cell->bss_bvc);
782
783 /* request reset of BSS-facing PTP-BVC */
784 osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
785}
786
787static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = {
788 .reset_notification = sgsn_ptp_bvc_reset_notif,
789};
790
791/* BVC FSM informs us about a SGSN-side reset of the signaling BVC */
792static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
793 uint16_t cell_id, uint8_t cause, void *priv)
794{
795 struct gbproxy_bvc *bvc = priv;
796 struct gbproxy_config *cfg = bvc->nse->cfg;
797 struct gbproxy_nse *bss_nse;
798 unsigned int i;
799
800 /* delete all SGSN-side PTP BVC for this SGSN */
801 gbproxy_cleanup_bvcs(bvc->nse, 0);
802 /* FIXME: what to do about the cells? */
803 /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */
804
805 /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */
806 hash_for_each(cfg->bss_nses, i, bss_nse, list) {
807 struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0);
808 if (!bss_bvc) {
809 LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n");
810 continue;
811 }
812 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
813 }
814}
815
816const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = {
817 .reset_notification = sgsn_sig_bvc_reset_notif,
818};
819
820/***********************************************************************
821 * Signaling BVC handling
822 ***********************************************************************/
823
824/* process a BVC-RESET message from the BSS side */
825static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp)
826{
827 struct gbproxy_bvc *from_bvc = NULL;
828 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
829 uint32_t features = 0; // FIXME: make configurable
830
831 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci);
832
Harald Welte314647b2020-12-02 23:03:22 +0100833 if (bvci == 0) {
834 /* If we receive a BVC reset on the signalling endpoint, we
835 * don't want the SGSN to reset, as the signalling endpoint
836 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte324f0652020-12-02 23:06:37 +0100837
Harald Weltee5209642020-12-05 19:59:45 +0100838 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
Harald Welte560bdb32020-12-04 22:24:47 +0100839 if (!from_bvc) {
Harald Weltee5209642020-12-05 19:59:45 +0100840 from_bvc = gbproxy_bvc_alloc(nse, 0);
841 OSMO_ASSERT(from_bvc);
842 from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features);
843 if (!from_bvc->fi) {
844 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
845 gbproxy_bvc_free(from_bvc);
846 return -ENOMEM;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100847 }
Daniel Willmanna8b61652021-02-12 05:05:14 +0100848 bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len);
Harald Weltee5209642020-12-05 19:59:45 +0100849 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc);
850 }
851 } else {
852 from_bvc = gbproxy_bvc_by_bvci(nse, bvci);
853 if (!from_bvc) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100854 /* if a PTP-BVC is reset, and we don't know that
Harald Welte560bdb32020-12-04 22:24:47 +0100855 * PTP-BVCI yet, we should allocate a new bvc */
856 from_bvc = gbproxy_bvc_alloc(nse, bvci);
857 OSMO_ASSERT(from_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100858 from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi,
859 nse->nsei, bvci);
860 if (!from_bvc->fi) {
861 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
862 gbproxy_bvc_free(from_bvc);
863 return -ENOMEM;
864 }
Daniel Willmanna8b61652021-02-12 05:05:14 +0100865 bssgp_bvc_fsm_set_max_pdu_len(from_bvc->fi, nse->max_sdu_len);
Harald Weltee5209642020-12-05 19:59:45 +0100866 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100867 }
Harald Weltee5209642020-12-05 19:59:45 +0100868#if 0
Harald Welte7df1e5a2020-12-02 22:53:26 +0100869 /* Could have moved to a different NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100870 if (!check_bvc_nsei(from_bvc, nsei)) {
871 LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100872
Harald Weltee5209642020-12-05 19:59:45 +0100873 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei, false);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100874 if (!nse_new) {
875 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
876 "BVCI=%05u\n", bvci, nsei);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100877 tx_status(nse, ns_bvci, BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100878 return 0;
879 }
880
Harald Welte560bdb32020-12-04 22:24:47 +0100881 /* Move bvc to different NSE */
882 gbproxy_bvc_move(from_bvc, nse_new);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100883 }
Harald Weltee5209642020-12-05 19:59:45 +0100884#endif
885 /* FIXME: do we need this, if it happens within FSM? */
Harald Welte173a1822020-12-03 15:36:59 +0100886 if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100887 struct gprs_ra_id raid;
888 /* We have a Cell Identifier present in this
889 * PDU, this means we can extend our local
890 * state information about this particular cell
891 * */
Philipp Maierda3af942021-02-04 21:54:09 +0100892 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
893 memcpy(&from_bvc->raid, &raid, sizeof(from_bvc->raid));
Harald Welte560bdb32020-12-04 22:24:47 +0100894 LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welte7df1e5a2020-12-02 22:53:26 +0100895 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100896 }
Harald Weltee5209642020-12-05 19:59:45 +0100897 /* hand into FSM for further processing */
898 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
899 return 0;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100900}
901
Philipp Maier1c5766b2021-02-09 17:03:03 +0100902/* Receive an incoming RIM message from a BSS-side NS-VC */
903static int gbprox_rx_rim_from_bss(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
904 const char *pdut_name)
905{
906 struct gbproxy_sgsn *sgsn;
907 struct gbproxy_cell *dest_cell;
908 struct gbproxy_cell *src_cell;
909 struct bssgp_rim_routing_info dest_ri;
910 struct bssgp_rim_routing_info src_ri;
911 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +0100912 char ri_src_str[64];
913 char ri_dest_str[64];
Daniel Willmannf8cba652021-02-12 04:59:47 +0100914 uint16_t ns_bvci = msgb_bvci(msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100915
916 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
917 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
918 if (rc < 0) {
919 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100920 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100921 }
922 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
923 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
924 if (rc < 0) {
925 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +0100926 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100927 }
928
929 /* Since gbproxy is 2G only we do not expect to get RIM messages only from GERAN cells. */
930 if (src_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
931 LOGP(DGPRS, LOGL_ERROR, "%s %s source RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
932 bssgp_rim_ri_name(&src_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100933 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100934 }
935
936 /* Lookup source cell to make sure that the source RIM routing information actually belongs
937 * to a valid cell that we know */
938 src_cell = gbproxy_cell_by_cellid(nse->cfg, &src_ri.geran.raid, src_ri.geran.cid);
939 if (!src_cell) {
940 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for source RIM routing info (%s)\n", log_pfx,
941 pdut_name, bssgp_rim_ri_name(&src_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100942 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100943 }
944
945 /* TODO: Use bssgp_bvc_get_features_negotiated(src_cell->bss_bvc->fi) to check if the the BSS sided BVC actually
946 * did negotiate RIM support. If not we should respond with a BSSGP STATUS message. The cause code should be
947 * BSSGP_CAUSE_PDU_INCOMP_FEAT. */
948
949 /* If Destination is known by gbproxy, route directly */
950 if (dest_ri.discr == BSSGP_RIM_ROUTING_INFO_GERAN) {
951 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
952 if (dest_cell) {
953 /* TODO: Also check if dest_cell->bss_bvc is RIM-capable (see also above). If not we should
954 * respond with a BSSGP STATUS message as well because it also would make no sense to try
955 * routing the RIM message to the next RIM-capable SGSN. */
Philipp Maier4499cf42021-02-10 17:54:44 +0100956 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to peer (nsei=%u) RIM-PDU: src=%s, dest=%s\n",
957 log_pfx, pdut_name, dest_cell->bss_bvc->nse->nsei,
958 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
959 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100960 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
961 }
962 }
963
964 /* Otherwise pass on to a RIM-capable SGSN */
965 /* TODO: We need to extend gbproxy_select_sgsn() so that it selects a RIM-capable SGSN, at the moment we just
966 * get any SGSN and just assume that it is RIM-capable. */
967 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
968 if (!sgsn) {
969 LOGP(DGPRS, LOGL_NOTICE,
970 "%s %s cannot route RIM message (%s to %s) since no RIM capable SGSN is found!\n", log_pfx,
971 pdut_name, bssgp_rim_ri_name(&src_ri), bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +0100972 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +0100973 }
Philipp Maier4499cf42021-02-10 17:54:44 +0100974 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
975 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
976 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
977 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100978
979 return gbprox_relay2nse(msg, sgsn->nse, 0);
980}
981
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200982/* Receive an incoming signalling message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100983static 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 +0200984{
985 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200986 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +0100987 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
Philipp Maier74882dc2021-02-04 16:31:46 +0100988 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200989 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte560bdb32020-12-04 22:24:47 +0100990 struct gbproxy_bvc *from_bvc = NULL;
Harald Welteec0f8012020-12-06 16:32:01 +0100991 char log_pfx[32];
Harald Weltee5209642020-12-05 19:59:45 +0100992 uint16_t ptp_bvci;
993 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200994 int rc;
995
Harald Weltee5209642020-12-05 19:59:45 +0100996 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
997
998 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +0100999
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001000 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Weltee5209642020-12-05 19:59:45 +01001001 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 +01001002 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001003 }
1004
Harald Welte278dd272020-12-06 13:35:24 +01001005 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001006 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001007 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001008 }
1009
1010 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001011 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001012 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001013 }
1014
Philipp Maier74882dc2021-02-04 16:31:46 +01001015 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 +01001016 DGPRS, log_pfx);
1017 if (rc < 0) {
Harald Weltee5209642020-12-05 19:59:45 +01001018 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001019 return tx_status_from_tlvp(nse, rc, msg);
Harald Welteec0f8012020-12-06 16:32:01 +01001020 }
Harald Weltee5209642020-12-05 19:59:45 +01001021 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001022 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001023
Harald Weltee5209642020-12-05 19:59:45 +01001024 /* special case handling for some PDU types */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001025 switch (pdu_type) {
Harald Weltee5209642020-12-05 19:59:45 +01001026 case BSSGP_PDUT_BVC_RESET:
1027 /* resolve or create gbproxy_bvc + handlei n BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001028 return rx_bvc_reset_from_bss(nse, msg, &tp[0]);
Harald Weltee5209642020-12-05 19:59:45 +01001029 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001030 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001031 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
Harald Welte560bdb32020-12-04 22:24:47 +01001032 if (!from_bvc)
1033 goto err_no_bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001034 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1035 case BSSGP_PDUT_BVC_BLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001036 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001037 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1038 if (!from_bvc)
1039 goto err_no_bvc;
1040 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg);
1041 case BSSGP_PDUT_BVC_UNBLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001042 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001043 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1044 if (!from_bvc)
1045 goto err_no_bvc;
1046 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg);
1047 case BSSGP_PDUT_SUSPEND:
1048 case BSSGP_PDUT_RESUME:
Daniel Willmann77493b12020-12-29 21:13:31 +01001049 {
1050 struct gbproxy_sgsn *sgsn;
1051
Philipp Maier74882dc2021-02-04 16:31:46 +01001052 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001053 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
1054 if (!sgsn) {
1055 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN for TLLI, dropping message!\n");
1056 rc = -EINVAL;
1057 break;
1058 }
1059
1060 gbproxy_tlli_cache_update(nse, tlli);
1061
1062 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
Harald Weltee5209642020-12-05 19:59:45 +01001063#if 0
1064 /* TODO: Validate the RAI for consistency with the RAI
1065 * we expect for any of the BVC within this BSS side NSE */
Philipp Maier74882dc2021-02-04 16:31:46 +01001066 memcpy(ra, TLVP_VAL(&tp[0], BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra));
Harald Welte560bdb32020-12-04 22:24:47 +01001067 gsm48_parse_ra(&raid, from_bvc->ra);
Harald Weltee5209642020-12-05 19:59:45 +01001068#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001069 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001070 }
Harald Weltee5209642020-12-05 19:59:45 +01001071 case BSSGP_PDUT_STATUS:
1072 /* FIXME: inspect the erroneous PDU IE (if any) and check
1073 * if we can extract a TLLI/RNI to route it to the correct SGSN */
1074 break;
1075 case BSSGP_PDUT_RAN_INFO:
1076 case BSSGP_PDUT_RAN_INFO_REQ:
1077 case BSSGP_PDUT_RAN_INFO_ACK:
1078 case BSSGP_PDUT_RAN_INFO_ERROR:
1079 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001080 rc = gbprox_rx_rim_from_bss(tp, nse, msg, log_pfx, pdut_name);
Harald Weltee5209642020-12-05 19:59:45 +01001081 break;
1082 case BSSGP_PDUT_LLC_DISCARD:
1083 case BSSGP_PDUT_FLUSH_LL_ACK:
1084 /* route based on BVCI + TLLI */
Philipp Maier74882dc2021-02-04 16:31:46 +01001085 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1086 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Harald Weltee5209642020-12-05 19:59:45 +01001087 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1088 if (!from_bvc)
1089 goto err_no_bvc;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +01001090 gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true);
Harald Weltee5209642020-12-05 19:59:45 +01001091 break;
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001092 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +01001093 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001094 {
1095 /* Route according to IMSI<->NSE cache entry */
1096 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001097 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1098 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001099 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1100 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi);
1101 if (!nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001102 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001103 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001104 OSMO_ASSERT(nse->sgsn_facing);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001105 rc = gbprox_relay2nse(msg, nse, 0);
1106 break;
1107 }
Harald Weltee5209642020-12-05 19:59:45 +01001108 default:
1109 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001110 break;
1111 }
1112
Harald Weltee5209642020-12-05 19:59:45 +01001113 return rc;
Harald Welte560bdb32020-12-04 22:24:47 +01001114err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001115 LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci);
1116 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001117 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001118}
1119
1120/* Receive paging request from SGSN, we need to relay to proper BSS */
Harald Weltedf690e82020-12-12 15:58:28 +01001121static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name,
Daniel Willmann5614e572021-01-18 18:38:27 +01001122 struct tlv_parsed *tp, uint16_t ns_bvci, bool broadcast)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001123{
Harald Weltedf690e82020-12-12 15:58:28 +01001124 struct gbproxy_config *cfg = sgsn_nse->cfg;
Harald Weltee5209642020-12-05 19:59:45 +01001125 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Weltedf690e82020-12-12 15:58:28 +01001126 struct gbproxy_nse *nse;
Daniel Willmann76205712020-11-30 17:08:58 +01001127 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001128 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte8b4c7942020-12-05 10:14:49 +01001129 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001130
Daniel Willmanne50550e2020-11-26 18:19:21 +01001131 /* FIXME: Handle paging logic to only page each matching NSE */
1132
Harald Welte173a1822020-12-03 15:36:59 +01001133 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001134 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001135 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Weltedf690e82020-12-12 15:58:28 +01001136 sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001137 if (!sgsn_bvc) {
Harald Weltedf690e82020-12-12 15:58:28 +01001138 LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n",
Harald Weltee5209642020-12-05 19:59:45 +01001139 pdut_name, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001140 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1141 return -EINVAL;
1142 }
Harald Weltee5209642020-12-05 19:59:45 +01001143 LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name);
1144 return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte173a1822020-12-03 15:36:59 +01001145 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001146 struct gprs_ra_id raid;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001147 errctr = GBPROX_GLOB_CTR_INV_RAI;
Philipp Maierda3af942021-02-04 21:54:09 +01001148 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte560bdb32020-12-04 22:24:47 +01001149 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001150 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001151 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Philipp Maierda3af942021-02-04 21:54:09 +01001152 if (gsm48_ra_equal(&bss_bvc->raid, &raid)) {
Harald Weltee5209642020-12-05 19:59:45 +01001153 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n",
1154 pdut_name);
1155 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001156 n_nses++;
1157 /* Only send it once to each NSE */
1158 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001159 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001160 }
1161 }
Harald Welte173a1822020-12-03 15:36:59 +01001162 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001163 struct gsm48_ra_id lac;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001164 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte560bdb32020-12-04 22:24:47 +01001165 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001166 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001167 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Philipp Maierda3af942021-02-04 21:54:09 +01001168 gsm48_encode_ra(&lac, &bss_bvc->raid);
1169 if (!memcmp(&lac, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Harald Weltee5209642020-12-05 19:59:45 +01001170 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n",
1171 pdut_name);
1172 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001173 n_nses++;
1174 /* Only send it once to each NSE */
1175 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001176 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001177 }
1178 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001179 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1) || broadcast) {
Harald Welte560bdb32020-12-04 22:24:47 +01001180 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001181 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001182 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1183 LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", 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 Welte53ee2062020-11-24 11:31:13 +01001189 }
1190 } else {
Harald Weltedf690e82020-12-12 15:58:28 +01001191 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n");
Harald Welte53ee2062020-11-24 11:31:13 +01001192 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1193 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001194
Daniel Willmann76205712020-11-30 17:08:58 +01001195 if (n_nses == 0) {
Harald Weltedf690e82020-12-12 15:58:28 +01001196 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001197 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1198 return -EINVAL;
1199 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001200 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001201}
1202
1203/* Receive an incoming BVC-RESET message from the SGSN */
Harald Weltee5209642020-12-05 19:59:45 +01001204static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp,
1205 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001206{
Harald Weltee5209642020-12-05 19:59:45 +01001207 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1208 struct gbproxy_bvc *from_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001209
Harald Weltee5209642020-12-05 19:59:45 +01001210 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001211
Harald Weltee5209642020-12-05 19:59:45 +01001212 if (ptp_bvci == 0) {
1213 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
1214 OSMO_ASSERT(from_bvc);
1215 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
1216 } else {
1217 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1218 if (!from_bvc) {
1219 LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci);
1220 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001221 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001222 }
Harald Weltee5209642020-12-05 19:59:45 +01001223 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001224 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001225
1226 return 0;
1227}
1228
Philipp Maier1c5766b2021-02-09 17:03:03 +01001229/* Receive an incoming RIM message from the SGSN-side NS-VC */
1230static int gbprox_rx_rim_from_sgsn(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
1231 const char *pdut_name)
1232{
1233 struct gbproxy_sgsn *sgsn;
1234 struct gbproxy_cell *dest_cell;
1235 struct bssgp_rim_routing_info dest_ri;
1236 struct bssgp_rim_routing_info src_ri;
1237 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +01001238 char ri_src_str[64];
1239 char ri_dest_str[64];
Daniel Willmannf8cba652021-02-12 04:59:47 +01001240 uint16_t ns_bvci = msgb_bvci(msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001241
1242 /* TODO: Reply with STATUS if BSSGP didn't negotiate RIM feature, see also comments in
1243 gbprox_rx_rim_from_bss() */
1244
1245 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
1246 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
1247 if (rc < 0) {
1248 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001249 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001250 }
1251 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
1252 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
1253 if (rc < 0) {
1254 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001255 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001256 }
1257
1258 /* Since gbproxy is 2G only we do not expect to get RIM messages that target non-GERAN cells. */
1259 if (dest_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
1260 LOGP(DGPRS, LOGL_ERROR, "%s %s destination RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
1261 bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001262 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001263 }
1264
1265 /* Lookup destination cell */
1266 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
1267 if (!dest_cell) {
1268 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for destination RIM routing info (%s)\n", log_pfx,
1269 pdut_name, bssgp_rim_ri_name(&dest_ri));
Daniel Willmannf8cba652021-02-12 04:59:47 +01001270 return tx_status(nse, ns_bvci, BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
Philipp Maier1c5766b2021-02-09 17:03:03 +01001271 }
1272
1273 /* TODO: Check if the BVC of the destination cell actually did negotiate RIM support, see also comments
1274 * in gbprox_rx_rim_from_bss() */
1275 sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
1276 OSMO_ASSERT(sgsn);
1277
Philipp Maier4499cf42021-02-10 17:54:44 +01001278 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying from SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
1279 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
1280 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
1281 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +01001282
1283 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
1284}
1285
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001286/* Receive an incoming signalling message from the SGSN-side NS-VC */
Harald Weltedbef0aa2020-12-07 17:48:11 +01001287static 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 +02001288{
Harald Weltedbef0aa2020-12-07 17:48:11 +01001289 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001290 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +01001291 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
1292 struct gbproxy_config *cfg = nse->cfg;
1293 struct gbproxy_bvc *sgsn_bvc;
Philipp Maier74882dc2021-02-04 16:31:46 +01001294 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001295 int data_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001296 uint16_t bvci;
Harald Welteec0f8012020-12-06 16:32:01 +01001297 char log_pfx[32];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001298 int rc = 0;
1299 int cause;
Harald Welted2fef952020-12-05 00:31:07 +01001300 int i;
Daniel Willmann5614e572021-01-18 18:38:27 +01001301 bool paging_bc = false;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001302
Harald Weltee5209642020-12-05 19:59:45 +01001303 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
1304
1305 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001306
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001307 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welteec0f8012020-12-06 16:32:01 +01001308 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001309 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001310 }
1311
Harald Welte278dd272020-12-06 13:35:24 +01001312 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001313 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001314 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001315 }
1316
1317 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001318 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001319 return tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001320 }
1321
Harald Weltedbef0aa2020-12-07 17:48:11 +01001322 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welteec0f8012020-12-06 16:32:01 +01001323
Philipp Maier74882dc2021-02-04 16:31:46 +01001324 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 +01001325 DGPRS, log_pfx);
1326 if (rc < 0) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001327 rc = tx_status_from_tlvp(nse, rc, msg);
Harald Welteec0f8012020-12-06 16:32:01 +01001328 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1329 return rc;
1330 }
Harald Weltee5209642020-12-05 19:59:45 +01001331 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001332 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001333
1334 switch (pdu_type) {
1335 case BSSGP_PDUT_BVC_RESET:
Harald Weltee5209642020-12-05 19:59:45 +01001336 /* resolve or create ggbproxy_bvc + handle in BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001337 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1338 rc = rx_bvc_reset_from_sgsn(nse, msg, &tp[0], ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001339 break;
1340 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001341 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001342 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1343 if (!sgsn_bvc)
1344 goto err_no_bvc;
1345 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1346 break;
1347 case BSSGP_PDUT_BVC_BLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001348 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001349 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1350 if (!sgsn_bvc)
1351 goto err_no_bvc;
1352 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg);
1353 break;
1354 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001355 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001356 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1357 if (!sgsn_bvc)
1358 goto err_no_bvc;
1359 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg);
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001360 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001361 case BSSGP_PDUT_FLUSH_LL:
1362 /* simple case: BVCI IE is mandatory */
Philipp Maier74882dc2021-02-04 16:31:46 +01001363 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001364 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1365 if (!sgsn_bvc)
1366 goto err_no_bvc;
1367 if (sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1368 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001369 break;
Daniel Willmann5614e572021-01-18 18:38:27 +01001370 case BSSGP_PDUT_DUMMY_PAGING_PS:
1371 /* Routing area is optional in dummy paging and we have nothing else to go by
1372 * so in case it is missing we need to broadcast the paging */
1373 paging_bc = true;
1374 /* fall through */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001375 case BSSGP_PDUT_PAGING_PS:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001376 {
1377 /* Cache the IMSI<->NSE to route PAGING REJECT */
1378 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001379 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1380 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001381 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1382 gbproxy_imsi_cache_update(nse, mi.imsi);
1383 /* fall through */
1384 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001385 case BSSGP_PDUT_PAGING_CS:
1386 /* process the paging request (LAI/RAI lookup) */
Philipp Maier74882dc2021-02-04 16:31:46 +01001387 rc = gbprox_rx_paging(nse, msg, pdut_name, &tp[0], ns_bvci, paging_bc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001388 break;
1389 case BSSGP_PDUT_STATUS:
1390 /* Some exception has occurred */
Philipp Maier74882dc2021-02-04 16:31:46 +01001391 cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE);
Harald Weltee5209642020-12-05 19:59:45 +01001392 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001393 bssgp_cause_str(cause));
Philipp Maier74882dc2021-02-04 16:31:46 +01001394 if (TLVP_PRES_LEN(&tp[0], BSSGP_IE_BVCI, 2)) {
1395 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001396 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001397 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1398 /* don't send STATUS in response to STATUS if !bvc */
1399 if (sgsn_bvc && sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1400 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001401 } else
1402 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1403 break;
1404 /* those only exist in the SGSN -> BSS direction */
1405 case BSSGP_PDUT_SUSPEND_ACK:
1406 case BSSGP_PDUT_SUSPEND_NACK:
1407 case BSSGP_PDUT_RESUME_ACK:
1408 case BSSGP_PDUT_RESUME_NACK:
Daniel Willmann77493b12020-12-29 21:13:31 +01001409 {
1410 struct gbproxy_nse *nse_peer;
Philipp Maier74882dc2021-02-04 16:31:46 +01001411 uint32_t tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001412
1413 nse_peer = gbproxy_nse_by_tlli(cfg, tlli);
1414 if (!nse_peer) {
1415 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1416 /* TODO: Counter */
Daniel Willmannf8cba652021-02-12 04:59:47 +01001417 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001418 }
1419 /* Delete the entry after we're done */
1420 gbproxy_tlli_cache_remove(cfg, tlli);
1421 LOGPNSE(nse_peer, LOGL_DEBUG, "Rx %s: forwarding\n", pdut_name);
1422 gbprox_relay2nse(msg, nse_peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001423 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001424 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001425 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte7479c4d2020-12-02 20:06:04 +01001426 case BSSGP_PDUT_OVERLOAD:
Harald Weltee5209642020-12-05 19:59:45 +01001427 LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name);
Harald Welte560bdb32020-12-04 22:24:47 +01001428 /* broadcast to all BSS-side bvcs */
Harald Welted2fef952020-12-05 00:31:07 +01001429 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte7479c4d2020-12-02 20:06:04 +01001430 gbprox_relay2nse(msg, nse, 0);
1431 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001432 break;
Harald Weltee5209642020-12-05 19:59:45 +01001433 case BSSGP_PDUT_RAN_INFO:
1434 case BSSGP_PDUT_RAN_INFO_REQ:
1435 case BSSGP_PDUT_RAN_INFO_ACK:
1436 case BSSGP_PDUT_RAN_INFO_ERROR:
1437 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001438 rc = gbprox_rx_rim_from_sgsn(tp, nse, msg, log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001439 default:
Harald Weltee5209642020-12-05 19:59:45 +01001440 LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name);
1441 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001442 rc = tx_status(nse, ns_bvci, BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001443 break;
1444 }
1445
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001446 return rc;
Harald Weltee5209642020-12-05 19:59:45 +01001447
Harald Welte560bdb32020-12-04 22:24:47 +01001448err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001449 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001450 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Daniel Willmannf8cba652021-02-12 04:59:47 +01001451 return tx_status(nse, ns_bvci, BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001452}
1453
Harald Weltee5209642020-12-05 19:59:45 +01001454
1455/***********************************************************************
1456 * libosmogb NS/BSSGP integration
1457 ***********************************************************************/
1458
Alexander Couzens951e1332020-09-22 13:21:46 +02001459int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001460{
1461 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001462 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1463 struct gprs_ns2_inst *nsi = cfg->nsi;
1464 struct osmo_gprs_ns2_prim nsp = {};
1465
1466 nsp.bvci = msgb_bvci(msg);
1467 nsp.nsei = msgb_nsei(msg);
1468
Alexander Couzens55c36f92021-01-27 20:56:55 +01001469 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001470 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1471
1472 return rc;
1473}
1474
1475/* Main input function for Gb proxy */
1476int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1477{
Alexander Couzens951e1332020-09-22 13:21:46 +02001478 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Harald Weltee5209642020-12-05 19:59:45 +01001479 uint16_t ns_bvci = msgb_bvci(msg);
1480 uint16_t nsei = msgb_nsei(msg);
1481 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001482
Harald Weltee5209642020-12-05 19:59:45 +01001483 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN);
1484 if (nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001485 /* ensure minimum length to decode PDU type */
1486 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1487 return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1488
Harald Weltee5209642020-12-05 19:59:45 +01001489 if (ns_bvci == 0 || ns_bvci == 1)
1490 return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001491 else
Harald Weltee5209642020-12-05 19:59:45 +01001492 return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001493 }
1494
Harald Weltee5209642020-12-05 19:59:45 +01001495 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS);
1496 if (!nse) {
1497 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei);
1498 nse = gbproxy_nse_alloc(cfg, nsei, false);
1499 }
1500 if (nse) {
Daniel Willmannf8cba652021-02-12 04:59:47 +01001501 /* ensure minimum length to decode PDU type */
1502 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1503 return tx_status(nse, ns_bvci, BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1504
Harald Weltee5209642020-12-05 19:59:45 +01001505 if (ns_bvci == 0 || ns_bvci == 1)
1506 return gbprox_rx_sig_from_bss(nse, msg, ns_bvci);
1507 else
1508 return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci);
1509 }
1510
1511 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001512}
1513
Alexander Couzens951e1332020-09-22 13:21:46 +02001514/* TODO: What about handling:
Alexander Couzens55c36f92021-01-27 20:56:55 +01001515 * GPRS_NS2_AFF_CAUSE_VC_FAILURE,
1516 GPRS_NS2_AFF_CAUSE_VC_RECOVERY,
1517 GPRS_NS2_AFF_CAUSE_FAILURE,
1518 GPRS_NS2_AFF_CAUSE_RECOVERY,
Alexander Couzens951e1332020-09-22 13:21:46 +02001519 osmocom own causes
Alexander Couzens55c36f92021-01-27 20:56:55 +01001520 GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED,
1521 GPRS_NS2_AFF_CAUSE_SNS_FAILURE,
Alexander Couzens951e1332020-09-22 13:21:46 +02001522 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001523
Alexander Couzens951e1332020-09-22 13:21:46 +02001524void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001525{
Harald Welte560bdb32020-12-04 22:24:47 +01001526 /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, bvc->bvci, 0);
Alexander Couzens951e1332020-09-22 13:21:46 +02001527 * TODO: sgsn nsei available/unavailable
Daniel Willmanna8b61652021-02-12 05:05:14 +01001528 * TODO: Update MTU
Alexander Couzens951e1332020-09-22 13:21:46 +02001529 */
Harald Weltee5209642020-12-05 19:59:45 +01001530
Harald Welte560bdb32020-12-04 22:24:47 +01001531 struct gbproxy_bvc *bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001532 struct gbproxy_nse *sgsn_nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001533
Alexander Couzens951e1332020-09-22 13:21:46 +02001534 switch (nsp->u.status.cause) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001535 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
1536 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens951e1332020-09-22 13:21:46 +02001537 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001538
Alexander Couzens55c36f92021-01-27 20:56:55 +01001539 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Harald Welte9b367d22021-01-18 13:55:51 +01001540 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Harald Weltee5209642020-12-05 19:59:45 +01001541 sgsn_nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN);
1542 if (sgsn_nse) {
1543 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
1544 bvc = gbproxy_bvc_by_bvci(sgsn_nse, 0);
1545 if (bvc)
Daniel Willmann3ea37932021-02-10 13:41:14 +01001546 osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001547 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001548 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001549 case GPRS_NS2_AFF_CAUSE_FAILURE:
Harald Weltee5209642020-12-05 19:59:45 +01001550#if 0
Harald Weltea0f70732020-12-05 17:50:23 +01001551 if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) {
Alexander Couzens951e1332020-09-22 13:21:46 +02001552 /* sgsn */
1553 /* TODO: BSVC: block all PtP towards bss */
1554 rate_ctr_inc(&cfg->ctrg->
1555 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1556 } else {
Daniel Willmanne50550e2020-11-26 18:19:21 +01001557 /* bss became unavailable
1558 * TODO: Block all BVC belonging to that NSE */
Harald Welte560bdb32020-12-04 22:24:47 +01001559 bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei);
1560 if (!bvc) {
Alexander Couzens951e1332020-09-22 13:21:46 +02001561 /* TODO: use primitive name + status cause name */
Harald Welte560bdb32020-12-04 22:24:47 +01001562 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n",
Alexander Couzens951e1332020-09-22 13:21:46 +02001563 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001564 break;
1565 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001566
Harald Welte560bdb32020-12-04 22:24:47 +01001567 if (!bvc->blocked)
Alexander Couzens951e1332020-09-22 13:21:46 +02001568 break;
Harald Weltee5209642020-12-05 19:59:45 +01001569 hash_for_each(cfg->sgsn_nses, _sgsn, sgsn_nse, list) {
1570 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, sgsn_nse->nsei, bvc->bvci, 0);
1571 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001572 }
Harald Weltee5209642020-12-05 19:59:45 +01001573#endif
Harald Welte9b367d22021-01-18 13:55:51 +01001574 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Alexander Couzens951e1332020-09-22 13:21:46 +02001575 break;
1576 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001577 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001578 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001579 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001580 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001581}
1582
Alexander Couzens951e1332020-09-22 13:21:46 +02001583/* called by the ns layer */
1584int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1585{
1586 struct osmo_gprs_ns2_prim *nsp;
1587 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001588 uintptr_t bvci;
Alexander Couzens951e1332020-09-22 13:21:46 +02001589 int rc = 0;
1590
1591 if (oph->sap != SAP_NS)
1592 return 0;
1593
1594 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1595
1596 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte9b367d22021-01-18 13:55:51 +01001597 LOGP(DGPRS, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001598 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001599 return 0;
1600 }
1601
1602 switch (oph->primitive) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001603 case GPRS_NS2_PRIM_UNIT_DATA:
Daniel Willmann8f407b12020-12-02 19:33:50 +01001604
Alexander Couzens951e1332020-09-22 13:21:46 +02001605 /* hand the message into the BSSGP implementation */
1606 msgb_bssgph(oph->msg) = oph->msg->l3h;
1607 msgb_bvci(oph->msg) = nsp->bvci;
1608 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001609 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens951e1332020-09-22 13:21:46 +02001610
Daniel Willmann8f407b12020-12-02 19:33:50 +01001611 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens951e1332020-09-22 13:21:46 +02001612 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001613 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001614 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001615 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens951e1332020-09-22 13:21:46 +02001616 gprs_ns_prim_status_cb(cfg, nsp);
1617 break;
1618 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001619 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001620 gprs_ns2_prim_str(oph->primitive),
1621 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001622 break;
1623 }
1624
1625 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001626}
1627
1628void gbprox_reset(struct gbproxy_config *cfg)
1629{
Harald Welted2fef952020-12-05 00:31:07 +01001630 struct gbproxy_nse *nse;
1631 struct hlist_node *ntmp;
Harald Welte8b4c7942020-12-05 10:14:49 +01001632 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001633
Harald Welted2fef952020-12-05 00:31:07 +01001634 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte8b4c7942020-12-05 10:14:49 +01001635 struct gbproxy_bvc *bvc;
1636 struct hlist_node *tmp;
1637 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +01001638 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001639
1640 gbproxy_nse_free(nse);
1641 }
Harald Weltee5209642020-12-05 19:59:45 +01001642 /* FIXME: cells */
1643 /* FIXME: SGSN side BVCs (except signaling) */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001644
1645 rate_ctr_group_free(cfg->ctrg);
1646 gbproxy_init_config(cfg);
1647}
1648
Daniel Willmann77493b12020-12-29 21:13:31 +01001649static void tlli_cache_cleanup(void *data)
1650{
1651 struct gbproxy_config *cfg = data;
1652 gbproxy_tlli_cache_cleanup(cfg);
1653
1654 /* TODO: Disable timer when cache is empty */
1655 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1656}
1657
Daniel Willmannc8a50092021-01-17 13:11:41 +01001658static void imsi_cache_cleanup(void *data)
1659{
1660 struct gbproxy_config *cfg = data;
1661 gbproxy_imsi_cache_cleanup(cfg);
1662
1663 /* TODO: Disable timer when cache is empty */
1664 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1665}
1666
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001667int gbproxy_init_config(struct gbproxy_config *cfg)
1668{
1669 struct timespec tp;
1670
Harald Welte209dc9f2020-12-12 19:02:16 +01001671 /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
1672 cfg->pool.bvc_fc_ratio = 100;
Daniel Willmannee834af2020-12-14 16:22:39 +01001673 cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001674 /* TODO: Make configurable */
Daniel Willmannbd12f3f2021-01-13 18:16:04 +01001675 cfg->tlli_cache.timeout = 10;
Daniel Willmannc8a50092021-01-17 13:11:41 +01001676 cfg->imsi_cache.timeout = 10;
Daniel Willmannee834af2020-12-14 16:22:39 +01001677
Harald Welted2fef952020-12-05 00:31:07 +01001678 hash_init(cfg->bss_nses);
Daniel Willmann1e7be5d2020-12-21 18:08:21 +01001679 hash_init(cfg->sgsn_nses);
1680 hash_init(cfg->cells);
Daniel Willmann77493b12020-12-29 21:13:31 +01001681 hash_init(cfg->tlli_cache.entries);
Daniel Willmannee834af2020-12-14 16:22:39 +01001682 INIT_LLIST_HEAD(&cfg->sgsns);
1683
Daniel Willmann77493b12020-12-29 21:13:31 +01001684 osmo_timer_setup(&cfg->tlli_cache.timer, tlli_cache_cleanup, cfg);
1685 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1686
Daniel Willmannc8a50092021-01-17 13:11:41 +01001687 /* We could also combine both timers */
1688 osmo_timer_setup(&cfg->imsi_cache.timer, imsi_cache_cleanup, cfg);
1689 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1690
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001691 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1692 if (!cfg->ctrg) {
1693 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1694 return -1;
1695 }
1696 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Harald Weltec169de42020-12-07 13:12:13 +01001697 osmo_fsm_log_timeouts(true);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001698
1699 return 0;
Oliver Smith29532c22021-01-29 11:13:00 +01001700}