blob: 4b6dc091f7745febc006cf58e7821061f44d1fb2 [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
Harald Welte5687ae62020-12-05 19:59:45 +01003/* (C) 2010-2020 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +02004 * (C) 2010-2013 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
Harald Welte9f75c352010-04-30 20:26:32 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * 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
Harald Welte9f75c352010-04-30 20:26:32 +020011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Harald Welte9f75c352010-04-30 20:26:32 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * 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/>.
Harald Welte9f75c352010-04-30 20:26:32 +020020 *
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>
Harald Welte7fc98222010-05-11 10:15:42 +020031#include <arpa/inet.h>
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020032#include <time.h>
Harald Welte9f75c352010-04-30 20:26:32 +020033
Harald Welte78db2442020-12-05 00:31:07 +010034#include <osmocom/core/hashtable.h>
Daniel Willmanne8c8ec92020-12-02 19:33:50 +010035#include <osmocom/core/logging.h>
Daniel Willmannef3c9af2020-12-14 16:22:39 +010036#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/talloc.h>
38#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020039#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010040#include <osmocom/core/stats.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020041
Alexander Couzens82182d02020-09-22 13:21:46 +020042#include <osmocom/gprs/gprs_ns2.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080043#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte9e917642020-12-12 19:02:16 +010044#include <osmocom/gprs/gprs_bssgp2.h>
Alexander Couzens82182d02020-09-22 13:21:46 +020045#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte5687ae62020-12-05 19:59:45 +010046#include <osmocom/gprs/bssgp_bvc_fsm.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080047
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020048#include <osmocom/gsm/gsm_utils.h>
49
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020050#include <osmocom/sgsn/signal.h>
51#include <osmocom/sgsn/debug.h>
52#include <osmocom/sgsn/gprs_gb_parse.h>
53#include <osmocom/sgsn/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020054
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020055#include <osmocom/sgsn/gprs_llc.h>
Harald Welte53373bc2016-04-20 17:11:43 +020056#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020057#include <osmocom/sgsn/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020058
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010059extern void *tall_sgsn_ctx;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020060
Jacob Erlbeckbc555742013-10-18 14:34:55 +020061static const struct rate_ctr_desc global_ctr_description[] = {
62 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020063 { "inv-lai", "Invalid Location Area Identifier" },
64 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020065 { "inv-nsei", "No BVC established for NSEI " },
Pau Espin Pedrol437e2092018-07-24 12:41:26 +020066 { "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)" },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020072 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020073 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020074};
75
76static const struct rate_ctr_group_desc global_ctrg_desc = {
Max8a01a802017-12-20 13:10:11 +010077 .group_name_prefix = "gbproxy:global",
Jacob Erlbeckbc555742013-10-18 14:34:55 +020078 .group_description = "GBProxy Global Statistics",
79 .num_ctr = ARRAY_SIZE(global_ctr_description),
80 .ctr_desc = global_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010081 .class_id = OSMO_STATS_CLASS_GLOBAL,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020082};
83
Harald Welte8cd74402020-12-04 22:24:47 +010084static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann8bca8de2020-11-03 21:11:45 +010085 uint16_t ns_bvci);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020086
Harald Welte4bf53ef2020-12-05 17:50:23 +010087
Harald Welte052d8552020-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
Harald Welte69619e32010-05-03 19:05:10 +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 Welte5687ae62020-12-05 19:59:45 +0100109#if 0
Harald Welte8cd74402020-12-04 22:24:47 +0100110/* feed a message down the NS-VC associated with the specified bvc */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200111static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200112 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200113{
Harald Welte39d0bb52010-05-12 18:10:25 +0000114 /* create a copy of the message so the old one can
115 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens82182d02020-09-22 13:21:46 +0200116 struct gprs_ns2_inst *nsi = cfg->nsi;
117 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +0200118 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200119 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000120
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100121 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200122 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200123
Alexander Couzens82182d02020-09-22 13:21:46 +0200124 nsp.bvci = ns_bvci;
125 nsp.nsei = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200126
Harald Welte69619e32010-05-03 19:05:10 +0200127 strip_ns_hdr(msg);
Alexander Couzens82182d02020-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);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200131 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200132 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200133 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200134}
Harald Welte5687ae62020-12-05 19:59:45 +0100135#endif
Harald Welte672f5c42010-05-03 18:54:58 +0200136
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100137/* feed a message down the NSE */
138static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann8bca8de2020-11-03 21:11:45 +0100139 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200140{
Daniel Willmann447ad442020-11-26 18:19:21 +0100141 OSMO_ASSERT(nse);
142 OSMO_ASSERT(nse->cfg);
143
Harald Welte39d0bb52010-05-12 18:10:25 +0000144 /* create a copy of the message so the old one can
145 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmann447ad442020-11-26 18:19:21 +0100146 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Alexander Couzens82182d02020-09-22 13:21:46 +0200147 struct osmo_gprs_ns2_prim nsp = {};
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100148 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Harald Welte2636e892020-11-18 12:01:46 +0100149 uint32_t tlli;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200150 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000151
Daniel Willmannea0b5d02020-12-21 10:40:27 +0100152 DEBUGP(DGPRS, "NSE(%05u/%s)-BVC(%05u/??) proxying to NSE(%05u/%s)\n", msgb_nsei(msg),
153 !nse->sgsn_facing ? "SGSN" : "BSS", ns_bvci, nse->nsei, nse->sgsn_facing ? "SGSN" : "BSS");
Harald Welte44c48302010-05-03 19:22:32 +0200154
Alexander Couzens82182d02020-09-22 13:21:46 +0200155 nsp.bvci = ns_bvci;
Daniel Willmann447ad442020-11-26 18:19:21 +0100156 nsp.nsei = nse->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200157
Harald Welte0ab535b2010-05-13 10:34:56 +0200158 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200159 strip_ns_hdr(msg);
160
Harald Welte2636e892020-11-18 12:01:46 +0100161 /* TS 48.018 Section 5.4.2: The link selector parameter is
162 * defined in 3GPP TS 48.016. At one side of the Gb interface,
163 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
164 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
165 * underlying network service. */
166 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
167 nsp.u.unitdata.link_selector = tlli;
168
Alexander Couzens82182d02020-09-22 13:21:46 +0200169 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
170 PRIM_OP_REQUEST, msg);
171 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100172 /* FIXME: We need a counter group for gbproxy_nse */
173 //if (rc < 0)
Harald Welte8cd74402020-12-04 22:24:47 +0100174 // rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100175
176 return rc;
177}
178
Harald Welte8cd74402020-12-04 22:24:47 +0100179/* feed a message down the NS-VC associated with the specified bvc */
180static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100181 uint16_t ns_bvci)
182{
183 int rc;
Harald Welte8cd74402020-12-04 22:24:47 +0100184 struct gbproxy_nse *nse = bvc->nse;
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100185 OSMO_ASSERT(nse);
186
187 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200188 if (rc < 0)
Harald Welte8cd74402020-12-04 22:24:47 +0100189 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200190
191 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200192}
193
Harald Welteb1fd9022012-06-17 12:16:31 +0800194int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
195{
196 return 0;
197}
198
Harald Welte5687ae62020-12-05 19:59:45 +0100199
200/***********************************************************************
201 * PTP BVC handling
202 ***********************************************************************/
203
204/* route an uplink message on a PTP-BVC to a SGSN using the TLLI */
205static int gbprox_bss2sgsn_tlli(struct gbproxy_cell *cell, struct msgb *msg, uint32_t tlli,
206 bool sig_bvci)
207{
208 struct gbproxy_bvc *sgsn_bvc;
209 unsigned int i;
210
211 /* FIXME: derive NRI from TLLI */
212 /* FIXME: find the SGSN for that NRI */
213
214 /* HACK: we currently simply pick the first SGSN we find */
215 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
216 sgsn_bvc = cell->sgsn_bvc[i];
217 if (sgsn_bvc)
218 return gbprox_relay2peer(msg, sgsn_bvc, sig_bvci ? 0 : sgsn_bvc->bvci);
219 }
220 return 0;
221}
222
223static int gbprox_bss2sgsn_null_nri(struct gbproxy_cell *cell, struct msgb *msg)
224{
225 struct gbproxy_bvc *sgsn_bvc;
226 unsigned int i;
227
228 /* FIXME: find the SGSN for that NRI */
229
230 /* HACK: we currently simply pick the first SGSN we find */
231 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
232 sgsn_bvc = cell->sgsn_bvc[i];
233 if (sgsn_bvc)
234 return gbprox_relay2peer(msg, sgsn_bvc, sgsn_bvc->bvci);
235 }
236 return 0;
237}
238
239
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200240/* Receive an incoming PTP message from a BSS-side NS-VC */
Harald Welte5687ae62020-12-05 19:59:45 +0100241static int gbprox_rx_ptp_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200242{
Harald Welted651edc2020-12-06 13:35:24 +0100243 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte5687ae62020-12-05 19:59:45 +0100244 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
245 struct gbproxy_bvc *bss_bvc;
246 struct tlv_parsed tp;
247 char log_pfx[32];
248 uint32_t tlli;
249 int rc;
250
251 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
252
253 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200254
Daniel Willmannc4c1db92020-12-10 17:59:46 +0100255 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Welte5687ae62020-12-05 19:59:45 +0100256 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not PTP\n", log_pfx, ns_bvci);
Harald Welted651edc2020-12-06 13:35:24 +0100257 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
258 }
259
260 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100261 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Harald Welted651edc2020-12-06 13:35:24 +0100262 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
263 }
264
265 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_UL)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100266 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Harald Welted651edc2020-12-06 13:35:24 +0100267 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
268 }
269
Harald Welte5687ae62020-12-05 19:59:45 +0100270 bss_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
271 if (!bss_bvc) {
272 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for PTP message, discarding\n",
273 log_pfx, pdut_name);
274 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200275 }
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200276
Harald Welte5687ae62020-12-05 19:59:45 +0100277 /* UL_UNITDATA has a different header than all other uplink PDUs */
278 if (bgph->pdu_type == BSSGP_PDUT_UL_UNITDATA) {
279 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
280 if (msgb_bssgp_len(msg) < sizeof(*budh))
281 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
282 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
283 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
284 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
285 * doesn't have to worry where the TLLI came from */
286 tp.lv[BSSGP_IE_TLLI].len = 4;
287 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
288 } else {
289 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
290 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
291 }
292 if (rc < 0) {
293 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
294 return tx_status_from_tlvp(rc, msg);
295 }
Harald Welte61ff2732020-12-08 21:43:22 +0100296 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
297 msgb_bcid(msg) = (void *)&tp;
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200298
Harald Welte5687ae62020-12-05 19:59:45 +0100299 switch (bgph->pdu_type) {
300 case BSSGP_PDUT_UL_UNITDATA:
301 case BSSGP_PDUT_RA_CAPA_UPDATE:
302 case BSSGP_PDUT_FLOW_CONTROL_MS:
303 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
304 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
305 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
306 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
307 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
308 case BSSGP_PDUT_FLOW_CONTROL_PFC:
309 case BSSGP_PDUT_DELETE_BSS_PFC_REQ:
310 case BSSGP_PDUT_PS_HO_REQUIRED:
311 case BSSGP_PDUT_PS_HO_REQUEST_ACK:
312 case BSSGP_PDUT_PS_HO_REQUEST_NACK:
313 case BSSGP_PDUT_PS_HO_COMPLETE:
314 case BSSGP_PDUT_PS_HO_CANCEL:
315 /* We can route based on TLLI-NRI */
316 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
317 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, tlli, false);
318 break;
319 case BSSGP_PDUT_RADIO_STATUS:
320 if (TLVP_PRESENT(&tp, BSSGP_IE_TLLI)) {
321 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
322 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, tlli, false);
323 } else if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI)) {
324 /* we treat the TMSI like a TLLI and extract the NRI from it */
325 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
326 rc = gbprox_bss2sgsn_tlli(bss_bvc->cell, msg, tlli, false);
327 } else if (TLVP_PRESENT(&tp, BSSGP_IE_IMSI)) {
328 rc = gbprox_bss2sgsn_null_nri(bss_bvc->cell, msg);
329 } else
330 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx RADIO-STATUS without any of the conditional IEs\n");
331 break;
332 case BSSGP_PDUT_DUMMY_PAGING_PS_RESP:
333 case BSSGP_PDUT_PAGING_PS_REJECT:
334 /* TODO: Implement via state tracking of PAGING-PS + DUMMY_PAGING_PS */
335 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
336 break;
337 case BSSGP_PDUT_FLOW_CONTROL_BVC:
Harald Welte61ff2732020-12-08 21:43:22 +0100338 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC, msg);
Harald Welte5687ae62020-12-05 19:59:45 +0100339 break;
340 case BSSGP_PDUT_STATUS:
341 /* TODO: Implement by inspecting the contained PDU */
342 if (!TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR))
343 break;
344 LOGPBVC(bss_bvc, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
345 break;
346 }
347
348 return 0;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200349}
350
351/* Receive an incoming PTP message from a SGSN-side NS-VC */
Harald Welte5687ae62020-12-05 19:59:45 +0100352static int gbprox_rx_ptp_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200353{
Harald Welted651edc2020-12-06 13:35:24 +0100354 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte5687ae62020-12-05 19:59:45 +0100355 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
356 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte61ff2732020-12-08 21:43:22 +0100357 struct tlv_parsed tp;
Harald Welte5687ae62020-12-05 19:59:45 +0100358 char log_pfx[32];
Harald Welte61ff2732020-12-08 21:43:22 +0100359 int rc;
Harald Welte5687ae62020-12-05 19:59:45 +0100360
361 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
362
363 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200364
Daniel Willmannc4c1db92020-12-10 17:59:46 +0100365 if (ns_bvci == 0 || ns_bvci == 1) {
Harald Welte5687ae62020-12-05 19:59:45 +0100366 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI is not PTP\n", log_pfx);
Harald Welted651edc2020-12-06 13:35:24 +0100367 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
368 }
369
370 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_PTP)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100371 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in PTP BVC\n", log_pfx, pdut_name);
Harald Welted651edc2020-12-06 13:35:24 +0100372 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
373 }
374
375 if (!(bssgp_pdu_type_flags(bgph->pdu_type) & BSSGP_PDUF_DL)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100376 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Harald Welted651edc2020-12-06 13:35:24 +0100377 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
378 }
379
Harald Welte5687ae62020-12-05 19:59:45 +0100380 sgsn_bvc = gbproxy_bvc_by_bvci(nse, ns_bvci);
381 if (!sgsn_bvc) {
382 LOGP(DGPRS, LOGL_NOTICE, "%s %s - Didn't find BVC for for PTP message, discarding\n",
383 log_pfx, pdut_name);
384 rate_ctr_inc(&nse->cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_BVCI]);
385 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200386 }
387
Harald Welte5687ae62020-12-05 19:59:45 +0100388 if (!bssgp_bvc_fsm_is_unblocked(sgsn_bvc->fi)) {
389 LOGPBVC(sgsn_bvc, LOGL_NOTICE, "Rx %s: Dropping on blocked BVC\n", pdut_name);
390 rate_ctr_inc(&sgsn_bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200391 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200392 }
Harald Welte61ff2732020-12-08 21:43:22 +0100393
394 /* DL_UNITDATA has a different header than all other uplink PDUs */
395 if (bgph->pdu_type == BSSGP_PDUT_DL_UNITDATA) {
396 const struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
397 if (msgb_bssgp_len(msg) < sizeof(*budh))
398 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
399 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, budh->data,
400 msgb_bssgp_len(msg) - sizeof(*budh), 0, 0, DGPRS, log_pfx);
401 /* populate TLLI from the fixed headser into the TLV-parsed array so later code
402 * doesn't have to worry where the TLLI came from */
403 tp.lv[BSSGP_IE_TLLI].len = 4;
404 tp.lv[BSSGP_IE_TLLI].val = (const uint8_t *) &budh->tlli;
405 } else {
406 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, bgph->pdu_type, bgph->data,
407 msgb_bssgp_len(msg) - sizeof(*bgph), 0, 0, DGPRS, log_pfx);
408 }
409 if (rc < 0) {
410 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
411 return tx_status_from_tlvp(rc, msg);
412 }
413 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
414 msgb_bcid(msg) = (void *)&tp;
415
Harald Welte5687ae62020-12-05 19:59:45 +0100416 OSMO_ASSERT(sgsn_bvc->cell);
417 bss_bvc = sgsn_bvc->cell->bss_bvc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200418
Harald Welte61ff2732020-12-08 21:43:22 +0100419 switch (bgph->pdu_type) {
420 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
421 return osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_FC_BVC_ACK, msg);
422 default:
423 return gbprox_relay2peer(msg, bss_bvc, bss_bvc->bvci);
424 }
425
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200426}
427
Harald Welte5687ae62020-12-05 19:59:45 +0100428/***********************************************************************
429 * BVC FSM call-backs
430 ***********************************************************************/
Harald Welteac44d6b2020-12-02 22:53:26 +0100431
Harald Welte5687ae62020-12-05 19:59:45 +0100432/* helper function to dispatch a FSM event to all SGSN-side BVC FSMs of a cell */
433static void dispatch_to_all_sgsn_bvc(struct gbproxy_cell *cell, uint32_t event, void *priv)
434{
435 unsigned int i;
436
437 for (i = 0; i < ARRAY_SIZE(cell->sgsn_bvc); i++) {
438 struct gbproxy_bvc *sgsn_bvc = cell->sgsn_bvc[i];
439 if (!sgsn_bvc)
440 continue;
441 osmo_fsm_inst_dispatch(sgsn_bvc->fi, event, priv);
442 }
443}
444
445/* BVC FSM informs us about a BSS-side reset of the signaling BVC */
446static void bss_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
447 uint16_t cell_id, uint8_t cause, void *priv)
448{
449 struct gbproxy_bvc *sig_bvc = priv;
450 struct gbproxy_nse *nse = sig_bvc->nse;
451 struct gbproxy_bvc *ptp_bvc;
452 unsigned int i;
453
454 /* BLOCK all SGSN-side PTP BVC within this NSE */
455 hash_for_each(nse->bvcs, i, ptp_bvc, list) {
456 if (ptp_bvc == sig_bvc)
457 continue;
458 OSMO_ASSERT(ptp_bvc->cell);
459
460 dispatch_to_all_sgsn_bvc(ptp_bvc->cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
Harald Weltefb7f8c52020-12-02 23:29:38 +0100461 }
Harald Welteac44d6b2020-12-02 22:53:26 +0100462
Harald Welte5687ae62020-12-05 19:59:45 +0100463 /* Delete all BSS-side PTP BVC within this NSE */
464 gbproxy_cleanup_bvcs(nse, 0);
465
466 /* TODO: we keep the "CELL" around for now, re-connecting it to
467 * any (later) new PTP-BVC for that BVCI. Not sure if that's the
468 * best idea ? */
469}
470
471/* forward declaration */
472static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops;
473
474static const struct bssgp_bvc_fsm_ops bss_sig_bvc_fsm_ops = {
475 .reset_notification = bss_sig_bvc_reset_notif,
476};
477
478/* BVC FSM informs us about a BSS-side reset of a PTP BVC */
479static void bss_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
480 uint16_t cell_id, uint8_t cause, void *priv)
481{
482 struct gbproxy_bvc *bvc = priv;
483 struct gbproxy_config *cfg = bvc->nse->cfg;
Harald Weltecab85882020-12-12 15:01:17 +0100484 struct gbproxy_nse *sgsn_nse;
Harald Welte5687ae62020-12-05 19:59:45 +0100485 unsigned int i;
486
487 OSMO_ASSERT(bvci != 0);
488
489 if (!bvc->cell) {
490 /* see if we have a CELL dangling around */
491 bvc->cell = gbproxy_cell_by_bvci(cfg, bvci);
492 if (bvc->cell) {
493 /* the CELL already exists. This means either it * was created before at an
494 * earlier PTP BVC-RESET, or that there are non-unique BVCIs and hence a
495 * malconfiguration */
496 if (bvc->cell->bss_bvc) {
497 LOGPBVC(bvc, LOGL_NOTICE, "Rx BVC-RESET via this NSE, but CELL already "
498 "has BVC on NSEI=%05u\n", bvc->cell->bss_bvc->nse->nsei);
499 LOGPBVC(bvc->cell->bss_bvc, LOGL_NOTICE, "Destroying due to conflicting "
500 "BVCI configuration (new NSEI=%05u)!\n", bvc->nse->nsei);
501 gbproxy_bvc_free(bvc->cell->bss_bvc);
502 }
503 bvc->cell->bss_bvc = bvc;
504 }
505 }
506
507 if (!bvc->cell) {
Harald Welte5687ae62020-12-05 19:59:45 +0100508 /* if we end up here, it means this is the first time we received a BVC-RESET
509 * for this BVC. We need to create the 'cell' data structure and the SGSN-side
510 * BVC counterparts */
511
512 bvc->cell = gbproxy_cell_alloc(cfg, bvci);
513 OSMO_ASSERT(bvc->cell);
Harald Welte4b4c9972020-12-12 14:22:32 +0100514 memcpy(bvc->cell->ra, bvc->ra, sizeof(bvc->cell->ra));
Harald Welte5687ae62020-12-05 19:59:45 +0100515
516 /* link us to the cell and vice-versa */
517 bvc->cell->bss_bvc = bvc;
Harald Weltecab85882020-12-12 15:01:17 +0100518 }
Harald Welte5687ae62020-12-05 19:59:45 +0100519
Harald Weltecab85882020-12-12 15:01:17 +0100520 /* allocate (any missing) SGSN-side BVCs within the cell, and reset them */
521 hash_for_each(cfg->sgsn_nses, i, sgsn_nse, list) {
522 struct gbproxy_bvc *sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
523 if (sgsn_bvc)
524 OSMO_ASSERT(sgsn_bvc->cell == bvc->cell || !sgsn_bvc->cell);
Harald Welte5687ae62020-12-05 19:59:45 +0100525
Harald Weltecab85882020-12-12 15:01:17 +0100526 if (!sgsn_bvc) {
527 sgsn_bvc = gbproxy_bvc_alloc(sgsn_nse, bvci);
528 OSMO_ASSERT(sgsn_bvc);
Harald Welte5687ae62020-12-05 19:59:45 +0100529
Harald Weltecab85882020-12-12 15:01:17 +0100530 sgsn_bvc->cell = bvc->cell;
531 memcpy(sgsn_bvc->ra, bvc->cell->ra, sizeof(sgsn_bvc->ra));
532 sgsn_bvc->fi = bssgp_bvc_fsm_alloc_ptp_bss(sgsn_bvc, cfg->nsi, sgsn_nse->nsei,
533 bvci, ra_id, cell_id);
534 OSMO_ASSERT(sgsn_bvc->fi);
535 bssgp_bvc_fsm_set_ops(sgsn_bvc->fi, &sgsn_ptp_bvc_fsm_ops, sgsn_bvc);
Harald Welte5687ae62020-12-05 19:59:45 +0100536
Harald Weltecab85882020-12-12 15:01:17 +0100537 gbproxy_cell_add_sgsn_bvc(bvc->cell, sgsn_bvc);
Harald Welte5687ae62020-12-05 19:59:45 +0100538 }
539 }
540
541 /* Trigger outbound BVC-RESET procedure toward each SGSN */
542 dispatch_to_all_sgsn_bvc(bvc->cell, BSSGP_BVCFSM_E_REQ_RESET, &cause);
543}
544
545/* BVC FSM informs us about a BSS-side FSM state change */
546static void bss_ptp_bvc_state_chg_notif(uint16_t nsei, uint16_t bvci, int old_state, int state, void *priv)
547{
548 struct gbproxy_bvc *bvc = priv;
549 struct gbproxy_cell *cell = bvc->cell;
550 uint8_t cause = bssgp_bvc_fsm_get_block_cause(bvc->fi);
551
552 /* we have just been created but due to callback ordering the cell is not associated */
553 if (!cell)
554 return;
555
556 switch (state) {
557 case BSSGP_BVCFSM_S_BLOCKED:
558 /* block the corresponding SGSN-side PTP BVCs */
559 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_BLOCK, &cause);
560 break;
561 case BSSGP_BVCFSM_S_UNBLOCKED:
562 /* unblock the corresponding SGSN-side PTP BVCs */
563 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_UNBLOCK, NULL);
564 break;
565 }
566}
567
Harald Welte61ff2732020-12-08 21:43:22 +0100568/* BVC FSM informs us about BVC-FC PDU receive */
569static void bss_ptp_bvc_fc_bvc(uint16_t nsei, uint16_t bvci, const struct bssgp2_flow_ctrl *fc, void *priv)
570{
Harald Welte9e917642020-12-12 19:02:16 +0100571 struct bssgp2_flow_ctrl fc_reduced;
Harald Welte61ff2732020-12-08 21:43:22 +0100572 struct gbproxy_bvc *bss_bvc = priv;
Harald Welte9e917642020-12-12 19:02:16 +0100573 struct gbproxy_cell *cell;
574 struct gbproxy_config *cfg;
Harald Welte61ff2732020-12-08 21:43:22 +0100575
Harald Welte9e917642020-12-12 19:02:16 +0100576 OSMO_ASSERT(bss_bvc);
577 OSMO_ASSERT(fc);
578
579 cell = bss_bvc->cell;
Harald Welte61ff2732020-12-08 21:43:22 +0100580 if (!cell)
581 return;
582
Harald Welte9e917642020-12-12 19:02:16 +0100583 cfg = cell->cfg;
Harald Welte61ff2732020-12-08 21:43:22 +0100584
Harald Welte9e917642020-12-12 19:02:16 +0100585 /* reduce / scale according to configuration to make sure we only advertise a fraction
586 * of the capacity to each of the SGSNs in the pool */
587 fc_reduced = *fc;
588 fc_reduced.bucket_size_max = (fc->bucket_size_max * cfg->pool.bvc_fc_ratio) / 100;
589 fc_reduced.bucket_leak_rate = (fc->bucket_leak_rate * cfg->pool.bvc_fc_ratio) / 100;
590 /* we don't modify the per-MS related values as any single MS is only served by one SGSN */
591
592 dispatch_to_all_sgsn_bvc(cell, BSSGP_BVCFSM_E_REQ_FC_BVC, (void *) &fc_reduced);
Harald Welte61ff2732020-12-08 21:43:22 +0100593}
594
Harald Welte5687ae62020-12-05 19:59:45 +0100595static const struct bssgp_bvc_fsm_ops bss_ptp_bvc_fsm_ops = {
596 .reset_notification = bss_ptp_bvc_reset_notif,
597 .state_chg_notification = bss_ptp_bvc_state_chg_notif,
Harald Welte61ff2732020-12-08 21:43:22 +0100598 .rx_fc_bvc = bss_ptp_bvc_fc_bvc,
Harald Welte5687ae62020-12-05 19:59:45 +0100599};
600
601/* BVC FSM informs us about a SGSN-side reset of a PTP BVC */
602static void sgsn_ptp_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
603 uint16_t cell_id, uint8_t cause, void *priv)
604{
605 struct gbproxy_bvc *bvc = priv;
606
607 if (!bvc->cell) {
608 LOGPBVC(bvc, LOGL_ERROR, "RESET of PTP BVC on SGSN side for which we have no BSS?\n");
609 return;
610 }
611
612 OSMO_ASSERT(bvc->cell->bss_bvc);
613
614 /* request reset of BSS-facing PTP-BVC */
615 osmo_fsm_inst_dispatch(bvc->cell->bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
616}
617
618static const struct bssgp_bvc_fsm_ops sgsn_ptp_bvc_fsm_ops = {
619 .reset_notification = sgsn_ptp_bvc_reset_notif,
620};
621
622/* BVC FSM informs us about a SGSN-side reset of the signaling BVC */
623static void sgsn_sig_bvc_reset_notif(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id,
624 uint16_t cell_id, uint8_t cause, void *priv)
625{
626 struct gbproxy_bvc *bvc = priv;
627 struct gbproxy_config *cfg = bvc->nse->cfg;
628 struct gbproxy_nse *bss_nse;
629 unsigned int i;
630
631 /* delete all SGSN-side PTP BVC for this SGSN */
632 gbproxy_cleanup_bvcs(bvc->nse, 0);
633 /* FIXME: what to do about the cells? */
634 /* FIXME: do we really want to RESET all signaling BVC on the BSS and affect all other SGSN? */
635
636 /* we need to trigger generating a reset procedure towards each BSS side signaling BVC */
637 hash_for_each(cfg->bss_nses, i, bss_nse, list) {
638 struct gbproxy_bvc *bss_bvc = gbproxy_bvc_by_bvci(bss_nse, 0);
639 if (!bss_bvc) {
640 LOGPNSE(bss_nse, LOGL_ERROR, "Doesn't have BVC with BVCI=0 ?!?\n");
641 continue;
642 }
643 osmo_fsm_inst_dispatch(bss_bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
644 }
645}
646
647const struct bssgp_bvc_fsm_ops sgsn_sig_bvc_fsm_ops = {
648 .reset_notification = sgsn_sig_bvc_reset_notif,
649};
650
651/***********************************************************************
652 * Signaling BVC handling
653 ***********************************************************************/
654
655/* process a BVC-RESET message from the BSS side */
656static int rx_bvc_reset_from_bss(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp)
657{
658 struct gbproxy_bvc *from_bvc = NULL;
659 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
660 uint32_t features = 0; // FIXME: make configurable
661
662 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", bvci);
663
Harald Welte1239cf42020-12-02 23:03:22 +0100664 if (bvci == 0) {
665 /* If we receive a BVC reset on the signalling endpoint, we
666 * don't want the SGSN to reset, as the signalling endpoint
667 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte55253712020-12-02 23:06:37 +0100668
Harald Welte5687ae62020-12-05 19:59:45 +0100669 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
Harald Welte8cd74402020-12-04 22:24:47 +0100670 if (!from_bvc) {
Harald Welte5687ae62020-12-05 19:59:45 +0100671 from_bvc = gbproxy_bvc_alloc(nse, 0);
672 OSMO_ASSERT(from_bvc);
673 from_bvc->fi = bssgp_bvc_fsm_alloc_sig_sgsn(from_bvc, nse->cfg->nsi, nse->nsei, features);
674 if (!from_bvc->fi) {
675 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
676 gbproxy_bvc_free(from_bvc);
677 return -ENOMEM;
Harald Welteac44d6b2020-12-02 22:53:26 +0100678 }
Harald Welte5687ae62020-12-05 19:59:45 +0100679 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_sig_bvc_fsm_ops, from_bvc);
680 }
681 } else {
682 from_bvc = gbproxy_bvc_by_bvci(nse, bvci);
683 if (!from_bvc) {
Harald Welteac44d6b2020-12-02 22:53:26 +0100684 /* if a PTP-BVC is reset, and we don't know that
Harald Welte8cd74402020-12-04 22:24:47 +0100685 * PTP-BVCI yet, we should allocate a new bvc */
686 from_bvc = gbproxy_bvc_alloc(nse, bvci);
687 OSMO_ASSERT(from_bvc);
Harald Welte5687ae62020-12-05 19:59:45 +0100688 from_bvc->fi = bssgp_bvc_fsm_alloc_ptp_sgsn(from_bvc, nse->cfg->nsi,
689 nse->nsei, bvci);
690 if (!from_bvc->fi) {
691 LOGPNSE(nse, LOGL_ERROR, "Cannot allocate SIG-BVC FSM\n");
692 gbproxy_bvc_free(from_bvc);
693 return -ENOMEM;
694 }
695 bssgp_bvc_fsm_set_ops(from_bvc->fi, &bss_ptp_bvc_fsm_ops, from_bvc);
Harald Welteac44d6b2020-12-02 22:53:26 +0100696 }
Harald Welte5687ae62020-12-05 19:59:45 +0100697#if 0
Harald Welteac44d6b2020-12-02 22:53:26 +0100698 /* Could have moved to a different NSE */
Harald Welte8cd74402020-12-04 22:24:47 +0100699 if (!check_bvc_nsei(from_bvc, nsei)) {
700 LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei);
Harald Welteac44d6b2020-12-02 22:53:26 +0100701
Harald Welte5687ae62020-12-05 19:59:45 +0100702 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei, false);
Harald Welteac44d6b2020-12-02 22:53:26 +0100703 if (!nse_new) {
704 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
705 "BVCI=%05u\n", bvci, nsei);
706 bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
707 return 0;
708 }
709
Harald Welte8cd74402020-12-04 22:24:47 +0100710 /* Move bvc to different NSE */
711 gbproxy_bvc_move(from_bvc, nse_new);
Harald Welteac44d6b2020-12-02 22:53:26 +0100712 }
Harald Welte5687ae62020-12-05 19:59:45 +0100713#endif
714 /* FIXME: do we need this, if it happens within FSM? */
Harald Welte6c4c6f02020-12-03 15:36:59 +0100715 if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welteac44d6b2020-12-02 22:53:26 +0100716 struct gprs_ra_id raid;
717 /* We have a Cell Identifier present in this
718 * PDU, this means we can extend our local
719 * state information about this particular cell
720 * */
Harald Welte8cd74402020-12-04 22:24:47 +0100721 memcpy(from_bvc->ra, TLVP_VAL(tp, BSSGP_IE_CELL_ID), sizeof(from_bvc->ra));
722 gsm48_parse_ra(&raid, from_bvc->ra);
723 LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welteac44d6b2020-12-02 22:53:26 +0100724 }
Harald Welteac44d6b2020-12-02 22:53:26 +0100725 }
Harald Welte5687ae62020-12-05 19:59:45 +0100726 /* hand into FSM for further processing */
727 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
728 return 0;
Harald Welteac44d6b2020-12-02 22:53:26 +0100729}
730
Harald Welte9f75c352010-04-30 20:26:32 +0200731/* Receive an incoming signalling message from a BSS-side NS-VC */
Harald Welte5687ae62020-12-05 19:59:45 +0100732static int gbprox_rx_sig_from_bss(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200733{
Harald Welteca3620a2010-05-03 16:30:59 +0200734 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200735 uint8_t pdu_type = bgph->pdu_type;
Harald Welte5687ae62020-12-05 19:59:45 +0100736 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
737 struct tlv_parsed tp;
Harald Welteca3620a2010-05-03 16:30:59 +0200738 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte8cd74402020-12-04 22:24:47 +0100739 struct gbproxy_bvc *from_bvc = NULL;
Harald Welte052d8552020-12-06 16:32:01 +0100740 char log_pfx[32];
Harald Welte5687ae62020-12-05 19:59:45 +0100741 uint16_t ptp_bvci;
742 uint32_t tlli;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200743 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200744
Harald Welte5687ae62020-12-05 19:59:45 +0100745 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/BSS)-BVC(%05u/??)", nse->nsei, ns_bvci);
746
747 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welte052d8552020-12-06 16:32:01 +0100748
Harald Weltec471d3d2011-02-06 17:13:12 +0100749 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte5687ae62020-12-05 19:59:45 +0100750 LOGP(DGPRS, LOGL_NOTICE, "%s %s BVCI=%05u is not signalling\n", log_pfx, pdut_name, ns_bvci);
Harald Welted651edc2020-12-06 13:35:24 +0100751 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200752 }
753
Harald Welted651edc2020-12-06 13:35:24 +0100754 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100755 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Harald Welted651edc2020-12-06 13:35:24 +0100756 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
757 }
758
759 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_UL)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100760 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in uplink direction\n", log_pfx, pdut_name);
Harald Welted651edc2020-12-06 13:35:24 +0100761 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200762 }
763
Harald Welte052d8552020-12-06 16:32:01 +0100764 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, pdu_type, bgph->data, data_len, 0, 0,
765 DGPRS, log_pfx);
766 if (rc < 0) {
Harald Welte5687ae62020-12-05 19:59:45 +0100767 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte052d8552020-12-06 16:32:01 +0100768 return tx_status_from_tlvp(rc, msg);
769 }
Harald Welte5687ae62020-12-05 19:59:45 +0100770 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
771 msgb_bcid(msg) = (void *)&tp;
Harald Welte9f75c352010-04-30 20:26:32 +0200772
Harald Welte5687ae62020-12-05 19:59:45 +0100773 /* special case handling for some PDU types */
Harald Welte9f75c352010-04-30 20:26:32 +0200774 switch (pdu_type) {
Harald Welte5687ae62020-12-05 19:59:45 +0100775 case BSSGP_PDUT_BVC_RESET:
776 /* resolve or create gbproxy_bvc + handlei n BVC-FSM */
777 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
778 return rx_bvc_reset_from_bss(nse, msg, &tp);
779 case BSSGP_PDUT_BVC_RESET_ACK:
780 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
781 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
Harald Welte8cd74402020-12-04 22:24:47 +0100782 if (!from_bvc)
783 goto err_no_bvc;
Harald Welte5687ae62020-12-05 19:59:45 +0100784 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
785 case BSSGP_PDUT_BVC_BLOCK:
786 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
787 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
788 if (!from_bvc)
789 goto err_no_bvc;
790 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK, msg);
791 case BSSGP_PDUT_BVC_UNBLOCK:
792 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
793 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
794 if (!from_bvc)
795 goto err_no_bvc;
796 return osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK, msg);
797 case BSSGP_PDUT_SUSPEND:
798 case BSSGP_PDUT_RESUME:
799 /* FIXME: Implement TLLI Cache. Every SUSPEND/RESUME we must
800 * take record of the TLLI->BVC mapping so we can map
801 * back from TLLI->BVC when the SUSPEND/RESUME-ACK
802 * arrives. Cache should have a timeout of 1-3 seconds
803 * and the ACK should explicitly delete entries. */
804#if 0
805 /* TODO: Validate the RAI for consistency with the RAI
806 * we expect for any of the BVC within this BSS side NSE */
807 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA), sizeof(from_bvc->ra));
Harald Welte8cd74402020-12-04 22:24:47 +0100808 gsm48_parse_ra(&raid, from_bvc->ra);
Harald Welte5687ae62020-12-05 19:59:45 +0100809#endif
Harald Welte9f75c352010-04-30 20:26:32 +0200810 break;
Harald Welte5687ae62020-12-05 19:59:45 +0100811 case BSSGP_PDUT_STATUS:
812 /* FIXME: inspect the erroneous PDU IE (if any) and check
813 * if we can extract a TLLI/RNI to route it to the correct SGSN */
814 break;
815 case BSSGP_PDUT_RAN_INFO:
816 case BSSGP_PDUT_RAN_INFO_REQ:
817 case BSSGP_PDUT_RAN_INFO_ACK:
818 case BSSGP_PDUT_RAN_INFO_ERROR:
819 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
820 /* FIXME: route based in RIM Routing IE */
821 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
822 break;
823 case BSSGP_PDUT_LLC_DISCARD:
824 case BSSGP_PDUT_FLUSH_LL_ACK:
825 /* route based on BVCI + TLLI */
826 ptp_bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
827 tlli = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TLLI));
828 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
829 if (!from_bvc)
830 goto err_no_bvc;
831 gbprox_bss2sgsn_tlli(from_bvc->cell, msg, tlli, true);
832 break;
833 default:
834 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Implementation missing\n", pdut_name);
Harald Welte44c48302010-05-03 19:22:32 +0200835 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200836 }
837
Harald Welte5687ae62020-12-05 19:59:45 +0100838 return rc;
Harald Welte8cd74402020-12-04 22:24:47 +0100839err_no_bvc:
Harald Welte5687ae62020-12-05 19:59:45 +0100840 LOGPNSE(nse, LOGL_ERROR, "Rx %s: cannot find BVC for BVCI=%05u\n", pdut_name, ptp_bvci);
841 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200842 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200843}
844
845/* Receive paging request from SGSN, we need to relay to proper BSS */
Harald Welte1aa0ae92020-12-12 15:58:28 +0100846static int gbprox_rx_paging(struct gbproxy_nse *sgsn_nse, struct msgb *msg, const char *pdut_name,
Harald Welte5687ae62020-12-05 19:59:45 +0100847 struct tlv_parsed *tp, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200848{
Harald Welte1aa0ae92020-12-12 15:58:28 +0100849 struct gbproxy_config *cfg = sgsn_nse->cfg;
Harald Welte5687ae62020-12-05 19:59:45 +0100850 struct gbproxy_bvc *sgsn_bvc, *bss_bvc;
Harald Welte1aa0ae92020-12-12 15:58:28 +0100851 struct gbproxy_nse *nse;
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100852 unsigned int n_nses = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200853 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte993d3f42020-12-05 10:14:49 +0100854 int i, j;
Harald Welte9f75c352010-04-30 20:26:32 +0200855
Daniel Willmann447ad442020-11-26 18:19:21 +0100856 /* FIXME: Handle paging logic to only page each matching NSE */
857
Harald Welte6c4c6f02020-12-03 15:36:59 +0100858 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200859 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200860 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte1aa0ae92020-12-12 15:58:28 +0100861 sgsn_bvc = gbproxy_bvc_by_bvci(sgsn_nse, bvci);
Harald Welte5687ae62020-12-05 19:59:45 +0100862 if (!sgsn_bvc) {
Harald Welte1aa0ae92020-12-12 15:58:28 +0100863 LOGPNSE(sgsn_nse, LOGL_NOTICE, "Rx %s: unable to route: BVCI=%05u unknown\n",
Harald Welte5687ae62020-12-05 19:59:45 +0100864 pdut_name, bvci);
Harald Welte3375fa42020-11-23 15:14:20 +0100865 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
866 return -EINVAL;
867 }
Harald Welte5687ae62020-12-05 19:59:45 +0100868 LOGPBVC(sgsn_bvc, LOGL_INFO, "Rx %s: routing by BVCI\n", pdut_name);
869 return gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte6c4c6f02020-12-03 15:36:59 +0100870 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200871 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte8cd74402020-12-04 22:24:47 +0100872 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welte78db2442020-12-05 00:31:07 +0100873 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte5687ae62020-12-05 19:59:45 +0100874 hash_for_each(nse->bvcs, j, bss_bvc, list) {
875 if (!memcmp(bss_bvc->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
876 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (RAI match)\n",
877 pdut_name);
878 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100879 n_nses++;
880 /* Only send it once to each NSE */
881 break;
Daniel Willmann447ad442020-11-26 18:19:21 +0100882 }
Harald Welte3375fa42020-11-23 15:14:20 +0100883 }
884 }
Harald Welte6c4c6f02020-12-03 15:36:59 +0100885 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200886 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte8cd74402020-12-04 22:24:47 +0100887 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welte78db2442020-12-05 00:31:07 +0100888 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte5687ae62020-12-05 19:59:45 +0100889 hash_for_each(nse->bvcs, j, bss_bvc, list) {
890 if (!memcmp(bss_bvc->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
891 LOGPNSE(nse, LOGL_INFO, "Rx %s: routing to NSE (LAI match)\n",
892 pdut_name);
893 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100894 n_nses++;
895 /* Only send it once to each NSE */
896 break;
Daniel Willmann447ad442020-11-26 18:19:21 +0100897 }
Harald Welte3375fa42020-11-23 15:14:20 +0100898 }
899 }
Harald Welte6c4c6f02020-12-03 15:36:59 +0100900 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1)) {
Harald Welte8cd74402020-12-04 22:24:47 +0100901 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welte78db2442020-12-05 00:31:07 +0100902 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte5687ae62020-12-05 19:59:45 +0100903 hash_for_each(nse->bvcs, j, bss_bvc, list) {
904 LOGPNSE(nse, LOGL_INFO, "Rx %s:routing to NSE (broadcast)\n", pdut_name);
905 gbprox_relay2peer(msg, bss_bvc, ns_bvci);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100906 n_nses++;
907 /* Only send it once to each NSE */
908 break;
Daniel Willmann447ad442020-11-26 18:19:21 +0100909 }
Harald Welte8553f552020-11-24 11:31:13 +0100910 }
911 } else {
Harald Welte1aa0ae92020-12-12 15:58:28 +0100912 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, missing IE\n");
Harald Welte8553f552020-11-24 11:31:13 +0100913 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
914 }
Harald Welte4cf12e92010-05-13 14:14:56 +0200915
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100916 if (n_nses == 0) {
Harald Welte1aa0ae92020-12-12 15:58:28 +0100917 LOGPNSE(sgsn_nse, LOGL_ERROR, "BSSGP PAGING: unable to route, no destination found\n");
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200918 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200919 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +0200920 }
Harald Welte3375fa42020-11-23 15:14:20 +0100921 return 0;
Harald Welte9f75c352010-04-30 20:26:32 +0200922}
923
Harald Welte0a4050c2010-05-11 10:01:17 +0200924/* Receive an incoming BVC-RESET message from the SGSN */
Harald Welte5687ae62020-12-05 19:59:45 +0100925static int rx_bvc_reset_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, struct tlv_parsed *tp,
926 uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +0200927{
Harald Welte5687ae62020-12-05 19:59:45 +0100928 uint16_t ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
929 struct gbproxy_bvc *from_bvc;
Harald Welte0a4050c2010-05-11 10:01:17 +0200930
Harald Welte5687ae62020-12-05 19:59:45 +0100931 LOGPNSE(nse, LOGL_INFO, "Rx BVC-RESET (BVCI=%05u)\n", ptp_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200932
Harald Welte5687ae62020-12-05 19:59:45 +0100933 if (ptp_bvci == 0) {
934 from_bvc = gbproxy_bvc_by_bvci(nse, 0);
935 OSMO_ASSERT(from_bvc);
936 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
937 } else {
938 from_bvc = gbproxy_bvc_by_bvci(nse, ptp_bvci);
939 if (!from_bvc) {
940 LOGPNSE(nse, LOGL_ERROR, "Rx BVC-RESET BVCI=%05u: Cannot find BVC\n", ptp_bvci);
941 rate_ctr_inc(&nse->cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
942 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &ptp_bvci, msg);
Harald Welte0a4050c2010-05-11 10:01:17 +0200943 }
Harald Welte5687ae62020-12-05 19:59:45 +0100944 osmo_fsm_inst_dispatch(from_bvc->fi, BSSGP_BVCFSM_E_RX_RESET, msg);
Daniel Willmann447ad442020-11-26 18:19:21 +0100945 }
Harald Welte0a4050c2010-05-11 10:01:17 +0200946
947 return 0;
948}
949
Harald Welte9f75c352010-04-30 20:26:32 +0200950/* Receive an incoming signalling message from the SGSN-side NS-VC */
Harald Weltebf698332020-12-07 17:48:11 +0100951static int gbprox_rx_sig_from_sgsn(struct gbproxy_nse *nse, struct msgb *msg, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200952{
Harald Weltebf698332020-12-07 17:48:11 +0100953 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200954 uint8_t pdu_type = bgph->pdu_type;
Harald Welte5687ae62020-12-05 19:59:45 +0100955 const char *pdut_name = osmo_tlv_prot_msg_name(&osmo_pdef_bssgp, bgph->pdu_type);
956 struct gbproxy_config *cfg = nse->cfg;
957 struct gbproxy_bvc *sgsn_bvc;
958 struct tlv_parsed tp;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200959 int data_len;
Harald Welte9f75c352010-04-30 20:26:32 +0200960 uint16_t bvci;
Harald Welte052d8552020-12-06 16:32:01 +0100961 char log_pfx[32];
Harald Welte9f75c352010-04-30 20:26:32 +0200962 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200963 int cause;
Harald Welte78db2442020-12-05 00:31:07 +0100964 int i;
Harald Welte9f75c352010-04-30 20:26:32 +0200965
Harald Welte5687ae62020-12-05 19:59:45 +0100966 snprintf(log_pfx, sizeof(log_pfx), "NSE(%05u/SGSN)-BVC(%05u/??)", nse->nsei, ns_bvci);
967
968 LOGP(DGPRS, LOGL_DEBUG, "%s Rx %s\n", log_pfx, pdut_name);
Harald Welte052d8552020-12-06 16:32:01 +0100969
Harald Weltec471d3d2011-02-06 17:13:12 +0100970 if (ns_bvci != 0 && ns_bvci != 1) {
Harald Welte052d8552020-12-06 16:32:01 +0100971 LOGP(DGPRS, LOGL_NOTICE, "%s BVCI=%05u is not signalling\n", log_pfx, ns_bvci);
Harald Weltebf698332020-12-07 17:48:11 +0100972 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200973 }
974
Harald Welted651edc2020-12-06 13:35:24 +0100975 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_SIG)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100976 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in signalling BVC\n", log_pfx, pdut_name);
Harald Weltebf698332020-12-07 17:48:11 +0100977 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welted651edc2020-12-06 13:35:24 +0100978 }
979
980 if (!(bssgp_pdu_type_flags(pdu_type) & BSSGP_PDUF_DL)) {
Harald Welte5687ae62020-12-05 19:59:45 +0100981 LOGP(DGPRS, LOGL_NOTICE, "%s %s not allowed in downlink direction\n", log_pfx, pdut_name);
Harald Weltebf698332020-12-07 17:48:11 +0100982 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200983 }
984
Harald Weltebf698332020-12-07 17:48:11 +0100985 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte052d8552020-12-06 16:32:01 +0100986
987 rc = osmo_tlv_prot_parse(&osmo_pdef_bssgp, &tp, 1, pdu_type, bgph->data, data_len, 0, 0,
988 DGPRS, log_pfx);
989 if (rc < 0) {
990 rc = tx_status_from_tlvp(rc, msg);
Harald Welte052d8552020-12-06 16:32:01 +0100991 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
992 return rc;
993 }
Harald Welte5687ae62020-12-05 19:59:45 +0100994 /* hack to get both msg + tlv_parsed passed via osmo_fsm_inst_dispatch */
995 msgb_bcid(msg) = (void *)&tp;
Harald Welte9f75c352010-04-30 20:26:32 +0200996
997 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200998 case BSSGP_PDUT_BVC_RESET:
Harald Welte5687ae62020-12-05 19:59:45 +0100999 /* resolve or create ggbproxy_bvc + handle in BVC-FSM */
1000 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1001 rc = rx_bvc_reset_from_sgsn(nse, msg, &tp, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001002 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001003 case BSSGP_PDUT_BVC_RESET_ACK:
Daniel Willmann15c9da22020-11-03 21:12:42 +01001004 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte5687ae62020-12-05 19:59:45 +01001005 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1006 if (!sgsn_bvc)
1007 goto err_no_bvc;
1008 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_RESET_ACK, msg);
1009 break;
1010 case BSSGP_PDUT_BVC_BLOCK_ACK:
1011 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1012 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1013 if (!sgsn_bvc)
1014 goto err_no_bvc;
1015 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_BLOCK_ACK, msg);
1016 break;
1017 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1018 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
1019 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1020 if (!sgsn_bvc)
1021 goto err_no_bvc;
1022 rc = osmo_fsm_inst_dispatch(sgsn_bvc->fi, BSSGP_BVCFSM_E_RX_UNBLOCK_ACK, msg);
Daniel Willmann15c9da22020-11-03 21:12:42 +01001023 break;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +02001024 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +02001025 /* simple case: BVCI IE is mandatory */
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +02001026 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte5687ae62020-12-05 19:59:45 +01001027 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1028 if (!sgsn_bvc)
1029 goto err_no_bvc;
1030 if (sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1031 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001032 break;
1033 case BSSGP_PDUT_PAGING_PS:
1034 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +02001035 /* process the paging request (LAI/RAI lookup) */
Harald Welte5687ae62020-12-05 19:59:45 +01001036 rc = gbprox_rx_paging(nse, msg, pdut_name, &tp, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001037 break;
1038 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +02001039 /* Some exception has occurred */
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001040 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte5687ae62020-12-05 19:59:45 +01001041 LOGPNSE(nse, LOGL_NOTICE, "Rx STATUS cause=0x%02x(%s) ", cause,
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001042 bssgp_cause_str(cause));
Harald Welte6c4c6f02020-12-03 15:36:59 +01001043 if (TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +02001044 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Daniel Willmann103a7ec2020-12-02 16:08:02 +01001045 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Harald Welte5687ae62020-12-05 19:59:45 +01001046 sgsn_bvc = gbproxy_bvc_by_bvci(nse, bvci);
1047 /* don't send STATUS in response to STATUS if !bvc */
1048 if (sgsn_bvc && sgsn_bvc->cell && sgsn_bvc->cell->bss_bvc)
1049 rc = gbprox_relay2peer(msg, sgsn_bvc->cell->bss_bvc, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +02001050 } else
1051 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +02001052 break;
1053 /* those only exist in the SGSN -> BSS direction */
1054 case BSSGP_PDUT_SUSPEND_ACK:
1055 case BSSGP_PDUT_SUSPEND_NACK:
1056 case BSSGP_PDUT_RESUME_ACK:
1057 case BSSGP_PDUT_RESUME_NACK:
Harald Welte5687ae62020-12-05 19:59:45 +01001058 /* FIXME: handle based on TLLI cache. The RA-ID is not a unique
1059 * criterion, so we have to rely on the TLLI->BVC state created
1060 * while processing the SUSPEND/RESUME in uplink */
1061 /* FIXME: route to SGSN baed on NRI derived from TLLI */
Harald Welte36f98d92011-02-06 13:09:29 +01001062 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001063 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltec8d98ac2020-12-02 20:06:04 +01001064 case BSSGP_PDUT_OVERLOAD:
Harald Welte5687ae62020-12-05 19:59:45 +01001065 LOGPNSE(nse, LOGL_DEBUG, "Rx %s: broadcasting\n", pdut_name);
Harald Welte8cd74402020-12-04 22:24:47 +01001066 /* broadcast to all BSS-side bvcs */
Harald Welte78db2442020-12-05 00:31:07 +01001067 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltec8d98ac2020-12-02 20:06:04 +01001068 gbprox_relay2nse(msg, nse, 0);
1069 }
Harald Welte9f75c352010-04-30 20:26:32 +02001070 break;
Harald Welte5687ae62020-12-05 19:59:45 +01001071 case BSSGP_PDUT_RAN_INFO:
1072 case BSSGP_PDUT_RAN_INFO_REQ:
1073 case BSSGP_PDUT_RAN_INFO_ACK:
1074 case BSSGP_PDUT_RAN_INFO_ERROR:
1075 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
1076 /* FIXME: route based in RIM Routing IE */
Harald Weltebf698332020-12-07 17:48:11 +01001077 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte5687ae62020-12-05 19:59:45 +01001078 break;
Harald Welte9f75c352010-04-30 20:26:32 +02001079 default:
Harald Welte5687ae62020-12-05 19:59:45 +01001080 LOGPNSE(nse, LOGL_NOTICE, "Rx %s: Not supported\n", pdut_name);
1081 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Weltebf698332020-12-07 17:48:11 +01001082 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001083 break;
1084 }
1085
1086 return rc;
Harald Welte5687ae62020-12-05 19:59:45 +01001087
Harald Welte8cd74402020-12-04 22:24:47 +01001088err_no_bvc:
Harald Welte5687ae62020-12-05 19:59:45 +01001089 LOGPNSE(nse, LOGL_ERROR, "Rx %s: Cannot find BVC\n", pdut_name);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001090 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Harald Weltebf698332020-12-07 17:48:11 +01001091 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +02001092}
1093
Harald Welte5687ae62020-12-05 19:59:45 +01001094
1095/***********************************************************************
1096 * libosmogb NS/BSSGP integration
1097 ***********************************************************************/
1098
Alexander Couzens82182d02020-09-22 13:21:46 +02001099int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Harald Welte9f75c352010-04-30 20:26:32 +02001100{
Harald Welte672f5c42010-05-03 18:54:58 +02001101 int rc;
Alexander Couzens82182d02020-09-22 13:21:46 +02001102 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
1103 struct gprs_ns2_inst *nsi = cfg->nsi;
1104 struct osmo_gprs_ns2_prim nsp = {};
1105
1106 nsp.bvci = msgb_bvci(msg);
1107 nsp.nsei = msgb_nsei(msg);
1108
1109 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
1110 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
1111
1112 return rc;
1113}
1114
1115/* Main input function for Gb proxy */
1116int gbprox_rcvmsg(void *ctx, struct msgb *msg)
1117{
Alexander Couzens82182d02020-09-22 13:21:46 +02001118 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Harald Welte5687ae62020-12-05 19:59:45 +01001119 uint16_t ns_bvci = msgb_bvci(msg);
1120 uint16_t nsei = msgb_nsei(msg);
1121 struct gbproxy_nse *nse;
Harald Welte9f75c352010-04-30 20:26:32 +02001122
Harald Welted651edc2020-12-06 13:35:24 +01001123 /* ensure minimum length to decode PCU type */
1124 if (msgb_bssgp_len(msg) < sizeof(struct bssgp_normal_hdr))
1125 return bssgp_tx_status(BSSGP_CAUSE_SEM_INCORR_PDU, NULL, msg);
1126
Harald Welte5687ae62020-12-05 19:59:45 +01001127 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_SGSN);
1128 if (nse) {
1129 if (ns_bvci == 0 || ns_bvci == 1)
1130 return gbprox_rx_sig_from_sgsn(nse, msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001131 else
Harald Welte5687ae62020-12-05 19:59:45 +01001132 return gbprox_rx_ptp_from_sgsn(nse, msg, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +02001133 }
1134
Harald Welte5687ae62020-12-05 19:59:45 +01001135 nse = gbproxy_nse_by_nsei(cfg, nsei, NSE_F_BSS);
1136 if (!nse) {
1137 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/BSS) not known -> allocating\n", nsei);
1138 nse = gbproxy_nse_alloc(cfg, nsei, false);
1139 }
1140 if (nse) {
1141 if (ns_bvci == 0 || ns_bvci == 1)
1142 return gbprox_rx_sig_from_bss(nse, msg, ns_bvci);
1143 else
1144 return gbprox_rx_ptp_from_bss(nse, msg, ns_bvci);
1145 }
1146
1147 return 0;
Harald Welte9f75c352010-04-30 20:26:32 +02001148}
Harald Welte85801d02010-05-11 05:49:43 +02001149
Alexander Couzens82182d02020-09-22 13:21:46 +02001150/* TODO: What about handling:
1151 * NS_AFF_CAUSE_VC_FAILURE,
1152 NS_AFF_CAUSE_VC_RECOVERY,
1153 NS_AFF_CAUSE_FAILURE,
1154 NS_AFF_CAUSE_RECOVERY,
1155 osmocom own causes
1156 NS_AFF_CAUSE_SNS_CONFIGURED,
1157 NS_AFF_CAUSE_SNS_FAILURE,
1158 */
Harald Welte1ccbf442010-05-14 11:53:08 +00001159
Alexander Couzens82182d02020-09-22 13:21:46 +02001160void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Harald Weltec1c1dd22010-05-11 06:34:24 +02001161{
Harald Welte8cd74402020-12-04 22:24:47 +01001162 /* TODO: bss nsei available/unavailable bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, nsvc->nsei, bvc->bvci, 0);
Alexander Couzens82182d02020-09-22 13:21:46 +02001163 * TODO: sgsn nsei available/unavailable
1164 */
Harald Welte5687ae62020-12-05 19:59:45 +01001165
Harald Welte8cd74402020-12-04 22:24:47 +01001166 struct gbproxy_bvc *bvc;
Harald Welte5687ae62020-12-05 19:59:45 +01001167 struct gbproxy_nse *sgsn_nse;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001168
Alexander Couzens82182d02020-09-22 13:21:46 +02001169 switch (nsp->u.status.cause) {
1170 case NS_AFF_CAUSE_SNS_FAILURE:
1171 case NS_AFF_CAUSE_SNS_CONFIGURED:
1172 break;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001173
Alexander Couzens82182d02020-09-22 13:21:46 +02001174 case NS_AFF_CAUSE_RECOVERY:
1175 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Harald Welte5687ae62020-12-05 19:59:45 +01001176 sgsn_nse = gbproxy_nse_by_nsei(cfg, nsp->nsei, NSE_F_SGSN);
1177 if (sgsn_nse) {
1178 uint8_t cause = BSSGP_CAUSE_OML_INTERV;
1179 bvc = gbproxy_bvc_by_bvci(sgsn_nse, 0);
1180 if (bvc)
1181 osmo_fsm_inst_dispatch(bvc->fi, BSSGP_BVCFSM_E_REQ_RESET, &cause);
Harald Welte5e106d72011-02-06 16:33:29 +01001182 }
Alexander Couzens82182d02020-09-22 13:21:46 +02001183 break;
1184 case NS_AFF_CAUSE_FAILURE:
Harald Welte5687ae62020-12-05 19:59:45 +01001185#if 0
Harald Welte4bf53ef2020-12-05 17:50:23 +01001186 if (gbproxy_is_sgsn_nsei(cfg, nsp->nsei)) {
Alexander Couzens82182d02020-09-22 13:21:46 +02001187 /* sgsn */
1188 /* TODO: BSVC: block all PtP towards bss */
1189 rate_ctr_inc(&cfg->ctrg->
1190 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
1191 } else {
Daniel Willmann447ad442020-11-26 18:19:21 +01001192 /* bss became unavailable
1193 * TODO: Block all BVC belonging to that NSE */
Harald Welte8cd74402020-12-04 22:24:47 +01001194 bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei);
1195 if (!bvc) {
Alexander Couzens82182d02020-09-22 13:21:46 +02001196 /* TODO: use primitive name + status cause name */
Harald Welte8cd74402020-12-04 22:24:47 +01001197 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n",
Alexander Couzens82182d02020-09-22 13:21:46 +02001198 nsp->u.status.cause, nsp->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +01001199 break;
1200 }
Alexander Couzens82182d02020-09-22 13:21:46 +02001201
Harald Welte8cd74402020-12-04 22:24:47 +01001202 if (!bvc->blocked)
Alexander Couzens82182d02020-09-22 13:21:46 +02001203 break;
Harald Welte5687ae62020-12-05 19:59:45 +01001204 hash_for_each(cfg->sgsn_nses, _sgsn, sgsn_nse, list) {
1205 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, sgsn_nse->nsei, bvc->bvci, 0);
1206 }
Harald Weltec1c1dd22010-05-11 06:34:24 +02001207 }
Harald Welte5687ae62020-12-05 19:59:45 +01001208#endif
Alexander Couzens82182d02020-09-22 13:21:46 +02001209 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
1210 break;
1211 default:
Harald Welted97ff682020-11-30 10:55:22 +01001212 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
1213 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens82182d02020-09-22 13:21:46 +02001214 break;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001215 }
Alexander Couzens82182d02020-09-22 13:21:46 +02001216}
1217
Alexander Couzens82182d02020-09-22 13:21:46 +02001218/* called by the ns layer */
1219int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
1220{
1221 struct osmo_gprs_ns2_prim *nsp;
1222 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmanne8c8ec92020-12-02 19:33:50 +01001223 uintptr_t bvci;
Alexander Couzens82182d02020-09-22 13:21:46 +02001224 int rc = 0;
1225
1226 if (oph->sap != SAP_NS)
1227 return 0;
1228
1229 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
1230
1231 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welted97ff682020-11-30 10:55:22 +01001232 LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
1233 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens82182d02020-09-22 13:21:46 +02001234 return 0;
1235 }
1236
1237 switch (oph->primitive) {
1238 case PRIM_NS_UNIT_DATA:
Daniel Willmanne8c8ec92020-12-02 19:33:50 +01001239
Alexander Couzens82182d02020-09-22 13:21:46 +02001240 /* hand the message into the BSSGP implementation */
1241 msgb_bssgph(oph->msg) = oph->msg->l3h;
1242 msgb_bvci(oph->msg) = nsp->bvci;
1243 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmanne8c8ec92020-12-02 19:33:50 +01001244 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens82182d02020-09-22 13:21:46 +02001245
Daniel Willmanne8c8ec92020-12-02 19:33:50 +01001246 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens82182d02020-09-22 13:21:46 +02001247 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmann02f2c342020-11-04 17:32:56 +01001248 msgb_free(oph->msg);
Alexander Couzens82182d02020-09-22 13:21:46 +02001249 break;
1250 case PRIM_NS_STATUS:
1251 gprs_ns_prim_status_cb(cfg, nsp);
1252 break;
1253 default:
Harald Welted97ff682020-11-30 10:55:22 +01001254 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
1255 gprs_ns2_prim_str(oph->primitive),
1256 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens82182d02020-09-22 13:21:46 +02001257 break;
1258 }
1259
1260 return rc;
Harald Weltec1c1dd22010-05-11 06:34:24 +02001261}
1262
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001263void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001264{
Harald Welte78db2442020-12-05 00:31:07 +01001265 struct gbproxy_nse *nse;
1266 struct hlist_node *ntmp;
Harald Welte993d3f42020-12-05 10:14:49 +01001267 int i, j;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001268
Harald Welte78db2442020-12-05 00:31:07 +01001269 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte993d3f42020-12-05 10:14:49 +01001270 struct gbproxy_bvc *bvc;
1271 struct hlist_node *tmp;
1272 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte8cd74402020-12-04 22:24:47 +01001273 gbproxy_bvc_free(bvc);
Daniel Willmann447ad442020-11-26 18:19:21 +01001274
1275 gbproxy_nse_free(nse);
1276 }
Harald Welte5687ae62020-12-05 19:59:45 +01001277 /* FIXME: cells */
1278 /* FIXME: SGSN side BVCs (except signaling) */
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001279
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +02001280 rate_ctr_group_free(cfg->ctrg);
1281 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +02001282}
1283
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001284int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001285{
Jacob Erlbeck0d376712014-08-11 19:12:24 +02001286 struct timespec tp;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001287
Harald Welte9e917642020-12-12 19:02:16 +01001288 /* by default we advertise 100% of the BSS-side capacity to _each_ SGSN */
1289 cfg->pool.bvc_fc_ratio = 100;
Daniel Willmannef3c9af2020-12-14 16:22:39 +01001290 cfg->pool.null_nri_ranges = osmo_nri_ranges_alloc(cfg);
1291
Harald Welte78db2442020-12-05 00:31:07 +01001292 hash_init(cfg->bss_nses);
Daniel Willmann30542132020-12-21 18:08:21 +01001293 hash_init(cfg->sgsn_nses);
1294 hash_init(cfg->cells);
Daniel Willmannef3c9af2020-12-14 16:22:39 +01001295 INIT_LLIST_HEAD(&cfg->sgsns);
1296
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +01001297 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
Harald Welte26c14652017-07-12 00:25:51 +02001298 if (!cfg->ctrg) {
1299 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
1300 return -1;
1301 }
Pau Espin Pedrol36abead2018-08-17 13:27:20 +02001302 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Harald Weltea54ed462020-12-07 13:12:13 +01001303 osmo_fsm_log_timeouts(true);
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +02001304
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +02001305 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +02001306}