blob: 14a5e9de7a9ae5786e9556026ce88cfbe6839b22 [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
Daniel Willmann2d42b902020-09-26 09:11:05 +020043#include "osmocom/gsm/gsm48.h"
Philipp Maier4d400472021-01-26 12:57:13 +010044#include "gprs_bssgp_internal.h"
Harald Weltecca49632012-06-16 17:45:59 +080045
Harald Welte6752fa42010-05-02 09:23:16 +020046void *bssgp_tall_ctx = NULL;
47
Alexander Couzens85a8fd32020-07-18 15:57:07 +020048static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg);
49
50bssgp_bvc_send bssgp_ns_send = _gprs_ns_sendmsg;
51void *bssgp_ns_send_data = NULL;
52
Harald Welte25de8112010-05-13 21:26:28 +020053static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080054 { "packets:in", "Packets at BSSGP Level ( In)" },
55 { "packets:out","Packets at BSSGP Level (Out)" },
56 { "bytes:in", "Bytes at BSSGP Level ( In)" },
57 { "bytes:out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020058 { "blocked", "BVC Blocking count" },
59 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010060 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020061};
62
63static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080064 .group_name_prefix = "bssgp:bss_ctx",
Harald Welte25de8112010-05-13 21:26:28 +020065 .group_description = "BSSGP Peer Statistics",
66 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
67 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010068 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020069};
70
Harald Weltea78b9c22010-05-17 23:02:42 +020071LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020072
Harald Welted11c0592012-09-06 21:57:11 +020073static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
74 uint32_t llc_pdu_len, void *priv);
75
Alexander Couzens85a8fd32020-07-18 15:57:07 +020076
Alexander Couzens83fb6862020-09-03 19:30:08 +020077/* callback to be backward compatible with old users which do not set the bssgp_ns_send function */
Alexander Couzens85a8fd32020-07-18 15:57:07 +020078static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg)
79{
80 return gprs_ns_sendmsg(bssgp_nsi, msg);
81}
82
Harald Welte6752fa42010-05-02 09:23:16 +020083/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020084struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020085{
Harald Welte8a521132010-05-17 22:59:29 +020086 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020087
Harald Weltea78b9c22010-05-17 23:02:42 +020088 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020089 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
90 bctx->cell_id == cid)
91 return bctx;
92 }
93 return NULL;
94}
95
Alexander Couzens7819ff62021-02-17 02:30:33 +010096/* Transmit a BVC-RESET or BVC-RESET-ACK with a given nsei and bvci (Chapter 10.4.12)
97 * \param[in] pdu Either BSSGP_PDUT_BVC_RESET or BSSGP_PDUT_BVC_RESET_ACK
Daniel Willmann2d42b902020-09-26 09:11:05 +020098 * \param[in] nsei The NSEI to transmit over
99 * \param[in] bvci BVCI of the BVC to reset
Alexander Couzens7819ff62021-02-17 02:30:33 +0100100 * \param[in] cause The cause of the reset only valid for BSSGP_PDUT_BVC_RESET.
Daniel Willmann2d42b902020-09-26 09:11:05 +0200101 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
102 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
Alexander Couzens7819ff62021-02-17 02:30:33 +0100103 * returns >= 0 on success, on error < 0.
Daniel Willmann2d42b902020-09-26 09:11:05 +0200104 */
Alexander Couzens7819ff62021-02-17 02:30:33 +0100105static int tx_bvc_reset_nsei_bvci(enum bssgp_pdu_type pdu, uint16_t nsei, uint16_t bvci,
106 enum gprs_bssgp_cause cause, const struct gprs_ra_id *ra_id, uint16_t cell_id)
Daniel Willmann2d42b902020-09-26 09:11:05 +0200107{
108 struct msgb *msg = bssgp_msgb_alloc();
109 struct bssgp_normal_hdr *bgph =
110 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
111 uint16_t _bvci = osmo_htons(bvci);
112
Alexander Couzens7819ff62021-02-17 02:30:33 +0100113 OSMO_ASSERT(pdu == BSSGP_PDUT_BVC_RESET || pdu == BSSGP_PDUT_BVC_RESET_ACK);
114
Daniel Willmann2d42b902020-09-26 09:11:05 +0200115 msgb_nsei(msg) = nsei;
Alexander Couzens5baa1412021-02-19 19:29:04 +0100116 msgb_bvci(msg) = BVCI_SIGNALLING;
Alexander Couzens7819ff62021-02-17 02:30:33 +0100117 bgph->pdu_type = pdu;
118
Daniel Willmann2d42b902020-09-26 09:11:05 +0200119 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
Alexander Couzens7819ff62021-02-17 02:30:33 +0100120
121 if (pdu == BSSGP_PDUT_BVC_RESET) {
122 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, (uint8_t *) &cause);
123 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
124 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
125 } else {
126 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET-ACK\n", bvci);
127 }
128
Daniel Willmann2d42b902020-09-26 09:11:05 +0200129 if (ra_id) {
130 uint8_t bssgp_cid[8];
131 bssgp_create_cell_id(bssgp_cid, ra_id, cell_id);
132 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
133 }
134
135 /* Optional: Feature Bitmap */
136
137 return bssgp_ns_send(bssgp_ns_send_data, msg);
138}
139
Alexander Couzens7819ff62021-02-17 02:30:33 +0100140/*! Transmit a BVC-RESET message with a given nsei and bvci (Chapter 10.4.12)
141 * \param[in] nsei The NSEI to transmit over
142 * \param[in] bvci BVCI of the BVC to reset
143 * \param[in] cause The cause of the reset
144 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
145 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
146 * returns >= 0 on success, on error < 0.
147 */
148int bssgp_tx_bvc_reset_nsei_bvci(uint16_t nsei, uint16_t bvci, enum gprs_bssgp_cause cause, const struct gprs_ra_id *ra_id, uint16_t cell_id)
149{
150 return tx_bvc_reset_nsei_bvci(BSSGP_PDUT_BVC_RESET, nsei, bvci, cause, ra_id, cell_id);
151}
152
153/*! Transmit a BVC-RESET-ACK message with a given nsei and bvci (Chapter 10.4.12)
154 * \param[in] nsei The NSEI to transmit over
155 * \param[in] bvci BVCI of the BVC to reset
156 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
157 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
158 * returns >= 0 on success, on error < 0.
159 */
160int bssgp_tx_bvc_reset_ack_nsei_bvci(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, uint16_t cell_id)
161{
162 return tx_bvc_reset_nsei_bvci(BSSGP_PDUT_BVC_RESET_ACK, nsei, bvci, 0, ra_id, cell_id);
163}
164
Max8b8938f2017-06-29 19:48:29 +0200165/*! Initiate reset procedure for all PTP BVC on a given NSEI.
166 *
167 * This function initiates reset procedure for all PTP BVC with a given cause.
168 * \param[in] nsei NSEI to which PTP BVC should belong to
169 * \param[in] cause Cause of BVC RESET
170 * \returns 0 on success, negative error code otherwise
171 */
172int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
173{
174 int rc;
175 struct bssgp_bvc_ctx *bctx;
176
177 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
178 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100179 LOGP(DLBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
Max8b8938f2017-06-29 19:48:29 +0200180 nsei, bctx->bvci, bssgp_cause_str(cause));
181 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
182 if (rc < 0)
183 return rc;
184 }
185 }
186
187 return 0;
188}
189
Harald Welte6752fa42010-05-02 09:23:16 +0200190/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200191struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200192{
Harald Welte8a521132010-05-17 22:59:29 +0200193 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200194
Harald Weltea78b9c22010-05-17 23:02:42 +0200195 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200196 if (bctx->nsei == nsei && bctx->bvci == bvci)
197 return bctx;
198 }
199 return NULL;
200}
201
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200202void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data)
203{
204 bssgp_ns_send = ns_send;
205 bssgp_ns_send_data = data;
206}
207
Harald Welte8a521132010-05-17 22:59:29 +0200208struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200209{
Harald Welte8a521132010-05-17 22:59:29 +0200210 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200211
Harald Welte8a521132010-05-17 22:59:29 +0200212 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200213 if (!ctx)
214 return NULL;
215 ctx->bvci = bvci;
216 ctx->nsei = nsei;
Alexander Couzens7819ff62021-02-17 02:30:33 +0100217 ctx->is_sgsn = true;
Harald Welte25de8112010-05-13 21:26:28 +0200218 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
219 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200220 if (!ctx->ctrg)
221 goto err_ctrg;
222
Harald Welted8b47692012-09-07 11:29:32 +0200223 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200224 if (!ctx->fc)
225 goto err_fc;
226
Harald Welted8b47692012-09-07 11:29:32 +0200227 /* cofigure for 2Mbit, 30 packets in queue */
228 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200229
Harald Weltea78b9c22010-05-17 23:02:42 +0200230 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200231
232 return ctx;
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200233
234err_fc:
235 rate_ctr_group_free(ctx->ctrg);
236err_ctrg:
237 talloc_free(ctx);
238 return NULL;
Harald Welte6752fa42010-05-02 09:23:16 +0200239}
240
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700241void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx)
242{
243 if (!ctx)
244 return;
Alexander Couzens495b4a72020-09-16 23:10:03 +0200245
246 osmo_timer_del(&ctx->fc->timer);
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700247 rate_ctr_group_free(ctx->ctrg);
248 llist_del(&ctx->list);
249 talloc_free(ctx);
250}
251
Harald Welte9ba50052010-03-14 15:45:01 +0800252/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200253static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800254{
255 struct msgb *msg = bssgp_msgb_alloc();
256 struct bssgp_normal_hdr *bgph =
257 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
258
Harald Welte24a655f2010-04-30 19:54:29 +0200259 msgb_nsei(msg) = nsei;
260 msgb_bvci(msg) = ns_bvci;
261
Harald Welte9ba50052010-03-14 15:45:01 +0800262 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
263 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
264
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200265 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800266}
267
Harald Weltea8aa4df2010-05-30 22:00:53 +0200268/* 10.3.7 SUSPEND-ACK PDU */
269int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
270 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
271{
272 struct msgb *msg = bssgp_msgb_alloc();
273 struct bssgp_normal_hdr *bgph =
274 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200275
276 msgb_nsei(msg) = nsei;
277 msgb_bvci(msg) = 0; /* Signalling */
278 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
279
Maxe29ec852018-01-05 14:30:22 +0100280 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100281 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200282 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
283
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200284 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200285}
286
287/* 10.3.8 SUSPEND-NACK PDU */
288int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100289 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200290 uint8_t *cause)
291{
292 struct msgb *msg = bssgp_msgb_alloc();
293 struct bssgp_normal_hdr *bgph =
294 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200295
296 msgb_nsei(msg) = nsei;
297 msgb_bvci(msg) = 0; /* Signalling */
298 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
299
Maxe29ec852018-01-05 14:30:22 +0100300 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100301 bssgp_msgb_ra_put(msg, ra_id);
302
Harald Weltea8aa4df2010-05-30 22:00:53 +0200303 if (cause)
304 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
305
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200306 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200307}
308
309/* 10.3.10 RESUME-ACK PDU */
310int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
311 const struct gprs_ra_id *ra_id)
312{
313 struct msgb *msg = bssgp_msgb_alloc();
314 struct bssgp_normal_hdr *bgph =
315 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200316
317 msgb_nsei(msg) = nsei;
318 msgb_bvci(msg) = 0; /* Signalling */
319 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
320
Maxe29ec852018-01-05 14:30:22 +0100321 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100322 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200323
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200324 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200325}
326
327/* 10.3.11 RESUME-NACK PDU */
328int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
329 const struct gprs_ra_id *ra_id, uint8_t *cause)
330{
331 struct msgb *msg = bssgp_msgb_alloc();
332 struct bssgp_normal_hdr *bgph =
333 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200334
335 msgb_nsei(msg) = nsei;
336 msgb_bvci(msg) = 0; /* Signalling */
337 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
338
Maxe29ec852018-01-05 14:30:22 +0100339 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100340 bssgp_msgb_ra_put(msg, ra_id);
341
Harald Weltea8aa4df2010-05-30 22:00:53 +0200342 if (cause)
343 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
344
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200345 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200346}
347
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200348uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200349{
350 /* 6 octets RAC */
351 gsm48_parse_ra(raid, buf);
352 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200353 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200354}
355
Harald Welte28610072011-11-24 21:32:07 +0100356int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
357 uint16_t cid)
358{
Harald Welte28610072011-11-24 21:32:07 +0100359 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100360 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100361 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200362 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100363
364 return 8;
365}
366
Harald Welte3fddf3c2010-05-01 16:48:27 +0200367/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200368static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200369 uint16_t ns_bvci)
370{
Harald Welte15a36432012-06-17 12:16:31 +0800371 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200372 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200373 uint16_t nsei = msgb_nsei(msg);
374 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200375
Harald Weltebfe62e52017-05-15 12:48:30 +0200376 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltefde19ed2020-12-07 21:43:51 +0100377 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200378 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
379
Harald Welte6752fa42010-05-02 09:23:16 +0200380 /* look-up or create the BTS context for this BVC */
381 bctx = btsctx_by_bvci_nsei(bvci, nsei);
382 if (!bctx)
383 bctx = btsctx_alloc(bvci, nsei);
384
Harald Welte25de8112010-05-13 21:26:28 +0200385 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
386 bctx->state &= ~BVC_S_BLOCKED;
387
Harald Welte3fddf3c2010-05-01 16:48:27 +0200388 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
389 * informs us about its RAC + Cell ID, so we can create a mapping */
Alexander Couzens5baa1412021-02-19 19:29:04 +0100390 if (bctx->is_sgsn && bvci != BVCI_SIGNALLING && bvci != BVCI_PTM) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100391 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100392 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200393 "missing mandatory IE\n", bvci);
394 return -EINVAL;
395 }
396 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200397 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
398 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Weltefde19ed2020-12-07 21:43:51 +0100399 LOGP(DLBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100400 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200401 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200402
Alexander Couzens7819ff62021-02-17 02:30:33 +0100403 /* Acknowledge the RESET to the BTS */
Alexander Couzens5baa1412021-02-19 19:29:04 +0100404 if (bvci == BVCI_SIGNALLING || bvci == BVCI_PTM || bctx->is_sgsn)
Alexander Couzens7819ff62021-02-17 02:30:33 +0100405 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
406 nsei, bvci, ns_bvci);
407 else
408 bssgp_tx_bvc_reset_ack_nsei_bvci(nsei, bvci, &bctx->ra_id, bctx->cell_id);
409
Harald Welte15a36432012-06-17 12:16:31 +0800410 /* Send NM_BVC_RESET.ind to NM */
411 memset(&nmp, 0, sizeof(nmp));
412 nmp.nsei = nsei;
413 nmp.bvci = bvci;
414 nmp.tp = tp;
415 nmp.ra_id = &bctx->ra_id;
416 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
417 PRIM_OP_INDICATION, msg);
418 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200419 return 0;
420}
421
Harald Welte25de8112010-05-13 21:26:28 +0200422static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
423{
Harald Welte15a36432012-06-17 12:16:31 +0800424 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100425 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200426 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200427
Harald Weltebfe62e52017-05-15 12:48:30 +0200428 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200429 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200430 /* 8.3.2: Signalling BVC shall never be blocked */
Harald Weltefde19ed2020-12-07 21:43:51 +0100431 LOGP(DLBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
Harald Welte58e65c92010-05-13 21:45:23 +0200432 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100433 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200434 return 0;
435 }
Harald Welte25de8112010-05-13 21:26:28 +0200436
Harald Weltefde19ed2020-12-07 21:43:51 +0100437 LOGP(DLBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200438
Max548caef2019-03-07 13:49:34 +0100439 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200440 if (!ptp_ctx)
441 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
442
443 ptp_ctx->state |= BVC_S_BLOCKED;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200444 rate_ctr_inc(rate_ctr_group_get_ctr(ptp_ctx->ctrg, BSSGP_CTR_BLOCKED));
Harald Welte25de8112010-05-13 21:26:28 +0200445
Harald Welte15a36432012-06-17 12:16:31 +0800446 /* Send NM_BVC_BLOCK.ind to NM */
447 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100448 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800449 nmp.bvci = bvci;
450 nmp.tp = tp;
451 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
452 PRIM_OP_INDICATION, msg);
453 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200454
455 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100456 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200457 bvci, msgb_bvci(msg));
458};
459
460static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
461{
Harald Welte15a36432012-06-17 12:16:31 +0800462 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100463 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200464 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200465
Harald Weltebfe62e52017-05-15 12:48:30 +0200466 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200467 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200468 /* 8.3.2: Signalling BVC shall never be blocked */
Harald Weltefde19ed2020-12-07 21:43:51 +0100469 LOGP(DLBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
Harald Welte58e65c92010-05-13 21:45:23 +0200470 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100471 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200472 return 0;
473 }
Harald Welte25de8112010-05-13 21:26:28 +0200474
Harald Weltefde19ed2020-12-07 21:43:51 +0100475 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200476
Max548caef2019-03-07 13:49:34 +0100477 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200478 if (!ptp_ctx)
479 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
480
481 ptp_ctx->state &= ~BVC_S_BLOCKED;
482
Harald Welte15a36432012-06-17 12:16:31 +0800483 /* Send NM_BVC_UNBLOCK.ind to NM */
484 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100485 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800486 nmp.bvci = bvci;
487 nmp.tp = tp;
488 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
489 PRIM_OP_INDICATION, msg);
490 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200491
492 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100493 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200494 bvci, msgb_bvci(msg));
495};
496
Harald Welte9ba50052010-03-14 15:45:01 +0800497/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200498static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200499 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800500{
Harald Welte15a36432012-06-17 12:16:31 +0800501 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200502 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800503
Harald Welte6752fa42010-05-02 09:23:16 +0200504 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200505 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800506
Harald Weltefde19ed2020-12-07 21:43:51 +0100507 DEBUGP(DLBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200508
Harald Welte9ba50052010-03-14 15:45:01 +0800509 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte2d9ce712020-12-03 15:53:59 +0100510 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200511 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100512 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
Harald Weltee9686b62010-05-31 18:07:17 +0200513 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200514 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200515 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200516
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200517 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800518 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
519 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800520
Harald Welte15a36432012-06-17 12:16:31 +0800521 /* Send BSSGP_UL_UD.ind to NM */
522 memset(&gbp, 0, sizeof(gbp));
523 gbp.nsei = ctx->nsei;
524 gbp.bvci = ctx->bvci;
525 gbp.tlli = msgb_tlli(msg);
526 gbp.tp = tp;
527 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
528 PRIM_OP_INDICATION, msg);
529 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800530}
531
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200532static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800533{
Harald Welte15a36432012-06-17 12:16:31 +0800534 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200535 struct gprs_ra_id raid;
536 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100537 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200538 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800539
Harald Welte2d9ce712020-12-03 15:53:59 +0100540 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
541 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100542 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200543 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200544 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200545 }
Harald Welte9ba50052010-03-14 15:45:01 +0800546
Harald Weltebfe62e52017-05-15 12:48:30 +0200547 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200548
Harald Weltefde19ed2020-12-07 21:43:51 +0100549 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200550 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200551
Harald Weltea8aa4df2010-05-30 22:00:53 +0200552 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
553
Harald Welte313cccf2010-06-09 11:22:47 +0200554 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800555 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100556 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200557 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800558 gbp.tlli = tlli;
559 gbp.ra_id = &raid;
560 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
561 PRIM_OP_REQUEST, msg);
562
563 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200564 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100565 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200566
Max548caef2019-03-07 13:49:34 +0100567 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200568
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800569 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800570}
571
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200572static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800573{
Harald Welte15a36432012-06-17 12:16:31 +0800574 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200575 struct gprs_ra_id raid;
576 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200577 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100578 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200579 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800580
Harald Welte2d9ce712020-12-03 15:53:59 +0100581 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4 ) ||
582 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6) ||
583 !TLVP_PRES_LEN(tp, BSSGP_IE_SUSPEND_REF_NR, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100584 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200585 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200586 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200587 }
Harald Welte9ba50052010-03-14 15:45:01 +0800588
Harald Weltebfe62e52017-05-15 12:48:30 +0200589 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200590 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200591
Harald Weltefde19ed2020-12-07 21:43:51 +0100592 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200593
Harald Weltea8aa4df2010-05-30 22:00:53 +0200594 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
595
Harald Welte313cccf2010-06-09 11:22:47 +0200596 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800597 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100598 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200599 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800600 gbp.tlli = tlli;
601 gbp.ra_id = &raid;
602 gbp.u.resume.suspend_ref = suspend_ref;
603 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
604 PRIM_OP_REQUEST, msg);
605
606 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200607 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100608 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200609 NULL);
610
Max548caef2019-03-07 13:49:34 +0100611 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800612 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800613}
614
Harald Weltee9686b62010-05-31 18:07:17 +0200615
616static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
617 struct bssgp_bvc_ctx *ctx)
618{
Harald Welte15a36432012-06-17 12:16:31 +0800619 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200620 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100621 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200622
Harald Welte2d9ce712020-12-03 15:53:59 +0100623 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
624 !TLVP_PRES_LEN(tp, BSSGP_IE_LLC_FRAMES_DISCARDED, 1) ||
625 !TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
626 !TLVP_PRES_LEN(tp, BSSGP_IE_NUM_OCT_AFF, 3)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100627 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
Harald Weltee9686b62010-05-31 18:07:17 +0200628 "missing mandatory IE\n", ctx->bvci);
Harald Welte2d9ce712020-12-03 15:53:59 +0100629 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200630 }
631
Harald Welte2d9ce712020-12-03 15:53:59 +0100632 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200633
Harald Weltefde19ed2020-12-07 21:43:51 +0100634 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200635 ctx->bvci, tlli);
636
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200637 rate_ctr_inc(rate_ctr_group_get_ctr(ctx->ctrg, BSSGP_CTR_DISCARDED));
Harald Weltee9686b62010-05-31 18:07:17 +0200638
Harald Welte15a36432012-06-17 12:16:31 +0800639 /* send NM_LLC_DISCARDED to NM */
640 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100641 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800642 nmp.bvci = ctx->bvci;
643 nmp.tlli = tlli;
644 nmp.tp = tp;
645 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
646 PRIM_OP_INDICATION, msg);
647
648 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200649}
650
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100651int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
652 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
653{
Max548caef2019-03-07 13:49:34 +0100654 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100655 struct osmo_bssgp_prim nmp;
656 enum gprs_bssgp_cause cause;
657
Harald Welte2d9ce712020-12-03 15:53:59 +0100658 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100659 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100660 "missing mandatory IE\n", bvci);
661 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
662 } else {
663 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
664 }
665
Harald Weltefde19ed2020-12-07 21:43:51 +0100666 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100667 bvci, bssgp_cause_str(cause));
668
669 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100670 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2))
Harald Weltefde19ed2020-12-07 21:43:51 +0100671 LOGP(DLBSSGP, LOGL_ERROR,
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100672 "BSSGP BVCI=%u Rx STATUS cause=%s "
673 "missing conditional BVCI IE\n",
674 bvci, bssgp_cause_str(cause));
675 }
676
677 if (bctx)
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200678 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_STATUS));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100679
680 /* send NM_STATUS to NM */
681 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100682 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100683 nmp.bvci = bvci;
684 nmp.tp = tp;
685 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
686 PRIM_OP_INDICATION, msg);
687
688 return bssgp_prim_cb(&nmp.oph, NULL);
689}
690
Harald Welted11c0592012-09-06 21:57:11 +0200691/* One element (msgb) in a BSSGP Flow Control queue */
692struct bssgp_fc_queue_element {
693 /* linked list of queue elements */
694 struct llist_head list;
695 /* The message that we have enqueued */
696 struct msgb *msg;
697 /* Length of the LLC PDU part of the contained message */
698 uint32_t llc_pdu_len;
699 /* private pointer passed to the flow control out_cb function */
700 void *priv;
701};
702
703static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
704static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
705
706static void fc_timer_cb(void *data)
707{
708 struct bssgp_flow_control *fc = data;
709 struct bssgp_fc_queue_element *fcqe;
710 struct timeval time_now;
711
712 /* if the queue is empty, we return without sending something
713 * and without re-starting the timer */
714 if (llist_empty(&fc->queue))
715 return;
716
717 /* get the first entry from the queue */
718 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
719 list);
720
721 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100722 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
Harald Welted11c0592012-09-06 21:57:11 +0200723 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
724 /* make sure we re-start the timer */
725 fc_queue_timer_cfg(fc);
726 return;
727 }
728
729 /* remove from the queue */
730 llist_del(&fcqe->list);
731
732 fc->queue_depth--;
733
734 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200735 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200736 fc->time_last_pdu = time_now;
737
738 /* call the output callback for this FC instance */
739 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
740
741 /* we expect that out_cb will in the end free the msgb once
742 * it is no longer needed */
743
744 /* but we have to free the queue element ourselves */
745 talloc_free(fcqe);
746
747 /* re-configure the timer for the next PDU */
748 fc_queue_timer_cfg(fc);
749}
750
751/* configure/schedule the flow control timer to expire once the bucket
752 * will have leaked a sufficient number of bytes to transmit the next
753 * PDU in the queue */
754static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
755{
756 struct bssgp_fc_queue_element *fcqe;
757 uint32_t msecs;
758
759 if (llist_empty(&fc->queue))
760 return 0;
761
Jacob Erlbeck97319352015-04-30 19:28:03 +0200762 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200763 list);
764
Harald Welte27b2bb72013-06-22 09:44:00 +0200765 if (fc->bucket_leak_rate != 0) {
766 /* Calculate the point in time at which we will have leaked
767 * a sufficient number of bytes from the bucket to transmit
768 * the first PDU in the queue */
769 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
770 /* FIXME: add that time to fc->time_last_pdu and subtract it from
771 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200772 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200773 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
774 } else {
775 /* If the PCU is telling us to not send any more data at all,
776 * there's no point starting a timer. */
777 }
Harald Welted11c0592012-09-06 21:57:11 +0200778
779 return 0;
780}
781
782/* Enqueue a PDU in the flow control queue for delayed transmission */
783static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
784 uint32_t llc_pdu_len, void *priv)
785{
786 struct bssgp_fc_queue_element *fcqe;
787
788 if (fc->queue_depth >= fc->max_queue_depth)
789 return -ENOSPC;
790
791 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
792 if (!fcqe)
793 return -ENOMEM;
794 fcqe->msg = msg;
795 fcqe->llc_pdu_len = llc_pdu_len;
796 fcqe->priv = priv;
797
798 llist_add_tail(&fcqe->list, &fc->queue);
799
800 fc->queue_depth++;
801
802 /* re-configure the timer for dequeueing the pdu */
803 fc_queue_timer_cfg(fc);
804
805 return 0;
806}
807
808/* According to Section 8.2 */
809static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
810{
811 struct timeval time_now, time_diff;
812 int64_t bucket_predicted;
813 uint32_t csecs_elapsed, leaked;
814
815 /* B' = B + L(p) - (Tc - Tp)*R */
816
817 /* compute number of centi-seconds that have elapsed since transmitting
818 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200819 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200820 timersub(&time_now, &fc->time_last_pdu, &time_diff);
821 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
822
823 /* compute number of bytes that have leaked in the elapsed number
824 * of centi-seconds */
825 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
826 /* add the current PDU length to the last bucket level */
827 bucket_predicted = fc->bucket_counter + pdu_len;
828 /* ... and subtract the number of leaked bytes */
829 bucket_predicted -= leaked;
830
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700831 if (bucket_predicted < pdu_len)
832 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200833
834 if (bucket_predicted <= fc->bucket_size_max) {
835 /* the bucket is not full yet, we can pass the packet */
836 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700837 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200838 }
839
840 /* bucket is full, PDU needs to be delayed */
841 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200842}
843
844/* output callback for BVC flow control */
845static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
846 uint32_t llc_pdu_len, void *priv)
847{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200848 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200849}
850
851/* input function of the flow control implementation, called first
852 * for the MM flow control, and then as the MM flow control output
853 * callback in order to perform BVC flow control */
854int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
855 uint32_t llc_pdu_len, void *priv)
856{
857 struct timeval time_now;
858
Harald Weltebb826222012-09-07 10:22:01 +0200859 if (llc_pdu_len > fc->bucket_size_max) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100860 LOGP(DLBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
Harald Weltebb826222012-09-07 10:22:01 +0200861 "than maximum bucket size (%u)!\n", llc_pdu_len,
862 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200863 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200864 return -EIO;
865 }
866
Harald Welted11c0592012-09-06 21:57:11 +0200867 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100868 int rc;
869 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
870 if (rc)
871 msgb_free(msg);
872 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200873 } else {
874 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200875 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200876 fc->time_last_pdu = time_now;
877 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
878 }
879}
880
Harald Weltebb826222012-09-07 10:22:01 +0200881
882/* Initialize the Flow Control structure */
883void bssgp_fc_init(struct bssgp_flow_control *fc,
884 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
885 uint32_t max_queue_depth,
886 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
887 uint32_t llc_pdu_len, void *priv))
888{
889 fc->out_cb = out_cb;
890 fc->bucket_size_max = bucket_size_max;
891 fc->bucket_leak_rate = bucket_leak_rate;
892 fc->max_queue_depth = max_queue_depth;
893 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200894 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200895}
896
Harald Welted11c0592012-09-06 21:57:11 +0200897/* Initialize the Flow Control parameters for a new MS according to
898 * default values for the BVC specified by BVCI and NSEI */
899int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200900 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200901{
902 struct bssgp_bvc_ctx *ctx;
903
904 ctx = btsctx_by_bvci_nsei(bvci, nsei);
905 if (!ctx)
906 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200907
908 /* output call-back of per-MS FC is per-CTX FC */
909 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
910 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200911
912 return 0;
913}
914
Harald Welte25de8112010-05-13 21:26:28 +0200915static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200916 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800917{
Harald Welte27b2bb72013-06-22 09:44:00 +0200918 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
919 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800920
Harald Weltefde19ed2020-12-07 21:43:51 +0100921 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200922 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800923
Harald Welte2d9ce712020-12-03 15:53:59 +0100924 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TAG, 1) ||
925 !TLVP_PRES_LEN(tp, BSSGP_IE_BVC_BUCKET_SIZE, 2) ||
926 !TLVP_PRES_LEN(tp, BSSGP_IE_BUCKET_LEAK_RATE, 2) ||
927 !TLVP_PRES_LEN(tp, BSSGP_IE_BMAX_DEFAULT_MS, 2) ||
928 !TLVP_PRES_LEN(tp, BSSGP_IE_R_DEFAULT_MS,2)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100929 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
Harald Weltee9686b62010-05-31 18:07:17 +0200930 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800931 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200932 }
Harald Welte9ba50052010-03-14 15:45:01 +0800933
Harald Weltebb826222012-09-07 10:22:01 +0200934 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200935 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200936 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200937 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200938 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200939 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200940 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200941 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200942
Harald Welte27b2bb72013-06-22 09:44:00 +0200943 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100944 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
Harald Welte27b2bb72013-06-22 09:44:00 +0200945 "rate of 0, stopping all DL GPRS!\n");
946 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100947 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
Harald Welte27b2bb72013-06-22 09:44:00 +0200948 "rate of != 0, restarting all DL GPRS!\n");
949
950 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100951 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
Harald Welte27b2bb72013-06-22 09:44:00 +0200952 "bucket leak rate of 0, stopping DL GPRS!\n");
953 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100954 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
Harald Welte27b2bb72013-06-22 09:44:00 +0200955 "bucket leak rate != 0, restarting DL GPRS!\n");
956
957 /* reconfigure the timer for flow control based on new values */
958 fc_queue_timer_cfg(bctx->fc);
959
Harald Welte9ba50052010-03-14 15:45:01 +0800960 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200961 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200962 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800963}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200964
Harald Welte25de8112010-05-13 21:26:28 +0200965/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800966static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
967 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800968{
Harald Welteec19c102010-05-02 09:50:42 +0200969 struct bssgp_normal_hdr *bgph =
970 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200971 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800972 int rc = 0;
973
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100974 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
975
Harald Welte58e65c92010-05-13 21:45:23 +0200976 /* If traffic is received on a BVC that is marked as blocked, the
977 * received PDU shall not be accepted and a STATUS PDU (Cause value:
978 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100979 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200980 uint16_t bvci = msgb_bvci(msg);
981 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
982 }
983
Harald Welte9ba50052010-03-14 15:45:01 +0800984 switch (pdu_type) {
985 case BSSGP_PDUT_UL_UNITDATA:
986 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200987 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800988 break;
989 case BSSGP_PDUT_RA_CAPABILITY:
990 /* BSS requests RA capability or IMSI */
Harald Weltefde19ed2020-12-07 21:43:51 +0100991 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200992 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200993 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800994 /* FIXME: send RA_CAPA_UPDATE_ACK */
995 break;
996 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltefde19ed2020-12-07 21:43:51 +0100997 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800998 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200999 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +08001000 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001001 case BSSGP_PDUT_FLOW_CONTROL_BVC:
1002 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +02001003 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +08001004 break;
1005 case BSSGP_PDUT_FLOW_CONTROL_MS:
1006 /* BSS informs us of available bandwidth to one MS */
Harald Weltefde19ed2020-12-07 21:43:51 +01001007 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
Harald Weltee9686b62010-05-31 18:07:17 +02001008 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +02001009 /* FIXME: actually implement flow control */
1010 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +08001011 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001012 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001013 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +01001014 break;
Alexander Couzens7819ff62021-02-17 02:30:33 +01001015 case BSSGP_PDUT_BVC_RESET:
1016 rc = bssgp_rx_bvc_reset(msg, tp, bctx->bvci);
1017 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001018 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
1019 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
1020 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
1021 case BSSGP_PDUT_MODIFY_BSS_PFC:
1022 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001023 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
Max2c34ab42016-03-17 15:42:26 +01001024 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001025 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001026 break;
1027 /* those only exist in the SGSN -> BSS direction */
1028 case BSSGP_PDUT_DL_UNITDATA:
1029 case BSSGP_PDUT_PAGING_PS:
1030 case BSSGP_PDUT_PAGING_CS:
1031 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +02001032 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
1033 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001034 DEBUGP(DLBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
Max2c34ab42016-03-17 15:42:26 +01001035 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001036 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1037 rc = -EINVAL;
1038 break;
1039 default:
Harald Weltefde19ed2020-12-07 21:43:51 +01001040 DEBUGP(DLBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
Max2c34ab42016-03-17 15:42:26 +01001041 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001042 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1043 break;
1044 }
1045
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +08001046 return rc;
Harald Welte25de8112010-05-13 21:26:28 +02001047}
1048
1049/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001050static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
1051 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +02001052{
1053 struct bssgp_normal_hdr *bgph =
1054 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1055 uint8_t pdu_type = bgph->pdu_type;
1056 int rc = 0;
1057 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001058 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001059
1060 switch (bgph->pdu_type) {
1061 case BSSGP_PDUT_SUSPEND:
1062 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001063 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001064 break;
1065 case BSSGP_PDUT_RESUME:
1066 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001067 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001068 break;
1069 case BSSGP_PDUT_FLUSH_LL_ACK:
1070 /* BSS informs us it has performed LL FLUSH */
Harald Weltefde19ed2020-12-07 21:43:51 +01001071 DEBUGP(DLBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001072 /* FIXME: send NM_FLUSH_LL.res to NM */
1073 break;
1074 case BSSGP_PDUT_LLC_DISCARD:
1075 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001076 if (!bctx) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001077 LOGP(DLBSSGP, LOGL_ERROR,
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001078 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
1079 goto err_mand_ie;
1080 }
Harald Weltee9686b62010-05-31 18:07:17 +02001081 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001082 break;
1083 case BSSGP_PDUT_BVC_BLOCK:
1084 /* BSS tells us that BVC shall be blocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001085 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1086 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001087 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
Harald Weltee9686b62010-05-31 18:07:17 +02001088 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001089 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001090 }
Harald Welte2677ea52010-05-31 17:16:36 +02001091 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001092 break;
1093 case BSSGP_PDUT_BVC_UNBLOCK:
1094 /* BSS tells us that BVC shall be unblocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001095 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001096 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
Harald Weltee9686b62010-05-31 18:07:17 +02001097 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001098 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001099 }
Harald Welte25de8112010-05-13 21:26:28 +02001100 rc = bssgp_rx_bvc_unblock(msg, tp);
1101 break;
Max590c4022017-06-28 14:29:24 +02001102 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001103 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
Max590c4022017-06-28 14:29:24 +02001104 break;
Harald Welte25de8112010-05-13 21:26:28 +02001105 case BSSGP_PDUT_BVC_RESET:
Alexander Couzens7819ff62021-02-17 02:30:33 +01001106 /* SGSN or BSS tells us that BVC init is required */
Harald Welte2d9ce712020-12-03 15:53:59 +01001107 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1108 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001109 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
Harald Weltee9686b62010-05-31 18:07:17 +02001110 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001111 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001112 }
Harald Welte25de8112010-05-13 21:26:28 +02001113 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1114 break;
1115 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001116 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001117 break;
Philipp Maier1eaa7bc2020-12-16 21:07:04 +01001118
1119 case BSSGP_PDUT_RAN_INFO:
1120 case BSSGP_PDUT_RAN_INFO_REQ:
1121 case BSSGP_PDUT_RAN_INFO_ACK:
1122 case BSSGP_PDUT_RAN_INFO_ERROR:
1123 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier7741bc32021-01-07 21:55:48 +01001124 rc = bssgp_rx_rim(msg, tp, bvci);
Philipp Maier1eaa7bc2020-12-16 21:07:04 +01001125 break;
1126
Harald Welte25de8112010-05-13 21:26:28 +02001127 /* those only exist in the SGSN -> BSS direction */
1128 case BSSGP_PDUT_PAGING_PS:
1129 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001130 case BSSGP_PDUT_SUSPEND_ACK:
1131 case BSSGP_PDUT_SUSPEND_NACK:
1132 case BSSGP_PDUT_RESUME_ACK:
1133 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001134 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001135 case BSSGP_PDUT_BVC_BLOCK_ACK:
1136 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1137 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltefde19ed2020-12-07 21:43:51 +01001138 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
Max2c34ab42016-03-17 15:42:26 +01001139 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001140 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001141 rc = -EINVAL;
1142 break;
1143 default:
Harald Weltefde19ed2020-12-07 21:43:51 +01001144 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
Max2c34ab42016-03-17 15:42:26 +01001145 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001146 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001147 break;
1148 }
1149
1150 return rc;
1151err_mand_ie:
1152 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1153}
1154
Harald Welte25de8112010-05-13 21:26:28 +02001155/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001156int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001157{
1158 struct bssgp_normal_hdr *bgph =
1159 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1160 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1161 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001162 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001163 uint8_t pdu_type = bgph->pdu_type;
1164 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001165 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001166 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001167 int data_len;
1168 int rc = 0;
1169
1170 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1171
1172 /* UNITDATA BSSGP headers have TLLI in front */
1173 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1174 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1175 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1176 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1177 } else {
1178 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1179 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1180 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001181 if (rc < 0) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001182 LOGP(DLBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
Stefan Sperling2b544b22018-06-25 12:20:43 +02001183 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001184 if (pdu_type != BSSGP_PDUT_STATUS)
1185 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001186 return rc;
1187 }
Harald Welte25de8112010-05-13 21:26:28 +02001188
Harald Welte2d9ce712020-12-03 15:53:59 +01001189 if (bvci == BVCI_SIGNALLING && TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Harald Weltebfe62e52017-05-15 12:48:30 +02001190 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001191
Harald Welte25de8112010-05-13 21:26:28 +02001192 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001193 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001194
Harald Welte16c8dbb2010-05-17 23:30:01 +02001195 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001196 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001197 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
1198 rate_ctr_add(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_BYTES_IN),
Harald Welte16c8dbb2010-05-17 23:30:01 +02001199 msgb_bssgp_len(msg));
1200 }
1201
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001202 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1203 * are otherwise unexpected */
1204 if (pdu_type == BSSGP_PDUT_STATUS)
1205 /* Some exception has occurred */
1206 return bssgp_rx_status(msg, &tp, bvci, bctx);
1207
1208 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1209 * registered if a BVCI is given. */
1210 if (!bctx && bvci != BVCI_SIGNALLING &&
1211 pdu_type != BSSGP_PDUT_BVC_RESET) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001212 LOGP(DLBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001213 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001214 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1215 }
1216
Harald Welte61c07842010-05-18 11:57:08 +02001217 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001218 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001219 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001220 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001221 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001222 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001223 else
Harald Weltefde19ed2020-12-07 21:43:51 +01001224 LOGP(DLBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001225 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1226 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001227
1228 return rc;
1229}
1230
Harald Weltede4599c2012-06-17 13:04:02 +08001231int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1232 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001233{
Harald Welte8a521132010-05-17 22:59:29 +02001234 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001235 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001236 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001237 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001238 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001239 uint16_t bvci = msgb_bvci(msg);
1240 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001241 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001242 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001243
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001244 OSMO_ASSERT(dup != NULL);
1245
Harald Welte30bc19a2010-05-02 11:19:37 +02001246 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001247 if (bvci <= BVCI_PTM ) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001248 LOGP(DLBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001249 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001250 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001251 return -EINVAL;
1252 }
1253
1254 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001255 if (!bctx) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001256 LOGP(DLBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
Harald Welted11c0592012-09-06 21:57:11 +02001257 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001258 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001259 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001260 }
Harald Welte9ba50052010-03-14 15:45:01 +08001261
1262 if (msg->len > TVLV_MAX_ONEBYTE)
1263 llc_pdu_tlv_hdr_len += 1;
1264
1265 /* prepend the tag and length of the LLC-PDU TLV */
1266 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1267 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1268 if (llc_pdu_tlv_hdr_len > 2) {
1269 llc_pdu_tlv[1] = msg_len >> 8;
1270 llc_pdu_tlv[2] = msg_len & 0xff;
1271 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001272 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001273 llc_pdu_tlv[1] |= 0x80;
1274 }
1275
Harald Welte2f946832010-05-31 22:12:30 +02001276 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1277
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001278 /* Old TLLI to help BSS map from old->new */
1279 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001280 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001281 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001282 }
Harald Welte9ba50052010-03-14 15:45:01 +08001283
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001284 /* IMSI */
1285 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001286 uint8_t mi[GSM48_MID_MAX_SIZE];
1287/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1288 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1289 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1290 * mi[131], which is wrong */
1291#pragma GCC diagnostic push
1292#pragma GCC diagnostic ignored "-Warray-bounds"
1293 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1294 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1295 if (imsi_len > 2)
1296 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1297 imsi_len-2, mi+2);
1298#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001299 }
1300
1301 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001302 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001303 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1304 (uint8_t *) &drx_params);
1305
1306 /* FIXME: Priority */
1307
1308 /* MS Radio Access Capability */
1309 if (dup->ms_ra_cap.len)
1310 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1311 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1312
Harald Welte9ba50052010-03-14 15:45:01 +08001313 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001314 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001315
1316 /* prepend the QoS profile, TLLI and pdu type */
1317 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001318 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001319 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001320 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1321
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001322 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_OUT));
1323 rate_ctr_add(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_BYTES_OUT), msg->len);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001324
Harald Welte30bc19a2010-05-02 11:19:37 +02001325 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001326
Harald Welted11c0592012-09-06 21:57:11 +02001327 /* check if we have to go through per-ms flow control or can go
1328 * directly to the per-BSS flow control */
1329 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001330 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001331 else
Harald Welted8b47692012-09-07 11:29:32 +02001332 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001333}
Harald Welte68b4f032010-06-09 16:22:28 +02001334
1335/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001336int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1337 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001338{
1339 struct msgb *msg = bssgp_msgb_alloc();
1340 struct bssgp_normal_hdr *bgph =
1341 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001342 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001343 uint8_t mi[GSM48_MID_MAX_SIZE];
1344 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001345 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001346
1347 if (imsi_len < 2)
1348 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001349
1350 msgb_nsei(msg) = nsei;
1351 msgb_bvci(msg) = ns_bvci;
1352
1353 if (pinfo->mode == BSSGP_PAGING_PS)
1354 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1355 else
1356 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1357 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001358/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1359 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1360 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1361 * mi[131], which is wrong */
1362#pragma GCC diagnostic push
1363#pragma GCC diagnostic ignored "-Warray-bounds"
1364 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1365 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1366#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001367 /* DRX Parameters */
1368 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1369 (uint8_t *) &drx_params);
1370 /* Scope */
1371 switch (pinfo->scope) {
1372 case BSSGP_PAGING_BSS_AREA:
1373 {
1374 uint8_t null = 0;
1375 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1376 }
1377 break;
1378 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001379 gsm48_encode_ra(&ra, &pinfo->raid);
1380 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001381 break;
1382 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001383 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001384 break;
1385 case BSSGP_PAGING_BVCI:
1386 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001387 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001388 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1389 }
1390 break;
1391 }
1392 /* QoS profile mandatory for PS */
1393 if (pinfo->mode == BSSGP_PAGING_PS)
1394 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1395
1396 /* Optional (P-)TMSI */
1397 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001398 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001399 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1400 }
1401
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001402 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001403}
Harald Weltecca49632012-06-16 17:45:59 +08001404
Harald Weltede4599c2012-06-17 13:04:02 +08001405void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001406{
Pau Espin Pedrol0e617162020-12-10 13:38:42 +01001407 /* BSSGP has moved from DGPRS to DLGPRS, please update your code if it's
1408 * still calling this function
1409 */
Harald Weltecca49632012-06-16 17:45:59 +08001410}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001411
1412/*!
1413 * \brief Flush the queue of the bssgp_flow_control
1414 * \param[in] The flow control object which holds the queue.
1415 */
1416void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1417{
1418 struct bssgp_fc_queue_element *element, *tmp;
1419
1420 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1421 msgb_free(element->msg);
1422 llist_del(&element->list);
1423 talloc_free(element);
1424 }
1425}
1426
1427/*!
1428 * \brief Flush the queues of all BSSGP contexts.
1429 */
1430void bssgp_flush_all_queues()
1431{
1432 struct bssgp_bvc_ctx *bctx;
1433
1434 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1435 if (bctx->fc)
1436 bssgp_fc_flush_queue(bctx->fc);
1437 }
1438}