blob: edd9f292f93191d56b323fb446258c362f84597c [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>
40#include <osmocom/core/stats.h>
Daniel Willmannd4ab1f92020-12-21 18:53:55 +010041#include <osmocom/core/utils.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020042
Alexander Couzens951e1332020-09-22 13:21:46 +020043#include <osmocom/gprs/gprs_ns2.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020044#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte209dc9f2020-12-12 19:02:16 +010045#include <osmocom/gprs/gprs_bssgp2.h>
Alexander Couzens951e1332020-09-22 13:21:46 +020046#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Weltee5209642020-12-05 19:59:45 +010047#include <osmocom/gprs/bssgp_bvc_fsm.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020048
Daniel Willmannd4ab1f92020-12-21 18:53:55 +010049#include <osmocom/gsm/gsm23236.h>
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020050#include <osmocom/gsm/gsm_utils.h>
51
52#include <osmocom/sgsn/signal.h>
53#include <osmocom/sgsn/debug.h>
54#include <osmocom/sgsn/gprs_gb_parse.h>
55#include <osmocom/sgsn/gb_proxy.h>
56
57#include <osmocom/sgsn/gprs_llc.h>
58#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
59#include <osmocom/sgsn/gprs_utils.h>
60
61extern void *tall_sgsn_ctx;
62
63static const struct rate_ctr_desc global_ctr_description[] = {
64 { "inv-bvci", "Invalid BVC Identifier " },
65 { "inv-lai", "Invalid Location Area Identifier" },
66 { "inv-rai", "Invalid Routing Area Identifier " },
67 { "inv-nsei", "No BVC established for NSEI " },
68 { "proto-err:bss", "BSSGP protocol error (BSS )" },
69 { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
70 { "not-supp:bss", "Feature not supported (BSS )" },
71 { "not-supp:sgsn", "Feature not supported (SGSN)" },
72 { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
73 { "tx-err:sgsn", "NS Transmission error (SGSN)" },
74 { "error", "Other error " },
75 { "mod-peer-err", "Patch error: no peer " },
76};
77
78static const struct rate_ctr_group_desc global_ctrg_desc = {
79 .group_name_prefix = "gbproxy:global",
80 .group_description = "GBProxy Global Statistics",
81 .num_ctr = ARRAY_SIZE(global_ctr_description),
82 .ctr_desc = global_ctr_description,
83 .class_id = OSMO_STATS_CLASS_GLOBAL,
84};
85
Harald Welte560bdb32020-12-04 22:24:47 +010086static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann35f7d332020-11-03 21:11:45 +010087 uint16_t ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +020088
Harald Weltea0f70732020-12-05 17:50:23 +010089
Harald Welteec0f8012020-12-06 16:32:01 +010090/* generate BVC-STATUS message with cause value derived from TLV-parser error */
91static int tx_status_from_tlvp(enum osmo_tlv_parser_error tlv_p_err, struct msgb *orig_msg)
92{
93 uint8_t bssgp_cause;
94 switch (tlv_p_err) {
95 case OSMO_TLVP_ERR_MAND_IE_MISSING:
96 bssgp_cause = BSSGP_CAUSE_MISSING_MAND_IE;
97 break;
98 default:
99 bssgp_cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
100 }
101 return bssgp_tx_status(bssgp_cause, NULL, orig_msg);
102}
103
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200104/* strip off the NS header */
105static void strip_ns_hdr(struct msgb *msg)
106{
107 int strip_len = msgb_bssgph(msg) - msg->data;
108 msgb_pull(msg, strip_len);
109}
110
Harald Weltee5209642020-12-05 19:59:45 +0100111#if 0
Harald Welte560bdb32020-12-04 22:24:47 +0100112/* feed a message down the NS-VC associated with the specified bvc */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200113static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
114 uint16_t ns_bvci, uint16_t sgsn_nsei)
115{
116 /* create a copy of the message so the old one can
117 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200118 struct gprs_ns2_inst *nsi = cfg->nsi;
119 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200120 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
121 int rc;
122
Daniel Willmann3696dce2020-12-02 16:08:02 +0100123 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200124 msgb_nsei(msg), ns_bvci, sgsn_nsei);
125
Alexander Couzens951e1332020-09-22 13:21:46 +0200126 nsp.bvci = ns_bvci;
127 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200128
129 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200130 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
131 PRIM_OP_REQUEST, msg);
132 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200133 if (rc < 0)
134 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200135 return rc;
136}
Harald Weltee5209642020-12-05 19:59:45 +0100137#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200138
Daniel Willmann76205712020-11-30 17:08:58 +0100139/* feed a message down the NSE */
140static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100141 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200142{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100143 OSMO_ASSERT(nse);
144 OSMO_ASSERT(nse->cfg);
145
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200146 /* create a copy of the message so the old one can
147 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100148 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Alexander Couzens951e1332020-09-22 13:21:46 +0200149 struct osmo_gprs_ns2_prim nsp = {};
Daniel Willmann76205712020-11-30 17:08:58 +0100150 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Harald Weltefe059582020-11-18 12:01:46 +0100151 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200152 int rc;
153
Daniel Willmann98b1b452020-12-21 10:40:27 +0100154 DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg),
155 !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200156
Alexander Couzens951e1332020-09-22 13:21:46 +0200157 nsp.bvci = ns_bvci;
Daniel Willmanne50550e2020-11-26 18:19:21 +0100158 nsp.nsei = nse->nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200159
160 /* Strip the old NS header, it will be replaced with a new one */
161 strip_ns_hdr(msg);
162
Harald Weltefe059582020-11-18 12:01:46 +0100163 /* TS 48.018 Section 5.4.2: The link selector parameter is
164 * defined in 3GPP TS 48.016. At one side of the Gb interface,
165 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
166 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
167 * underlying network service. */
168 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
169 nsp.u.unitdata.link_selector = tlli;
170
Alexander Couzens951e1332020-09-22 13:21:46 +0200171 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
172 PRIM_OP_REQUEST, msg);
173 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Daniel Willmann76205712020-11-30 17:08:58 +0100174 /* FIXME: We need a counter group for gbproxy_nse */
175 //if (rc < 0)
Harald Welte560bdb32020-12-04 22:24:47 +0100176 // rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Daniel Willmann76205712020-11-30 17:08:58 +0100177
178 return rc;
179}
180
Harald Welte560bdb32020-12-04 22:24:47 +0100181/* feed a message down the NS-VC associated with the specified bvc */
182static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann76205712020-11-30 17:08:58 +0100183 uint16_t ns_bvci)
184{
185 int rc;
Harald Welte560bdb32020-12-04 22:24:47 +0100186 struct gbproxy_nse *nse = bvc->nse;
Daniel Willmann76205712020-11-30 17:08:58 +0100187 OSMO_ASSERT(nse);
188
189 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200190 if (rc < 0)
Harald Welte560bdb32020-12-04 22:24:47 +0100191 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200192
193 return rc;
194}
195
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200196int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
197{
198 return 0;
199}
200
Harald Weltee5209642020-12-05 19:59:45 +0100201
202/***********************************************************************
203 * PTP BVC handling
204 ***********************************************************************/
205
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100206/* FIXME: Handle the tlli NULL case correctly,
207 * This function should take a generic selector
208 * and choose an sgsn based on that
209 */
210static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli)
211{
212 struct gbproxy_sgsn *sgsn = NULL;
213 struct gbproxy_sgsn *sgsn_avoid = NULL;
214
215 int tlli_type;
216 int16_t nri;
217 bool null_nri = false;
218
219 if (!tlli) {
220 sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);
221 if (!sgsn) {
222 return NULL;
223 }
224 LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first SGSN\n");
225 return sgsn;
226 }
227
228 if (cfg->pool.nri_bitlen == 0) {
229 /* Pooling is disabled */
230 sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);
231 if (!sgsn) {
232 return NULL;
233 }
234
235 LOGPSGSN(sgsn, LOGL_INFO, "Pooling disabled, using first configured SGSN\n");
236 } else {
237 /* Pooling is enabled, try to use the NRI for routing to an SGSN
238 * See 3GPP TS 23.236 Ch. 5.3.2 */
239 tlli_type = gprs_tlli_type(*tlli);
240 if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) {
241 /* Only get/use the NRI if tlli type is local */
242 osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen);
243 if (nri >= 0) {
244 /* Get the SGSN for the NRI */
245 sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri);
246 if (sgsn && !null_nri)
247 return sgsn;
248 /* If the NRI is the null NRI, we need to avoid the chosen SGSN */
249 if (null_nri && sgsn) {
250 sgsn_avoid = sgsn;
251 }
252 } else {
253 /* We couldn't get the NRI from the TLLI */
254 LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %u\n", *tlli);
255 }
256 }
257 }
258
259 /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid
260 * NOTE: This function is not stable if the number of SGSNs or allow_attach changes
261 * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that
262 * we can just wait for the MS to reattempt the procedure.
263 */
264 if (!sgsn)
265 sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli);
266
267 if (!sgsn) {
268 LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli);
269 return NULL;
270 }
271
272 return sgsn;
273}
274
275/*! Find the correct gbproxy_bvc given a cell and an SGSN
276 * \param[in] cfg The gbproxy configuration
277 * \param[in] cell The cell the message belongs to
278 * \param[in] tlli An optional TLLI used for tracking
279 * \return Returns 0 on success, otherwise a negative value
280 */
281static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli)
282{
283 struct gbproxy_sgsn *sgsn;
284 struct gbproxy_bvc *sgsn_bvc = NULL;
Harald Welte02d7c482020-12-30 12:13:36 +0100285 int i;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100286
287 sgsn = gbproxy_select_sgsn(cfg, tlli);
288 if (!sgsn) {
289 LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
290 return NULL;
291 }
292
293 /* Get the BVC for this SGSN/NSE */
Harald Welte02d7c482020-12-30 12:13:36 +0100294 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100295 sgsn_bvc = cell->sgsn_bvc[i];
296 if (!sgsn_bvc)
297 continue;
298 if (sgsn->nse != sgsn_bvc->nse)
299 continue;
300
301 return sgsn_bvc;
302 }
303
304 /* This shouldn't happen */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100305 LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN %s, dropping message!\n", sgsn->name);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100306 return NULL;
307}
308
309/*! Send a message to the next SGSN, possibly ignoring the null SGSN
310 * route an uplink message on a PTP-BVC to a SGSN using the TLLI
311 * \param[in] cell The cell the message belongs to
312 * \param[in] msg The BSSGP message
313 * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI)
314 * \param[in] tlli An optional TLLI used for tracking
315 * \return Returns 0 on success, otherwise a negative value
316 */
317static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli,
Harald Weltee5209642020-12-05 19:59:45 +0100318 bool sig_bvci)
319{
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100320 struct gbproxy_config *cfg = cell->cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100321 struct gbproxy_bvc *sgsn_bvc;
Harald Weltee5209642020-12-05 19:59:45 +0100322
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100323 sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli);
324 if (!sgsn_bvc) {
325 LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli);
326 return -EINVAL;
Harald Weltee5209642020-12-05 19:59:45 +0100327 }
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100328
329 return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);
Harald Weltee5209642020-12-05 19:59:45 +0100330}
331
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200332/* Receive an incoming PTP message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100333static 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 +0200334{
Harald Welte278dd272020-12-06 13:35:24 +0100335 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100336 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
337 struct gbproxy_bvc *bss_bvc;
338 struct tlv_parsed tp;
339 char log_pfx[32];
340 uint32_t tlli;
341 int rc;
342
343 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
344
345 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200346
Daniel Willmann06331ac2020-12-10 17:59:46 +0100347 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100348 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci);
Harald Welte278dd272020-12-06 13:35:24 +0100349 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
350 }
351
352 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100353 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100354 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
355 }
356
357 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100358 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100359 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
360 }
361
Harald Weltee5209642020-12-05 19:59:45 +0100362 bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
363 if (!bss_bvc) {
364 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n",
365 log_pfx, pdut_name);
366 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200367 }
368
Harald Weltee5209642020-12-05 19:59:45 +0100369 /* UL_UNITDATA has a different header than all other uplink PDUs */
370 if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA) {
371 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
372 if (msgb_bssgp_len(msg) < sizeof(*budh))
373 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
374 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
375 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
376 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
377 * doesn't have to worry where the TLLI came from */
378 tp.lv[BSSGP_IE_TLLI].len = 4;
379 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
380 } else {
381 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
382 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
383 }
384 if (rc < 0) {
385 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
386 return tx_status_from_tlvp(rc, msg);
387 }
Harald Welte85a40272020-12-08 21:43:22 +0100388 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
389 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200390
Harald Weltee5209642020-12-05 19:59:45 +0100391 switch (bgph->pdu_type) {
392 case BSSGP_PDUT_UL_UNITDATA:
393 case BSSGP_PDUT_RA_CAPA_UPDATE:
394 case BSSGP_PDUT_FLOW_CONTROL_MS:
395 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
396 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
397 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
398 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
399 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
400 case BSSGP_PDUT_FLOW_CONTROL_PFC:
401 case BSSGP_PDUT_DELETE_BSS_PFC_REQ:
402 case BSSGP_PDUT_PS_HO_REQUIRED:
403 case BSSGP_PDUT_PS_HO_REQUEST_ACK:
404 case BSSGP_PDUT_PS_HO_REQUEST_NACK:
405 case BSSGP_PDUT_PS_HO_COMPLETE:
406 case BSSGP_PDUT_PS_HO_CANCEL:
407 /* We can route based on TLLI-NRI */
408 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100409 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100410 break;
411 case BSSGP_PDUT_RADIO_STATUS:
412 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
413 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100414 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100415 } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {
416 /* we treat the TMSI like a TLLI and extract the NRI from it */
417 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100418 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100419 } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {
Daniel Willmann5193f222021-01-11 05:00:46 +0100420 /* FIXME: Use the IMSI as selector? */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100421 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false);
Daniel Willmann5193f222021-01-11 05:00:46 +0100422 /* rc = gbprox_bss2sgsn_hashed(bss_bvc->cell, msg, NULL); */
Harald Weltee5209642020-12-05 19:59:45 +0100423 } else
424 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");
425 break;
426 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
427 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +0100428 {
429 /* Route according to IMSI<->NSE cache entry */
430 struct osmo_mobile_identity mi;
431 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
432 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
433 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
434 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi);
435 if (nse) {
436 OSMO_ASSERT(nse->sgsn_facing);
437 rc = gbprox_relay2nse(msg, nse, ns_bvci);
438 } else {
Daniel Willmann82669182021-01-19 11:37:55 +0100439 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx unmatched %s with IMSI %s\n", pdut_name, mi.imsi);
Daniel Willmann5614e572021-01-18 18:38:27 +0100440 }
Harald Weltee5209642020-12-05 19:59:45 +0100441 break;
Daniel Willmann5614e572021-01-18 18:38:27 +0100442 }
Harald Weltee5209642020-12-05 19:59:45 +0100443 case BSSGP_PDUT_FLOW_CONTROL_BVC:
Harald Welte85a40272020-12-08 21:43:22 +0100444 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100445 break;
446 case BSSGP_PDUT_STATUS:
447 /* TODO: Implement by inspecting the contained PDU */
448 if (!TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR))
449 break;
450 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
451 break;
452 }
453
454 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200455}
456
457/* Receive an incoming PTP message from a SGSN-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100458static 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 +0200459{
Harald Welte278dd272020-12-06 13:35:24 +0100460 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100461 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
462 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte85a40272020-12-08 21:43:22 +0100463 struct tlv_parsed tp;
Harald Weltee5209642020-12-05 19:59:45 +0100464 char log_pfx[32];
Harald Welte85a40272020-12-08 21:43:22 +0100465 int rc;
Harald Weltee5209642020-12-05 19:59:45 +0100466
467 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
468
469 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200470
Daniel Willmann06331ac2020-12-10 17:59:46 +0100471 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100472 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx);
Harald Welte278dd272020-12-06 13:35:24 +0100473 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
474 }
475
476 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100477 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100478 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
479 }
480
481 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100482 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100483 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
484 }
485
Harald Weltee5209642020-12-05 19:59:45 +0100486 sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
487 if (!sgsn_bvc) {
488 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n",
489 log_pfx, pdut_name);
490 rate_ctr_inc(&nse->cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_BVCI]);
491 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200492 }
493
Harald Weltee5209642020-12-05 19:59:45 +0100494 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) {
495 LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name);
496 rate_ctr_inc(&sgsn_bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200497 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
498 }
Harald Welte85a40272020-12-08 21:43:22 +0100499
500 /* DL_UNITDATA has a different header than all other uplink PDUs */
501 if (bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) {
502 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
503 if (msgb_bssgp_len(msg) < sizeof(*budh))
504 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
505 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
506 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
507 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
508 * doesn't have to worry where the TLLI came from */
509 tp.lv[BSSGP_IE_TLLI].len = 4;
510 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
511 } else {
512 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
513 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
514 }
515 if (rc < 0) {
516 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
517 return tx_status_from_tlvp(rc, msg);
518 }
519 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
520 msgb_bcid(msg) = (void *)&tp;
521
Harald Weltee5209642020-12-05 19:59:45 +0100522 OSMO_ASSERT(sgsn_bvc->cell);
523 bss_bvc = sgsn_bvc->cell->bss_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200524
Harald Welte85a40272020-12-08 21:43:22 +0100525 switch (bgph->pdu_type) {
526 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
527 return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg);
Daniel Willmann5614e572021-01-18 18:38:27 +0100528 case BSSGP_PDUT_DUMMY_PAGING_PS:
529 case BSSGP_PDUT_PAGING_PS:
530 {
531 /* Cache the IMSI<->NSE to route PAGING REJECT */
532 struct osmo_mobile_identity mi;
533 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
534 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
535 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
536 gbproxy_imsi_cache_update(nse, mi.imsi);
537 break;
Harald Welte85a40272020-12-08 21:43:22 +0100538 }
Daniel Willmann5614e572021-01-18 18:38:27 +0100539 default:
540 break;
541 }
542 return gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci);
Harald Welte85a40272020-12-08 21:43:22 +0100543
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200544}
545
Harald Weltee5209642020-12-05 19:59:45 +0100546/***********************************************************************
547 * BVC FSM call-backs
548 ***********************************************************************/
Harald Welte7df1e5a2020-12-02 22:53:26 +0100549
Harald Weltee5209642020-12-05 19:59:45 +0100550/* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */
551static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv)
552{
553 unsigned int i;
554
555 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
556 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i];
557 if (!sgsn_bvc)
558 continue;
559 osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv);
560 }
561}
562
563/* BVC FSM informs us about a BSS-side reset of the signaling BVC */
564static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
565 uint16_t cell_id, uint8_t cause, void *priv)
566{
567 struct gbproxy_bvc *sig_bvc = priv;
568 struct gbproxy_nse *nse = sig_bvc->nse;
569 struct gbproxy_bvc *ptp_bvc;
570 unsigned int i;
571
572 /* BLOCK all SGSN-side PTP BVC within this NSE */
573 hash_for_each(nse->bvcs, i, ptp_bvc, list) {
574 if (ptp_bvc == sig_bvc)
575 continue;
576 OSMO_ASSERT(ptp_bvc->cell);
577
578 dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Harald Weltef9e149b2020-12-02 23:29:38 +0100579 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100580
Harald Weltee5209642020-12-05 19:59:45 +0100581 /* Delete all BSS-side PTP BVC within this NSE */
582 gbproxy_cleanup_bvcs(nse, 0);
583
584 /* TODO: we keep the "CELL" around for now, re-connecting it to
585 * any (later) new PTP-BVC for that BVCI. Not sure if that's the
586 * best idea ? */
587}
588
589/* forward declaration */
590static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops;
591
592static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = {
593 .reset_notification = bss_sig_bvc_reset_notif,
594};
595
596/* BVC FSM informs us about a BSS-side reset of a PTP BVC */
597static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
598 uint16_t cell_id, uint8_t cause, void *priv)
599{
600 struct gbproxy_bvc *bvc = priv;
601 struct gbproxy_config *cfg = bvc->nse->cfg;
Harald Welte664c24e2020-12-12 15:01:17 +0100602 struct gbproxy_nse *sgsn_nse;
Harald Weltee5209642020-12-05 19:59:45 +0100603 unsigned int i;
604
605 OSMO_ASSERT(bvci != 0);
606
607 if (!bvc->cell) {
608 /* see if we have a CELL dangling around */
609 bvc->cell = gbproxy_cell_by_bvci(cfg, bvci);
610 if (bvc->cell) {
611 /* the CELL already exists. This means either it * was created before at an
612 * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a
613 * malconfiguration */
614 if (bvc->cell->bss_bvc) {
615 LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already "
616 "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei);
617 LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting "
618 "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei);
619 gbproxy_bvc_free(bvc->cell->bss_bvc);
620 }
621 bvc->cell->bss_bvc = bvc;
622 }
623 }
624
625 if (!bvc->cell) {
Harald Weltee5209642020-12-05 19:59:45 +0100626 /* if we end up here, it means this is the first time we received a BVC-RESET
627 * for this BVC. We need to create the 'cell' data structure and the SGSN-side
628 * BVC counterparts */
629
630 bvc->cell = gbproxy_cell_alloc(cfg, bvci);
631 OSMO_ASSERT(bvc->cell);
Harald Welteee0cc812020-12-12 14:22:32 +0100632 memcpy(bvc->cell->ra, bvc->ra, sizeof(bvc->cell->ra));
Harald Weltee5209642020-12-05 19:59:45 +0100633
634 /* link us to the cell and vice-versa */
635 bvc->cell->bss_bvc = bvc;
Harald Welte664c24e2020-12-12 15:01:17 +0100636 }
Harald Weltee5209642020-12-05 19:59:45 +0100637
Harald Welte664c24e2020-12-12 15:01:17 +0100638 /* allocate (any missing) SGSN-side BVCs within the cell, and reset them */
639 hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) {
640 struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
641 if (sgsn_bvc)
642 OSMO_ASSERT(sgsn_bvc->cell == bvc->cell || !sgsn_bvc->cell);
Harald Weltee5209642020-12-05 19:59:45 +0100643
Harald Welte664c24e2020-12-12 15:01:17 +0100644 if (!sgsn_bvc) {
645 sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci);
646 OSMO_ASSERT(sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100647
Harald Welte664c24e2020-12-12 15:01:17 +0100648 sgsn_bvc->cell = bvc->cell;
649 memcpy(sgsn_bvc->ra, bvc->cell->ra, sizeof(sgsn_bvc->ra));
650 sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei,
651 bvci, ra_id, cell_id);
652 OSMO_ASSERT(sgsn_bvc->fi);
653 bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100654
Harald Welte664c24e2020-12-12 15:01:17 +0100655 gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100656 }
657 }
658
659 /* Trigger outbound BVC-RESET procedure toward each SGSN */
660 dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause);
661}
662
663/* BVC FSM informs us about a BSS-side FSM state change */
664static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv)
665{
666 struct gbproxy_bvc *bvc = priv;
667 struct gbproxy_cell *cell = bvc->cell;
668 uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi);
669
670 /* we have just been created but due to callback ordering the cell is not associated */
671 if (!cell)
672 return;
673
674 switch (state) {
675 case BSSGP_BVCFSM_S_BLOCKED:
676 /* block the corresponding SGSN-side PTP BVCs */
677 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
678 break;
679 case BSSGP_BVCFSM_S_UNBLOCKED:
680 /* unblock the corresponding SGSN-side PTP BVCs */
681 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL);
682 break;
683 }
684}
685
Harald Welte85a40272020-12-08 21:43:22 +0100686/* BVC FSM informs us about BVC-FC PDU receive */
687static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
688{
Harald Welte209dc9f2020-12-12 19:02:16 +0100689 struct bssgp2_flow_ctrl fc_reduced;
Harald Welte85a40272020-12-08 21:43:22 +0100690 struct gbproxy_bvc *bss_bvc = priv;
Harald Welte209dc9f2020-12-12 19:02:16 +0100691 struct gbproxy_cell *cell;
692 struct gbproxy_config *cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100693
Harald Welte209dc9f2020-12-12 19:02:16 +0100694 OSMO_ASSERT(bss_bvc);
695 OSMO_ASSERT(fc);
696
697 cell = bss_bvc->cell;
Harald Welte85a40272020-12-08 21:43:22 +0100698 if (!cell)
699 return;
700
Harald Welte209dc9f2020-12-12 19:02:16 +0100701 cfg = cell->cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100702
Harald Welte209dc9f2020-12-12 19:02:16 +0100703 /* reduce / scale according to configuration to make sure we only advertise a fraction
704 * of the capacity to each of the SGSNs in the pool */
705 fc_reduced = *fc;
706 fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
707 fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
708 /* we don't modify the per-MS related values as any single MS is only served by one SGSN */
709
710 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
Harald Welte85a40272020-12-08 21:43:22 +0100711}
712
Harald Weltee5209642020-12-05 19:59:45 +0100713static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
714 .reset_notification = bss_ptp_bvc_reset_notif,
715 .state_chg_notification = bss_ptp_bvc_state_chg_notif,
Harald Welte85a40272020-12-08 21:43:22 +0100716 .rx_fc_bvc = bss_ptp_bvc_fc_bvc,
Harald Weltee5209642020-12-05 19:59:45 +0100717};
718
719/* BVC FSM informs us about a SGSN-side reset of a PTP BVC */
720static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
721 uint16_t cell_id, uint8_t cause, void *priv)
722{
723 struct gbproxy_bvc *bvc = priv;
724
725 if (!bvc->cell) {
726 LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n");
727 return;
728 }
729
730 OSMO_ASSERT(bvc->cell->bss_bvc);
731
732 /* request reset of BSS-facing PTP-BVC */
733 osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
734}
735
736static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = {
737 .reset_notification = sgsn_ptp_bvc_reset_notif,
738};
739
740/* BVC FSM informs us about a SGSN-side reset of the signaling BVC */
741static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
742 uint16_t cell_id, uint8_t cause, void *priv)
743{
744 struct gbproxy_bvc *bvc = priv;
745 struct gbproxy_config *cfg = bvc->nse->cfg;
746 struct gbproxy_nse *bss_nse;
747 unsigned int i;
748
749 /* delete all SGSN-side PTP BVC for this SGSN */
750 gbproxy_cleanup_bvcs(bvc->nse, 0);
751 /* FIXME: what to do about the cells? */
752 /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */
753
754 /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */
755 hash_for_each(cfg->bss_nses, i, bss_nse, list) {
756 struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0);
757 if (!bss_bvc) {
758 LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n");
759 continue;
760 }
761 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
762 }
763}
764
765const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = {
766 .reset_notification = sgsn_sig_bvc_reset_notif,
767};
768
769/***********************************************************************
770 * Signaling BVC handling
771 ***********************************************************************/
772
773/* process a BVC-RESET message from the BSS side */
774static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp)
775{
776 struct gbproxy_bvc *from_bvc = NULL;
777 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
778 uint32_t features = 0; // FIXME: make configurable
779
780 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci);
781
Harald Welte314647b2020-12-02 23:03:22 +0100782 if (bvci == 0) {
783 /* If we receive a BVC reset on the signalling endpoint, we
784 * don't want the SGSN to reset, as the signalling endpoint
785 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte324f0652020-12-02 23:06:37 +0100786
Harald Weltee5209642020-12-05 19:59:45 +0100787 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
Harald Welte560bdb32020-12-04 22:24:47 +0100788 if (!from_bvc) {
Harald Weltee5209642020-12-05 19:59:45 +0100789 from_bvc = gbproxy_bvc_alloc(nse, 0);
790 OSMO_ASSERT(from_bvc);
791 from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features);
792 if (!from_bvc->fi) {
793 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
794 gbproxy_bvc_free(from_bvc);
795 return -ENOMEM;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100796 }
Harald Weltee5209642020-12-05 19:59:45 +0100797 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc);
798 }
799 } else {
800 from_bvc = gbproxy_bvc_by_bvci(nse, bvci);
801 if (!from_bvc) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100802 /* if a PTP-BVC is reset, and we don't know that
Harald Welte560bdb32020-12-04 22:24:47 +0100803 * PTP-BVCI yet, we should allocate a new bvc */
804 from_bvc = gbproxy_bvc_alloc(nse, bvci);
805 OSMO_ASSERT(from_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100806 from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi,
807 nse->nsei, bvci);
808 if (!from_bvc->fi) {
809 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
810 gbproxy_bvc_free(from_bvc);
811 return -ENOMEM;
812 }
813 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100814 }
Harald Weltee5209642020-12-05 19:59:45 +0100815#if 0
Harald Welte7df1e5a2020-12-02 22:53:26 +0100816 /* Could have moved to a different NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100817 if (!check_bvc_nsei(from_bvc, nsei)) {
818 LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100819
Harald Weltee5209642020-12-05 19:59:45 +0100820 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei, false);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100821 if (!nse_new) {
822 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
823 "BVCI=%05u\n", bvci, nsei);
824 bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
825 return 0;
826 }
827
Harald Welte560bdb32020-12-04 22:24:47 +0100828 /* Move bvc to different NSE */
829 gbproxy_bvc_move(from_bvc, nse_new);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100830 }
Harald Weltee5209642020-12-05 19:59:45 +0100831#endif
832 /* FIXME: do we need this, if it happens within FSM? */
Harald Welte173a1822020-12-03 15:36:59 +0100833 if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100834 struct gprs_ra_id raid;
835 /* We have a Cell Identifier present in this
836 * PDU, this means we can extend our local
837 * state information about this particular cell
838 * */
Harald Welte560bdb32020-12-04 22:24:47 +0100839 memcpy(from_bvc->ra, TLVP_VAL(tp, BSSGP_IE_CELL_ID), sizeof(from_bvc->ra));
840 gsm48_parse_ra(&raid, from_bvc->ra);
841 LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welte7df1e5a2020-12-02 22:53:26 +0100842 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100843 }
Harald Weltee5209642020-12-05 19:59:45 +0100844 /* hand into FSM for further processing */
845 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
846 return 0;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100847}
848
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200849/* Receive an incoming signalling message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100850static 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 +0200851{
852 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200853 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +0100854 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
855 struct tlv_parsed tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200856 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte560bdb32020-12-04 22:24:47 +0100857 struct gbproxy_bvc *from_bvc = NULL;
Harald Welteec0f8012020-12-06 16:32:01 +0100858 char log_pfx[32];
Harald Weltee5209642020-12-05 19:59:45 +0100859 uint16_t ptp_bvci;
860 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200861 int rc;
862
Harald Weltee5209642020-12-05 19:59:45 +0100863 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
864
865 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +0100866
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200867 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100868 LOGP(DGPRS, LOGL_NOTICE, "%s %s BVCI=%05u is not signalling\n", log_pfx, pdut_name, ns_bvci);
Harald Welte278dd272020-12-06 13:35:24 +0100869 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200870 }
871
Harald Welte278dd272020-12-06 13:35:24 +0100872 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +0100873 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100874 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
875 }
876
877 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100878 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100879 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200880 }
881
Harald Welteec0f8012020-12-06 16:32:01 +0100882 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, pdu_type, bgph->data, data_len, 0, 0,
883 DGPRS, log_pfx);
884 if (rc < 0) {
Harald Weltee5209642020-12-05 19:59:45 +0100885 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welteec0f8012020-12-06 16:32:01 +0100886 return tx_status_from_tlvp(rc, msg);
887 }
Harald Weltee5209642020-12-05 19:59:45 +0100888 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
889 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200890
Harald Weltee5209642020-12-05 19:59:45 +0100891 /* special case handling for some PDU types */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200892 switch (pdu_type) {
Harald Weltee5209642020-12-05 19:59:45 +0100893 case BSSGP_PDUT_BVC_RESET:
894 /* resolve or create gbproxy_bvc + handlei n BVC-FSM */
Harald Weltee5209642020-12-05 19:59:45 +0100895 return rx_bvc_reset_from_bss(nse, msg, &tp);
896 case BSSGP_PDUT_BVC_RESET_ACK:
897 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
898 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
Harald Welte560bdb32020-12-04 22:24:47 +0100899 if (!from_bvc)
900 goto err_no_bvc;
Harald Weltee5209642020-12-05 19:59:45 +0100901 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
902 case BSSGP_PDUT_BVC_BLOCK:
903 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
904 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
905 if (!from_bvc)
906 goto err_no_bvc;
907 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg);
908 case BSSGP_PDUT_BVC_UNBLOCK:
909 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
910 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
911 if (!from_bvc)
912 goto err_no_bvc;
913 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg);
914 case BSSGP_PDUT_SUSPEND:
915 case BSSGP_PDUT_RESUME:
Daniel Willmann77493b12020-12-29 21:13:31 +0100916 {
917 struct gbproxy_sgsn *sgsn;
918
919 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
920 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
921 if (!sgsn) {
922 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN for TLLI, dropping message!\n");
923 rc = -EINVAL;
924 break;
925 }
926
927 gbproxy_tlli_cache_update(nse, tlli);
928
929 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
Harald Weltee5209642020-12-05 19:59:45 +0100930#if 0
931 /* TODO: Validate the RAI for consistency with the RAI
932 * we expect for any of the BVC within this BSS side NSE */
933 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra));
Harald Welte560bdb32020-12-04 22:24:47 +0100934 gsm48_parse_ra(&raid, from_bvc->ra);
Harald Weltee5209642020-12-05 19:59:45 +0100935#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200936 break;
Daniel Willmann77493b12020-12-29 21:13:31 +0100937 }
Harald Weltee5209642020-12-05 19:59:45 +0100938 case BSSGP_PDUT_STATUS:
939 /* FIXME: inspect the erroneous PDU IE (if any) and check
940 * if we can extract a TLLI/RNI to route it to the correct SGSN */
941 break;
942 case BSSGP_PDUT_RAN_INFO:
943 case BSSGP_PDUT_RAN_INFO_REQ:
944 case BSSGP_PDUT_RAN_INFO_ACK:
945 case BSSGP_PDUT_RAN_INFO_ERROR:
946 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
947 /* FIXME: route based in RIM Routing IE */
948 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
949 break;
950 case BSSGP_PDUT_LLC_DISCARD:
951 case BSSGP_PDUT_FLUSH_LL_ACK:
952 /* route based on BVCI + TLLI */
953 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
954 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
955 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
956 if (!from_bvc)
957 goto err_no_bvc;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100958 gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true);
Harald Weltee5209642020-12-05 19:59:45 +0100959 break;
Daniel Willmann8613c9d2021-01-17 13:40:38 +0100960 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +0100961 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
Daniel Willmann8613c9d2021-01-17 13:40:38 +0100962 {
963 /* Route according to IMSI<->NSE cache entry */
964 struct osmo_mobile_identity mi;
965 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
966 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
967 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
968 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi);
969 if (!nse) {
970 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
971 }
Daniel Willmann5614e572021-01-18 18:38:27 +0100972 OSMO_ASSERT(nse->sgsn_facing);
Daniel Willmann8613c9d2021-01-17 13:40:38 +0100973 rc = gbprox_relay2nse(msg, nse, 0);
974 break;
975 }
Harald Weltee5209642020-12-05 19:59:45 +0100976 default:
977 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200978 break;
979 }
980
Harald Weltee5209642020-12-05 19:59:45 +0100981 return rc;
Harald Welte560bdb32020-12-04 22:24:47 +0100982err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +0100983 LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci);
984 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200985 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200986}
987
988/* Receive paging request from SGSN, we need to relay to proper BSS */
Harald Weltedf690e82020-12-12 15:58:28 +0100989static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name,
Daniel Willmann5614e572021-01-18 18:38:27 +0100990 struct tlv_parsed *tp, uint16_t ns_bvci, bool broadcast)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200991{
Harald Weltedf690e82020-12-12 15:58:28 +0100992 struct gbproxy_config *cfg = sgsn_nse->cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100993 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Weltedf690e82020-12-12 15:58:28 +0100994 struct gbproxy_nse *nse;
Daniel Willmann76205712020-11-30 17:08:58 +0100995 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200996 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte8b4c7942020-12-05 10:14:49 +0100997 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200998
Daniel Willmanne50550e2020-11-26 18:19:21 +0100999 /* FIXME: Handle paging logic to only page each matching NSE */
1000
Harald Welte173a1822020-12-03 15:36:59 +01001001 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001002 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001003 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Weltedf690e82020-12-12 15:58:28 +01001004 sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001005 if (!sgsn_bvc) {
Harald Weltedf690e82020-12-12 15:58:28 +01001006 LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n",
Harald Weltee5209642020-12-05 19:59:45 +01001007 pdut_name, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001008 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1009 return -EINVAL;
1010 }
Harald Weltee5209642020-12-05 19:59:45 +01001011 LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name);
1012 return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte173a1822020-12-03 15:36:59 +01001013 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001014 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte560bdb32020-12-04 22:24:47 +01001015 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001016 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001017 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1018 if (!memcmp(bss_bvc->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
1019 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n",
1020 pdut_name);
1021 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001022 n_nses++;
1023 /* Only send it once to each NSE */
1024 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001025 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001026 }
1027 }
Harald Welte173a1822020-12-03 15:36:59 +01001028 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001029 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte560bdb32020-12-04 22:24:47 +01001030 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001031 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001032 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1033 if (!memcmp(bss_bvc->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
1034 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n",
1035 pdut_name);
1036 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001037 n_nses++;
1038 /* Only send it once to each NSE */
1039 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001040 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001041 }
1042 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001043 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1) || broadcast) {
Harald Welte560bdb32020-12-04 22:24:47 +01001044 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001045 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001046 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1047 LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name);
1048 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001049 n_nses++;
1050 /* Only send it once to each NSE */
1051 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001052 }
Harald Welte53ee2062020-11-24 11:31:13 +01001053 }
1054 } else {
Harald Weltedf690e82020-12-12 15:58:28 +01001055 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n");
Harald Welte53ee2062020-11-24 11:31:13 +01001056 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1057 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001058
Daniel Willmann76205712020-11-30 17:08:58 +01001059 if (n_nses == 0) {
Harald Weltedf690e82020-12-12 15:58:28 +01001060 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001061 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1062 return -EINVAL;
1063 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001064 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001065}
1066
1067/* Receive an incoming BVC-RESET message from the SGSN */
Harald Weltee5209642020-12-05 19:59:45 +01001068static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp,
1069 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001070{
Harald Weltee5209642020-12-05 19:59:45 +01001071 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1072 struct gbproxy_bvc *from_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001073
Harald Weltee5209642020-12-05 19:59:45 +01001074 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001075
Harald Weltee5209642020-12-05 19:59:45 +01001076 if (ptp_bvci == 0) {
1077 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
1078 OSMO_ASSERT(from_bvc);
1079 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
1080 } else {
1081 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1082 if (!from_bvc) {
1083 LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci);
1084 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1085 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001086 }
Harald Weltee5209642020-12-05 19:59:45 +01001087 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001088 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001089
1090 return 0;
1091}
1092
1093/* Receive an incoming signalling message from the SGSN-side NS-VC */
Harald Weltedbef0aa2020-12-07 17:48:11 +01001094static 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 +02001095{
Harald Weltedbef0aa2020-12-07 17:48:11 +01001096 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001097 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +01001098 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
1099 struct gbproxy_config *cfg = nse->cfg;
1100 struct gbproxy_bvc *sgsn_bvc;
1101 struct tlv_parsed tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001102 int data_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001103 uint16_t bvci;
Harald Welteec0f8012020-12-06 16:32:01 +01001104 char log_pfx[32];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001105 int rc = 0;
1106 int cause;
Harald Welted2fef952020-12-05 00:31:07 +01001107 int i;
Daniel Willmann5614e572021-01-18 18:38:27 +01001108 bool paging_bc = false;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001109
Harald Weltee5209642020-12-05 19:59:45 +01001110 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
1111
1112 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001113
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001114 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welteec0f8012020-12-06 16:32:01 +01001115 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001116 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001117 }
1118
Harald Welte278dd272020-12-06 13:35:24 +01001119 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001120 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001121 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001122 }
1123
1124 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001125 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001126 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001127 }
1128
Harald Weltedbef0aa2020-12-07 17:48:11 +01001129 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welteec0f8012020-12-06 16:32:01 +01001130
1131 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, pdu_type, bgph->data, data_len, 0, 0,
1132 DGPRS, log_pfx);
1133 if (rc < 0) {
1134 rc = tx_status_from_tlvp(rc, msg);
Harald Welteec0f8012020-12-06 16:32:01 +01001135 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1136 return rc;
1137 }
Harald Weltee5209642020-12-05 19:59:45 +01001138 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
1139 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001140
1141 switch (pdu_type) {
1142 case BSSGP_PDUT_BVC_RESET:
Harald Weltee5209642020-12-05 19:59:45 +01001143 /* resolve or create ggbproxy_bvc + handle in BVC-FSM */
1144 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1145 rc = rx_bvc_reset_from_sgsn(nse, msg, &tp, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001146 break;
1147 case BSSGP_PDUT_BVC_RESET_ACK:
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001148 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001149 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1150 if (!sgsn_bvc)
1151 goto err_no_bvc;
1152 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1153 break;
1154 case BSSGP_PDUT_BVC_BLOCK_ACK:
1155 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1156 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1157 if (!sgsn_bvc)
1158 goto err_no_bvc;
1159 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg);
1160 break;
1161 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1162 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1163 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1164 if (!sgsn_bvc)
1165 goto err_no_bvc;
1166 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg);
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001167 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001168 case BSSGP_PDUT_FLUSH_LL:
1169 /* simple case: BVCI IE is mandatory */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001170 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001171 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1172 if (!sgsn_bvc)
1173 goto err_no_bvc;
1174 if (sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1175 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001176 break;
Daniel Willmann5614e572021-01-18 18:38:27 +01001177 case BSSGP_PDUT_DUMMY_PAGING_PS:
1178 /* Routing area is optional in dummy paging and we have nothing else to go by
1179 * so in case it is missing we need to broadcast the paging */
1180 paging_bc = true;
1181 /* fall through */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001182 case BSSGP_PDUT_PAGING_PS:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001183 {
1184 /* Cache the IMSI<->NSE to route PAGING REJECT */
1185 struct osmo_mobile_identity mi;
1186 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
1187 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
1188 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1189 gbproxy_imsi_cache_update(nse, mi.imsi);
1190 /* fall through */
1191 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001192 case BSSGP_PDUT_PAGING_CS:
1193 /* process the paging request (LAI/RAI lookup) */
Daniel Willmann5614e572021-01-18 18:38:27 +01001194 rc = gbprox_rx_paging(nse, msg, pdut_name, &tp, ns_bvci, paging_bc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001195 break;
1196 case BSSGP_PDUT_STATUS:
1197 /* Some exception has occurred */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001198 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Weltee5209642020-12-05 19:59:45 +01001199 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001200 bssgp_cause_str(cause));
Harald Welte173a1822020-12-03 15:36:59 +01001201 if (TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001202 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001203 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001204 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1205 /* don't send STATUS in response to STATUS if !bvc */
1206 if (sgsn_bvc && sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1207 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001208 } else
1209 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1210 break;
1211 /* those only exist in the SGSN -> BSS direction */
1212 case BSSGP_PDUT_SUSPEND_ACK:
1213 case BSSGP_PDUT_SUSPEND_NACK:
1214 case BSSGP_PDUT_RESUME_ACK:
1215 case BSSGP_PDUT_RESUME_NACK:
Daniel Willmann77493b12020-12-29 21:13:31 +01001216 {
1217 struct gbproxy_nse *nse_peer;
1218 uint32_t tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
1219
1220 nse_peer = gbproxy_nse_by_tlli(cfg, tlli);
1221 if (!nse_peer) {
1222 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1223 /* TODO: Counter */
1224 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1225 }
1226 /* Delete the entry after we're done */
1227 gbproxy_tlli_cache_remove(cfg, tlli);
1228 LOGPNSE(nse_peer, LOGL_DEBUG, "Rx %s: forwarding\n", pdut_name);
1229 gbprox_relay2nse(msg, nse_peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001230 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001231 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001232 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte7479c4d2020-12-02 20:06:04 +01001233 case BSSGP_PDUT_OVERLOAD:
Harald Weltee5209642020-12-05 19:59:45 +01001234 LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name);
Harald Welte560bdb32020-12-04 22:24:47 +01001235 /* broadcast to all BSS-side bvcs */
Harald Welted2fef952020-12-05 00:31:07 +01001236 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte7479c4d2020-12-02 20:06:04 +01001237 gbprox_relay2nse(msg, nse, 0);
1238 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001239 break;
Harald Weltee5209642020-12-05 19:59:45 +01001240 case BSSGP_PDUT_RAN_INFO:
1241 case BSSGP_PDUT_RAN_INFO_REQ:
1242 case BSSGP_PDUT_RAN_INFO_ACK:
1243 case BSSGP_PDUT_RAN_INFO_ERROR:
1244 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
1245 /* FIXME: route based in RIM Routing IE */
Harald Weltedbef0aa2020-12-07 17:48:11 +01001246 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Weltee5209642020-12-05 19:59:45 +01001247 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001248 default:
Harald Weltee5209642020-12-05 19:59:45 +01001249 LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name);
1250 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001251 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001252 break;
1253 }
1254
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001255 return rc;
Harald Weltee5209642020-12-05 19:59:45 +01001256
Harald Welte560bdb32020-12-04 22:24:47 +01001257err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001258 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001259 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001260 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001261}
1262
Harald Weltee5209642020-12-05 19:59:45 +01001263
1264/***********************************************************************
1265 * libosmogb NS/BSSGP integration
1266 ***********************************************************************/
1267
Alexander Couzens951e1332020-09-22 13:21:46 +02001268int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001269{
1270 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001271 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1272 struct gprs_ns2_inst *nsi = cfg->nsi;
1273 struct osmo_gprs_ns2_prim nsp = {};
1274
1275 nsp.bvci = msgb_bvci(msg);
1276 nsp.nsei = msgb_nsei(msg);
1277
1278 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
1279 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1280
1281 return rc;
1282}
1283
1284/* Main input function for Gb proxy */
1285int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1286{
Alexander Couzens951e1332020-09-22 13:21:46 +02001287 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Harald Weltee5209642020-12-05 19:59:45 +01001288 uint16_t ns_bvci = msgb_bvci(msg);
1289 uint16_t nsei = msgb_nsei(msg);
1290 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001291
Harald Welte278dd272020-12-06 13:35:24 +01001292 /* ensure minimum length to decode PCU type */
1293 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1294 return bssgp_tx_status(BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1295
Harald Weltee5209642020-12-05 19:59:45 +01001296 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN);
1297 if (nse) {
1298 if (ns_bvci == 0 || ns_bvci == 1)
1299 return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001300 else
Harald Weltee5209642020-12-05 19:59:45 +01001301 return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001302 }
1303
Harald Weltee5209642020-12-05 19:59:45 +01001304 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS);
1305 if (!nse) {
1306 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei);
1307 nse = gbproxy_nse_alloc(cfg, nsei, false);
1308 }
1309 if (nse) {
1310 if (ns_bvci == 0 || ns_bvci == 1)
1311 return gbprox_rx_sig_from_bss(nse, msg, ns_bvci);
1312 else
1313 return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci);
1314 }
1315
1316 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001317}
1318
Alexander Couzens951e1332020-09-22 13:21:46 +02001319/* TODO: What about handling:
1320 * NS_AFF_CAUSE_VC_FAILURE,
1321 NS_AFF_CAUSE_VC_RECOVERY,
1322 NS_AFF_CAUSE_FAILURE,
1323 NS_AFF_CAUSE_RECOVERY,
1324 osmocom own causes
1325 NS_AFF_CAUSE_SNS_CONFIGURED,
1326 NS_AFF_CAUSE_SNS_FAILURE,
1327 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001328
Alexander Couzens951e1332020-09-22 13:21:46 +02001329void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001330{
Harald Welte560bdb32020-12-04 22:24:47 +01001331 /* 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 +02001332 * TODO: sgsn nsei available/unavailable
1333 */
Harald Weltee5209642020-12-05 19:59:45 +01001334
Harald Welte560bdb32020-12-04 22:24:47 +01001335 struct gbproxy_bvc *bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001336 struct gbproxy_nse *sgsn_nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001337
Alexander Couzens951e1332020-09-22 13:21:46 +02001338 switch (nsp->u.status.cause) {
1339 case NS_AFF_CAUSE_SNS_FAILURE:
1340 case NS_AFF_CAUSE_SNS_CONFIGURED:
1341 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001342
Alexander Couzens951e1332020-09-22 13:21:46 +02001343 case NS_AFF_CAUSE_RECOVERY:
Harald Welte9b367d22021-01-18 13:55:51 +01001344 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Harald Weltee5209642020-12-05 19:59:45 +01001345 sgsn_nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN);
1346 if (sgsn_nse) {
1347 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
1348 bvc = gbproxy_bvc_by_bvci(sgsn_nse, 0);
1349 if (bvc)
1350 osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001351 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001352 break;
1353 case NS_AFF_CAUSE_FAILURE:
Harald Weltee5209642020-12-05 19:59:45 +01001354#if 0
Harald Weltea0f70732020-12-05 17:50:23 +01001355 if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) {
Alexander Couzens951e1332020-09-22 13:21:46 +02001356 /* sgsn */
1357 /* TODO: BSVC: block all PtP towards bss */
1358 rate_ctr_inc(&cfg->ctrg->
1359 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1360 } else {
Daniel Willmanne50550e2020-11-26 18:19:21 +01001361 /* bss became unavailable
1362 * TODO: Block all BVC belonging to that NSE */
Harald Welte560bdb32020-12-04 22:24:47 +01001363 bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei);
1364 if (!bvc) {
Alexander Couzens951e1332020-09-22 13:21:46 +02001365 /* TODO: use primitive name + status cause name */
Harald Welte560bdb32020-12-04 22:24:47 +01001366 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n",
Alexander Couzens951e1332020-09-22 13:21:46 +02001367 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001368 break;
1369 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001370
Harald Welte560bdb32020-12-04 22:24:47 +01001371 if (!bvc->blocked)
Alexander Couzens951e1332020-09-22 13:21:46 +02001372 break;
Harald Weltee5209642020-12-05 19:59:45 +01001373 hash_for_each(cfg->sgsn_nses, _sgsn, sgsn_nse, list) {
1374 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, sgsn_nse->nsei, bvc->bvci, 0);
1375 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001376 }
Harald Weltee5209642020-12-05 19:59:45 +01001377#endif
Harald Welte9b367d22021-01-18 13:55:51 +01001378 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Alexander Couzens951e1332020-09-22 13:21:46 +02001379 break;
1380 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001381 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001382 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001383 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001384 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001385}
1386
Alexander Couzens951e1332020-09-22 13:21:46 +02001387/* called by the ns layer */
1388int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1389{
1390 struct osmo_gprs_ns2_prim *nsp;
1391 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001392 uintptr_t bvci;
Alexander Couzens951e1332020-09-22 13:21:46 +02001393 int rc = 0;
1394
1395 if (oph->sap != SAP_NS)
1396 return 0;
1397
1398 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1399
1400 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte9b367d22021-01-18 13:55:51 +01001401 LOGP(DGPRS, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001402 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001403 return 0;
1404 }
1405
1406 switch (oph->primitive) {
1407 case PRIM_NS_UNIT_DATA:
Daniel Willmann8f407b12020-12-02 19:33:50 +01001408
Alexander Couzens951e1332020-09-22 13:21:46 +02001409 /* hand the message into the BSSGP implementation */
1410 msgb_bssgph(oph->msg) = oph->msg->l3h;
1411 msgb_bvci(oph->msg) = nsp->bvci;
1412 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001413 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens951e1332020-09-22 13:21:46 +02001414
Daniel Willmann8f407b12020-12-02 19:33:50 +01001415 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens951e1332020-09-22 13:21:46 +02001416 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001417 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001418 break;
1419 case PRIM_NS_STATUS:
1420 gprs_ns_prim_status_cb(cfg, nsp);
1421 break;
1422 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001423 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001424 gprs_ns2_prim_str(oph->primitive),
1425 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001426 break;
1427 }
1428
1429 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001430}
1431
1432void gbprox_reset(struct gbproxy_config *cfg)
1433{
Harald Welted2fef952020-12-05 00:31:07 +01001434 struct gbproxy_nse *nse;
1435 struct hlist_node *ntmp;
Harald Welte8b4c7942020-12-05 10:14:49 +01001436 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001437
Harald Welted2fef952020-12-05 00:31:07 +01001438 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte8b4c7942020-12-05 10:14:49 +01001439 struct gbproxy_bvc *bvc;
1440 struct hlist_node *tmp;
1441 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +01001442 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001443
1444 gbproxy_nse_free(nse);
1445 }
Harald Weltee5209642020-12-05 19:59:45 +01001446 /* FIXME: cells */
1447 /* FIXME: SGSN side BVCs (except signaling) */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001448
1449 rate_ctr_group_free(cfg->ctrg);
1450 gbproxy_init_config(cfg);
1451}
1452
Daniel Willmann77493b12020-12-29 21:13:31 +01001453static void tlli_cache_cleanup(void *data)
1454{
1455 struct gbproxy_config *cfg = data;
1456 gbproxy_tlli_cache_cleanup(cfg);
1457
1458 /* TODO: Disable timer when cache is empty */
1459 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1460}
1461
Daniel Willmannc8a50092021-01-17 13:11:41 +01001462static void imsi_cache_cleanup(void *data)
1463{
1464 struct gbproxy_config *cfg = data;
1465 gbproxy_imsi_cache_cleanup(cfg);
1466
1467 /* TODO: Disable timer when cache is empty */
1468 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1469}
1470
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001471int gbproxy_init_config(struct gbproxy_config *cfg)
1472{
1473 struct timespec tp;
1474
Harald Welte209dc9f2020-12-12 19:02:16 +01001475 /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
1476 cfg->pool.bvc_fc_ratio = 100;
Daniel Willmannee834af2020-12-14 16:22:39 +01001477 cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001478 /* TODO: Make configurable */
Daniel Willmannbd12f3f2021-01-13 18:16:04 +01001479 cfg->tlli_cache.timeout = 10;
Daniel Willmannc8a50092021-01-17 13:11:41 +01001480 cfg->imsi_cache.timeout = 10;
Daniel Willmannee834af2020-12-14 16:22:39 +01001481
Harald Welted2fef952020-12-05 00:31:07 +01001482 hash_init(cfg->bss_nses);
Daniel Willmann1e7be5d2020-12-21 18:08:21 +01001483 hash_init(cfg->sgsn_nses);
1484 hash_init(cfg->cells);
Daniel Willmann77493b12020-12-29 21:13:31 +01001485 hash_init(cfg->tlli_cache.entries);
Daniel Willmannee834af2020-12-14 16:22:39 +01001486 INIT_LLIST_HEAD(&cfg->sgsns);
1487
Daniel Willmann77493b12020-12-29 21:13:31 +01001488 osmo_timer_setup(&cfg->tlli_cache.timer, tlli_cache_cleanup, cfg);
1489 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1490
Daniel Willmannc8a50092021-01-17 13:11:41 +01001491 /* We could also combine both timers */
1492 osmo_timer_setup(&cfg->imsi_cache.timer, imsi_cache_cleanup, cfg);
1493 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1494
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001495 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1496 if (!cfg->ctrg) {
1497 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1498 return -1;
1499 }
1500 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Harald Weltec169de42020-12-07 13:12:13 +01001501 osmo_fsm_log_timeouts(true);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001502
1503 return 0;
Daniel Willmann77493b12020-12-29 21:13:31 +01001504}