blob: 74f799d841ec89729b9925cd68d48639eb81ae8f [file] [log] [blame]
Harald Welte9f75c352010-04-30 20:26:32 +02001/* NS-over-IP proxy */
2
3/* (C) 2010 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>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/talloc.h>
37#include <osmocom/core/select.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020038#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010039#include <osmocom/core/stats.h>
Jacob Erlbeckbc555742013-10-18 14:34:55 +020040
Alexander Couzens82182d02020-09-22 13:21:46 +020041#include <osmocom/gprs/gprs_ns2.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080042#include <osmocom/gprs/gprs_bssgp.h>
Alexander Couzens82182d02020-09-22 13:21:46 +020043#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080044
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020045#include <osmocom/gsm/gsm_utils.h>
46
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020047#include <osmocom/sgsn/signal.h>
48#include <osmocom/sgsn/debug.h>
49#include <osmocom/sgsn/gprs_gb_parse.h>
50#include <osmocom/sgsn/gb_proxy.h>
Harald Welte9f75c352010-04-30 20:26:32 +020051
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020052#include <osmocom/sgsn/gprs_llc.h>
Harald Welte53373bc2016-04-20 17:11:43 +020053#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020054#include <osmocom/sgsn/gprs_utils.h>
Jacob Erlbeck67a44452014-05-19 10:14:58 +020055
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010056extern void *tall_sgsn_ctx;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020057
Jacob Erlbeckbc555742013-10-18 14:34:55 +020058static const struct rate_ctr_desc global_ctr_description[] = {
59 { "inv-bvci", "Invalid BVC Identifier " },
Jacob Erlbeck8f503592014-06-02 10:49:00 +020060 { "inv-lai", "Invalid Location Area Identifier" },
61 { "inv-rai", "Invalid Routing Area Identifier " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020062 { "inv-nsei", "No BVC established for NSEI " },
Pau Espin Pedrol437e2092018-07-24 12:41:26 +020063 { "proto-err:bss", "BSSGP protocol error (BSS )" },
64 { "proto-err:sgsn", "BSSGP protocol error (SGSN)" },
65 { "not-supp:bss", "Feature not supported (BSS )" },
66 { "not-supp:sgsn", "Feature not supported (SGSN)" },
67 { "restart:sgsn", "Restarted RESET procedure (SGSN)" },
68 { "tx-err:sgsn", "NS Transmission error (SGSN)" },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020069 { "error", "Other error " },
Jacob Erlbeck006c0382014-05-27 13:49:04 +020070 { "mod-peer-err", "Patch error: no peer " },
Jacob Erlbeckbc555742013-10-18 14:34:55 +020071};
72
73static const struct rate_ctr_group_desc global_ctrg_desc = {
Max8a01a802017-12-20 13:10:11 +010074 .group_name_prefix = "gbproxy:global",
Jacob Erlbeckbc555742013-10-18 14:34:55 +020075 .group_description = "GBProxy Global Statistics",
76 .num_ctr = ARRAY_SIZE(global_ctr_description),
77 .ctr_desc = global_ctr_description,
Jacob Erlbeck46caed82015-11-02 15:15:38 +010078 .class_id = OSMO_STATS_CLASS_GLOBAL,
Jacob Erlbeckbc555742013-10-18 14:34:55 +020079};
80
Harald Welte8cd74402020-12-04 22:24:47 +010081static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann8bca8de2020-11-03 21:11:45 +010082 uint16_t ns_bvci);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020083static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +020084 uint16_t ns_bvci, uint16_t sgsn_nsei);
Jacob Erlbeck5f4ef322014-08-22 17:10:01 +020085
Harald Welte8cd74402020-12-04 22:24:47 +010086static int check_bvc_nsei(struct gbproxy_bvc *bvc, uint16_t nsei)
Jacob Erlbeckbc555742013-10-18 14:34:55 +020087{
Harald Welte8cd74402020-12-04 22:24:47 +010088 OSMO_ASSERT(bvc);
89 OSMO_ASSERT(bvc->nse);
Daniel Willmann447ad442020-11-26 18:19:21 +010090
Harald Welte8cd74402020-12-04 22:24:47 +010091 if (bvc->nse->nsei != nsei) {
92 LOGPBVC(bvc, LOGL_NOTICE, "Peer entry doesn't match current NSEI "
Daniel Willmann6e8ed272020-12-02 13:54:21 +010093 "via NSE(%05u/BSS)\n", nsei);
Harald Welte8cd74402020-12-04 22:24:47 +010094 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_INV_NSEI]);
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020095 return 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020096 }
97
Jacob Erlbeck64cb9242013-10-24 12:48:56 +020098 return 1;
Jacob Erlbeckbc555742013-10-18 14:34:55 +020099}
100
Harald Welte69619e32010-05-03 19:05:10 +0200101/* strip off the NS header */
102static void strip_ns_hdr(struct msgb *msg)
103{
104 int strip_len = msgb_bssgph(msg) - msg->data;
105 msgb_pull(msg, strip_len);
106}
107
Harald Welte8cd74402020-12-04 22:24:47 +0100108/* feed a message down the NS-VC associated with the specified bvc */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200109static int gbprox_relay2sgsn(struct gbproxy_config *cfg, struct msgb *old_msg,
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200110 uint16_t ns_bvci, uint16_t sgsn_nsei)
Harald Welte672f5c42010-05-03 18:54:58 +0200111{
Harald Welte39d0bb52010-05-12 18:10:25 +0000112 /* create a copy of the message so the old one can
113 * be free()d safely when we return from gbprox_rcvmsg() */
Alexander Couzens82182d02020-09-22 13:21:46 +0200114 struct gprs_ns2_inst *nsi = cfg->nsi;
115 struct osmo_gprs_ns2_prim nsp = {};
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +0200116 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2sgsn");
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200117 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000118
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100119 DEBUGP(DGPRS, "NSE(%05u/BSS)-BVC(%05u) proxying BTS->SGSN NSE(%05u/SGSN)\n",
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200120 msgb_nsei(msg), ns_bvci, sgsn_nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200121
Alexander Couzens82182d02020-09-22 13:21:46 +0200122 nsp.bvci = ns_bvci;
123 nsp.nsei = sgsn_nsei;
Harald Welte672f5c42010-05-03 18:54:58 +0200124
Harald Welte69619e32010-05-03 19:05:10 +0200125 strip_ns_hdr(msg);
Alexander Couzens82182d02020-09-22 13:21:46 +0200126 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
127 PRIM_OP_REQUEST, msg);
128 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200129 if (rc < 0)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200130 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_TX_ERR_SGSN]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200131 return rc;
Harald Welte672f5c42010-05-03 18:54:58 +0200132}
133
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100134/* feed a message down the NSE */
135static int gbprox_relay2nse(struct msgb *old_msg, struct gbproxy_nse *nse,
Daniel Willmann8bca8de2020-11-03 21:11:45 +0100136 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200137{
Daniel Willmann447ad442020-11-26 18:19:21 +0100138 OSMO_ASSERT(nse);
139 OSMO_ASSERT(nse->cfg);
140
Harald Welte39d0bb52010-05-12 18:10:25 +0000141 /* create a copy of the message so the old one can
142 * be free()d safely when we return from gbprox_rcvmsg() */
Daniel Willmann447ad442020-11-26 18:19:21 +0100143 struct gprs_ns2_inst *nsi = nse->cfg->nsi;
Alexander Couzens82182d02020-09-22 13:21:46 +0200144 struct osmo_gprs_ns2_prim nsp = {};
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100145 struct msgb *msg = bssgp_msgb_copy(old_msg, "msgb_relay2nse");
Harald Welte2636e892020-11-18 12:01:46 +0100146 uint32_t tlli;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200147 int rc;
Harald Welte39d0bb52010-05-12 18:10:25 +0000148
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100149 DEBUGP(DGPRS, "NSE(%05u/SGSN)-BVC(%05u) proxying SGSN->BSS NSE(%05u/BSS)\n",
Daniel Willmann447ad442020-11-26 18:19:21 +0100150 msgb_nsei(msg), ns_bvci, nse->nsei);
Harald Welte44c48302010-05-03 19:22:32 +0200151
Alexander Couzens82182d02020-09-22 13:21:46 +0200152 nsp.bvci = ns_bvci;
Daniel Willmann447ad442020-11-26 18:19:21 +0100153 nsp.nsei = nse->nsei;
Harald Welte9f75c352010-04-30 20:26:32 +0200154
Harald Welte0ab535b2010-05-13 10:34:56 +0200155 /* Strip the old NS header, it will be replaced with a new one */
Harald Welte69619e32010-05-03 19:05:10 +0200156 strip_ns_hdr(msg);
157
Harald Welte2636e892020-11-18 12:01:46 +0100158 /* TS 48.018 Section 5.4.2: The link selector parameter is
159 * defined in 3GPP TS 48.016. At one side of the Gb interface,
160 * all BSSGP UNITDATA PDUs related to an MS shall be passed with
161 * the same LSP, e.g. the LSP contains the MS's TLLI, to the
162 * underlying network service. */
163 if (gprs_gb_parse_tlli(msgb_data(msg), msgb_length(msg), &tlli) == 1)
164 nsp.u.unitdata.link_selector = tlli;
165
Alexander Couzens82182d02020-09-22 13:21:46 +0200166 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA,
167 PRIM_OP_REQUEST, msg);
168 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100169 /* FIXME: We need a counter group for gbproxy_nse */
170 //if (rc < 0)
Harald Welte8cd74402020-12-04 22:24:47 +0100171 // rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100172
173 return rc;
174}
175
Harald Welte8cd74402020-12-04 22:24:47 +0100176/* feed a message down the NS-VC associated with the specified bvc */
177static int gbprox_relay2peer(struct msgb *old_msg, struct gbproxy_bvc *bvc,
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100178 uint16_t ns_bvci)
179{
180 int rc;
Harald Welte8cd74402020-12-04 22:24:47 +0100181 struct gbproxy_nse *nse = bvc->nse;
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100182 OSMO_ASSERT(nse);
183
184 rc = gbprox_relay2nse(old_msg, nse, ns_bvci);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200185 if (rc < 0)
Harald Welte8cd74402020-12-04 22:24:47 +0100186 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_TX_ERR]);
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200187
188 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200189}
190
Harald Welte8cd74402020-12-04 22:24:47 +0100191static int block_unblock_bvc(struct gbproxy_config *cfg, uint16_t ptp_bvci, uint8_t pdu_type)
Harald Welte36f98d92011-02-06 13:09:29 +0100192{
Harald Welte8cd74402020-12-04 22:24:47 +0100193 struct gbproxy_bvc *bvc;
Harald Welte36f98d92011-02-06 13:09:29 +0100194
Harald Welte8cd74402020-12-04 22:24:47 +0100195 bvc = gbproxy_bvc_by_bvci(cfg, ptp_bvci);
196 if (!bvc) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100197 LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n",
Harald Welte36f98d92011-02-06 13:09:29 +0100198 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200199 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100200 return -ENOENT;
201 }
202
203 switch (pdu_type) {
204 case BSSGP_PDUT_BVC_BLOCK_ACK:
Harald Welte8cd74402020-12-04 22:24:47 +0100205 bvc->blocked = true;
206 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_BLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100207 break;
208 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte8cd74402020-12-04 22:24:47 +0100209 bvc->blocked = false;
210 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_UNBLOCKED]);
Harald Welte36f98d92011-02-06 13:09:29 +0100211 break;
212 default:
213 break;
214 }
215 return 0;
216}
217
Harald Welte8cd74402020-12-04 22:24:47 +0100218/* Send a message to a bvc identified by ptp_bvci but using ns_bvci
Harald Welte9f75c352010-04-30 20:26:32 +0200219 * in the NS hdr */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200220static int gbprox_relay2bvci(struct gbproxy_config *cfg, struct msgb *msg, uint16_t ptp_bvci,
Harald Welte9f75c352010-04-30 20:26:32 +0200221 uint16_t ns_bvci)
222{
Harald Welte8cd74402020-12-04 22:24:47 +0100223 struct gbproxy_bvc *bvc;
Harald Welte9f75c352010-04-30 20:26:32 +0200224
Harald Welte8cd74402020-12-04 22:24:47 +0100225 bvc = gbproxy_bvc_by_bvci(cfg, ptp_bvci);
226 if (!bvc) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100227 LOGP(DGPRS, LOGL_ERROR, "BVC(%05u/??) Cannot find BSS\n",
Harald Welte1c77c6e2010-05-03 21:37:11 +0200228 ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200229 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte9f75c352010-04-30 20:26:32 +0200230 return -ENOENT;
Harald Welte1c77c6e2010-05-03 21:37:11 +0200231 }
Harald Welte9f75c352010-04-30 20:26:32 +0200232
Harald Welte8cd74402020-12-04 22:24:47 +0100233 return gbprox_relay2peer(msg, bvc, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200234}
235
Harald Welteb1fd9022012-06-17 12:16:31 +0800236int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
237{
238 return 0;
239}
240
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200241/* Receive an incoming PTP message from a BSS-side NS-VC */
242static int gbprox_rx_ptp_from_bss(struct gbproxy_config *cfg,
243 struct msgb *msg, uint16_t nsei,
Alexander Couzens82182d02020-09-22 13:21:46 +0200244 uint16_t ns_bvci)
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200245{
Harald Welte8cd74402020-12-04 22:24:47 +0100246 struct gbproxy_bvc *bvc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200247
Harald Welte8cd74402020-12-04 22:24:47 +0100248 bvc = gbproxy_bvc_by_bvci(cfg, ns_bvci);
249 if (!bvc) {
250 LOGP(DGPRS, LOGL_NOTICE, "BVC(%05u/??) Didn't find bvc "
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100251 "for PTP message from NSE(%05u/BSS), "
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200252 "discarding message\n",
Alexander Couzens82182d02020-09-22 13:21:46 +0200253 ns_bvci, nsei);
Jacob Erlbeck058ae122014-10-10 09:07:04 +0200254 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
255 &ns_bvci, msg);
256 }
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200257
Daniel Willmann6626bbc2020-12-02 17:46:56 +0100258 /* TODO: Should we discard this message if the check fails */
Harald Welte8cd74402020-12-04 22:24:47 +0100259 check_bvc_nsei(bvc, nsei);
Jacob Erlbeck43b8f9f2014-10-02 14:56:44 +0200260
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200261 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200262}
263
264/* Receive an incoming PTP message from a SGSN-side NS-VC */
265static int gbprox_rx_ptp_from_sgsn(struct gbproxy_config *cfg,
266 struct msgb *msg, uint16_t nsei,
Alexander Couzens82182d02020-09-22 13:21:46 +0200267 uint16_t ns_bvci)
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200268{
Harald Welte8cd74402020-12-04 22:24:47 +0100269 struct gbproxy_bvc *bvc;
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200270
Harald Welte8cd74402020-12-04 22:24:47 +0100271 bvc = gbproxy_bvc_by_bvci(cfg, ns_bvci);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200272
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200273 /* Send status messages before patching */
274
Harald Welte8cd74402020-12-04 22:24:47 +0100275 if (!bvc) {
276 LOGP(DGPRS, LOGL_INFO, "BVC(%05u/??) Didn't find bvc for "
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100277 "for message from NSE(%05u/SGSN)\n",
Alexander Couzens82182d02020-09-22 13:21:46 +0200278 ns_bvci, nsei);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200279 rate_ctr_inc(&cfg->ctrg->
280 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
281 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
282 &ns_bvci, msg);
283 }
284
Harald Welte8cd74402020-12-04 22:24:47 +0100285 if (bvc->blocked) {
286 LOGPBVC(bvc, LOGL_NOTICE, "Dropping PDU for "
Daniel Willmann6e8ed272020-12-02 13:54:21 +0100287 "blocked BVC via NSE(%05u/SGSN)\n", nsei);
Harald Welte8cd74402020-12-04 22:24:47 +0100288 rate_ctr_inc(&bvc->ctrg->ctr[GBPROX_PEER_CTR_DROPPED]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200289 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &ns_bvci, msg);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200290 }
291
Harald Welte8cd74402020-12-04 22:24:47 +0100292 return gbprox_relay2peer(msg, bvc, ns_bvci);
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200293}
294
Harald Welteac44d6b2020-12-02 22:53:26 +0100295/* process a BVC-RESET message from the BSS side */
296static int gbprox_rx_bvc_reset_from_bss(struct gbproxy_config *cfg, struct msgb *msg,
Harald Welte91bb7202020-12-05 11:18:23 +0100297 uint16_t nsei, struct tlv_parsed *tp)
Harald Welteac44d6b2020-12-02 22:53:26 +0100298{
Harald Welte8cd74402020-12-04 22:24:47 +0100299 struct gbproxy_bvc *from_bvc = NULL;
Harald Welte1239cf42020-12-02 23:03:22 +0100300 uint16_t bvci;
Harald Welteac44d6b2020-12-02 22:53:26 +0100301
Harald Welte6c4c6f02020-12-03 15:36:59 +0100302 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) || !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefb7f8c52020-12-02 23:29:38 +0100303 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
304 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
305 }
Harald Welteac44d6b2020-12-02 22:53:26 +0100306
Harald Welte1239cf42020-12-02 23:03:22 +0100307 bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
308 LOGP(DGPRS, LOGL_INFO, "NSE(%05u) Rx BVC RESET (BVCI=%05u)\n", nsei, bvci);
309 if (bvci == 0) {
310 /* If we receive a BVC reset on the signalling endpoint, we
311 * don't want the SGSN to reset, as the signalling endpoint
312 * is common for all point-to-point BVCs (and thus all BTS) */
Harald Welte55253712020-12-02 23:06:37 +0100313
Harald Welte8cd74402020-12-04 22:24:47 +0100314 /* Ensure the NSE bvc is there and clear all PtP BVCs */
Harald Welte55253712020-12-02 23:06:37 +0100315 struct gbproxy_nse *nse = gbproxy_nse_by_nsei_or_new(cfg, nsei);
Harald Welte1239cf42020-12-02 23:03:22 +0100316 if (!nse) {
317 LOGP(DGPRS, LOGL_ERROR, "Could not create NSE(%05u)\n", nsei);
318 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
Harald Welteac44d6b2020-12-02 22:53:26 +0100319 return 0;
320 }
Harald Welte1239cf42020-12-02 23:03:22 +0100321
Harald Welte8cd74402020-12-04 22:24:47 +0100322 gbproxy_cleanup_bvcs(cfg, nsei, 0);
Harald Welte1239cf42020-12-02 23:03:22 +0100323
324 /* FIXME: only do this if SGSN is alive! */
Harald Welte55253712020-12-02 23:06:37 +0100325 LOGPNSE(nse, LOGL_INFO, "Tx fake BVC RESET ACK of BVCI=0\n");
Harald Welte1239cf42020-12-02 23:03:22 +0100326 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK, nsei, 0, 0);
327 return 0;
328 } else {
Harald Welte8cd74402020-12-04 22:24:47 +0100329 from_bvc = gbproxy_bvc_by_bvci(cfg, bvci);
330 if (!from_bvc) {
Harald Welteac44d6b2020-12-02 22:53:26 +0100331 struct gbproxy_nse *nse = gbproxy_nse_by_nsei(cfg, nsei);
332 if (!nse) {
333 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
334 "BVCI=%05u\n", nsei, bvci);
335 bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
336 return 0;
337 }
338 /* if a PTP-BVC is reset, and we don't know that
Harald Welte8cd74402020-12-04 22:24:47 +0100339 * PTP-BVCI yet, we should allocate a new bvc */
340 from_bvc = gbproxy_bvc_alloc(nse, bvci);
341 OSMO_ASSERT(from_bvc);
342 LOGPBVC(from_bvc, LOGL_INFO, "Allocated new bvc\n");
Harald Welteac44d6b2020-12-02 22:53:26 +0100343 }
344
345 /* Could have moved to a different NSE */
Harald Welte8cd74402020-12-04 22:24:47 +0100346 if (!check_bvc_nsei(from_bvc, nsei)) {
347 LOGPBVC(from_bvc, LOGL_NOTICE, "moving bvc to NSE(%05u)\n", nsei);
Harald Welteac44d6b2020-12-02 22:53:26 +0100348
349 struct gbproxy_nse *nse_new = gbproxy_nse_by_nsei(cfg, nsei);
350 if (!nse_new) {
351 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) Got PtP BVC reset before signalling reset for "
352 "BVCI=%05u\n", bvci, nsei);
353 bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_STATE, NULL, msg);
354 return 0;
355 }
356
Harald Welte8cd74402020-12-04 22:24:47 +0100357 /* Move bvc to different NSE */
358 gbproxy_bvc_move(from_bvc, nse_new);
Harald Welteac44d6b2020-12-02 22:53:26 +0100359 }
360
Harald Welte6c4c6f02020-12-03 15:36:59 +0100361 if (TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welteac44d6b2020-12-02 22:53:26 +0100362 struct gprs_ra_id raid;
363 /* We have a Cell Identifier present in this
364 * PDU, this means we can extend our local
365 * state information about this particular cell
366 * */
Harald Welte8cd74402020-12-04 22:24:47 +0100367 memcpy(from_bvc->ra, TLVP_VAL(tp, BSSGP_IE_CELL_ID), sizeof(from_bvc->ra));
368 gsm48_parse_ra(&raid, from_bvc->ra);
369 LOGPBVC(from_bvc, LOGL_INFO, "Cell ID %s\n", osmo_rai_name(&raid));
Harald Welteac44d6b2020-12-02 22:53:26 +0100370 }
Harald Welteac44d6b2020-12-02 22:53:26 +0100371 }
372 /* continue processing / relaying to SGSN[s] */
373 return 1;
374}
375
Harald Welte9f75c352010-04-30 20:26:32 +0200376/* Receive an incoming signalling message from a BSS-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200377static int gbprox_rx_sig_from_bss(struct gbproxy_config *cfg,
378 struct msgb *msg, uint16_t nsei,
Harald Welte9f75c352010-04-30 20:26:32 +0200379 uint16_t ns_bvci)
380{
Harald Welteca3620a2010-05-03 16:30:59 +0200381 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200382 struct tlv_parsed tp;
383 uint8_t pdu_type = bgph->pdu_type;
Harald Welteca3620a2010-05-03 16:30:59 +0200384 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte8cd74402020-12-04 22:24:47 +0100385 struct gbproxy_bvc *from_bvc = NULL;
Harald Welte70f38d22010-05-01 12:10:57 +0200386 struct gprs_ra_id raid;
Jacob Erlbeck59300642014-09-08 09:04:01 +0200387 int rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200388
Harald Weltec471d3d2011-02-06 17:13:12 +0100389 if (ns_bvci != 0 && ns_bvci != 1) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100390 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) BVCI=%05u is not signalling\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200391 nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200392 return -EINVAL;
393 }
394
395 /* we actually should never see those two for BVCI == 0, but double-check
396 * just to make sure */
397 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
398 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100399 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200400 "signalling\n", nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200401 return -EINVAL;
402 }
403
404 bssgp_tlv_parse(&tp, bgph->data, data_len);
405
406 switch (pdu_type) {
407 case BSSGP_PDUT_SUSPEND:
408 case BSSGP_PDUT_RESUME:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200409 /* We implement RAI snooping during SUSPEND/RESUME, since it
Harald Welte8cd74402020-12-04 22:24:47 +0100410 * establishes a relationsip between BVCI/bvc and the routeing
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200411 * area identification. The snooped information is then used
412 * for routing the {SUSPEND,RESUME}_[N]ACK back to the correct
413 * BSSGP */
Harald Welte6c4c6f02020-12-03 15:36:59 +0100414 if (!TLVP_PRES_LEN(&tp, BSSGP_IE_ROUTEING_AREA, 6))
Harald Welte9f75c352010-04-30 20:26:32 +0200415 goto err_mand_ie;
Harald Welte8cd74402020-12-04 22:24:47 +0100416 from_bvc = gbproxy_bvc_by_nsei(cfg, nsei);
417 if (!from_bvc)
418 goto err_no_bvc;
419 memcpy(from_bvc->ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
420 sizeof(from_bvc->ra));
421 gsm48_parse_ra(&raid, from_bvc->ra);
422 LOGPBVC(from_bvc, LOGL_INFO, "BSSGP SUSPEND/RESUME "
Daniel Willmann6e8ed272020-12-02 13:54:21 +0100423 "RAI snooping: RAI %s\n",
424 osmo_rai_name(&raid));
Harald Welte9f75c352010-04-30 20:26:32 +0200425 /* FIXME: This only supports one BSS per RA */
426 break;
Harald Welte44c48302010-05-03 19:22:32 +0200427 case BSSGP_PDUT_BVC_RESET:
Harald Welte91bb7202020-12-05 11:18:23 +0100428 rc = gbprox_rx_bvc_reset_from_bss(cfg, msg, nsei, &tp);
Harald Welteac44d6b2020-12-02 22:53:26 +0100429 /* if function retruns 0, we terminate processing here */
430 if (rc == 0)
431 return 0;
Harald Welte44c48302010-05-03 19:22:32 +0200432 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200433 }
434
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200435 return gbprox_relay2sgsn(cfg, msg, ns_bvci, cfg->nsip_sgsn_nsei);
Harald Welte8cd74402020-12-04 22:24:47 +0100436err_no_bvc:
437 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) cannot find bvc based on NSEI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200438 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200439 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_INV_NSEI]);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200440 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200441err_mand_ie:
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100442 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/BSS) missing mandatory RA IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200443 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200444 rate_ctr_inc(&cfg->ctrg->ctr[GBPROX_GLOB_CTR_PROTO_ERR_BSS]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200445 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200446}
447
448/* Receive paging request from SGSN, we need to relay to proper BSS */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200449static int gbprox_rx_paging(struct gbproxy_config *cfg, struct msgb *msg, struct tlv_parsed *tp,
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200450 uint32_t nsei, uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200451{
Daniel Willmann447ad442020-11-26 18:19:21 +0100452 struct gbproxy_nse *nse;
Harald Welte8cd74402020-12-04 22:24:47 +0100453 struct gbproxy_bvc *bvc;
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100454 unsigned int n_nses = 0;
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200455 int errctr = GBPROX_GLOB_CTR_PROTO_ERR_SGSN;
Harald Welte993d3f42020-12-05 10:14:49 +0100456 int i, j;
Harald Welte9f75c352010-04-30 20:26:32 +0200457
Daniel Willmann447ad442020-11-26 18:19:21 +0100458 /* FIXME: Handle paging logic to only page each matching NSE */
459
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100460 LOGP(DGPRS, LOGL_INFO, "NSE(%05u/SGSN) BSSGP PAGING\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200461 nsei);
Harald Welte6c4c6f02020-12-03 15:36:59 +0100462 if (TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200463 uint16_t bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200464 errctr = GBPROX_GLOB_CTR_OTHER_ERR;
Harald Welte8cd74402020-12-04 22:24:47 +0100465 bvc = gbproxy_bvc_by_bvci(cfg, bvci);
466 if (!bvc) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100467 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PAGING: "
468 "unable to route: BVCI=%05u unknown\n", nsei, bvci);
Harald Welte3375fa42020-11-23 15:14:20 +0100469 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
470 return -EINVAL;
471 }
Harald Welte8cd74402020-12-04 22:24:47 +0100472 LOGPBVC(bvc, LOGL_INFO, "routing by BVCI\n");
473 return gbprox_relay2peer(msg, bvc, ns_bvci);
Harald Welte6c4c6f02020-12-03 15:36:59 +0100474 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200475 errctr = GBPROX_GLOB_CTR_INV_RAI;
Harald Welte8cd74402020-12-04 22:24:47 +0100476 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welte78db2442020-12-05 00:31:07 +0100477 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte993d3f42020-12-05 10:14:49 +0100478 hash_for_each(nse->bvcs, j, bvc, list) {
Harald Welte8cd74402020-12-04 22:24:47 +0100479 if (!memcmp(bvc->ra, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA), 6)) {
Daniel Willmannbd356a62020-12-04 01:24:34 +0100480 LOGPNSE(nse, LOGL_INFO, "routing to NSE (RAI match)\n");
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100481 gbprox_relay2nse(msg, nse, ns_bvci);
482 n_nses++;
483 /* Only send it once to each NSE */
484 break;
Daniel Willmann447ad442020-11-26 18:19:21 +0100485 }
Harald Welte3375fa42020-11-23 15:14:20 +0100486 }
487 }
Harald Welte6c4c6f02020-12-03 15:36:59 +0100488 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_LOCATION_AREA, 5)) {
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200489 errctr = GBPROX_GLOB_CTR_INV_LAI;
Harald Welte8cd74402020-12-04 22:24:47 +0100490 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welte78db2442020-12-05 00:31:07 +0100491 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte993d3f42020-12-05 10:14:49 +0100492 hash_for_each(nse->bvcs, j, bvc, list) {
Harald Welte8cd74402020-12-04 22:24:47 +0100493 if (!memcmp(bvc->ra, TLVP_VAL(tp, BSSGP_IE_LOCATION_AREA), 5)) {
Daniel Willmann6e8ed272020-12-02 13:54:21 +0100494 LOGPNSE(nse, LOGL_INFO, "routing to NSE (LAI match)\n");
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100495 gbprox_relay2nse(msg, nse, ns_bvci);
496 n_nses++;
497 /* Only send it once to each NSE */
498 break;
Daniel Willmann447ad442020-11-26 18:19:21 +0100499 }
Harald Welte3375fa42020-11-23 15:14:20 +0100500 }
501 }
Harald Welte6c4c6f02020-12-03 15:36:59 +0100502 } else if (TLVP_PRES_LEN(tp, BSSGP_IE_BSS_AREA_ID, 1)) {
Harald Welte8cd74402020-12-04 22:24:47 +0100503 /* iterate over all bvcs and dispatch the paging to each matching one */
Harald Welte78db2442020-12-05 00:31:07 +0100504 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte993d3f42020-12-05 10:14:49 +0100505 hash_for_each(nse->bvcs, j, bvc, list) {
Daniel Willmann6e8ed272020-12-02 13:54:21 +0100506 LOGPNSE(nse, LOGL_INFO, "routing to NSE (broadcast)\n");
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100507 gbprox_relay2nse(msg, nse, ns_bvci);
508 n_nses++;
509 /* Only send it once to each NSE */
510 break;
Daniel Willmann447ad442020-11-26 18:19:21 +0100511 }
Harald Welte8553f552020-11-24 11:31:13 +0100512 }
513 } else {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100514 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: "
Harald Welte8553f552020-11-24 11:31:13 +0100515 "unable to route, missing IE\n", nsei);
516 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
517 }
Harald Welte4cf12e92010-05-13 14:14:56 +0200518
Daniel Willmann5937dfd2020-11-30 17:08:58 +0100519 if (n_nses == 0) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100520 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BSSGP PAGING: "
Harald Welte8553f552020-11-24 11:31:13 +0100521 "unable to route, no destination found\n", nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200522 rate_ctr_inc(&cfg->ctrg->ctr[errctr]);
Harald Welte9f75c352010-04-30 20:26:32 +0200523 return -EINVAL;
Harald Welte4cf12e92010-05-13 14:14:56 +0200524 }
Harald Welte3375fa42020-11-23 15:14:20 +0100525 return 0;
Harald Welte9f75c352010-04-30 20:26:32 +0200526}
527
Harald Welte0a4050c2010-05-11 10:01:17 +0200528/* Receive an incoming BVC-RESET message from the SGSN */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200529static int rx_reset_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200530 struct msgb *orig_msg,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200531 struct msgb *msg, struct tlv_parsed *tp,
532 uint32_t nsei, uint16_t ns_bvci)
Harald Welte0a4050c2010-05-11 10:01:17 +0200533{
Daniel Willmann447ad442020-11-26 18:19:21 +0100534 struct gbproxy_nse *nse;
Harald Welte8cd74402020-12-04 22:24:47 +0100535 struct gbproxy_bvc *bvc;
Harald Welte0a4050c2010-05-11 10:01:17 +0200536 uint16_t ptp_bvci;
Harald Welte993d3f42020-12-05 10:14:49 +0100537 int i, j;
Harald Welte0a4050c2010-05-11 10:01:17 +0200538
Harald Welte6c4c6f02020-12-03 15:36:59 +0100539 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200540 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200541 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200542 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200543 NULL, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +0200544 }
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200545 ptp_bvci = ntohs(tlvp_val16_unal(tp, BSSGP_IE_BVCI));
Harald Welte0a4050c2010-05-11 10:01:17 +0200546
547 if (ptp_bvci >= 2) {
548 /* A reset for a PTP BVC was received, forward it to its
Harald Welte8cd74402020-12-04 22:24:47 +0100549 * respective bvc */
550 bvc = gbproxy_bvc_by_bvci(cfg, ptp_bvci);
551 if (!bvc) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100552 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) BVCI=%05u: Cannot find BSS\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200553 nsei, ptp_bvci);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200554 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200555 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte0a4050c2010-05-11 10:01:17 +0200556 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI,
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200557 &ptp_bvci, orig_msg);
Harald Welte0a4050c2010-05-11 10:01:17 +0200558 }
Harald Welte8cd74402020-12-04 22:24:47 +0100559 return gbprox_relay2peer(msg, bvc, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200560 }
561
562 /* A reset for the Signalling entity has been received
563 * from the SGSN. As the signalling BVCI is shared
564 * among all the BSS's that we multiplex, it needs to
565 * be relayed */
Harald Welte78db2442020-12-05 00:31:07 +0100566 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Welte993d3f42020-12-05 10:14:49 +0100567 hash_for_each(nse->bvcs, j, bvc, list)
Harald Welte8cd74402020-12-04 22:24:47 +0100568 gbprox_relay2peer(msg, bvc, ns_bvci);
Daniel Willmann447ad442020-11-26 18:19:21 +0100569 }
Harald Welte0a4050c2010-05-11 10:01:17 +0200570
571 return 0;
572}
573
Harald Welte9f75c352010-04-30 20:26:32 +0200574/* Receive an incoming signalling message from the SGSN-side NS-VC */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200575static int gbprox_rx_sig_from_sgsn(struct gbproxy_config *cfg,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200576 struct msgb *orig_msg, uint32_t nsei,
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200577 uint16_t ns_bvci)
Harald Welte9f75c352010-04-30 20:26:32 +0200578{
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200579 struct bssgp_normal_hdr *bgph =
580 (struct bssgp_normal_hdr *) msgb_bssgph(orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200581 struct tlv_parsed tp;
582 uint8_t pdu_type = bgph->pdu_type;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200583 int data_len;
Harald Weltec8d98ac2020-12-02 20:06:04 +0100584 struct gbproxy_nse *nse;
Harald Welte8cd74402020-12-04 22:24:47 +0100585 struct gbproxy_bvc *bvc;
Harald Welte9f75c352010-04-30 20:26:32 +0200586 uint16_t bvci;
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200587 struct msgb *msg;
Harald Welte9f75c352010-04-30 20:26:32 +0200588 int rc = 0;
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200589 int cause;
Harald Welte78db2442020-12-05 00:31:07 +0100590 int i;
Harald Welte9f75c352010-04-30 20:26:32 +0200591
Harald Weltec471d3d2011-02-06 17:13:12 +0100592 if (ns_bvci != 0 && ns_bvci != 1) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100593 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BVCI=%05u is not "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200594 "signalling\n", nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200595 /* FIXME: Send proper error message */
Harald Welte9f75c352010-04-30 20:26:32 +0200596 return -EINVAL;
597 }
598
599 /* we actually should never see those two for BVCI == 0, but double-check
600 * just to make sure */
601 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
602 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100603 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) UNITDATA not allowed in "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200604 "signalling\n", nsei);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200605 return bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200606 }
607
Pau Espin Pedrol816a67d2018-08-15 13:53:48 +0200608 msg = bssgp_msgb_copy(orig_msg, "rx_sig_from_sgsn");
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200609 /* Update message info */
610 bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
611 data_len = msgb_bssgp_len(orig_msg) - sizeof(*bgph);
Harald Welte9f75c352010-04-30 20:26:32 +0200612 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
613
614 switch (pdu_type) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200615 case BSSGP_PDUT_BVC_RESET:
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200616 rc = rx_reset_from_sgsn(cfg, msg, orig_msg, &tp, nsei, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200617 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200618 case BSSGP_PDUT_BVC_RESET_ACK:
Daniel Willmann15c9da22020-11-03 21:12:42 +0100619 /* simple case: BVCI IE is mandatory */
Harald Welte6c4c6f02020-12-03 15:36:59 +0100620 if (!TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Daniel Willmann15c9da22020-11-03 21:12:42 +0100621 goto err_mand_ie;
622 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
623 if (bvci == BVCI_SIGNALLING) {
624 /* TODO: Reset all PTP BVCIs */
625 } else {
626 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
627 }
628 break;
Jacob Erlbeckc1c57d32014-08-28 13:20:39 +0200629 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9f75c352010-04-30 20:26:32 +0200630 /* simple case: BVCI IE is mandatory */
Harald Welte6c4c6f02020-12-03 15:36:59 +0100631 if (!TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Harald Welte9f75c352010-04-30 20:26:32 +0200632 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200633 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200634 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200635 break;
636 case BSSGP_PDUT_PAGING_PS:
637 case BSSGP_PDUT_PAGING_CS:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200638 /* process the paging request (LAI/RAI lookup) */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200639 rc = gbprox_rx_paging(cfg, msg, &tp, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200640 break;
641 case BSSGP_PDUT_STATUS:
Harald Welte0a4050c2010-05-11 10:01:17 +0200642 /* Some exception has occurred */
Harald Welte44c48302010-05-03 19:22:32 +0200643 LOGP(DGPRS, LOGL_NOTICE,
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100644 "NSE(%05u/SGSN) BSSGP STATUS ", nsei);
Harald Welte6c4c6f02020-12-03 15:36:59 +0100645 if (!TLVP_PRES_LEN(&tp, BSSGP_IE_CAUSE, 1)) {
Harald Welte0a4050c2010-05-11 10:01:17 +0200646 LOGPC(DGPRS, LOGL_NOTICE, "\n");
647 goto err_mand_ie;
648 }
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200649 cause = *TLVP_VAL(&tp, BSSGP_IE_CAUSE);
Harald Welte0a4050c2010-05-11 10:01:17 +0200650 LOGPC(DGPRS, LOGL_NOTICE,
651 "cause=0x%02x(%s) ", *TLVP_VAL(&tp, BSSGP_IE_CAUSE),
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200652 bssgp_cause_str(cause));
Harald Welte6c4c6f02020-12-03 15:36:59 +0100653 if (TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2)) {
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200654 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100655 LOGPC(DGPRS, LOGL_NOTICE, "BVCI=%05u\n", bvci);
Jacob Erlbeck1e65b0e2014-08-29 13:32:17 +0200656
657 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI)
658 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte0a4050c2010-05-11 10:01:17 +0200659 } else
660 LOGPC(DGPRS, LOGL_NOTICE, "\n");
Harald Welte9f75c352010-04-30 20:26:32 +0200661 break;
662 /* those only exist in the SGSN -> BSS direction */
663 case BSSGP_PDUT_SUSPEND_ACK:
664 case BSSGP_PDUT_SUSPEND_NACK:
665 case BSSGP_PDUT_RESUME_ACK:
666 case BSSGP_PDUT_RESUME_NACK:
Jacob Erlbeck8f503592014-06-02 10:49:00 +0200667 /* RAI IE is mandatory */
Harald Welte6c4c6f02020-12-03 15:36:59 +0100668 if (!TLVP_PRES_LEN(&tp, BSSGP_IE_ROUTEING_AREA, 6))
Harald Welte9f75c352010-04-30 20:26:32 +0200669 goto err_mand_ie;
Harald Welte8cd74402020-12-04 22:24:47 +0100670 bvc = gbproxy_bvc_by_rai(cfg, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA));
671 if (!bvc)
672 goto err_no_bvc;
673 rc = gbprox_relay2peer(msg, bvc, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200674 break;
Harald Welte36f98d92011-02-06 13:09:29 +0100675 case BSSGP_PDUT_BVC_BLOCK_ACK:
676 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte6c4c6f02020-12-03 15:36:59 +0100677 if (!TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Harald Welte36f98d92011-02-06 13:09:29 +0100678 goto err_mand_ie;
Holger Hans Peter Freytherffd68562012-09-04 20:42:20 +0200679 bvci = ntohs(tlvp_val16_unal(&tp, BSSGP_IE_BVCI));
Harald Welte36f98d92011-02-06 13:09:29 +0100680 if (bvci == 0) {
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100681 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP "
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200682 "%sBLOCK_ACK for signalling BVCI ?!?\n", nsei,
Harald Welte36f98d92011-02-06 13:09:29 +0100683 pdu_type == BSSGP_PDUT_BVC_UNBLOCK_ACK ? "UN":"");
Daniel Willmann6626bbc2020-12-02 17:46:56 +0100684 /* TODO: should we send STATUS ? */
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200685 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200686 ctr[GBPROX_GLOB_CTR_INV_BVCI]);
Harald Welte36f98d92011-02-06 13:09:29 +0100687 } else {
688 /* Mark BVC as (un)blocked */
Harald Welte8cd74402020-12-04 22:24:47 +0100689 block_unblock_bvc(cfg, bvci, pdu_type);
Harald Welte36f98d92011-02-06 13:09:29 +0100690 }
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200691 rc = gbprox_relay2bvci(cfg, msg, bvci, ns_bvci);
Harald Welte36f98d92011-02-06 13:09:29 +0100692 break;
Harald Welte9f75c352010-04-30 20:26:32 +0200693 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltec8d98ac2020-12-02 20:06:04 +0100694 case BSSGP_PDUT_OVERLOAD:
695 LOGP(DGPRS, LOGL_DEBUG,
696 "NSE(%05u/SGSN) BSSGP %s: broadcasting\n", nsei, bssgp_pdu_str(pdu_type));
Harald Welte8cd74402020-12-04 22:24:47 +0100697 /* broadcast to all BSS-side bvcs */
Harald Welte78db2442020-12-05 00:31:07 +0100698 hash_for_each(cfg->bss_nses, i, nse, list) {
Harald Weltec8d98ac2020-12-02 20:06:04 +0100699 gbprox_relay2nse(msg, nse, 0);
700 }
Harald Welte9f75c352010-04-30 20:26:32 +0200701 break;
702 default:
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100703 LOGP(DGPRS, LOGL_NOTICE, "NSE(%05u/SGSN) BSSGP PDU type %s not supported\n", nsei,
704 bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200705 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200706 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200707 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200708 break;
709 }
710
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200711 msgb_free(msg);
712
Harald Welte9f75c352010-04-30 20:26:32 +0200713 return rc;
714err_mand_ie:
Daniel Willmann103a7ec2020-12-02 16:08:02 +0100715 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) missing mandatory IE\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200716 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200717 rate_ctr_inc(&cfg->ctrg->
Jacob Erlbeckbc555742013-10-18 14:34:55 +0200718 ctr[GBPROX_GLOB_CTR_PROTO_ERR_SGSN]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200719 msgb_free(msg);
720 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, orig_msg);
Harald Welte8cd74402020-12-04 22:24:47 +0100721err_no_bvc:
722 LOGP(DGPRS, LOGL_ERROR, "NSE(%05u/SGSN) cannot find bvc based on RAI\n",
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200723 nsei);
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200724 rate_ctr_inc(&cfg->ctrg-> ctr[GBPROX_GLOB_CTR_INV_RAI]);
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200725 msgb_free(msg);
Jacob Erlbeck17b42b82014-08-29 12:20:15 +0200726 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, orig_msg);
Harald Welte9f75c352010-04-30 20:26:32 +0200727}
728
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200729static int gbproxy_is_sgsn_nsei(struct gbproxy_config *cfg, uint16_t nsei)
730{
Harald Welte91bb7202020-12-05 11:18:23 +0100731 return nsei == cfg->nsip_sgsn_nsei;
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200732}
733
Alexander Couzens82182d02020-09-22 13:21:46 +0200734int gbprox_bssgp_send_cb(void *ctx, struct msgb *msg)
Harald Welte9f75c352010-04-30 20:26:32 +0200735{
Harald Welte672f5c42010-05-03 18:54:58 +0200736 int rc;
Alexander Couzens82182d02020-09-22 13:21:46 +0200737 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
738 struct gprs_ns2_inst *nsi = cfg->nsi;
739 struct osmo_gprs_ns2_prim nsp = {};
740
741 nsp.bvci = msgb_bvci(msg);
742 nsp.nsei = msgb_nsei(msg);
743
744 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_UNIT_DATA, PRIM_OP_REQUEST, msg);
745 rc = gprs_ns2_recv_prim(nsi, &nsp.oph);
746
747 return rc;
748}
749
750/* Main input function for Gb proxy */
751int gbprox_rcvmsg(void *ctx, struct msgb *msg)
752{
753 int rc;
754 uint16_t nsei = msgb_nsei(msg);
755 uint16_t ns_bvci = msgb_bvci(msg);
756 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
757
Jacob Erlbeckf4d60c82014-08-26 14:47:15 +0200758 int remote_end_is_sgsn = gbproxy_is_sgsn_nsei(cfg, nsei);
Harald Welte9f75c352010-04-30 20:26:32 +0200759
760 /* Only BVCI=0 messages need special treatment */
761 if (ns_bvci == 0 || ns_bvci == 1) {
Jacob Erlbecke75fec62013-10-15 12:00:27 +0200762 if (remote_end_is_sgsn)
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200763 rc = gbprox_rx_sig_from_sgsn(cfg, msg, nsei, ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200764 else
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200765 rc = gbprox_rx_sig_from_bss(cfg, msg, nsei, ns_bvci);
Harald Welte672f5c42010-05-03 18:54:58 +0200766 } else {
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200767 /* All other BVCI are PTP */
768 if (remote_end_is_sgsn)
Alexander Couzens82182d02020-09-22 13:21:46 +0200769 rc = gbprox_rx_ptp_from_sgsn(cfg, msg, nsei,
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200770 ns_bvci);
771 else
Alexander Couzens82182d02020-09-22 13:21:46 +0200772 rc = gbprox_rx_ptp_from_bss(cfg, msg, nsei,
Jacob Erlbeck46f1d6f2014-08-21 13:45:04 +0200773 ns_bvci);
Harald Welte9f75c352010-04-30 20:26:32 +0200774 }
775
Harald Welte672f5c42010-05-03 18:54:58 +0200776 return rc;
Harald Welte9f75c352010-04-30 20:26:32 +0200777}
Harald Welte85801d02010-05-11 05:49:43 +0200778
Alexander Couzens82182d02020-09-22 13:21:46 +0200779/* TODO: What about handling:
780 * NS_AFF_CAUSE_VC_FAILURE,
781 NS_AFF_CAUSE_VC_RECOVERY,
782 NS_AFF_CAUSE_FAILURE,
783 NS_AFF_CAUSE_RECOVERY,
784 osmocom own causes
785 NS_AFF_CAUSE_SNS_CONFIGURED,
786 NS_AFF_CAUSE_SNS_FAILURE,
787 */
Harald Welte1ccbf442010-05-14 11:53:08 +0000788
Alexander Couzens82182d02020-09-22 13:21:46 +0200789void gprs_ns_prim_status_cb(struct gbproxy_config *cfg, struct osmo_gprs_ns2_prim *nsp)
Harald Weltec1c1dd22010-05-11 06:34:24 +0200790{
Harald Welte8cd74402020-12-04 22:24:47 +0100791 /* 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 +0200792 * TODO: sgsn nsei available/unavailable
793 */
Harald Welte8cd74402020-12-04 22:24:47 +0100794 struct gbproxy_bvc *bvc;
Harald Weltec1c1dd22010-05-11 06:34:24 +0200795
Alexander Couzens82182d02020-09-22 13:21:46 +0200796 switch (nsp->u.status.cause) {
797 case NS_AFF_CAUSE_SNS_FAILURE:
798 case NS_AFF_CAUSE_SNS_CONFIGURED:
799 break;
Harald Weltec1c1dd22010-05-11 06:34:24 +0200800
Alexander Couzens82182d02020-09-22 13:21:46 +0200801 case NS_AFF_CAUSE_RECOVERY:
802 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
803 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
804 /* look-up or create the BTS context for this BVC */
805 struct bssgp_bvc_ctx *bctx = btsctx_by_bvci_nsei(nsp->bvci, nsp->nsei);
806 if (!bctx)
807 bctx = btsctx_alloc(nsp->bvci, nsp->nsei);
Harald Weltef69c0592010-05-11 18:29:44 +0200808
Alexander Couzens82182d02020-09-22 13:21:46 +0200809 bssgp_tx_bvc_reset_nsei_bvci(cfg->nsip_sgsn_nsei, 0, BSSGP_CAUSE_OML_INTERV, NULL, 0);
Harald Welte5e106d72011-02-06 16:33:29 +0100810 }
Alexander Couzens82182d02020-09-22 13:21:46 +0200811 break;
812 case NS_AFF_CAUSE_FAILURE:
813 if (nsp->nsei == cfg->nsip_sgsn_nsei) {
814 /* sgsn */
815 /* TODO: BSVC: block all PtP towards bss */
816 rate_ctr_inc(&cfg->ctrg->
817 ctr[GBPROX_GLOB_CTR_RESTART_RESET_SGSN]);
818 } else {
Daniel Willmann447ad442020-11-26 18:19:21 +0100819 /* bss became unavailable
820 * TODO: Block all BVC belonging to that NSE */
Harald Welte8cd74402020-12-04 22:24:47 +0100821 bvc = gbproxy_bvc_by_nsei(cfg, nsp->nsei);
822 if (!bvc) {
Alexander Couzens82182d02020-09-22 13:21:46 +0200823 /* TODO: use primitive name + status cause name */
Harald Welte8cd74402020-12-04 22:24:47 +0100824 LOGP(DGPRS, LOGL_NOTICE, "Received ns2 primitive %d for unknown bvc NSEI=%u\n",
Alexander Couzens82182d02020-09-22 13:21:46 +0200825 nsp->u.status.cause, nsp->nsei);
Harald Welte5e106d72011-02-06 16:33:29 +0100826 break;
827 }
Alexander Couzens82182d02020-09-22 13:21:46 +0200828
Harald Welte8cd74402020-12-04 22:24:47 +0100829 if (!bvc->blocked)
Alexander Couzens82182d02020-09-22 13:21:46 +0200830 break;
831 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK, cfg->nsip_sgsn_nsei,
Harald Welte8cd74402020-12-04 22:24:47 +0100832 bvc->bvci, 0);
Harald Weltec1c1dd22010-05-11 06:34:24 +0200833 }
Alexander Couzens82182d02020-09-22 13:21:46 +0200834 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
835 break;
836 default:
Harald Welted97ff682020-11-30 10:55:22 +0100837 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown NS-STATUS.ind cause=%s from NS\n",
838 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause));
Alexander Couzens82182d02020-09-22 13:21:46 +0200839 break;
Harald Weltec1c1dd22010-05-11 06:34:24 +0200840 }
Alexander Couzens82182d02020-09-22 13:21:46 +0200841}
842
Alexander Couzens82182d02020-09-22 13:21:46 +0200843/* called by the ns layer */
844int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
845{
846 struct osmo_gprs_ns2_prim *nsp;
847 struct gbproxy_config *cfg = (struct gbproxy_config *) ctx;
Daniel Willmanne8c8ec92020-12-02 19:33:50 +0100848 uintptr_t bvci;
Alexander Couzens82182d02020-09-22 13:21:46 +0200849 int rc = 0;
850
851 if (oph->sap != SAP_NS)
852 return 0;
853
854 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
855
856 if (oph->operation != PRIM_OP_INDICATION) {
Harald Welted97ff682020-11-30 10:55:22 +0100857 LOGP(DPCU, LOGL_NOTICE, "NS: Unexpected primitive operation %s from NS\n",
858 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens82182d02020-09-22 13:21:46 +0200859 return 0;
860 }
861
862 switch (oph->primitive) {
863 case PRIM_NS_UNIT_DATA:
Daniel Willmanne8c8ec92020-12-02 19:33:50 +0100864
Alexander Couzens82182d02020-09-22 13:21:46 +0200865 /* hand the message into the BSSGP implementation */
866 msgb_bssgph(oph->msg) = oph->msg->l3h;
867 msgb_bvci(oph->msg) = nsp->bvci;
868 msgb_nsei(oph->msg) = nsp->nsei;
Daniel Willmanne8c8ec92020-12-02 19:33:50 +0100869 bvci = nsp->bvci | BVC_LOG_CTX_FLAG;
Alexander Couzens82182d02020-09-22 13:21:46 +0200870
Daniel Willmanne8c8ec92020-12-02 19:33:50 +0100871 log_set_context(LOG_CTX_GB_BVC, (void *)bvci);
Alexander Couzens82182d02020-09-22 13:21:46 +0200872 rc = gbprox_rcvmsg(cfg, oph->msg);
Daniel Willmann02f2c342020-11-04 17:32:56 +0100873 msgb_free(oph->msg);
Alexander Couzens82182d02020-09-22 13:21:46 +0200874 break;
875 case PRIM_NS_STATUS:
876 gprs_ns_prim_status_cb(cfg, nsp);
877 break;
878 default:
Harald Welted97ff682020-11-30 10:55:22 +0100879 LOGP(DPCU, LOGL_NOTICE, "NS: Unknown prim %s %s from NS\n",
880 gprs_ns2_prim_str(oph->primitive),
881 get_value_string(osmo_prim_op_names, oph->operation));
Alexander Couzens82182d02020-09-22 13:21:46 +0200882 break;
883 }
884
885 return rc;
Harald Weltec1c1dd22010-05-11 06:34:24 +0200886}
887
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200888void gbprox_reset(struct gbproxy_config *cfg)
Jacob Erlbeck72b401f2013-10-24 12:48:55 +0200889{
Harald Welte78db2442020-12-05 00:31:07 +0100890 struct gbproxy_nse *nse;
891 struct hlist_node *ntmp;
Harald Welte993d3f42020-12-05 10:14:49 +0100892 int i, j;
Jacob Erlbeck72b401f2013-10-24 12:48:55 +0200893
Harald Welte78db2442020-12-05 00:31:07 +0100894 hash_for_each_safe(cfg->bss_nses, i, ntmp, nse, list) {
Harald Welte993d3f42020-12-05 10:14:49 +0100895 struct gbproxy_bvc *bvc;
896 struct hlist_node *tmp;
897 hash_for_each_safe(nse->bvcs, j, tmp, bvc, list)
Harald Welte8cd74402020-12-04 22:24:47 +0100898 gbproxy_bvc_free(bvc);
Daniel Willmann447ad442020-11-26 18:19:21 +0100899
900 gbproxy_nse_free(nse);
901 }
Jacob Erlbeck72b401f2013-10-24 12:48:55 +0200902
Holger Hans Peter Freythereece6272014-08-04 15:42:36 +0200903 rate_ctr_group_free(cfg->ctrg);
904 gbproxy_init_config(cfg);
Jacob Erlbeck72b401f2013-10-24 12:48:55 +0200905}
906
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200907int gbproxy_init_config(struct gbproxy_config *cfg)
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200908{
Jacob Erlbeck0d376712014-08-11 19:12:24 +0200909 struct timespec tp;
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200910
Harald Welte78db2442020-12-05 00:31:07 +0100911 hash_init(cfg->bss_nses);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100912 cfg->ctrg = rate_ctr_group_alloc(tall_sgsn_ctx, &global_ctrg_desc, 0);
Harald Welte26c14652017-07-12 00:25:51 +0200913 if (!cfg->ctrg) {
914 LOGP(DGPRS, LOGL_ERROR, "Cannot allocate global counter group!\n");
915 return -1;
916 }
Pau Espin Pedrol36abead2018-08-17 13:27:20 +0200917 osmo_clock_gettime(CLOCK_REALTIME, &tp);
Jacob Erlbeck9a83d7a2014-09-25 11:17:31 +0200918
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200919 return 0;
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200920}