blob: 8b8d534e4546a98241506f3c416b5589c898ca1c [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_bssgp.c
2 * GPRS BSSGP protocol implementation as per 3GPP TS 08.18. */
3/*
4 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte9ba50052010-03-14 15:45:01 +080010 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080013 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080019 *
Harald Welte7fa89c22014-10-26 20:33:09 +010020 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080022 *
Harald Welte4e5721d2010-05-17 23:41:43 +020023 * TODO:
24 * o properly count incoming BVC-RESET packets in counter group
25 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080026 */
27
28#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020029#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080030
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010031#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020032#include <osmocom/core/byteswap.h>
33#include <osmocom/core/bit16gen.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010034#include <osmocom/gsm/tlv.h>
35#include <osmocom/core/talloc.h>
36#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010037#include <osmocom/core/stats.h>
Harald Welte6752fa42010-05-02 09:23:16 +020038
Harald Welte73952e32012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_bssgp.h>
Max8b8938f2017-06-29 19:48:29 +020040#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte73952e32012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_ns.h>
42
Harald Weltecca49632012-06-16 17:45:59 +080043#include "common_vty.h"
44
Harald Welte6752fa42010-05-02 09:23:16 +020045void *bssgp_tall_ctx = NULL;
46
Alexander Couzens85a8fd32020-07-18 15:57:07 +020047static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg);
48
49bssgp_bvc_send bssgp_ns_send = _gprs_ns_sendmsg;
50void *bssgp_ns_send_data = NULL;
51
Harald Welte25de8112010-05-13 21:26:28 +020052static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080053 { "packets:in", "Packets at BSSGP Level ( In)" },
54 { "packets:out","Packets at BSSGP Level (Out)" },
55 { "bytes:in", "Bytes at BSSGP Level ( In)" },
56 { "bytes:out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020057 { "blocked", "BVC Blocking count" },
58 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010059 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020060};
61
62static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080063 .group_name_prefix = "bssgp:bss_ctx",
Harald Welte25de8112010-05-13 21:26:28 +020064 .group_description = "BSSGP Peer Statistics",
65 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
66 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010067 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020068};
69
Harald Weltea78b9c22010-05-17 23:02:42 +020070LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020071
Harald Welted11c0592012-09-06 21:57:11 +020072static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
73 uint32_t llc_pdu_len, void *priv);
74
Alexander Couzens85a8fd32020-07-18 15:57:07 +020075
Alexander Couzens83fb6862020-09-03 19:30:08 +020076/* callback to be backward compatible with old users which do not set the bssgp_ns_send function */
Alexander Couzens85a8fd32020-07-18 15:57:07 +020077static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg)
78{
79 return gprs_ns_sendmsg(bssgp_nsi, msg);
80}
81
Harald Welte6752fa42010-05-02 09:23:16 +020082/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020083struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020084{
Harald Welte8a521132010-05-17 22:59:29 +020085 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020086
Harald Weltea78b9c22010-05-17 23:02:42 +020087 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020088 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
89 bctx->cell_id == cid)
90 return bctx;
91 }
92 return NULL;
93}
94
Max8b8938f2017-06-29 19:48:29 +020095/*! Initiate reset procedure for all PTP BVC on a given NSEI.
96 *
97 * This function initiates reset procedure for all PTP BVC with a given cause.
98 * \param[in] nsei NSEI to which PTP BVC should belong to
99 * \param[in] cause Cause of BVC RESET
100 * \returns 0 on success, negative error code otherwise
101 */
102int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
103{
104 int rc;
105 struct bssgp_bvc_ctx *bctx;
106
107 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
108 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
109 LOGP(DBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
110 nsei, bctx->bvci, bssgp_cause_str(cause));
111 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
112 if (rc < 0)
113 return rc;
114 }
115 }
116
117 return 0;
118}
119
Harald Welte6752fa42010-05-02 09:23:16 +0200120/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200121struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200122{
Harald Welte8a521132010-05-17 22:59:29 +0200123 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200124
Harald Weltea78b9c22010-05-17 23:02:42 +0200125 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200126 if (bctx->nsei == nsei && bctx->bvci == bvci)
127 return bctx;
128 }
129 return NULL;
130}
131
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200132void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data)
133{
134 bssgp_ns_send = ns_send;
135 bssgp_ns_send_data = data;
136}
137
Harald Welte8a521132010-05-17 22:59:29 +0200138struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200139{
Harald Welte8a521132010-05-17 22:59:29 +0200140 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200141
Harald Welte8a521132010-05-17 22:59:29 +0200142 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200143 if (!ctx)
144 return NULL;
145 ctx->bvci = bvci;
146 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200147 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
148 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200149 if (!ctx->ctrg)
150 goto err_ctrg;
151
Harald Welted8b47692012-09-07 11:29:32 +0200152 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200153 if (!ctx->fc)
154 goto err_fc;
155
Harald Welted8b47692012-09-07 11:29:32 +0200156 /* cofigure for 2Mbit, 30 packets in queue */
157 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200158
Harald Weltea78b9c22010-05-17 23:02:42 +0200159 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200160
161 return ctx;
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200162
163err_fc:
164 rate_ctr_group_free(ctx->ctrg);
165err_ctrg:
166 talloc_free(ctx);
167 return NULL;
Harald Welte6752fa42010-05-02 09:23:16 +0200168}
169
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700170void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx)
171{
172 if (!ctx)
173 return;
Alexander Couzens495b4a72020-09-16 23:10:03 +0200174
175 osmo_timer_del(&ctx->fc->timer);
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700176 rate_ctr_group_free(ctx->ctrg);
177 llist_del(&ctx->list);
178 talloc_free(ctx);
179}
180
Harald Welte9ba50052010-03-14 15:45:01 +0800181/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200182static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800183{
184 struct msgb *msg = bssgp_msgb_alloc();
185 struct bssgp_normal_hdr *bgph =
186 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
187
Harald Welte24a655f2010-04-30 19:54:29 +0200188 msgb_nsei(msg) = nsei;
189 msgb_bvci(msg) = ns_bvci;
190
Harald Welte9ba50052010-03-14 15:45:01 +0800191 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
192 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
193
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200194 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800195}
196
Harald Weltea8aa4df2010-05-30 22:00:53 +0200197/* 10.3.7 SUSPEND-ACK PDU */
198int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
199 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
200{
201 struct msgb *msg = bssgp_msgb_alloc();
202 struct bssgp_normal_hdr *bgph =
203 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200204
205 msgb_nsei(msg) = nsei;
206 msgb_bvci(msg) = 0; /* Signalling */
207 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
208
Maxe29ec852018-01-05 14:30:22 +0100209 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100210 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200211 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
212
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200213 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200214}
215
216/* 10.3.8 SUSPEND-NACK PDU */
217int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100218 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200219 uint8_t *cause)
220{
221 struct msgb *msg = bssgp_msgb_alloc();
222 struct bssgp_normal_hdr *bgph =
223 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200224
225 msgb_nsei(msg) = nsei;
226 msgb_bvci(msg) = 0; /* Signalling */
227 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
228
Maxe29ec852018-01-05 14:30:22 +0100229 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100230 bssgp_msgb_ra_put(msg, ra_id);
231
Harald Weltea8aa4df2010-05-30 22:00:53 +0200232 if (cause)
233 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
234
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200235 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200236}
237
238/* 10.3.10 RESUME-ACK PDU */
239int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
240 const struct gprs_ra_id *ra_id)
241{
242 struct msgb *msg = bssgp_msgb_alloc();
243 struct bssgp_normal_hdr *bgph =
244 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200245
246 msgb_nsei(msg) = nsei;
247 msgb_bvci(msg) = 0; /* Signalling */
248 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
249
Maxe29ec852018-01-05 14:30:22 +0100250 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100251 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200252
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200253 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200254}
255
256/* 10.3.11 RESUME-NACK PDU */
257int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
258 const struct gprs_ra_id *ra_id, uint8_t *cause)
259{
260 struct msgb *msg = bssgp_msgb_alloc();
261 struct bssgp_normal_hdr *bgph =
262 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200263
264 msgb_nsei(msg) = nsei;
265 msgb_bvci(msg) = 0; /* Signalling */
266 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
267
Maxe29ec852018-01-05 14:30:22 +0100268 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100269 bssgp_msgb_ra_put(msg, ra_id);
270
Harald Weltea8aa4df2010-05-30 22:00:53 +0200271 if (cause)
272 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
273
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200274 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200275}
276
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200277uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200278{
279 /* 6 octets RAC */
280 gsm48_parse_ra(raid, buf);
281 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200282 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200283}
284
Harald Welte28610072011-11-24 21:32:07 +0100285int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
286 uint16_t cid)
287{
Harald Welte28610072011-11-24 21:32:07 +0100288 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100289 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100290 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200291 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100292
293 return 8;
294}
295
Harald Welte3fddf3c2010-05-01 16:48:27 +0200296/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200297static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200298 uint16_t ns_bvci)
299{
Harald Welte15a36432012-06-17 12:16:31 +0800300 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200301 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200302 uint16_t nsei = msgb_nsei(msg);
303 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200304
Harald Weltebfe62e52017-05-15 12:48:30 +0200305 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200306 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200307 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
308
Harald Welte6752fa42010-05-02 09:23:16 +0200309 /* look-up or create the BTS context for this BVC */
310 bctx = btsctx_by_bvci_nsei(bvci, nsei);
311 if (!bctx)
312 bctx = btsctx_alloc(bvci, nsei);
313
Harald Welte25de8112010-05-13 21:26:28 +0200314 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
315 bctx->state &= ~BVC_S_BLOCKED;
316
Harald Welte3fddf3c2010-05-01 16:48:27 +0200317 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
318 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200319 if (bvci != 0 && bvci != 1) {
320 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200321 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200322 "missing mandatory IE\n", bvci);
323 return -EINVAL;
324 }
325 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200326 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
327 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100328 LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
329 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200330 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200331
Harald Welte15a36432012-06-17 12:16:31 +0800332 /* Send NM_BVC_RESET.ind to NM */
333 memset(&nmp, 0, sizeof(nmp));
334 nmp.nsei = nsei;
335 nmp.bvci = bvci;
336 nmp.tp = tp;
337 nmp.ra_id = &bctx->ra_id;
338 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
339 PRIM_OP_INDICATION, msg);
340 bssgp_prim_cb(&nmp.oph, NULL);
341
Harald Welte6752fa42010-05-02 09:23:16 +0200342 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200343 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
344 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200345 return 0;
346}
347
Harald Welte25de8112010-05-13 21:26:28 +0200348static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
349{
Harald Welte15a36432012-06-17 12:16:31 +0800350 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100351 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200352 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200353
Harald Weltebfe62e52017-05-15 12:48:30 +0200354 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200355 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200356 /* 8.3.2: Signalling BVC shall never be blocked */
357 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
358 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100359 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200360 return 0;
361 }
Harald Welte25de8112010-05-13 21:26:28 +0200362
Harald Welte086fe322011-08-19 16:45:19 +0200363 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200364
Max548caef2019-03-07 13:49:34 +0100365 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200366 if (!ptp_ctx)
367 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
368
369 ptp_ctx->state |= BVC_S_BLOCKED;
370 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
371
Harald Welte15a36432012-06-17 12:16:31 +0800372 /* Send NM_BVC_BLOCK.ind to NM */
373 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100374 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800375 nmp.bvci = bvci;
376 nmp.tp = tp;
377 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
378 PRIM_OP_INDICATION, msg);
379 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200380
381 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100382 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200383 bvci, msgb_bvci(msg));
384};
385
386static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
387{
Harald Welte15a36432012-06-17 12:16:31 +0800388 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100389 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200390 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200391
Harald Weltebfe62e52017-05-15 12:48:30 +0200392 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200393 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200394 /* 8.3.2: Signalling BVC shall never be blocked */
395 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
396 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100397 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200398 return 0;
399 }
Harald Welte25de8112010-05-13 21:26:28 +0200400
Harald Weltee9686b62010-05-31 18:07:17 +0200401 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200402
Max548caef2019-03-07 13:49:34 +0100403 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200404 if (!ptp_ctx)
405 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
406
407 ptp_ctx->state &= ~BVC_S_BLOCKED;
408
Harald Welte15a36432012-06-17 12:16:31 +0800409 /* Send NM_BVC_UNBLOCK.ind to NM */
410 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100411 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800412 nmp.bvci = bvci;
413 nmp.tp = tp;
414 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
415 PRIM_OP_INDICATION, msg);
416 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200417
418 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100419 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200420 bvci, msgb_bvci(msg));
421};
422
Harald Welte9ba50052010-03-14 15:45:01 +0800423/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200424static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200425 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800426{
Harald Welte15a36432012-06-17 12:16:31 +0800427 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200428 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800429
Harald Welte6752fa42010-05-02 09:23:16 +0200430 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200431 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800432
Harald Welte086fe322011-08-19 16:45:19 +0200433 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200434
Harald Welte9ba50052010-03-14 15:45:01 +0800435 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200436 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200437 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
438 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
439 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200440 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200441 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200442
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200443 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800444 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
445 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800446
Harald Welte15a36432012-06-17 12:16:31 +0800447 /* Send BSSGP_UL_UD.ind to NM */
448 memset(&gbp, 0, sizeof(gbp));
449 gbp.nsei = ctx->nsei;
450 gbp.bvci = ctx->bvci;
451 gbp.tlli = msgb_tlli(msg);
452 gbp.tp = tp;
453 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
454 PRIM_OP_INDICATION, msg);
455 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800456}
457
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200458static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800459{
Harald Welte15a36432012-06-17 12:16:31 +0800460 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200461 struct gprs_ra_id raid;
462 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100463 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200464 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800465
Harald Welte25de8112010-05-13 21:26:28 +0200466 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200467 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
468 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200469 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200470 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200471 }
Harald Welte9ba50052010-03-14 15:45:01 +0800472
Harald Weltebfe62e52017-05-15 12:48:30 +0200473 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200474
Harald Welte17925322010-05-31 20:18:35 +0200475 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200476 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200477
Harald Weltea8aa4df2010-05-30 22:00:53 +0200478 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
479
Harald Welte313cccf2010-06-09 11:22:47 +0200480 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800481 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100482 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200483 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800484 gbp.tlli = tlli;
485 gbp.ra_id = &raid;
486 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
487 PRIM_OP_REQUEST, msg);
488
489 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200490 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100491 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200492
Max548caef2019-03-07 13:49:34 +0100493 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200494
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800495 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800496}
497
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200498static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800499{
Harald Welte15a36432012-06-17 12:16:31 +0800500 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200501 struct gprs_ra_id raid;
502 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200503 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100504 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200505 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800506
Harald Welte25de8112010-05-13 21:26:28 +0200507 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
508 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200509 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
510 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200511 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200512 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200513 }
Harald Welte9ba50052010-03-14 15:45:01 +0800514
Harald Weltebfe62e52017-05-15 12:48:30 +0200515 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200516 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200517
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200518 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200519
Harald Weltea8aa4df2010-05-30 22:00:53 +0200520 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
521
Harald Welte313cccf2010-06-09 11:22:47 +0200522 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800523 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100524 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200525 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800526 gbp.tlli = tlli;
527 gbp.ra_id = &raid;
528 gbp.u.resume.suspend_ref = suspend_ref;
529 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
530 PRIM_OP_REQUEST, msg);
531
532 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200533 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100534 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200535 NULL);
536
Max548caef2019-03-07 13:49:34 +0100537 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800538 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800539}
540
Harald Weltee9686b62010-05-31 18:07:17 +0200541
542static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
543 struct bssgp_bvc_ctx *ctx)
544{
Harald Welte15a36432012-06-17 12:16:31 +0800545 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200546 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100547 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200548
549 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
550 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
551 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
552 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
553 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
554 "missing mandatory IE\n", ctx->bvci);
555 }
556
Harald Welteb7363142010-07-23 21:59:29 +0200557 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200558 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200559
Harald Welte086fe322011-08-19 16:45:19 +0200560 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200561 ctx->bvci, tlli);
562
563 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
564
Harald Welte15a36432012-06-17 12:16:31 +0800565 /* send NM_LLC_DISCARDED to NM */
566 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100567 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800568 nmp.bvci = ctx->bvci;
569 nmp.tlli = tlli;
570 nmp.tp = tp;
571 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
572 PRIM_OP_INDICATION, msg);
573
574 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200575}
576
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100577int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
578 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
579{
Max548caef2019-03-07 13:49:34 +0100580 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100581 struct osmo_bssgp_prim nmp;
582 enum gprs_bssgp_cause cause;
583
584 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
585 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
586 "missing mandatory IE\n", bvci);
587 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
588 } else {
589 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
590 }
591
592 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
593 bvci, bssgp_cause_str(cause));
594
595 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
596 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
597 LOGP(DBSSGP, LOGL_ERROR,
598 "BSSGP BVCI=%u Rx STATUS cause=%s "
599 "missing conditional BVCI IE\n",
600 bvci, bssgp_cause_str(cause));
601 }
602
603 if (bctx)
604 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
605
606 /* send NM_STATUS to NM */
607 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100608 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100609 nmp.bvci = bvci;
610 nmp.tp = tp;
611 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
612 PRIM_OP_INDICATION, msg);
613
614 return bssgp_prim_cb(&nmp.oph, NULL);
615}
616
617
Harald Welted11c0592012-09-06 21:57:11 +0200618/* One element (msgb) in a BSSGP Flow Control queue */
619struct bssgp_fc_queue_element {
620 /* linked list of queue elements */
621 struct llist_head list;
622 /* The message that we have enqueued */
623 struct msgb *msg;
624 /* Length of the LLC PDU part of the contained message */
625 uint32_t llc_pdu_len;
626 /* private pointer passed to the flow control out_cb function */
627 void *priv;
628};
629
630static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
631static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
632
633static void fc_timer_cb(void *data)
634{
635 struct bssgp_flow_control *fc = data;
636 struct bssgp_fc_queue_element *fcqe;
637 struct timeval time_now;
638
639 /* if the queue is empty, we return without sending something
640 * and without re-starting the timer */
641 if (llist_empty(&fc->queue))
642 return;
643
644 /* get the first entry from the queue */
645 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
646 list);
647
648 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
649 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
650 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
651 /* make sure we re-start the timer */
652 fc_queue_timer_cfg(fc);
653 return;
654 }
655
656 /* remove from the queue */
657 llist_del(&fcqe->list);
658
659 fc->queue_depth--;
660
661 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200662 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200663 fc->time_last_pdu = time_now;
664
665 /* call the output callback for this FC instance */
666 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
667
668 /* we expect that out_cb will in the end free the msgb once
669 * it is no longer needed */
670
671 /* but we have to free the queue element ourselves */
672 talloc_free(fcqe);
673
674 /* re-configure the timer for the next PDU */
675 fc_queue_timer_cfg(fc);
676}
677
678/* configure/schedule the flow control timer to expire once the bucket
679 * will have leaked a sufficient number of bytes to transmit the next
680 * PDU in the queue */
681static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
682{
683 struct bssgp_fc_queue_element *fcqe;
684 uint32_t msecs;
685
686 if (llist_empty(&fc->queue))
687 return 0;
688
Jacob Erlbeck97319352015-04-30 19:28:03 +0200689 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200690 list);
691
Harald Welte27b2bb72013-06-22 09:44:00 +0200692 if (fc->bucket_leak_rate != 0) {
693 /* Calculate the point in time at which we will have leaked
694 * a sufficient number of bytes from the bucket to transmit
695 * the first PDU in the queue */
696 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
697 /* FIXME: add that time to fc->time_last_pdu and subtract it from
698 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200699 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200700 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
701 } else {
702 /* If the PCU is telling us to not send any more data at all,
703 * there's no point starting a timer. */
704 }
Harald Welted11c0592012-09-06 21:57:11 +0200705
706 return 0;
707}
708
709/* Enqueue a PDU in the flow control queue for delayed transmission */
710static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
711 uint32_t llc_pdu_len, void *priv)
712{
713 struct bssgp_fc_queue_element *fcqe;
714
715 if (fc->queue_depth >= fc->max_queue_depth)
716 return -ENOSPC;
717
718 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
719 if (!fcqe)
720 return -ENOMEM;
721 fcqe->msg = msg;
722 fcqe->llc_pdu_len = llc_pdu_len;
723 fcqe->priv = priv;
724
725 llist_add_tail(&fcqe->list, &fc->queue);
726
727 fc->queue_depth++;
728
729 /* re-configure the timer for dequeueing the pdu */
730 fc_queue_timer_cfg(fc);
731
732 return 0;
733}
734
735/* According to Section 8.2 */
736static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
737{
738 struct timeval time_now, time_diff;
739 int64_t bucket_predicted;
740 uint32_t csecs_elapsed, leaked;
741
742 /* B' = B + L(p) - (Tc - Tp)*R */
743
744 /* compute number of centi-seconds that have elapsed since transmitting
745 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200746 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200747 timersub(&time_now, &fc->time_last_pdu, &time_diff);
748 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
749
750 /* compute number of bytes that have leaked in the elapsed number
751 * of centi-seconds */
752 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
753 /* add the current PDU length to the last bucket level */
754 bucket_predicted = fc->bucket_counter + pdu_len;
755 /* ... and subtract the number of leaked bytes */
756 bucket_predicted -= leaked;
757
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700758 if (bucket_predicted < pdu_len)
759 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200760
761 if (bucket_predicted <= fc->bucket_size_max) {
762 /* the bucket is not full yet, we can pass the packet */
763 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700764 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200765 }
766
767 /* bucket is full, PDU needs to be delayed */
768 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200769}
770
771/* output callback for BVC flow control */
772static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
773 uint32_t llc_pdu_len, void *priv)
774{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200775 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200776}
777
778/* input function of the flow control implementation, called first
779 * for the MM flow control, and then as the MM flow control output
780 * callback in order to perform BVC flow control */
781int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
782 uint32_t llc_pdu_len, void *priv)
783{
784 struct timeval time_now;
785
Harald Weltebb826222012-09-07 10:22:01 +0200786 if (llc_pdu_len > fc->bucket_size_max) {
787 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
788 "than maximum bucket size (%u)!\n", llc_pdu_len,
789 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200790 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200791 return -EIO;
792 }
793
Harald Welted11c0592012-09-06 21:57:11 +0200794 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100795 int rc;
796 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
797 if (rc)
798 msgb_free(msg);
799 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200800 } else {
801 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200802 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200803 fc->time_last_pdu = time_now;
804 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
805 }
806}
807
Harald Weltebb826222012-09-07 10:22:01 +0200808
809/* Initialize the Flow Control structure */
810void bssgp_fc_init(struct bssgp_flow_control *fc,
811 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
812 uint32_t max_queue_depth,
813 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
814 uint32_t llc_pdu_len, void *priv))
815{
816 fc->out_cb = out_cb;
817 fc->bucket_size_max = bucket_size_max;
818 fc->bucket_leak_rate = bucket_leak_rate;
819 fc->max_queue_depth = max_queue_depth;
820 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200821 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200822}
823
Harald Welted11c0592012-09-06 21:57:11 +0200824/* Initialize the Flow Control parameters for a new MS according to
825 * default values for the BVC specified by BVCI and NSEI */
826int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200827 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200828{
829 struct bssgp_bvc_ctx *ctx;
830
831 ctx = btsctx_by_bvci_nsei(bvci, nsei);
832 if (!ctx)
833 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200834
835 /* output call-back of per-MS FC is per-CTX FC */
836 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
837 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200838
839 return 0;
840}
841
Harald Welte25de8112010-05-13 21:26:28 +0200842static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200843 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800844{
Harald Welte27b2bb72013-06-22 09:44:00 +0200845 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
846 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800847
Harald Weltee9686b62010-05-31 18:07:17 +0200848 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
849 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800850
851 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
852 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
853 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
854 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200855 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
856 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
857 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800858 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200859 }
Harald Welte9ba50052010-03-14 15:45:01 +0800860
Harald Weltebb826222012-09-07 10:22:01 +0200861 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200862 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200863 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200864 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200865 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200866 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200867 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200868 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200869
Harald Welte27b2bb72013-06-22 09:44:00 +0200870 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
871 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
872 "rate of 0, stopping all DL GPRS!\n");
873 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
874 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
875 "rate of != 0, restarting all DL GPRS!\n");
876
877 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
878 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
879 "bucket leak rate of 0, stopping DL GPRS!\n");
880 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
881 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
882 "bucket leak rate != 0, restarting DL GPRS!\n");
883
884 /* reconfigure the timer for flow control based on new values */
885 fc_queue_timer_cfg(bctx->fc);
886
Harald Welte9ba50052010-03-14 15:45:01 +0800887 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200888 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200889 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800890}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200891
Harald Welte25de8112010-05-13 21:26:28 +0200892/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800893static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
894 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800895{
Harald Welteec19c102010-05-02 09:50:42 +0200896 struct bssgp_normal_hdr *bgph =
897 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200898 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800899 int rc = 0;
900
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100901 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
902
Harald Welte58e65c92010-05-13 21:45:23 +0200903 /* If traffic is received on a BVC that is marked as blocked, the
904 * received PDU shall not be accepted and a STATUS PDU (Cause value:
905 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100906 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200907 uint16_t bvci = msgb_bvci(msg);
908 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
909 }
910
Harald Welte9ba50052010-03-14 15:45:01 +0800911 switch (pdu_type) {
912 case BSSGP_PDUT_UL_UNITDATA:
913 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200914 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800915 break;
916 case BSSGP_PDUT_RA_CAPABILITY:
917 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200918 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
919 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200920 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800921 /* FIXME: send RA_CAPA_UPDATE_ACK */
922 break;
923 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200924 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800925 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200926 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800927 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800928 case BSSGP_PDUT_FLOW_CONTROL_BVC:
929 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200930 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800931 break;
932 case BSSGP_PDUT_FLOW_CONTROL_MS:
933 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200934 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
935 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200936 /* FIXME: actually implement flow control */
937 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800938 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800939 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100940 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100941 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800942 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
943 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
944 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
945 case BSSGP_PDUT_MODIFY_BSS_PFC:
946 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100947 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
948 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200949 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800950 break;
951 /* those only exist in the SGSN -> BSS direction */
952 case BSSGP_PDUT_DL_UNITDATA:
953 case BSSGP_PDUT_PAGING_PS:
954 case BSSGP_PDUT_PAGING_CS:
955 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200956 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
957 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100958 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
959 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200960 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
961 rc = -EINVAL;
962 break;
963 default:
Max2c34ab42016-03-17 15:42:26 +0100964 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
965 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200966 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
967 break;
968 }
969
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800970 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200971}
972
973/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800974static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
975 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200976{
977 struct bssgp_normal_hdr *bgph =
978 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
979 uint8_t pdu_type = bgph->pdu_type;
980 int rc = 0;
981 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200982 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200983
984 switch (bgph->pdu_type) {
985 case BSSGP_PDUT_SUSPEND:
986 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200987 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200988 break;
989 case BSSGP_PDUT_RESUME:
990 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200991 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200992 break;
993 case BSSGP_PDUT_FLUSH_LL_ACK:
994 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200995 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200996 /* FIXME: send NM_FLUSH_LL.res to NM */
997 break;
998 case BSSGP_PDUT_LLC_DISCARD:
999 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001000 if (!bctx) {
1001 LOGP(DBSSGP, LOGL_ERROR,
1002 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
1003 goto err_mand_ie;
1004 }
Harald Weltee9686b62010-05-31 18:07:17 +02001005 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001006 break;
1007 case BSSGP_PDUT_BVC_BLOCK:
1008 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +02001009 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001010 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1011 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
1012 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001013 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001014 }
Harald Welte2677ea52010-05-31 17:16:36 +02001015 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001016 break;
1017 case BSSGP_PDUT_BVC_UNBLOCK:
1018 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +02001019 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1020 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
1021 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001022 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001023 }
Harald Welte25de8112010-05-13 21:26:28 +02001024 rc = bssgp_rx_bvc_unblock(msg, tp);
1025 break;
Max590c4022017-06-28 14:29:24 +02001026 case BSSGP_PDUT_BVC_RESET_ACK:
1027 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
1028 break;
Harald Welte25de8112010-05-13 21:26:28 +02001029 case BSSGP_PDUT_BVC_RESET:
1030 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +02001031 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001032 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1033 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1034 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001035 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001036 }
Harald Welte25de8112010-05-13 21:26:28 +02001037 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1038 break;
1039 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001040 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001041 break;
1042 /* those only exist in the SGSN -> BSS direction */
1043 case BSSGP_PDUT_PAGING_PS:
1044 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001045 case BSSGP_PDUT_SUSPEND_ACK:
1046 case BSSGP_PDUT_SUSPEND_NACK:
1047 case BSSGP_PDUT_RESUME_ACK:
1048 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001049 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001050 case BSSGP_PDUT_BVC_BLOCK_ACK:
1051 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1052 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001053 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1054 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001055 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001056 rc = -EINVAL;
1057 break;
1058 default:
Max2c34ab42016-03-17 15:42:26 +01001059 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1060 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001061 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001062 break;
1063 }
1064
1065 return rc;
1066err_mand_ie:
1067 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1068}
1069
Harald Welte25de8112010-05-13 21:26:28 +02001070/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001071int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001072{
1073 struct bssgp_normal_hdr *bgph =
1074 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1075 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1076 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001077 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001078 uint8_t pdu_type = bgph->pdu_type;
1079 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001080 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001081 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001082 int data_len;
1083 int rc = 0;
1084
1085 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1086
1087 /* UNITDATA BSSGP headers have TLLI in front */
1088 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1089 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1090 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1091 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1092 } else {
1093 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1094 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1095 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001096 if (rc < 0) {
1097 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
1098 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001099 if (pdu_type != BSSGP_PDUT_STATUS)
1100 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001101 return rc;
1102 }
Harald Welte25de8112010-05-13 21:26:28 +02001103
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001104 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001105 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001106
Harald Welte25de8112010-05-13 21:26:28 +02001107 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001108 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001109
Harald Welte16c8dbb2010-05-17 23:30:01 +02001110 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001111 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001112 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1113 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1114 msgb_bssgp_len(msg));
1115 }
1116
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001117 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1118 * are otherwise unexpected */
1119 if (pdu_type == BSSGP_PDUT_STATUS)
1120 /* Some exception has occurred */
1121 return bssgp_rx_status(msg, &tp, bvci, bctx);
1122
1123 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1124 * registered if a BVCI is given. */
1125 if (!bctx && bvci != BVCI_SIGNALLING &&
1126 pdu_type != BSSGP_PDUT_BVC_RESET) {
Max548caef2019-03-07 13:49:34 +01001127 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001128 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001129 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1130 }
1131
Harald Welte61c07842010-05-18 11:57:08 +02001132 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001133 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001134 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001135 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001136 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001137 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001138 else
1139 LOGP(DBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001140 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1141 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001142
1143 return rc;
1144}
1145
Harald Weltede4599c2012-06-17 13:04:02 +08001146int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1147 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001148{
Harald Welte8a521132010-05-17 22:59:29 +02001149 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001150 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001151 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001152 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001153 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001154 uint16_t bvci = msgb_bvci(msg);
1155 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001156 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001157 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001158
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001159 OSMO_ASSERT(dup != NULL);
1160
Harald Welte30bc19a2010-05-02 11:19:37 +02001161 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001162 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001163 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001164 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001165 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001166 return -EINVAL;
1167 }
1168
1169 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001170 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001171 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1172 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001173 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001174 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001175 }
Harald Welte9ba50052010-03-14 15:45:01 +08001176
1177 if (msg->len > TVLV_MAX_ONEBYTE)
1178 llc_pdu_tlv_hdr_len += 1;
1179
1180 /* prepend the tag and length of the LLC-PDU TLV */
1181 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1182 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1183 if (llc_pdu_tlv_hdr_len > 2) {
1184 llc_pdu_tlv[1] = msg_len >> 8;
1185 llc_pdu_tlv[2] = msg_len & 0xff;
1186 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001187 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001188 llc_pdu_tlv[1] |= 0x80;
1189 }
1190
Harald Welte2f946832010-05-31 22:12:30 +02001191 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1192
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001193 /* Old TLLI to help BSS map from old->new */
1194 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001195 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001196 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001197 }
Harald Welte9ba50052010-03-14 15:45:01 +08001198
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001199 /* IMSI */
1200 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001201 uint8_t mi[GSM48_MID_MAX_SIZE];
1202/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1203 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1204 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1205 * mi[131], which is wrong */
1206#pragma GCC diagnostic push
1207#pragma GCC diagnostic ignored "-Warray-bounds"
1208 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1209 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1210 if (imsi_len > 2)
1211 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1212 imsi_len-2, mi+2);
1213#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001214 }
1215
1216 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001217 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001218 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1219 (uint8_t *) &drx_params);
1220
1221 /* FIXME: Priority */
1222
1223 /* MS Radio Access Capability */
1224 if (dup->ms_ra_cap.len)
1225 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1226 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1227
Harald Welte9ba50052010-03-14 15:45:01 +08001228 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001229 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001230
1231 /* prepend the QoS profile, TLLI and pdu type */
1232 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001233 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001234 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001235 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1236
Harald Welte16c8dbb2010-05-17 23:30:01 +02001237 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1238 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1239
Harald Welte30bc19a2010-05-02 11:19:37 +02001240 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001241
Harald Welted11c0592012-09-06 21:57:11 +02001242 /* check if we have to go through per-ms flow control or can go
1243 * directly to the per-BSS flow control */
1244 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001245 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001246 else
Harald Welted8b47692012-09-07 11:29:32 +02001247 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001248}
Harald Welte68b4f032010-06-09 16:22:28 +02001249
1250/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001251int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1252 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001253{
1254 struct msgb *msg = bssgp_msgb_alloc();
1255 struct bssgp_normal_hdr *bgph =
1256 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001257 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001258 uint8_t mi[GSM48_MID_MAX_SIZE];
1259 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001260 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001261
1262 if (imsi_len < 2)
1263 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001264
1265 msgb_nsei(msg) = nsei;
1266 msgb_bvci(msg) = ns_bvci;
1267
1268 if (pinfo->mode == BSSGP_PAGING_PS)
1269 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1270 else
1271 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1272 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001273/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1274 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1275 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1276 * mi[131], which is wrong */
1277#pragma GCC diagnostic push
1278#pragma GCC diagnostic ignored "-Warray-bounds"
1279 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1280 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1281#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001282 /* DRX Parameters */
1283 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1284 (uint8_t *) &drx_params);
1285 /* Scope */
1286 switch (pinfo->scope) {
1287 case BSSGP_PAGING_BSS_AREA:
1288 {
1289 uint8_t null = 0;
1290 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1291 }
1292 break;
1293 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001294 gsm48_encode_ra(&ra, &pinfo->raid);
1295 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001296 break;
1297 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001298 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001299 break;
1300 case BSSGP_PAGING_BVCI:
1301 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001302 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001303 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1304 }
1305 break;
1306 }
1307 /* QoS profile mandatory for PS */
1308 if (pinfo->mode == BSSGP_PAGING_PS)
1309 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1310
1311 /* Optional (P-)TMSI */
1312 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001313 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001314 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1315 }
1316
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001317 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001318}
Harald Weltecca49632012-06-16 17:45:59 +08001319
Harald Weltede4599c2012-06-17 13:04:02 +08001320void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001321{
1322 DBSSGP = ss;
1323}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001324
1325/*!
1326 * \brief Flush the queue of the bssgp_flow_control
1327 * \param[in] The flow control object which holds the queue.
1328 */
1329void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1330{
1331 struct bssgp_fc_queue_element *element, *tmp;
1332
1333 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1334 msgb_free(element->msg);
1335 llist_del(&element->list);
1336 talloc_free(element);
1337 }
1338}
1339
1340/*!
1341 * \brief Flush the queues of all BSSGP contexts.
1342 */
1343void bssgp_flush_all_queues()
1344{
1345 struct bssgp_bvc_ctx *bctx;
1346
1347 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1348 if (bctx->fc)
1349 bssgp_fc_flush_queue(bctx->fc);
1350 }
1351}