blob: 7abef804b456bc2aa43ed3393ae61d3e1c181442 [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{
Pau Espin Pedrol26a42c02021-07-26 14:37:39 +020080 OSMO_ASSERT(bssgp_nsi);
Alexander Couzens85a8fd32020-07-18 15:57:07 +020081 return gprs_ns_sendmsg(bssgp_nsi, msg);
82}
83
Harald Welte6752fa42010-05-02 09:23:16 +020084/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020085struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020086{
Harald Welte8a521132010-05-17 22:59:29 +020087 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020088
Harald Weltea78b9c22010-05-17 23:02:42 +020089 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020090 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
91 bctx->cell_id == cid)
92 return bctx;
93 }
94 return NULL;
95}
96
Alexander Couzens7819ff62021-02-17 02:30:33 +010097/* Transmit a BVC-RESET or BVC-RESET-ACK with a given nsei and bvci (Chapter 10.4.12)
98 * \param[in] pdu Either BSSGP_PDUT_BVC_RESET or BSSGP_PDUT_BVC_RESET_ACK
Daniel Willmann2d42b902020-09-26 09:11:05 +020099 * \param[in] nsei The NSEI to transmit over
100 * \param[in] bvci BVCI of the BVC to reset
Alexander Couzens7819ff62021-02-17 02:30:33 +0100101 * \param[in] cause The cause of the reset only valid for BSSGP_PDUT_BVC_RESET.
Daniel Willmann2d42b902020-09-26 09:11:05 +0200102 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
103 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
Alexander Couzens7819ff62021-02-17 02:30:33 +0100104 * returns >= 0 on success, on error < 0.
Daniel Willmann2d42b902020-09-26 09:11:05 +0200105 */
Alexander Couzens7819ff62021-02-17 02:30:33 +0100106static int tx_bvc_reset_nsei_bvci(enum bssgp_pdu_type pdu, uint16_t nsei, uint16_t bvci,
107 enum gprs_bssgp_cause cause, const struct gprs_ra_id *ra_id, uint16_t cell_id)
Daniel Willmann2d42b902020-09-26 09:11:05 +0200108{
109 struct msgb *msg = bssgp_msgb_alloc();
110 struct bssgp_normal_hdr *bgph =
111 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
112 uint16_t _bvci = osmo_htons(bvci);
113
Alexander Couzens7819ff62021-02-17 02:30:33 +0100114 OSMO_ASSERT(pdu == BSSGP_PDUT_BVC_RESET || pdu == BSSGP_PDUT_BVC_RESET_ACK);
115
Daniel Willmann2d42b902020-09-26 09:11:05 +0200116 msgb_nsei(msg) = nsei;
Alexander Couzens5baa1412021-02-19 19:29:04 +0100117 msgb_bvci(msg) = BVCI_SIGNALLING;
Alexander Couzens7819ff62021-02-17 02:30:33 +0100118 bgph->pdu_type = pdu;
119
Daniel Willmann2d42b902020-09-26 09:11:05 +0200120 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
Alexander Couzens7819ff62021-02-17 02:30:33 +0100121
122 if (pdu == BSSGP_PDUT_BVC_RESET) {
123 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, (uint8_t *) &cause);
124 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
125 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
126 } else {
127 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET-ACK\n", bvci);
128 }
129
Daniel Willmann2d42b902020-09-26 09:11:05 +0200130 if (ra_id) {
131 uint8_t bssgp_cid[8];
132 bssgp_create_cell_id(bssgp_cid, ra_id, cell_id);
133 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
134 }
135
136 /* Optional: Feature Bitmap */
137
138 return bssgp_ns_send(bssgp_ns_send_data, msg);
139}
140
Alexander Couzens7819ff62021-02-17 02:30:33 +0100141/*! Transmit a BVC-RESET message with a given nsei and bvci (Chapter 10.4.12)
142 * \param[in] nsei The NSEI to transmit over
143 * \param[in] bvci BVCI of the BVC to reset
144 * \param[in] cause The cause of the reset
145 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
146 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
147 * returns >= 0 on success, on error < 0.
148 */
149int 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)
150{
151 return tx_bvc_reset_nsei_bvci(BSSGP_PDUT_BVC_RESET, nsei, bvci, cause, ra_id, cell_id);
152}
153
154/*! Transmit a BVC-RESET-ACK message with a given nsei and bvci (Chapter 10.4.12)
155 * \param[in] nsei The NSEI to transmit over
156 * \param[in] bvci BVCI of the BVC to reset
157 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
158 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
159 * returns >= 0 on success, on error < 0.
160 */
161int bssgp_tx_bvc_reset_ack_nsei_bvci(uint16_t nsei, uint16_t bvci, const struct gprs_ra_id *ra_id, uint16_t cell_id)
162{
163 return tx_bvc_reset_nsei_bvci(BSSGP_PDUT_BVC_RESET_ACK, nsei, bvci, 0, ra_id, cell_id);
164}
165
Max8b8938f2017-06-29 19:48:29 +0200166/*! Initiate reset procedure for all PTP BVC on a given NSEI.
167 *
168 * This function initiates reset procedure for all PTP BVC with a given cause.
169 * \param[in] nsei NSEI to which PTP BVC should belong to
170 * \param[in] cause Cause of BVC RESET
171 * \returns 0 on success, negative error code otherwise
172 */
173int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
174{
175 int rc;
176 struct bssgp_bvc_ctx *bctx;
177
178 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
179 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100180 LOGP(DLBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
Max8b8938f2017-06-29 19:48:29 +0200181 nsei, bctx->bvci, bssgp_cause_str(cause));
182 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
183 if (rc < 0)
184 return rc;
185 }
186 }
187
188 return 0;
189}
190
Harald Welte6752fa42010-05-02 09:23:16 +0200191/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200192struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200193{
Harald Welte8a521132010-05-17 22:59:29 +0200194 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200195
Harald Weltea78b9c22010-05-17 23:02:42 +0200196 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200197 if (bctx->nsei == nsei && bctx->bvci == bvci)
198 return bctx;
199 }
200 return NULL;
201}
202
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200203void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data)
204{
205 bssgp_ns_send = ns_send;
206 bssgp_ns_send_data = data;
207}
208
Harald Welte8a521132010-05-17 22:59:29 +0200209struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200210{
Harald Welte8a521132010-05-17 22:59:29 +0200211 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200212
Harald Welte8a521132010-05-17 22:59:29 +0200213 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200214 if (!ctx)
215 return NULL;
216 ctx->bvci = bvci;
217 ctx->nsei = nsei;
Alexander Couzens7819ff62021-02-17 02:30:33 +0100218 ctx->is_sgsn = true;
Harald Welte25de8112010-05-13 21:26:28 +0200219 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
220 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200221 if (!ctx->ctrg)
222 goto err_ctrg;
223
Harald Welted8b47692012-09-07 11:29:32 +0200224 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200225 if (!ctx->fc)
226 goto err_fc;
227
Harald Welted8b47692012-09-07 11:29:32 +0200228 /* cofigure for 2Mbit, 30 packets in queue */
229 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200230
Harald Weltea78b9c22010-05-17 23:02:42 +0200231 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200232
233 return ctx;
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200234
235err_fc:
236 rate_ctr_group_free(ctx->ctrg);
237err_ctrg:
238 talloc_free(ctx);
239 return NULL;
Harald Welte6752fa42010-05-02 09:23:16 +0200240}
241
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700242void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx)
243{
244 if (!ctx)
245 return;
Alexander Couzens495b4a72020-09-16 23:10:03 +0200246
247 osmo_timer_del(&ctx->fc->timer);
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700248 rate_ctr_group_free(ctx->ctrg);
249 llist_del(&ctx->list);
250 talloc_free(ctx);
251}
252
Harald Welte9ba50052010-03-14 15:45:01 +0800253/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200254static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800255{
256 struct msgb *msg = bssgp_msgb_alloc();
257 struct bssgp_normal_hdr *bgph =
258 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
259
Harald Welte24a655f2010-04-30 19:54:29 +0200260 msgb_nsei(msg) = nsei;
261 msgb_bvci(msg) = ns_bvci;
262
Harald Welte9ba50052010-03-14 15:45:01 +0800263 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
264 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
265
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200266 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800267}
268
Harald Weltea8aa4df2010-05-30 22:00:53 +0200269/* 10.3.7 SUSPEND-ACK PDU */
270int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
271 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
272{
273 struct msgb *msg = bssgp_msgb_alloc();
274 struct bssgp_normal_hdr *bgph =
275 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200276
277 msgb_nsei(msg) = nsei;
278 msgb_bvci(msg) = 0; /* Signalling */
279 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
280
Maxe29ec852018-01-05 14:30:22 +0100281 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100282 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200283 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
284
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200285 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200286}
287
288/* 10.3.8 SUSPEND-NACK PDU */
289int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100290 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200291 uint8_t *cause)
292{
293 struct msgb *msg = bssgp_msgb_alloc();
294 struct bssgp_normal_hdr *bgph =
295 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200296
297 msgb_nsei(msg) = nsei;
298 msgb_bvci(msg) = 0; /* Signalling */
299 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
300
Maxe29ec852018-01-05 14:30:22 +0100301 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100302 bssgp_msgb_ra_put(msg, ra_id);
303
Harald Weltea8aa4df2010-05-30 22:00:53 +0200304 if (cause)
305 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
306
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200307 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200308}
309
310/* 10.3.10 RESUME-ACK PDU */
311int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
312 const struct gprs_ra_id *ra_id)
313{
314 struct msgb *msg = bssgp_msgb_alloc();
315 struct bssgp_normal_hdr *bgph =
316 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200317
318 msgb_nsei(msg) = nsei;
319 msgb_bvci(msg) = 0; /* Signalling */
320 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
321
Maxe29ec852018-01-05 14:30:22 +0100322 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100323 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200324
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200325 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200326}
327
328/* 10.3.11 RESUME-NACK PDU */
329int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
330 const struct gprs_ra_id *ra_id, uint8_t *cause)
331{
332 struct msgb *msg = bssgp_msgb_alloc();
333 struct bssgp_normal_hdr *bgph =
334 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200335
336 msgb_nsei(msg) = nsei;
337 msgb_bvci(msg) = 0; /* Signalling */
338 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
339
Maxe29ec852018-01-05 14:30:22 +0100340 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100341 bssgp_msgb_ra_put(msg, ra_id);
342
Harald Weltea8aa4df2010-05-30 22:00:53 +0200343 if (cause)
344 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
345
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200346 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200347}
348
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200349uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200350{
351 /* 6 octets RAC */
352 gsm48_parse_ra(raid, buf);
353 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200354 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200355}
356
Harald Welte28610072011-11-24 21:32:07 +0100357int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
358 uint16_t cid)
359{
Harald Welte28610072011-11-24 21:32:07 +0100360 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100361 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100362 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200363 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100364
365 return 8;
366}
367
Harald Welte3fddf3c2010-05-01 16:48:27 +0200368/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200369static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200370 uint16_t ns_bvci)
371{
Harald Welte15a36432012-06-17 12:16:31 +0800372 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200373 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200374 uint16_t nsei = msgb_nsei(msg);
375 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200376
Harald Weltebfe62e52017-05-15 12:48:30 +0200377 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltefde19ed2020-12-07 21:43:51 +0100378 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200379 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
380
Harald Welte6752fa42010-05-02 09:23:16 +0200381 /* look-up or create the BTS context for this BVC */
382 bctx = btsctx_by_bvci_nsei(bvci, nsei);
383 if (!bctx)
384 bctx = btsctx_alloc(bvci, nsei);
385
Harald Welte25de8112010-05-13 21:26:28 +0200386 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
387 bctx->state &= ~BVC_S_BLOCKED;
388
Harald Welte3fddf3c2010-05-01 16:48:27 +0200389 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
390 * informs us about its RAC + Cell ID, so we can create a mapping */
Alexander Couzens5baa1412021-02-19 19:29:04 +0100391 if (bctx->is_sgsn && bvci != BVCI_SIGNALLING && bvci != BVCI_PTM) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100392 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100393 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200394 "missing mandatory IE\n", bvci);
395 return -EINVAL;
396 }
397 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200398 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
399 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Weltefde19ed2020-12-07 21:43:51 +0100400 LOGP(DLBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100401 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200402 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200403
Alexander Couzens7819ff62021-02-17 02:30:33 +0100404 /* Acknowledge the RESET to the BTS */
Alexander Couzens5baa1412021-02-19 19:29:04 +0100405 if (bvci == BVCI_SIGNALLING || bvci == BVCI_PTM || bctx->is_sgsn)
Alexander Couzens7819ff62021-02-17 02:30:33 +0100406 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
407 nsei, bvci, ns_bvci);
408 else
409 bssgp_tx_bvc_reset_ack_nsei_bvci(nsei, bvci, &bctx->ra_id, bctx->cell_id);
410
Harald Welte15a36432012-06-17 12:16:31 +0800411 /* Send NM_BVC_RESET.ind to NM */
412 memset(&nmp, 0, sizeof(nmp));
413 nmp.nsei = nsei;
414 nmp.bvci = bvci;
415 nmp.tp = tp;
416 nmp.ra_id = &bctx->ra_id;
417 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
418 PRIM_OP_INDICATION, msg);
419 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200420 return 0;
421}
422
Harald Welte25de8112010-05-13 21:26:28 +0200423static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
424{
Harald Welte15a36432012-06-17 12:16:31 +0800425 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100426 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200427 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200428
Harald Weltebfe62e52017-05-15 12:48:30 +0200429 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200430 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200431 /* 8.3.2: Signalling BVC shall never be blocked */
Harald Weltefde19ed2020-12-07 21:43:51 +0100432 LOGP(DLBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
Harald Welte58e65c92010-05-13 21:45:23 +0200433 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100434 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200435 return 0;
436 }
Harald Welte25de8112010-05-13 21:26:28 +0200437
Harald Weltefde19ed2020-12-07 21:43:51 +0100438 LOGP(DLBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200439
Max548caef2019-03-07 13:49:34 +0100440 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200441 if (!ptp_ctx)
442 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
443
444 ptp_ctx->state |= BVC_S_BLOCKED;
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200445 rate_ctr_inc(rate_ctr_group_get_ctr(ptp_ctx->ctrg, BSSGP_CTR_BLOCKED));
Harald Welte25de8112010-05-13 21:26:28 +0200446
Harald Welte15a36432012-06-17 12:16:31 +0800447 /* Send NM_BVC_BLOCK.ind to NM */
448 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100449 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800450 nmp.bvci = bvci;
451 nmp.tp = tp;
452 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
453 PRIM_OP_INDICATION, msg);
454 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200455
456 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100457 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200458 bvci, msgb_bvci(msg));
459};
460
461static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
462{
Harald Welte15a36432012-06-17 12:16:31 +0800463 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100464 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200465 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200466
Harald Weltebfe62e52017-05-15 12:48:30 +0200467 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200468 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200469 /* 8.3.2: Signalling BVC shall never be blocked */
Harald Weltefde19ed2020-12-07 21:43:51 +0100470 LOGP(DLBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
Harald Welte58e65c92010-05-13 21:45:23 +0200471 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100472 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200473 return 0;
474 }
Harald Welte25de8112010-05-13 21:26:28 +0200475
Harald Weltefde19ed2020-12-07 21:43:51 +0100476 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200477
Max548caef2019-03-07 13:49:34 +0100478 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200479 if (!ptp_ctx)
480 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
481
482 ptp_ctx->state &= ~BVC_S_BLOCKED;
483
Harald Welte15a36432012-06-17 12:16:31 +0800484 /* Send NM_BVC_UNBLOCK.ind to NM */
485 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100486 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800487 nmp.bvci = bvci;
488 nmp.tp = tp;
489 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
490 PRIM_OP_INDICATION, msg);
491 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200492
493 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100494 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200495 bvci, msgb_bvci(msg));
496};
497
Harald Welte9ba50052010-03-14 15:45:01 +0800498/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200499static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200500 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800501{
Harald Welte15a36432012-06-17 12:16:31 +0800502 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200503 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800504
Harald Welte6752fa42010-05-02 09:23:16 +0200505 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200506 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800507
Harald Weltefde19ed2020-12-07 21:43:51 +0100508 DEBUGP(DLBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200509
Harald Welte9ba50052010-03-14 15:45:01 +0800510 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte2d9ce712020-12-03 15:53:59 +0100511 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200512 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100513 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
Harald Weltee9686b62010-05-31 18:07:17 +0200514 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200515 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200516 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200517
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200518 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800519 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
520 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800521
Harald Welte15a36432012-06-17 12:16:31 +0800522 /* Send BSSGP_UL_UD.ind to NM */
523 memset(&gbp, 0, sizeof(gbp));
524 gbp.nsei = ctx->nsei;
525 gbp.bvci = ctx->bvci;
526 gbp.tlli = msgb_tlli(msg);
527 gbp.tp = tp;
528 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
529 PRIM_OP_INDICATION, msg);
530 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800531}
532
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200533static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800534{
Harald Welte15a36432012-06-17 12:16:31 +0800535 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200536 struct gprs_ra_id raid;
537 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100538 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200539 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800540
Harald Welte2d9ce712020-12-03 15:53:59 +0100541 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
542 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100543 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200544 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200545 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200546 }
Harald Welte9ba50052010-03-14 15:45:01 +0800547
Harald Weltebfe62e52017-05-15 12:48:30 +0200548 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200549
Harald Weltefde19ed2020-12-07 21:43:51 +0100550 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200551 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200552
Harald Weltea8aa4df2010-05-30 22:00:53 +0200553 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
554
Harald Welte313cccf2010-06-09 11:22:47 +0200555 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800556 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100557 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200558 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800559 gbp.tlli = tlli;
560 gbp.ra_id = &raid;
561 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
562 PRIM_OP_REQUEST, msg);
563
564 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200565 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100566 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200567
Max548caef2019-03-07 13:49:34 +0100568 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200569
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800570 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800571}
572
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200573static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800574{
Harald Welte15a36432012-06-17 12:16:31 +0800575 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200576 struct gprs_ra_id raid;
577 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200578 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100579 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200580 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800581
Harald Welte2d9ce712020-12-03 15:53:59 +0100582 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4 ) ||
583 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6) ||
584 !TLVP_PRES_LEN(tp, BSSGP_IE_SUSPEND_REF_NR, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100585 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200586 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200587 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200588 }
Harald Welte9ba50052010-03-14 15:45:01 +0800589
Harald Weltebfe62e52017-05-15 12:48:30 +0200590 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200591 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200592
Harald Weltefde19ed2020-12-07 21:43:51 +0100593 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200594
Harald Weltea8aa4df2010-05-30 22:00:53 +0200595 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
596
Harald Welte313cccf2010-06-09 11:22:47 +0200597 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800598 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100599 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200600 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800601 gbp.tlli = tlli;
602 gbp.ra_id = &raid;
603 gbp.u.resume.suspend_ref = suspend_ref;
604 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
605 PRIM_OP_REQUEST, msg);
606
607 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200608 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100609 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200610 NULL);
611
Max548caef2019-03-07 13:49:34 +0100612 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800613 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800614}
615
Harald Weltee9686b62010-05-31 18:07:17 +0200616
617static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
618 struct bssgp_bvc_ctx *ctx)
619{
Harald Welte15a36432012-06-17 12:16:31 +0800620 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200621 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100622 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200623
Harald Welte2d9ce712020-12-03 15:53:59 +0100624 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
625 !TLVP_PRES_LEN(tp, BSSGP_IE_LLC_FRAMES_DISCARDED, 1) ||
626 !TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
627 !TLVP_PRES_LEN(tp, BSSGP_IE_NUM_OCT_AFF, 3)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100628 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
Harald Weltee9686b62010-05-31 18:07:17 +0200629 "missing mandatory IE\n", ctx->bvci);
Harald Welte2d9ce712020-12-03 15:53:59 +0100630 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200631 }
632
Harald Welte2d9ce712020-12-03 15:53:59 +0100633 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200634
Harald Weltefde19ed2020-12-07 21:43:51 +0100635 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200636 ctx->bvci, tlli);
637
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200638 rate_ctr_inc(rate_ctr_group_get_ctr(ctx->ctrg, BSSGP_CTR_DISCARDED));
Harald Weltee9686b62010-05-31 18:07:17 +0200639
Harald Welte15a36432012-06-17 12:16:31 +0800640 /* send NM_LLC_DISCARDED to NM */
641 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100642 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800643 nmp.bvci = ctx->bvci;
644 nmp.tlli = tlli;
645 nmp.tp = tp;
646 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
647 PRIM_OP_INDICATION, msg);
648
649 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200650}
651
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100652int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
653 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
654{
Max548caef2019-03-07 13:49:34 +0100655 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100656 struct osmo_bssgp_prim nmp;
657 enum gprs_bssgp_cause cause;
658
Harald Welte2d9ce712020-12-03 15:53:59 +0100659 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100660 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100661 "missing mandatory IE\n", bvci);
662 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
663 } else {
664 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
665 }
666
Harald Weltefde19ed2020-12-07 21:43:51 +0100667 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100668 bvci, bssgp_cause_str(cause));
669
670 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100671 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2))
Harald Weltefde19ed2020-12-07 21:43:51 +0100672 LOGP(DLBSSGP, LOGL_ERROR,
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100673 "BSSGP BVCI=%u Rx STATUS cause=%s "
674 "missing conditional BVCI IE\n",
675 bvci, bssgp_cause_str(cause));
676 }
677
678 if (bctx)
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200679 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_STATUS));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100680
681 /* send NM_STATUS to NM */
682 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100683 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100684 nmp.bvci = bvci;
685 nmp.tp = tp;
686 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
687 PRIM_OP_INDICATION, msg);
688
689 return bssgp_prim_cb(&nmp.oph, NULL);
690}
691
Harald Welted11c0592012-09-06 21:57:11 +0200692/* One element (msgb) in a BSSGP Flow Control queue */
693struct bssgp_fc_queue_element {
694 /* linked list of queue elements */
695 struct llist_head list;
696 /* The message that we have enqueued */
697 struct msgb *msg;
698 /* Length of the LLC PDU part of the contained message */
699 uint32_t llc_pdu_len;
700 /* private pointer passed to the flow control out_cb function */
701 void *priv;
702};
703
704static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
705static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
706
707static void fc_timer_cb(void *data)
708{
709 struct bssgp_flow_control *fc = data;
710 struct bssgp_fc_queue_element *fcqe;
711 struct timeval time_now;
712
713 /* if the queue is empty, we return without sending something
714 * and without re-starting the timer */
715 if (llist_empty(&fc->queue))
716 return;
717
718 /* get the first entry from the queue */
719 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
720 list);
721
722 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100723 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
Harald Welted11c0592012-09-06 21:57:11 +0200724 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
725 /* make sure we re-start the timer */
726 fc_queue_timer_cfg(fc);
727 return;
728 }
729
730 /* remove from the queue */
731 llist_del(&fcqe->list);
732
733 fc->queue_depth--;
734
735 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200736 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200737 fc->time_last_pdu = time_now;
738
739 /* call the output callback for this FC instance */
740 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
741
742 /* we expect that out_cb will in the end free the msgb once
743 * it is no longer needed */
744
745 /* but we have to free the queue element ourselves */
746 talloc_free(fcqe);
747
748 /* re-configure the timer for the next PDU */
749 fc_queue_timer_cfg(fc);
750}
751
752/* configure/schedule the flow control timer to expire once the bucket
753 * will have leaked a sufficient number of bytes to transmit the next
754 * PDU in the queue */
755static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
756{
757 struct bssgp_fc_queue_element *fcqe;
758 uint32_t msecs;
759
760 if (llist_empty(&fc->queue))
761 return 0;
762
Jacob Erlbeck97319352015-04-30 19:28:03 +0200763 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200764 list);
765
Harald Welte27b2bb72013-06-22 09:44:00 +0200766 if (fc->bucket_leak_rate != 0) {
767 /* Calculate the point in time at which we will have leaked
768 * a sufficient number of bytes from the bucket to transmit
769 * the first PDU in the queue */
770 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
771 /* FIXME: add that time to fc->time_last_pdu and subtract it from
772 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200773 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200774 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
775 } else {
776 /* If the PCU is telling us to not send any more data at all,
777 * there's no point starting a timer. */
778 }
Harald Welted11c0592012-09-06 21:57:11 +0200779
780 return 0;
781}
782
783/* Enqueue a PDU in the flow control queue for delayed transmission */
784static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
785 uint32_t llc_pdu_len, void *priv)
786{
787 struct bssgp_fc_queue_element *fcqe;
788
789 if (fc->queue_depth >= fc->max_queue_depth)
790 return -ENOSPC;
791
792 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
793 if (!fcqe)
794 return -ENOMEM;
795 fcqe->msg = msg;
796 fcqe->llc_pdu_len = llc_pdu_len;
797 fcqe->priv = priv;
798
799 llist_add_tail(&fcqe->list, &fc->queue);
800
801 fc->queue_depth++;
802
803 /* re-configure the timer for dequeueing the pdu */
804 fc_queue_timer_cfg(fc);
805
806 return 0;
807}
808
809/* According to Section 8.2 */
810static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
811{
812 struct timeval time_now, time_diff;
813 int64_t bucket_predicted;
814 uint32_t csecs_elapsed, leaked;
815
816 /* B' = B + L(p) - (Tc - Tp)*R */
817
818 /* compute number of centi-seconds that have elapsed since transmitting
819 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200820 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200821 timersub(&time_now, &fc->time_last_pdu, &time_diff);
822 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
823
824 /* compute number of bytes that have leaked in the elapsed number
825 * of centi-seconds */
826 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
827 /* add the current PDU length to the last bucket level */
828 bucket_predicted = fc->bucket_counter + pdu_len;
829 /* ... and subtract the number of leaked bytes */
830 bucket_predicted -= leaked;
831
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700832 if (bucket_predicted < pdu_len)
833 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200834
835 if (bucket_predicted <= fc->bucket_size_max) {
836 /* the bucket is not full yet, we can pass the packet */
837 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700838 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200839 }
840
841 /* bucket is full, PDU needs to be delayed */
842 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200843}
844
845/* output callback for BVC flow control */
846static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
847 uint32_t llc_pdu_len, void *priv)
848{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200849 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200850}
851
852/* input function of the flow control implementation, called first
853 * for the MM flow control, and then as the MM flow control output
854 * callback in order to perform BVC flow control */
855int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
856 uint32_t llc_pdu_len, void *priv)
857{
858 struct timeval time_now;
859
Harald Weltebb826222012-09-07 10:22:01 +0200860 if (llc_pdu_len > fc->bucket_size_max) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100861 LOGP(DLBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
Harald Weltebb826222012-09-07 10:22:01 +0200862 "than maximum bucket size (%u)!\n", llc_pdu_len,
863 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200864 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200865 return -EIO;
866 }
867
Harald Welted11c0592012-09-06 21:57:11 +0200868 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100869 int rc;
870 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
871 if (rc)
872 msgb_free(msg);
873 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200874 } else {
875 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200876 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200877 fc->time_last_pdu = time_now;
878 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
879 }
880}
881
Harald Weltebb826222012-09-07 10:22:01 +0200882
883/* Initialize the Flow Control structure */
884void bssgp_fc_init(struct bssgp_flow_control *fc,
885 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
886 uint32_t max_queue_depth,
887 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
888 uint32_t llc_pdu_len, void *priv))
889{
890 fc->out_cb = out_cb;
891 fc->bucket_size_max = bucket_size_max;
892 fc->bucket_leak_rate = bucket_leak_rate;
893 fc->max_queue_depth = max_queue_depth;
894 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200895 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200896}
897
Harald Welted11c0592012-09-06 21:57:11 +0200898/* Initialize the Flow Control parameters for a new MS according to
899 * default values for the BVC specified by BVCI and NSEI */
900int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200901 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200902{
903 struct bssgp_bvc_ctx *ctx;
904
905 ctx = btsctx_by_bvci_nsei(bvci, nsei);
906 if (!ctx)
907 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200908
909 /* output call-back of per-MS FC is per-CTX FC */
910 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
911 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200912
913 return 0;
914}
915
Harald Welte25de8112010-05-13 21:26:28 +0200916static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200917 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800918{
Harald Welte27b2bb72013-06-22 09:44:00 +0200919 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
920 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800921
Harald Weltefde19ed2020-12-07 21:43:51 +0100922 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200923 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800924
Harald Welte2d9ce712020-12-03 15:53:59 +0100925 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TAG, 1) ||
926 !TLVP_PRES_LEN(tp, BSSGP_IE_BVC_BUCKET_SIZE, 2) ||
927 !TLVP_PRES_LEN(tp, BSSGP_IE_BUCKET_LEAK_RATE, 2) ||
928 !TLVP_PRES_LEN(tp, BSSGP_IE_BMAX_DEFAULT_MS, 2) ||
929 !TLVP_PRES_LEN(tp, BSSGP_IE_R_DEFAULT_MS,2)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100930 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
Harald Weltee9686b62010-05-31 18:07:17 +0200931 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800932 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200933 }
Harald Welte9ba50052010-03-14 15:45:01 +0800934
Harald Weltebb826222012-09-07 10:22:01 +0200935 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200936 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200937 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200938 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200939 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200940 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200941 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200942 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200943
Harald Welte27b2bb72013-06-22 09:44:00 +0200944 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100945 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
Harald Welte27b2bb72013-06-22 09:44:00 +0200946 "rate of 0, stopping all DL GPRS!\n");
947 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100948 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
Harald Welte27b2bb72013-06-22 09:44:00 +0200949 "rate of != 0, restarting all DL GPRS!\n");
950
951 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100952 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
Harald Welte27b2bb72013-06-22 09:44:00 +0200953 "bucket leak rate of 0, stopping DL GPRS!\n");
954 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
Harald Weltefde19ed2020-12-07 21:43:51 +0100955 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
Harald Welte27b2bb72013-06-22 09:44:00 +0200956 "bucket leak rate != 0, restarting DL GPRS!\n");
957
958 /* reconfigure the timer for flow control based on new values */
959 fc_queue_timer_cfg(bctx->fc);
960
Harald Welte9ba50052010-03-14 15:45:01 +0800961 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200962 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200963 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800964}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200965
Harald Welte25de8112010-05-13 21:26:28 +0200966/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800967static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
968 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800969{
Harald Welteec19c102010-05-02 09:50:42 +0200970 struct bssgp_normal_hdr *bgph =
971 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200972 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800973 int rc = 0;
974
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100975 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
976
Harald Welte58e65c92010-05-13 21:45:23 +0200977 /* If traffic is received on a BVC that is marked as blocked, the
978 * received PDU shall not be accepted and a STATUS PDU (Cause value:
979 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100980 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200981 uint16_t bvci = msgb_bvci(msg);
982 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
983 }
984
Harald Welte9ba50052010-03-14 15:45:01 +0800985 switch (pdu_type) {
986 case BSSGP_PDUT_UL_UNITDATA:
987 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200988 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800989 break;
990 case BSSGP_PDUT_RA_CAPABILITY:
991 /* BSS requests RA capability or IMSI */
Harald Weltefde19ed2020-12-07 21:43:51 +0100992 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200993 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200994 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800995 /* FIXME: send RA_CAPA_UPDATE_ACK */
996 break;
997 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltefde19ed2020-12-07 21:43:51 +0100998 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800999 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +02001000 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +08001001 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001002 case BSSGP_PDUT_FLOW_CONTROL_BVC:
1003 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +02001004 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +08001005 break;
1006 case BSSGP_PDUT_FLOW_CONTROL_MS:
1007 /* BSS informs us of available bandwidth to one MS */
Harald Weltefde19ed2020-12-07 21:43:51 +01001008 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
Harald Weltee9686b62010-05-31 18:07:17 +02001009 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +02001010 /* FIXME: actually implement flow control */
1011 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +08001012 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001013 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001014 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +01001015 break;
Alexander Couzens7819ff62021-02-17 02:30:33 +01001016 case BSSGP_PDUT_BVC_RESET:
1017 rc = bssgp_rx_bvc_reset(msg, tp, bctx->bvci);
1018 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001019 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
1020 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
1021 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
1022 case BSSGP_PDUT_MODIFY_BSS_PFC:
1023 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001024 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
Max2c34ab42016-03-17 15:42:26 +01001025 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001026 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001027 break;
1028 /* those only exist in the SGSN -> BSS direction */
1029 case BSSGP_PDUT_DL_UNITDATA:
1030 case BSSGP_PDUT_PAGING_PS:
1031 case BSSGP_PDUT_PAGING_CS:
1032 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +02001033 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
1034 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001035 DEBUGP(DLBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
Max2c34ab42016-03-17 15:42:26 +01001036 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001037 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1038 rc = -EINVAL;
1039 break;
1040 default:
Harald Weltefde19ed2020-12-07 21:43:51 +01001041 DEBUGP(DLBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
Max2c34ab42016-03-17 15:42:26 +01001042 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001043 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1044 break;
1045 }
1046
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +08001047 return rc;
Harald Welte25de8112010-05-13 21:26:28 +02001048}
1049
1050/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001051static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
1052 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +02001053{
1054 struct bssgp_normal_hdr *bgph =
1055 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1056 uint8_t pdu_type = bgph->pdu_type;
1057 int rc = 0;
1058 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001059 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001060
1061 switch (bgph->pdu_type) {
1062 case BSSGP_PDUT_SUSPEND:
1063 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001064 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001065 break;
1066 case BSSGP_PDUT_RESUME:
1067 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001068 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001069 break;
1070 case BSSGP_PDUT_FLUSH_LL_ACK:
1071 /* BSS informs us it has performed LL FLUSH */
Harald Weltefde19ed2020-12-07 21:43:51 +01001072 DEBUGP(DLBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001073 /* FIXME: send NM_FLUSH_LL.res to NM */
1074 break;
1075 case BSSGP_PDUT_LLC_DISCARD:
1076 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001077 if (!bctx) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001078 LOGP(DLBSSGP, LOGL_ERROR,
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001079 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
1080 goto err_mand_ie;
1081 }
Harald Weltee9686b62010-05-31 18:07:17 +02001082 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001083 break;
1084 case BSSGP_PDUT_BVC_BLOCK:
1085 /* BSS tells us that BVC shall be blocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001086 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1087 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001088 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
Harald Weltee9686b62010-05-31 18:07:17 +02001089 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001090 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001091 }
Harald Welte2677ea52010-05-31 17:16:36 +02001092 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001093 break;
1094 case BSSGP_PDUT_BVC_UNBLOCK:
1095 /* BSS tells us that BVC shall be unblocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001096 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001097 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
Harald Weltee9686b62010-05-31 18:07:17 +02001098 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001099 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001100 }
Harald Welte25de8112010-05-13 21:26:28 +02001101 rc = bssgp_rx_bvc_unblock(msg, tp);
1102 break;
Max590c4022017-06-28 14:29:24 +02001103 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001104 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
Max590c4022017-06-28 14:29:24 +02001105 break;
Harald Welte25de8112010-05-13 21:26:28 +02001106 case BSSGP_PDUT_BVC_RESET:
Alexander Couzens7819ff62021-02-17 02:30:33 +01001107 /* SGSN or BSS tells us that BVC init is required */
Harald Welte2d9ce712020-12-03 15:53:59 +01001108 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1109 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001110 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
Harald Weltee9686b62010-05-31 18:07:17 +02001111 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001112 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001113 }
Harald Welte25de8112010-05-13 21:26:28 +02001114 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1115 break;
1116 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001117 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001118 break;
Philipp Maier1eaa7bc2020-12-16 21:07:04 +01001119
1120 case BSSGP_PDUT_RAN_INFO:
1121 case BSSGP_PDUT_RAN_INFO_REQ:
1122 case BSSGP_PDUT_RAN_INFO_ACK:
1123 case BSSGP_PDUT_RAN_INFO_ERROR:
1124 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
Philipp Maier7741bc32021-01-07 21:55:48 +01001125 rc = bssgp_rx_rim(msg, tp, bvci);
Philipp Maier1eaa7bc2020-12-16 21:07:04 +01001126 break;
1127
Harald Welte25de8112010-05-13 21:26:28 +02001128 /* those only exist in the SGSN -> BSS direction */
1129 case BSSGP_PDUT_PAGING_PS:
1130 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001131 case BSSGP_PDUT_SUSPEND_ACK:
1132 case BSSGP_PDUT_SUSPEND_NACK:
1133 case BSSGP_PDUT_RESUME_ACK:
1134 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001135 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001136 case BSSGP_PDUT_BVC_BLOCK_ACK:
1137 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1138 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltefde19ed2020-12-07 21:43:51 +01001139 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
Max2c34ab42016-03-17 15:42:26 +01001140 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001141 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001142 rc = -EINVAL;
1143 break;
1144 default:
Harald Weltefde19ed2020-12-07 21:43:51 +01001145 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
Max2c34ab42016-03-17 15:42:26 +01001146 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001147 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001148 break;
1149 }
1150
1151 return rc;
1152err_mand_ie:
1153 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1154}
1155
Harald Welte25de8112010-05-13 21:26:28 +02001156/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001157int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001158{
1159 struct bssgp_normal_hdr *bgph =
1160 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1161 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1162 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001163 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001164 uint8_t pdu_type = bgph->pdu_type;
1165 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001166 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001167 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001168 int data_len;
1169 int rc = 0;
1170
1171 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1172
1173 /* UNITDATA BSSGP headers have TLLI in front */
1174 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1175 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1176 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1177 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1178 } else {
1179 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1180 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1181 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001182 if (rc < 0) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001183 LOGP(DLBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
Stefan Sperling2b544b22018-06-25 12:20:43 +02001184 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001185 if (pdu_type != BSSGP_PDUT_STATUS)
1186 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001187 return rc;
1188 }
Harald Welte25de8112010-05-13 21:26:28 +02001189
Harald Welte2d9ce712020-12-03 15:53:59 +01001190 if (bvci == BVCI_SIGNALLING && TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Harald Weltebfe62e52017-05-15 12:48:30 +02001191 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001192
Harald Welte25de8112010-05-13 21:26:28 +02001193 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001194 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001195
Harald Welte16c8dbb2010-05-17 23:30:01 +02001196 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001197 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001198 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
1199 rate_ctr_add(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_BYTES_IN),
Harald Welte16c8dbb2010-05-17 23:30:01 +02001200 msgb_bssgp_len(msg));
1201 }
1202
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001203 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1204 * are otherwise unexpected */
1205 if (pdu_type == BSSGP_PDUT_STATUS)
1206 /* Some exception has occurred */
1207 return bssgp_rx_status(msg, &tp, bvci, bctx);
1208
1209 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1210 * registered if a BVCI is given. */
1211 if (!bctx && bvci != BVCI_SIGNALLING &&
1212 pdu_type != BSSGP_PDUT_BVC_RESET) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001213 LOGP(DLBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001214 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001215 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1216 }
1217
Harald Welte61c07842010-05-18 11:57:08 +02001218 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001219 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001220 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001221 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001222 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001223 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001224 else
Harald Weltefde19ed2020-12-07 21:43:51 +01001225 LOGP(DLBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001226 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1227 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001228
1229 return rc;
1230}
1231
Harald Weltede4599c2012-06-17 13:04:02 +08001232int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1233 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001234{
Harald Welte8a521132010-05-17 22:59:29 +02001235 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001236 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001237 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001238 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001239 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001240 uint16_t bvci = msgb_bvci(msg);
1241 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001242 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001243 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001244
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001245 OSMO_ASSERT(dup != NULL);
1246
Harald Welte30bc19a2010-05-02 11:19:37 +02001247 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001248 if (bvci <= BVCI_PTM ) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001249 LOGP(DLBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001250 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001251 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001252 return -EINVAL;
1253 }
1254
1255 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001256 if (!bctx) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001257 LOGP(DLBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
Harald Welted11c0592012-09-06 21:57:11 +02001258 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001259 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001260 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001261 }
Harald Welte9ba50052010-03-14 15:45:01 +08001262
1263 if (msg->len > TVLV_MAX_ONEBYTE)
1264 llc_pdu_tlv_hdr_len += 1;
1265
1266 /* prepend the tag and length of the LLC-PDU TLV */
1267 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1268 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1269 if (llc_pdu_tlv_hdr_len > 2) {
1270 llc_pdu_tlv[1] = msg_len >> 8;
1271 llc_pdu_tlv[2] = msg_len & 0xff;
1272 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001273 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001274 llc_pdu_tlv[1] |= 0x80;
1275 }
1276
Harald Welte2f946832010-05-31 22:12:30 +02001277 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1278
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001279 /* Old TLLI to help BSS map from old->new */
1280 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001281 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001282 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001283 }
Harald Welte9ba50052010-03-14 15:45:01 +08001284
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001285 /* IMSI */
1286 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001287 uint8_t mi[GSM48_MID_MAX_SIZE];
1288/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1289 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1290 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1291 * mi[131], which is wrong */
1292#pragma GCC diagnostic push
1293#pragma GCC diagnostic ignored "-Warray-bounds"
1294 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1295 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1296 if (imsi_len > 2)
1297 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1298 imsi_len-2, mi+2);
1299#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001300 }
1301
1302 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001303 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001304 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1305 (uint8_t *) &drx_params);
1306
1307 /* FIXME: Priority */
1308
1309 /* MS Radio Access Capability */
1310 if (dup->ms_ra_cap.len)
1311 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1312 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1313
Harald Welte9ba50052010-03-14 15:45:01 +08001314 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001315 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001316
1317 /* prepend the QoS profile, TLLI and pdu type */
1318 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001319 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001320 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001321 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1322
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +02001323 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_OUT));
1324 rate_ctr_add(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_BYTES_OUT), msg->len);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001325
Harald Welte30bc19a2010-05-02 11:19:37 +02001326 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001327
Harald Welted11c0592012-09-06 21:57:11 +02001328 /* check if we have to go through per-ms flow control or can go
1329 * directly to the per-BSS flow control */
1330 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001331 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001332 else
Harald Welted8b47692012-09-07 11:29:32 +02001333 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001334}
Harald Welte68b4f032010-06-09 16:22:28 +02001335
1336/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001337int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1338 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001339{
1340 struct msgb *msg = bssgp_msgb_alloc();
1341 struct bssgp_normal_hdr *bgph =
1342 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001343 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001344 uint8_t mi[GSM48_MID_MAX_SIZE];
1345 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001346 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001347
1348 if (imsi_len < 2)
1349 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001350
1351 msgb_nsei(msg) = nsei;
1352 msgb_bvci(msg) = ns_bvci;
1353
1354 if (pinfo->mode == BSSGP_PAGING_PS)
1355 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1356 else
1357 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1358 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001359/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1360 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1361 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1362 * mi[131], which is wrong */
1363#pragma GCC diagnostic push
1364#pragma GCC diagnostic ignored "-Warray-bounds"
1365 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1366 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1367#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001368 /* DRX Parameters */
1369 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1370 (uint8_t *) &drx_params);
1371 /* Scope */
1372 switch (pinfo->scope) {
1373 case BSSGP_PAGING_BSS_AREA:
1374 {
1375 uint8_t null = 0;
1376 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1377 }
1378 break;
1379 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001380 gsm48_encode_ra(&ra, &pinfo->raid);
1381 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001382 break;
1383 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001384 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001385 break;
1386 case BSSGP_PAGING_BVCI:
1387 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001388 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001389 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1390 }
1391 break;
1392 }
1393 /* QoS profile mandatory for PS */
1394 if (pinfo->mode == BSSGP_PAGING_PS)
1395 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1396
1397 /* Optional (P-)TMSI */
1398 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001399 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001400 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1401 }
1402
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001403 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001404}
Harald Weltecca49632012-06-16 17:45:59 +08001405
Harald Weltede4599c2012-06-17 13:04:02 +08001406void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001407{
Pau Espin Pedrol0e617162020-12-10 13:38:42 +01001408 /* BSSGP has moved from DGPRS to DLGPRS, please update your code if it's
1409 * still calling this function
1410 */
Harald Weltecca49632012-06-16 17:45:59 +08001411}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001412
1413/*!
1414 * \brief Flush the queue of the bssgp_flow_control
1415 * \param[in] The flow control object which holds the queue.
1416 */
1417void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1418{
1419 struct bssgp_fc_queue_element *element, *tmp;
1420
1421 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1422 msgb_free(element->msg);
1423 llist_del(&element->list);
1424 talloc_free(element);
1425 }
1426}
1427
1428/*!
1429 * \brief Flush the queues of all BSSGP contexts.
1430 */
Harald Weltee61d4592022-11-03 11:05:58 +01001431void bssgp_flush_all_queues(void)
Alexander Couzensacc0a072018-08-07 11:22:28 +02001432{
1433 struct bssgp_bvc_ctx *bctx;
1434
1435 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1436 if (bctx->fc)
1437 bssgp_fc_flush_queue(bctx->fc);
1438 }
1439}