blob: 7fb3a30253fcf29e4d4338352ba4b61ca729c16a [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_bssgp.c
2 * GPRS BSSGP protocol implementation as per 3GPP TS 08.18. */
3/*
4 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte9ba50052010-03-14 15:45:01 +080010 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080013 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080019 *
Harald Welte7fa89c22014-10-26 20:33:09 +010020 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080022 *
Harald Welte4e5721d2010-05-17 23:41:43 +020023 * TODO:
24 * o properly count incoming BVC-RESET packets in counter group
25 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080026 */
27
28#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020029#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080030
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010031#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020032#include <osmocom/core/byteswap.h>
33#include <osmocom/core/bit16gen.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010034#include <osmocom/gsm/tlv.h>
35#include <osmocom/core/talloc.h>
36#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010037#include <osmocom/core/stats.h>
Harald Welte6752fa42010-05-02 09:23:16 +020038
Harald Welte73952e32012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_bssgp.h>
Max8b8938f2017-06-29 19:48:29 +020040#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte73952e32012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_ns.h>
42
Harald Weltecca49632012-06-16 17:45:59 +080043#include "common_vty.h"
Daniel Willmann2d42b902020-09-26 09:11:05 +020044#include "osmocom/gsm/gsm48.h"
Harald Weltecca49632012-06-16 17:45:59 +080045
Harald Welte6752fa42010-05-02 09:23:16 +020046void *bssgp_tall_ctx = NULL;
47
Alexander Couzens85a8fd32020-07-18 15:57:07 +020048static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg);
49
50bssgp_bvc_send bssgp_ns_send = _gprs_ns_sendmsg;
51void *bssgp_ns_send_data = NULL;
52
Harald Welte25de8112010-05-13 21:26:28 +020053static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080054 { "packets:in", "Packets at BSSGP Level ( In)" },
55 { "packets:out","Packets at BSSGP Level (Out)" },
56 { "bytes:in", "Bytes at BSSGP Level ( In)" },
57 { "bytes:out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020058 { "blocked", "BVC Blocking count" },
59 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010060 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020061};
62
63static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080064 .group_name_prefix = "bssgp:bss_ctx",
Harald Welte25de8112010-05-13 21:26:28 +020065 .group_description = "BSSGP Peer Statistics",
66 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
67 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010068 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020069};
70
Harald Weltea78b9c22010-05-17 23:02:42 +020071LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020072
Harald Welted11c0592012-09-06 21:57:11 +020073static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
74 uint32_t llc_pdu_len, void *priv);
75
Alexander Couzens85a8fd32020-07-18 15:57:07 +020076
Alexander Couzens83fb6862020-09-03 19:30:08 +020077/* callback to be backward compatible with old users which do not set the bssgp_ns_send function */
Alexander Couzens85a8fd32020-07-18 15:57:07 +020078static int _gprs_ns_sendmsg(void *ctx, struct msgb *msg)
79{
80 return gprs_ns_sendmsg(bssgp_nsi, msg);
81}
82
Harald Welte6752fa42010-05-02 09:23:16 +020083/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020084struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020085{
Harald Welte8a521132010-05-17 22:59:29 +020086 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020087
Harald Weltea78b9c22010-05-17 23:02:42 +020088 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020089 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
90 bctx->cell_id == cid)
91 return bctx;
92 }
93 return NULL;
94}
95
Daniel Willmann2d42b902020-09-26 09:11:05 +020096/*! Transmit a BVC-RESET message with a given nsei and bvci (Chapter 10.4.12)
97 * \param[in] nsei The NSEI to transmit over
98 * \param[in] bvci BVCI of the BVC to reset
99 * \param[in] cause The cause of the reset
100 * \param[in] ra_id Pointer to the ra_id to include. If NULL no cell information will be included
101 * \param[in] cell_id The cell_id to include (if ra_id is not NULL)
102 */
103int 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)
104{
105 struct msgb *msg = bssgp_msgb_alloc();
106 struct bssgp_normal_hdr *bgph =
107 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
108 uint16_t _bvci = osmo_htons(bvci);
109
110 msgb_nsei(msg) = nsei;
111 msgb_bvci(msg) = 0; /* Signalling */
112 bgph->pdu_type = BSSGP_PDUT_BVC_RESET;
113 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
114 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
115
116 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
117 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, (uint8_t *) &cause);
118 if (ra_id) {
119 uint8_t bssgp_cid[8];
120 bssgp_create_cell_id(bssgp_cid, ra_id, cell_id);
121 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
122 }
123
124 /* Optional: Feature Bitmap */
125
126 return bssgp_ns_send(bssgp_ns_send_data, msg);
127}
128
Max8b8938f2017-06-29 19:48:29 +0200129/*! Initiate reset procedure for all PTP BVC on a given NSEI.
130 *
131 * This function initiates reset procedure for all PTP BVC with a given cause.
132 * \param[in] nsei NSEI to which PTP BVC should belong to
133 * \param[in] cause Cause of BVC RESET
134 * \returns 0 on success, negative error code otherwise
135 */
136int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
137{
138 int rc;
139 struct bssgp_bvc_ctx *bctx;
140
141 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
142 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
143 LOGP(DBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
144 nsei, bctx->bvci, bssgp_cause_str(cause));
145 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
146 if (rc < 0)
147 return rc;
148 }
149 }
150
151 return 0;
152}
153
Harald Welte6752fa42010-05-02 09:23:16 +0200154/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200155struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200156{
Harald Welte8a521132010-05-17 22:59:29 +0200157 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200158
Harald Weltea78b9c22010-05-17 23:02:42 +0200159 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200160 if (bctx->nsei == nsei && bctx->bvci == bvci)
161 return bctx;
162 }
163 return NULL;
164}
165
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200166void bssgp_set_bssgp_callback(bssgp_bvc_send ns_send, void *data)
167{
168 bssgp_ns_send = ns_send;
169 bssgp_ns_send_data = data;
170}
171
Harald Welte8a521132010-05-17 22:59:29 +0200172struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200173{
Harald Welte8a521132010-05-17 22:59:29 +0200174 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200175
Harald Welte8a521132010-05-17 22:59:29 +0200176 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200177 if (!ctx)
178 return NULL;
179 ctx->bvci = bvci;
180 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200181 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
182 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200183 if (!ctx->ctrg)
184 goto err_ctrg;
185
Harald Welted8b47692012-09-07 11:29:32 +0200186 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200187 if (!ctx->fc)
188 goto err_fc;
189
Harald Welted8b47692012-09-07 11:29:32 +0200190 /* cofigure for 2Mbit, 30 packets in queue */
191 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200192
Harald Weltea78b9c22010-05-17 23:02:42 +0200193 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200194
195 return ctx;
Alexander Couzens6a2c0742020-09-16 23:09:24 +0200196
197err_fc:
198 rate_ctr_group_free(ctx->ctrg);
199err_ctrg:
200 talloc_free(ctx);
201 return NULL;
Harald Welte6752fa42010-05-02 09:23:16 +0200202}
203
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700204void bssgp_bvc_ctx_free(struct bssgp_bvc_ctx *ctx)
205{
206 if (!ctx)
207 return;
Alexander Couzens495b4a72020-09-16 23:10:03 +0200208
209 osmo_timer_del(&ctx->fc->timer);
Vadim Yanitskiy8eae2fc2019-11-09 01:45:11 +0700210 rate_ctr_group_free(ctx->ctrg);
211 llist_del(&ctx->list);
212 talloc_free(ctx);
213}
214
Harald Welte9ba50052010-03-14 15:45:01 +0800215/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200216static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800217{
218 struct msgb *msg = bssgp_msgb_alloc();
219 struct bssgp_normal_hdr *bgph =
220 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
221
Harald Welte24a655f2010-04-30 19:54:29 +0200222 msgb_nsei(msg) = nsei;
223 msgb_bvci(msg) = ns_bvci;
224
Harald Welte9ba50052010-03-14 15:45:01 +0800225 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
226 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
227
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200228 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800229}
230
Harald Weltea8aa4df2010-05-30 22:00:53 +0200231/* 10.3.7 SUSPEND-ACK PDU */
232int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
233 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
234{
235 struct msgb *msg = bssgp_msgb_alloc();
236 struct bssgp_normal_hdr *bgph =
237 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200238
239 msgb_nsei(msg) = nsei;
240 msgb_bvci(msg) = 0; /* Signalling */
241 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
242
Maxe29ec852018-01-05 14:30:22 +0100243 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100244 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200245 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
246
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200247 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200248}
249
250/* 10.3.8 SUSPEND-NACK PDU */
251int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100252 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200253 uint8_t *cause)
254{
255 struct msgb *msg = bssgp_msgb_alloc();
256 struct bssgp_normal_hdr *bgph =
257 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200258
259 msgb_nsei(msg) = nsei;
260 msgb_bvci(msg) = 0; /* Signalling */
261 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
262
Maxe29ec852018-01-05 14:30:22 +0100263 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100264 bssgp_msgb_ra_put(msg, ra_id);
265
Harald Weltea8aa4df2010-05-30 22:00:53 +0200266 if (cause)
267 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
268
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200269 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200270}
271
272/* 10.3.10 RESUME-ACK PDU */
273int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
274 const struct gprs_ra_id *ra_id)
275{
276 struct msgb *msg = bssgp_msgb_alloc();
277 struct bssgp_normal_hdr *bgph =
278 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200279
280 msgb_nsei(msg) = nsei;
281 msgb_bvci(msg) = 0; /* Signalling */
282 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
283
Maxe29ec852018-01-05 14:30:22 +0100284 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100285 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200286
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200287 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200288}
289
290/* 10.3.11 RESUME-NACK PDU */
291int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
292 const struct gprs_ra_id *ra_id, uint8_t *cause)
293{
294 struct msgb *msg = bssgp_msgb_alloc();
295 struct bssgp_normal_hdr *bgph =
296 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200297
298 msgb_nsei(msg) = nsei;
299 msgb_bvci(msg) = 0; /* Signalling */
300 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
301
Maxe29ec852018-01-05 14:30:22 +0100302 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100303 bssgp_msgb_ra_put(msg, ra_id);
304
Harald Weltea8aa4df2010-05-30 22:00:53 +0200305 if (cause)
306 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
307
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200308 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200309}
310
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200311uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200312{
313 /* 6 octets RAC */
314 gsm48_parse_ra(raid, buf);
315 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200316 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200317}
318
Harald Welte28610072011-11-24 21:32:07 +0100319int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
320 uint16_t cid)
321{
Harald Welte28610072011-11-24 21:32:07 +0100322 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100323 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100324 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200325 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100326
327 return 8;
328}
329
Harald Welte3fddf3c2010-05-01 16:48:27 +0200330/* Chapter 8.4 BVC-Reset Procedure */
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200331static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200332 uint16_t ns_bvci)
333{
Harald Welte15a36432012-06-17 12:16:31 +0800334 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200335 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200336 uint16_t nsei = msgb_nsei(msg);
337 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200338
Harald Weltebfe62e52017-05-15 12:48:30 +0200339 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200340 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200341 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
342
Harald Welte6752fa42010-05-02 09:23:16 +0200343 /* look-up or create the BTS context for this BVC */
344 bctx = btsctx_by_bvci_nsei(bvci, nsei);
345 if (!bctx)
346 bctx = btsctx_alloc(bvci, nsei);
347
Harald Welte25de8112010-05-13 21:26:28 +0200348 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
349 bctx->state &= ~BVC_S_BLOCKED;
350
Harald Welte3fddf3c2010-05-01 16:48:27 +0200351 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
352 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200353 if (bvci != 0 && bvci != 1) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100354 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8)) {
Harald Welte086fe322011-08-19 16:45:19 +0200355 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200356 "missing mandatory IE\n", bvci);
357 return -EINVAL;
358 }
359 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200360 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
361 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100362 LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
363 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200364 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200365
Harald Welte15a36432012-06-17 12:16:31 +0800366 /* Send NM_BVC_RESET.ind to NM */
367 memset(&nmp, 0, sizeof(nmp));
368 nmp.nsei = nsei;
369 nmp.bvci = bvci;
370 nmp.tp = tp;
371 nmp.ra_id = &bctx->ra_id;
372 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
373 PRIM_OP_INDICATION, msg);
374 bssgp_prim_cb(&nmp.oph, NULL);
375
Harald Welte6752fa42010-05-02 09:23:16 +0200376 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200377 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
378 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200379 return 0;
380}
381
Harald Welte25de8112010-05-13 21:26:28 +0200382static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
383{
Harald Welte15a36432012-06-17 12:16:31 +0800384 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100385 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200386 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200387
Harald Weltebfe62e52017-05-15 12:48:30 +0200388 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200389 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200390 /* 8.3.2: Signalling BVC shall never be blocked */
391 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
392 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100393 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200394 return 0;
395 }
Harald Welte25de8112010-05-13 21:26:28 +0200396
Harald Welte086fe322011-08-19 16:45:19 +0200397 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200398
Max548caef2019-03-07 13:49:34 +0100399 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200400 if (!ptp_ctx)
401 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
402
403 ptp_ctx->state |= BVC_S_BLOCKED;
404 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
405
Harald Welte15a36432012-06-17 12:16:31 +0800406 /* Send NM_BVC_BLOCK.ind to NM */
407 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100408 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800409 nmp.bvci = bvci;
410 nmp.tp = tp;
411 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
412 PRIM_OP_INDICATION, msg);
413 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200414
415 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100416 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200417 bvci, msgb_bvci(msg));
418};
419
420static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
421{
Harald Welte15a36432012-06-17 12:16:31 +0800422 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100423 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200424 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200425
Harald Weltebfe62e52017-05-15 12:48:30 +0200426 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200427 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200428 /* 8.3.2: Signalling BVC shall never be blocked */
429 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
430 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100431 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200432 return 0;
433 }
Harald Welte25de8112010-05-13 21:26:28 +0200434
Harald Weltee9686b62010-05-31 18:07:17 +0200435 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200436
Max548caef2019-03-07 13:49:34 +0100437 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200438 if (!ptp_ctx)
439 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
440
441 ptp_ctx->state &= ~BVC_S_BLOCKED;
442
Harald Welte15a36432012-06-17 12:16:31 +0800443 /* Send NM_BVC_UNBLOCK.ind to NM */
444 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100445 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800446 nmp.bvci = bvci;
447 nmp.tp = tp;
448 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
449 PRIM_OP_INDICATION, msg);
450 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200451
452 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100453 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200454 bvci, msgb_bvci(msg));
455};
456
Harald Welte9ba50052010-03-14 15:45:01 +0800457/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200458static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200459 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800460{
Harald Welte15a36432012-06-17 12:16:31 +0800461 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200462 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800463
Harald Welte6752fa42010-05-02 09:23:16 +0200464 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200465 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800466
Harald Welte086fe322011-08-19 16:45:19 +0200467 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200468
Harald Welte9ba50052010-03-14 15:45:01 +0800469 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte2d9ce712020-12-03 15:53:59 +0100470 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CELL_ID, 8) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200471 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
472 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
473 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200474 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200475 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200476
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200477 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800478 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
479 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800480
Harald Welte15a36432012-06-17 12:16:31 +0800481 /* Send BSSGP_UL_UD.ind to NM */
482 memset(&gbp, 0, sizeof(gbp));
483 gbp.nsei = ctx->nsei;
484 gbp.bvci = ctx->bvci;
485 gbp.tlli = msgb_tlli(msg);
486 gbp.tp = tp;
487 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
488 PRIM_OP_INDICATION, msg);
489 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800490}
491
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200492static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800493{
Harald Welte15a36432012-06-17 12:16:31 +0800494 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200495 struct gprs_ra_id raid;
496 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100497 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200498 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800499
Harald Welte2d9ce712020-12-03 15:53:59 +0100500 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
501 !TLVP_PRES_LEN(tp, BSSGP_IE_ROUTEING_AREA, 6)) {
Harald Weltee9686b62010-05-31 18:07:17 +0200502 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200503 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200504 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200505 }
Harald Welte9ba50052010-03-14 15:45:01 +0800506
Harald Weltebfe62e52017-05-15 12:48:30 +0200507 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200508
Harald Welte17925322010-05-31 20:18:35 +0200509 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200510 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200511
Harald Weltea8aa4df2010-05-30 22:00:53 +0200512 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
513
Harald Welte313cccf2010-06-09 11:22:47 +0200514 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800515 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100516 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200517 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800518 gbp.tlli = tlli;
519 gbp.ra_id = &raid;
520 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
521 PRIM_OP_REQUEST, msg);
522
523 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200524 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100525 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200526
Max548caef2019-03-07 13:49:34 +0100527 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200528
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800529 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800530}
531
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200532static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800533{
Harald Welte15a36432012-06-17 12:16:31 +0800534 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200535 struct gprs_ra_id raid;
536 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200537 uint8_t suspend_ref;
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) ||
543 !TLVP_PRES_LEN(tp, BSSGP_IE_SUSPEND_REF_NR, 1)) {
Harald Weltee9686b62010-05-31 18:07:17 +0200544 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200545 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200546 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200547 }
Harald Welte9ba50052010-03-14 15:45:01 +0800548
Harald Weltebfe62e52017-05-15 12:48:30 +0200549 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200550 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200551
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200552 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200553
Harald Weltea8aa4df2010-05-30 22:00:53 +0200554 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
555
Harald Welte313cccf2010-06-09 11:22:47 +0200556 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800557 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100558 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200559 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800560 gbp.tlli = tlli;
561 gbp.ra_id = &raid;
562 gbp.u.resume.suspend_ref = suspend_ref;
563 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
564 PRIM_OP_REQUEST, msg);
565
566 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200567 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100568 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200569 NULL);
570
Max548caef2019-03-07 13:49:34 +0100571 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800572 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800573}
574
Harald Weltee9686b62010-05-31 18:07:17 +0200575
576static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
577 struct bssgp_bvc_ctx *ctx)
578{
Harald Welte15a36432012-06-17 12:16:31 +0800579 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200580 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100581 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200582
Harald Welte2d9ce712020-12-03 15:53:59 +0100583 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TLLI, 4) ||
584 !TLVP_PRES_LEN(tp, BSSGP_IE_LLC_FRAMES_DISCARDED, 1) ||
585 !TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
586 !TLVP_PRES_LEN(tp, BSSGP_IE_NUM_OCT_AFF, 3)) {
Harald Weltee9686b62010-05-31 18:07:17 +0200587 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
588 "missing mandatory IE\n", ctx->bvci);
Harald Welte2d9ce712020-12-03 15:53:59 +0100589 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200590 }
591
Harald Welte2d9ce712020-12-03 15:53:59 +0100592 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200593
Harald Welte086fe322011-08-19 16:45:19 +0200594 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200595 ctx->bvci, tlli);
596
597 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
598
Harald Welte15a36432012-06-17 12:16:31 +0800599 /* send NM_LLC_DISCARDED to NM */
600 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100601 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800602 nmp.bvci = ctx->bvci;
603 nmp.tlli = tlli;
604 nmp.tp = tp;
605 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
606 PRIM_OP_INDICATION, msg);
607
608 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200609}
610
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100611int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
612 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
613{
Max548caef2019-03-07 13:49:34 +0100614 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100615 struct osmo_bssgp_prim nmp;
616 enum gprs_bssgp_cause cause;
617
Harald Welte2d9ce712020-12-03 15:53:59 +0100618 if (!TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100619 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
620 "missing mandatory IE\n", bvci);
621 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
622 } else {
623 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
624 }
625
626 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
627 bvci, bssgp_cause_str(cause));
628
629 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
Harald Welte2d9ce712020-12-03 15:53:59 +0100630 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2))
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100631 LOGP(DBSSGP, LOGL_ERROR,
632 "BSSGP BVCI=%u Rx STATUS cause=%s "
633 "missing conditional BVCI IE\n",
634 bvci, bssgp_cause_str(cause));
635 }
636
637 if (bctx)
638 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
639
640 /* send NM_STATUS to NM */
641 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100642 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100643 nmp.bvci = bvci;
644 nmp.tp = tp;
645 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
646 PRIM_OP_INDICATION, msg);
647
648 return bssgp_prim_cb(&nmp.oph, NULL);
649}
650
651
Harald Welted11c0592012-09-06 21:57:11 +0200652/* One element (msgb) in a BSSGP Flow Control queue */
653struct bssgp_fc_queue_element {
654 /* linked list of queue elements */
655 struct llist_head list;
656 /* The message that we have enqueued */
657 struct msgb *msg;
658 /* Length of the LLC PDU part of the contained message */
659 uint32_t llc_pdu_len;
660 /* private pointer passed to the flow control out_cb function */
661 void *priv;
662};
663
664static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
665static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
666
667static void fc_timer_cb(void *data)
668{
669 struct bssgp_flow_control *fc = data;
670 struct bssgp_fc_queue_element *fcqe;
671 struct timeval time_now;
672
673 /* if the queue is empty, we return without sending something
674 * and without re-starting the timer */
675 if (llist_empty(&fc->queue))
676 return;
677
678 /* get the first entry from the queue */
679 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
680 list);
681
682 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
683 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
684 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
685 /* make sure we re-start the timer */
686 fc_queue_timer_cfg(fc);
687 return;
688 }
689
690 /* remove from the queue */
691 llist_del(&fcqe->list);
692
693 fc->queue_depth--;
694
695 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200696 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200697 fc->time_last_pdu = time_now;
698
699 /* call the output callback for this FC instance */
700 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
701
702 /* we expect that out_cb will in the end free the msgb once
703 * it is no longer needed */
704
705 /* but we have to free the queue element ourselves */
706 talloc_free(fcqe);
707
708 /* re-configure the timer for the next PDU */
709 fc_queue_timer_cfg(fc);
710}
711
712/* configure/schedule the flow control timer to expire once the bucket
713 * will have leaked a sufficient number of bytes to transmit the next
714 * PDU in the queue */
715static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
716{
717 struct bssgp_fc_queue_element *fcqe;
718 uint32_t msecs;
719
720 if (llist_empty(&fc->queue))
721 return 0;
722
Jacob Erlbeck97319352015-04-30 19:28:03 +0200723 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200724 list);
725
Harald Welte27b2bb72013-06-22 09:44:00 +0200726 if (fc->bucket_leak_rate != 0) {
727 /* Calculate the point in time at which we will have leaked
728 * a sufficient number of bytes from the bucket to transmit
729 * the first PDU in the queue */
730 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
731 /* FIXME: add that time to fc->time_last_pdu and subtract it from
732 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200733 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200734 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
735 } else {
736 /* If the PCU is telling us to not send any more data at all,
737 * there's no point starting a timer. */
738 }
Harald Welted11c0592012-09-06 21:57:11 +0200739
740 return 0;
741}
742
743/* Enqueue a PDU in the flow control queue for delayed transmission */
744static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
745 uint32_t llc_pdu_len, void *priv)
746{
747 struct bssgp_fc_queue_element *fcqe;
748
749 if (fc->queue_depth >= fc->max_queue_depth)
750 return -ENOSPC;
751
752 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
753 if (!fcqe)
754 return -ENOMEM;
755 fcqe->msg = msg;
756 fcqe->llc_pdu_len = llc_pdu_len;
757 fcqe->priv = priv;
758
759 llist_add_tail(&fcqe->list, &fc->queue);
760
761 fc->queue_depth++;
762
763 /* re-configure the timer for dequeueing the pdu */
764 fc_queue_timer_cfg(fc);
765
766 return 0;
767}
768
769/* According to Section 8.2 */
770static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
771{
772 struct timeval time_now, time_diff;
773 int64_t bucket_predicted;
774 uint32_t csecs_elapsed, leaked;
775
776 /* B' = B + L(p) - (Tc - Tp)*R */
777
778 /* compute number of centi-seconds that have elapsed since transmitting
779 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200780 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200781 timersub(&time_now, &fc->time_last_pdu, &time_diff);
782 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
783
784 /* compute number of bytes that have leaked in the elapsed number
785 * of centi-seconds */
786 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
787 /* add the current PDU length to the last bucket level */
788 bucket_predicted = fc->bucket_counter + pdu_len;
789 /* ... and subtract the number of leaked bytes */
790 bucket_predicted -= leaked;
791
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700792 if (bucket_predicted < pdu_len)
793 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200794
795 if (bucket_predicted <= fc->bucket_size_max) {
796 /* the bucket is not full yet, we can pass the packet */
797 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700798 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200799 }
800
801 /* bucket is full, PDU needs to be delayed */
802 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200803}
804
805/* output callback for BVC flow control */
806static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
807 uint32_t llc_pdu_len, void *priv)
808{
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200809 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welted11c0592012-09-06 21:57:11 +0200810}
811
812/* input function of the flow control implementation, called first
813 * for the MM flow control, and then as the MM flow control output
814 * callback in order to perform BVC flow control */
815int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
816 uint32_t llc_pdu_len, void *priv)
817{
818 struct timeval time_now;
819
Harald Weltebb826222012-09-07 10:22:01 +0200820 if (llc_pdu_len > fc->bucket_size_max) {
821 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
822 "than maximum bucket size (%u)!\n", llc_pdu_len,
823 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200824 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200825 return -EIO;
826 }
827
Harald Welted11c0592012-09-06 21:57:11 +0200828 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100829 int rc;
830 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
831 if (rc)
832 msgb_free(msg);
833 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200834 } else {
835 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200836 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200837 fc->time_last_pdu = time_now;
838 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
839 }
840}
841
Harald Weltebb826222012-09-07 10:22:01 +0200842
843/* Initialize the Flow Control structure */
844void bssgp_fc_init(struct bssgp_flow_control *fc,
845 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
846 uint32_t max_queue_depth,
847 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
848 uint32_t llc_pdu_len, void *priv))
849{
850 fc->out_cb = out_cb;
851 fc->bucket_size_max = bucket_size_max;
852 fc->bucket_leak_rate = bucket_leak_rate;
853 fc->max_queue_depth = max_queue_depth;
854 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200855 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200856}
857
Harald Welted11c0592012-09-06 21:57:11 +0200858/* Initialize the Flow Control parameters for a new MS according to
859 * default values for the BVC specified by BVCI and NSEI */
860int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200861 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200862{
863 struct bssgp_bvc_ctx *ctx;
864
865 ctx = btsctx_by_bvci_nsei(bvci, nsei);
866 if (!ctx)
867 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200868
869 /* output call-back of per-MS FC is per-CTX FC */
870 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
871 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200872
873 return 0;
874}
875
Harald Welte25de8112010-05-13 21:26:28 +0200876static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200877 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800878{
Harald Welte27b2bb72013-06-22 09:44:00 +0200879 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
880 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800881
Harald Weltee9686b62010-05-31 18:07:17 +0200882 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
883 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800884
Harald Welte2d9ce712020-12-03 15:53:59 +0100885 if (!TLVP_PRES_LEN(tp, BSSGP_IE_TAG, 1) ||
886 !TLVP_PRES_LEN(tp, BSSGP_IE_BVC_BUCKET_SIZE, 2) ||
887 !TLVP_PRES_LEN(tp, BSSGP_IE_BUCKET_LEAK_RATE, 2) ||
888 !TLVP_PRES_LEN(tp, BSSGP_IE_BMAX_DEFAULT_MS, 2) ||
889 !TLVP_PRES_LEN(tp, BSSGP_IE_R_DEFAULT_MS,2)) {
Harald Weltee9686b62010-05-31 18:07:17 +0200890 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
891 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800892 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200893 }
Harald Welte9ba50052010-03-14 15:45:01 +0800894
Harald Weltebb826222012-09-07 10:22:01 +0200895 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200896 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200897 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200898 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200899 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200900 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200901 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200902 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200903
Harald Welte27b2bb72013-06-22 09:44:00 +0200904 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
905 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
906 "rate of 0, stopping all DL GPRS!\n");
907 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
908 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
909 "rate of != 0, restarting all DL GPRS!\n");
910
911 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
912 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
913 "bucket leak rate of 0, stopping DL GPRS!\n");
914 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
915 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
916 "bucket leak rate != 0, restarting DL GPRS!\n");
917
918 /* reconfigure the timer for flow control based on new values */
919 fc_queue_timer_cfg(bctx->fc);
920
Harald Welte9ba50052010-03-14 15:45:01 +0800921 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200922 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200923 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800924}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200925
Harald Welte25de8112010-05-13 21:26:28 +0200926/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800927static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
928 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800929{
Harald Welteec19c102010-05-02 09:50:42 +0200930 struct bssgp_normal_hdr *bgph =
931 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200932 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800933 int rc = 0;
934
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100935 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
936
Harald Welte58e65c92010-05-13 21:45:23 +0200937 /* If traffic is received on a BVC that is marked as blocked, the
938 * received PDU shall not be accepted and a STATUS PDU (Cause value:
939 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100940 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200941 uint16_t bvci = msgb_bvci(msg);
942 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
943 }
944
Harald Welte9ba50052010-03-14 15:45:01 +0800945 switch (pdu_type) {
946 case BSSGP_PDUT_UL_UNITDATA:
947 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200948 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800949 break;
950 case BSSGP_PDUT_RA_CAPABILITY:
951 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200952 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
953 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200954 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800955 /* FIXME: send RA_CAPA_UPDATE_ACK */
956 break;
957 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200958 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800959 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200960 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800961 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800962 case BSSGP_PDUT_FLOW_CONTROL_BVC:
963 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200964 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800965 break;
966 case BSSGP_PDUT_FLOW_CONTROL_MS:
967 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200968 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
969 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200970 /* FIXME: actually implement flow control */
971 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800972 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800973 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100974 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100975 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800976 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
977 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
978 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
979 case BSSGP_PDUT_MODIFY_BSS_PFC:
980 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100981 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
982 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200983 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800984 break;
985 /* those only exist in the SGSN -> BSS direction */
986 case BSSGP_PDUT_DL_UNITDATA:
987 case BSSGP_PDUT_PAGING_PS:
988 case BSSGP_PDUT_PAGING_CS:
989 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200990 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
991 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100992 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
993 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200994 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
995 rc = -EINVAL;
996 break;
997 default:
Max2c34ab42016-03-17 15:42:26 +0100998 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
999 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001000 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
1001 break;
1002 }
1003
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +08001004 return rc;
Harald Welte25de8112010-05-13 21:26:28 +02001005}
1006
1007/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001008static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
1009 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +02001010{
1011 struct bssgp_normal_hdr *bgph =
1012 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1013 uint8_t pdu_type = bgph->pdu_type;
1014 int rc = 0;
1015 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001016 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001017
1018 switch (bgph->pdu_type) {
1019 case BSSGP_PDUT_SUSPEND:
1020 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001021 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001022 break;
1023 case BSSGP_PDUT_RESUME:
1024 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001025 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001026 break;
1027 case BSSGP_PDUT_FLUSH_LL_ACK:
1028 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001029 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001030 /* FIXME: send NM_FLUSH_LL.res to NM */
1031 break;
1032 case BSSGP_PDUT_LLC_DISCARD:
1033 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001034 if (!bctx) {
1035 LOGP(DBSSGP, LOGL_ERROR,
1036 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
1037 goto err_mand_ie;
1038 }
Harald Weltee9686b62010-05-31 18:07:17 +02001039 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001040 break;
1041 case BSSGP_PDUT_BVC_BLOCK:
1042 /* BSS tells us that BVC shall be blocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001043 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1044 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltee9686b62010-05-31 18:07:17 +02001045 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
1046 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001047 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001048 }
Harald Welte2677ea52010-05-31 17:16:36 +02001049 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +02001050 break;
1051 case BSSGP_PDUT_BVC_UNBLOCK:
1052 /* BSS tells us that BVC shall be unblocked */
Harald Welte2d9ce712020-12-03 15:53:59 +01001053 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2)) {
Harald Weltee9686b62010-05-31 18:07:17 +02001054 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
1055 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001056 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001057 }
Harald Welte25de8112010-05-13 21:26:28 +02001058 rc = bssgp_rx_bvc_unblock(msg, tp);
1059 break;
Max590c4022017-06-28 14:29:24 +02001060 case BSSGP_PDUT_BVC_RESET_ACK:
1061 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
1062 break;
Harald Welte25de8112010-05-13 21:26:28 +02001063 case BSSGP_PDUT_BVC_RESET:
1064 /* BSS tells us that BVC init is required */
Harald Welte2d9ce712020-12-03 15:53:59 +01001065 if (!TLVP_PRES_LEN(tp, BSSGP_IE_BVCI, 2) ||
1066 !TLVP_PRES_LEN(tp, BSSGP_IE_CAUSE, 1)) {
Harald Weltee9686b62010-05-31 18:07:17 +02001067 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1068 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001069 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001070 }
Harald Welte25de8112010-05-13 21:26:28 +02001071 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1072 break;
1073 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001074 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001075 break;
1076 /* those only exist in the SGSN -> BSS direction */
1077 case BSSGP_PDUT_PAGING_PS:
1078 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001079 case BSSGP_PDUT_SUSPEND_ACK:
1080 case BSSGP_PDUT_SUSPEND_NACK:
1081 case BSSGP_PDUT_RESUME_ACK:
1082 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001083 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001084 case BSSGP_PDUT_BVC_BLOCK_ACK:
1085 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1086 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001087 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1088 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001089 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001090 rc = -EINVAL;
1091 break;
1092 default:
Max2c34ab42016-03-17 15:42:26 +01001093 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1094 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001095 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001096 break;
1097 }
1098
1099 return rc;
1100err_mand_ie:
1101 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1102}
1103
Harald Welte25de8112010-05-13 21:26:28 +02001104/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001105int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001106{
1107 struct bssgp_normal_hdr *bgph =
1108 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1109 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1110 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001111 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001112 uint8_t pdu_type = bgph->pdu_type;
1113 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001114 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001115 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001116 int data_len;
1117 int rc = 0;
1118
1119 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1120
1121 /* UNITDATA BSSGP headers have TLLI in front */
1122 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1123 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1124 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1125 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1126 } else {
1127 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1128 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1129 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001130 if (rc < 0) {
1131 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
1132 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001133 if (pdu_type != BSSGP_PDUT_STATUS)
1134 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001135 return rc;
1136 }
Harald Welte25de8112010-05-13 21:26:28 +02001137
Harald Welte2d9ce712020-12-03 15:53:59 +01001138 if (bvci == BVCI_SIGNALLING && TLVP_PRES_LEN(&tp, BSSGP_IE_BVCI, 2))
Harald Weltebfe62e52017-05-15 12:48:30 +02001139 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001140
Harald Welte25de8112010-05-13 21:26:28 +02001141 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001142 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001143
Harald Welte16c8dbb2010-05-17 23:30:01 +02001144 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001145 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001146 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1147 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1148 msgb_bssgp_len(msg));
1149 }
1150
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001151 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1152 * are otherwise unexpected */
1153 if (pdu_type == BSSGP_PDUT_STATUS)
1154 /* Some exception has occurred */
1155 return bssgp_rx_status(msg, &tp, bvci, bctx);
1156
1157 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1158 * registered if a BVCI is given. */
1159 if (!bctx && bvci != BVCI_SIGNALLING &&
1160 pdu_type != BSSGP_PDUT_BVC_RESET) {
Max548caef2019-03-07 13:49:34 +01001161 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001162 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001163 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1164 }
1165
Harald Welte61c07842010-05-18 11:57:08 +02001166 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001167 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001168 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001169 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001170 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001171 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001172 else
1173 LOGP(DBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001174 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1175 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001176
1177 return rc;
1178}
1179
Harald Weltede4599c2012-06-17 13:04:02 +08001180int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1181 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001182{
Harald Welte8a521132010-05-17 22:59:29 +02001183 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001184 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001185 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001186 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001187 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001188 uint16_t bvci = msgb_bvci(msg);
1189 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001190 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001191 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001192
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001193 OSMO_ASSERT(dup != NULL);
1194
Harald Welte30bc19a2010-05-02 11:19:37 +02001195 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001196 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001197 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001198 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001199 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001200 return -EINVAL;
1201 }
1202
1203 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001204 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001205 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1206 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001207 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001208 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001209 }
Harald Welte9ba50052010-03-14 15:45:01 +08001210
1211 if (msg->len > TVLV_MAX_ONEBYTE)
1212 llc_pdu_tlv_hdr_len += 1;
1213
1214 /* prepend the tag and length of the LLC-PDU TLV */
1215 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1216 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1217 if (llc_pdu_tlv_hdr_len > 2) {
1218 llc_pdu_tlv[1] = msg_len >> 8;
1219 llc_pdu_tlv[2] = msg_len & 0xff;
1220 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001221 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001222 llc_pdu_tlv[1] |= 0x80;
1223 }
1224
Harald Welte2f946832010-05-31 22:12:30 +02001225 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1226
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001227 /* Old TLLI to help BSS map from old->new */
1228 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001229 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001230 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001231 }
Harald Welte9ba50052010-03-14 15:45:01 +08001232
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001233 /* IMSI */
1234 if (dup->imsi && strlen(dup->imsi)) {
Harald Weltea13fb752020-06-16 08:44:42 +02001235 uint8_t mi[GSM48_MID_MAX_SIZE];
1236/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1237 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1238 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1239 * mi[131], which is wrong */
1240#pragma GCC diagnostic push
1241#pragma GCC diagnostic ignored "-Warray-bounds"
1242 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1243 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1244 if (imsi_len > 2)
1245 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1246 imsi_len-2, mi+2);
1247#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001248 }
1249
1250 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001251 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001252 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1253 (uint8_t *) &drx_params);
1254
1255 /* FIXME: Priority */
1256
1257 /* MS Radio Access Capability */
1258 if (dup->ms_ra_cap.len)
1259 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1260 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1261
Harald Welte9ba50052010-03-14 15:45:01 +08001262 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001263 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001264
1265 /* prepend the QoS profile, TLLI and pdu type */
1266 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001267 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001268 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001269 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1270
Harald Welte16c8dbb2010-05-17 23:30:01 +02001271 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1272 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1273
Harald Welte30bc19a2010-05-02 11:19:37 +02001274 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001275
Harald Welted11c0592012-09-06 21:57:11 +02001276 /* check if we have to go through per-ms flow control or can go
1277 * directly to the per-BSS flow control */
1278 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001279 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001280 else
Harald Welted8b47692012-09-07 11:29:32 +02001281 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001282}
Harald Welte68b4f032010-06-09 16:22:28 +02001283
1284/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001285int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1286 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001287{
1288 struct msgb *msg = bssgp_msgb_alloc();
1289 struct bssgp_normal_hdr *bgph =
1290 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001291 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Weltea13fb752020-06-16 08:44:42 +02001292 uint8_t mi[GSM48_MID_MAX_SIZE];
1293 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001294 struct gsm48_ra_id ra;
Harald Weltea13fb752020-06-16 08:44:42 +02001295
1296 if (imsi_len < 2)
1297 return -EINVAL;
Harald Welte68b4f032010-06-09 16:22:28 +02001298
1299 msgb_nsei(msg) = nsei;
1300 msgb_bvci(msg) = ns_bvci;
1301
1302 if (pinfo->mode == BSSGP_PAGING_PS)
1303 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1304 else
1305 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1306 /* IMSI */
Harald Weltea13fb752020-06-16 08:44:42 +02001307/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1308 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1309 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1310 * mi[131], which is wrong */
1311#pragma GCC diagnostic push
1312#pragma GCC diagnostic ignored "-Warray-bounds"
1313 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
1314 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1315#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001316 /* DRX Parameters */
1317 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1318 (uint8_t *) &drx_params);
1319 /* Scope */
1320 switch (pinfo->scope) {
1321 case BSSGP_PAGING_BSS_AREA:
1322 {
1323 uint8_t null = 0;
1324 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1325 }
1326 break;
1327 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001328 gsm48_encode_ra(&ra, &pinfo->raid);
1329 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001330 break;
1331 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001332 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001333 break;
1334 case BSSGP_PAGING_BVCI:
1335 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001336 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001337 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1338 }
1339 break;
1340 }
1341 /* QoS profile mandatory for PS */
1342 if (pinfo->mode == BSSGP_PAGING_PS)
1343 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1344
1345 /* Optional (P-)TMSI */
1346 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001347 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001348 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1349 }
1350
Alexander Couzens85a8fd32020-07-18 15:57:07 +02001351 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welte68b4f032010-06-09 16:22:28 +02001352}
Harald Weltecca49632012-06-16 17:45:59 +08001353
Harald Weltede4599c2012-06-17 13:04:02 +08001354void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001355{
1356 DBSSGP = ss;
1357}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001358
1359/*!
1360 * \brief Flush the queue of the bssgp_flow_control
1361 * \param[in] The flow control object which holds the queue.
1362 */
1363void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1364{
1365 struct bssgp_fc_queue_element *element, *tmp;
1366
1367 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1368 msgb_free(element->msg);
1369 llist_del(&element->list);
1370 talloc_free(element);
1371 }
1372}
1373
1374/*!
1375 * \brief Flush the queues of all BSSGP contexts.
1376 */
1377void bssgp_flush_all_queues()
1378{
1379 struct bssgp_bvc_ctx *bctx;
1380
1381 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1382 if (bctx->fc)
1383 bssgp_fc_flush_queue(bctx->fc);
1384 }
1385}