blob: fa4e187aee09a0b3e323cb6f974a1f5873cabdbf [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
76/* callback to be backward compatible with old users which do not set the bssgp_ns_send function */
77static 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);
Harald Welte6d3135c2019-05-08 14:00:37 +0200149 if (!ctx->ctrg) {
150 talloc_free(ctx);
151 return NULL;
152 }
Harald Welted8b47692012-09-07 11:29:32 +0200153 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
154 /* cofigure for 2Mbit, 30 packets in queue */
155 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200156
Harald Weltea78b9c22010-05-17 23:02:42 +0200157 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200158
159 return ctx;
160}
161
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700162void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx)
163{
164 if (!ctx)
165 return;
166 rate_ctr_group_free(ctx->ctrg);
167 llist_del(&ctx->list);
168 talloc_free(ctx);
169}
170
Harald Welte9ba50052010-03-14 15:45:01 +0800171/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200172static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800173{
174 struct msgb *msg = bssgp_msgb_alloc();
175 struct bssgp_normal_hdr *bgph =
176 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
177
Harald Welte24a655f2010-04-30 19:54:29 +0200178 msgb_nsei(msg) = nsei;
179 msgb_bvci(msg) = ns_bvci;
180
Harald Welte9ba50052010-03-14 15:45:01 +0800181 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
182 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
183
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200184 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800185}
186
Harald Weltea8aa4df2010-05-30 22:00:53 +0200187/* 10.3.7 SUSPEND-ACK PDU */
188int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
189 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
190{
191 struct msgb *msg = bssgp_msgb_alloc();
192 struct bssgp_normal_hdr *bgph =
193 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200194
195 msgb_nsei(msg) = nsei;
196 msgb_bvci(msg) = 0; /* Signalling */
197 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
198
Maxe29ec852018-01-05 14:30:22 +0100199 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100200 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200201 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
202
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200203 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200204}
205
206/* 10.3.8 SUSPEND-NACK PDU */
207int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100208 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200209 uint8_t *cause)
210{
211 struct msgb *msg = bssgp_msgb_alloc();
212 struct bssgp_normal_hdr *bgph =
213 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200214
215 msgb_nsei(msg) = nsei;
216 msgb_bvci(msg) = 0; /* Signalling */
217 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
218
Maxe29ec852018-01-05 14:30:22 +0100219 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100220 bssgp_msgb_ra_put(msg, ra_id);
221
Harald Weltea8aa4df2010-05-30 22:00:53 +0200222 if (cause)
223 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
224
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200225 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200226}
227
228/* 10.3.10 RESUME-ACK PDU */
229int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
230 const struct gprs_ra_id *ra_id)
231{
232 struct msgb *msg = bssgp_msgb_alloc();
233 struct bssgp_normal_hdr *bgph =
234 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200235
236 msgb_nsei(msg) = nsei;
237 msgb_bvci(msg) = 0; /* Signalling */
238 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
239
Maxe29ec852018-01-05 14:30:22 +0100240 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100241 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200242
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200243 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200244}
245
246/* 10.3.11 RESUME-NACK PDU */
247int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
248 const struct gprs_ra_id *ra_id, uint8_t *cause)
249{
250 struct msgb *msg = bssgp_msgb_alloc();
251 struct bssgp_normal_hdr *bgph =
252 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200253
254 msgb_nsei(msg) = nsei;
255 msgb_bvci(msg) = 0; /* Signalling */
256 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
257
Maxe29ec852018-01-05 14:30:22 +0100258 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100259 bssgp_msgb_ra_put(msg, ra_id);
260
Harald Weltea8aa4df2010-05-30 22:00:53 +0200261 if (cause)
262 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
263
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200264 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200265}
266
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200267uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200268{
269 /* 6 octets RAC */
270 gsm48_parse_ra(raid, buf);
271 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200272 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200273}
274
Harald Welte28610072011-11-24 21:32:07 +0100275int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
276 uint16_t cid)
277{
Harald Welte28610072011-11-24 21:32:07 +0100278 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100279 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100280 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200281 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100282
283 return 8;
284}
285
Harald Welte3fddf3c2010-05-01 16:48:27 +0200286/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200287static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200288 uint16_t ns_bvci)
289{
Harald Welte15a36432012-06-17 12:16:31 +0800290 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200291 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200292 uint16_t nsei = msgb_nsei(msg);
293 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200294
Harald Weltebfe62e52017-05-15 12:48:30 +0200295 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200296 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200297 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
298
Harald Welte6752fa42010-05-02 09:23:16 +0200299 /* look-up or create the BTS context for this BVC */
300 bctx = btsctx_by_bvci_nsei(bvci, nsei);
301 if (!bctx)
302 bctx = btsctx_alloc(bvci, nsei);
303
Harald Welte25de8112010-05-13 21:26:28 +0200304 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
305 bctx->state &= ~BVC_S_BLOCKED;
306
Harald Welte3fddf3c2010-05-01 16:48:27 +0200307 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
308 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200309 if (bvci != 0 && bvci != 1) {
310 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200311 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200312 "missing mandatory IE\n", bvci);
313 return -EINVAL;
314 }
315 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200316 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
317 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100318 LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
319 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200320 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200321
Harald Welte15a36432012-06-17 12:16:31 +0800322 /* Send NM_BVC_RESET.ind to NM */
323 memset(&nmp, 0, sizeof(nmp));
324 nmp.nsei = nsei;
325 nmp.bvci = bvci;
326 nmp.tp = tp;
327 nmp.ra_id = &bctx->ra_id;
328 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
329 PRIM_OP_INDICATION, msg);
330 bssgp_prim_cb(&nmp.oph, NULL);
331
Harald Welte6752fa42010-05-02 09:23:16 +0200332 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200333 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
334 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200335 return 0;
336}
337
Harald Welte25de8112010-05-13 21:26:28 +0200338static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
339{
Harald Welte15a36432012-06-17 12:16:31 +0800340 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100341 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200342 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200343
Harald Weltebfe62e52017-05-15 12:48:30 +0200344 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200345 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200346 /* 8.3.2: Signalling BVC shall never be blocked */
347 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
348 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100349 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200350 return 0;
351 }
Harald Welte25de8112010-05-13 21:26:28 +0200352
Harald Welte086fe322011-08-19 16:45:19 +0200353 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200354
Max548caef2019-03-07 13:49:34 +0100355 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200356 if (!ptp_ctx)
357 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
358
359 ptp_ctx->state |= BVC_S_BLOCKED;
360 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
361
Harald Welte15a36432012-06-17 12:16:31 +0800362 /* Send NM_BVC_BLOCK.ind to NM */
363 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100364 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800365 nmp.bvci = bvci;
366 nmp.tp = tp;
367 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
368 PRIM_OP_INDICATION, msg);
369 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200370
371 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100372 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200373 bvci, msgb_bvci(msg));
374};
375
376static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
377{
Harald Welte15a36432012-06-17 12:16:31 +0800378 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100379 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200380 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200381
Harald Weltebfe62e52017-05-15 12:48:30 +0200382 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200383 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200384 /* 8.3.2: Signalling BVC shall never be blocked */
385 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
386 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100387 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200388 return 0;
389 }
Harald Welte25de8112010-05-13 21:26:28 +0200390
Harald Weltee9686b62010-05-31 18:07:17 +0200391 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200392
Max548caef2019-03-07 13:49:34 +0100393 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200394 if (!ptp_ctx)
395 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
396
397 ptp_ctx->state &= ~BVC_S_BLOCKED;
398
Harald Welte15a36432012-06-17 12:16:31 +0800399 /* Send NM_BVC_UNBLOCK.ind to NM */
400 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100401 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800402 nmp.bvci = bvci;
403 nmp.tp = tp;
404 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
405 PRIM_OP_INDICATION, msg);
406 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200407
408 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100409 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200410 bvci, msgb_bvci(msg));
411};
412
Harald Welte9ba50052010-03-14 15:45:01 +0800413/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200414static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200415 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800416{
Harald Welte15a36432012-06-17 12:16:31 +0800417 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200418 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800419
Harald Welte6752fa42010-05-02 09:23:16 +0200420 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200421 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800422
Harald Welte086fe322011-08-19 16:45:19 +0200423 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200424
Harald Welte9ba50052010-03-14 15:45:01 +0800425 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200426 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200427 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
428 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
429 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200430 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200431 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200432
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200433 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800434 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
435 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800436
Harald Welte15a36432012-06-17 12:16:31 +0800437 /* Send BSSGP_UL_UD.ind to NM */
438 memset(&gbp, 0, sizeof(gbp));
439 gbp.nsei = ctx->nsei;
440 gbp.bvci = ctx->bvci;
441 gbp.tlli = msgb_tlli(msg);
442 gbp.tp = tp;
443 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
444 PRIM_OP_INDICATION, msg);
445 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800446}
447
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200448static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800449{
Harald Welte15a36432012-06-17 12:16:31 +0800450 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200451 struct gprs_ra_id raid;
452 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100453 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200454 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800455
Harald Welte25de8112010-05-13 21:26:28 +0200456 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200457 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
458 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200459 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200460 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200461 }
Harald Welte9ba50052010-03-14 15:45:01 +0800462
Harald Weltebfe62e52017-05-15 12:48:30 +0200463 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200464
Harald Welte17925322010-05-31 20:18:35 +0200465 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200466 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200467
Harald Weltea8aa4df2010-05-30 22:00:53 +0200468 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
469
Harald Welte313cccf2010-06-09 11:22:47 +0200470 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800471 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100472 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200473 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800474 gbp.tlli = tlli;
475 gbp.ra_id = &raid;
476 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
477 PRIM_OP_REQUEST, msg);
478
479 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200480 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100481 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200482
Max548caef2019-03-07 13:49:34 +0100483 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200484
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800485 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800486}
487
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200488static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800489{
Harald Welte15a36432012-06-17 12:16:31 +0800490 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200491 struct gprs_ra_id raid;
492 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200493 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100494 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200495 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800496
Harald Welte25de8112010-05-13 21:26:28 +0200497 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
498 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200499 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
500 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200501 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200502 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200503 }
Harald Welte9ba50052010-03-14 15:45:01 +0800504
Harald Weltebfe62e52017-05-15 12:48:30 +0200505 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200506 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200507
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200508 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200509
Harald Weltea8aa4df2010-05-30 22:00:53 +0200510 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
511
Harald Welte313cccf2010-06-09 11:22:47 +0200512 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800513 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100514 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200515 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800516 gbp.tlli = tlli;
517 gbp.ra_id = &raid;
518 gbp.u.resume.suspend_ref = suspend_ref;
519 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
520 PRIM_OP_REQUEST, msg);
521
522 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200523 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100524 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200525 NULL);
526
Max548caef2019-03-07 13:49:34 +0100527 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800528 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800529}
530
Harald Weltee9686b62010-05-31 18:07:17 +0200531
532static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
533 struct bssgp_bvc_ctx *ctx)
534{
Harald Welte15a36432012-06-17 12:16:31 +0800535 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200536 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100537 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200538
539 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
540 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
541 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
542 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
543 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
544 "missing mandatory IE\n", ctx->bvci);
545 }
546
Harald Welteb7363142010-07-23 21:59:29 +0200547 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200548 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200549
Harald Welte086fe322011-08-19 16:45:19 +0200550 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200551 ctx->bvci, tlli);
552
553 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
554
Harald Welte15a36432012-06-17 12:16:31 +0800555 /* send NM_LLC_DISCARDED to NM */
556 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100557 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800558 nmp.bvci = ctx->bvci;
559 nmp.tlli = tlli;
560 nmp.tp = tp;
561 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
562 PRIM_OP_INDICATION, msg);
563
564 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200565}
566
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100567int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
568 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
569{
Max548caef2019-03-07 13:49:34 +0100570 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100571 struct osmo_bssgp_prim nmp;
572 enum gprs_bssgp_cause cause;
573
574 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
575 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
576 "missing mandatory IE\n", bvci);
577 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
578 } else {
579 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
580 }
581
582 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
583 bvci, bssgp_cause_str(cause));
584
585 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
586 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
587 LOGP(DBSSGP, LOGL_ERROR,
588 "BSSGP BVCI=%u Rx STATUS cause=%s "
589 "missing conditional BVCI IE\n",
590 bvci, bssgp_cause_str(cause));
591 }
592
593 if (bctx)
594 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
595
596 /* send NM_STATUS to NM */
597 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100598 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100599 nmp.bvci = bvci;
600 nmp.tp = tp;
601 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
602 PRIM_OP_INDICATION, msg);
603
604 return bssgp_prim_cb(&nmp.oph, NULL);
605}
606
607
Harald Welted11c0592012-09-06 21:57:11 +0200608/* One element (msgb) in a BSSGP Flow Control queue */
609struct bssgp_fc_queue_element {
610 /* linked list of queue elements */
611 struct llist_head list;
612 /* The message that we have enqueued */
613 struct msgb *msg;
614 /* Length of the LLC PDU part of the contained message */
615 uint32_t llc_pdu_len;
616 /* private pointer passed to the flow control out_cb function */
617 void *priv;
618};
619
620static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
621static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
622
623static void fc_timer_cb(void *data)
624{
625 struct bssgp_flow_control *fc = data;
626 struct bssgp_fc_queue_element *fcqe;
627 struct timeval time_now;
628
629 /* if the queue is empty, we return without sending something
630 * and without re-starting the timer */
631 if (llist_empty(&fc->queue))
632 return;
633
634 /* get the first entry from the queue */
635 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
636 list);
637
638 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
639 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
640 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
641 /* make sure we re-start the timer */
642 fc_queue_timer_cfg(fc);
643 return;
644 }
645
646 /* remove from the queue */
647 llist_del(&fcqe->list);
648
649 fc->queue_depth--;
650
651 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200652 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200653 fc->time_last_pdu = time_now;
654
655 /* call the output callback for this FC instance */
656 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
657
658 /* we expect that out_cb will in the end free the msgb once
659 * it is no longer needed */
660
661 /* but we have to free the queue element ourselves */
662 talloc_free(fcqe);
663
664 /* re-configure the timer for the next PDU */
665 fc_queue_timer_cfg(fc);
666}
667
668/* configure/schedule the flow control timer to expire once the bucket
669 * will have leaked a sufficient number of bytes to transmit the next
670 * PDU in the queue */
671static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
672{
673 struct bssgp_fc_queue_element *fcqe;
674 uint32_t msecs;
675
676 if (llist_empty(&fc->queue))
677 return 0;
678
Jacob Erlbeck97319352015-04-30 19:28:03 +0200679 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200680 list);
681
Harald Welte27b2bb72013-06-22 09:44:00 +0200682 if (fc->bucket_leak_rate != 0) {
683 /* Calculate the point in time at which we will have leaked
684 * a sufficient number of bytes from the bucket to transmit
685 * the first PDU in the queue */
686 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
687 /* FIXME: add that time to fc->time_last_pdu and subtract it from
688 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200689 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200690 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
691 } else {
692 /* If the PCU is telling us to not send any more data at all,
693 * there's no point starting a timer. */
694 }
Harald Welted11c0592012-09-06 21:57:11 +0200695
696 return 0;
697}
698
699/* Enqueue a PDU in the flow control queue for delayed transmission */
700static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
701 uint32_t llc_pdu_len, void *priv)
702{
703 struct bssgp_fc_queue_element *fcqe;
704
705 if (fc->queue_depth >= fc->max_queue_depth)
706 return -ENOSPC;
707
708 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
709 if (!fcqe)
710 return -ENOMEM;
711 fcqe->msg = msg;
712 fcqe->llc_pdu_len = llc_pdu_len;
713 fcqe->priv = priv;
714
715 llist_add_tail(&fcqe->list, &fc->queue);
716
717 fc->queue_depth++;
718
719 /* re-configure the timer for dequeueing the pdu */
720 fc_queue_timer_cfg(fc);
721
722 return 0;
723}
724
725/* According to Section 8.2 */
726static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
727{
728 struct timeval time_now, time_diff;
729 int64_t bucket_predicted;
730 uint32_t csecs_elapsed, leaked;
731
732 /* B' = B + L(p) - (Tc - Tp)*R */
733
734 /* compute number of centi-seconds that have elapsed since transmitting
735 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200736 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200737 timersub(&time_now, &fc->time_last_pdu, &time_diff);
738 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
739
740 /* compute number of bytes that have leaked in the elapsed number
741 * of centi-seconds */
742 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
743 /* add the current PDU length to the last bucket level */
744 bucket_predicted = fc->bucket_counter + pdu_len;
745 /* ... and subtract the number of leaked bytes */
746 bucket_predicted -= leaked;
747
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700748 if (bucket_predicted < pdu_len)
749 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200750
751 if (bucket_predicted <= fc->bucket_size_max) {
752 /* the bucket is not full yet, we can pass the packet */
753 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700754 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200755 }
756
757 /* bucket is full, PDU needs to be delayed */
758 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200759}
760
761/* output callback for BVC flow control */
762static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
763 uint32_t llc_pdu_len, void *priv)
764{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200765 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200766}
767
768/* input function of the flow control implementation, called first
769 * for the MM flow control, and then as the MM flow control output
770 * callback in order to perform BVC flow control */
771int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
772 uint32_t llc_pdu_len, void *priv)
773{
774 struct timeval time_now;
775
Harald Weltebb826222012-09-07 10:22:01 +0200776 if (llc_pdu_len > fc->bucket_size_max) {
777 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
778 "than maximum bucket size (%u)!\n", llc_pdu_len,
779 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200780 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200781 return -EIO;
782 }
783
Harald Welted11c0592012-09-06 21:57:11 +0200784 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100785 int rc;
786 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
787 if (rc)
788 msgb_free(msg);
789 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200790 } else {
791 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200792 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200793 fc->time_last_pdu = time_now;
794 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
795 }
796}
797
Harald Weltebb826222012-09-07 10:22:01 +0200798
799/* Initialize the Flow Control structure */
800void bssgp_fc_init(struct bssgp_flow_control *fc,
801 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
802 uint32_t max_queue_depth,
803 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
804 uint32_t llc_pdu_len, void *priv))
805{
806 fc->out_cb = out_cb;
807 fc->bucket_size_max = bucket_size_max;
808 fc->bucket_leak_rate = bucket_leak_rate;
809 fc->max_queue_depth = max_queue_depth;
810 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200811 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200812}
813
Harald Welted11c0592012-09-06 21:57:11 +0200814/* Initialize the Flow Control parameters for a new MS according to
815 * default values for the BVC specified by BVCI and NSEI */
816int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200817 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200818{
819 struct bssgp_bvc_ctx *ctx;
820
821 ctx = btsctx_by_bvci_nsei(bvci, nsei);
822 if (!ctx)
823 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200824
825 /* output call-back of per-MS FC is per-CTX FC */
826 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
827 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200828
829 return 0;
830}
831
Harald Welte25de8112010-05-13 21:26:28 +0200832static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200833 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800834{
Harald Welte27b2bb72013-06-22 09:44:00 +0200835 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
836 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800837
Harald Weltee9686b62010-05-31 18:07:17 +0200838 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
839 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800840
841 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
842 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
843 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
844 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200845 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
846 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
847 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800848 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200849 }
Harald Welte9ba50052010-03-14 15:45:01 +0800850
Harald Weltebb826222012-09-07 10:22:01 +0200851 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200852 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200853 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200854 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200855 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200856 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200857 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200858 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200859
Harald Welte27b2bb72013-06-22 09:44:00 +0200860 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
861 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
862 "rate of 0, stopping all DL GPRS!\n");
863 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
864 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
865 "rate of != 0, restarting all DL GPRS!\n");
866
867 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
868 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
869 "bucket leak rate of 0, stopping DL GPRS!\n");
870 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
871 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
872 "bucket leak rate != 0, restarting DL GPRS!\n");
873
874 /* reconfigure the timer for flow control based on new values */
875 fc_queue_timer_cfg(bctx->fc);
876
Harald Welte9ba50052010-03-14 15:45:01 +0800877 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200878 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200879 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800880}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200881
Harald Welte25de8112010-05-13 21:26:28 +0200882/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800883static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
884 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800885{
Harald Welteec19c102010-05-02 09:50:42 +0200886 struct bssgp_normal_hdr *bgph =
887 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200888 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800889 int rc = 0;
890
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100891 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
892
Harald Welte58e65c92010-05-13 21:45:23 +0200893 /* If traffic is received on a BVC that is marked as blocked, the
894 * received PDU shall not be accepted and a STATUS PDU (Cause value:
895 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100896 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200897 uint16_t bvci = msgb_bvci(msg);
898 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
899 }
900
Harald Welte9ba50052010-03-14 15:45:01 +0800901 switch (pdu_type) {
902 case BSSGP_PDUT_UL_UNITDATA:
903 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200904 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800905 break;
906 case BSSGP_PDUT_RA_CAPABILITY:
907 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200908 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
909 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200910 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800911 /* FIXME: send RA_CAPA_UPDATE_ACK */
912 break;
913 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200914 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800915 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200916 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800917 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800918 case BSSGP_PDUT_FLOW_CONTROL_BVC:
919 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200920 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800921 break;
922 case BSSGP_PDUT_FLOW_CONTROL_MS:
923 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200924 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
925 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200926 /* FIXME: actually implement flow control */
927 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800928 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800929 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100930 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100931 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800932 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
933 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
934 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
935 case BSSGP_PDUT_MODIFY_BSS_PFC:
936 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100937 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
938 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200939 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800940 break;
941 /* those only exist in the SGSN -> BSS direction */
942 case BSSGP_PDUT_DL_UNITDATA:
943 case BSSGP_PDUT_PAGING_PS:
944 case BSSGP_PDUT_PAGING_CS:
945 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200946 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
947 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100948 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
949 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200950 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
951 rc = -EINVAL;
952 break;
953 default:
Max2c34ab42016-03-17 15:42:26 +0100954 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
955 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200956 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
957 break;
958 }
959
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800960 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200961}
962
963/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800964static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
965 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200966{
967 struct bssgp_normal_hdr *bgph =
968 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
969 uint8_t pdu_type = bgph->pdu_type;
970 int rc = 0;
971 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200972 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200973
974 switch (bgph->pdu_type) {
975 case BSSGP_PDUT_SUSPEND:
976 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200977 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200978 break;
979 case BSSGP_PDUT_RESUME:
980 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200981 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200982 break;
983 case BSSGP_PDUT_FLUSH_LL_ACK:
984 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200985 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200986 /* FIXME: send NM_FLUSH_LL.res to NM */
987 break;
988 case BSSGP_PDUT_LLC_DISCARD:
989 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200990 if (!bctx) {
991 LOGP(DBSSGP, LOGL_ERROR,
992 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
993 goto err_mand_ie;
994 }
Harald Weltee9686b62010-05-31 18:07:17 +0200995 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200996 break;
997 case BSSGP_PDUT_BVC_BLOCK:
998 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200999 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001000 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1001 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
1002 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001003 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001004 }
Harald Welte2677ea52010-05-31 17:16:36 +02001005 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001006 break;
1007 case BSSGP_PDUT_BVC_UNBLOCK:
1008 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +02001009 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1010 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
1011 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001012 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001013 }
Harald Welte25de8112010-05-13 21:26:28 +02001014 rc = bssgp_rx_bvc_unblock(msg, tp);
1015 break;
Max590c4022017-06-28 14:29:24 +02001016 case BSSGP_PDUT_BVC_RESET_ACK:
1017 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
1018 break;
Harald Welte25de8112010-05-13 21:26:28 +02001019 case BSSGP_PDUT_BVC_RESET:
1020 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +02001021 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001022 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1023 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1024 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001025 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001026 }
Harald Welte25de8112010-05-13 21:26:28 +02001027 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1028 break;
1029 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001030 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001031 break;
1032 /* those only exist in the SGSN -> BSS direction */
1033 case BSSGP_PDUT_PAGING_PS:
1034 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001035 case BSSGP_PDUT_SUSPEND_ACK:
1036 case BSSGP_PDUT_SUSPEND_NACK:
1037 case BSSGP_PDUT_RESUME_ACK:
1038 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001039 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001040 case BSSGP_PDUT_BVC_BLOCK_ACK:
1041 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1042 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001043 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1044 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001045 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001046 rc = -EINVAL;
1047 break;
1048 default:
Max2c34ab42016-03-17 15:42:26 +01001049 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1050 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001051 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001052 break;
1053 }
1054
1055 return rc;
1056err_mand_ie:
1057 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1058}
1059
Harald Welte25de8112010-05-13 21:26:28 +02001060/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001061int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001062{
1063 struct bssgp_normal_hdr *bgph =
1064 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1065 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1066 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001067 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001068 uint8_t pdu_type = bgph->pdu_type;
1069 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001070 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001071 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001072 int data_len;
1073 int rc = 0;
1074
1075 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1076
1077 /* UNITDATA BSSGP headers have TLLI in front */
1078 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1079 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1080 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1081 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1082 } else {
1083 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1084 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1085 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001086 if (rc < 0) {
1087 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
1088 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001089 if (pdu_type != BSSGP_PDUT_STATUS)
1090 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001091 return rc;
1092 }
Harald Welte25de8112010-05-13 21:26:28 +02001093
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001094 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001095 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001096
Harald Welte25de8112010-05-13 21:26:28 +02001097 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001098 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001099
Harald Welte16c8dbb2010-05-17 23:30:01 +02001100 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001101 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001102 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1103 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1104 msgb_bssgp_len(msg));
1105 }
1106
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001107 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1108 * are otherwise unexpected */
1109 if (pdu_type == BSSGP_PDUT_STATUS)
1110 /* Some exception has occurred */
1111 return bssgp_rx_status(msg, &tp, bvci, bctx);
1112
1113 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1114 * registered if a BVCI is given. */
1115 if (!bctx && bvci != BVCI_SIGNALLING &&
1116 pdu_type != BSSGP_PDUT_BVC_RESET) {
Max548caef2019-03-07 13:49:34 +01001117 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001118 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001119 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1120 }
1121
Harald Welte61c07842010-05-18 11:57:08 +02001122 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001123 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001124 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001125 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001126 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001127 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001128 else
1129 LOGP(DBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001130 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1131 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001132
1133 return rc;
1134}
1135
Harald Weltede4599c2012-06-17 13:04:02 +08001136int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1137 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001138{
Harald Welte8a521132010-05-17 22:59:29 +02001139 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001140 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001141 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001142 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001143 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001144 uint16_t bvci = msgb_bvci(msg);
1145 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001146 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001147 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001148
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001149 OSMO_ASSERT(dup != NULL);
1150
Harald Welte30bc19a2010-05-02 11:19:37 +02001151 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001152 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001153 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001154 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001155 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001156 return -EINVAL;
1157 }
1158
1159 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001160 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001161 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1162 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001163 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001164 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001165 }
Harald Welte9ba50052010-03-14 15:45:01 +08001166
1167 if (msg->len > TVLV_MAX_ONEBYTE)
1168 llc_pdu_tlv_hdr_len += 1;
1169
1170 /* prepend the tag and length of the LLC-PDU TLV */
1171 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1172 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1173 if (llc_pdu_tlv_hdr_len > 2) {
1174 llc_pdu_tlv[1] = msg_len >> 8;
1175 llc_pdu_tlv[2] = msg_len & 0xff;
1176 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001177 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001178 llc_pdu_tlv[1] |= 0x80;
1179 }
1180
Harald Welte2f946832010-05-31 22:12:30 +02001181 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1182
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001183 /* Old TLLI to help BSS map from old->new */
1184 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001185 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001186 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001187 }
Harald Welte9ba50052010-03-14 15:45:01 +08001188
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001189 /* IMSI */
1190 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001191 uint8_t mi[GSM48_MID_MAX_SIZE];
1192/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1193 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1194 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1195 * mi[131], which is wrong */
1196#pragma GCC diagnostic push
1197#pragma GCC diagnostic ignored "-Warray-bounds"
1198 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1199 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1200 if (imsi_len > 2)
1201 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1202 imsi_len-2, mi+2);
1203#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001204 }
1205
1206 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001207 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001208 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1209 (uint8_t *) &drx_params);
1210
1211 /* FIXME: Priority */
1212
1213 /* MS Radio Access Capability */
1214 if (dup->ms_ra_cap.len)
1215 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1216 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1217
Harald Welte9ba50052010-03-14 15:45:01 +08001218 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001219 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001220
1221 /* prepend the QoS profile, TLLI and pdu type */
1222 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001223 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001224 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001225 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1226
Harald Welte16c8dbb2010-05-17 23:30:01 +02001227 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1228 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1229
Harald Welte30bc19a2010-05-02 11:19:37 +02001230 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001231
Harald Welted11c0592012-09-06 21:57:11 +02001232 /* check if we have to go through per-ms flow control or can go
1233 * directly to the per-BSS flow control */
1234 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001235 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001236 else
Harald Welted8b47692012-09-07 11:29:32 +02001237 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001238}
Harald Welte68b4f032010-06-09 16:22:28 +02001239
1240/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001241int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1242 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001243{
1244 struct msgb *msg = bssgp_msgb_alloc();
1245 struct bssgp_normal_hdr *bgph =
1246 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001247 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001248 uint8_t mi[GSM48_MID_MAX_SIZE];
1249 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001250 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001251
1252 if (imsi_len < 2)
1253 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001254
1255 msgb_nsei(msg) = nsei;
1256 msgb_bvci(msg) = ns_bvci;
1257
1258 if (pinfo->mode == BSSGP_PAGING_PS)
1259 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1260 else
1261 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1262 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001263/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1264 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1265 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1266 * mi[131], which is wrong */
1267#pragma GCC diagnostic push
1268#pragma GCC diagnostic ignored "-Warray-bounds"
1269 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1270 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1271#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001272 /* DRX Parameters */
1273 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1274 (uint8_t *) &drx_params);
1275 /* Scope */
1276 switch (pinfo->scope) {
1277 case BSSGP_PAGING_BSS_AREA:
1278 {
1279 uint8_t null = 0;
1280 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1281 }
1282 break;
1283 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001284 gsm48_encode_ra(&ra, &pinfo->raid);
1285 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001286 break;
1287 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001288 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001289 break;
1290 case BSSGP_PAGING_BVCI:
1291 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001292 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001293 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1294 }
1295 break;
1296 }
1297 /* QoS profile mandatory for PS */
1298 if (pinfo->mode == BSSGP_PAGING_PS)
1299 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1300
1301 /* Optional (P-)TMSI */
1302 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001303 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001304 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1305 }
1306
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001307 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001308}
Harald Weltecca49632012-06-16 17:45:59 +08001309
Harald Weltede4599c2012-06-17 13:04:02 +08001310void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001311{
1312 DBSSGP = ss;
1313}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001314
1315/*!
1316 * \brief Flush the queue of the bssgp_flow_control
1317 * \param[in] The flow control object which holds the queue.
1318 */
1319void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1320{
1321 struct bssgp_fc_queue_element *element, *tmp;
1322
1323 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1324 msgb_free(element->msg);
1325 llist_del(&element->list);
1326 talloc_free(element);
1327 }
1328}
1329
1330/*!
1331 * \brief Flush the queues of all BSSGP contexts.
1332 */
1333void bssgp_flush_all_queues()
1334{
1335 struct bssgp_bvc_ctx *bctx;
1336
1337 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1338 if (bctx->fc)
1339 bssgp_fc_flush_queue(bctx->fc);
1340 }
1341}