blob: 1445a56b16ea85837a670d82ebce4dffb8b43c58 [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 Welted8b47692012-09-07 11:29:32 +0200131 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
132 /* cofigure for 2Mbit, 30 packets in queue */
133 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200134
Harald Weltea78b9c22010-05-17 23:02:42 +0200135 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200136
137 return ctx;
138}
139
Harald Welte9ba50052010-03-14 15:45:01 +0800140/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200141static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800142{
143 struct msgb *msg = bssgp_msgb_alloc();
144 struct bssgp_normal_hdr *bgph =
145 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
146
Harald Welte24a655f2010-04-30 19:54:29 +0200147 msgb_nsei(msg) = nsei;
148 msgb_bvci(msg) = ns_bvci;
149
Harald Welte9ba50052010-03-14 15:45:01 +0800150 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
151 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
152
Harald Welte24a655f2010-04-30 19:54:29 +0200153 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800154}
155
Harald Weltea8aa4df2010-05-30 22:00:53 +0200156/* 10.3.7 SUSPEND-ACK PDU */
157int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
158 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
159{
160 struct msgb *msg = bssgp_msgb_alloc();
161 struct bssgp_normal_hdr *bgph =
162 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200163
164 msgb_nsei(msg) = nsei;
165 msgb_bvci(msg) = 0; /* Signalling */
166 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
167
Maxe29ec852018-01-05 14:30:22 +0100168 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100169 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200170 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
171
172 return gprs_ns_sendmsg(bssgp_nsi, msg);
173}
174
175/* 10.3.8 SUSPEND-NACK PDU */
176int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100177 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200178 uint8_t *cause)
179{
180 struct msgb *msg = bssgp_msgb_alloc();
181 struct bssgp_normal_hdr *bgph =
182 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200183
184 msgb_nsei(msg) = nsei;
185 msgb_bvci(msg) = 0; /* Signalling */
186 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
187
Maxe29ec852018-01-05 14:30:22 +0100188 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100189 bssgp_msgb_ra_put(msg, ra_id);
190
Harald Weltea8aa4df2010-05-30 22:00:53 +0200191 if (cause)
192 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
193
194 return gprs_ns_sendmsg(bssgp_nsi, msg);
195}
196
197/* 10.3.10 RESUME-ACK PDU */
198int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
199 const struct gprs_ra_id *ra_id)
200{
201 struct msgb *msg = bssgp_msgb_alloc();
202 struct bssgp_normal_hdr *bgph =
203 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200204
205 msgb_nsei(msg) = nsei;
206 msgb_bvci(msg) = 0; /* Signalling */
207 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
208
Maxe29ec852018-01-05 14:30:22 +0100209 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100210 bssgp_msgb_ra_put(msg, ra_id);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200211
212 return gprs_ns_sendmsg(bssgp_nsi, msg);
213}
214
215/* 10.3.11 RESUME-NACK PDU */
216int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
217 const struct gprs_ra_id *ra_id, uint8_t *cause)
218{
219 struct msgb *msg = bssgp_msgb_alloc();
220 struct bssgp_normal_hdr *bgph =
221 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltea8aa4df2010-05-30 22:00:53 +0200222
223 msgb_nsei(msg) = nsei;
224 msgb_bvci(msg) = 0; /* Signalling */
225 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
226
Maxe29ec852018-01-05 14:30:22 +0100227 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +0100228 bssgp_msgb_ra_put(msg, ra_id);
229
Harald Weltea8aa4df2010-05-30 22:00:53 +0200230 if (cause)
231 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
232
233 return gprs_ns_sendmsg(bssgp_nsi, msg);
234}
235
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200236uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200237{
238 /* 6 octets RAC */
239 gsm48_parse_ra(raid, buf);
240 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200241 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200242}
243
Harald Welte28610072011-11-24 21:32:07 +0100244int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
245 uint16_t cid)
246{
Harald Welte28610072011-11-24 21:32:07 +0100247 /* 6 octets RAC */
Maxf1ad60e2018-01-05 14:19:33 +0100248 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte28610072011-11-24 21:32:07 +0100249 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200250 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100251
252 return 8;
253}
254
Harald Welte3fddf3c2010-05-01 16:48:27 +0200255/* Chapter 8.4 BVC-Reset Procedure */
256static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
257 uint16_t ns_bvci)
258{
Harald Welte15a36432012-06-17 12:16:31 +0800259 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200260 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200261 uint16_t nsei = msgb_nsei(msg);
262 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200263
Harald Weltebfe62e52017-05-15 12:48:30 +0200264 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200265 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200266 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
267
Harald Welte6752fa42010-05-02 09:23:16 +0200268 /* look-up or create the BTS context for this BVC */
269 bctx = btsctx_by_bvci_nsei(bvci, nsei);
270 if (!bctx)
271 bctx = btsctx_alloc(bvci, nsei);
272
Harald Welte25de8112010-05-13 21:26:28 +0200273 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
274 bctx->state &= ~BVC_S_BLOCKED;
275
Harald Welte3fddf3c2010-05-01 16:48:27 +0200276 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
277 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200278 if (bvci != 0 && bvci != 1) {
279 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200280 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200281 "missing mandatory IE\n", bvci);
282 return -EINVAL;
283 }
284 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200285 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
286 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100287 LOGP(DBSSGP, LOGL_NOTICE, "Cell %s CI %u on BVCI %u\n",
288 osmo_rai_name(&bctx->ra_id), bctx->cell_id, bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200289 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200290
Harald Welte15a36432012-06-17 12:16:31 +0800291 /* Send NM_BVC_RESET.ind to NM */
292 memset(&nmp, 0, sizeof(nmp));
293 nmp.nsei = nsei;
294 nmp.bvci = bvci;
295 nmp.tp = tp;
296 nmp.ra_id = &bctx->ra_id;
297 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
298 PRIM_OP_INDICATION, msg);
299 bssgp_prim_cb(&nmp.oph, NULL);
300
Harald Welte6752fa42010-05-02 09:23:16 +0200301 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200302 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
303 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200304 return 0;
305}
306
Harald Welte25de8112010-05-13 21:26:28 +0200307static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
308{
Harald Welte15a36432012-06-17 12:16:31 +0800309 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200310 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200311 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200312
Harald Weltebfe62e52017-05-15 12:48:30 +0200313 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200314 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200315 /* 8.3.2: Signalling BVC shall never be blocked */
316 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
317 "received block for signalling BVC!?!\n",
318 msgb_nsei(msg), msgb_bvci(msg));
319 return 0;
320 }
Harald Welte25de8112010-05-13 21:26:28 +0200321
Harald Welte086fe322011-08-19 16:45:19 +0200322 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200323
324 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
325 if (!ptp_ctx)
326 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
327
328 ptp_ctx->state |= BVC_S_BLOCKED;
329 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
330
Harald Welte15a36432012-06-17 12:16:31 +0800331 /* Send NM_BVC_BLOCK.ind to NM */
332 memset(&nmp, 0, sizeof(nmp));
333 nmp.nsei = msgb_nsei(msg);
334 nmp.bvci = bvci;
335 nmp.tp = tp;
336 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
337 PRIM_OP_INDICATION, msg);
338 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200339
340 /* We always acknowledge the BLOCKing */
341 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
342 bvci, msgb_bvci(msg));
343};
344
345static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
346{
Harald Welte15a36432012-06-17 12:16:31 +0800347 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200348 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200349 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200350
Harald Weltebfe62e52017-05-15 12:48:30 +0200351 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200352 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200353 /* 8.3.2: Signalling BVC shall never be blocked */
354 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
355 "received unblock for signalling BVC!?!\n",
356 msgb_nsei(msg), msgb_bvci(msg));
357 return 0;
358 }
Harald Welte25de8112010-05-13 21:26:28 +0200359
Harald Weltee9686b62010-05-31 18:07:17 +0200360 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200361
362 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
363 if (!ptp_ctx)
364 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
365
366 ptp_ctx->state &= ~BVC_S_BLOCKED;
367
Harald Welte15a36432012-06-17 12:16:31 +0800368 /* Send NM_BVC_UNBLOCK.ind to NM */
369 memset(&nmp, 0, sizeof(nmp));
370 nmp.nsei = msgb_nsei(msg);
371 nmp.bvci = bvci;
372 nmp.tp = tp;
373 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
374 PRIM_OP_INDICATION, msg);
375 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200376
377 /* We always acknowledge the unBLOCKing */
378 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
379 bvci, msgb_bvci(msg));
380};
381
Harald Welte9ba50052010-03-14 15:45:01 +0800382/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200383static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200384 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800385{
Harald Welte15a36432012-06-17 12:16:31 +0800386 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200387 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800388
Harald Welte6752fa42010-05-02 09:23:16 +0200389 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200390 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800391
Harald Welte086fe322011-08-19 16:45:19 +0200392 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200393
Harald Welte9ba50052010-03-14 15:45:01 +0800394 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200395 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200396 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
397 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
398 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200399 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200400 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200401
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200402 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800403 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
404 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800405
Harald Welte15a36432012-06-17 12:16:31 +0800406 /* Send BSSGP_UL_UD.ind to NM */
407 memset(&gbp, 0, sizeof(gbp));
408 gbp.nsei = ctx->nsei;
409 gbp.bvci = ctx->bvci;
410 gbp.tlli = msgb_tlli(msg);
411 gbp.tp = tp;
412 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
413 PRIM_OP_INDICATION, msg);
414 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800415}
416
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200417static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800418{
Harald Welte15a36432012-06-17 12:16:31 +0800419 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200420 struct gprs_ra_id raid;
421 uint32_t tlli;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200422 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200423 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800424
Harald Welte25de8112010-05-13 21:26:28 +0200425 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200426 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
427 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200428 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200429 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200430 }
Harald Welte9ba50052010-03-14 15:45:01 +0800431
Harald Weltebfe62e52017-05-15 12:48:30 +0200432 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200433
Harald Welte17925322010-05-31 20:18:35 +0200434 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200435 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200436
Harald Weltea8aa4df2010-05-30 22:00:53 +0200437 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
438
Harald Welte313cccf2010-06-09 11:22:47 +0200439 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800440 memset(&gbp, 0, sizeof(gbp));
441 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200442 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800443 gbp.tlli = tlli;
444 gbp.ra_id = &raid;
445 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
446 PRIM_OP_REQUEST, msg);
447
448 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200449 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100450 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200451
Harald Weltea8aa4df2010-05-30 22:00:53 +0200452 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
453
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800454 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800455}
456
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200457static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800458{
Harald Welte15a36432012-06-17 12:16:31 +0800459 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200460 struct gprs_ra_id raid;
461 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200462 uint8_t suspend_ref;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200463 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200464 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800465
Harald Welte25de8112010-05-13 21:26:28 +0200466 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
467 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200468 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
469 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200470 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200471 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200472 }
Harald Welte9ba50052010-03-14 15:45:01 +0800473
Harald Weltebfe62e52017-05-15 12:48:30 +0200474 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200475 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200476
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200477 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200478
Harald Weltea8aa4df2010-05-30 22:00:53 +0200479 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
480
Harald Welte313cccf2010-06-09 11:22:47 +0200481 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800482 memset(&gbp, 0, sizeof(gbp));
483 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200484 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800485 gbp.tlli = tlli;
486 gbp.ra_id = &raid;
487 gbp.u.resume.suspend_ref = suspend_ref;
488 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
489 PRIM_OP_REQUEST, msg);
490
491 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200492 if (rc < 0)
493 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
494 NULL);
495
Harald Weltea8aa4df2010-05-30 22:00:53 +0200496 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800497 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800498}
499
Harald Weltee9686b62010-05-31 18:07:17 +0200500
501static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
502 struct bssgp_bvc_ctx *ctx)
503{
Harald Welte15a36432012-06-17 12:16:31 +0800504 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200505 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200506
507 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
508 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
509 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
510 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
511 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
512 "missing mandatory IE\n", ctx->bvci);
513 }
514
Harald Welteb7363142010-07-23 21:59:29 +0200515 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200516 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200517
Harald Welte086fe322011-08-19 16:45:19 +0200518 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200519 ctx->bvci, tlli);
520
521 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
522
Harald Welte15a36432012-06-17 12:16:31 +0800523 /* send NM_LLC_DISCARDED to NM */
524 memset(&nmp, 0, sizeof(nmp));
525 nmp.nsei = msgb_nsei(msg);
526 nmp.bvci = ctx->bvci;
527 nmp.tlli = tlli;
528 nmp.tp = tp;
529 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
530 PRIM_OP_INDICATION, msg);
531
532 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200533}
534
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100535int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
536 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
537{
538 struct osmo_bssgp_prim nmp;
539 enum gprs_bssgp_cause cause;
540
541 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
542 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
543 "missing mandatory IE\n", bvci);
544 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
545 } else {
546 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
547 }
548
549 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
550 bvci, bssgp_cause_str(cause));
551
552 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
553 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
554 LOGP(DBSSGP, LOGL_ERROR,
555 "BSSGP BVCI=%u Rx STATUS cause=%s "
556 "missing conditional BVCI IE\n",
557 bvci, bssgp_cause_str(cause));
558 }
559
560 if (bctx)
561 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
562
563 /* send NM_STATUS to NM */
564 memset(&nmp, 0, sizeof(nmp));
565 nmp.nsei = msgb_nsei(msg);
566 nmp.bvci = bvci;
567 nmp.tp = tp;
568 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
569 PRIM_OP_INDICATION, msg);
570
571 return bssgp_prim_cb(&nmp.oph, NULL);
572}
573
574
Harald Welted11c0592012-09-06 21:57:11 +0200575/* One element (msgb) in a BSSGP Flow Control queue */
576struct bssgp_fc_queue_element {
577 /* linked list of queue elements */
578 struct llist_head list;
579 /* The message that we have enqueued */
580 struct msgb *msg;
581 /* Length of the LLC PDU part of the contained message */
582 uint32_t llc_pdu_len;
583 /* private pointer passed to the flow control out_cb function */
584 void *priv;
585};
586
587static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
588static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
589
590static void fc_timer_cb(void *data)
591{
592 struct bssgp_flow_control *fc = data;
593 struct bssgp_fc_queue_element *fcqe;
594 struct timeval time_now;
595
596 /* if the queue is empty, we return without sending something
597 * and without re-starting the timer */
598 if (llist_empty(&fc->queue))
599 return;
600
601 /* get the first entry from the queue */
602 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
603 list);
604
605 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
606 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
607 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
608 /* make sure we re-start the timer */
609 fc_queue_timer_cfg(fc);
610 return;
611 }
612
613 /* remove from the queue */
614 llist_del(&fcqe->list);
615
616 fc->queue_depth--;
617
618 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200619 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200620 fc->time_last_pdu = time_now;
621
622 /* call the output callback for this FC instance */
623 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
624
625 /* we expect that out_cb will in the end free the msgb once
626 * it is no longer needed */
627
628 /* but we have to free the queue element ourselves */
629 talloc_free(fcqe);
630
631 /* re-configure the timer for the next PDU */
632 fc_queue_timer_cfg(fc);
633}
634
635/* configure/schedule the flow control timer to expire once the bucket
636 * will have leaked a sufficient number of bytes to transmit the next
637 * PDU in the queue */
638static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
639{
640 struct bssgp_fc_queue_element *fcqe;
641 uint32_t msecs;
642
643 if (llist_empty(&fc->queue))
644 return 0;
645
Jacob Erlbeck97319352015-04-30 19:28:03 +0200646 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200647 list);
648
Harald Welte27b2bb72013-06-22 09:44:00 +0200649 if (fc->bucket_leak_rate != 0) {
650 /* Calculate the point in time at which we will have leaked
651 * a sufficient number of bytes from the bucket to transmit
652 * the first PDU in the queue */
653 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
654 /* FIXME: add that time to fc->time_last_pdu and subtract it from
655 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200656 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200657 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
658 } else {
659 /* If the PCU is telling us to not send any more data at all,
660 * there's no point starting a timer. */
661 }
Harald Welted11c0592012-09-06 21:57:11 +0200662
663 return 0;
664}
665
666/* Enqueue a PDU in the flow control queue for delayed transmission */
667static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
668 uint32_t llc_pdu_len, void *priv)
669{
670 struct bssgp_fc_queue_element *fcqe;
671
672 if (fc->queue_depth >= fc->max_queue_depth)
673 return -ENOSPC;
674
675 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
676 if (!fcqe)
677 return -ENOMEM;
678 fcqe->msg = msg;
679 fcqe->llc_pdu_len = llc_pdu_len;
680 fcqe->priv = priv;
681
682 llist_add_tail(&fcqe->list, &fc->queue);
683
684 fc->queue_depth++;
685
686 /* re-configure the timer for dequeueing the pdu */
687 fc_queue_timer_cfg(fc);
688
689 return 0;
690}
691
692/* According to Section 8.2 */
693static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
694{
695 struct timeval time_now, time_diff;
696 int64_t bucket_predicted;
697 uint32_t csecs_elapsed, leaked;
698
699 /* B' = B + L(p) - (Tc - Tp)*R */
700
701 /* compute number of centi-seconds that have elapsed since transmitting
702 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200703 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200704 timersub(&time_now, &fc->time_last_pdu, &time_diff);
705 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
706
707 /* compute number of bytes that have leaked in the elapsed number
708 * of centi-seconds */
709 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
710 /* add the current PDU length to the last bucket level */
711 bucket_predicted = fc->bucket_counter + pdu_len;
712 /* ... and subtract the number of leaked bytes */
713 bucket_predicted -= leaked;
714
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700715 if (bucket_predicted < pdu_len)
716 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200717
718 if (bucket_predicted <= fc->bucket_size_max) {
719 /* the bucket is not full yet, we can pass the packet */
720 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700721 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200722 }
723
724 /* bucket is full, PDU needs to be delayed */
725 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200726}
727
728/* output callback for BVC flow control */
729static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
730 uint32_t llc_pdu_len, void *priv)
731{
732 return gprs_ns_sendmsg(bssgp_nsi, msg);
733}
734
735/* input function of the flow control implementation, called first
736 * for the MM flow control, and then as the MM flow control output
737 * callback in order to perform BVC flow control */
738int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
739 uint32_t llc_pdu_len, void *priv)
740{
741 struct timeval time_now;
742
Harald Weltebb826222012-09-07 10:22:01 +0200743 if (llc_pdu_len > fc->bucket_size_max) {
744 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
745 "than maximum bucket size (%u)!\n", llc_pdu_len,
746 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200747 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200748 return -EIO;
749 }
750
Harald Welted11c0592012-09-06 21:57:11 +0200751 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100752 int rc;
753 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
754 if (rc)
755 msgb_free(msg);
756 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200757 } else {
758 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200759 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200760 fc->time_last_pdu = time_now;
761 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
762 }
763}
764
Harald Weltebb826222012-09-07 10:22:01 +0200765
766/* Initialize the Flow Control structure */
767void bssgp_fc_init(struct bssgp_flow_control *fc,
768 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
769 uint32_t max_queue_depth,
770 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
771 uint32_t llc_pdu_len, void *priv))
772{
773 fc->out_cb = out_cb;
774 fc->bucket_size_max = bucket_size_max;
775 fc->bucket_leak_rate = bucket_leak_rate;
776 fc->max_queue_depth = max_queue_depth;
777 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200778 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200779}
780
Harald Welted11c0592012-09-06 21:57:11 +0200781/* Initialize the Flow Control parameters for a new MS according to
782 * default values for the BVC specified by BVCI and NSEI */
783int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200784 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200785{
786 struct bssgp_bvc_ctx *ctx;
787
788 ctx = btsctx_by_bvci_nsei(bvci, nsei);
789 if (!ctx)
790 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200791
792 /* output call-back of per-MS FC is per-CTX FC */
793 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
794 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200795
796 return 0;
797}
798
Harald Welte25de8112010-05-13 21:26:28 +0200799static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200800 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800801{
Harald Welte27b2bb72013-06-22 09:44:00 +0200802 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
803 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800804
Harald Weltee9686b62010-05-31 18:07:17 +0200805 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
806 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800807
808 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
809 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
810 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
811 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200812 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
813 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
814 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800815 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200816 }
Harald Welte9ba50052010-03-14 15:45:01 +0800817
Harald Weltebb826222012-09-07 10:22:01 +0200818 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200819 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200820 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200821 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200822 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200823 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200824 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200825 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200826
Harald Welte27b2bb72013-06-22 09:44:00 +0200827 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
828 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
829 "rate of 0, stopping all DL GPRS!\n");
830 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
831 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
832 "rate of != 0, restarting all DL GPRS!\n");
833
834 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
835 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
836 "bucket leak rate of 0, stopping DL GPRS!\n");
837 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
838 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
839 "bucket leak rate != 0, restarting DL GPRS!\n");
840
841 /* reconfigure the timer for flow control based on new values */
842 fc_queue_timer_cfg(bctx->fc);
843
Harald Welte9ba50052010-03-14 15:45:01 +0800844 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200845 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200846 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800847}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200848
Harald Welte25de8112010-05-13 21:26:28 +0200849/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800850static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
851 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800852{
Harald Welteec19c102010-05-02 09:50:42 +0200853 struct bssgp_normal_hdr *bgph =
854 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200855 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800856 int rc = 0;
857
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100858 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
859
Harald Welte58e65c92010-05-13 21:45:23 +0200860 /* If traffic is received on a BVC that is marked as blocked, the
861 * received PDU shall not be accepted and a STATUS PDU (Cause value:
862 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100863 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200864 uint16_t bvci = msgb_bvci(msg);
865 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
866 }
867
Harald Welte9ba50052010-03-14 15:45:01 +0800868 switch (pdu_type) {
869 case BSSGP_PDUT_UL_UNITDATA:
870 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200871 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800872 break;
873 case BSSGP_PDUT_RA_CAPABILITY:
874 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200875 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
876 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200877 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800878 /* FIXME: send RA_CAPA_UPDATE_ACK */
879 break;
880 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200881 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800882 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200883 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800884 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800885 case BSSGP_PDUT_FLOW_CONTROL_BVC:
886 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200887 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800888 break;
889 case BSSGP_PDUT_FLOW_CONTROL_MS:
890 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200891 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
892 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200893 /* FIXME: actually implement flow control */
894 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800895 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800896 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100897 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100898 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800899 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
900 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
901 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
902 case BSSGP_PDUT_MODIFY_BSS_PFC:
903 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100904 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
905 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200906 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800907 break;
908 /* those only exist in the SGSN -> BSS direction */
909 case BSSGP_PDUT_DL_UNITDATA:
910 case BSSGP_PDUT_PAGING_PS:
911 case BSSGP_PDUT_PAGING_CS:
912 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200913 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
914 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100915 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
916 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200917 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
918 rc = -EINVAL;
919 break;
920 default:
Max2c34ab42016-03-17 15:42:26 +0100921 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
922 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200923 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
924 break;
925 }
926
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800927 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200928}
929
930/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800931static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
932 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200933{
934 struct bssgp_normal_hdr *bgph =
935 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
936 uint8_t pdu_type = bgph->pdu_type;
937 int rc = 0;
938 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200939 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200940
941 switch (bgph->pdu_type) {
942 case BSSGP_PDUT_SUSPEND:
943 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200944 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200945 break;
946 case BSSGP_PDUT_RESUME:
947 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200948 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200949 break;
950 case BSSGP_PDUT_FLUSH_LL_ACK:
951 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200952 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200953 /* FIXME: send NM_FLUSH_LL.res to NM */
954 break;
955 case BSSGP_PDUT_LLC_DISCARD:
956 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200957 if (!bctx) {
958 LOGP(DBSSGP, LOGL_ERROR,
959 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
960 goto err_mand_ie;
961 }
Harald Weltee9686b62010-05-31 18:07:17 +0200962 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200963 break;
964 case BSSGP_PDUT_BVC_BLOCK:
965 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200966 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200967 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
968 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
969 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200970 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200971 }
Harald Welte2677ea52010-05-31 17:16:36 +0200972 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200973 break;
974 case BSSGP_PDUT_BVC_UNBLOCK:
975 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200976 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
977 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
978 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200979 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200980 }
Harald Welte25de8112010-05-13 21:26:28 +0200981 rc = bssgp_rx_bvc_unblock(msg, tp);
982 break;
Max590c4022017-06-28 14:29:24 +0200983 case BSSGP_PDUT_BVC_RESET_ACK:
984 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
985 break;
Harald Welte25de8112010-05-13 21:26:28 +0200986 case BSSGP_PDUT_BVC_RESET:
987 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200988 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200989 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
990 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
991 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200992 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200993 }
Harald Welte25de8112010-05-13 21:26:28 +0200994 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
995 break;
996 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100997 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +0200998 break;
999 /* those only exist in the SGSN -> BSS direction */
1000 case BSSGP_PDUT_PAGING_PS:
1001 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001002 case BSSGP_PDUT_SUSPEND_ACK:
1003 case BSSGP_PDUT_SUSPEND_NACK:
1004 case BSSGP_PDUT_RESUME_ACK:
1005 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001006 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001007 case BSSGP_PDUT_BVC_BLOCK_ACK:
1008 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1009 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001010 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1011 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001012 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001013 rc = -EINVAL;
1014 break;
1015 default:
Max2c34ab42016-03-17 15:42:26 +01001016 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1017 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001018 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001019 break;
1020 }
1021
1022 return rc;
1023err_mand_ie:
1024 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1025}
1026
Harald Welte25de8112010-05-13 21:26:28 +02001027/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001028int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001029{
1030 struct bssgp_normal_hdr *bgph =
1031 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1032 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1033 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001034 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001035 uint8_t pdu_type = bgph->pdu_type;
1036 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001037 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001038 int data_len;
1039 int rc = 0;
1040
1041 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1042
1043 /* UNITDATA BSSGP headers have TLLI in front */
1044 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1045 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1046 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1047 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1048 } else {
1049 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1050 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1051 }
Stefan Sperling2b544b22018-06-25 12:20:43 +02001052 if (rc < 0) {
1053 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
1054 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
1055 return rc;
1056 }
Harald Welte25de8112010-05-13 21:26:28 +02001057
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001058 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001059 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001060
Harald Welte25de8112010-05-13 21:26:28 +02001061 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001062 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Harald Welte25de8112010-05-13 21:26:28 +02001063
Harald Welte16c8dbb2010-05-17 23:30:01 +02001064 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001065 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001066 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1067 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1068 msgb_bssgp_len(msg));
1069 }
1070
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001071 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1072 * are otherwise unexpected */
1073 if (pdu_type == BSSGP_PDUT_STATUS)
1074 /* Some exception has occurred */
1075 return bssgp_rx_status(msg, &tp, bvci, bctx);
1076
1077 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1078 * registered if a BVCI is given. */
1079 if (!bctx && bvci != BVCI_SIGNALLING &&
1080 pdu_type != BSSGP_PDUT_BVC_RESET) {
1081 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
Max2c34ab42016-03-17 15:42:26 +01001082 "type %s for unknown BVCI\n", msgb_nsei(msg), bvci,
1083 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001084 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1085 }
1086
Harald Welte61c07842010-05-18 11:57:08 +02001087 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001088 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001089 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001090 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001091 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001092 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001093 else
1094 LOGP(DBSSGP, LOGL_NOTICE,
Max2c34ab42016-03-17 15:42:26 +01001095 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for "
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001096 "unknown BVCI, NS BVCI %u\n",
Max2c34ab42016-03-17 15:42:26 +01001097 msgb_nsei(msg), bvci, bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001098
1099 return rc;
1100}
1101
Harald Weltede4599c2012-06-17 13:04:02 +08001102int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1103 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001104{
Harald Welte8a521132010-05-17 22:59:29 +02001105 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001106 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001107 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001108 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001109 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001110 uint16_t bvci = msgb_bvci(msg);
1111 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001112 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001113 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001114
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001115 OSMO_ASSERT(dup != NULL);
1116
Harald Welte30bc19a2010-05-02 11:19:37 +02001117 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001118 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001119 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001120 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001121 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001122 return -EINVAL;
1123 }
1124
1125 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001126 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001127 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1128 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001129 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001130 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001131 }
Harald Welte9ba50052010-03-14 15:45:01 +08001132
1133 if (msg->len > TVLV_MAX_ONEBYTE)
1134 llc_pdu_tlv_hdr_len += 1;
1135
1136 /* prepend the tag and length of the LLC-PDU TLV */
1137 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1138 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1139 if (llc_pdu_tlv_hdr_len > 2) {
1140 llc_pdu_tlv[1] = msg_len >> 8;
1141 llc_pdu_tlv[2] = msg_len & 0xff;
1142 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001143 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001144 llc_pdu_tlv[1] |= 0x80;
1145 }
1146
Harald Welte2f946832010-05-31 22:12:30 +02001147 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1148
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001149 /* Old TLLI to help BSS map from old->new */
1150 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001151 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001152 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001153 }
Harald Welte9ba50052010-03-14 15:45:01 +08001154
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001155 /* IMSI */
1156 if (dup->imsi && strlen(dup->imsi)) {
1157 uint8_t mi[10];
1158 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1159 if (imsi_len > 2)
1160 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1161 imsi_len-2, mi+2);
1162 }
1163
1164 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001165 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001166 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1167 (uint8_t *) &drx_params);
1168
1169 /* FIXME: Priority */
1170
1171 /* MS Radio Access Capability */
1172 if (dup->ms_ra_cap.len)
1173 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1174 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1175
Harald Welte9ba50052010-03-14 15:45:01 +08001176 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001177 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001178
1179 /* prepend the QoS profile, TLLI and pdu type */
1180 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001181 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001182 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001183 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1184
Harald Welte16c8dbb2010-05-17 23:30:01 +02001185 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1186 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1187
Harald Welte30bc19a2010-05-02 11:19:37 +02001188 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001189
Harald Welted11c0592012-09-06 21:57:11 +02001190 /* check if we have to go through per-ms flow control or can go
1191 * directly to the per-BSS flow control */
1192 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001193 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001194 else
Harald Welted8b47692012-09-07 11:29:32 +02001195 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001196}
Harald Welte68b4f032010-06-09 16:22:28 +02001197
1198/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001199int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1200 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001201{
1202 struct msgb *msg = bssgp_msgb_alloc();
1203 struct bssgp_normal_hdr *bgph =
1204 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001205 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Welte68b4f032010-06-09 16:22:28 +02001206 uint8_t mi[10];
1207 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
Maxf1ad60e2018-01-05 14:19:33 +01001208 struct gsm48_ra_id ra;
Harald Welte68b4f032010-06-09 16:22:28 +02001209
1210 if (imsi_len < 2)
1211 return -EINVAL;
1212
1213 msgb_nsei(msg) = nsei;
1214 msgb_bvci(msg) = ns_bvci;
1215
1216 if (pinfo->mode == BSSGP_PAGING_PS)
1217 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1218 else
1219 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1220 /* IMSI */
1221 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1222 /* DRX Parameters */
1223 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1224 (uint8_t *) &drx_params);
1225 /* Scope */
1226 switch (pinfo->scope) {
1227 case BSSGP_PAGING_BSS_AREA:
1228 {
1229 uint8_t null = 0;
1230 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1231 }
1232 break;
1233 case BSSGP_PAGING_LOCATION_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001234 gsm48_encode_ra(&ra, &pinfo->raid);
1235 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, (const uint8_t *)&ra);
Harald Welte68b4f032010-06-09 16:22:28 +02001236 break;
1237 case BSSGP_PAGING_ROUTEING_AREA:
Maxf1ad60e2018-01-05 14:19:33 +01001238 bssgp_msgb_ra_put(msg, &pinfo->raid);
Harald Welte68b4f032010-06-09 16:22:28 +02001239 break;
1240 case BSSGP_PAGING_BVCI:
1241 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001242 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001243 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1244 }
1245 break;
1246 }
1247 /* QoS profile mandatory for PS */
1248 if (pinfo->mode == BSSGP_PAGING_PS)
1249 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1250
1251 /* Optional (P-)TMSI */
1252 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001253 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001254 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1255 }
1256
1257 return gprs_ns_sendmsg(bssgp_nsi, msg);
1258}
Harald Weltecca49632012-06-16 17:45:59 +08001259
Harald Weltede4599c2012-06-17 13:04:02 +08001260void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001261{
1262 DBSSGP = ss;
1263}