blob: 8b57315c06a9c7fbc04339c00e33409fd9ba6822 [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
Harald Weltea0f70732020-12-05 17:50:23 +010087
Harald Welteec0f8012020-12-06 16:32:01 +010088/* generate BVC-STATUS message with cause value derived from TLV-parser error */
89static int tx_status_from_tlvp(enum osmo_tlv_parser_error tlv_p_err, struct msgb *orig_msg)
90{
91 uint8_t bssgp_cause;
92 switch (tlv_p_err) {
93 case OSMO_TLVP_ERR_MAND_IE_MISSING:
94 bssgp_cause = BSSGP_CAUSE_MISSING_MAND_IE;
95 break;
96 default:
97 bssgp_cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
98 }
99 return bssgp_tx_status(bssgp_cause, NULL, orig_msg);
100}
101
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200102/* strip off the NS header */
103static void strip_ns_hdr(struct msgb *msg)
104{
105 int strip_len = msgb_bssgph(msg) - msg->data;
106 msgb_pull(msg, strip_len);
107}
108
Harald Weltee5209642020-12-05 19:59:45 +0100109#if 0
Harald Welte560bdb32020-12-04 22:24:47 +0100110/* feed a message down the NS-VC associated with the specified bvc */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200111static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
112 uint16_t ns_bvci, uint16_t sgsn_nsei)
113{
114 /* create a copy of the message so the old one can
115 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens951e1332020-09-22 13:21:46 +0200116 struct gprs_ns2_inst *nsi = cfg->nsi;
117 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200118 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
119 int rc;
120
Daniel Willmann3696dce2020-12-02 16:08:02 +0100121 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200122 msgb_nsei(msg), ns_bvci, sgsn_nsei);
123
Alexander Couzens951e1332020-09-22 13:21:46 +0200124 nsp.bvci = ns_bvci;
125 nsp.nsei = sgsn_nsei;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200126
127 strip_ns_hdr(msg);
Alexander Couzens951e1332020-09-22 13:21:46 +0200128 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
129 PRIM_OP_REQUEST, msg);
130 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200131 if (rc < 0)
132 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200133 return rc;
134}
Harald Weltee5209642020-12-05 19:59:45 +0100135#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200136
Harald Weltee30985e2021-01-28 19:13:19 +0100137/*! Determine the TLLI from the given BSSGP message.
138 * \param[in] bssgp pointer to start of BSSGP header
139 * \param[in] bssgp_len length of BSSGP message in octets
140 * \param[out] tlli TLLI (if any) in host byte order
141 * \returns 1 if TLLI found; 0 if none found; negative on parse error */
142int gprs_gb_parse_tlli(const uint8_t *bssgp, size_t bssgp_len, uint32_t *tlli)
143{
144 const struct bssgp_normal_hdr *bgph;
145 uint8_t pdu_type;
146
147 if (bssgp_len < sizeof(struct bssgp_normal_hdr))
148 return -EINVAL;
149
150 bgph = (struct bssgp_normal_hdr *)bssgp;
151 pdu_type = bgph->pdu_type;
152
153 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
154 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
155 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *)bssgp;
156 if (bssgp_len < sizeof(struct bssgp_ud_hdr))
157 return -EINVAL;
158 *tlli = osmo_load32be((const uint8_t *)&budh->tlli);
159 return 1;
160 } else {
161 const uint8_t *data = bgph->data;
162 size_t data_len = bssgp_len - sizeof(*bgph);
163 struct tlv_parsed tp;
164
165 if (bssgp_tlv_parse(&tp, data, data_len) < 0)
166 return -EINVAL;
167
168 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
169 *tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
170 return 1;
171 }
172 }
173
174 /* No TLLI present in message */
175 return 0;
176}
177
Daniel Willmann76205712020-11-30 17:08:58 +0100178/* feed a message down the NSE */
179static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann35f7d332020-11-03 21:11:45 +0100180 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200181{
Daniel Willmanne50550e2020-11-26 18:19:21 +0100182 OSMO_ASSERT(nse);
183 OSMO_ASSERT(nse->cfg);
184
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200185 /* create a copy of the message so the old one can
186 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmanne50550e2020-11-26 18:19:21 +0100187 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Daniel Willmann76205712020-11-30 17:08:58 +0100188 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Daniel Willmann44fa2012021-02-12 04:55:40 +0100189 uint32_t tlli = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200190 int rc;
191
Daniel Willmann98b1b452020-12-21 10:40:27 +0100192 DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg),
193 !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200194
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200195 /* Strip the old NS header, it will be replaced with a new one */
196 strip_ns_hdr(msg);
197
Harald Weltefe059582020-11-18 12:01:46 +0100198 /* TS 48.018 Section 5.4.2: The link selector parameter is
199 * defined in 3GPP TS 48.016. At one side of the Gb interface,
200 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
201 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
202 * underlying network service. */
Daniel Willmann44fa2012021-02-12 04:55:40 +0100203 gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli);
Harald Weltefe059582020-11-18 12:01:46 +0100204
Daniel Willmann44fa2012021-02-12 04:55:40 +0100205 rc = bssgp2_nsi_tx_ptp(nsi, nse->nsei, ns_bvci, msg, tlli);
Daniel Willmann76205712020-11-30 17:08:58 +0100206 /* FIXME: We need a counter group for gbproxy_nse */
207 //if (rc < 0)
Harald Welte560bdb32020-12-04 22:24:47 +0100208 // rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Daniel Willmann76205712020-11-30 17:08:58 +0100209
210 return rc;
211}
212
Harald Welte560bdb32020-12-04 22:24:47 +0100213/* feed a message down the NS-VC associated with the specified bvc */
214static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann76205712020-11-30 17:08:58 +0100215 uint16_t ns_bvci)
216{
217 int rc;
Harald Welte560bdb32020-12-04 22:24:47 +0100218 struct gbproxy_nse *nse = bvc->nse;
Daniel Willmann76205712020-11-30 17:08:58 +0100219 OSMO_ASSERT(nse);
220
221 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200222 if (rc < 0)
Harald Welte560bdb32020-12-04 22:24:47 +0100223 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200224
225 return rc;
226}
227
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200228int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
229{
230 return 0;
231}
232
Harald Weltee5209642020-12-05 19:59:45 +0100233
234/***********************************************************************
235 * PTP BVC handling
236 ***********************************************************************/
237
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100238/* FIXME: Handle the tlli NULL case correctly,
239 * This function should take a generic selector
240 * and choose an sgsn based on that
241 */
242static struct gbproxy_sgsn *gbproxy_select_sgsn(struct gbproxy_config *cfg, const uint32_t *tlli)
243{
244 struct gbproxy_sgsn *sgsn = NULL;
245 struct gbproxy_sgsn *sgsn_avoid = NULL;
246
247 int tlli_type;
248 int16_t nri;
249 bool null_nri = false;
250
251 if (!tlli) {
252 sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);
253 if (!sgsn) {
254 return NULL;
255 }
256 LOGPSGSN(sgsn, LOGL_INFO, "Could not get TLLI, using first SGSN\n");
257 return sgsn;
258 }
259
260 if (cfg->pool.nri_bitlen == 0) {
261 /* Pooling is disabled */
262 sgsn = llist_first_entry(&cfg->sgsns, struct gbproxy_sgsn, list);
263 if (!sgsn) {
264 return NULL;
265 }
266
267 LOGPSGSN(sgsn, LOGL_INFO, "Pooling disabled, using first configured SGSN\n");
268 } else {
269 /* Pooling is enabled, try to use the NRI for routing to an SGSN
270 * See 3GPP TS 23.236 Ch. 5.3.2 */
271 tlli_type = gprs_tlli_type(*tlli);
272 if (tlli_type == TLLI_LOCAL || tlli_type == TLLI_FOREIGN) {
273 /* Only get/use the NRI if tlli type is local */
274 osmo_tmsi_nri_v_get(&nri, *tlli, cfg->pool.nri_bitlen);
275 if (nri >= 0) {
276 /* Get the SGSN for the NRI */
277 sgsn = gbproxy_sgsn_by_nri(cfg, nri, &null_nri);
278 if (sgsn && !null_nri)
279 return sgsn;
280 /* If the NRI is the null NRI, we need to avoid the chosen SGSN */
281 if (null_nri && sgsn) {
282 sgsn_avoid = sgsn;
283 }
284 } else {
285 /* We couldn't get the NRI from the TLLI */
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100286 LOGP(DGPRS, LOGL_ERROR, "Could not extract NRI from local TLLI %08x\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100287 }
Daniel Willmanncd21afe2021-01-21 18:44:51 +0100288 } else {
289 LOGP(DGPRS, LOGL_INFO, "TLLI %08x is neither local nor foreign, not routing by NRI\n", *tlli);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100290 }
291 }
292
293 /* If we haven't found an SGSN yet we need to choose one, but avoid the one in sgsn_avoid
294 * NOTE: This function is not stable if the number of SGSNs or allow_attach changes
295 * We could implement TLLI tracking here, but 3GPP TS 23.236 Ch. 5.3.2 (see NOTE) argues that
296 * we can just wait for the MS to reattempt the procedure.
297 */
298 if (!sgsn)
299 sgsn = gbproxy_sgsn_by_tlli(cfg, sgsn_avoid, *tlli);
300
301 if (!sgsn) {
302 LOGP(DGPRS, LOGL_ERROR, "No suitable SGSN found for TLLI %u\n", *tlli);
303 return NULL;
304 }
305
306 return sgsn;
307}
308
309/*! Find the correct gbproxy_bvc given a cell and an SGSN
310 * \param[in] cfg The gbproxy configuration
311 * \param[in] cell The cell the message belongs to
312 * \param[in] tlli An optional TLLI used for tracking
313 * \return Returns 0 on success, otherwise a negative value
314 */
315static struct gbproxy_bvc *gbproxy_select_sgsn_bvc(struct gbproxy_config *cfg, struct gbproxy_cell *cell, const uint32_t *tlli)
316{
317 struct gbproxy_sgsn *sgsn;
318 struct gbproxy_bvc *sgsn_bvc = NULL;
Harald Welte02d7c482020-12-30 12:13:36 +0100319 int i;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100320
321 sgsn = gbproxy_select_sgsn(cfg, tlli);
322 if (!sgsn) {
323 LOGPCELL(cell, LOGL_ERROR, "Could not find any SGSN, dropping message!\n");
324 return NULL;
325 }
326
327 /* Get the BVC for this SGSN/NSE */
Harald Welte02d7c482020-12-30 12:13:36 +0100328 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100329 sgsn_bvc = cell->sgsn_bvc[i];
330 if (!sgsn_bvc)
331 continue;
332 if (sgsn->nse != sgsn_bvc->nse)
333 continue;
334
335 return sgsn_bvc;
336 }
337
338 /* This shouldn't happen */
Daniel Willmanna648f3c2020-12-28 18:07:27 +0100339 LOGPCELL(cell, LOGL_ERROR, "Could not find matching BVC for SGSN %s, dropping message!\n", sgsn->name);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100340 return NULL;
341}
342
343/*! Send a message to the next SGSN, possibly ignoring the null SGSN
344 * route an uplink message on a PTP-BVC to a SGSN using the TLLI
345 * \param[in] cell The cell the message belongs to
346 * \param[in] msg The BSSGP message
347 * \param[in] null_sgsn If not NULL then avoid this SGSN (because this message contains its null NRI)
348 * \param[in] tlli An optional TLLI used for tracking
349 * \return Returns 0 on success, otherwise a negative value
350 */
351static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, const uint32_t *tlli,
Harald Weltee5209642020-12-05 19:59:45 +0100352 bool sig_bvci)
353{
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100354 struct gbproxy_config *cfg = cell->cfg;
Harald Weltee5209642020-12-05 19:59:45 +0100355 struct gbproxy_bvc *sgsn_bvc;
Harald Weltee5209642020-12-05 19:59:45 +0100356
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100357 sgsn_bvc = gbproxy_select_sgsn_bvc(cfg, cell, tlli);
358 if (!sgsn_bvc) {
359 LOGPCELL(cell, LOGL_NOTICE, "Could not find any SGSN for TLLI %u, dropping message!\n", *tlli);
360 return -EINVAL;
Harald Weltee5209642020-12-05 19:59:45 +0100361 }
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100362
363 return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);
Harald Weltee5209642020-12-05 19:59:45 +0100364}
365
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200366/* Receive an incoming PTP message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100367static 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 +0200368{
Harald Welte278dd272020-12-06 13:35:24 +0100369 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100370 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
371 struct gbproxy_bvc *bss_bvc;
372 struct tlv_parsed tp;
373 char log_pfx[32];
374 uint32_t tlli;
375 int rc;
376
377 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
378
379 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200380
Daniel Willmann06331ac2020-12-10 17:59:46 +0100381 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100382 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci);
Harald Welte278dd272020-12-06 13:35:24 +0100383 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
384 }
385
386 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100387 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100388 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
389 }
390
391 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100392 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100393 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
394 }
395
Harald Weltee5209642020-12-05 19:59:45 +0100396 bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
397 if (!bss_bvc) {
398 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n",
399 log_pfx, pdut_name);
400 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200401 }
402
Harald Weltee5209642020-12-05 19:59:45 +0100403 /* UL_UNITDATA has a different header than all other uplink PDUs */
404 if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA) {
405 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
406 if (msgb_bssgp_len(msg) < sizeof(*budh))
407 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
408 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
409 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
410 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
411 * doesn't have to worry where the TLLI came from */
412 tp.lv[BSSGP_IE_TLLI].len = 4;
413 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
414 } else {
415 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
416 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
417 }
418 if (rc < 0) {
419 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
420 return tx_status_from_tlvp(rc, msg);
421 }
Harald Welte85a40272020-12-08 21:43:22 +0100422 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
423 msgb_bcid(msg) = (void *)&tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200424
Harald Weltee5209642020-12-05 19:59:45 +0100425 switch (bgph->pdu_type) {
426 case BSSGP_PDUT_UL_UNITDATA:
427 case BSSGP_PDUT_RA_CAPA_UPDATE:
428 case BSSGP_PDUT_FLOW_CONTROL_MS:
429 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
430 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
431 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
432 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
433 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
434 case BSSGP_PDUT_FLOW_CONTROL_PFC:
435 case BSSGP_PDUT_DELETE_BSS_PFC_REQ:
436 case BSSGP_PDUT_PS_HO_REQUIRED:
437 case BSSGP_PDUT_PS_HO_REQUEST_ACK:
438 case BSSGP_PDUT_PS_HO_REQUEST_NACK:
439 case BSSGP_PDUT_PS_HO_COMPLETE:
440 case BSSGP_PDUT_PS_HO_CANCEL:
441 /* We can route based on TLLI-NRI */
442 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100443 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100444 break;
445 case BSSGP_PDUT_RADIO_STATUS:
446 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
447 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100448 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100449 } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {
450 /* we treat the TMSI like a TLLI and extract the NRI from it */
451 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Daniel Willmann8b3ed292021-01-21 18:46:51 +0100452 /* Convert the TMSI into a FOREIGN TLLI so it is routed appropriately */
453 tlli = gprs_tmsi2tlli(tlli, TLLI_FOREIGN);
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100454 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, &tlli, false);
Harald Weltee5209642020-12-05 19:59:45 +0100455 } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {
Daniel Willmann5193f222021-01-11 05:00:46 +0100456 /* FIXME: Use the IMSI as selector? */
Daniel Willmannd4ab1f92020-12-21 18:53:55 +0100457 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, NULL, false);
Harald Weltee5209642020-12-05 19:59:45 +0100458 } else
459 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");
460 break;
461 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
462 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +0100463 {
464 /* Route according to IMSI<->NSE cache entry */
465 struct osmo_mobile_identity mi;
466 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
467 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
468 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
469 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi);
470 if (nse) {
471 OSMO_ASSERT(nse->sgsn_facing);
472 rc = gbprox_relay2nse(msg, nse, ns_bvci);
473 } else {
Daniel Willmann82669182021-01-19 11:37:55 +0100474 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx unmatched %s with IMSI %s\n", pdut_name, mi.imsi);
Daniel Willmann5614e572021-01-18 18:38:27 +0100475 }
Harald Weltee5209642020-12-05 19:59:45 +0100476 break;
Daniel Willmann5614e572021-01-18 18:38:27 +0100477 }
Harald Weltee5209642020-12-05 19:59:45 +0100478 case BSSGP_PDUT_FLOW_CONTROL_BVC:
Harald Welte85a40272020-12-08 21:43:22 +0100479 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg);
Harald Weltee5209642020-12-05 19:59:45 +0100480 break;
481 case BSSGP_PDUT_STATUS:
482 /* TODO: Implement by inspecting the contained PDU */
483 if (!TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR))
484 break;
485 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
486 break;
487 }
488
489 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200490}
491
492/* Receive an incoming PTP message from a SGSN-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100493static 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 +0200494{
Harald Welte278dd272020-12-06 13:35:24 +0100495 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltee5209642020-12-05 19:59:45 +0100496 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
497 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte85a40272020-12-08 21:43:22 +0100498 struct tlv_parsed tp;
Harald Weltee5209642020-12-05 19:59:45 +0100499 char log_pfx[32];
Harald Welte85a40272020-12-08 21:43:22 +0100500 int rc;
Harald Weltee5209642020-12-05 19:59:45 +0100501
502 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
503
504 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200505
Daniel Willmann06331ac2020-12-10 17:59:46 +0100506 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100507 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx);
Harald Welte278dd272020-12-06 13:35:24 +0100508 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
509 }
510
511 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Weltee5209642020-12-05 19:59:45 +0100512 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100513 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
514 }
515
516 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100517 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100518 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
519 }
520
Harald Weltee5209642020-12-05 19:59:45 +0100521 sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
522 if (!sgsn_bvc) {
523 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n",
524 log_pfx, pdut_name);
525 rate_ctr_inc(&nse->cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_BVCI]);
526 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200527 }
528
Harald Weltee5209642020-12-05 19:59:45 +0100529 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) {
530 LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name);
531 rate_ctr_inc(&sgsn_bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200532 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
533 }
Harald Welte85a40272020-12-08 21:43:22 +0100534
535 /* DL_UNITDATA has a different header than all other uplink PDUs */
536 if (bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) {
537 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
538 if (msgb_bssgp_len(msg) < sizeof(*budh))
539 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
540 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
541 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
542 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
543 * doesn't have to worry where the TLLI came from */
544 tp.lv[BSSGP_IE_TLLI].len = 4;
545 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
546 } else {
547 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
548 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
549 }
550 if (rc < 0) {
551 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
552 return tx_status_from_tlvp(rc, msg);
553 }
554 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
555 msgb_bcid(msg) = (void *)&tp;
556
Harald Weltee5209642020-12-05 19:59:45 +0100557 OSMO_ASSERT(sgsn_bvc->cell);
558 bss_bvc = sgsn_bvc->cell->bss_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200559
Harald Welte85a40272020-12-08 21:43:22 +0100560 switch (bgph->pdu_type) {
561 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
562 return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg);
Daniel Willmann5614e572021-01-18 18:38:27 +0100563 case BSSGP_PDUT_DUMMY_PAGING_PS:
564 case BSSGP_PDUT_PAGING_PS:
565 {
566 /* Cache the IMSI<->NSE to route PAGING REJECT */
567 struct osmo_mobile_identity mi;
568 const uint8_t *mi_data = TLVP_VAL(&tp, BSSGP_IE_IMSI);
569 uint8_t mi_len = TLVP_LEN(&tp, BSSGP_IE_IMSI);
570 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
571 gbproxy_imsi_cache_update(nse, mi.imsi);
572 break;
Harald Welte85a40272020-12-08 21:43:22 +0100573 }
Daniel Willmann5614e572021-01-18 18:38:27 +0100574 default:
575 break;
576 }
577 return gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci);
Harald Welte85a40272020-12-08 21:43:22 +0100578
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200579}
580
Harald Weltee5209642020-12-05 19:59:45 +0100581/***********************************************************************
582 * BVC FSM call-backs
583 ***********************************************************************/
Harald Welte7df1e5a2020-12-02 22:53:26 +0100584
Harald Weltee5209642020-12-05 19:59:45 +0100585/* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */
586static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv)
587{
588 unsigned int i;
589
590 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
591 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i];
592 if (!sgsn_bvc)
593 continue;
594 osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv);
595 }
596}
597
598/* BVC FSM informs us about a BSS-side reset of the signaling BVC */
599static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
600 uint16_t cell_id, uint8_t cause, void *priv)
601{
602 struct gbproxy_bvc *sig_bvc = priv;
603 struct gbproxy_nse *nse = sig_bvc->nse;
604 struct gbproxy_bvc *ptp_bvc;
605 unsigned int i;
606
607 /* BLOCK all SGSN-side PTP BVC within this NSE */
608 hash_for_each(nse->bvcs, i, ptp_bvc, list) {
609 if (ptp_bvc == sig_bvc)
610 continue;
611 OSMO_ASSERT(ptp_bvc->cell);
612
613 dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Harald Weltef9e149b2020-12-02 23:29:38 +0100614 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100615
Harald Weltee5209642020-12-05 19:59:45 +0100616 /* Delete all BSS-side PTP BVC within this NSE */
617 gbproxy_cleanup_bvcs(nse, 0);
618
619 /* TODO: we keep the "CELL" around for now, re-connecting it to
620 * any (later) new PTP-BVC for that BVCI. Not sure if that's the
621 * best idea ? */
622}
623
624/* forward declaration */
625static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops;
626
627static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = {
628 .reset_notification = bss_sig_bvc_reset_notif,
629};
630
631/* BVC FSM informs us about a BSS-side reset of a PTP BVC */
632static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
633 uint16_t cell_id, uint8_t cause, void *priv)
634{
635 struct gbproxy_bvc *bvc = priv;
636 struct gbproxy_config *cfg = bvc->nse->cfg;
Harald Welte664c24e2020-12-12 15:01:17 +0100637 struct gbproxy_nse *sgsn_nse;
Harald Weltee5209642020-12-05 19:59:45 +0100638 unsigned int i;
639
640 OSMO_ASSERT(bvci != 0);
641
642 if (!bvc->cell) {
643 /* see if we have a CELL dangling around */
644 bvc->cell = gbproxy_cell_by_bvci(cfg, bvci);
645 if (bvc->cell) {
646 /* the CELL already exists. This means either it * was created before at an
647 * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a
648 * malconfiguration */
649 if (bvc->cell->bss_bvc) {
650 LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already "
651 "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei);
652 LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting "
653 "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei);
654 gbproxy_bvc_free(bvc->cell->bss_bvc);
655 }
656 bvc->cell->bss_bvc = bvc;
657 }
658 }
659
660 if (!bvc->cell) {
Harald Weltee5209642020-12-05 19:59:45 +0100661 /* if we end up here, it means this is the first time we received a BVC-RESET
662 * for this BVC. We need to create the 'cell' data structure and the SGSN-side
663 * BVC counterparts */
664
Philipp Maiere4597ec2021-02-09 16:02:00 +0100665 bvc->cell = gbproxy_cell_alloc(cfg, bvci, ra_id, cell_id);
Harald Weltee5209642020-12-05 19:59:45 +0100666 OSMO_ASSERT(bvc->cell);
667
668 /* link us to the cell and vice-versa */
669 bvc->cell->bss_bvc = bvc;
Harald Welte664c24e2020-12-12 15:01:17 +0100670 }
Harald Weltee5209642020-12-05 19:59:45 +0100671
Harald Welte664c24e2020-12-12 15:01:17 +0100672 /* allocate (any missing) SGSN-side BVCs within the cell, and reset them */
673 hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) {
674 struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
675 if (sgsn_bvc)
676 OSMO_ASSERT(sgsn_bvc->cell == bvc->cell || !sgsn_bvc->cell);
Harald Weltee5209642020-12-05 19:59:45 +0100677
Harald Welte664c24e2020-12-12 15:01:17 +0100678 if (!sgsn_bvc) {
679 sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci);
680 OSMO_ASSERT(sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100681
Harald Welte664c24e2020-12-12 15:01:17 +0100682 sgsn_bvc->cell = bvc->cell;
Philipp Maierda3af942021-02-04 21:54:09 +0100683 memcpy(&sgsn_bvc->raid, &bvc->cell->id.raid, sizeof(sgsn_bvc->raid));
Harald Welte664c24e2020-12-12 15:01:17 +0100684 sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei,
685 bvci, ra_id, cell_id);
686 OSMO_ASSERT(sgsn_bvc->fi);
687 bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100688
Harald Welte664c24e2020-12-12 15:01:17 +0100689 gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100690 }
691 }
692
693 /* Trigger outbound BVC-RESET procedure toward each SGSN */
694 dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause);
695}
696
697/* BVC FSM informs us about a BSS-side FSM state change */
698static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv)
699{
700 struct gbproxy_bvc *bvc = priv;
701 struct gbproxy_cell *cell = bvc->cell;
702 uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi);
703
704 /* we have just been created but due to callback ordering the cell is not associated */
705 if (!cell)
706 return;
707
708 switch (state) {
709 case BSSGP_BVCFSM_S_BLOCKED:
710 /* block the corresponding SGSN-side PTP BVCs */
711 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
712 break;
713 case BSSGP_BVCFSM_S_UNBLOCKED:
714 /* unblock the corresponding SGSN-side PTP BVCs */
715 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL);
716 break;
717 }
718}
719
Harald Welte85a40272020-12-08 21:43:22 +0100720/* BVC FSM informs us about BVC-FC PDU receive */
721static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
722{
Harald Welte209dc9f2020-12-12 19:02:16 +0100723 struct bssgp2_flow_ctrl fc_reduced;
Harald Welte85a40272020-12-08 21:43:22 +0100724 struct gbproxy_bvc *bss_bvc = priv;
Harald Welte209dc9f2020-12-12 19:02:16 +0100725 struct gbproxy_cell *cell;
726 struct gbproxy_config *cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100727
Harald Welte209dc9f2020-12-12 19:02:16 +0100728 OSMO_ASSERT(bss_bvc);
729 OSMO_ASSERT(fc);
730
731 cell = bss_bvc->cell;
Harald Welte85a40272020-12-08 21:43:22 +0100732 if (!cell)
733 return;
734
Harald Welte209dc9f2020-12-12 19:02:16 +0100735 cfg = cell->cfg;
Harald Welte85a40272020-12-08 21:43:22 +0100736
Harald Welte209dc9f2020-12-12 19:02:16 +0100737 /* reduce / scale according to configuration to make sure we only advertise a fraction
738 * of the capacity to each of the SGSNs in the pool */
739 fc_reduced = *fc;
740 fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
741 fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
742 /* we don't modify the per-MS related values as any single MS is only served by one SGSN */
743
744 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
Harald Welte85a40272020-12-08 21:43:22 +0100745}
746
Harald Weltee5209642020-12-05 19:59:45 +0100747static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
748 .reset_notification = bss_ptp_bvc_reset_notif,
749 .state_chg_notification = bss_ptp_bvc_state_chg_notif,
Harald Welte85a40272020-12-08 21:43:22 +0100750 .rx_fc_bvc = bss_ptp_bvc_fc_bvc,
Harald Weltee5209642020-12-05 19:59:45 +0100751};
752
753/* BVC FSM informs us about a SGSN-side reset of a PTP BVC */
754static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
755 uint16_t cell_id, uint8_t cause, void *priv)
756{
757 struct gbproxy_bvc *bvc = priv;
758
759 if (!bvc->cell) {
760 LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n");
761 return;
762 }
763
764 OSMO_ASSERT(bvc->cell->bss_bvc);
765
766 /* request reset of BSS-facing PTP-BVC */
767 osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
768}
769
770static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = {
771 .reset_notification = sgsn_ptp_bvc_reset_notif,
772};
773
774/* BVC FSM informs us about a SGSN-side reset of the signaling BVC */
775static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
776 uint16_t cell_id, uint8_t cause, void *priv)
777{
778 struct gbproxy_bvc *bvc = priv;
779 struct gbproxy_config *cfg = bvc->nse->cfg;
780 struct gbproxy_nse *bss_nse;
781 unsigned int i;
782
783 /* delete all SGSN-side PTP BVC for this SGSN */
784 gbproxy_cleanup_bvcs(bvc->nse, 0);
785 /* FIXME: what to do about the cells? */
786 /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */
787
788 /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */
789 hash_for_each(cfg->bss_nses, i, bss_nse, list) {
790 struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0);
791 if (!bss_bvc) {
792 LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n");
793 continue;
794 }
795 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
796 }
797}
798
799const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = {
800 .reset_notification = sgsn_sig_bvc_reset_notif,
801};
802
803/***********************************************************************
804 * Signaling BVC handling
805 ***********************************************************************/
806
807/* process a BVC-RESET message from the BSS side */
808static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp)
809{
810 struct gbproxy_bvc *from_bvc = NULL;
811 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
812 uint32_t features = 0; // FIXME: make configurable
813
814 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci);
815
Harald Welte314647b2020-12-02 23:03:22 +0100816 if (bvci == 0) {
817 /* If we receive a BVC reset on the signalling endpoint, we
818 * don't want the SGSN to reset, as the signalling endpoint
819 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte324f0652020-12-02 23:06:37 +0100820
Harald Weltee5209642020-12-05 19:59:45 +0100821 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
Harald Welte560bdb32020-12-04 22:24:47 +0100822 if (!from_bvc) {
Harald Weltee5209642020-12-05 19:59:45 +0100823 from_bvc = gbproxy_bvc_alloc(nse, 0);
824 OSMO_ASSERT(from_bvc);
825 from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features);
826 if (!from_bvc->fi) {
827 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
828 gbproxy_bvc_free(from_bvc);
829 return -ENOMEM;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100830 }
Harald Weltee5209642020-12-05 19:59:45 +0100831 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc);
832 }
833 } else {
834 from_bvc = gbproxy_bvc_by_bvci(nse, bvci);
835 if (!from_bvc) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100836 /* if a PTP-BVC is reset, and we don't know that
Harald Welte560bdb32020-12-04 22:24:47 +0100837 * PTP-BVCI yet, we should allocate a new bvc */
838 from_bvc = gbproxy_bvc_alloc(nse, bvci);
839 OSMO_ASSERT(from_bvc);
Harald Weltee5209642020-12-05 19:59:45 +0100840 from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi,
841 nse->nsei, bvci);
842 if (!from_bvc->fi) {
843 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
844 gbproxy_bvc_free(from_bvc);
845 return -ENOMEM;
846 }
847 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100848 }
Harald Weltee5209642020-12-05 19:59:45 +0100849#if 0
Harald Welte7df1e5a2020-12-02 22:53:26 +0100850 /* Could have moved to a different NSE */
Harald Welte560bdb32020-12-04 22:24:47 +0100851 if (!check_bvc_nsei(from_bvc, nsei)) {
852 LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100853
Harald Weltee5209642020-12-05 19:59:45 +0100854 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei, false);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100855 if (!nse_new) {
856 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
857 "BVCI=%05u\n", bvci, nsei);
858 bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
859 return 0;
860 }
861
Harald Welte560bdb32020-12-04 22:24:47 +0100862 /* Move bvc to different NSE */
863 gbproxy_bvc_move(from_bvc, nse_new);
Harald Welte7df1e5a2020-12-02 22:53:26 +0100864 }
Harald Weltee5209642020-12-05 19:59:45 +0100865#endif
866 /* FIXME: do we need this, if it happens within FSM? */
Harald Welte173a1822020-12-03 15:36:59 +0100867 if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welte7df1e5a2020-12-02 22:53:26 +0100868 struct gprs_ra_id raid;
869 /* We have a Cell Identifier present in this
870 * PDU, this means we can extend our local
871 * state information about this particular cell
872 * */
Philipp Maierda3af942021-02-04 21:54:09 +0100873 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_CELL_ID));
874 memcpy(&from_bvc->raid, &raid, sizeof(from_bvc->raid));
Harald Welte560bdb32020-12-04 22:24:47 +0100875 LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welte7df1e5a2020-12-02 22:53:26 +0100876 }
Harald Welte7df1e5a2020-12-02 22:53:26 +0100877 }
Harald Weltee5209642020-12-05 19:59:45 +0100878 /* hand into FSM for further processing */
879 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
880 return 0;
Harald Welte7df1e5a2020-12-02 22:53:26 +0100881}
882
Philipp Maier1c5766b2021-02-09 17:03:03 +0100883/* Receive an incoming RIM message from a BSS-side NS-VC */
884static int gbprox_rx_rim_from_bss(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
885 const char *pdut_name)
886{
887 struct gbproxy_sgsn *sgsn;
888 struct gbproxy_cell *dest_cell;
889 struct gbproxy_cell *src_cell;
890 struct bssgp_rim_routing_info dest_ri;
891 struct bssgp_rim_routing_info src_ri;
892 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +0100893 char ri_src_str[64];
894 char ri_dest_str[64];
Philipp Maier1c5766b2021-02-09 17:03:03 +0100895
896 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
897 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
898 if (rc < 0) {
899 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
900 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
901 }
902 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
903 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
904 if (rc < 0) {
905 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
906 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
907 }
908
909 /* Since gbproxy is 2G only we do not expect to get RIM messages only from GERAN cells. */
910 if (src_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
911 LOGP(DGPRS, LOGL_ERROR, "%s %s source RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
912 bssgp_rim_ri_name(&src_ri));
913 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
914 }
915
916 /* Lookup source cell to make sure that the source RIM routing information actually belongs
917 * to a valid cell that we know */
918 src_cell = gbproxy_cell_by_cellid(nse->cfg, &src_ri.geran.raid, src_ri.geran.cid);
919 if (!src_cell) {
920 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for source RIM routing info (%s)\n", log_pfx,
921 pdut_name, bssgp_rim_ri_name(&src_ri));
922 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
923 }
924
925 /* TODO: Use bssgp_bvc_get_features_negotiated(src_cell->bss_bvc->fi) to check if the the BSS sided BVC actually
926 * did negotiate RIM support. If not we should respond with a BSSGP STATUS message. The cause code should be
927 * BSSGP_CAUSE_PDU_INCOMP_FEAT. */
928
929 /* If Destination is known by gbproxy, route directly */
930 if (dest_ri.discr == BSSGP_RIM_ROUTING_INFO_GERAN) {
931 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
932 if (dest_cell) {
933 /* TODO: Also check if dest_cell->bss_bvc is RIM-capable (see also above). If not we should
934 * respond with a BSSGP STATUS message as well because it also would make no sense to try
935 * routing the RIM message to the next RIM-capable SGSN. */
Philipp Maier4499cf42021-02-10 17:54:44 +0100936 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to peer (nsei=%u) RIM-PDU: src=%s, dest=%s\n",
937 log_pfx, pdut_name, dest_cell->bss_bvc->nse->nsei,
938 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
939 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100940 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
941 }
942 }
943
944 /* Otherwise pass on to a RIM-capable SGSN */
945 /* TODO: We need to extend gbproxy_select_sgsn() so that it selects a RIM-capable SGSN, at the moment we just
946 * get any SGSN and just assume that it is RIM-capable. */
947 sgsn = gbproxy_select_sgsn(nse->cfg, NULL);
948 if (!sgsn) {
949 LOGP(DGPRS, LOGL_NOTICE,
950 "%s %s cannot route RIM message (%s to %s) since no RIM capable SGSN is found!\n", log_pfx,
951 pdut_name, bssgp_rim_ri_name(&src_ri), bssgp_rim_ri_name(&dest_ri));
952 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
953 }
Philipp Maier4499cf42021-02-10 17:54:44 +0100954 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying to SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
955 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
956 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
957 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +0100958
959 return gbprox_relay2nse(msg, sgsn->nse, 0);
960}
961
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200962/* Receive an incoming signalling message from a BSS-side NS-VC */
Harald Weltee5209642020-12-05 19:59:45 +0100963static 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 +0200964{
965 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200966 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +0100967 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
Philipp Maier74882dc2021-02-04 16:31:46 +0100968 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200969 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte560bdb32020-12-04 22:24:47 +0100970 struct gbproxy_bvc *from_bvc = NULL;
Harald Welteec0f8012020-12-06 16:32:01 +0100971 char log_pfx[32];
Harald Weltee5209642020-12-05 19:59:45 +0100972 uint16_t ptp_bvci;
973 uint32_t tlli;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200974 int rc;
975
Harald Weltee5209642020-12-05 19:59:45 +0100976 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
977
978 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +0100979
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200980 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Weltee5209642020-12-05 19:59:45 +0100981 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 +0100982 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200983 }
984
Harald Welte278dd272020-12-06 13:35:24 +0100985 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +0100986 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100987 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
988 }
989
990 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) {
Harald Weltee5209642020-12-05 19:59:45 +0100991 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Harald Welte278dd272020-12-06 13:35:24 +0100992 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +0200993 }
994
Philipp Maier74882dc2021-02-04 16:31:46 +0100995 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 +0100996 DGPRS, log_pfx);
997 if (rc < 0) {
Harald Weltee5209642020-12-05 19:59:45 +0100998 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welteec0f8012020-12-06 16:32:01 +0100999 return tx_status_from_tlvp(rc, msg);
1000 }
Harald Weltee5209642020-12-05 19:59:45 +01001001 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001002 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001003
Harald Weltee5209642020-12-05 19:59:45 +01001004 /* special case handling for some PDU types */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001005 switch (pdu_type) {
Harald Weltee5209642020-12-05 19:59:45 +01001006 case BSSGP_PDUT_BVC_RESET:
1007 /* resolve or create gbproxy_bvc + handlei n BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001008 return rx_bvc_reset_from_bss(nse, msg, &tp[0]);
Harald Weltee5209642020-12-05 19:59:45 +01001009 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001010 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001011 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
Harald Welte560bdb32020-12-04 22:24:47 +01001012 if (!from_bvc)
1013 goto err_no_bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001014 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1015 case BSSGP_PDUT_BVC_BLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001016 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001017 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1018 if (!from_bvc)
1019 goto err_no_bvc;
1020 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg);
1021 case BSSGP_PDUT_BVC_UNBLOCK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001022 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001023 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1024 if (!from_bvc)
1025 goto err_no_bvc;
1026 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg);
1027 case BSSGP_PDUT_SUSPEND:
1028 case BSSGP_PDUT_RESUME:
Daniel Willmann77493b12020-12-29 21:13:31 +01001029 {
1030 struct gbproxy_sgsn *sgsn;
1031
Philipp Maier74882dc2021-02-04 16:31:46 +01001032 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001033 sgsn = gbproxy_select_sgsn(nse->cfg, &tlli);
1034 if (!sgsn) {
1035 LOGP(DGPRS, LOGL_ERROR, "Could not find any SGSN for TLLI, dropping message!\n");
1036 rc = -EINVAL;
1037 break;
1038 }
1039
1040 gbproxy_tlli_cache_update(nse, tlli);
1041
1042 rc = gbprox_relay2nse(msg, sgsn->nse, 0);
Harald Weltee5209642020-12-05 19:59:45 +01001043#if 0
1044 /* TODO: Validate the RAI for consistency with the RAI
1045 * we expect for any of the BVC within this BSS side NSE */
Philipp Maier74882dc2021-02-04 16:31:46 +01001046 memcpy(ra, TLVP_VAL(&tp[0], BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra));
Harald Welte560bdb32020-12-04 22:24:47 +01001047 gsm48_parse_ra(&raid, from_bvc->ra);
Harald Weltee5209642020-12-05 19:59:45 +01001048#endif
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001049 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001050 }
Harald Weltee5209642020-12-05 19:59:45 +01001051 case BSSGP_PDUT_STATUS:
1052 /* FIXME: inspect the erroneous PDU IE (if any) and check
1053 * if we can extract a TLLI/RNI to route it to the correct SGSN */
1054 break;
1055 case BSSGP_PDUT_RAN_INFO:
1056 case BSSGP_PDUT_RAN_INFO_REQ:
1057 case BSSGP_PDUT_RAN_INFO_ACK:
1058 case BSSGP_PDUT_RAN_INFO_ERROR:
1059 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001060 rc = gbprox_rx_rim_from_bss(tp, nse, msg, log_pfx, pdut_name);
Harald Weltee5209642020-12-05 19:59:45 +01001061 break;
1062 case BSSGP_PDUT_LLC_DISCARD:
1063 case BSSGP_PDUT_FLUSH_LL_ACK:
1064 /* route based on BVCI + TLLI */
Philipp Maier74882dc2021-02-04 16:31:46 +01001065 ptp_bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1066 tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Harald Weltee5209642020-12-05 19:59:45 +01001067 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1068 if (!from_bvc)
1069 goto err_no_bvc;
Daniel Willmannd4ab1f92020-12-21 18:53:55 +01001070 gbprox_bss2sgsn_tlli(from_bvc->cell, msg, &tlli, true);
Harald Weltee5209642020-12-05 19:59:45 +01001071 break;
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001072 case BSSGP_PDUT_PAGING_PS_REJECT:
Daniel Willmann5614e572021-01-18 18:38:27 +01001073 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001074 {
1075 /* Route according to IMSI<->NSE cache entry */
1076 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001077 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1078 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001079 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1080 nse = gbproxy_nse_by_imsi(nse->cfg, mi.imsi);
1081 if (!nse) {
1082 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1083 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001084 OSMO_ASSERT(nse->sgsn_facing);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001085 rc = gbprox_relay2nse(msg, nse, 0);
1086 break;
1087 }
Harald Weltee5209642020-12-05 19:59:45 +01001088 default:
1089 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001090 break;
1091 }
1092
Harald Weltee5209642020-12-05 19:59:45 +01001093 return rc;
Harald Welte560bdb32020-12-04 22:24:47 +01001094err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001095 LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci);
1096 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001097 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001098}
1099
1100/* Receive paging request from SGSN, we need to relay to proper BSS */
Harald Weltedf690e82020-12-12 15:58:28 +01001101static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name,
Daniel Willmann5614e572021-01-18 18:38:27 +01001102 struct tlv_parsed *tp, uint16_t ns_bvci, bool broadcast)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001103{
Harald Weltedf690e82020-12-12 15:58:28 +01001104 struct gbproxy_config *cfg = sgsn_nse->cfg;
Harald Weltee5209642020-12-05 19:59:45 +01001105 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Weltedf690e82020-12-12 15:58:28 +01001106 struct gbproxy_nse *nse;
Daniel Willmann76205712020-11-30 17:08:58 +01001107 unsigned int n_nses = 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001108 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte8b4c7942020-12-05 10:14:49 +01001109 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001110
Daniel Willmanne50550e2020-11-26 18:19:21 +01001111 /* FIXME: Handle paging logic to only page each matching NSE */
1112
Harald Welte173a1822020-12-03 15:36:59 +01001113 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001114 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001115 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Weltedf690e82020-12-12 15:58:28 +01001116 sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001117 if (!sgsn_bvc) {
Harald Weltedf690e82020-12-12 15:58:28 +01001118 LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n",
Harald Weltee5209642020-12-05 19:59:45 +01001119 pdut_name, bvci);
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001120 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1121 return -EINVAL;
1122 }
Harald Weltee5209642020-12-05 19:59:45 +01001123 LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name);
1124 return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte173a1822020-12-03 15:36:59 +01001125 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001126 struct gprs_ra_id raid;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001127 errctr = GBPROX_GLOB_CTR_INV_RAI;
Philipp Maierda3af942021-02-04 21:54:09 +01001128 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
Harald Welte560bdb32020-12-04 22:24:47 +01001129 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001130 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001131 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Philipp Maierda3af942021-02-04 21:54:09 +01001132 if (gsm48_ra_equal(&bss_bvc->raid, &raid)) {
Harald Weltee5209642020-12-05 19:59:45 +01001133 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n",
1134 pdut_name);
1135 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001136 n_nses++;
1137 /* Only send it once to each NSE */
1138 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001139 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001140 }
1141 }
Harald Welte173a1822020-12-03 15:36:59 +01001142 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Philipp Maierda3af942021-02-04 21:54:09 +01001143 struct gsm48_ra_id lac;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001144 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte560bdb32020-12-04 22:24:47 +01001145 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001146 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001147 hash_for_each(nse->bvcs, j, bss_bvc, list) {
Philipp Maierda3af942021-02-04 21:54:09 +01001148 gsm48_encode_ra(&lac, &bss_bvc->raid);
1149 if (!memcmp(&lac, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Harald Weltee5209642020-12-05 19:59:45 +01001150 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n",
1151 pdut_name);
1152 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001153 n_nses++;
1154 /* Only send it once to each NSE */
1155 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001156 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001157 }
1158 }
Daniel Willmann5614e572021-01-18 18:38:27 +01001159 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1) || broadcast) {
Harald Welte560bdb32020-12-04 22:24:47 +01001160 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welted2fef952020-12-05 00:31:07 +01001161 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltee5209642020-12-05 19:59:45 +01001162 hash_for_each(nse->bvcs, j, bss_bvc, list) {
1163 LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name);
1164 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann76205712020-11-30 17:08:58 +01001165 n_nses++;
1166 /* Only send it once to each NSE */
1167 break;
Daniel Willmanne50550e2020-11-26 18:19:21 +01001168 }
Harald Welte53ee2062020-11-24 11:31:13 +01001169 }
1170 } else {
Harald Weltedf690e82020-12-12 15:58:28 +01001171 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n");
Harald Welte53ee2062020-11-24 11:31:13 +01001172 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1173 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001174
Daniel Willmann76205712020-11-30 17:08:58 +01001175 if (n_nses == 0) {
Harald Weltedf690e82020-12-12 15:58:28 +01001176 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n");
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001177 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
1178 return -EINVAL;
1179 }
Harald Welte3d1bd4d2020-11-23 15:14:20 +01001180 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001181}
1182
1183/* Receive an incoming BVC-RESET message from the SGSN */
Harald Weltee5209642020-12-05 19:59:45 +01001184static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp,
1185 uint16_t ns_bvci)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001186{
Harald Weltee5209642020-12-05 19:59:45 +01001187 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
1188 struct gbproxy_bvc *from_bvc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001189
Harald Weltee5209642020-12-05 19:59:45 +01001190 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001191
Harald Weltee5209642020-12-05 19:59:45 +01001192 if (ptp_bvci == 0) {
1193 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
1194 OSMO_ASSERT(from_bvc);
1195 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
1196 } else {
1197 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
1198 if (!from_bvc) {
1199 LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci);
1200 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
1201 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001202 }
Harald Weltee5209642020-12-05 19:59:45 +01001203 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001204 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001205
1206 return 0;
1207}
1208
Philipp Maier1c5766b2021-02-09 17:03:03 +01001209/* Receive an incoming RIM message from the SGSN-side NS-VC */
1210static int gbprox_rx_rim_from_sgsn(struct tlv_parsed *tp, struct gbproxy_nse *nse, struct msgb *msg, char *log_pfx,
1211 const char *pdut_name)
1212{
1213 struct gbproxy_sgsn *sgsn;
1214 struct gbproxy_cell *dest_cell;
1215 struct bssgp_rim_routing_info dest_ri;
1216 struct bssgp_rim_routing_info src_ri;
1217 int rc;
Philipp Maier4499cf42021-02-10 17:54:44 +01001218 char ri_src_str[64];
1219 char ri_dest_str[64];
Philipp Maier1c5766b2021-02-09 17:03:03 +01001220
1221 /* TODO: Reply with STATUS if BSSGP didn't negotiate RIM feature, see also comments in
1222 gbprox_rx_rim_from_bss() */
1223
1224 rc = bssgp_parse_rim_ri(&dest_ri, TLVP_VAL(&tp[0], BSSGP_IE_RIM_ROUTING_INFO),
1225 TLVP_LEN(&tp[0], BSSGP_IE_RIM_ROUTING_INFO));
1226 if (rc < 0) {
1227 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse destination RIM routing info\n", log_pfx, pdut_name);
1228 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1229 }
1230 rc = bssgp_parse_rim_ri(&src_ri, TLVP_VAL(&tp[1], BSSGP_IE_RIM_ROUTING_INFO),
1231 TLVP_LEN(&tp[1], BSSGP_IE_RIM_ROUTING_INFO));
1232 if (rc < 0) {
1233 LOGP(DGPRS, LOGL_ERROR, "%s %s cannot parse source RIM routing info\n", log_pfx, pdut_name);
1234 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1235 }
1236
1237 /* Since gbproxy is 2G only we do not expect to get RIM messages that target non-GERAN cells. */
1238 if (dest_ri.discr != BSSGP_RIM_ROUTING_INFO_GERAN) {
1239 LOGP(DGPRS, LOGL_ERROR, "%s %s destination RIM routing info is not GERAN (%s)\n", log_pfx, pdut_name,
1240 bssgp_rim_ri_name(&dest_ri));
1241 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
1242 }
1243
1244 /* Lookup destination cell */
1245 dest_cell = gbproxy_cell_by_cellid(nse->cfg, &dest_ri.geran.raid, dest_ri.geran.cid);
1246 if (!dest_cell) {
1247 LOGP(DGPRS, LOGL_NOTICE, "%s %s cannot find cell for destination RIM routing info (%s)\n", log_pfx,
1248 pdut_name, bssgp_rim_ri_name(&dest_ri));
1249 return bssgp_tx_status(BSSGP_CAUSE_UNKN_RIM_AI, NULL, msg);
1250 }
1251
1252 /* TODO: Check if the BVC of the destination cell actually did negotiate RIM support, see also comments
1253 * in gbprox_rx_rim_from_bss() */
1254 sgsn = gbproxy_sgsn_by_nsei(nse->cfg, nse->nsei);
1255 OSMO_ASSERT(sgsn);
1256
Philipp Maier4499cf42021-02-10 17:54:44 +01001257 LOGP(DLBSSGP, LOGL_DEBUG, "%s %s relaying from SGSN(%05u/%s) RIM-PDU: src=%s, dest=%s\n",
1258 log_pfx, pdut_name, sgsn->nse->nsei, sgsn->name,
1259 bssgp_rim_ri_name_buf(ri_src_str, sizeof(ri_src_str), &src_ri),
1260 bssgp_rim_ri_name_buf(ri_dest_str, sizeof(ri_dest_str), &dest_ri));
Philipp Maier1c5766b2021-02-09 17:03:03 +01001261
1262 return gbprox_relay2peer(msg, dest_cell->bss_bvc, 0);
1263}
1264
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001265/* Receive an incoming signalling message from the SGSN-side NS-VC */
Harald Weltedbef0aa2020-12-07 17:48:11 +01001266static 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 +02001267{
Harald Weltedbef0aa2020-12-07 17:48:11 +01001268 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001269 uint8_t pdu_type = bgph->pdu_type;
Harald Weltee5209642020-12-05 19:59:45 +01001270 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
1271 struct gbproxy_config *cfg = nse->cfg;
1272 struct gbproxy_bvc *sgsn_bvc;
Philipp Maier74882dc2021-02-04 16:31:46 +01001273 struct tlv_parsed tp[2];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001274 int data_len;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001275 uint16_t bvci;
Harald Welteec0f8012020-12-06 16:32:01 +01001276 char log_pfx[32];
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001277 int rc = 0;
1278 int cause;
Harald Welted2fef952020-12-05 00:31:07 +01001279 int i;
Daniel Willmann5614e572021-01-18 18:38:27 +01001280 bool paging_bc = false;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001281
Harald Weltee5209642020-12-05 19:59:45 +01001282 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
1283
1284 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welteec0f8012020-12-06 16:32:01 +01001285
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001286 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welteec0f8012020-12-06 16:32:01 +01001287 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001288 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001289 }
1290
Harald Welte278dd272020-12-06 13:35:24 +01001291 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Weltee5209642020-12-05 19:59:45 +01001292 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001293 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte278dd272020-12-06 13:35:24 +01001294 }
1295
1296 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) {
Harald Weltee5209642020-12-05 19:59:45 +01001297 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001298 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001299 }
1300
Harald Weltedbef0aa2020-12-07 17:48:11 +01001301 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welteec0f8012020-12-06 16:32:01 +01001302
Philipp Maier74882dc2021-02-04 16:31:46 +01001303 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 +01001304 DGPRS, log_pfx);
1305 if (rc < 0) {
1306 rc = tx_status_from_tlvp(rc, msg);
Harald Welteec0f8012020-12-06 16:32:01 +01001307 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
1308 return rc;
1309 }
Harald Weltee5209642020-12-05 19:59:45 +01001310 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
Philipp Maier74882dc2021-02-04 16:31:46 +01001311 msgb_bcid(msg) = (void *)tp;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001312
1313 switch (pdu_type) {
1314 case BSSGP_PDUT_BVC_RESET:
Harald Weltee5209642020-12-05 19:59:45 +01001315 /* resolve or create ggbproxy_bvc + handle in BVC-FSM */
Philipp Maier74882dc2021-02-04 16:31:46 +01001316 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
1317 rc = rx_bvc_reset_from_sgsn(nse, msg, &tp[0], ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001318 break;
1319 case BSSGP_PDUT_BVC_RESET_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001320 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001321 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1322 if (!sgsn_bvc)
1323 goto err_no_bvc;
1324 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1325 break;
1326 case BSSGP_PDUT_BVC_BLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001327 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001328 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1329 if (!sgsn_bvc)
1330 goto err_no_bvc;
1331 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg);
1332 break;
1333 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Philipp Maier74882dc2021-02-04 16:31:46 +01001334 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001335 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1336 if (!sgsn_bvc)
1337 goto err_no_bvc;
1338 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg);
Daniel Willmann8489e7a2020-11-03 21:12:42 +01001339 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001340 case BSSGP_PDUT_FLUSH_LL:
1341 /* simple case: BVCI IE is mandatory */
Philipp Maier74882dc2021-02-04 16:31:46 +01001342 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Harald Weltee5209642020-12-05 19:59:45 +01001343 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1344 if (!sgsn_bvc)
1345 goto err_no_bvc;
1346 if (sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1347 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001348 break;
Daniel Willmann5614e572021-01-18 18:38:27 +01001349 case BSSGP_PDUT_DUMMY_PAGING_PS:
1350 /* Routing area is optional in dummy paging and we have nothing else to go by
1351 * so in case it is missing we need to broadcast the paging */
1352 paging_bc = true;
1353 /* fall through */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001354 case BSSGP_PDUT_PAGING_PS:
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001355 {
1356 /* Cache the IMSI<->NSE to route PAGING REJECT */
1357 struct osmo_mobile_identity mi;
Philipp Maier74882dc2021-02-04 16:31:46 +01001358 const uint8_t *mi_data = TLVP_VAL(&tp[0], BSSGP_IE_IMSI);
1359 uint8_t mi_len = TLVP_LEN(&tp[0], BSSGP_IE_IMSI);
Daniel Willmann8613c9d2021-01-17 13:40:38 +01001360 osmo_mobile_identity_decode(&mi, mi_data, mi_len, false);
1361 gbproxy_imsi_cache_update(nse, mi.imsi);
1362 /* fall through */
1363 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001364 case BSSGP_PDUT_PAGING_CS:
1365 /* process the paging request (LAI/RAI lookup) */
Philipp Maier74882dc2021-02-04 16:31:46 +01001366 rc = gbprox_rx_paging(nse, msg, pdut_name, &tp[0], ns_bvci, paging_bc);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001367 break;
1368 case BSSGP_PDUT_STATUS:
1369 /* Some exception has occurred */
Philipp Maier74882dc2021-02-04 16:31:46 +01001370 cause = *TLVP_VAL(&tp[0], BSSGP_IE_CAUSE);
Harald Weltee5209642020-12-05 19:59:45 +01001371 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause,
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001372 bssgp_cause_str(cause));
Philipp Maier74882dc2021-02-04 16:31:46 +01001373 if (TLVP_PRES_LEN(&tp[0], BSSGP_IE_BVCI, 2)) {
1374 bvci = ntohs(tlvp_val16_unal(&tp[0], BSSGP_IE_BVCI));
Daniel Willmann3696dce2020-12-02 16:08:02 +01001375 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Harald Weltee5209642020-12-05 19:59:45 +01001376 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1377 /* don't send STATUS in response to STATUS if !bvc */
1378 if (sgsn_bvc && sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1379 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001380 } else
1381 LOGPC(DGPRS, LOGL_NOTICE, "\n");
1382 break;
1383 /* those only exist in the SGSN -> BSS direction */
1384 case BSSGP_PDUT_SUSPEND_ACK:
1385 case BSSGP_PDUT_SUSPEND_NACK:
1386 case BSSGP_PDUT_RESUME_ACK:
1387 case BSSGP_PDUT_RESUME_NACK:
Daniel Willmann77493b12020-12-29 21:13:31 +01001388 {
1389 struct gbproxy_nse *nse_peer;
Philipp Maier74882dc2021-02-04 16:31:46 +01001390 uint32_t tlli = osmo_load32be(TLVP_VAL(&tp[0], BSSGP_IE_TLLI));
Daniel Willmann77493b12020-12-29 21:13:31 +01001391
1392 nse_peer = gbproxy_nse_by_tlli(cfg, tlli);
1393 if (!nse_peer) {
1394 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find NSE\n", pdut_name);
1395 /* TODO: Counter */
1396 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
1397 }
1398 /* Delete the entry after we're done */
1399 gbproxy_tlli_cache_remove(cfg, tlli);
1400 LOGPNSE(nse_peer, LOGL_DEBUG, "Rx %s: forwarding\n", pdut_name);
1401 gbprox_relay2nse(msg, nse_peer, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001402 break;
Daniel Willmann77493b12020-12-29 21:13:31 +01001403 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001404 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte7479c4d2020-12-02 20:06:04 +01001405 case BSSGP_PDUT_OVERLOAD:
Harald Weltee5209642020-12-05 19:59:45 +01001406 LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name);
Harald Welte560bdb32020-12-04 22:24:47 +01001407 /* broadcast to all BSS-side bvcs */
Harald Welted2fef952020-12-05 00:31:07 +01001408 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte7479c4d2020-12-02 20:06:04 +01001409 gbprox_relay2nse(msg, nse, 0);
1410 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001411 break;
Harald Weltee5209642020-12-05 19:59:45 +01001412 case BSSGP_PDUT_RAN_INFO:
1413 case BSSGP_PDUT_RAN_INFO_REQ:
1414 case BSSGP_PDUT_RAN_INFO_ACK:
1415 case BSSGP_PDUT_RAN_INFO_ERROR:
1416 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier1c5766b2021-02-09 17:03:03 +01001417 rc = gbprox_rx_rim_from_sgsn(tp, nse, msg, log_pfx, pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001418 default:
Harald Weltee5209642020-12-05 19:59:45 +01001419 LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name);
1420 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001421 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001422 break;
1423 }
1424
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001425 return rc;
Harald Weltee5209642020-12-05 19:59:45 +01001426
Harald Welte560bdb32020-12-04 22:24:47 +01001427err_no_bvc:
Harald Weltee5209642020-12-05 19:59:45 +01001428 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC\n", pdut_name);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001429 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Harald Weltedbef0aa2020-12-07 17:48:11 +01001430 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001431}
1432
Harald Weltee5209642020-12-05 19:59:45 +01001433
1434/***********************************************************************
1435 * libosmogb NS/BSSGP integration
1436 ***********************************************************************/
1437
Alexander Couzens951e1332020-09-22 13:21:46 +02001438int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001439{
1440 int rc;
Alexander Couzens951e1332020-09-22 13:21:46 +02001441 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1442 struct gprs_ns2_inst *nsi = cfg->nsi;
1443 struct osmo_gprs_ns2_prim nsp = {};
1444
1445 nsp.bvci = msgb_bvci(msg);
1446 nsp.nsei = msgb_nsei(msg);
1447
Alexander Couzens55c36f92021-01-27 20:56:55 +01001448 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA, PRIM_OP_REQUEST, msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001449 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1450
1451 return rc;
1452}
1453
1454/* Main input function for Gb proxy */
1455int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1456{
Alexander Couzens951e1332020-09-22 13:21:46 +02001457 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Harald Weltee5209642020-12-05 19:59:45 +01001458 uint16_t ns_bvci = msgb_bvci(msg);
1459 uint16_t nsei = msgb_nsei(msg);
1460 struct gbproxy_nse *nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001461
Harald Welte278dd272020-12-06 13:35:24 +01001462 /* ensure minimum length to decode PCU type */
1463 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1464 return bssgp_tx_status(BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1465
Harald Weltee5209642020-12-05 19:59:45 +01001466 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN);
1467 if (nse) {
1468 if (ns_bvci == 0 || ns_bvci == 1)
1469 return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001470 else
Harald Weltee5209642020-12-05 19:59:45 +01001471 return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001472 }
1473
Harald Weltee5209642020-12-05 19:59:45 +01001474 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS);
1475 if (!nse) {
1476 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei);
1477 nse = gbproxy_nse_alloc(cfg, nsei, false);
1478 }
1479 if (nse) {
1480 if (ns_bvci == 0 || ns_bvci == 1)
1481 return gbprox_rx_sig_from_bss(nse, msg, ns_bvci);
1482 else
1483 return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci);
1484 }
1485
1486 return 0;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001487}
1488
Alexander Couzens951e1332020-09-22 13:21:46 +02001489/* TODO: What about handling:
Alexander Couzens55c36f92021-01-27 20:56:55 +01001490 * GPRS_NS2_AFF_CAUSE_VC_FAILURE,
1491 GPRS_NS2_AFF_CAUSE_VC_RECOVERY,
1492 GPRS_NS2_AFF_CAUSE_FAILURE,
1493 GPRS_NS2_AFF_CAUSE_RECOVERY,
Alexander Couzens951e1332020-09-22 13:21:46 +02001494 osmocom own causes
Alexander Couzens55c36f92021-01-27 20:56:55 +01001495 GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED,
1496 GPRS_NS2_AFF_CAUSE_SNS_FAILURE,
Alexander Couzens951e1332020-09-22 13:21:46 +02001497 */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001498
Alexander Couzens951e1332020-09-22 13:21:46 +02001499void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001500{
Harald Welte560bdb32020-12-04 22:24:47 +01001501 /* 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 +02001502 * TODO: sgsn nsei available/unavailable
1503 */
Harald Weltee5209642020-12-05 19:59:45 +01001504
Harald Welte560bdb32020-12-04 22:24:47 +01001505 struct gbproxy_bvc *bvc;
Harald Weltee5209642020-12-05 19:59:45 +01001506 struct gbproxy_nse *sgsn_nse;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001507
Alexander Couzens951e1332020-09-22 13:21:46 +02001508 switch (nsp->u.status.cause) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001509 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
1510 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens951e1332020-09-22 13:21:46 +02001511 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001512
Alexander Couzens55c36f92021-01-27 20:56:55 +01001513 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Harald Welte9b367d22021-01-18 13:55:51 +01001514 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Harald Weltee5209642020-12-05 19:59:45 +01001515 sgsn_nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN);
1516 if (sgsn_nse) {
1517 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
1518 bvc = gbproxy_bvc_by_bvci(sgsn_nse, 0);
1519 if (bvc)
Daniel Willmann3ea37932021-02-10 13:41:14 +01001520 osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001521 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001522 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001523 case GPRS_NS2_AFF_CAUSE_FAILURE:
Harald Weltee5209642020-12-05 19:59:45 +01001524#if 0
Harald Weltea0f70732020-12-05 17:50:23 +01001525 if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) {
Alexander Couzens951e1332020-09-22 13:21:46 +02001526 /* sgsn */
1527 /* TODO: BSVC: block all PtP towards bss */
1528 rate_ctr_inc(&cfg->ctrg->
1529 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1530 } else {
Daniel Willmanne50550e2020-11-26 18:19:21 +01001531 /* bss became unavailable
1532 * TODO: Block all BVC belonging to that NSE */
Harald Welte560bdb32020-12-04 22:24:47 +01001533 bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei);
1534 if (!bvc) {
Alexander Couzens951e1332020-09-22 13:21:46 +02001535 /* TODO: use primitive name + status cause name */
Harald Welte560bdb32020-12-04 22:24:47 +01001536 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n",
Alexander Couzens951e1332020-09-22 13:21:46 +02001537 nsp->u.status.cause, nsp->nsei);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001538 break;
1539 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001540
Harald Welte560bdb32020-12-04 22:24:47 +01001541 if (!bvc->blocked)
Alexander Couzens951e1332020-09-22 13:21:46 +02001542 break;
Harald Weltee5209642020-12-05 19:59:45 +01001543 hash_for_each(cfg->sgsn_nses, _sgsn, sgsn_nse, list) {
1544 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, sgsn_nse->nsei, bvc->bvci, 0);
1545 }
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001546 }
Harald Weltee5209642020-12-05 19:59:45 +01001547#endif
Harald Welte9b367d22021-01-18 13:55:51 +01001548 LOGP(DGPRS, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Alexander Couzens951e1332020-09-22 13:21:46 +02001549 break;
1550 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001551 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001552 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens951e1332020-09-22 13:21:46 +02001553 break;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001554 }
Alexander Couzens951e1332020-09-22 13:21:46 +02001555}
1556
Alexander Couzens951e1332020-09-22 13:21:46 +02001557/* called by the ns layer */
1558int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1559{
1560 struct osmo_gprs_ns2_prim *nsp;
1561 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001562 uintptr_t bvci;
Alexander Couzens951e1332020-09-22 13:21:46 +02001563 int rc = 0;
1564
1565 if (oph->sap != SAP_NS)
1566 return 0;
1567
1568 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1569
1570 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welte9b367d22021-01-18 13:55:51 +01001571 LOGP(DGPRS, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001572 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001573 return 0;
1574 }
1575
1576 switch (oph->primitive) {
Alexander Couzens55c36f92021-01-27 20:56:55 +01001577 case GPRS_NS2_PRIM_UNIT_DATA:
Daniel Willmann8f407b12020-12-02 19:33:50 +01001578
Alexander Couzens951e1332020-09-22 13:21:46 +02001579 /* hand the message into the BSSGP implementation */
1580 msgb_bssgph(oph->msg) = oph->msg->l3h;
1581 msgb_bvci(oph->msg) = nsp->bvci;
1582 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmann8f407b12020-12-02 19:33:50 +01001583 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens951e1332020-09-22 13:21:46 +02001584
Daniel Willmann8f407b12020-12-02 19:33:50 +01001585 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens951e1332020-09-22 13:21:46 +02001586 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmannb6550102020-11-04 17:32:56 +01001587 msgb_free(oph->msg);
Alexander Couzens951e1332020-09-22 13:21:46 +02001588 break;
Alexander Couzens55c36f92021-01-27 20:56:55 +01001589 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens951e1332020-09-22 13:21:46 +02001590 gprs_ns_prim_status_cb(cfg, nsp);
1591 break;
1592 default:
Harald Welte9b367d22021-01-18 13:55:51 +01001593 LOGP(DGPRS, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
Harald Welte95cf9fb2020-11-30 10:55:22 +01001594 gprs_ns2_prim_str(oph->primitive),
1595 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens951e1332020-09-22 13:21:46 +02001596 break;
1597 }
1598
1599 return rc;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001600}
1601
1602void gbprox_reset(struct gbproxy_config *cfg)
1603{
Harald Welted2fef952020-12-05 00:31:07 +01001604 struct gbproxy_nse *nse;
1605 struct hlist_node *ntmp;
Harald Welte8b4c7942020-12-05 10:14:49 +01001606 int i, j;
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001607
Harald Welted2fef952020-12-05 00:31:07 +01001608 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte8b4c7942020-12-05 10:14:49 +01001609 struct gbproxy_bvc *bvc;
1610 struct hlist_node *tmp;
1611 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte560bdb32020-12-04 22:24:47 +01001612 gbproxy_bvc_free(bvc);
Daniel Willmanne50550e2020-11-26 18:19:21 +01001613
1614 gbproxy_nse_free(nse);
1615 }
Harald Weltee5209642020-12-05 19:59:45 +01001616 /* FIXME: cells */
1617 /* FIXME: SGSN side BVCs (except signaling) */
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001618
1619 rate_ctr_group_free(cfg->ctrg);
1620 gbproxy_init_config(cfg);
1621}
1622
Daniel Willmann77493b12020-12-29 21:13:31 +01001623static void tlli_cache_cleanup(void *data)
1624{
1625 struct gbproxy_config *cfg = data;
1626 gbproxy_tlli_cache_cleanup(cfg);
1627
1628 /* TODO: Disable timer when cache is empty */
1629 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1630}
1631
Daniel Willmannc8a50092021-01-17 13:11:41 +01001632static void imsi_cache_cleanup(void *data)
1633{
1634 struct gbproxy_config *cfg = data;
1635 gbproxy_imsi_cache_cleanup(cfg);
1636
1637 /* TODO: Disable timer when cache is empty */
1638 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1639}
1640
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001641int gbproxy_init_config(struct gbproxy_config *cfg)
1642{
1643 struct timespec tp;
1644
Harald Welte209dc9f2020-12-12 19:02:16 +01001645 /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
1646 cfg->pool.bvc_fc_ratio = 100;
Daniel Willmannee834af2020-12-14 16:22:39 +01001647 cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);
Daniel Willmann77493b12020-12-29 21:13:31 +01001648 /* TODO: Make configurable */
Daniel Willmannbd12f3f2021-01-13 18:16:04 +01001649 cfg->tlli_cache.timeout = 10;
Daniel Willmannc8a50092021-01-17 13:11:41 +01001650 cfg->imsi_cache.timeout = 10;
Daniel Willmannee834af2020-12-14 16:22:39 +01001651
Harald Welted2fef952020-12-05 00:31:07 +01001652 hash_init(cfg->bss_nses);
Daniel Willmann1e7be5d2020-12-21 18:08:21 +01001653 hash_init(cfg->sgsn_nses);
1654 hash_init(cfg->cells);
Daniel Willmann77493b12020-12-29 21:13:31 +01001655 hash_init(cfg->tlli_cache.entries);
Daniel Willmannee834af2020-12-14 16:22:39 +01001656 INIT_LLIST_HEAD(&cfg->sgsns);
1657
Daniel Willmann77493b12020-12-29 21:13:31 +01001658 osmo_timer_setup(&cfg->tlli_cache.timer, tlli_cache_cleanup, cfg);
1659 osmo_timer_schedule(&cfg->tlli_cache.timer, 2, 0);
1660
Daniel Willmannc8a50092021-01-17 13:11:41 +01001661 /* We could also combine both timers */
1662 osmo_timer_setup(&cfg->imsi_cache.timer, imsi_cache_cleanup, cfg);
1663 osmo_timer_schedule(&cfg->imsi_cache.timer, 2, 0);
1664
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001665 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
1666 if (!cfg->ctrg) {
1667 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1668 return -1;
1669 }
1670 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Harald Weltec169de42020-12-07 13:12:13 +01001671 osmo_fsm_log_timeouts(true);
Pau Espin Pedrol1ddefb12019-08-30 19:48:34 +02001672
1673 return 0;
Oliver Smith29532c22021-01-29 11:13:00 +01001674}