blob: b906643295b00e10b926405d76c1d1b2924a0d39 [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 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010016 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080017 *
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080020 *
Harald Welte4e5721d2010-05-17 23:41:43 +020021 * TODO:
22 * o properly count incoming BVC-RESET packets in counter group
23 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080024 */
25
26#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020027#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080028
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010029#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020030#include <osmocom/core/byteswap.h>
31#include <osmocom/core/bit16gen.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010032#include <osmocom/gsm/tlv.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010035#include <osmocom/core/stats.h>
Harald Welte6752fa42010-05-02 09:23:16 +020036
Harald Welte73952e32012-06-16 14:59:56 +080037#include <osmocom/gprs/gprs_bssgp.h>
Max8b8938f2017-06-29 19:48:29 +020038#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte73952e32012-06-16 14:59:56 +080039#include <osmocom/gprs/gprs_ns.h>
40
Harald Weltecca49632012-06-16 17:45:59 +080041#include "common_vty.h"
42
Harald Welte6752fa42010-05-02 09:23:16 +020043void *bssgp_tall_ctx = NULL;
44
Harald Welte25de8112010-05-13 21:26:28 +020045static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Weltea7a50652017-10-03 17:49:21 +080046 { "packets:in", "Packets at BSSGP Level ( In)" },
47 { "packets:out","Packets at BSSGP Level (Out)" },
48 { "bytes:in", "Bytes at BSSGP Level ( In)" },
49 { "bytes:out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020050 { "blocked", "BVC Blocking count" },
51 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010052 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020053};
54
55static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
Harald Weltea7a50652017-10-03 17:49:21 +080056 .group_name_prefix = "bssgp:bss_ctx",
Harald Welte25de8112010-05-13 21:26:28 +020057 .group_description = "BSSGP Peer Statistics",
58 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
59 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010060 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020061};
62
Harald Weltea78b9c22010-05-17 23:02:42 +020063LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020064
Harald Welted11c0592012-09-06 21:57:11 +020065static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
66 uint32_t llc_pdu_len, void *priv);
67
Harald Welte6752fa42010-05-02 09:23:16 +020068/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020069struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020070{
Harald Welte8a521132010-05-17 22:59:29 +020071 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020072
Harald Weltea78b9c22010-05-17 23:02:42 +020073 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020074 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
75 bctx->cell_id == cid)
76 return bctx;
77 }
78 return NULL;
79}
80
Max8b8938f2017-06-29 19:48:29 +020081/*! Initiate reset procedure for all PTP BVC on a given NSEI.
82 *
83 * This function initiates reset procedure for all PTP BVC with a given cause.
84 * \param[in] nsei NSEI to which PTP BVC should belong to
85 * \param[in] cause Cause of BVC RESET
86 * \returns 0 on success, negative error code otherwise
87 */
88int bssgp_tx_bvc_ptp_reset(uint16_t nsei, enum gprs_bssgp_cause cause)
89{
90 int rc;
91 struct bssgp_bvc_ctx *bctx;
92
93 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
94 if (bctx->nsei == nsei && bctx->bvci != BVCI_SIGNALLING) {
95 LOGP(DBSSGP, LOGL_DEBUG, "NSEI=%u/BVCI=%u RESET due to %s\n",
96 nsei, bctx->bvci, bssgp_cause_str(cause));
97 rc = bssgp_tx_bvc_reset(bctx, bctx->bvci, cause);
98 if (rc < 0)
99 return rc;
100 }
101 }
102
103 return 0;
104}
105
Harald Welte6752fa42010-05-02 09:23:16 +0200106/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200107struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200108{
Harald Welte8a521132010-05-17 22:59:29 +0200109 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200110
Harald Weltea78b9c22010-05-17 23:02:42 +0200111 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +0200112 if (bctx->nsei == nsei && bctx->bvci == bvci)
113 return bctx;
114 }
115 return NULL;
116}
117
Harald Welte8a521132010-05-17 22:59:29 +0200118struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200119{
Harald Welte8a521132010-05-17 22:59:29 +0200120 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200121
Harald Welte8a521132010-05-17 22:59:29 +0200122 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200123 if (!ctx)
124 return NULL;
125 ctx->bvci = bvci;
126 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200127 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
128 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Harald Welted8b47692012-09-07 11:29:32 +0200129 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
130 /* cofigure for 2Mbit, 30 packets in queue */
131 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200132
Harald Weltea78b9c22010-05-17 23:02:42 +0200133 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200134
135 return ctx;
136}
137
Harald Welte9ba50052010-03-14 15:45:01 +0800138/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200139static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800140{
141 struct msgb *msg = bssgp_msgb_alloc();
142 struct bssgp_normal_hdr *bgph =
143 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
144
Harald Welte24a655f2010-04-30 19:54:29 +0200145 msgb_nsei(msg) = nsei;
146 msgb_bvci(msg) = ns_bvci;
147
Harald Welte9ba50052010-03-14 15:45:01 +0800148 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
149 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
150
Harald Welte24a655f2010-04-30 19:54:29 +0200151 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800152}
153
Harald Weltea8aa4df2010-05-30 22:00:53 +0200154/* 10.3.7 SUSPEND-ACK PDU */
155int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
156 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
157{
158 struct msgb *msg = bssgp_msgb_alloc();
159 struct bssgp_normal_hdr *bgph =
160 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
161 uint32_t _tlli;
162 uint8_t ra[6];
163
164 msgb_nsei(msg) = nsei;
165 msgb_bvci(msg) = 0; /* Signalling */
166 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
167
Harald Weltebfe62e52017-05-15 12:48:30 +0200168 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200169 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
170 gsm48_construct_ra(ra, ra_id);
171 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
172 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
173
174 return gprs_ns_sendmsg(bssgp_nsi, msg);
175}
176
177/* 10.3.8 SUSPEND-NACK PDU */
178int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100179 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200180 uint8_t *cause)
181{
182 struct msgb *msg = bssgp_msgb_alloc();
183 struct bssgp_normal_hdr *bgph =
184 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
185 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100186 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200187
188 msgb_nsei(msg) = nsei;
189 msgb_bvci(msg) = 0; /* Signalling */
190 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
191
Harald Weltebfe62e52017-05-15 12:48:30 +0200192 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200193 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100194 gsm48_construct_ra(ra, ra_id);
195 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200196 if (cause)
197 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
198
199 return gprs_ns_sendmsg(bssgp_nsi, msg);
200}
201
202/* 10.3.10 RESUME-ACK PDU */
203int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
204 const struct gprs_ra_id *ra_id)
205{
206 struct msgb *msg = bssgp_msgb_alloc();
207 struct bssgp_normal_hdr *bgph =
208 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
209 uint32_t _tlli;
210 uint8_t ra[6];
211
212 msgb_nsei(msg) = nsei;
213 msgb_bvci(msg) = 0; /* Signalling */
214 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
215
Harald Weltebfe62e52017-05-15 12:48:30 +0200216 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200217 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
218 gsm48_construct_ra(ra, ra_id);
219 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
220
221 return gprs_ns_sendmsg(bssgp_nsi, msg);
222}
223
224/* 10.3.11 RESUME-NACK PDU */
225int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
226 const struct gprs_ra_id *ra_id, uint8_t *cause)
227{
228 struct msgb *msg = bssgp_msgb_alloc();
229 struct bssgp_normal_hdr *bgph =
230 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
231 uint32_t _tlli;
232 uint8_t ra[6];
233
234 msgb_nsei(msg) = nsei;
235 msgb_bvci(msg) = 0; /* Signalling */
236 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
237
Harald Weltebfe62e52017-05-15 12:48:30 +0200238 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200239 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
240 gsm48_construct_ra(ra, ra_id);
241 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
242 if (cause)
243 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
244
245 return gprs_ns_sendmsg(bssgp_nsi, msg);
246}
247
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200248uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200249{
250 /* 6 octets RAC */
251 gsm48_parse_ra(raid, buf);
252 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200253 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200254}
255
Harald Welte28610072011-11-24 21:32:07 +0100256int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
257 uint16_t cid)
258{
Harald Welte28610072011-11-24 21:32:07 +0100259 /* 6 octets RAC */
260 gsm48_construct_ra(buf, raid);
261 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200262 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100263
264 return 8;
265}
266
Harald Welte3fddf3c2010-05-01 16:48:27 +0200267/* Chapter 8.4 BVC-Reset Procedure */
268static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
269 uint16_t ns_bvci)
270{
Harald Welte15a36432012-06-17 12:16:31 +0800271 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200272 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200273 uint16_t nsei = msgb_nsei(msg);
274 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200275
Harald Weltebfe62e52017-05-15 12:48:30 +0200276 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200277 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200278 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
279
Harald Welte6752fa42010-05-02 09:23:16 +0200280 /* look-up or create the BTS context for this BVC */
281 bctx = btsctx_by_bvci_nsei(bvci, nsei);
282 if (!bctx)
283 bctx = btsctx_alloc(bvci, nsei);
284
Harald Welte25de8112010-05-13 21:26:28 +0200285 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
286 bctx->state &= ~BVC_S_BLOCKED;
287
Harald Welte3fddf3c2010-05-01 16:48:27 +0200288 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
289 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200290 if (bvci != 0 && bvci != 1) {
291 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200292 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200293 "missing mandatory IE\n", bvci);
294 return -EINVAL;
295 }
296 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200297 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
298 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200299 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200300 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
301 bctx->ra_id.rac, bctx->cell_id, bvci);
302 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200303
Harald Welte15a36432012-06-17 12:16:31 +0800304 /* Send NM_BVC_RESET.ind to NM */
305 memset(&nmp, 0, sizeof(nmp));
306 nmp.nsei = nsei;
307 nmp.bvci = bvci;
308 nmp.tp = tp;
309 nmp.ra_id = &bctx->ra_id;
310 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
311 PRIM_OP_INDICATION, msg);
312 bssgp_prim_cb(&nmp.oph, NULL);
313
Harald Welte6752fa42010-05-02 09:23:16 +0200314 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200315 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
316 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200317 return 0;
318}
319
Harald Welte25de8112010-05-13 21:26:28 +0200320static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
321{
Harald Welte15a36432012-06-17 12:16:31 +0800322 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200323 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200324 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200325
Harald Weltebfe62e52017-05-15 12:48:30 +0200326 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200327 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200328 /* 8.3.2: Signalling BVC shall never be blocked */
329 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
330 "received block for signalling BVC!?!\n",
331 msgb_nsei(msg), msgb_bvci(msg));
332 return 0;
333 }
Harald Welte25de8112010-05-13 21:26:28 +0200334
Harald Welte086fe322011-08-19 16:45:19 +0200335 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200336
337 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
338 if (!ptp_ctx)
339 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
340
341 ptp_ctx->state |= BVC_S_BLOCKED;
342 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
343
Harald Welte15a36432012-06-17 12:16:31 +0800344 /* Send NM_BVC_BLOCK.ind to NM */
345 memset(&nmp, 0, sizeof(nmp));
346 nmp.nsei = msgb_nsei(msg);
347 nmp.bvci = bvci;
348 nmp.tp = tp;
349 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
350 PRIM_OP_INDICATION, msg);
351 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200352
353 /* We always acknowledge the BLOCKing */
354 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
355 bvci, msgb_bvci(msg));
356};
357
358static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
359{
Harald Welte15a36432012-06-17 12:16:31 +0800360 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200361 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200362 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200363
Harald Weltebfe62e52017-05-15 12:48:30 +0200364 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200365 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200366 /* 8.3.2: Signalling BVC shall never be blocked */
367 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
368 "received unblock for signalling BVC!?!\n",
369 msgb_nsei(msg), msgb_bvci(msg));
370 return 0;
371 }
Harald Welte25de8112010-05-13 21:26:28 +0200372
Harald Weltee9686b62010-05-31 18:07:17 +0200373 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200374
375 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
376 if (!ptp_ctx)
377 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
378
379 ptp_ctx->state &= ~BVC_S_BLOCKED;
380
Harald Welte15a36432012-06-17 12:16:31 +0800381 /* Send NM_BVC_UNBLOCK.ind to NM */
382 memset(&nmp, 0, sizeof(nmp));
383 nmp.nsei = msgb_nsei(msg);
384 nmp.bvci = bvci;
385 nmp.tp = tp;
386 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
387 PRIM_OP_INDICATION, msg);
388 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200389
390 /* We always acknowledge the unBLOCKing */
391 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
392 bvci, msgb_bvci(msg));
393};
394
Harald Welte9ba50052010-03-14 15:45:01 +0800395/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200396static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200397 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800398{
Harald Welte15a36432012-06-17 12:16:31 +0800399 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200400 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800401
Harald Welte6752fa42010-05-02 09:23:16 +0200402 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200403 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800404
Harald Welte086fe322011-08-19 16:45:19 +0200405 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200406
Harald Welte9ba50052010-03-14 15:45:01 +0800407 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200408 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200409 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
410 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
411 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200412 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200413 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200414
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200415 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800416 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
417 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800418
Harald Welte15a36432012-06-17 12:16:31 +0800419 /* Send BSSGP_UL_UD.ind to NM */
420 memset(&gbp, 0, sizeof(gbp));
421 gbp.nsei = ctx->nsei;
422 gbp.bvci = ctx->bvci;
423 gbp.tlli = msgb_tlli(msg);
424 gbp.tp = tp;
425 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
426 PRIM_OP_INDICATION, msg);
427 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800428}
429
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200430static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800431{
Harald Welte15a36432012-06-17 12:16:31 +0800432 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200433 struct gprs_ra_id raid;
434 uint32_t tlli;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200435 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200436 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800437
Harald Welte25de8112010-05-13 21:26:28 +0200438 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200439 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
440 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200441 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200442 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200443 }
Harald Welte9ba50052010-03-14 15:45:01 +0800444
Harald Weltebfe62e52017-05-15 12:48:30 +0200445 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200446
Harald Welte17925322010-05-31 20:18:35 +0200447 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200448 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200449
Harald Weltea8aa4df2010-05-30 22:00:53 +0200450 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
451
Harald Welte313cccf2010-06-09 11:22:47 +0200452 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800453 memset(&gbp, 0, sizeof(gbp));
454 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200455 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800456 gbp.tlli = tlli;
457 gbp.ra_id = &raid;
458 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
459 PRIM_OP_REQUEST, msg);
460
461 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200462 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100463 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200464
Harald Weltea8aa4df2010-05-30 22:00:53 +0200465 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
466
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800467 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800468}
469
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200470static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800471{
Harald Welte15a36432012-06-17 12:16:31 +0800472 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200473 struct gprs_ra_id raid;
474 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200475 uint8_t suspend_ref;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200476 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200477 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800478
Harald Welte25de8112010-05-13 21:26:28 +0200479 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
480 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200481 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
482 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200483 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200484 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200485 }
Harald Welte9ba50052010-03-14 15:45:01 +0800486
Harald Weltebfe62e52017-05-15 12:48:30 +0200487 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200488 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200489
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200490 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200491
Harald Weltea8aa4df2010-05-30 22:00:53 +0200492 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
493
Harald Welte313cccf2010-06-09 11:22:47 +0200494 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800495 memset(&gbp, 0, sizeof(gbp));
496 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200497 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800498 gbp.tlli = tlli;
499 gbp.ra_id = &raid;
500 gbp.u.resume.suspend_ref = suspend_ref;
501 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
502 PRIM_OP_REQUEST, msg);
503
504 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200505 if (rc < 0)
506 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
507 NULL);
508
Harald Weltea8aa4df2010-05-30 22:00:53 +0200509 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800510 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800511}
512
Harald Weltee9686b62010-05-31 18:07:17 +0200513
514static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
515 struct bssgp_bvc_ctx *ctx)
516{
Harald Welte15a36432012-06-17 12:16:31 +0800517 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200518 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200519
520 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
521 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
522 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
523 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
524 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
525 "missing mandatory IE\n", ctx->bvci);
526 }
527
Harald Welteb7363142010-07-23 21:59:29 +0200528 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200529 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200530
Harald Welte086fe322011-08-19 16:45:19 +0200531 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200532 ctx->bvci, tlli);
533
534 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
535
Harald Welte15a36432012-06-17 12:16:31 +0800536 /* send NM_LLC_DISCARDED to NM */
537 memset(&nmp, 0, sizeof(nmp));
538 nmp.nsei = msgb_nsei(msg);
539 nmp.bvci = ctx->bvci;
540 nmp.tlli = tlli;
541 nmp.tp = tp;
542 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
543 PRIM_OP_INDICATION, msg);
544
545 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200546}
547
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100548int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
549 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
550{
551 struct osmo_bssgp_prim nmp;
552 enum gprs_bssgp_cause cause;
553
554 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
555 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
556 "missing mandatory IE\n", bvci);
557 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
558 } else {
559 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
560 }
561
562 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
563 bvci, bssgp_cause_str(cause));
564
565 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
566 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
567 LOGP(DBSSGP, LOGL_ERROR,
568 "BSSGP BVCI=%u Rx STATUS cause=%s "
569 "missing conditional BVCI IE\n",
570 bvci, bssgp_cause_str(cause));
571 }
572
573 if (bctx)
574 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
575
576 /* send NM_STATUS to NM */
577 memset(&nmp, 0, sizeof(nmp));
578 nmp.nsei = msgb_nsei(msg);
579 nmp.bvci = bvci;
580 nmp.tp = tp;
581 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
582 PRIM_OP_INDICATION, msg);
583
584 return bssgp_prim_cb(&nmp.oph, NULL);
585}
586
587
Harald Welted11c0592012-09-06 21:57:11 +0200588/* One element (msgb) in a BSSGP Flow Control queue */
589struct bssgp_fc_queue_element {
590 /* linked list of queue elements */
591 struct llist_head list;
592 /* The message that we have enqueued */
593 struct msgb *msg;
594 /* Length of the LLC PDU part of the contained message */
595 uint32_t llc_pdu_len;
596 /* private pointer passed to the flow control out_cb function */
597 void *priv;
598};
599
600static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
601static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
602
603static void fc_timer_cb(void *data)
604{
605 struct bssgp_flow_control *fc = data;
606 struct bssgp_fc_queue_element *fcqe;
607 struct timeval time_now;
608
609 /* if the queue is empty, we return without sending something
610 * and without re-starting the timer */
611 if (llist_empty(&fc->queue))
612 return;
613
614 /* get the first entry from the queue */
615 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
616 list);
617
618 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
619 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
620 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
621 /* make sure we re-start the timer */
622 fc_queue_timer_cfg(fc);
623 return;
624 }
625
626 /* remove from the queue */
627 llist_del(&fcqe->list);
628
629 fc->queue_depth--;
630
631 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200632 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200633 fc->time_last_pdu = time_now;
634
635 /* call the output callback for this FC instance */
636 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
637
638 /* we expect that out_cb will in the end free the msgb once
639 * it is no longer needed */
640
641 /* but we have to free the queue element ourselves */
642 talloc_free(fcqe);
643
644 /* re-configure the timer for the next PDU */
645 fc_queue_timer_cfg(fc);
646}
647
648/* configure/schedule the flow control timer to expire once the bucket
649 * will have leaked a sufficient number of bytes to transmit the next
650 * PDU in the queue */
651static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
652{
653 struct bssgp_fc_queue_element *fcqe;
654 uint32_t msecs;
655
656 if (llist_empty(&fc->queue))
657 return 0;
658
Jacob Erlbeck97319352015-04-30 19:28:03 +0200659 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200660 list);
661
Harald Welte27b2bb72013-06-22 09:44:00 +0200662 if (fc->bucket_leak_rate != 0) {
663 /* Calculate the point in time at which we will have leaked
664 * a sufficient number of bytes from the bucket to transmit
665 * the first PDU in the queue */
666 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
667 /* FIXME: add that time to fc->time_last_pdu and subtract it from
668 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200669 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200670 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
671 } else {
672 /* If the PCU is telling us to not send any more data at all,
673 * there's no point starting a timer. */
674 }
Harald Welted11c0592012-09-06 21:57:11 +0200675
676 return 0;
677}
678
679/* Enqueue a PDU in the flow control queue for delayed transmission */
680static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
681 uint32_t llc_pdu_len, void *priv)
682{
683 struct bssgp_fc_queue_element *fcqe;
684
685 if (fc->queue_depth >= fc->max_queue_depth)
686 return -ENOSPC;
687
688 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
689 if (!fcqe)
690 return -ENOMEM;
691 fcqe->msg = msg;
692 fcqe->llc_pdu_len = llc_pdu_len;
693 fcqe->priv = priv;
694
695 llist_add_tail(&fcqe->list, &fc->queue);
696
697 fc->queue_depth++;
698
699 /* re-configure the timer for dequeueing the pdu */
700 fc_queue_timer_cfg(fc);
701
702 return 0;
703}
704
705/* According to Section 8.2 */
706static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
707{
708 struct timeval time_now, time_diff;
709 int64_t bucket_predicted;
710 uint32_t csecs_elapsed, leaked;
711
712 /* B' = B + L(p) - (Tc - Tp)*R */
713
714 /* compute number of centi-seconds that have elapsed since transmitting
715 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200716 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200717 timersub(&time_now, &fc->time_last_pdu, &time_diff);
718 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
719
720 /* compute number of bytes that have leaked in the elapsed number
721 * of centi-seconds */
722 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
723 /* add the current PDU length to the last bucket level */
724 bucket_predicted = fc->bucket_counter + pdu_len;
725 /* ... and subtract the number of leaked bytes */
726 bucket_predicted -= leaked;
727
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700728 if (bucket_predicted < pdu_len)
729 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200730
731 if (bucket_predicted <= fc->bucket_size_max) {
732 /* the bucket is not full yet, we can pass the packet */
733 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700734 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200735 }
736
737 /* bucket is full, PDU needs to be delayed */
738 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200739}
740
741/* output callback for BVC flow control */
742static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
743 uint32_t llc_pdu_len, void *priv)
744{
745 return gprs_ns_sendmsg(bssgp_nsi, msg);
746}
747
748/* input function of the flow control implementation, called first
749 * for the MM flow control, and then as the MM flow control output
750 * callback in order to perform BVC flow control */
751int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
752 uint32_t llc_pdu_len, void *priv)
753{
754 struct timeval time_now;
755
Harald Weltebb826222012-09-07 10:22:01 +0200756 if (llc_pdu_len > fc->bucket_size_max) {
757 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
758 "than maximum bucket size (%u)!\n", llc_pdu_len,
759 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200760 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200761 return -EIO;
762 }
763
Harald Welted11c0592012-09-06 21:57:11 +0200764 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
765 return fc_enqueue(fc, msg, llc_pdu_len, priv);
766 } else {
767 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200768 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200769 fc->time_last_pdu = time_now;
770 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
771 }
772}
773
Harald Weltebb826222012-09-07 10:22:01 +0200774
775/* Initialize the Flow Control structure */
776void bssgp_fc_init(struct bssgp_flow_control *fc,
777 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
778 uint32_t max_queue_depth,
779 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
780 uint32_t llc_pdu_len, void *priv))
781{
782 fc->out_cb = out_cb;
783 fc->bucket_size_max = bucket_size_max;
784 fc->bucket_leak_rate = bucket_leak_rate;
785 fc->max_queue_depth = max_queue_depth;
786 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200787 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200788}
789
Harald Welted11c0592012-09-06 21:57:11 +0200790/* Initialize the Flow Control parameters for a new MS according to
791 * default values for the BVC specified by BVCI and NSEI */
792int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200793 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200794{
795 struct bssgp_bvc_ctx *ctx;
796
797 ctx = btsctx_by_bvci_nsei(bvci, nsei);
798 if (!ctx)
799 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200800
801 /* output call-back of per-MS FC is per-CTX FC */
802 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
803 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200804
805 return 0;
806}
807
Harald Welte25de8112010-05-13 21:26:28 +0200808static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200809 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800810{
Harald Welte27b2bb72013-06-22 09:44:00 +0200811 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
812 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800813
Harald Weltee9686b62010-05-31 18:07:17 +0200814 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
815 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800816
817 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
818 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
819 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
820 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200821 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
822 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
823 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800824 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200825 }
Harald Welte9ba50052010-03-14 15:45:01 +0800826
Harald Weltebb826222012-09-07 10:22:01 +0200827 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200828 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200829 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200830 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200831 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200832 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200833 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200834 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200835
Harald Welte27b2bb72013-06-22 09:44:00 +0200836 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
837 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
838 "rate of 0, stopping all DL GPRS!\n");
839 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
840 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
841 "rate of != 0, restarting all DL GPRS!\n");
842
843 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
844 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
845 "bucket leak rate of 0, stopping DL GPRS!\n");
846 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
847 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
848 "bucket leak rate != 0, restarting DL GPRS!\n");
849
850 /* reconfigure the timer for flow control based on new values */
851 fc_queue_timer_cfg(bctx->fc);
852
Harald Welte9ba50052010-03-14 15:45:01 +0800853 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200854 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200855 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800856}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200857
Harald Welte25de8112010-05-13 21:26:28 +0200858/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800859static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
860 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800861{
Harald Welteec19c102010-05-02 09:50:42 +0200862 struct bssgp_normal_hdr *bgph =
863 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200864 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800865 int rc = 0;
866
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100867 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
868
Harald Welte58e65c92010-05-13 21:45:23 +0200869 /* If traffic is received on a BVC that is marked as blocked, the
870 * received PDU shall not be accepted and a STATUS PDU (Cause value:
871 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100872 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200873 uint16_t bvci = msgb_bvci(msg);
874 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
875 }
876
Harald Welte9ba50052010-03-14 15:45:01 +0800877 switch (pdu_type) {
878 case BSSGP_PDUT_UL_UNITDATA:
879 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200880 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800881 break;
882 case BSSGP_PDUT_RA_CAPABILITY:
883 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200884 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
885 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200886 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800887 /* FIXME: send RA_CAPA_UPDATE_ACK */
888 break;
889 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200890 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800891 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200892 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800893 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800894 case BSSGP_PDUT_FLOW_CONTROL_BVC:
895 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200896 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800897 break;
898 case BSSGP_PDUT_FLOW_CONTROL_MS:
899 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200900 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
901 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200902 /* FIXME: actually implement flow control */
903 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800904 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800905 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100906 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100907 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800908 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
909 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
910 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
911 case BSSGP_PDUT_MODIFY_BSS_PFC:
912 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100913 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
914 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200915 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800916 break;
917 /* those only exist in the SGSN -> BSS direction */
918 case BSSGP_PDUT_DL_UNITDATA:
919 case BSSGP_PDUT_PAGING_PS:
920 case BSSGP_PDUT_PAGING_CS:
921 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200922 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
923 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100924 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
925 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200926 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
927 rc = -EINVAL;
928 break;
929 default:
Max2c34ab42016-03-17 15:42:26 +0100930 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
931 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200932 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
933 break;
934 }
935
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800936 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200937}
938
939/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800940static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
941 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200942{
943 struct bssgp_normal_hdr *bgph =
944 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
945 uint8_t pdu_type = bgph->pdu_type;
946 int rc = 0;
947 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200948 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200949
950 switch (bgph->pdu_type) {
951 case BSSGP_PDUT_SUSPEND:
952 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200953 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200954 break;
955 case BSSGP_PDUT_RESUME:
956 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200957 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200958 break;
959 case BSSGP_PDUT_FLUSH_LL_ACK:
960 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200961 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200962 /* FIXME: send NM_FLUSH_LL.res to NM */
963 break;
964 case BSSGP_PDUT_LLC_DISCARD:
965 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200966 if (!bctx) {
967 LOGP(DBSSGP, LOGL_ERROR,
968 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
969 goto err_mand_ie;
970 }
Harald Weltee9686b62010-05-31 18:07:17 +0200971 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200972 break;
973 case BSSGP_PDUT_BVC_BLOCK:
974 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200975 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200976 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
977 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
978 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200979 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200980 }
Harald Welte2677ea52010-05-31 17:16:36 +0200981 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200982 break;
983 case BSSGP_PDUT_BVC_UNBLOCK:
984 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200985 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
986 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
987 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200988 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200989 }
Harald Welte25de8112010-05-13 21:26:28 +0200990 rc = bssgp_rx_bvc_unblock(msg, tp);
991 break;
Max590c4022017-06-28 14:29:24 +0200992 case BSSGP_PDUT_BVC_RESET_ACK:
993 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
994 break;
Harald Welte25de8112010-05-13 21:26:28 +0200995 case BSSGP_PDUT_BVC_RESET:
996 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200997 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200998 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
999 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
1000 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +02001001 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +02001002 }
Harald Welte25de8112010-05-13 21:26:28 +02001003 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
1004 break;
1005 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001006 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +02001007 break;
1008 /* those only exist in the SGSN -> BSS direction */
1009 case BSSGP_PDUT_PAGING_PS:
1010 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +08001011 case BSSGP_PDUT_SUSPEND_ACK:
1012 case BSSGP_PDUT_SUSPEND_NACK:
1013 case BSSGP_PDUT_RESUME_ACK:
1014 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +02001015 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +08001016 case BSSGP_PDUT_BVC_BLOCK_ACK:
1017 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
1018 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +01001019 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
1020 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001021 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001022 rc = -EINVAL;
1023 break;
1024 default:
Max2c34ab42016-03-17 15:42:26 +01001025 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1026 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001027 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001028 break;
1029 }
1030
1031 return rc;
1032err_mand_ie:
1033 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1034}
1035
Harald Welte25de8112010-05-13 21:26:28 +02001036/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001037int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001038{
1039 struct bssgp_normal_hdr *bgph =
1040 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1041 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1042 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001043 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001044 uint8_t pdu_type = bgph->pdu_type;
1045 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001046 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001047 int data_len;
1048 int rc = 0;
1049
1050 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1051
1052 /* UNITDATA BSSGP headers have TLLI in front */
1053 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1054 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1055 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1056 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1057 } else {
1058 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1059 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1060 }
1061
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001062 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001063 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001064
Harald Welte25de8112010-05-13 21:26:28 +02001065 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001066 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Harald Welte25de8112010-05-13 21:26:28 +02001067
Harald Welte16c8dbb2010-05-17 23:30:01 +02001068 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001069 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001070 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1071 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1072 msgb_bssgp_len(msg));
1073 }
1074
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001075 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1076 * are otherwise unexpected */
1077 if (pdu_type == BSSGP_PDUT_STATUS)
1078 /* Some exception has occurred */
1079 return bssgp_rx_status(msg, &tp, bvci, bctx);
1080
1081 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1082 * registered if a BVCI is given. */
1083 if (!bctx && bvci != BVCI_SIGNALLING &&
1084 pdu_type != BSSGP_PDUT_BVC_RESET) {
1085 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
Max2c34ab42016-03-17 15:42:26 +01001086 "type %s for unknown BVCI\n", msgb_nsei(msg), bvci,
1087 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001088 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1089 }
1090
Harald Welte61c07842010-05-18 11:57:08 +02001091 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001092 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001093 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001094 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001095 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001096 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001097 else
1098 LOGP(DBSSGP, LOGL_NOTICE,
Max2c34ab42016-03-17 15:42:26 +01001099 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for "
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001100 "unknown BVCI, NS BVCI %u\n",
Max2c34ab42016-03-17 15:42:26 +01001101 msgb_nsei(msg), bvci, bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001102
1103 return rc;
1104}
1105
Harald Weltede4599c2012-06-17 13:04:02 +08001106int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1107 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001108{
Harald Welte8a521132010-05-17 22:59:29 +02001109 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001110 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001111 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001112 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001113 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001114 uint16_t bvci = msgb_bvci(msg);
1115 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001116 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001117 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001118
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001119 OSMO_ASSERT(dup != NULL);
1120
Harald Welte30bc19a2010-05-02 11:19:37 +02001121 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001122 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001123 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001124 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001125 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001126 return -EINVAL;
1127 }
1128
1129 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001130 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001131 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1132 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001133 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001134 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001135 }
Harald Welte9ba50052010-03-14 15:45:01 +08001136
1137 if (msg->len > TVLV_MAX_ONEBYTE)
1138 llc_pdu_tlv_hdr_len += 1;
1139
1140 /* prepend the tag and length of the LLC-PDU TLV */
1141 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1142 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1143 if (llc_pdu_tlv_hdr_len > 2) {
1144 llc_pdu_tlv[1] = msg_len >> 8;
1145 llc_pdu_tlv[2] = msg_len & 0xff;
1146 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001147 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001148 llc_pdu_tlv[1] |= 0x80;
1149 }
1150
Harald Welte2f946832010-05-31 22:12:30 +02001151 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1152
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001153 /* Old TLLI to help BSS map from old->new */
1154 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001155 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001156 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001157 }
Harald Welte9ba50052010-03-14 15:45:01 +08001158
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001159 /* IMSI */
1160 if (dup->imsi && strlen(dup->imsi)) {
1161 uint8_t mi[10];
1162 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1163 if (imsi_len > 2)
1164 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1165 imsi_len-2, mi+2);
1166 }
1167
1168 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001169 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001170 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1171 (uint8_t *) &drx_params);
1172
1173 /* FIXME: Priority */
1174
1175 /* MS Radio Access Capability */
1176 if (dup->ms_ra_cap.len)
1177 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1178 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1179
Harald Welte9ba50052010-03-14 15:45:01 +08001180 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001181 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001182
1183 /* prepend the QoS profile, TLLI and pdu type */
1184 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001185 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001186 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001187 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1188
Harald Welte16c8dbb2010-05-17 23:30:01 +02001189 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1190 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1191
Harald Welte30bc19a2010-05-02 11:19:37 +02001192 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001193
Harald Welted11c0592012-09-06 21:57:11 +02001194 /* check if we have to go through per-ms flow control or can go
1195 * directly to the per-BSS flow control */
1196 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001197 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001198 else
Harald Welted8b47692012-09-07 11:29:32 +02001199 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001200}
Harald Welte68b4f032010-06-09 16:22:28 +02001201
1202/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001203int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1204 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001205{
1206 struct msgb *msg = bssgp_msgb_alloc();
1207 struct bssgp_normal_hdr *bgph =
1208 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001209 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Welte68b4f032010-06-09 16:22:28 +02001210 uint8_t mi[10];
1211 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1212 uint8_t ra[6];
1213
1214 if (imsi_len < 2)
1215 return -EINVAL;
1216
1217 msgb_nsei(msg) = nsei;
1218 msgb_bvci(msg) = ns_bvci;
1219
1220 if (pinfo->mode == BSSGP_PAGING_PS)
1221 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1222 else
1223 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1224 /* IMSI */
1225 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1226 /* DRX Parameters */
1227 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1228 (uint8_t *) &drx_params);
1229 /* Scope */
1230 switch (pinfo->scope) {
1231 case BSSGP_PAGING_BSS_AREA:
1232 {
1233 uint8_t null = 0;
1234 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1235 }
1236 break;
1237 case BSSGP_PAGING_LOCATION_AREA:
1238 gsm48_construct_ra(ra, &pinfo->raid);
1239 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1240 break;
1241 case BSSGP_PAGING_ROUTEING_AREA:
1242 gsm48_construct_ra(ra, &pinfo->raid);
1243 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1244 break;
1245 case BSSGP_PAGING_BVCI:
1246 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001247 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001248 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1249 }
1250 break;
1251 }
1252 /* QoS profile mandatory for PS */
1253 if (pinfo->mode == BSSGP_PAGING_PS)
1254 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1255
1256 /* Optional (P-)TMSI */
1257 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001258 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001259 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1260 }
1261
1262 return gprs_ns_sendmsg(bssgp_nsi, msg);
1263}
Harald Weltecca49632012-06-16 17:45:59 +08001264
Harald Weltede4599c2012-06-17 13:04:02 +08001265void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001266{
1267 DBSSGP = ss;
1268}