blob: d27a94f75436ad911a33488cdff82ae2270a792b [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));
163 uint32_t _tlli;
164 uint8_t ra[6];
165
166 msgb_nsei(msg) = nsei;
167 msgb_bvci(msg) = 0; /* Signalling */
168 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
169
Harald Weltebfe62e52017-05-15 12:48:30 +0200170 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200171 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
172 gsm48_construct_ra(ra, ra_id);
173 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
174 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));
187 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100188 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200189
190 msgb_nsei(msg) = nsei;
191 msgb_bvci(msg) = 0; /* Signalling */
192 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
193
Harald Weltebfe62e52017-05-15 12:48:30 +0200194 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200195 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100196 gsm48_construct_ra(ra, ra_id);
197 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200198 if (cause)
199 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
200
201 return gprs_ns_sendmsg(bssgp_nsi, msg);
202}
203
204/* 10.3.10 RESUME-ACK PDU */
205int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
206 const struct gprs_ra_id *ra_id)
207{
208 struct msgb *msg = bssgp_msgb_alloc();
209 struct bssgp_normal_hdr *bgph =
210 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
211 uint32_t _tlli;
212 uint8_t ra[6];
213
214 msgb_nsei(msg) = nsei;
215 msgb_bvci(msg) = 0; /* Signalling */
216 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
217
Harald Weltebfe62e52017-05-15 12:48:30 +0200218 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200219 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
220 gsm48_construct_ra(ra, ra_id);
221 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
222
223 return gprs_ns_sendmsg(bssgp_nsi, msg);
224}
225
226/* 10.3.11 RESUME-NACK PDU */
227int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
228 const struct gprs_ra_id *ra_id, uint8_t *cause)
229{
230 struct msgb *msg = bssgp_msgb_alloc();
231 struct bssgp_normal_hdr *bgph =
232 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
233 uint32_t _tlli;
234 uint8_t ra[6];
235
236 msgb_nsei(msg) = nsei;
237 msgb_bvci(msg) = 0; /* Signalling */
238 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
239
Harald Weltebfe62e52017-05-15 12:48:30 +0200240 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200241 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
242 gsm48_construct_ra(ra, ra_id);
243 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
244 if (cause)
245 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
246
247 return gprs_ns_sendmsg(bssgp_nsi, msg);
248}
249
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200250uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200251{
252 /* 6 octets RAC */
253 gsm48_parse_ra(raid, buf);
254 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200255 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200256}
257
Harald Welte28610072011-11-24 21:32:07 +0100258int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
259 uint16_t cid)
260{
Harald Welte28610072011-11-24 21:32:07 +0100261 /* 6 octets RAC */
262 gsm48_construct_ra(buf, raid);
263 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200264 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100265
266 return 8;
267}
268
Harald Welte3fddf3c2010-05-01 16:48:27 +0200269/* Chapter 8.4 BVC-Reset Procedure */
270static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
271 uint16_t ns_bvci)
272{
Harald Welte15a36432012-06-17 12:16:31 +0800273 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200274 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200275 uint16_t nsei = msgb_nsei(msg);
276 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200277
Harald Weltebfe62e52017-05-15 12:48:30 +0200278 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200279 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200280 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
281
Harald Welte6752fa42010-05-02 09:23:16 +0200282 /* look-up or create the BTS context for this BVC */
283 bctx = btsctx_by_bvci_nsei(bvci, nsei);
284 if (!bctx)
285 bctx = btsctx_alloc(bvci, nsei);
286
Harald Welte25de8112010-05-13 21:26:28 +0200287 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
288 bctx->state &= ~BVC_S_BLOCKED;
289
Harald Welte3fddf3c2010-05-01 16:48:27 +0200290 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
291 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200292 if (bvci != 0 && bvci != 1) {
293 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200294 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200295 "missing mandatory IE\n", bvci);
296 return -EINVAL;
297 }
298 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200299 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
300 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200301 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200302 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
303 bctx->ra_id.rac, bctx->cell_id, bvci);
304 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200305
Harald Welte15a36432012-06-17 12:16:31 +0800306 /* Send NM_BVC_RESET.ind to NM */
307 memset(&nmp, 0, sizeof(nmp));
308 nmp.nsei = nsei;
309 nmp.bvci = bvci;
310 nmp.tp = tp;
311 nmp.ra_id = &bctx->ra_id;
312 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
313 PRIM_OP_INDICATION, msg);
314 bssgp_prim_cb(&nmp.oph, NULL);
315
Harald Welte6752fa42010-05-02 09:23:16 +0200316 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200317 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
318 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200319 return 0;
320}
321
Harald Welte25de8112010-05-13 21:26:28 +0200322static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
323{
Harald Welte15a36432012-06-17 12:16:31 +0800324 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200325 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200326 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200327
Harald Weltebfe62e52017-05-15 12:48:30 +0200328 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200329 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200330 /* 8.3.2: Signalling BVC shall never be blocked */
331 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
332 "received block for signalling BVC!?!\n",
333 msgb_nsei(msg), msgb_bvci(msg));
334 return 0;
335 }
Harald Welte25de8112010-05-13 21:26:28 +0200336
Harald Welte086fe322011-08-19 16:45:19 +0200337 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200338
339 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
340 if (!ptp_ctx)
341 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
342
343 ptp_ctx->state |= BVC_S_BLOCKED;
344 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
345
Harald Welte15a36432012-06-17 12:16:31 +0800346 /* Send NM_BVC_BLOCK.ind to NM */
347 memset(&nmp, 0, sizeof(nmp));
348 nmp.nsei = msgb_nsei(msg);
349 nmp.bvci = bvci;
350 nmp.tp = tp;
351 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
352 PRIM_OP_INDICATION, msg);
353 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200354
355 /* We always acknowledge the BLOCKing */
356 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
357 bvci, msgb_bvci(msg));
358};
359
360static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
361{
Harald Welte15a36432012-06-17 12:16:31 +0800362 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200363 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200364 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200365
Harald Weltebfe62e52017-05-15 12:48:30 +0200366 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200367 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200368 /* 8.3.2: Signalling BVC shall never be blocked */
369 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
370 "received unblock for signalling BVC!?!\n",
371 msgb_nsei(msg), msgb_bvci(msg));
372 return 0;
373 }
Harald Welte25de8112010-05-13 21:26:28 +0200374
Harald Weltee9686b62010-05-31 18:07:17 +0200375 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200376
377 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
378 if (!ptp_ctx)
379 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
380
381 ptp_ctx->state &= ~BVC_S_BLOCKED;
382
Harald Welte15a36432012-06-17 12:16:31 +0800383 /* Send NM_BVC_UNBLOCK.ind to NM */
384 memset(&nmp, 0, sizeof(nmp));
385 nmp.nsei = msgb_nsei(msg);
386 nmp.bvci = bvci;
387 nmp.tp = tp;
388 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
389 PRIM_OP_INDICATION, msg);
390 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200391
392 /* We always acknowledge the unBLOCKing */
393 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
394 bvci, msgb_bvci(msg));
395};
396
Harald Welte9ba50052010-03-14 15:45:01 +0800397/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200398static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200399 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800400{
Harald Welte15a36432012-06-17 12:16:31 +0800401 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200402 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800403
Harald Welte6752fa42010-05-02 09:23:16 +0200404 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200405 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800406
Harald Welte086fe322011-08-19 16:45:19 +0200407 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200408
Harald Welte9ba50052010-03-14 15:45:01 +0800409 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200410 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200411 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
412 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
413 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200414 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200415 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200416
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200417 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800418 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
419 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800420
Harald Welte15a36432012-06-17 12:16:31 +0800421 /* Send BSSGP_UL_UD.ind to NM */
422 memset(&gbp, 0, sizeof(gbp));
423 gbp.nsei = ctx->nsei;
424 gbp.bvci = ctx->bvci;
425 gbp.tlli = msgb_tlli(msg);
426 gbp.tp = tp;
427 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
428 PRIM_OP_INDICATION, msg);
429 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800430}
431
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200432static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800433{
Harald Welte15a36432012-06-17 12:16:31 +0800434 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200435 struct gprs_ra_id raid;
436 uint32_t tlli;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200437 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200438 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800439
Harald Welte25de8112010-05-13 21:26:28 +0200440 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200441 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
442 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200443 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200444 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200445 }
Harald Welte9ba50052010-03-14 15:45:01 +0800446
Harald Weltebfe62e52017-05-15 12:48:30 +0200447 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200448
Harald Welte17925322010-05-31 20:18:35 +0200449 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200450 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200451
Harald Weltea8aa4df2010-05-30 22:00:53 +0200452 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
453
Harald Welte313cccf2010-06-09 11:22:47 +0200454 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800455 memset(&gbp, 0, sizeof(gbp));
456 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200457 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800458 gbp.tlli = tlli;
459 gbp.ra_id = &raid;
460 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
461 PRIM_OP_REQUEST, msg);
462
463 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200464 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100465 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200466
Harald Weltea8aa4df2010-05-30 22:00:53 +0200467 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
468
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800469 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800470}
471
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200472static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800473{
Harald Welte15a36432012-06-17 12:16:31 +0800474 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200475 struct gprs_ra_id raid;
476 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200477 uint8_t suspend_ref;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200478 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200479 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800480
Harald Welte25de8112010-05-13 21:26:28 +0200481 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
482 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200483 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
484 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200485 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200486 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200487 }
Harald Welte9ba50052010-03-14 15:45:01 +0800488
Harald Weltebfe62e52017-05-15 12:48:30 +0200489 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200490 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200491
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200492 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200493
Harald Weltea8aa4df2010-05-30 22:00:53 +0200494 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
495
Harald Welte313cccf2010-06-09 11:22:47 +0200496 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800497 memset(&gbp, 0, sizeof(gbp));
498 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200499 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800500 gbp.tlli = tlli;
501 gbp.ra_id = &raid;
502 gbp.u.resume.suspend_ref = suspend_ref;
503 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
504 PRIM_OP_REQUEST, msg);
505
506 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200507 if (rc < 0)
508 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
509 NULL);
510
Harald Weltea8aa4df2010-05-30 22:00:53 +0200511 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800512 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800513}
514
Harald Weltee9686b62010-05-31 18:07:17 +0200515
516static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
517 struct bssgp_bvc_ctx *ctx)
518{
Harald Welte15a36432012-06-17 12:16:31 +0800519 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200520 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200521
522 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
523 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
524 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
525 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
526 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
527 "missing mandatory IE\n", ctx->bvci);
528 }
529
Harald Welteb7363142010-07-23 21:59:29 +0200530 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200531 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200532
Harald Welte086fe322011-08-19 16:45:19 +0200533 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200534 ctx->bvci, tlli);
535
536 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
537
Harald Welte15a36432012-06-17 12:16:31 +0800538 /* send NM_LLC_DISCARDED to NM */
539 memset(&nmp, 0, sizeof(nmp));
540 nmp.nsei = msgb_nsei(msg);
541 nmp.bvci = ctx->bvci;
542 nmp.tlli = tlli;
543 nmp.tp = tp;
544 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
545 PRIM_OP_INDICATION, msg);
546
547 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200548}
549
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100550int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
551 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
552{
553 struct osmo_bssgp_prim nmp;
554 enum gprs_bssgp_cause cause;
555
556 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
557 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
558 "missing mandatory IE\n", bvci);
559 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
560 } else {
561 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
562 }
563
564 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
565 bvci, bssgp_cause_str(cause));
566
567 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
568 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
569 LOGP(DBSSGP, LOGL_ERROR,
570 "BSSGP BVCI=%u Rx STATUS cause=%s "
571 "missing conditional BVCI IE\n",
572 bvci, bssgp_cause_str(cause));
573 }
574
575 if (bctx)
576 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
577
578 /* send NM_STATUS to NM */
579 memset(&nmp, 0, sizeof(nmp));
580 nmp.nsei = msgb_nsei(msg);
581 nmp.bvci = bvci;
582 nmp.tp = tp;
583 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
584 PRIM_OP_INDICATION, msg);
585
586 return bssgp_prim_cb(&nmp.oph, NULL);
587}
588
589
Harald Welted11c0592012-09-06 21:57:11 +0200590/* One element (msgb) in a BSSGP Flow Control queue */
591struct bssgp_fc_queue_element {
592 /* linked list of queue elements */
593 struct llist_head list;
594 /* The message that we have enqueued */
595 struct msgb *msg;
596 /* Length of the LLC PDU part of the contained message */
597 uint32_t llc_pdu_len;
598 /* private pointer passed to the flow control out_cb function */
599 void *priv;
600};
601
602static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
603static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
604
605static void fc_timer_cb(void *data)
606{
607 struct bssgp_flow_control *fc = data;
608 struct bssgp_fc_queue_element *fcqe;
609 struct timeval time_now;
610
611 /* if the queue is empty, we return without sending something
612 * and without re-starting the timer */
613 if (llist_empty(&fc->queue))
614 return;
615
616 /* get the first entry from the queue */
617 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
618 list);
619
620 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
621 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
622 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
623 /* make sure we re-start the timer */
624 fc_queue_timer_cfg(fc);
625 return;
626 }
627
628 /* remove from the queue */
629 llist_del(&fcqe->list);
630
631 fc->queue_depth--;
632
633 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200634 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200635 fc->time_last_pdu = time_now;
636
637 /* call the output callback for this FC instance */
638 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
639
640 /* we expect that out_cb will in the end free the msgb once
641 * it is no longer needed */
642
643 /* but we have to free the queue element ourselves */
644 talloc_free(fcqe);
645
646 /* re-configure the timer for the next PDU */
647 fc_queue_timer_cfg(fc);
648}
649
650/* configure/schedule the flow control timer to expire once the bucket
651 * will have leaked a sufficient number of bytes to transmit the next
652 * PDU in the queue */
653static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
654{
655 struct bssgp_fc_queue_element *fcqe;
656 uint32_t msecs;
657
658 if (llist_empty(&fc->queue))
659 return 0;
660
Jacob Erlbeck97319352015-04-30 19:28:03 +0200661 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200662 list);
663
Harald Welte27b2bb72013-06-22 09:44:00 +0200664 if (fc->bucket_leak_rate != 0) {
665 /* Calculate the point in time at which we will have leaked
666 * a sufficient number of bytes from the bucket to transmit
667 * the first PDU in the queue */
668 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
669 /* FIXME: add that time to fc->time_last_pdu and subtract it from
670 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200671 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200672 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
673 } else {
674 /* If the PCU is telling us to not send any more data at all,
675 * there's no point starting a timer. */
676 }
Harald Welted11c0592012-09-06 21:57:11 +0200677
678 return 0;
679}
680
681/* Enqueue a PDU in the flow control queue for delayed transmission */
682static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
683 uint32_t llc_pdu_len, void *priv)
684{
685 struct bssgp_fc_queue_element *fcqe;
686
687 if (fc->queue_depth >= fc->max_queue_depth)
688 return -ENOSPC;
689
690 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
691 if (!fcqe)
692 return -ENOMEM;
693 fcqe->msg = msg;
694 fcqe->llc_pdu_len = llc_pdu_len;
695 fcqe->priv = priv;
696
697 llist_add_tail(&fcqe->list, &fc->queue);
698
699 fc->queue_depth++;
700
701 /* re-configure the timer for dequeueing the pdu */
702 fc_queue_timer_cfg(fc);
703
704 return 0;
705}
706
707/* According to Section 8.2 */
708static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
709{
710 struct timeval time_now, time_diff;
711 int64_t bucket_predicted;
712 uint32_t csecs_elapsed, leaked;
713
714 /* B' = B + L(p) - (Tc - Tp)*R */
715
716 /* compute number of centi-seconds that have elapsed since transmitting
717 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200718 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200719 timersub(&time_now, &fc->time_last_pdu, &time_diff);
720 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
721
722 /* compute number of bytes that have leaked in the elapsed number
723 * of centi-seconds */
724 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
725 /* add the current PDU length to the last bucket level */
726 bucket_predicted = fc->bucket_counter + pdu_len;
727 /* ... and subtract the number of leaked bytes */
728 bucket_predicted -= leaked;
729
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700730 if (bucket_predicted < pdu_len)
731 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200732
733 if (bucket_predicted <= fc->bucket_size_max) {
734 /* the bucket is not full yet, we can pass the packet */
735 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700736 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200737 }
738
739 /* bucket is full, PDU needs to be delayed */
740 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200741}
742
743/* output callback for BVC flow control */
744static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
745 uint32_t llc_pdu_len, void *priv)
746{
747 return gprs_ns_sendmsg(bssgp_nsi, msg);
748}
749
750/* input function of the flow control implementation, called first
751 * for the MM flow control, and then as the MM flow control output
752 * callback in order to perform BVC flow control */
753int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
754 uint32_t llc_pdu_len, void *priv)
755{
756 struct timeval time_now;
757
Harald Weltebb826222012-09-07 10:22:01 +0200758 if (llc_pdu_len > fc->bucket_size_max) {
759 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
760 "than maximum bucket size (%u)!\n", llc_pdu_len,
761 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200762 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200763 return -EIO;
764 }
765
Harald Welted11c0592012-09-06 21:57:11 +0200766 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
Neels Hofmeyrcd325ef2017-11-16 22:32:36 +0100767 int rc;
768 rc = fc_enqueue(fc, msg, llc_pdu_len, priv);
769 if (rc)
770 msgb_free(msg);
771 return rc;
Harald Welted11c0592012-09-06 21:57:11 +0200772 } else {
773 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200774 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200775 fc->time_last_pdu = time_now;
776 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
777 }
778}
779
Harald Weltebb826222012-09-07 10:22:01 +0200780
781/* Initialize the Flow Control structure */
782void bssgp_fc_init(struct bssgp_flow_control *fc,
783 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
784 uint32_t max_queue_depth,
785 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
786 uint32_t llc_pdu_len, void *priv))
787{
788 fc->out_cb = out_cb;
789 fc->bucket_size_max = bucket_size_max;
790 fc->bucket_leak_rate = bucket_leak_rate;
791 fc->max_queue_depth = max_queue_depth;
792 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200793 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200794}
795
Harald Welted11c0592012-09-06 21:57:11 +0200796/* Initialize the Flow Control parameters for a new MS according to
797 * default values for the BVC specified by BVCI and NSEI */
798int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200799 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200800{
801 struct bssgp_bvc_ctx *ctx;
802
803 ctx = btsctx_by_bvci_nsei(bvci, nsei);
804 if (!ctx)
805 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200806
807 /* output call-back of per-MS FC is per-CTX FC */
808 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
809 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200810
811 return 0;
812}
813
Harald Welte25de8112010-05-13 21:26:28 +0200814static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200815 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800816{
Harald Welte27b2bb72013-06-22 09:44:00 +0200817 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
818 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800819
Harald Weltee9686b62010-05-31 18:07:17 +0200820 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
821 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800822
823 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
824 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
825 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
826 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200827 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
828 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
829 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800830 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200831 }
Harald Welte9ba50052010-03-14 15:45:01 +0800832
Harald Weltebb826222012-09-07 10:22:01 +0200833 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200834 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200835 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200836 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200837 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200838 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200839 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200840 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200841
Harald Welte27b2bb72013-06-22 09:44:00 +0200842 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
843 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
844 "rate of 0, stopping all DL GPRS!\n");
845 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
846 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
847 "rate of != 0, restarting all DL GPRS!\n");
848
849 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
850 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
851 "bucket leak rate of 0, stopping DL GPRS!\n");
852 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
853 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
854 "bucket leak rate != 0, restarting DL GPRS!\n");
855
856 /* reconfigure the timer for flow control based on new values */
857 fc_queue_timer_cfg(bctx->fc);
858
Harald Welte9ba50052010-03-14 15:45:01 +0800859 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200860 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200861 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800862}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200863
Harald Welte25de8112010-05-13 21:26:28 +0200864/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800865static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
866 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800867{
Harald Welteec19c102010-05-02 09:50:42 +0200868 struct bssgp_normal_hdr *bgph =
869 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200870 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800871 int rc = 0;
872
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100873 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
874
Harald Welte58e65c92010-05-13 21:45:23 +0200875 /* If traffic is received on a BVC that is marked as blocked, the
876 * received PDU shall not be accepted and a STATUS PDU (Cause value:
877 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100878 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200879 uint16_t bvci = msgb_bvci(msg);
880 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
881 }
882
Harald Welte9ba50052010-03-14 15:45:01 +0800883 switch (pdu_type) {
884 case BSSGP_PDUT_UL_UNITDATA:
885 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200886 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800887 break;
888 case BSSGP_PDUT_RA_CAPABILITY:
889 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200890 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
891 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200892 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800893 /* FIXME: send RA_CAPA_UPDATE_ACK */
894 break;
895 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200896 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800897 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200898 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800899 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800900 case BSSGP_PDUT_FLOW_CONTROL_BVC:
901 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200902 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800903 break;
904 case BSSGP_PDUT_FLOW_CONTROL_MS:
905 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200906 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
907 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200908 /* FIXME: actually implement flow control */
909 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800910 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800911 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100912 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100913 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800914 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
915 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
916 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
917 case BSSGP_PDUT_MODIFY_BSS_PFC:
918 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100919 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
920 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200921 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800922 break;
923 /* those only exist in the SGSN -> BSS direction */
924 case BSSGP_PDUT_DL_UNITDATA:
925 case BSSGP_PDUT_PAGING_PS:
926 case BSSGP_PDUT_PAGING_CS:
927 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200928 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
929 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100930 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
931 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200932 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
933 rc = -EINVAL;
934 break;
935 default:
Max2c34ab42016-03-17 15:42:26 +0100936 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
937 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200938 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
939 break;
940 }
941
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800942 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200943}
944
945/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800946static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
947 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200948{
949 struct bssgp_normal_hdr *bgph =
950 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
951 uint8_t pdu_type = bgph->pdu_type;
952 int rc = 0;
953 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200954 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200955
956 switch (bgph->pdu_type) {
957 case BSSGP_PDUT_SUSPEND:
958 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200959 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200960 break;
961 case BSSGP_PDUT_RESUME:
962 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200963 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200964 break;
965 case BSSGP_PDUT_FLUSH_LL_ACK:
966 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200967 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200968 /* FIXME: send NM_FLUSH_LL.res to NM */
969 break;
970 case BSSGP_PDUT_LLC_DISCARD:
971 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200972 if (!bctx) {
973 LOGP(DBSSGP, LOGL_ERROR,
974 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
975 goto err_mand_ie;
976 }
Harald Weltee9686b62010-05-31 18:07:17 +0200977 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200978 break;
979 case BSSGP_PDUT_BVC_BLOCK:
980 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200981 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200982 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
983 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
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 Welte2677ea52010-05-31 17:16:36 +0200987 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200988 break;
989 case BSSGP_PDUT_BVC_UNBLOCK:
990 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200991 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
992 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
993 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200994 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200995 }
Harald Welte25de8112010-05-13 21:26:28 +0200996 rc = bssgp_rx_bvc_unblock(msg, tp);
997 break;
Max590c4022017-06-28 14:29:24 +0200998 case BSSGP_PDUT_BVC_RESET_ACK:
999 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
1000 break;
Harald Welte25de8112010-05-13 21:26:28 +02001001 case BSSGP_PDUT_BVC_RESET:
1002 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +02001003 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001004 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1005 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1006 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001007 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001008 }
Harald Welte25de8112010-05-13 21:26:28 +02001009 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1010 break;
1011 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001012 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001013 break;
1014 /* those only exist in the SGSN -> BSS direction */
1015 case BSSGP_PDUT_PAGING_PS:
1016 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001017 case BSSGP_PDUT_SUSPEND_ACK:
1018 case BSSGP_PDUT_SUSPEND_NACK:
1019 case BSSGP_PDUT_RESUME_ACK:
1020 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001021 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001022 case BSSGP_PDUT_BVC_BLOCK_ACK:
1023 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1024 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001025 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1026 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001027 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001028 rc = -EINVAL;
1029 break;
1030 default:
Max2c34ab42016-03-17 15:42:26 +01001031 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1032 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001033 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001034 break;
1035 }
1036
1037 return rc;
1038err_mand_ie:
1039 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1040}
1041
Harald Welte25de8112010-05-13 21:26:28 +02001042/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001043int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001044{
1045 struct bssgp_normal_hdr *bgph =
1046 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1047 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1048 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001049 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001050 uint8_t pdu_type = bgph->pdu_type;
1051 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001052 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001053 int data_len;
1054 int rc = 0;
1055
1056 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1057
1058 /* UNITDATA BSSGP headers have TLLI in front */
1059 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1060 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1061 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1062 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1063 } else {
1064 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1065 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1066 }
1067
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001068 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001069 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001070
Harald Welte25de8112010-05-13 21:26:28 +02001071 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001072 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Harald Welte25de8112010-05-13 21:26:28 +02001073
Harald Welte16c8dbb2010-05-17 23:30:01 +02001074 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001075 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001076 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1077 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1078 msgb_bssgp_len(msg));
1079 }
1080
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001081 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1082 * are otherwise unexpected */
1083 if (pdu_type == BSSGP_PDUT_STATUS)
1084 /* Some exception has occurred */
1085 return bssgp_rx_status(msg, &tp, bvci, bctx);
1086
1087 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1088 * registered if a BVCI is given. */
1089 if (!bctx && bvci != BVCI_SIGNALLING &&
1090 pdu_type != BSSGP_PDUT_BVC_RESET) {
1091 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
Max2c34ab42016-03-17 15:42:26 +01001092 "type %s for unknown BVCI\n", msgb_nsei(msg), bvci,
1093 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001094 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1095 }
1096
Harald Welte61c07842010-05-18 11:57:08 +02001097 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001098 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001099 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001100 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001101 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001102 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001103 else
1104 LOGP(DBSSGP, LOGL_NOTICE,
Max2c34ab42016-03-17 15:42:26 +01001105 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for "
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001106 "unknown BVCI, NS BVCI %u\n",
Max2c34ab42016-03-17 15:42:26 +01001107 msgb_nsei(msg), bvci, bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001108
1109 return rc;
1110}
1111
Harald Weltede4599c2012-06-17 13:04:02 +08001112int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1113 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001114{
Harald Welte8a521132010-05-17 22:59:29 +02001115 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001116 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001117 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001118 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001119 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001120 uint16_t bvci = msgb_bvci(msg);
1121 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001122 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001123 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001124
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001125 OSMO_ASSERT(dup != NULL);
1126
Harald Welte30bc19a2010-05-02 11:19:37 +02001127 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001128 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001129 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001130 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001131 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001132 return -EINVAL;
1133 }
1134
1135 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001136 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001137 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1138 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001139 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001140 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001141 }
Harald Welte9ba50052010-03-14 15:45:01 +08001142
1143 if (msg->len > TVLV_MAX_ONEBYTE)
1144 llc_pdu_tlv_hdr_len += 1;
1145
1146 /* prepend the tag and length of the LLC-PDU TLV */
1147 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1148 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1149 if (llc_pdu_tlv_hdr_len > 2) {
1150 llc_pdu_tlv[1] = msg_len >> 8;
1151 llc_pdu_tlv[2] = msg_len & 0xff;
1152 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001153 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001154 llc_pdu_tlv[1] |= 0x80;
1155 }
1156
Harald Welte2f946832010-05-31 22:12:30 +02001157 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1158
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001159 /* Old TLLI to help BSS map from old->new */
1160 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001161 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001162 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001163 }
Harald Welte9ba50052010-03-14 15:45:01 +08001164
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001165 /* IMSI */
1166 if (dup->imsi && strlen(dup->imsi)) {
1167 uint8_t mi[10];
1168 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1169 if (imsi_len > 2)
1170 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1171 imsi_len-2, mi+2);
1172 }
1173
1174 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001175 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001176 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1177 (uint8_t *) &drx_params);
1178
1179 /* FIXME: Priority */
1180
1181 /* MS Radio Access Capability */
1182 if (dup->ms_ra_cap.len)
1183 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1184 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1185
Harald Welte9ba50052010-03-14 15:45:01 +08001186 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001187 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001188
1189 /* prepend the QoS profile, TLLI and pdu type */
1190 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001191 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001192 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001193 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1194
Harald Welte16c8dbb2010-05-17 23:30:01 +02001195 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1196 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1197
Harald Welte30bc19a2010-05-02 11:19:37 +02001198 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001199
Harald Welted11c0592012-09-06 21:57:11 +02001200 /* check if we have to go through per-ms flow control or can go
1201 * directly to the per-BSS flow control */
1202 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001203 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001204 else
Harald Welted8b47692012-09-07 11:29:32 +02001205 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001206}
Harald Welte68b4f032010-06-09 16:22:28 +02001207
1208/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001209int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1210 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001211{
1212 struct msgb *msg = bssgp_msgb_alloc();
1213 struct bssgp_normal_hdr *bgph =
1214 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001215 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Welte68b4f032010-06-09 16:22:28 +02001216 uint8_t mi[10];
1217 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1218 uint8_t ra[6];
1219
1220 if (imsi_len < 2)
1221 return -EINVAL;
1222
1223 msgb_nsei(msg) = nsei;
1224 msgb_bvci(msg) = ns_bvci;
1225
1226 if (pinfo->mode == BSSGP_PAGING_PS)
1227 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1228 else
1229 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1230 /* IMSI */
1231 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1232 /* DRX Parameters */
1233 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1234 (uint8_t *) &drx_params);
1235 /* Scope */
1236 switch (pinfo->scope) {
1237 case BSSGP_PAGING_BSS_AREA:
1238 {
1239 uint8_t null = 0;
1240 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1241 }
1242 break;
1243 case BSSGP_PAGING_LOCATION_AREA:
1244 gsm48_construct_ra(ra, &pinfo->raid);
1245 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1246 break;
1247 case BSSGP_PAGING_ROUTEING_AREA:
1248 gsm48_construct_ra(ra, &pinfo->raid);
1249 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1250 break;
1251 case BSSGP_PAGING_BVCI:
1252 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001253 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001254 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1255 }
1256 break;
1257 }
1258 /* QoS profile mandatory for PS */
1259 if (pinfo->mode == BSSGP_PAGING_PS)
1260 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1261
1262 /* Optional (P-)TMSI */
1263 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001264 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001265 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1266 }
1267
1268 return gprs_ns_sendmsg(bssgp_nsi, msg);
1269}
Harald Weltecca49632012-06-16 17:45:59 +08001270
Harald Weltede4599c2012-06-17 13:04:02 +08001271void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001272{
1273 DBSSGP = ss;
1274}