blob: bf0e821abf5db4710e60d05387dfb8d40ec83390 [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"
Harald Weltecca49632012-06-16 17:45:59 +080044
Harald Welte6752fa42010-05-02 09:23:16 +020045void *bssgp_tall_ctx = NULL;
46
Alexander Couzens85a8fd32020-07-18 15:57:07 +020047static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg);
48
49bssgp_bvc_send bssgp_ns_send = _gprs_ns_sendmsg;
50void *bssgp_ns_send_data = NULL;
51
Harald Welte25de8112010-05-13 21:26:28 +020052static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080053 { "packets:in", "Packets at BSSGP Level ( In)" },
54 { "packets:out","Packets at BSSGP Level (Out)" },
55 { "bytes:in", "Bytes at BSSGP Level ( In)" },
56 { "bytes:out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020057 { "blocked", "BVC Blocking count" },
58 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010059 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020060};
61
62static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080063 .group_name_prefix = "bssgp:bss_ctx",
Harald Welte25de8112010-05-13 21:26:28 +020064 .group_description = "BSSGP Peer Statistics",
65 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
66 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010067 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020068};
69
Harald Weltea78b9c22010-05-17 23:02:42 +020070LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020071
Harald Welted11c0592012-09-06 21:57:11 +020072static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
73 uint32_t llc_pdu_len, void *priv);
74
Alexander Couzens85a8fd32020-07-18 15:57:07 +020075
Alexander Couzens83fb6862020-09-03 19:30:08 +020076/* callback to be backward compatible with old users which do not set the bssgp_ns_send function */
Alexander Couzens85a8fd32020-07-18 15:57:07 +020077static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg)
78{
79 return gprs_ns_sendmsg(bssgp_nsi, msg);
80}
81
Harald Welte6752fa42010-05-02 09:23:16 +020082/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020083struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020084{
Harald Welte8a521132010-05-17 22:59:29 +020085 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020086
Harald Weltea78b9c22010-05-17 23:02:42 +020087 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020088 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
89 bctx->cell_id == cid)
90 return bctx;
91 }
92 return NULL;
93}
94
Daniel Willmann2d42b902020-09-26 09:11:05 +020095/*! Transmit a BVC-RESET message with a given nsei and bvci (Chapter 10.4.12)
96 * \param[in] nsei The NSEI to transmit over
97 * \param[in] bvci BVCI of the BVC to reset
98 * \param[in] cause The cause of the reset
99 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
100 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
101 */
102int 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)
103{
104 struct msgb *msg = bssgp_msgb_alloc();
105 struct bssgp_normal_hdr *bgph =
106 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
107 uint16_t _bvci = osmo_htons(bvci);
108
109 msgb_nsei(msg) = nsei;
110 msgb_bvci(msg) = 0; /* Signalling */
111 bgph->pdu_type = BSSGP_PDUT_BVC_RESET;
Harald Weltefde19ed2020-12-07 21:43:51 +0100112 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
Daniel Willmann2d42b902020-09-26 09:11:05 +0200113 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
114
115 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
116 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, (uint8_t *) &cause);
117 if (ra_id) {
118 uint8_t bssgp_cid[8];
119 bssgp_create_cell_id(bssgp_cid, ra_id, cell_id);
120 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
121 }
122
123 /* Optional: Feature Bitmap */
124
125 return bssgp_ns_send(bssgp_ns_send_data, msg);
126}
127
Max8b8938f2017-06-29 19:48:29 +0200128/*! Initiate reset procedure for all PTP BVC on a given NSEI.
129 *
130 * This function initiates reset procedure for all PTP BVC with a given cause.
131 * \param[in] nsei NSEI to which PTP BVC should belong to
132 * \param[in] cause Cause of BVC RESET
133 * \returns 0 on success, negative error code otherwise
134 */
135int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
136{
137 int rc;
138 struct bssgp_bvc_ctx *bctx;
139
140 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
141 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100142 LOGP(DLBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
Max8b8938f2017-06-29 19:48:29 +0200143 nsei, bctx->bvci, bssgp_cause_str(cause));
144 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
145 if (rc < 0)
146 return rc;
147 }
148 }
149
150 return 0;
151}
152
Harald Welte6752fa42010-05-02 09:23:16 +0200153/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200154struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200155{
Harald Welte8a521132010-05-17 22:59:29 +0200156 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200157
Harald Weltea78b9c22010-05-17 23:02:42 +0200158 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200159 if (bctx->nsei == nsei && bctx->bvci == bvci)
160 return bctx;
161 }
162 return NULL;
163}
164
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200165void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data)
166{
167 bssgp_ns_send = ns_send;
168 bssgp_ns_send_data = data;
169}
170
Harald Welte8a521132010-05-17 22:59:29 +0200171struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200172{
Harald Welte8a521132010-05-17 22:59:29 +0200173 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200174
Harald Welte8a521132010-05-17 22:59:29 +0200175 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200176 if (!ctx)
177 return NULL;
178 ctx->bvci = bvci;
179 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200180 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
181 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200182 if (!ctx->ctrg)
183 goto err_ctrg;
184
Harald Welted8b47692012-09-07 11:29:32 +0200185 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200186 if (!ctx->fc)
187 goto err_fc;
188
Harald Welted8b47692012-09-07 11:29:32 +0200189 /* cofigure for 2Mbit, 30 packets in queue */
190 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200191
Harald Weltea78b9c22010-05-17 23:02:42 +0200192 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200193
194 return ctx;
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200195
196err_fc:
197 rate_ctr_group_free(ctx->ctrg);
198err_ctrg:
199 talloc_free(ctx);
200 return NULL;
Harald Welte6752fa42010-05-02 09:23:16 +0200201}
202
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700203void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx)
204{
205 if (!ctx)
206 return;
Alexander Couzens495b4a72020-09-16 23:10:03 +0200207
208 osmo_timer_del(&ctx->fc->timer);
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700209 rate_ctr_group_free(ctx->ctrg);
210 llist_del(&ctx->list);
211 talloc_free(ctx);
212}
213
Harald Welte9ba50052010-03-14 15:45:01 +0800214/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200215static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800216{
217 struct msgb *msg = bssgp_msgb_alloc();
218 struct bssgp_normal_hdr *bgph =
219 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
220
Harald Welte24a655f2010-04-30 19:54:29 +0200221 msgb_nsei(msg) = nsei;
222 msgb_bvci(msg) = ns_bvci;
223
Harald Welte9ba50052010-03-14 15:45:01 +0800224 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
225 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
226
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200227 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800228}
229
Harald Weltea8aa4df2010-05-30 22:00:53 +0200230/* 10.3.7 SUSPEND-ACK PDU */
231int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
232 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
233{
234 struct msgb *msg = bssgp_msgb_alloc();
235 struct bssgp_normal_hdr *bgph =
236 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200237
238 msgb_nsei(msg) = nsei;
239 msgb_bvci(msg) = 0; /* Signalling */
240 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
241
Maxe29ec852018-01-05 14:30:22 +0100242 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100243 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200244 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
245
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200246 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200247}
248
249/* 10.3.8 SUSPEND-NACK PDU */
250int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100251 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200252 uint8_t *cause)
253{
254 struct msgb *msg = bssgp_msgb_alloc();
255 struct bssgp_normal_hdr *bgph =
256 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200257
258 msgb_nsei(msg) = nsei;
259 msgb_bvci(msg) = 0; /* Signalling */
260 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
261
Maxe29ec852018-01-05 14:30:22 +0100262 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100263 bssgp_msgb_ra_put(msg, ra_id);
264
Harald Weltea8aa4df2010-05-30 22:00:53 +0200265 if (cause)
266 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
267
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200268 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200269}
270
271/* 10.3.10 RESUME-ACK PDU */
272int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
273 const struct gprs_ra_id *ra_id)
274{
275 struct msgb *msg = bssgp_msgb_alloc();
276 struct bssgp_normal_hdr *bgph =
277 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200278
279 msgb_nsei(msg) = nsei;
280 msgb_bvci(msg) = 0; /* Signalling */
281 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
282
Maxe29ec852018-01-05 14:30:22 +0100283 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100284 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200285
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200286 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200287}
288
289/* 10.3.11 RESUME-NACK PDU */
290int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
291 const struct gprs_ra_id *ra_id, 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
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200310uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200311{
312 /* 6 octets RAC */
313 gsm48_parse_ra(raid, buf);
314 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200315 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200316}
317
Harald Welte28610072011-11-24 21:32:07 +0100318int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
319 uint16_t cid)
320{
Harald Welte28610072011-11-24 21:32:07 +0100321 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100322 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100323 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200324 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100325
326 return 8;
327}
328
Philipp Maierbd10c212020-12-14 22:28:19 +0100329/*! Parse a RIM Routing information IE (3GPP TS 48.018, chapter 11.3.70).
330 * \param[out] ri user provided memory to store the parsed results.
331 * \param[in] buf input buffer of the value part of the IE.
332 * \returns length of parsed octets, -EINVAL on error. */
333int bssgp_parse_rim_ri(struct bssgp_rim_routing_info *ri, const uint8_t *buf,
334 unsigned int len)
335{
336 struct gprs_ra_id raid_temp;
337
338 memset(ri, 0, sizeof(*ri));
339 if (len < 2)
340 return -EINVAL;
341
342 ri->discr = buf[0] & 0x0f;
343
344 switch (ri->discr) {
345 case BSSGP_RIM_ROUTING_INFO_GERAN:
346 if (len < 9)
347 return -EINVAL;
348 ri->geran.cid = bssgp_parse_cell_id(&ri->geran.raid, buf + 1);
349 return 9;
350 case BSSGP_RIM_ROUTING_INFO_UTRAN:
351 if (len < 9)
352 return -EINVAL;
353 gsm48_parse_ra(&ri->utran.raid, buf + 1);
354 ri->utran.rncid = osmo_load16be(buf + 7);
355 return 9;
356 case BSSGP_RIM_ROUTING_INFO_EUTRAN:
357 if (len < 7 || len > 14)
358 return -EINVAL;
359 /* Note: 3GPP TS 24.301 Figure 9.9.3.32.1 and 3GPP TS 24.008
360 * Figure 10.5.130 specify MCC/MNC encoding in the same way,
361 * so we can re-use gsm48_parse_ra() for that. */
362 gsm48_parse_ra(&raid_temp, buf + 1);
363 ri->eutran.tai.mcc = raid_temp.mcc;
364 ri->eutran.tai.mnc = raid_temp.mnc;
365 ri->eutran.tai.mnc_3_digits = raid_temp.mnc_3_digits;
366 ri->eutran.tai.tac = osmo_load16be(buf + 4);
367 memcpy(ri->eutran.global_enb_id, buf + 6, len - 6);
368 ri->eutran.global_enb_id_len = len - 6;
369 return len;
370 default:
371 return -EINVAL;
372 }
373}
374
375/*! Encode a RIM Routing information IE (3GPP TS 48.018, chapter 11.3.70).
376 * \param[out] buf user provided memory (at least 14 byte) for the generated value part of the IE.
377 * \param[in] ri user provided input data struct.
378 * \returns length of encoded octets, -EINVAL on error. */
379int bssgp_create_rim_ri(uint8_t *buf, const struct bssgp_rim_routing_info *ri)
380{
381 int rc;
382 struct gprs_ra_id raid_temp;
383
384 buf[0] = ri->discr & 0x0f;
385 buf++;
386
387 switch (ri->discr) {
388 case BSSGP_RIM_ROUTING_INFO_GERAN:
389 rc = bssgp_create_cell_id(buf, &ri->geran.raid, ri->geran.cid);
390 if (rc < 0)
391 return -EINVAL;
392 return rc + 1;
393 case BSSGP_RIM_ROUTING_INFO_UTRAN:
394 gsm48_encode_ra((struct gsm48_ra_id *)buf, &ri->utran.raid);
395 osmo_store16be(ri->utran.rncid, buf + 6);
396 return 9;
397 case BSSGP_RIM_ROUTING_INFO_EUTRAN:
398 /* Note: 3GPP TS 24.301 Figure 9.9.3.32.1 and 3GPP TS 24.008
399 * Figure 10.5.130 specify MCC/MNC encoding in the same way,
400 * so we can re-use gsm48_encode_ra() for that. */
Vadim Yanitskiyc9f4c492021-01-05 14:53:25 +0100401 raid_temp = (struct gprs_ra_id) {
402 .mcc = ri->eutran.tai.mcc,
403 .mnc = ri->eutran.tai.mnc,
404 .mnc_3_digits = ri->eutran.tai.mnc_3_digits,
405 };
406
Philipp Maierbd10c212020-12-14 22:28:19 +0100407 gsm48_encode_ra((struct gsm48_ra_id *)buf, &raid_temp);
408 osmo_store16be(ri->eutran.tai.tac, buf + 3);
409 OSMO_ASSERT(ri->eutran.global_enb_id_len <=
410 sizeof(ri->eutran.global_enb_id));
411 memcpy(buf + 5, ri->eutran.global_enb_id,
412 ri->eutran.global_enb_id_len);
413 return ri->eutran.global_enb_id_len + 6;
414 default:
415 return -EINVAL;
416 }
417}
418
Harald Welte3fddf3c2010-05-01 16:48:27 +0200419/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200420static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200421 uint16_t ns_bvci)
422{
Harald Welte15a36432012-06-17 12:16:31 +0800423 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200424 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200425 uint16_t nsei = msgb_nsei(msg);
426 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200427
Harald Weltebfe62e52017-05-15 12:48:30 +0200428 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltefde19ed2020-12-07 21:43:51 +0100429 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200430 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
431
Harald Welte6752fa42010-05-02 09:23:16 +0200432 /* look-up or create the BTS context for this BVC */
433 bctx = btsctx_by_bvci_nsei(bvci, nsei);
434 if (!bctx)
435 bctx = btsctx_alloc(bvci, nsei);
436
Harald Welte25de8112010-05-13 21:26:28 +0200437 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
438 bctx->state &= ~BVC_S_BLOCKED;
439
Harald Welte3fddf3c2010-05-01 16:48:27 +0200440 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
441 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200442 if (bvci != 0 && bvci != 1) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100443 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100444 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200445 "missing mandatory IE\n", bvci);
446 return -EINVAL;
447 }
448 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200449 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
450 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Weltefde19ed2020-12-07 21:43:51 +0100451 LOGP(DLBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100452 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200453 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200454
Harald Welte15a36432012-06-17 12:16:31 +0800455 /* Send NM_BVC_RESET.ind to NM */
456 memset(&nmp, 0, sizeof(nmp));
457 nmp.nsei = nsei;
458 nmp.bvci = bvci;
459 nmp.tp = tp;
460 nmp.ra_id = &bctx->ra_id;
461 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
462 PRIM_OP_INDICATION, msg);
463 bssgp_prim_cb(&nmp.oph, NULL);
464
Harald Welte6752fa42010-05-02 09:23:16 +0200465 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200466 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
467 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200468 return 0;
469}
470
Harald Welte25de8112010-05-13 21:26:28 +0200471static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
472{
Harald Welte15a36432012-06-17 12:16:31 +0800473 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100474 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200475 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200476
Harald Weltebfe62e52017-05-15 12:48:30 +0200477 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200478 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200479 /* 8.3.2: Signalling BVC shall never be blocked */
Harald Weltefde19ed2020-12-07 21:43:51 +0100480 LOGP(DLBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
Harald Welte58e65c92010-05-13 21:45:23 +0200481 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100482 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200483 return 0;
484 }
Harald Welte25de8112010-05-13 21:26:28 +0200485
Harald Weltefde19ed2020-12-07 21:43:51 +0100486 LOGP(DLBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200487
Max548caef2019-03-07 13:49:34 +0100488 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200489 if (!ptp_ctx)
490 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
491
492 ptp_ctx->state |= BVC_S_BLOCKED;
493 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
494
Harald Welte15a36432012-06-17 12:16:31 +0800495 /* Send NM_BVC_BLOCK.ind to NM */
496 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100497 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800498 nmp.bvci = bvci;
499 nmp.tp = tp;
500 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
501 PRIM_OP_INDICATION, msg);
502 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200503
504 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100505 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200506 bvci, msgb_bvci(msg));
507};
508
509static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
510{
Harald Welte15a36432012-06-17 12:16:31 +0800511 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100512 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200513 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200514
Harald Weltebfe62e52017-05-15 12:48:30 +0200515 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200516 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200517 /* 8.3.2: Signalling BVC shall never be blocked */
Harald Weltefde19ed2020-12-07 21:43:51 +0100518 LOGP(DLBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
Harald Welte58e65c92010-05-13 21:45:23 +0200519 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100520 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200521 return 0;
522 }
Harald Welte25de8112010-05-13 21:26:28 +0200523
Harald Weltefde19ed2020-12-07 21:43:51 +0100524 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200525
Max548caef2019-03-07 13:49:34 +0100526 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200527 if (!ptp_ctx)
528 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
529
530 ptp_ctx->state &= ~BVC_S_BLOCKED;
531
Harald Welte15a36432012-06-17 12:16:31 +0800532 /* Send NM_BVC_UNBLOCK.ind to NM */
533 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100534 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800535 nmp.bvci = bvci;
536 nmp.tp = tp;
537 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
538 PRIM_OP_INDICATION, msg);
539 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200540
541 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100542 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200543 bvci, msgb_bvci(msg));
544};
545
Harald Welte9ba50052010-03-14 15:45:01 +0800546/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200547static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200548 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800549{
Harald Welte15a36432012-06-17 12:16:31 +0800550 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200551 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800552
Harald Welte6752fa42010-05-02 09:23:16 +0200553 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200554 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800555
Harald Weltefde19ed2020-12-07 21:43:51 +0100556 DEBUGP(DLBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200557
Harald Welte9ba50052010-03-14 15:45:01 +0800558 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte2d9ce712020-12-03 15:53:59 +0100559 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200560 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100561 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
Harald Weltee9686b62010-05-31 18:07:17 +0200562 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200563 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200564 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200565
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200566 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800567 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
568 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800569
Harald Welte15a36432012-06-17 12:16:31 +0800570 /* Send BSSGP_UL_UD.ind to NM */
571 memset(&gbp, 0, sizeof(gbp));
572 gbp.nsei = ctx->nsei;
573 gbp.bvci = ctx->bvci;
574 gbp.tlli = msgb_tlli(msg);
575 gbp.tp = tp;
576 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
577 PRIM_OP_INDICATION, msg);
578 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800579}
580
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200581static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800582{
Harald Welte15a36432012-06-17 12:16:31 +0800583 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200584 struct gprs_ra_id raid;
585 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100586 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200587 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800588
Harald Welte2d9ce712020-12-03 15:53:59 +0100589 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
590 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100591 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200592 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200593 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200594 }
Harald Welte9ba50052010-03-14 15:45:01 +0800595
Harald Weltebfe62e52017-05-15 12:48:30 +0200596 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200597
Harald Weltefde19ed2020-12-07 21:43:51 +0100598 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200599 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200600
Harald Weltea8aa4df2010-05-30 22:00:53 +0200601 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
602
Harald Welte313cccf2010-06-09 11:22:47 +0200603 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800604 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100605 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200606 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800607 gbp.tlli = tlli;
608 gbp.ra_id = &raid;
609 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
610 PRIM_OP_REQUEST, msg);
611
612 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200613 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100614 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200615
Max548caef2019-03-07 13:49:34 +0100616 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200617
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800618 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800619}
620
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200621static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800622{
Harald Welte15a36432012-06-17 12:16:31 +0800623 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200624 struct gprs_ra_id raid;
625 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200626 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100627 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200628 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800629
Harald Welte2d9ce712020-12-03 15:53:59 +0100630 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4 ) ||
631 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6) ||
632 !TLVP_PRES_LEN(tp, BSSGP_IE_SUSPEND_REF_NR, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100633 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200634 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200635 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200636 }
Harald Welte9ba50052010-03-14 15:45:01 +0800637
Harald Weltebfe62e52017-05-15 12:48:30 +0200638 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200639 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200640
Harald Weltefde19ed2020-12-07 21:43:51 +0100641 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200642
Harald Weltea8aa4df2010-05-30 22:00:53 +0200643 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
644
Harald Welte313cccf2010-06-09 11:22:47 +0200645 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800646 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100647 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200648 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800649 gbp.tlli = tlli;
650 gbp.ra_id = &raid;
651 gbp.u.resume.suspend_ref = suspend_ref;
652 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
653 PRIM_OP_REQUEST, msg);
654
655 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200656 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100657 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200658 NULL);
659
Max548caef2019-03-07 13:49:34 +0100660 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800661 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800662}
663
Harald Weltee9686b62010-05-31 18:07:17 +0200664
665static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
666 struct bssgp_bvc_ctx *ctx)
667{
Harald Welte15a36432012-06-17 12:16:31 +0800668 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200669 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100670 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200671
Harald Welte2d9ce712020-12-03 15:53:59 +0100672 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
673 !TLVP_PRES_LEN(tp, BSSGP_IE_LLC_FRAMES_DISCARDED, 1) ||
674 !TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
675 !TLVP_PRES_LEN(tp, BSSGP_IE_NUM_OCT_AFF, 3)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100676 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
Harald Weltee9686b62010-05-31 18:07:17 +0200677 "missing mandatory IE\n", ctx->bvci);
Harald Welte2d9ce712020-12-03 15:53:59 +0100678 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200679 }
680
Harald Welte2d9ce712020-12-03 15:53:59 +0100681 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200682
Harald Weltefde19ed2020-12-07 21:43:51 +0100683 DEBUGP(DLBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200684 ctx->bvci, tlli);
685
686 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
687
Harald Welte15a36432012-06-17 12:16:31 +0800688 /* send NM_LLC_DISCARDED to NM */
689 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100690 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800691 nmp.bvci = ctx->bvci;
692 nmp.tlli = tlli;
693 nmp.tp = tp;
694 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
695 PRIM_OP_INDICATION, msg);
696
697 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200698}
699
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100700int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
701 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
702{
Max548caef2019-03-07 13:49:34 +0100703 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100704 struct osmo_bssgp_prim nmp;
705 enum gprs_bssgp_cause cause;
706
Harald Welte2d9ce712020-12-03 15:53:59 +0100707 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100708 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100709 "missing mandatory IE\n", bvci);
710 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
711 } else {
712 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
713 }
714
Harald Weltefde19ed2020-12-07 21:43:51 +0100715 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100716 bvci, bssgp_cause_str(cause));
717
718 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100719 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2))
Harald Weltefde19ed2020-12-07 21:43:51 +0100720 LOGP(DLBSSGP, LOGL_ERROR,
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100721 "BSSGP BVCI=%u Rx STATUS cause=%s "
722 "missing conditional BVCI IE\n",
723 bvci, bssgp_cause_str(cause));
724 }
725
726 if (bctx)
727 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
728
729 /* send NM_STATUS to NM */
730 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100731 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100732 nmp.bvci = bvci;
733 nmp.tp = tp;
734 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
735 PRIM_OP_INDICATION, msg);
736
737 return bssgp_prim_cb(&nmp.oph, NULL);
738}
739
Philipp Maier1eaa7bc2020-12-16 21:07:04 +0100740static int bssgp_rx_rim(struct msgb *msg, struct tlv_parsed *tp, uint16_t bvci)
741{
742 struct osmo_bssgp_prim nmp;
743 uint16_t nsei = msgb_nsei(msg);
744 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *)msgb_bssgph(msg);
745 enum bssgp_prim prim;
746
747 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RIM-PDU:%s\n", bvci, bssgp_pdu_str(bgph->pdu_type));
748
749 /* Specify PRIM type based on the RIM PDU */
750 switch (bgph->pdu_type) {
751 case BSSGP_PDUT_RAN_INFO:
752 case BSSGP_PDUT_RAN_INFO_REQ:
753 case BSSGP_PDUT_RAN_INFO_ACK:
754 case BSSGP_PDUT_RAN_INFO_ERROR:
755 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
756 prim = PRIM_BSSGP_RIM_PDU_TRANSFER;
757 break;
758 default:
759 /* Caller already makes sure that this can't happen. */
760 OSMO_ASSERT(false);
761 }
762
763 /* Send BSSGP RIM indication to NM */
764 memset(&nmp, 0, sizeof(nmp));
765 nmp.nsei = nsei;
766 nmp.bvci = bvci;
767 nmp.tp = tp;
768 osmo_prim_init(&nmp.oph, SAP_BSSGP_RIM, prim, PRIM_OP_INDICATION, msg);
769 bssgp_prim_cb(&nmp.oph, NULL);
770
771 return 0;
772}
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100773
Harald Welted11c0592012-09-06 21:57:11 +0200774/* One element (msgb) in a BSSGP Flow Control queue */
775struct bssgp_fc_queue_element {
776 /* linked list of queue elements */
777 struct llist_head list;
778 /* The message that we have enqueued */
779 struct msgb *msg;
780 /* Length of the LLC PDU part of the contained message */
781 uint32_t llc_pdu_len;
782 /* private pointer passed to the flow control out_cb function */
783 void *priv;
784};
785
786static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
787static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
788
789static void fc_timer_cb(void *data)
790{
791 struct bssgp_flow_control *fc = data;
792 struct bssgp_fc_queue_element *fcqe;
793 struct timeval time_now;
794
795 /* if the queue is empty, we return without sending something
796 * and without re-starting the timer */
797 if (llist_empty(&fc->queue))
798 return;
799
800 /* get the first entry from the queue */
801 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
802 list);
803
804 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100805 LOGP(DLBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
Harald Welted11c0592012-09-06 21:57:11 +0200806 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
807 /* make sure we re-start the timer */
808 fc_queue_timer_cfg(fc);
809 return;
810 }
811
812 /* remove from the queue */
813 llist_del(&fcqe->list);
814
815 fc->queue_depth--;
816
817 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200818 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200819 fc->time_last_pdu = time_now;
820
821 /* call the output callback for this FC instance */
822 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
823
824 /* we expect that out_cb will in the end free the msgb once
825 * it is no longer needed */
826
827 /* but we have to free the queue element ourselves */
828 talloc_free(fcqe);
829
830 /* re-configure the timer for the next PDU */
831 fc_queue_timer_cfg(fc);
832}
833
834/* configure/schedule the flow control timer to expire once the bucket
835 * will have leaked a sufficient number of bytes to transmit the next
836 * PDU in the queue */
837static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
838{
839 struct bssgp_fc_queue_element *fcqe;
840 uint32_t msecs;
841
842 if (llist_empty(&fc->queue))
843 return 0;
844
Jacob Erlbeck97319352015-04-30 19:28:03 +0200845 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200846 list);
847
Harald Welte27b2bb72013-06-22 09:44:00 +0200848 if (fc->bucket_leak_rate != 0) {
849 /* Calculate the point in time at which we will have leaked
850 * a sufficient number of bytes from the bucket to transmit
851 * the first PDU in the queue */
852 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
853 /* FIXME: add that time to fc->time_last_pdu and subtract it from
854 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200855 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200856 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
857 } else {
858 /* If the PCU is telling us to not send any more data at all,
859 * there's no point starting a timer. */
860 }
Harald Welted11c0592012-09-06 21:57:11 +0200861
862 return 0;
863}
864
865/* Enqueue a PDU in the flow control queue for delayed transmission */
866static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
867 uint32_t llc_pdu_len, void *priv)
868{
869 struct bssgp_fc_queue_element *fcqe;
870
871 if (fc->queue_depth >= fc->max_queue_depth)
872 return -ENOSPC;
873
874 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
875 if (!fcqe)
876 return -ENOMEM;
877 fcqe->msg = msg;
878 fcqe->llc_pdu_len = llc_pdu_len;
879 fcqe->priv = priv;
880
881 llist_add_tail(&fcqe->list, &fc->queue);
882
883 fc->queue_depth++;
884
885 /* re-configure the timer for dequeueing the pdu */
886 fc_queue_timer_cfg(fc);
887
888 return 0;
889}
890
891/* According to Section 8.2 */
892static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
893{
894 struct timeval time_now, time_diff;
895 int64_t bucket_predicted;
896 uint32_t csecs_elapsed, leaked;
897
898 /* B' = B + L(p) - (Tc - Tp)*R */
899
900 /* compute number of centi-seconds that have elapsed since transmitting
901 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200902 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200903 timersub(&time_now, &fc->time_last_pdu, &time_diff);
904 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
905
906 /* compute number of bytes that have leaked in the elapsed number
907 * of centi-seconds */
908 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
909 /* add the current PDU length to the last bucket level */
910 bucket_predicted = fc->bucket_counter + pdu_len;
911 /* ... and subtract the number of leaked bytes */
912 bucket_predicted -= leaked;
913
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700914 if (bucket_predicted < pdu_len)
915 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200916
917 if (bucket_predicted <= fc->bucket_size_max) {
918 /* the bucket is not full yet, we can pass the packet */
919 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700920 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200921 }
922
923 /* bucket is full, PDU needs to be delayed */
924 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200925}
926
927/* output callback for BVC flow control */
928static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
929 uint32_t llc_pdu_len, void *priv)
930{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200931 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200932}
933
934/* input function of the flow control implementation, called first
935 * for the MM flow control, and then as the MM flow control output
936 * callback in order to perform BVC flow control */
937int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
938 uint32_t llc_pdu_len, void *priv)
939{
940 struct timeval time_now;
941
Harald Weltebb826222012-09-07 10:22:01 +0200942 if (llc_pdu_len > fc->bucket_size_max) {
Harald Weltefde19ed2020-12-07 21:43:51 +0100943 LOGP(DLBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
Harald Weltebb826222012-09-07 10:22:01 +0200944 "than maximum bucket size (%u)!\n", llc_pdu_len,
945 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200946 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200947 return -EIO;
948 }
949
Harald Welted11c0592012-09-06 21:57:11 +0200950 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100951 int rc;
952 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
953 if (rc)
954 msgb_free(msg);
955 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200956 } else {
957 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200958 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200959 fc->time_last_pdu = time_now;
960 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
961 }
962}
963
Harald Weltebb826222012-09-07 10:22:01 +0200964
965/* Initialize the Flow Control structure */
966void bssgp_fc_init(struct bssgp_flow_control *fc,
967 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
968 uint32_t max_queue_depth,
969 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
970 uint32_t llc_pdu_len, void *priv))
971{
972 fc->out_cb = out_cb;
973 fc->bucket_size_max = bucket_size_max;
974 fc->bucket_leak_rate = bucket_leak_rate;
975 fc->max_queue_depth = max_queue_depth;
976 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200977 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200978}
979
Harald Welted11c0592012-09-06 21:57:11 +0200980/* Initialize the Flow Control parameters for a new MS according to
981 * default values for the BVC specified by BVCI and NSEI */
982int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200983 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200984{
985 struct bssgp_bvc_ctx *ctx;
986
987 ctx = btsctx_by_bvci_nsei(bvci, nsei);
988 if (!ctx)
989 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200990
991 /* output call-back of per-MS FC is per-CTX FC */
992 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
993 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200994
995 return 0;
996}
997
Harald Welte25de8112010-05-13 21:26:28 +0200998static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200999 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +08001000{
Harald Welte27b2bb72013-06-22 09:44:00 +02001001 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
1002 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +08001003
Harald Weltefde19ed2020-12-07 21:43:51 +01001004 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
Harald Weltee9686b62010-05-31 18:07:17 +02001005 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +08001006
Harald Welte2d9ce712020-12-03 15:53:59 +01001007 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TAG, 1) ||
1008 !TLVP_PRES_LEN(tp, BSSGP_IE_BVC_BUCKET_SIZE, 2) ||
1009 !TLVP_PRES_LEN(tp, BSSGP_IE_BUCKET_LEAK_RATE, 2) ||
1010 !TLVP_PRES_LEN(tp, BSSGP_IE_BMAX_DEFAULT_MS, 2) ||
1011 !TLVP_PRES_LEN(tp, BSSGP_IE_R_DEFAULT_MS,2)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001012 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
Harald Weltee9686b62010-05-31 18:07:17 +02001013 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +08001014 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +02001015 }
Harald Welte9ba50052010-03-14 15:45:01 +08001016
Harald Weltebb826222012-09-07 10:22:01 +02001017 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +02001018 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +02001019 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +02001020 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +02001021 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +02001022 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +02001023 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +02001024 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +02001025
Harald Welte27b2bb72013-06-22 09:44:00 +02001026 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
Harald Weltefde19ed2020-12-07 21:43:51 +01001027 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
Harald Welte27b2bb72013-06-22 09:44:00 +02001028 "rate of 0, stopping all DL GPRS!\n");
1029 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
Harald Weltefde19ed2020-12-07 21:43:51 +01001030 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
Harald Welte27b2bb72013-06-22 09:44:00 +02001031 "rate of != 0, restarting all DL GPRS!\n");
1032
1033 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
Harald Weltefde19ed2020-12-07 21:43:51 +01001034 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
Harald Welte27b2bb72013-06-22 09:44:00 +02001035 "bucket leak rate of 0, stopping DL GPRS!\n");
1036 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
Harald Weltefde19ed2020-12-07 21:43:51 +01001037 LOGP(DLBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
Harald Welte27b2bb72013-06-22 09:44:00 +02001038 "bucket leak rate != 0, restarting DL GPRS!\n");
1039
1040 /* reconfigure the timer for flow control based on new values */
1041 fc_queue_timer_cfg(bctx->fc);
1042
Harald Welte9ba50052010-03-14 15:45:01 +08001043 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +02001044 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +02001045 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001046}
Harald Welte3fddf3c2010-05-01 16:48:27 +02001047
Harald Welte25de8112010-05-13 21:26:28 +02001048/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001049static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
1050 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +08001051{
Harald Welteec19c102010-05-02 09:50:42 +02001052 struct bssgp_normal_hdr *bgph =
1053 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001054 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +08001055 int rc = 0;
1056
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001057 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
1058
Harald Welte58e65c92010-05-13 21:45:23 +02001059 /* If traffic is received on a BVC that is marked as blocked, the
1060 * received PDU shall not be accepted and a STATUS PDU (Cause value:
1061 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001062 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +02001063 uint16_t bvci = msgb_bvci(msg);
1064 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
1065 }
1066
Harald Welte9ba50052010-03-14 15:45:01 +08001067 switch (pdu_type) {
1068 case BSSGP_PDUT_UL_UNITDATA:
1069 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +02001070 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +08001071 break;
1072 case BSSGP_PDUT_RA_CAPABILITY:
1073 /* BSS requests RA capability or IMSI */
Harald Weltefde19ed2020-12-07 21:43:51 +01001074 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
Harald Weltee9686b62010-05-31 18:07:17 +02001075 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +02001076 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +08001077 /* FIXME: send RA_CAPA_UPDATE_ACK */
1078 break;
1079 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltefde19ed2020-12-07 21:43:51 +01001080 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +08001081 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +02001082 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +08001083 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001084 case BSSGP_PDUT_FLOW_CONTROL_BVC:
1085 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +02001086 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +08001087 break;
1088 case BSSGP_PDUT_FLOW_CONTROL_MS:
1089 /* BSS informs us of available bandwidth to one MS */
Harald Weltefde19ed2020-12-07 21:43:51 +01001090 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
Harald Weltee9686b62010-05-31 18:07:17 +02001091 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +02001092 /* FIXME: actually implement flow control */
1093 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +08001094 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001095 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001096 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +01001097 break;
Harald Welte9ba50052010-03-14 15:45:01 +08001098 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
1099 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
1100 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
1101 case BSSGP_PDUT_MODIFY_BSS_PFC:
1102 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001103 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
Max2c34ab42016-03-17 15:42:26 +01001104 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001105 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001106 break;
1107 /* those only exist in the SGSN -> BSS direction */
1108 case BSSGP_PDUT_DL_UNITDATA:
1109 case BSSGP_PDUT_PAGING_PS:
1110 case BSSGP_PDUT_PAGING_CS:
1111 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +02001112 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
1113 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001114 DEBUGP(DLBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
Max2c34ab42016-03-17 15:42:26 +01001115 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001116 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1117 rc = -EINVAL;
1118 break;
1119 default:
Harald Weltefde19ed2020-12-07 21:43:51 +01001120 DEBUGP(DLBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
Max2c34ab42016-03-17 15:42:26 +01001121 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001122 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1123 break;
1124 }
1125
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +08001126 return rc;
Harald Welte25de8112010-05-13 21:26:28 +02001127}
1128
1129/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001130static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
1131 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +02001132{
1133 struct bssgp_normal_hdr *bgph =
1134 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1135 uint8_t pdu_type = bgph->pdu_type;
1136 int rc = 0;
1137 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001138 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001139
1140 switch (bgph->pdu_type) {
1141 case BSSGP_PDUT_SUSPEND:
1142 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001143 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001144 break;
1145 case BSSGP_PDUT_RESUME:
1146 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001147 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001148 break;
1149 case BSSGP_PDUT_FLUSH_LL_ACK:
1150 /* BSS informs us it has performed LL FLUSH */
Harald Weltefde19ed2020-12-07 21:43:51 +01001151 DEBUGP(DLBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001152 /* FIXME: send NM_FLUSH_LL.res to NM */
1153 break;
1154 case BSSGP_PDUT_LLC_DISCARD:
1155 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001156 if (!bctx) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001157 LOGP(DLBSSGP, LOGL_ERROR,
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001158 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
1159 goto err_mand_ie;
1160 }
Harald Weltee9686b62010-05-31 18:07:17 +02001161 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001162 break;
1163 case BSSGP_PDUT_BVC_BLOCK:
1164 /* BSS tells us that BVC shall be blocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001165 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1166 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001167 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
Harald Weltee9686b62010-05-31 18:07:17 +02001168 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001169 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001170 }
Harald Welte2677ea52010-05-31 17:16:36 +02001171 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001172 break;
1173 case BSSGP_PDUT_BVC_UNBLOCK:
1174 /* BSS tells us that BVC shall be unblocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001175 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001176 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
Harald Weltee9686b62010-05-31 18:07:17 +02001177 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001178 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001179 }
Harald Welte25de8112010-05-13 21:26:28 +02001180 rc = bssgp_rx_bvc_unblock(msg, tp);
1181 break;
Max590c4022017-06-28 14:29:24 +02001182 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Weltefde19ed2020-12-07 21:43:51 +01001183 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
Max590c4022017-06-28 14:29:24 +02001184 break;
Harald Welte25de8112010-05-13 21:26:28 +02001185 case BSSGP_PDUT_BVC_RESET:
1186 /* BSS tells us that BVC init is required */
Harald Welte2d9ce712020-12-03 15:53:59 +01001187 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1188 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001189 LOGP(DLBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
Harald Weltee9686b62010-05-31 18:07:17 +02001190 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001191 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001192 }
Harald Welte25de8112010-05-13 21:26:28 +02001193 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1194 break;
1195 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001196 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001197 break;
Philipp Maier1eaa7bc2020-12-16 21:07:04 +01001198
1199 case BSSGP_PDUT_RAN_INFO:
1200 case BSSGP_PDUT_RAN_INFO_REQ:
1201 case BSSGP_PDUT_RAN_INFO_ACK:
1202 case BSSGP_PDUT_RAN_INFO_ERROR:
1203 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
1204 bssgp_rx_rim(msg, tp, bvci);
1205 break;
1206
Harald Welte25de8112010-05-13 21:26:28 +02001207 /* those only exist in the SGSN -> BSS direction */
1208 case BSSGP_PDUT_PAGING_PS:
1209 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001210 case BSSGP_PDUT_SUSPEND_ACK:
1211 case BSSGP_PDUT_SUSPEND_NACK:
1212 case BSSGP_PDUT_RESUME_ACK:
1213 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001214 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001215 case BSSGP_PDUT_BVC_BLOCK_ACK:
1216 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1217 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltefde19ed2020-12-07 21:43:51 +01001218 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
Max2c34ab42016-03-17 15:42:26 +01001219 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001220 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001221 rc = -EINVAL;
1222 break;
1223 default:
Harald Weltefde19ed2020-12-07 21:43:51 +01001224 DEBUGP(DLBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
Max2c34ab42016-03-17 15:42:26 +01001225 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001226 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001227 break;
1228 }
1229
1230 return rc;
1231err_mand_ie:
1232 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1233}
1234
Harald Welte25de8112010-05-13 21:26:28 +02001235/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001236int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001237{
1238 struct bssgp_normal_hdr *bgph =
1239 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1240 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1241 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001242 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001243 uint8_t pdu_type = bgph->pdu_type;
1244 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001245 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001246 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001247 int data_len;
1248 int rc = 0;
1249
1250 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1251
1252 /* UNITDATA BSSGP headers have TLLI in front */
1253 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1254 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1255 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1256 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1257 } else {
1258 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1259 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1260 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001261 if (rc < 0) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001262 LOGP(DLBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
Stefan Sperling2b544b22018-06-25 12:20:43 +02001263 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001264 if (pdu_type != BSSGP_PDUT_STATUS)
1265 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001266 return rc;
1267 }
Harald Welte25de8112010-05-13 21:26:28 +02001268
Harald Welte2d9ce712020-12-03 15:53:59 +01001269 if (bvci == BVCI_SIGNALLING && TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Harald Weltebfe62e52017-05-15 12:48:30 +02001270 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001271
Harald Welte25de8112010-05-13 21:26:28 +02001272 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001273 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001274
Harald Welte16c8dbb2010-05-17 23:30:01 +02001275 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001276 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001277 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1278 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1279 msgb_bssgp_len(msg));
1280 }
1281
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001282 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1283 * are otherwise unexpected */
1284 if (pdu_type == BSSGP_PDUT_STATUS)
1285 /* Some exception has occurred */
1286 return bssgp_rx_status(msg, &tp, bvci, bctx);
1287
1288 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1289 * registered if a BVCI is given. */
1290 if (!bctx && bvci != BVCI_SIGNALLING &&
1291 pdu_type != BSSGP_PDUT_BVC_RESET) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001292 LOGP(DLBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001293 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001294 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1295 }
1296
Harald Welte61c07842010-05-18 11:57:08 +02001297 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001298 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001299 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001300 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001301 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001302 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001303 else
Harald Weltefde19ed2020-12-07 21:43:51 +01001304 LOGP(DLBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001305 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1306 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001307
1308 return rc;
1309}
1310
Harald Weltede4599c2012-06-17 13:04:02 +08001311int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1312 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001313{
Harald Welte8a521132010-05-17 22:59:29 +02001314 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001315 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001316 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001317 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001318 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001319 uint16_t bvci = msgb_bvci(msg);
1320 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001321 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001322 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001323
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001324 OSMO_ASSERT(dup != NULL);
1325
Harald Welte30bc19a2010-05-02 11:19:37 +02001326 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001327 if (bvci <= BVCI_PTM ) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001328 LOGP(DLBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001329 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001330 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001331 return -EINVAL;
1332 }
1333
1334 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001335 if (!bctx) {
Harald Weltefde19ed2020-12-07 21:43:51 +01001336 LOGP(DLBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
Harald Welted11c0592012-09-06 21:57:11 +02001337 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001338 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001339 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001340 }
Harald Welte9ba50052010-03-14 15:45:01 +08001341
1342 if (msg->len > TVLV_MAX_ONEBYTE)
1343 llc_pdu_tlv_hdr_len += 1;
1344
1345 /* prepend the tag and length of the LLC-PDU TLV */
1346 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1347 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1348 if (llc_pdu_tlv_hdr_len > 2) {
1349 llc_pdu_tlv[1] = msg_len >> 8;
1350 llc_pdu_tlv[2] = msg_len & 0xff;
1351 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001352 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001353 llc_pdu_tlv[1] |= 0x80;
1354 }
1355
Harald Welte2f946832010-05-31 22:12:30 +02001356 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1357
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001358 /* Old TLLI to help BSS map from old->new */
1359 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001360 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001361 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001362 }
Harald Welte9ba50052010-03-14 15:45:01 +08001363
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001364 /* IMSI */
1365 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001366 uint8_t mi[GSM48_MID_MAX_SIZE];
1367/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1368 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1369 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1370 * mi[131], which is wrong */
1371#pragma GCC diagnostic push
1372#pragma GCC diagnostic ignored "-Warray-bounds"
1373 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1374 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1375 if (imsi_len > 2)
1376 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1377 imsi_len-2, mi+2);
1378#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001379 }
1380
1381 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001382 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001383 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1384 (uint8_t *) &drx_params);
1385
1386 /* FIXME: Priority */
1387
1388 /* MS Radio Access Capability */
1389 if (dup->ms_ra_cap.len)
1390 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1391 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1392
Harald Welte9ba50052010-03-14 15:45:01 +08001393 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001394 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001395
1396 /* prepend the QoS profile, TLLI and pdu type */
1397 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001398 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001399 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001400 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1401
Harald Welte16c8dbb2010-05-17 23:30:01 +02001402 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1403 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1404
Harald Welte30bc19a2010-05-02 11:19:37 +02001405 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001406
Harald Welted11c0592012-09-06 21:57:11 +02001407 /* check if we have to go through per-ms flow control or can go
1408 * directly to the per-BSS flow control */
1409 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001410 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001411 else
Harald Welted8b47692012-09-07 11:29:32 +02001412 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001413}
Harald Welte68b4f032010-06-09 16:22:28 +02001414
1415/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001416int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1417 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001418{
1419 struct msgb *msg = bssgp_msgb_alloc();
1420 struct bssgp_normal_hdr *bgph =
1421 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001422 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001423 uint8_t mi[GSM48_MID_MAX_SIZE];
1424 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001425 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001426
1427 if (imsi_len < 2)
1428 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001429
1430 msgb_nsei(msg) = nsei;
1431 msgb_bvci(msg) = ns_bvci;
1432
1433 if (pinfo->mode == BSSGP_PAGING_PS)
1434 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1435 else
1436 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1437 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001438/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1439 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1440 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1441 * mi[131], which is wrong */
1442#pragma GCC diagnostic push
1443#pragma GCC diagnostic ignored "-Warray-bounds"
1444 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1445 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1446#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001447 /* DRX Parameters */
1448 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1449 (uint8_t *) &drx_params);
1450 /* Scope */
1451 switch (pinfo->scope) {
1452 case BSSGP_PAGING_BSS_AREA:
1453 {
1454 uint8_t null = 0;
1455 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1456 }
1457 break;
1458 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001459 gsm48_encode_ra(&ra, &pinfo->raid);
1460 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001461 break;
1462 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001463 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001464 break;
1465 case BSSGP_PAGING_BVCI:
1466 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001467 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001468 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1469 }
1470 break;
1471 }
1472 /* QoS profile mandatory for PS */
1473 if (pinfo->mode == BSSGP_PAGING_PS)
1474 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1475
1476 /* Optional (P-)TMSI */
1477 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001478 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001479 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1480 }
1481
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001482 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001483}
Harald Weltecca49632012-06-16 17:45:59 +08001484
Harald Weltede4599c2012-06-17 13:04:02 +08001485void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001486{
Pau Espin Pedrol0e617162020-12-10 13:38:42 +01001487 /* BSSGP has moved from DGPRS to DLGPRS, please update your code if it's
1488 * still calling this function
1489 */
Harald Weltecca49632012-06-16 17:45:59 +08001490}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001491
1492/*!
1493 * \brief Flush the queue of the bssgp_flow_control
1494 * \param[in] The flow control object which holds the queue.
1495 */
1496void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1497{
1498 struct bssgp_fc_queue_element *element, *tmp;
1499
1500 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1501 msgb_free(element->msg);
1502 llist_del(&element->list);
1503 talloc_free(element);
1504 }
1505}
1506
1507/*!
1508 * \brief Flush the queues of all BSSGP contexts.
1509 */
1510void bssgp_flush_all_queues()
1511{
1512 struct bssgp_bvc_ctx *bctx;
1513
1514 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1515 if (bctx->fc)
1516 bssgp_fc_flush_queue(bctx->fc);
1517 }
1518}