blob: e5b4380f9ed59ced675de6c4d8340ac7ca8bb589 [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;
174 rate_ctr_group_free(ctx->ctrg);
175 llist_del(&ctx->list);
176 talloc_free(ctx);
177}
178
Harald Welte9ba50052010-03-14 15:45:01 +0800179/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200180static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800181{
182 struct msgb *msg = bssgp_msgb_alloc();
183 struct bssgp_normal_hdr *bgph =
184 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
185
Harald Welte24a655f2010-04-30 19:54:29 +0200186 msgb_nsei(msg) = nsei;
187 msgb_bvci(msg) = ns_bvci;
188
Harald Welte9ba50052010-03-14 15:45:01 +0800189 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
190 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
191
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200192 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800193}
194
Harald Weltea8aa4df2010-05-30 22:00:53 +0200195/* 10.3.7 SUSPEND-ACK PDU */
196int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
197 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
198{
199 struct msgb *msg = bssgp_msgb_alloc();
200 struct bssgp_normal_hdr *bgph =
201 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200202
203 msgb_nsei(msg) = nsei;
204 msgb_bvci(msg) = 0; /* Signalling */
205 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
206
Maxe29ec852018-01-05 14:30:22 +0100207 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100208 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200209 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
210
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200211 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200212}
213
214/* 10.3.8 SUSPEND-NACK PDU */
215int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100216 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200217 uint8_t *cause)
218{
219 struct msgb *msg = bssgp_msgb_alloc();
220 struct bssgp_normal_hdr *bgph =
221 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200222
223 msgb_nsei(msg) = nsei;
224 msgb_bvci(msg) = 0; /* Signalling */
225 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
226
Maxe29ec852018-01-05 14:30:22 +0100227 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100228 bssgp_msgb_ra_put(msg, ra_id);
229
Harald Weltea8aa4df2010-05-30 22:00:53 +0200230 if (cause)
231 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
232
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200233 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200234}
235
236/* 10.3.10 RESUME-ACK PDU */
237int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
238 const struct gprs_ra_id *ra_id)
239{
240 struct msgb *msg = bssgp_msgb_alloc();
241 struct bssgp_normal_hdr *bgph =
242 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200243
244 msgb_nsei(msg) = nsei;
245 msgb_bvci(msg) = 0; /* Signalling */
246 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
247
Maxe29ec852018-01-05 14:30:22 +0100248 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100249 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200250
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200251 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200252}
253
254/* 10.3.11 RESUME-NACK PDU */
255int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
256 const struct gprs_ra_id *ra_id, uint8_t *cause)
257{
258 struct msgb *msg = bssgp_msgb_alloc();
259 struct bssgp_normal_hdr *bgph =
260 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200261
262 msgb_nsei(msg) = nsei;
263 msgb_bvci(msg) = 0; /* Signalling */
264 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
265
Maxe29ec852018-01-05 14:30:22 +0100266 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100267 bssgp_msgb_ra_put(msg, ra_id);
268
Harald Weltea8aa4df2010-05-30 22:00:53 +0200269 if (cause)
270 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
271
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200272 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200273}
274
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200275uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200276{
277 /* 6 octets RAC */
278 gsm48_parse_ra(raid, buf);
279 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200280 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200281}
282
Harald Welte28610072011-11-24 21:32:07 +0100283int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
284 uint16_t cid)
285{
Harald Welte28610072011-11-24 21:32:07 +0100286 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100287 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100288 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200289 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100290
291 return 8;
292}
293
Harald Welte3fddf3c2010-05-01 16:48:27 +0200294/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200295static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200296 uint16_t ns_bvci)
297{
Harald Welte15a36432012-06-17 12:16:31 +0800298 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200299 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200300 uint16_t nsei = msgb_nsei(msg);
301 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200302
Harald Weltebfe62e52017-05-15 12:48:30 +0200303 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200304 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200305 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
306
Harald Welte6752fa42010-05-02 09:23:16 +0200307 /* look-up or create the BTS context for this BVC */
308 bctx = btsctx_by_bvci_nsei(bvci, nsei);
309 if (!bctx)
310 bctx = btsctx_alloc(bvci, nsei);
311
Harald Welte25de8112010-05-13 21:26:28 +0200312 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
313 bctx->state &= ~BVC_S_BLOCKED;
314
Harald Welte3fddf3c2010-05-01 16:48:27 +0200315 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
316 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200317 if (bvci != 0 && bvci != 1) {
318 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200319 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200320 "missing mandatory IE\n", bvci);
321 return -EINVAL;
322 }
323 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200324 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
325 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100326 LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
327 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200328 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200329
Harald Welte15a36432012-06-17 12:16:31 +0800330 /* Send NM_BVC_RESET.ind to NM */
331 memset(&nmp, 0, sizeof(nmp));
332 nmp.nsei = nsei;
333 nmp.bvci = bvci;
334 nmp.tp = tp;
335 nmp.ra_id = &bctx->ra_id;
336 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
337 PRIM_OP_INDICATION, msg);
338 bssgp_prim_cb(&nmp.oph, NULL);
339
Harald Welte6752fa42010-05-02 09:23:16 +0200340 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200341 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
342 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200343 return 0;
344}
345
Harald Welte25de8112010-05-13 21:26:28 +0200346static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
347{
Harald Welte15a36432012-06-17 12:16:31 +0800348 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100349 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200350 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200351
Harald Weltebfe62e52017-05-15 12:48:30 +0200352 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200353 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200354 /* 8.3.2: Signalling BVC shall never be blocked */
355 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
356 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100357 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200358 return 0;
359 }
Harald Welte25de8112010-05-13 21:26:28 +0200360
Harald Welte086fe322011-08-19 16:45:19 +0200361 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200362
Max548caef2019-03-07 13:49:34 +0100363 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200364 if (!ptp_ctx)
365 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
366
367 ptp_ctx->state |= BVC_S_BLOCKED;
368 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
369
Harald Welte15a36432012-06-17 12:16:31 +0800370 /* Send NM_BVC_BLOCK.ind to NM */
371 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100372 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800373 nmp.bvci = bvci;
374 nmp.tp = tp;
375 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
376 PRIM_OP_INDICATION, msg);
377 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200378
379 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100380 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200381 bvci, msgb_bvci(msg));
382};
383
384static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
385{
Harald Welte15a36432012-06-17 12:16:31 +0800386 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100387 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200388 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200389
Harald Weltebfe62e52017-05-15 12:48:30 +0200390 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200391 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200392 /* 8.3.2: Signalling BVC shall never be blocked */
393 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
394 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100395 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200396 return 0;
397 }
Harald Welte25de8112010-05-13 21:26:28 +0200398
Harald Weltee9686b62010-05-31 18:07:17 +0200399 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200400
Max548caef2019-03-07 13:49:34 +0100401 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200402 if (!ptp_ctx)
403 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
404
405 ptp_ctx->state &= ~BVC_S_BLOCKED;
406
Harald Welte15a36432012-06-17 12:16:31 +0800407 /* Send NM_BVC_UNBLOCK.ind to NM */
408 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100409 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800410 nmp.bvci = bvci;
411 nmp.tp = tp;
412 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
413 PRIM_OP_INDICATION, msg);
414 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200415
416 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100417 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200418 bvci, msgb_bvci(msg));
419};
420
Harald Welte9ba50052010-03-14 15:45:01 +0800421/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200422static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200423 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800424{
Harald Welte15a36432012-06-17 12:16:31 +0800425 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200426 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800427
Harald Welte6752fa42010-05-02 09:23:16 +0200428 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200429 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800430
Harald Welte086fe322011-08-19 16:45:19 +0200431 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200432
Harald Welte9ba50052010-03-14 15:45:01 +0800433 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200434 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200435 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
436 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
437 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200438 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200439 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200440
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200441 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800442 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
443 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800444
Harald Welte15a36432012-06-17 12:16:31 +0800445 /* Send BSSGP_UL_UD.ind to NM */
446 memset(&gbp, 0, sizeof(gbp));
447 gbp.nsei = ctx->nsei;
448 gbp.bvci = ctx->bvci;
449 gbp.tlli = msgb_tlli(msg);
450 gbp.tp = tp;
451 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
452 PRIM_OP_INDICATION, msg);
453 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800454}
455
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200456static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800457{
Harald Welte15a36432012-06-17 12:16:31 +0800458 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200459 struct gprs_ra_id raid;
460 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100461 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200462 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800463
Harald Welte25de8112010-05-13 21:26:28 +0200464 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200465 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
466 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200467 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200468 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200469 }
Harald Welte9ba50052010-03-14 15:45:01 +0800470
Harald Weltebfe62e52017-05-15 12:48:30 +0200471 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200472
Harald Welte17925322010-05-31 20:18:35 +0200473 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200474 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200475
Harald Weltea8aa4df2010-05-30 22:00:53 +0200476 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
477
Harald Welte313cccf2010-06-09 11:22:47 +0200478 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800479 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100480 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200481 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800482 gbp.tlli = tlli;
483 gbp.ra_id = &raid;
484 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
485 PRIM_OP_REQUEST, msg);
486
487 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200488 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100489 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200490
Max548caef2019-03-07 13:49:34 +0100491 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200492
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800493 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800494}
495
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200496static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800497{
Harald Welte15a36432012-06-17 12:16:31 +0800498 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200499 struct gprs_ra_id raid;
500 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200501 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100502 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200503 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800504
Harald Welte25de8112010-05-13 21:26:28 +0200505 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
506 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200507 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
508 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200509 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200510 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200511 }
Harald Welte9ba50052010-03-14 15:45:01 +0800512
Harald Weltebfe62e52017-05-15 12:48:30 +0200513 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200514 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200515
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200516 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200517
Harald Weltea8aa4df2010-05-30 22:00:53 +0200518 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
519
Harald Welte313cccf2010-06-09 11:22:47 +0200520 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800521 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100522 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200523 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800524 gbp.tlli = tlli;
525 gbp.ra_id = &raid;
526 gbp.u.resume.suspend_ref = suspend_ref;
527 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
528 PRIM_OP_REQUEST, msg);
529
530 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200531 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100532 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200533 NULL);
534
Max548caef2019-03-07 13:49:34 +0100535 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800536 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800537}
538
Harald Weltee9686b62010-05-31 18:07:17 +0200539
540static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
541 struct bssgp_bvc_ctx *ctx)
542{
Harald Welte15a36432012-06-17 12:16:31 +0800543 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200544 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100545 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200546
547 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
548 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
549 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
550 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
551 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
552 "missing mandatory IE\n", ctx->bvci);
553 }
554
Harald Welteb7363142010-07-23 21:59:29 +0200555 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200556 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200557
Harald Welte086fe322011-08-19 16:45:19 +0200558 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200559 ctx->bvci, tlli);
560
561 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
562
Harald Welte15a36432012-06-17 12:16:31 +0800563 /* send NM_LLC_DISCARDED to NM */
564 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100565 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800566 nmp.bvci = ctx->bvci;
567 nmp.tlli = tlli;
568 nmp.tp = tp;
569 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
570 PRIM_OP_INDICATION, msg);
571
572 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200573}
574
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100575int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
576 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
577{
Max548caef2019-03-07 13:49:34 +0100578 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100579 struct osmo_bssgp_prim nmp;
580 enum gprs_bssgp_cause cause;
581
582 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
583 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
584 "missing mandatory IE\n", bvci);
585 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
586 } else {
587 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
588 }
589
590 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
591 bvci, bssgp_cause_str(cause));
592
593 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
594 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
595 LOGP(DBSSGP, LOGL_ERROR,
596 "BSSGP BVCI=%u Rx STATUS cause=%s "
597 "missing conditional BVCI IE\n",
598 bvci, bssgp_cause_str(cause));
599 }
600
601 if (bctx)
602 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
603
604 /* send NM_STATUS to NM */
605 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100606 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100607 nmp.bvci = bvci;
608 nmp.tp = tp;
609 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
610 PRIM_OP_INDICATION, msg);
611
612 return bssgp_prim_cb(&nmp.oph, NULL);
613}
614
615
Harald Welted11c0592012-09-06 21:57:11 +0200616/* One element (msgb) in a BSSGP Flow Control queue */
617struct bssgp_fc_queue_element {
618 /* linked list of queue elements */
619 struct llist_head list;
620 /* The message that we have enqueued */
621 struct msgb *msg;
622 /* Length of the LLC PDU part of the contained message */
623 uint32_t llc_pdu_len;
624 /* private pointer passed to the flow control out_cb function */
625 void *priv;
626};
627
628static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
629static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
630
631static void fc_timer_cb(void *data)
632{
633 struct bssgp_flow_control *fc = data;
634 struct bssgp_fc_queue_element *fcqe;
635 struct timeval time_now;
636
637 /* if the queue is empty, we return without sending something
638 * and without re-starting the timer */
639 if (llist_empty(&fc->queue))
640 return;
641
642 /* get the first entry from the queue */
643 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
644 list);
645
646 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
647 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
648 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
649 /* make sure we re-start the timer */
650 fc_queue_timer_cfg(fc);
651 return;
652 }
653
654 /* remove from the queue */
655 llist_del(&fcqe->list);
656
657 fc->queue_depth--;
658
659 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200660 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200661 fc->time_last_pdu = time_now;
662
663 /* call the output callback for this FC instance */
664 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
665
666 /* we expect that out_cb will in the end free the msgb once
667 * it is no longer needed */
668
669 /* but we have to free the queue element ourselves */
670 talloc_free(fcqe);
671
672 /* re-configure the timer for the next PDU */
673 fc_queue_timer_cfg(fc);
674}
675
676/* configure/schedule the flow control timer to expire once the bucket
677 * will have leaked a sufficient number of bytes to transmit the next
678 * PDU in the queue */
679static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
680{
681 struct bssgp_fc_queue_element *fcqe;
682 uint32_t msecs;
683
684 if (llist_empty(&fc->queue))
685 return 0;
686
Jacob Erlbeck97319352015-04-30 19:28:03 +0200687 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200688 list);
689
Harald Welte27b2bb72013-06-22 09:44:00 +0200690 if (fc->bucket_leak_rate != 0) {
691 /* Calculate the point in time at which we will have leaked
692 * a sufficient number of bytes from the bucket to transmit
693 * the first PDU in the queue */
694 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
695 /* FIXME: add that time to fc->time_last_pdu and subtract it from
696 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200697 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200698 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
699 } else {
700 /* If the PCU is telling us to not send any more data at all,
701 * there's no point starting a timer. */
702 }
Harald Welted11c0592012-09-06 21:57:11 +0200703
704 return 0;
705}
706
707/* Enqueue a PDU in the flow control queue for delayed transmission */
708static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
709 uint32_t llc_pdu_len, void *priv)
710{
711 struct bssgp_fc_queue_element *fcqe;
712
713 if (fc->queue_depth >= fc->max_queue_depth)
714 return -ENOSPC;
715
716 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
717 if (!fcqe)
718 return -ENOMEM;
719 fcqe->msg = msg;
720 fcqe->llc_pdu_len = llc_pdu_len;
721 fcqe->priv = priv;
722
723 llist_add_tail(&fcqe->list, &fc->queue);
724
725 fc->queue_depth++;
726
727 /* re-configure the timer for dequeueing the pdu */
728 fc_queue_timer_cfg(fc);
729
730 return 0;
731}
732
733/* According to Section 8.2 */
734static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
735{
736 struct timeval time_now, time_diff;
737 int64_t bucket_predicted;
738 uint32_t csecs_elapsed, leaked;
739
740 /* B' = B + L(p) - (Tc - Tp)*R */
741
742 /* compute number of centi-seconds that have elapsed since transmitting
743 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200744 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200745 timersub(&time_now, &fc->time_last_pdu, &time_diff);
746 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
747
748 /* compute number of bytes that have leaked in the elapsed number
749 * of centi-seconds */
750 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
751 /* add the current PDU length to the last bucket level */
752 bucket_predicted = fc->bucket_counter + pdu_len;
753 /* ... and subtract the number of leaked bytes */
754 bucket_predicted -= leaked;
755
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700756 if (bucket_predicted < pdu_len)
757 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200758
759 if (bucket_predicted <= fc->bucket_size_max) {
760 /* the bucket is not full yet, we can pass the packet */
761 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700762 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200763 }
764
765 /* bucket is full, PDU needs to be delayed */
766 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200767}
768
769/* output callback for BVC flow control */
770static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
771 uint32_t llc_pdu_len, void *priv)
772{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200773 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200774}
775
776/* input function of the flow control implementation, called first
777 * for the MM flow control, and then as the MM flow control output
778 * callback in order to perform BVC flow control */
779int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
780 uint32_t llc_pdu_len, void *priv)
781{
782 struct timeval time_now;
783
Harald Weltebb826222012-09-07 10:22:01 +0200784 if (llc_pdu_len > fc->bucket_size_max) {
785 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
786 "than maximum bucket size (%u)!\n", llc_pdu_len,
787 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200788 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200789 return -EIO;
790 }
791
Harald Welted11c0592012-09-06 21:57:11 +0200792 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100793 int rc;
794 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
795 if (rc)
796 msgb_free(msg);
797 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200798 } else {
799 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200800 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200801 fc->time_last_pdu = time_now;
802 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
803 }
804}
805
Harald Weltebb826222012-09-07 10:22:01 +0200806
807/* Initialize the Flow Control structure */
808void bssgp_fc_init(struct bssgp_flow_control *fc,
809 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
810 uint32_t max_queue_depth,
811 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
812 uint32_t llc_pdu_len, void *priv))
813{
814 fc->out_cb = out_cb;
815 fc->bucket_size_max = bucket_size_max;
816 fc->bucket_leak_rate = bucket_leak_rate;
817 fc->max_queue_depth = max_queue_depth;
818 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200819 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200820}
821
Harald Welted11c0592012-09-06 21:57:11 +0200822/* Initialize the Flow Control parameters for a new MS according to
823 * default values for the BVC specified by BVCI and NSEI */
824int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200825 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200826{
827 struct bssgp_bvc_ctx *ctx;
828
829 ctx = btsctx_by_bvci_nsei(bvci, nsei);
830 if (!ctx)
831 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200832
833 /* output call-back of per-MS FC is per-CTX FC */
834 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
835 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200836
837 return 0;
838}
839
Harald Welte25de8112010-05-13 21:26:28 +0200840static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200841 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800842{
Harald Welte27b2bb72013-06-22 09:44:00 +0200843 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
844 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800845
Harald Weltee9686b62010-05-31 18:07:17 +0200846 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
847 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800848
849 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
850 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
851 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
852 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200853 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
854 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
855 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800856 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200857 }
Harald Welte9ba50052010-03-14 15:45:01 +0800858
Harald Weltebb826222012-09-07 10:22:01 +0200859 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200860 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200861 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200862 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200863 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200864 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200865 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200866 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200867
Harald Welte27b2bb72013-06-22 09:44:00 +0200868 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
869 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
870 "rate of 0, stopping all DL GPRS!\n");
871 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
872 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
873 "rate of != 0, restarting all DL GPRS!\n");
874
875 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
876 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
877 "bucket leak rate of 0, stopping DL GPRS!\n");
878 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
879 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
880 "bucket leak rate != 0, restarting DL GPRS!\n");
881
882 /* reconfigure the timer for flow control based on new values */
883 fc_queue_timer_cfg(bctx->fc);
884
Harald Welte9ba50052010-03-14 15:45:01 +0800885 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200886 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200887 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800888}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200889
Harald Welte25de8112010-05-13 21:26:28 +0200890/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800891static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
892 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800893{
Harald Welteec19c102010-05-02 09:50:42 +0200894 struct bssgp_normal_hdr *bgph =
895 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200896 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800897 int rc = 0;
898
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100899 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
900
Harald Welte58e65c92010-05-13 21:45:23 +0200901 /* If traffic is received on a BVC that is marked as blocked, the
902 * received PDU shall not be accepted and a STATUS PDU (Cause value:
903 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100904 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200905 uint16_t bvci = msgb_bvci(msg);
906 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
907 }
908
Harald Welte9ba50052010-03-14 15:45:01 +0800909 switch (pdu_type) {
910 case BSSGP_PDUT_UL_UNITDATA:
911 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200912 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800913 break;
914 case BSSGP_PDUT_RA_CAPABILITY:
915 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200916 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
917 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200918 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800919 /* FIXME: send RA_CAPA_UPDATE_ACK */
920 break;
921 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200922 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800923 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200924 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800925 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800926 case BSSGP_PDUT_FLOW_CONTROL_BVC:
927 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200928 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800929 break;
930 case BSSGP_PDUT_FLOW_CONTROL_MS:
931 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200932 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
933 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200934 /* FIXME: actually implement flow control */
935 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800936 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800937 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100938 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100939 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800940 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
941 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
942 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
943 case BSSGP_PDUT_MODIFY_BSS_PFC:
944 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100945 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
946 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200947 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800948 break;
949 /* those only exist in the SGSN -> BSS direction */
950 case BSSGP_PDUT_DL_UNITDATA:
951 case BSSGP_PDUT_PAGING_PS:
952 case BSSGP_PDUT_PAGING_CS:
953 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200954 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
955 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100956 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
957 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200958 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
959 rc = -EINVAL;
960 break;
961 default:
Max2c34ab42016-03-17 15:42:26 +0100962 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
963 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200964 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
965 break;
966 }
967
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800968 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200969}
970
971/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800972static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
973 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200974{
975 struct bssgp_normal_hdr *bgph =
976 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
977 uint8_t pdu_type = bgph->pdu_type;
978 int rc = 0;
979 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200980 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200981
982 switch (bgph->pdu_type) {
983 case BSSGP_PDUT_SUSPEND:
984 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200985 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200986 break;
987 case BSSGP_PDUT_RESUME:
988 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200989 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200990 break;
991 case BSSGP_PDUT_FLUSH_LL_ACK:
992 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200993 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200994 /* FIXME: send NM_FLUSH_LL.res to NM */
995 break;
996 case BSSGP_PDUT_LLC_DISCARD:
997 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200998 if (!bctx) {
999 LOGP(DBSSGP, LOGL_ERROR,
1000 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
1001 goto err_mand_ie;
1002 }
Harald Weltee9686b62010-05-31 18:07:17 +02001003 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001004 break;
1005 case BSSGP_PDUT_BVC_BLOCK:
1006 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +02001007 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001008 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1009 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
1010 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001011 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001012 }
Harald Welte2677ea52010-05-31 17:16:36 +02001013 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001014 break;
1015 case BSSGP_PDUT_BVC_UNBLOCK:
1016 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +02001017 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
1018 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
1019 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001020 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001021 }
Harald Welte25de8112010-05-13 21:26:28 +02001022 rc = bssgp_rx_bvc_unblock(msg, tp);
1023 break;
Max590c4022017-06-28 14:29:24 +02001024 case BSSGP_PDUT_BVC_RESET_ACK:
1025 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
1026 break;
Harald Welte25de8112010-05-13 21:26:28 +02001027 case BSSGP_PDUT_BVC_RESET:
1028 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +02001029 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001030 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1031 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1032 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001033 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001034 }
Harald Welte25de8112010-05-13 21:26:28 +02001035 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1036 break;
1037 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001038 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001039 break;
1040 /* those only exist in the SGSN -> BSS direction */
1041 case BSSGP_PDUT_PAGING_PS:
1042 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001043 case BSSGP_PDUT_SUSPEND_ACK:
1044 case BSSGP_PDUT_SUSPEND_NACK:
1045 case BSSGP_PDUT_RESUME_ACK:
1046 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001047 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001048 case BSSGP_PDUT_BVC_BLOCK_ACK:
1049 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1050 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001051 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1052 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001053 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001054 rc = -EINVAL;
1055 break;
1056 default:
Max2c34ab42016-03-17 15:42:26 +01001057 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1058 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001059 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001060 break;
1061 }
1062
1063 return rc;
1064err_mand_ie:
1065 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1066}
1067
Harald Welte25de8112010-05-13 21:26:28 +02001068/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001069int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001070{
1071 struct bssgp_normal_hdr *bgph =
1072 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1073 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1074 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001075 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001076 uint8_t pdu_type = bgph->pdu_type;
1077 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001078 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001079 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001080 int data_len;
1081 int rc = 0;
1082
1083 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1084
1085 /* UNITDATA BSSGP headers have TLLI in front */
1086 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1087 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1088 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1089 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1090 } else {
1091 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1092 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1093 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001094 if (rc < 0) {
1095 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
1096 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001097 if (pdu_type != BSSGP_PDUT_STATUS)
1098 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001099 return rc;
1100 }
Harald Welte25de8112010-05-13 21:26:28 +02001101
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001102 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001103 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001104
Harald Welte25de8112010-05-13 21:26:28 +02001105 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001106 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001107
Harald Welte16c8dbb2010-05-17 23:30:01 +02001108 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001109 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001110 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1111 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1112 msgb_bssgp_len(msg));
1113 }
1114
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001115 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1116 * are otherwise unexpected */
1117 if (pdu_type == BSSGP_PDUT_STATUS)
1118 /* Some exception has occurred */
1119 return bssgp_rx_status(msg, &tp, bvci, bctx);
1120
1121 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1122 * registered if a BVCI is given. */
1123 if (!bctx && bvci != BVCI_SIGNALLING &&
1124 pdu_type != BSSGP_PDUT_BVC_RESET) {
Max548caef2019-03-07 13:49:34 +01001125 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001126 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001127 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1128 }
1129
Harald Welte61c07842010-05-18 11:57:08 +02001130 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001131 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001132 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001133 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001134 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001135 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001136 else
1137 LOGP(DBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001138 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1139 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001140
1141 return rc;
1142}
1143
Harald Weltede4599c2012-06-17 13:04:02 +08001144int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1145 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001146{
Harald Welte8a521132010-05-17 22:59:29 +02001147 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001148 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001149 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001150 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001151 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001152 uint16_t bvci = msgb_bvci(msg);
1153 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001154 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001155 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001156
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001157 OSMO_ASSERT(dup != NULL);
1158
Harald Welte30bc19a2010-05-02 11:19:37 +02001159 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001160 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001161 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001162 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001163 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001164 return -EINVAL;
1165 }
1166
1167 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001168 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001169 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1170 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001171 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001172 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001173 }
Harald Welte9ba50052010-03-14 15:45:01 +08001174
1175 if (msg->len > TVLV_MAX_ONEBYTE)
1176 llc_pdu_tlv_hdr_len += 1;
1177
1178 /* prepend the tag and length of the LLC-PDU TLV */
1179 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1180 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1181 if (llc_pdu_tlv_hdr_len > 2) {
1182 llc_pdu_tlv[1] = msg_len >> 8;
1183 llc_pdu_tlv[2] = msg_len & 0xff;
1184 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001185 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001186 llc_pdu_tlv[1] |= 0x80;
1187 }
1188
Harald Welte2f946832010-05-31 22:12:30 +02001189 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1190
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001191 /* Old TLLI to help BSS map from old->new */
1192 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001193 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001194 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001195 }
Harald Welte9ba50052010-03-14 15:45:01 +08001196
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001197 /* IMSI */
1198 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001199 uint8_t mi[GSM48_MID_MAX_SIZE];
1200/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1201 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1202 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1203 * mi[131], which is wrong */
1204#pragma GCC diagnostic push
1205#pragma GCC diagnostic ignored "-Warray-bounds"
1206 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1207 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1208 if (imsi_len > 2)
1209 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1210 imsi_len-2, mi+2);
1211#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001212 }
1213
1214 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001215 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001216 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1217 (uint8_t *) &drx_params);
1218
1219 /* FIXME: Priority */
1220
1221 /* MS Radio Access Capability */
1222 if (dup->ms_ra_cap.len)
1223 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1224 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1225
Harald Welte9ba50052010-03-14 15:45:01 +08001226 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001227 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001228
1229 /* prepend the QoS profile, TLLI and pdu type */
1230 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001231 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001232 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001233 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1234
Harald Welte16c8dbb2010-05-17 23:30:01 +02001235 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1236 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1237
Harald Welte30bc19a2010-05-02 11:19:37 +02001238 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001239
Harald Welted11c0592012-09-06 21:57:11 +02001240 /* check if we have to go through per-ms flow control or can go
1241 * directly to the per-BSS flow control */
1242 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001243 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001244 else
Harald Welted8b47692012-09-07 11:29:32 +02001245 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001246}
Harald Welte68b4f032010-06-09 16:22:28 +02001247
1248/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001249int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1250 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001251{
1252 struct msgb *msg = bssgp_msgb_alloc();
1253 struct bssgp_normal_hdr *bgph =
1254 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001255 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001256 uint8_t mi[GSM48_MID_MAX_SIZE];
1257 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001258 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001259
1260 if (imsi_len < 2)
1261 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001262
1263 msgb_nsei(msg) = nsei;
1264 msgb_bvci(msg) = ns_bvci;
1265
1266 if (pinfo->mode == BSSGP_PAGING_PS)
1267 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1268 else
1269 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1270 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001271/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1272 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1273 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1274 * mi[131], which is wrong */
1275#pragma GCC diagnostic push
1276#pragma GCC diagnostic ignored "-Warray-bounds"
1277 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1278 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1279#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001280 /* DRX Parameters */
1281 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1282 (uint8_t *) &drx_params);
1283 /* Scope */
1284 switch (pinfo->scope) {
1285 case BSSGP_PAGING_BSS_AREA:
1286 {
1287 uint8_t null = 0;
1288 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1289 }
1290 break;
1291 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001292 gsm48_encode_ra(&ra, &pinfo->raid);
1293 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001294 break;
1295 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001296 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001297 break;
1298 case BSSGP_PAGING_BVCI:
1299 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001300 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001301 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1302 }
1303 break;
1304 }
1305 /* QoS profile mandatory for PS */
1306 if (pinfo->mode == BSSGP_PAGING_PS)
1307 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1308
1309 /* Optional (P-)TMSI */
1310 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001311 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001312 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1313 }
1314
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001315 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001316}
Harald Weltecca49632012-06-16 17:45:59 +08001317
Harald Weltede4599c2012-06-17 13:04:02 +08001318void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001319{
1320 DBSSGP = ss;
1321}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001322
1323/*!
1324 * \brief Flush the queue of the bssgp_flow_control
1325 * \param[in] The flow control object which holds the queue.
1326 */
1327void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1328{
1329 struct bssgp_fc_queue_element *element, *tmp;
1330
1331 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1332 msgb_free(element->msg);
1333 llist_del(&element->list);
1334 talloc_free(element);
1335 }
1336}
1337
1338/*!
1339 * \brief Flush the queues of all BSSGP contexts.
1340 */
1341void bssgp_flush_all_queues()
1342{
1343 struct bssgp_bvc_ctx *bctx;
1344
1345 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1346 if (bctx->fc)
1347 bssgp_fc_flush_queue(bctx->fc);
1348 }
1349}