blob: b695c284cce2a5c1b9732f784e97aeec9c29fa80 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_bssgp.c
2 * GPRS BSSGP protocol implementation as per 3GPP TS 08.18. */
3/*
4 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte9ba50052010-03-14 15:45:01 +080010 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080013 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080019 *
Harald Welte7fa89c22014-10-26 20:33:09 +010020 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080022 *
Harald Welte4e5721d2010-05-17 23:41:43 +020023 * TODO:
24 * o properly count incoming BVC-RESET packets in counter group
25 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080026 */
27
28#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020029#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080030
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010031#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020032#include <osmocom/core/byteswap.h>
33#include <osmocom/core/bit16gen.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010034#include <osmocom/gsm/tlv.h>
35#include <osmocom/core/talloc.h>
36#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010037#include <osmocom/core/stats.h>
Harald Welte6752fa42010-05-02 09:23:16 +020038
Harald Welte73952e32012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_bssgp.h>
Max8b8938f2017-06-29 19:48:29 +020040#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte73952e32012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_ns.h>
42
Harald Weltecca49632012-06-16 17:45:59 +080043#include "common_vty.h"
44
Harald Welte6752fa42010-05-02 09:23:16 +020045void *bssgp_tall_ctx = NULL;
46
Harald Welte25de8112010-05-13 21:26:28 +020047static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080048 { "packets:in", "Packets at BSSGP Level ( In)" },
49 { "packets:out","Packets at BSSGP Level (Out)" },
50 { "bytes:in", "Bytes at BSSGP Level ( In)" },
51 { "bytes:out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020052 { "blocked", "BVC Blocking count" },
53 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010054 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020055};
56
57static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080058 .group_name_prefix = "bssgp:bss_ctx",
Harald Welte25de8112010-05-13 21:26:28 +020059 .group_description = "BSSGP Peer Statistics",
60 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
61 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010062 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020063};
64
Harald Weltea78b9c22010-05-17 23:02:42 +020065LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020066
Harald Welted11c0592012-09-06 21:57:11 +020067static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
68 uint32_t llc_pdu_len, void *priv);
69
Harald Welte6752fa42010-05-02 09:23:16 +020070/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020071struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020072{
Harald Welte8a521132010-05-17 22:59:29 +020073 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020074
Harald Weltea78b9c22010-05-17 23:02:42 +020075 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020076 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
77 bctx->cell_id == cid)
78 return bctx;
79 }
80 return NULL;
81}
82
Max8b8938f2017-06-29 19:48:29 +020083/*! Initiate reset procedure for all PTP BVC on a given NSEI.
84 *
85 * This function initiates reset procedure for all PTP BVC with a given cause.
86 * \param[in] nsei NSEI to which PTP BVC should belong to
87 * \param[in] cause Cause of BVC RESET
88 * \returns 0 on success, negative error code otherwise
89 */
90int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
91{
92 int rc;
93 struct bssgp_bvc_ctx *bctx;
94
95 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
96 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
97 LOGP(DBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
98 nsei, bctx->bvci, bssgp_cause_str(cause));
99 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
100 if (rc < 0)
101 return rc;
102 }
103 }
104
105 return 0;
106}
107
Harald Welte6752fa42010-05-02 09:23:16 +0200108/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200109struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200110{
Harald Welte8a521132010-05-17 22:59:29 +0200111 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200112
Harald Weltea78b9c22010-05-17 23:02:42 +0200113 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200114 if (bctx->nsei == nsei && bctx->bvci == bvci)
115 return bctx;
116 }
117 return NULL;
118}
119
Harald Welte8a521132010-05-17 22:59:29 +0200120struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200121{
Harald Welte8a521132010-05-17 22:59:29 +0200122 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200123
Harald Welte8a521132010-05-17 22:59:29 +0200124 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200125 if (!ctx)
126 return NULL;
127 ctx->bvci = bvci;
128 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200129 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
130 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Harald Welte6d3135c2019-05-08 14:00:37 +0200131 if (!ctx->ctrg) {
132 talloc_free(ctx);
133 return NULL;
134 }
Harald Welted8b47692012-09-07 11:29:32 +0200135 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
136 /* cofigure for 2Mbit, 30 packets in queue */
137 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200138
Harald Weltea78b9c22010-05-17 23:02:42 +0200139 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200140
141 return ctx;
142}
143
Harald Welte9ba50052010-03-14 15:45:01 +0800144/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200145static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800146{
147 struct msgb *msg = bssgp_msgb_alloc();
148 struct bssgp_normal_hdr *bgph =
149 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
150
Harald Welte24a655f2010-04-30 19:54:29 +0200151 msgb_nsei(msg) = nsei;
152 msgb_bvci(msg) = ns_bvci;
153
Harald Welte9ba50052010-03-14 15:45:01 +0800154 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
155 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
156
Harald Welte24a655f2010-04-30 19:54:29 +0200157 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800158}
159
Harald Weltea8aa4df2010-05-30 22:00:53 +0200160/* 10.3.7 SUSPEND-ACK PDU */
161int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
162 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
163{
164 struct msgb *msg = bssgp_msgb_alloc();
165 struct bssgp_normal_hdr *bgph =
166 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200167
168 msgb_nsei(msg) = nsei;
169 msgb_bvci(msg) = 0; /* Signalling */
170 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
171
Maxe29ec852018-01-05 14:30:22 +0100172 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100173 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200174 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
175
176 return gprs_ns_sendmsg(bssgp_nsi, msg);
177}
178
179/* 10.3.8 SUSPEND-NACK PDU */
180int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100181 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200182 uint8_t *cause)
183{
184 struct msgb *msg = bssgp_msgb_alloc();
185 struct bssgp_normal_hdr *bgph =
186 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200187
188 msgb_nsei(msg) = nsei;
189 msgb_bvci(msg) = 0; /* Signalling */
190 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
191
Maxe29ec852018-01-05 14:30:22 +0100192 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100193 bssgp_msgb_ra_put(msg, ra_id);
194
Harald Weltea8aa4df2010-05-30 22:00:53 +0200195 if (cause)
196 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
197
198 return gprs_ns_sendmsg(bssgp_nsi, msg);
199}
200
201/* 10.3.10 RESUME-ACK PDU */
202int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
203 const struct gprs_ra_id *ra_id)
204{
205 struct msgb *msg = bssgp_msgb_alloc();
206 struct bssgp_normal_hdr *bgph =
207 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200208
209 msgb_nsei(msg) = nsei;
210 msgb_bvci(msg) = 0; /* Signalling */
211 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
212
Maxe29ec852018-01-05 14:30:22 +0100213 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100214 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200215
216 return gprs_ns_sendmsg(bssgp_nsi, msg);
217}
218
219/* 10.3.11 RESUME-NACK PDU */
220int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
221 const struct gprs_ra_id *ra_id, uint8_t *cause)
222{
223 struct msgb *msg = bssgp_msgb_alloc();
224 struct bssgp_normal_hdr *bgph =
225 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200226
227 msgb_nsei(msg) = nsei;
228 msgb_bvci(msg) = 0; /* Signalling */
229 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
230
Maxe29ec852018-01-05 14:30:22 +0100231 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100232 bssgp_msgb_ra_put(msg, ra_id);
233
Harald Weltea8aa4df2010-05-30 22:00:53 +0200234 if (cause)
235 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
236
237 return gprs_ns_sendmsg(bssgp_nsi, msg);
238}
239
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200240uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200241{
242 /* 6 octets RAC */
243 gsm48_parse_ra(raid, buf);
244 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200245 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200246}
247
Harald Welte28610072011-11-24 21:32:07 +0100248int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
249 uint16_t cid)
250{
Harald Welte28610072011-11-24 21:32:07 +0100251 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100252 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100253 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200254 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100255
256 return 8;
257}
258
Harald Welte3fddf3c2010-05-01 16:48:27 +0200259/* Chapter 8.4 BVC-Reset Procedure */
260static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
261 uint16_t ns_bvci)
262{
Harald Welte15a36432012-06-17 12:16:31 +0800263 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200264 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200265 uint16_t nsei = msgb_nsei(msg);
266 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200267
Harald Weltebfe62e52017-05-15 12:48:30 +0200268 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200269 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200270 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
271
Harald Welte6752fa42010-05-02 09:23:16 +0200272 /* look-up or create the BTS context for this BVC */
273 bctx = btsctx_by_bvci_nsei(bvci, nsei);
274 if (!bctx)
275 bctx = btsctx_alloc(bvci, nsei);
276
Harald Welte25de8112010-05-13 21:26:28 +0200277 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
278 bctx->state &= ~BVC_S_BLOCKED;
279
Harald Welte3fddf3c2010-05-01 16:48:27 +0200280 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
281 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200282 if (bvci != 0 && bvci != 1) {
283 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200284 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200285 "missing mandatory IE\n", bvci);
286 return -EINVAL;
287 }
288 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200289 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
290 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100291 LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
292 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200293 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200294
Harald Welte15a36432012-06-17 12:16:31 +0800295 /* Send NM_BVC_RESET.ind to NM */
296 memset(&nmp, 0, sizeof(nmp));
297 nmp.nsei = nsei;
298 nmp.bvci = bvci;
299 nmp.tp = tp;
300 nmp.ra_id = &bctx->ra_id;
301 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
302 PRIM_OP_INDICATION, msg);
303 bssgp_prim_cb(&nmp.oph, NULL);
304
Harald Welte6752fa42010-05-02 09:23:16 +0200305 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200306 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
307 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200308 return 0;
309}
310
Harald Welte25de8112010-05-13 21:26:28 +0200311static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
312{
Harald Welte15a36432012-06-17 12:16:31 +0800313 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100314 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200315 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200316
Harald Weltebfe62e52017-05-15 12:48:30 +0200317 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200318 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200319 /* 8.3.2: Signalling BVC shall never be blocked */
320 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
321 "received block for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100322 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200323 return 0;
324 }
Harald Welte25de8112010-05-13 21:26:28 +0200325
Harald Welte086fe322011-08-19 16:45:19 +0200326 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200327
Max548caef2019-03-07 13:49:34 +0100328 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200329 if (!ptp_ctx)
330 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
331
332 ptp_ctx->state |= BVC_S_BLOCKED;
333 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
334
Harald Welte15a36432012-06-17 12:16:31 +0800335 /* Send NM_BVC_BLOCK.ind to NM */
336 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100337 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800338 nmp.bvci = bvci;
339 nmp.tp = tp;
340 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
341 PRIM_OP_INDICATION, msg);
342 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200343
344 /* We always acknowledge the BLOCKing */
Max548caef2019-03-07 13:49:34 +0100345 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200346 bvci, msgb_bvci(msg));
347};
348
349static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
350{
Harald Welte15a36432012-06-17 12:16:31 +0800351 struct osmo_bssgp_prim nmp;
Max548caef2019-03-07 13:49:34 +0100352 uint16_t bvci, nsei = msgb_nsei(msg);
Harald Welte8a521132010-05-17 22:59:29 +0200353 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200354
Harald Weltebfe62e52017-05-15 12:48:30 +0200355 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200356 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200357 /* 8.3.2: Signalling BVC shall never be blocked */
358 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
359 "received unblock for signalling BVC!?!\n",
Max548caef2019-03-07 13:49:34 +0100360 nsei, msgb_bvci(msg));
Harald Welte58e65c92010-05-13 21:45:23 +0200361 return 0;
362 }
Harald Welte25de8112010-05-13 21:26:28 +0200363
Harald Weltee9686b62010-05-31 18:07:17 +0200364 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200365
Max548caef2019-03-07 13:49:34 +0100366 ptp_ctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200367 if (!ptp_ctx)
368 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
369
370 ptp_ctx->state &= ~BVC_S_BLOCKED;
371
Harald Welte15a36432012-06-17 12:16:31 +0800372 /* Send NM_BVC_UNBLOCK.ind to NM */
373 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100374 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800375 nmp.bvci = bvci;
376 nmp.tp = tp;
377 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
378 PRIM_OP_INDICATION, msg);
379 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200380
381 /* We always acknowledge the unBLOCKing */
Max548caef2019-03-07 13:49:34 +0100382 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, nsei,
Harald Welte25de8112010-05-13 21:26:28 +0200383 bvci, msgb_bvci(msg));
384};
385
Harald Welte9ba50052010-03-14 15:45:01 +0800386/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200387static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200388 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800389{
Harald Welte15a36432012-06-17 12:16:31 +0800390 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200391 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800392
Harald Welte6752fa42010-05-02 09:23:16 +0200393 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200394 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800395
Harald Welte086fe322011-08-19 16:45:19 +0200396 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200397
Harald Welte9ba50052010-03-14 15:45:01 +0800398 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200399 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200400 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
401 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
402 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200403 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200404 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200405
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200406 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800407 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
408 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800409
Harald Welte15a36432012-06-17 12:16:31 +0800410 /* Send BSSGP_UL_UD.ind to NM */
411 memset(&gbp, 0, sizeof(gbp));
412 gbp.nsei = ctx->nsei;
413 gbp.bvci = ctx->bvci;
414 gbp.tlli = msgb_tlli(msg);
415 gbp.tp = tp;
416 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
417 PRIM_OP_INDICATION, msg);
418 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800419}
420
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200421static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800422{
Harald Welte15a36432012-06-17 12:16:31 +0800423 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200424 struct gprs_ra_id raid;
425 uint32_t tlli;
Max548caef2019-03-07 13:49:34 +0100426 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200427 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800428
Harald Welte25de8112010-05-13 21:26:28 +0200429 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200430 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
431 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200432 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200433 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200434 }
Harald Welte9ba50052010-03-14 15:45:01 +0800435
Harald Weltebfe62e52017-05-15 12:48:30 +0200436 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200437
Harald Welte17925322010-05-31 20:18:35 +0200438 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200439 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200440
Harald Weltea8aa4df2010-05-30 22:00:53 +0200441 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
442
Harald Welte313cccf2010-06-09 11:22:47 +0200443 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800444 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100445 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200446 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800447 gbp.tlli = tlli;
448 gbp.ra_id = &raid;
449 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
450 PRIM_OP_REQUEST, msg);
451
452 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200453 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100454 return bssgp_tx_suspend_nack(nsei, tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200455
Max548caef2019-03-07 13:49:34 +0100456 bssgp_tx_suspend_ack(nsei, tlli, &raid, 0);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200457
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800458 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800459}
460
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200461static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800462{
Harald Welte15a36432012-06-17 12:16:31 +0800463 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200464 struct gprs_ra_id raid;
465 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200466 uint8_t suspend_ref;
Max548caef2019-03-07 13:49:34 +0100467 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200468 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800469
Harald Welte25de8112010-05-13 21:26:28 +0200470 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
471 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200472 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
473 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200474 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200475 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200476 }
Harald Welte9ba50052010-03-14 15:45:01 +0800477
Harald Weltebfe62e52017-05-15 12:48:30 +0200478 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200479 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200480
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200481 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200482
Harald Weltea8aa4df2010-05-30 22:00:53 +0200483 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
484
Harald Welte313cccf2010-06-09 11:22:47 +0200485 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800486 memset(&gbp, 0, sizeof(gbp));
Max548caef2019-03-07 13:49:34 +0100487 gbp.nsei = nsei;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200488 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800489 gbp.tlli = tlli;
490 gbp.ra_id = &raid;
491 gbp.u.resume.suspend_ref = suspend_ref;
492 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
493 PRIM_OP_REQUEST, msg);
494
495 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200496 if (rc < 0)
Max548caef2019-03-07 13:49:34 +0100497 return bssgp_tx_resume_nack(nsei, tlli, &raid,
Harald Welte313cccf2010-06-09 11:22:47 +0200498 NULL);
499
Max548caef2019-03-07 13:49:34 +0100500 bssgp_tx_resume_ack(nsei, tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800501 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800502}
503
Harald Weltee9686b62010-05-31 18:07:17 +0200504
505static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
506 struct bssgp_bvc_ctx *ctx)
507{
Harald Welte15a36432012-06-17 12:16:31 +0800508 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200509 uint32_t tlli = 0;
Max548caef2019-03-07 13:49:34 +0100510 uint16_t nsei = msgb_nsei(msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200511
512 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
513 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
514 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
515 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
516 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
517 "missing mandatory IE\n", ctx->bvci);
518 }
519
Harald Welteb7363142010-07-23 21:59:29 +0200520 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200521 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200522
Harald Welte086fe322011-08-19 16:45:19 +0200523 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200524 ctx->bvci, tlli);
525
526 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
527
Harald Welte15a36432012-06-17 12:16:31 +0800528 /* send NM_LLC_DISCARDED to NM */
529 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100530 nmp.nsei = nsei;
Harald Welte15a36432012-06-17 12:16:31 +0800531 nmp.bvci = ctx->bvci;
532 nmp.tlli = tlli;
533 nmp.tp = tp;
534 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
535 PRIM_OP_INDICATION, msg);
536
537 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200538}
539
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100540int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
541 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
542{
Max548caef2019-03-07 13:49:34 +0100543 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100544 struct osmo_bssgp_prim nmp;
545 enum gprs_bssgp_cause cause;
546
547 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
548 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
549 "missing mandatory IE\n", bvci);
550 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
551 } else {
552 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
553 }
554
555 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
556 bvci, bssgp_cause_str(cause));
557
558 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
559 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
560 LOGP(DBSSGP, LOGL_ERROR,
561 "BSSGP BVCI=%u Rx STATUS cause=%s "
562 "missing conditional BVCI IE\n",
563 bvci, bssgp_cause_str(cause));
564 }
565
566 if (bctx)
567 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
568
569 /* send NM_STATUS to NM */
570 memset(&nmp, 0, sizeof(nmp));
Max548caef2019-03-07 13:49:34 +0100571 nmp.nsei = nsei;
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100572 nmp.bvci = bvci;
573 nmp.tp = tp;
574 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
575 PRIM_OP_INDICATION, msg);
576
577 return bssgp_prim_cb(&nmp.oph, NULL);
578}
579
580
Harald Welted11c0592012-09-06 21:57:11 +0200581/* One element (msgb) in a BSSGP Flow Control queue */
582struct bssgp_fc_queue_element {
583 /* linked list of queue elements */
584 struct llist_head list;
585 /* The message that we have enqueued */
586 struct msgb *msg;
587 /* Length of the LLC PDU part of the contained message */
588 uint32_t llc_pdu_len;
589 /* private pointer passed to the flow control out_cb function */
590 void *priv;
591};
592
593static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
594static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
595
596static void fc_timer_cb(void *data)
597{
598 struct bssgp_flow_control *fc = data;
599 struct bssgp_fc_queue_element *fcqe;
600 struct timeval time_now;
601
602 /* if the queue is empty, we return without sending something
603 * and without re-starting the timer */
604 if (llist_empty(&fc->queue))
605 return;
606
607 /* get the first entry from the queue */
608 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
609 list);
610
611 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
612 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
613 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
614 /* make sure we re-start the timer */
615 fc_queue_timer_cfg(fc);
616 return;
617 }
618
619 /* remove from the queue */
620 llist_del(&fcqe->list);
621
622 fc->queue_depth--;
623
624 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200625 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200626 fc->time_last_pdu = time_now;
627
628 /* call the output callback for this FC instance */
629 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
630
631 /* we expect that out_cb will in the end free the msgb once
632 * it is no longer needed */
633
634 /* but we have to free the queue element ourselves */
635 talloc_free(fcqe);
636
637 /* re-configure the timer for the next PDU */
638 fc_queue_timer_cfg(fc);
639}
640
641/* configure/schedule the flow control timer to expire once the bucket
642 * will have leaked a sufficient number of bytes to transmit the next
643 * PDU in the queue */
644static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
645{
646 struct bssgp_fc_queue_element *fcqe;
647 uint32_t msecs;
648
649 if (llist_empty(&fc->queue))
650 return 0;
651
Jacob Erlbeck97319352015-04-30 19:28:03 +0200652 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200653 list);
654
Harald Welte27b2bb72013-06-22 09:44:00 +0200655 if (fc->bucket_leak_rate != 0) {
656 /* Calculate the point in time at which we will have leaked
657 * a sufficient number of bytes from the bucket to transmit
658 * the first PDU in the queue */
659 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
660 /* FIXME: add that time to fc->time_last_pdu and subtract it from
661 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200662 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200663 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
664 } else {
665 /* If the PCU is telling us to not send any more data at all,
666 * there's no point starting a timer. */
667 }
Harald Welted11c0592012-09-06 21:57:11 +0200668
669 return 0;
670}
671
672/* Enqueue a PDU in the flow control queue for delayed transmission */
673static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
674 uint32_t llc_pdu_len, void *priv)
675{
676 struct bssgp_fc_queue_element *fcqe;
677
678 if (fc->queue_depth >= fc->max_queue_depth)
679 return -ENOSPC;
680
681 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
682 if (!fcqe)
683 return -ENOMEM;
684 fcqe->msg = msg;
685 fcqe->llc_pdu_len = llc_pdu_len;
686 fcqe->priv = priv;
687
688 llist_add_tail(&fcqe->list, &fc->queue);
689
690 fc->queue_depth++;
691
692 /* re-configure the timer for dequeueing the pdu */
693 fc_queue_timer_cfg(fc);
694
695 return 0;
696}
697
698/* According to Section 8.2 */
699static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
700{
701 struct timeval time_now, time_diff;
702 int64_t bucket_predicted;
703 uint32_t csecs_elapsed, leaked;
704
705 /* B' = B + L(p) - (Tc - Tp)*R */
706
707 /* compute number of centi-seconds that have elapsed since transmitting
708 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200709 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200710 timersub(&time_now, &fc->time_last_pdu, &time_diff);
711 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
712
713 /* compute number of bytes that have leaked in the elapsed number
714 * of centi-seconds */
715 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
716 /* add the current PDU length to the last bucket level */
717 bucket_predicted = fc->bucket_counter + pdu_len;
718 /* ... and subtract the number of leaked bytes */
719 bucket_predicted -= leaked;
720
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700721 if (bucket_predicted < pdu_len)
722 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200723
724 if (bucket_predicted <= fc->bucket_size_max) {
725 /* the bucket is not full yet, we can pass the packet */
726 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700727 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200728 }
729
730 /* bucket is full, PDU needs to be delayed */
731 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200732}
733
734/* output callback for BVC flow control */
735static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
736 uint32_t llc_pdu_len, void *priv)
737{
738 return gprs_ns_sendmsg(bssgp_nsi, msg);
739}
740
741/* input function of the flow control implementation, called first
742 * for the MM flow control, and then as the MM flow control output
743 * callback in order to perform BVC flow control */
744int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
745 uint32_t llc_pdu_len, void *priv)
746{
747 struct timeval time_now;
748
Harald Weltebb826222012-09-07 10:22:01 +0200749 if (llc_pdu_len > fc->bucket_size_max) {
750 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
751 "than maximum bucket size (%u)!\n", llc_pdu_len,
752 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200753 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200754 return -EIO;
755 }
756
Harald Welted11c0592012-09-06 21:57:11 +0200757 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100758 int rc;
759 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
760 if (rc)
761 msgb_free(msg);
762 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200763 } else {
764 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200765 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200766 fc->time_last_pdu = time_now;
767 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
768 }
769}
770
Harald Weltebb826222012-09-07 10:22:01 +0200771
772/* Initialize the Flow Control structure */
773void bssgp_fc_init(struct bssgp_flow_control *fc,
774 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
775 uint32_t max_queue_depth,
776 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
777 uint32_t llc_pdu_len, void *priv))
778{
779 fc->out_cb = out_cb;
780 fc->bucket_size_max = bucket_size_max;
781 fc->bucket_leak_rate = bucket_leak_rate;
782 fc->max_queue_depth = max_queue_depth;
783 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200784 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200785}
786
Harald Welted11c0592012-09-06 21:57:11 +0200787/* Initialize the Flow Control parameters for a new MS according to
788 * default values for the BVC specified by BVCI and NSEI */
789int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200790 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200791{
792 struct bssgp_bvc_ctx *ctx;
793
794 ctx = btsctx_by_bvci_nsei(bvci, nsei);
795 if (!ctx)
796 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200797
798 /* output call-back of per-MS FC is per-CTX FC */
799 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
800 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200801
802 return 0;
803}
804
Harald Welte25de8112010-05-13 21:26:28 +0200805static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200806 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800807{
Harald Welte27b2bb72013-06-22 09:44:00 +0200808 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
809 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800810
Harald Weltee9686b62010-05-31 18:07:17 +0200811 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
812 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800813
814 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
815 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
816 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
817 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200818 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
819 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
820 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800821 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200822 }
Harald Welte9ba50052010-03-14 15:45:01 +0800823
Harald Weltebb826222012-09-07 10:22:01 +0200824 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200825 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200826 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200827 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200828 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200829 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200830 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200831 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200832
Harald Welte27b2bb72013-06-22 09:44:00 +0200833 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
834 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
835 "rate of 0, stopping all DL GPRS!\n");
836 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
837 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
838 "rate of != 0, restarting all DL GPRS!\n");
839
840 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
841 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
842 "bucket leak rate of 0, stopping DL GPRS!\n");
843 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
844 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
845 "bucket leak rate != 0, restarting DL GPRS!\n");
846
847 /* reconfigure the timer for flow control based on new values */
848 fc_queue_timer_cfg(bctx->fc);
849
Harald Welte9ba50052010-03-14 15:45:01 +0800850 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200851 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200852 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800853}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200854
Harald Welte25de8112010-05-13 21:26:28 +0200855/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800856static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
857 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800858{
Harald Welteec19c102010-05-02 09:50:42 +0200859 struct bssgp_normal_hdr *bgph =
860 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200861 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800862 int rc = 0;
863
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100864 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
865
Harald Welte58e65c92010-05-13 21:45:23 +0200866 /* If traffic is received on a BVC that is marked as blocked, the
867 * received PDU shall not be accepted and a STATUS PDU (Cause value:
868 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100869 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200870 uint16_t bvci = msgb_bvci(msg);
871 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
872 }
873
Harald Welte9ba50052010-03-14 15:45:01 +0800874 switch (pdu_type) {
875 case BSSGP_PDUT_UL_UNITDATA:
876 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200877 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800878 break;
879 case BSSGP_PDUT_RA_CAPABILITY:
880 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200881 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
882 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200883 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800884 /* FIXME: send RA_CAPA_UPDATE_ACK */
885 break;
886 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200887 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800888 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200889 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800890 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800891 case BSSGP_PDUT_FLOW_CONTROL_BVC:
892 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200893 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800894 break;
895 case BSSGP_PDUT_FLOW_CONTROL_MS:
896 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200897 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
898 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200899 /* FIXME: actually implement flow control */
900 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800901 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800902 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100903 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100904 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800905 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
906 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
907 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
908 case BSSGP_PDUT_MODIFY_BSS_PFC:
909 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100910 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
911 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200912 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800913 break;
914 /* those only exist in the SGSN -> BSS direction */
915 case BSSGP_PDUT_DL_UNITDATA:
916 case BSSGP_PDUT_PAGING_PS:
917 case BSSGP_PDUT_PAGING_CS:
918 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200919 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
920 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100921 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
922 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200923 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
924 rc = -EINVAL;
925 break;
926 default:
Max2c34ab42016-03-17 15:42:26 +0100927 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
928 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200929 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
930 break;
931 }
932
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800933 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200934}
935
936/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800937static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
938 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200939{
940 struct bssgp_normal_hdr *bgph =
941 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
942 uint8_t pdu_type = bgph->pdu_type;
943 int rc = 0;
944 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200945 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200946
947 switch (bgph->pdu_type) {
948 case BSSGP_PDUT_SUSPEND:
949 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200950 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200951 break;
952 case BSSGP_PDUT_RESUME:
953 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200954 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200955 break;
956 case BSSGP_PDUT_FLUSH_LL_ACK:
957 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200958 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200959 /* FIXME: send NM_FLUSH_LL.res to NM */
960 break;
961 case BSSGP_PDUT_LLC_DISCARD:
962 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200963 if (!bctx) {
964 LOGP(DBSSGP, LOGL_ERROR,
965 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
966 goto err_mand_ie;
967 }
Harald Weltee9686b62010-05-31 18:07:17 +0200968 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200969 break;
970 case BSSGP_PDUT_BVC_BLOCK:
971 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200972 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200973 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
974 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
975 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200976 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200977 }
Harald Welte2677ea52010-05-31 17:16:36 +0200978 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200979 break;
980 case BSSGP_PDUT_BVC_UNBLOCK:
981 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200982 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
983 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
984 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200985 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200986 }
Harald Welte25de8112010-05-13 21:26:28 +0200987 rc = bssgp_rx_bvc_unblock(msg, tp);
988 break;
Max590c4022017-06-28 14:29:24 +0200989 case BSSGP_PDUT_BVC_RESET_ACK:
990 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
991 break;
Harald Welte25de8112010-05-13 21:26:28 +0200992 case BSSGP_PDUT_BVC_RESET:
993 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200994 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200995 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
996 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
997 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200998 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200999 }
Harald Welte25de8112010-05-13 21:26:28 +02001000 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1001 break;
1002 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001003 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001004 break;
1005 /* those only exist in the SGSN -> BSS direction */
1006 case BSSGP_PDUT_PAGING_PS:
1007 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001008 case BSSGP_PDUT_SUSPEND_ACK:
1009 case BSSGP_PDUT_SUSPEND_NACK:
1010 case BSSGP_PDUT_RESUME_ACK:
1011 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001012 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001013 case BSSGP_PDUT_BVC_BLOCK_ACK:
1014 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1015 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001016 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1017 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001018 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001019 rc = -EINVAL;
1020 break;
1021 default:
Max2c34ab42016-03-17 15:42:26 +01001022 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1023 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001024 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001025 break;
1026 }
1027
1028 return rc;
1029err_mand_ie:
1030 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1031}
1032
Harald Welte25de8112010-05-13 21:26:28 +02001033/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001034int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001035{
1036 struct bssgp_normal_hdr *bgph =
1037 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1038 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1039 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001040 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001041 uint8_t pdu_type = bgph->pdu_type;
1042 uint16_t ns_bvci = msgb_bvci(msg);
Max548caef2019-03-07 13:49:34 +01001043 uint16_t nsei = msgb_nsei(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001044 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001045 int data_len;
1046 int rc = 0;
1047
1048 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1049
1050 /* UNITDATA BSSGP headers have TLLI in front */
1051 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1052 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1053 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1054 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1055 } else {
1056 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1057 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1058 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001059 if (rc < 0) {
1060 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
1061 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Stefan Sperlingf1e13d62018-06-25 12:20:43 +02001062 if (pdu_type != BSSGP_PDUT_STATUS)
1063 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling2b544b22018-06-25 12:20:43 +02001064 return rc;
1065 }
Harald Welte25de8112010-05-13 21:26:28 +02001066
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001067 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001068 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001069
Harald Welte25de8112010-05-13 21:26:28 +02001070 /* look-up or create the BTS context for this BVC */
Max548caef2019-03-07 13:49:34 +01001071 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001072
Harald Welte16c8dbb2010-05-17 23:30:01 +02001073 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001074 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001075 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1076 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1077 msgb_bssgp_len(msg));
1078 }
1079
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001080 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1081 * are otherwise unexpected */
1082 if (pdu_type == BSSGP_PDUT_STATUS)
1083 /* Some exception has occurred */
1084 return bssgp_rx_status(msg, &tp, bvci, bctx);
1085
1086 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1087 * registered if a BVCI is given. */
1088 if (!bctx && bvci != BVCI_SIGNALLING &&
1089 pdu_type != BSSGP_PDUT_BVC_RESET) {
Max548caef2019-03-07 13:49:34 +01001090 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", nsei, bvci,
Max2c34ab42016-03-17 15:42:26 +01001091 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001092 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1093 }
1094
Harald Welte61c07842010-05-18 11:57:08 +02001095 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001096 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001097 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001098 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001099 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001100 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001101 else
1102 LOGP(DBSSGP, LOGL_NOTICE,
Max548caef2019-03-07 13:49:34 +01001103 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for unknown BVCI, NS BVCI %u\n", nsei, bvci,
1104 bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001105
1106 return rc;
1107}
1108
Harald Weltede4599c2012-06-17 13:04:02 +08001109int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1110 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001111{
Harald Welte8a521132010-05-17 22:59:29 +02001112 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001113 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001114 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001115 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001116 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001117 uint16_t bvci = msgb_bvci(msg);
1118 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001119 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001120 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001121
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001122 OSMO_ASSERT(dup != NULL);
1123
Harald Welte30bc19a2010-05-02 11:19:37 +02001124 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001125 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001126 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001127 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001128 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001129 return -EINVAL;
1130 }
1131
1132 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001133 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001134 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1135 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001136 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001137 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001138 }
Harald Welte9ba50052010-03-14 15:45:01 +08001139
1140 if (msg->len > TVLV_MAX_ONEBYTE)
1141 llc_pdu_tlv_hdr_len += 1;
1142
1143 /* prepend the tag and length of the LLC-PDU TLV */
1144 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1145 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1146 if (llc_pdu_tlv_hdr_len > 2) {
1147 llc_pdu_tlv[1] = msg_len >> 8;
1148 llc_pdu_tlv[2] = msg_len & 0xff;
1149 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001150 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001151 llc_pdu_tlv[1] |= 0x80;
1152 }
1153
Harald Welte2f946832010-05-31 22:12:30 +02001154 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1155
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001156 /* Old TLLI to help BSS map from old->new */
1157 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001158 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001159 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001160 }
Harald Welte9ba50052010-03-14 15:45:01 +08001161
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001162 /* IMSI */
1163 if (dup->imsi && strlen(dup->imsi)) {
Harald Welte1c3bae12019-01-20 10:37:49 +01001164 uint8_t mi[GSM48_MID_MAX_SIZE];
Harald Welte2033be82019-01-20 13:45:31 +01001165/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1166 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1167 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1168 * mi[131], which is wrong */
1169#pragma GCC diagnostic push
1170#pragma GCC diagnostic ignored "-Warray-bounds"
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001171 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1172 if (imsi_len > 2)
1173 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1174 imsi_len-2, mi+2);
Harald Welte2033be82019-01-20 13:45:31 +01001175#pragma GCC diagnostic pop
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001176 }
1177
1178 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001179 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001180 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1181 (uint8_t *) &drx_params);
1182
1183 /* FIXME: Priority */
1184
1185 /* MS Radio Access Capability */
1186 if (dup->ms_ra_cap.len)
1187 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1188 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1189
Harald Welte9ba50052010-03-14 15:45:01 +08001190 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001191 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001192
1193 /* prepend the QoS profile, TLLI and pdu type */
1194 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001195 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001196 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001197 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1198
Harald Welte16c8dbb2010-05-17 23:30:01 +02001199 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1200 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1201
Harald Welte30bc19a2010-05-02 11:19:37 +02001202 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001203
Harald Welted11c0592012-09-06 21:57:11 +02001204 /* check if we have to go through per-ms flow control or can go
1205 * directly to the per-BSS flow control */
1206 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001207 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001208 else
Harald Welted8b47692012-09-07 11:29:32 +02001209 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001210}
Harald Welte68b4f032010-06-09 16:22:28 +02001211
1212/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001213int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1214 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001215{
1216 struct msgb *msg = bssgp_msgb_alloc();
1217 struct bssgp_normal_hdr *bgph =
1218 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001219 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Welte1c3bae12019-01-20 10:37:49 +01001220 uint8_t mi[GSM48_MID_MAX_SIZE];
Harald Welte68b4f032010-06-09 16:22:28 +02001221 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001222 struct gsm48_ra_id ra;
Harald Welte68b4f032010-06-09 16:22:28 +02001223
1224 if (imsi_len < 2)
1225 return -EINVAL;
1226
1227 msgb_nsei(msg) = nsei;
1228 msgb_bvci(msg) = ns_bvci;
1229
1230 if (pinfo->mode == BSSGP_PAGING_PS)
1231 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1232 else
1233 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1234 /* IMSI */
Harald Welte2033be82019-01-20 13:45:31 +01001235/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
1236 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
1237 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
1238 * mi[131], which is wrong */
1239#pragma GCC diagnostic push
1240#pragma GCC diagnostic ignored "-Warray-bounds"
Harald Welte68b4f032010-06-09 16:22:28 +02001241 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
Harald Welte2033be82019-01-20 13:45:31 +01001242#pragma GCC diagnostic pop
Harald Welte68b4f032010-06-09 16:22:28 +02001243 /* DRX Parameters */
1244 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1245 (uint8_t *) &drx_params);
1246 /* Scope */
1247 switch (pinfo->scope) {
1248 case BSSGP_PAGING_BSS_AREA:
1249 {
1250 uint8_t null = 0;
1251 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1252 }
1253 break;
1254 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001255 gsm48_encode_ra(&ra, &pinfo->raid);
1256 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001257 break;
1258 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001259 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001260 break;
1261 case BSSGP_PAGING_BVCI:
1262 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001263 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001264 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1265 }
1266 break;
1267 }
1268 /* QoS profile mandatory for PS */
1269 if (pinfo->mode == BSSGP_PAGING_PS)
1270 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1271
1272 /* Optional (P-)TMSI */
1273 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001274 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001275 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1276 }
1277
1278 return gprs_ns_sendmsg(bssgp_nsi, msg);
1279}
Harald Weltecca49632012-06-16 17:45:59 +08001280
Harald Weltede4599c2012-06-17 13:04:02 +08001281void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001282{
1283 DBSSGP = ss;
1284}
Alexander Couzensacc0a072018-08-07 11:22:28 +02001285
1286/*!
1287 * \brief Flush the queue of the bssgp_flow_control
1288 * \param[in] The flow control object which holds the queue.
1289 */
1290void bssgp_fc_flush_queue(struct bssgp_flow_control *fc)
1291{
1292 struct bssgp_fc_queue_element *element, *tmp;
1293
1294 llist_for_each_entry_safe(element, tmp, &fc->queue, list) {
1295 msgb_free(element->msg);
1296 llist_del(&element->list);
1297 talloc_free(element);
1298 }
1299}
1300
1301/*!
1302 * \brief Flush the queues of all BSSGP contexts.
1303 */
1304void bssgp_flush_all_queues()
1305{
1306 struct bssgp_bvc_ctx *bctx;
1307
1308 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
1309 if (bctx->fc)
1310 bssgp_fc_flush_queue(bctx->fc);
1311 }
1312}