blob: 520868e07edd7f2f73ed9cfdc1d2eb4dd6cfa923 [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)) {
767 return fc_enqueue(fc, msg, llc_pdu_len, priv);
768 } else {
769 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200770 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200771 fc->time_last_pdu = time_now;
772 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
773 }
774}
775
Harald Weltebb826222012-09-07 10:22:01 +0200776
777/* Initialize the Flow Control structure */
778void bssgp_fc_init(struct bssgp_flow_control *fc,
779 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
780 uint32_t max_queue_depth,
781 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
782 uint32_t llc_pdu_len, void *priv))
783{
784 fc->out_cb = out_cb;
785 fc->bucket_size_max = bucket_size_max;
786 fc->bucket_leak_rate = bucket_leak_rate;
787 fc->max_queue_depth = max_queue_depth;
788 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200789 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200790}
791
Harald Welted11c0592012-09-06 21:57:11 +0200792/* Initialize the Flow Control parameters for a new MS according to
793 * default values for the BVC specified by BVCI and NSEI */
794int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200795 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200796{
797 struct bssgp_bvc_ctx *ctx;
798
799 ctx = btsctx_by_bvci_nsei(bvci, nsei);
800 if (!ctx)
801 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200802
803 /* output call-back of per-MS FC is per-CTX FC */
804 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
805 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200806
807 return 0;
808}
809
Harald Welte25de8112010-05-13 21:26:28 +0200810static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200811 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800812{
Harald Welte27b2bb72013-06-22 09:44:00 +0200813 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
814 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800815
Harald Weltee9686b62010-05-31 18:07:17 +0200816 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
817 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800818
819 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
820 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
821 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
822 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200823 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
824 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
825 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800826 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200827 }
Harald Welte9ba50052010-03-14 15:45:01 +0800828
Harald Weltebb826222012-09-07 10:22:01 +0200829 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200830 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200831 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200832 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200833 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200834 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200835 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200836 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200837
Harald Welte27b2bb72013-06-22 09:44:00 +0200838 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
839 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
840 "rate of 0, stopping all DL GPRS!\n");
841 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
842 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
843 "rate of != 0, restarting all DL GPRS!\n");
844
845 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
846 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
847 "bucket leak rate of 0, stopping DL GPRS!\n");
848 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
849 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
850 "bucket leak rate != 0, restarting DL GPRS!\n");
851
852 /* reconfigure the timer for flow control based on new values */
853 fc_queue_timer_cfg(bctx->fc);
854
Harald Welte9ba50052010-03-14 15:45:01 +0800855 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200856 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200857 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800858}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200859
Harald Welte25de8112010-05-13 21:26:28 +0200860/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800861static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
862 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800863{
Harald Welteec19c102010-05-02 09:50:42 +0200864 struct bssgp_normal_hdr *bgph =
865 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200866 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800867 int rc = 0;
868
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100869 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
870
Harald Welte58e65c92010-05-13 21:45:23 +0200871 /* If traffic is received on a BVC that is marked as blocked, the
872 * received PDU shall not be accepted and a STATUS PDU (Cause value:
873 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100874 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200875 uint16_t bvci = msgb_bvci(msg);
876 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
877 }
878
Harald Welte9ba50052010-03-14 15:45:01 +0800879 switch (pdu_type) {
880 case BSSGP_PDUT_UL_UNITDATA:
881 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200882 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800883 break;
884 case BSSGP_PDUT_RA_CAPABILITY:
885 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200886 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
887 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200888 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800889 /* FIXME: send RA_CAPA_UPDATE_ACK */
890 break;
891 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200892 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800893 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200894 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800895 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800896 case BSSGP_PDUT_FLOW_CONTROL_BVC:
897 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200898 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800899 break;
900 case BSSGP_PDUT_FLOW_CONTROL_MS:
901 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200902 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
903 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200904 /* FIXME: actually implement flow control */
905 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800906 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800907 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100908 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100909 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800910 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
911 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
912 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
913 case BSSGP_PDUT_MODIFY_BSS_PFC:
914 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100915 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
916 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200917 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800918 break;
919 /* those only exist in the SGSN -> BSS direction */
920 case BSSGP_PDUT_DL_UNITDATA:
921 case BSSGP_PDUT_PAGING_PS:
922 case BSSGP_PDUT_PAGING_CS:
923 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200924 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
925 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100926 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
927 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200928 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
929 rc = -EINVAL;
930 break;
931 default:
Max2c34ab42016-03-17 15:42:26 +0100932 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
933 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200934 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
935 break;
936 }
937
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800938 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200939}
940
941/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800942static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
943 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200944{
945 struct bssgp_normal_hdr *bgph =
946 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
947 uint8_t pdu_type = bgph->pdu_type;
948 int rc = 0;
949 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200950 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200951
952 switch (bgph->pdu_type) {
953 case BSSGP_PDUT_SUSPEND:
954 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200955 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200956 break;
957 case BSSGP_PDUT_RESUME:
958 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200959 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200960 break;
961 case BSSGP_PDUT_FLUSH_LL_ACK:
962 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200963 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200964 /* FIXME: send NM_FLUSH_LL.res to NM */
965 break;
966 case BSSGP_PDUT_LLC_DISCARD:
967 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200968 if (!bctx) {
969 LOGP(DBSSGP, LOGL_ERROR,
970 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
971 goto err_mand_ie;
972 }
Harald Weltee9686b62010-05-31 18:07:17 +0200973 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200974 break;
975 case BSSGP_PDUT_BVC_BLOCK:
976 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200977 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200978 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
979 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
980 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200981 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200982 }
Harald Welte2677ea52010-05-31 17:16:36 +0200983 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200984 break;
985 case BSSGP_PDUT_BVC_UNBLOCK:
986 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200987 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
988 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
989 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200990 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200991 }
Harald Welte25de8112010-05-13 21:26:28 +0200992 rc = bssgp_rx_bvc_unblock(msg, tp);
993 break;
Max590c4022017-06-28 14:29:24 +0200994 case BSSGP_PDUT_BVC_RESET_ACK:
995 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
996 break;
Harald Welte25de8112010-05-13 21:26:28 +0200997 case BSSGP_PDUT_BVC_RESET:
998 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200999 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +02001000 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
1001 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1002 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001003 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001004 }
Harald Welte25de8112010-05-13 21:26:28 +02001005 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1006 break;
1007 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001008 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001009 break;
1010 /* those only exist in the SGSN -> BSS direction */
1011 case BSSGP_PDUT_PAGING_PS:
1012 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001013 case BSSGP_PDUT_SUSPEND_ACK:
1014 case BSSGP_PDUT_SUSPEND_NACK:
1015 case BSSGP_PDUT_RESUME_ACK:
1016 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001017 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001018 case BSSGP_PDUT_BVC_BLOCK_ACK:
1019 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1020 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001021 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1022 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001023 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001024 rc = -EINVAL;
1025 break;
1026 default:
Max2c34ab42016-03-17 15:42:26 +01001027 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1028 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001029 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001030 break;
1031 }
1032
1033 return rc;
1034err_mand_ie:
1035 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1036}
1037
Harald Welte25de8112010-05-13 21:26:28 +02001038/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001039int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001040{
1041 struct bssgp_normal_hdr *bgph =
1042 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1043 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1044 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001045 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001046 uint8_t pdu_type = bgph->pdu_type;
1047 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001048 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001049 int data_len;
1050 int rc = 0;
1051
1052 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1053
1054 /* UNITDATA BSSGP headers have TLLI in front */
1055 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1056 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1057 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1058 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1059 } else {
1060 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1061 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1062 }
1063
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001064 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001065 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001066
Harald Welte25de8112010-05-13 21:26:28 +02001067 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001068 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Harald Welte25de8112010-05-13 21:26:28 +02001069
Harald Welte16c8dbb2010-05-17 23:30:01 +02001070 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001071 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001072 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1073 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1074 msgb_bssgp_len(msg));
1075 }
1076
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001077 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1078 * are otherwise unexpected */
1079 if (pdu_type == BSSGP_PDUT_STATUS)
1080 /* Some exception has occurred */
1081 return bssgp_rx_status(msg, &tp, bvci, bctx);
1082
1083 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1084 * registered if a BVCI is given. */
1085 if (!bctx && bvci != BVCI_SIGNALLING &&
1086 pdu_type != BSSGP_PDUT_BVC_RESET) {
1087 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
Max2c34ab42016-03-17 15:42:26 +01001088 "type %s for unknown BVCI\n", msgb_nsei(msg), bvci,
1089 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001090 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1091 }
1092
Harald Welte61c07842010-05-18 11:57:08 +02001093 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001094 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001095 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001096 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001097 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001098 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001099 else
1100 LOGP(DBSSGP, LOGL_NOTICE,
Max2c34ab42016-03-17 15:42:26 +01001101 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for "
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001102 "unknown BVCI, NS BVCI %u\n",
Max2c34ab42016-03-17 15:42:26 +01001103 msgb_nsei(msg), bvci, bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001104
1105 return rc;
1106}
1107
Harald Weltede4599c2012-06-17 13:04:02 +08001108int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1109 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001110{
Harald Welte8a521132010-05-17 22:59:29 +02001111 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001112 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001113 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001114 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001115 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001116 uint16_t bvci = msgb_bvci(msg);
1117 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001118 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001119 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001120
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001121 OSMO_ASSERT(dup != NULL);
1122
Harald Welte30bc19a2010-05-02 11:19:37 +02001123 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001124 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001125 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001126 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001127 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001128 return -EINVAL;
1129 }
1130
1131 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001132 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001133 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1134 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001135 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001136 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001137 }
Harald Welte9ba50052010-03-14 15:45:01 +08001138
1139 if (msg->len > TVLV_MAX_ONEBYTE)
1140 llc_pdu_tlv_hdr_len += 1;
1141
1142 /* prepend the tag and length of the LLC-PDU TLV */
1143 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1144 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1145 if (llc_pdu_tlv_hdr_len > 2) {
1146 llc_pdu_tlv[1] = msg_len >> 8;
1147 llc_pdu_tlv[2] = msg_len & 0xff;
1148 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001149 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001150 llc_pdu_tlv[1] |= 0x80;
1151 }
1152
Harald Welte2f946832010-05-31 22:12:30 +02001153 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1154
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001155 /* Old TLLI to help BSS map from old->new */
1156 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001157 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001158 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001159 }
Harald Welte9ba50052010-03-14 15:45:01 +08001160
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001161 /* IMSI */
1162 if (dup->imsi && strlen(dup->imsi)) {
1163 uint8_t mi[10];
1164 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1165 if (imsi_len > 2)
1166 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1167 imsi_len-2, mi+2);
1168 }
1169
1170 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001171 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001172 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1173 (uint8_t *) &drx_params);
1174
1175 /* FIXME: Priority */
1176
1177 /* MS Radio Access Capability */
1178 if (dup->ms_ra_cap.len)
1179 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1180 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1181
Harald Welte9ba50052010-03-14 15:45:01 +08001182 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001183 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001184
1185 /* prepend the QoS profile, TLLI and pdu type */
1186 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001187 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001188 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001189 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1190
Harald Welte16c8dbb2010-05-17 23:30:01 +02001191 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1192 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1193
Harald Welte30bc19a2010-05-02 11:19:37 +02001194 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001195
Harald Welted11c0592012-09-06 21:57:11 +02001196 /* check if we have to go through per-ms flow control or can go
1197 * directly to the per-BSS flow control */
1198 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001199 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001200 else
Harald Welted8b47692012-09-07 11:29:32 +02001201 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001202}
Harald Welte68b4f032010-06-09 16:22:28 +02001203
1204/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001205int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1206 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001207{
1208 struct msgb *msg = bssgp_msgb_alloc();
1209 struct bssgp_normal_hdr *bgph =
1210 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001211 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Welte68b4f032010-06-09 16:22:28 +02001212 uint8_t mi[10];
1213 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1214 uint8_t ra[6];
1215
1216 if (imsi_len < 2)
1217 return -EINVAL;
1218
1219 msgb_nsei(msg) = nsei;
1220 msgb_bvci(msg) = ns_bvci;
1221
1222 if (pinfo->mode == BSSGP_PAGING_PS)
1223 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1224 else
1225 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1226 /* IMSI */
1227 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1228 /* DRX Parameters */
1229 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1230 (uint8_t *) &drx_params);
1231 /* Scope */
1232 switch (pinfo->scope) {
1233 case BSSGP_PAGING_BSS_AREA:
1234 {
1235 uint8_t null = 0;
1236 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1237 }
1238 break;
1239 case BSSGP_PAGING_LOCATION_AREA:
1240 gsm48_construct_ra(ra, &pinfo->raid);
1241 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1242 break;
1243 case BSSGP_PAGING_ROUTEING_AREA:
1244 gsm48_construct_ra(ra, &pinfo->raid);
1245 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1246 break;
1247 case BSSGP_PAGING_BVCI:
1248 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001249 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001250 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1251 }
1252 break;
1253 }
1254 /* QoS profile mandatory for PS */
1255 if (pinfo->mode == BSSGP_PAGING_PS)
1256 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1257
1258 /* Optional (P-)TMSI */
1259 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001260 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001261 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1262 }
1263
1264 return gprs_ns_sendmsg(bssgp_nsi, msg);
1265}
Harald Weltecca49632012-06-16 17:45:59 +08001266
Harald Weltede4599c2012-06-17 13:04:02 +08001267void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001268{
1269 DBSSGP = ss;
1270}