blob: 8daf39f0a3bd50b1cd77c2c5e7449431b7809131 [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>
38#include <osmocom/gprs/gprs_ns.h>
39
Harald Weltecca49632012-06-16 17:45:59 +080040#include "common_vty.h"
41
Harald Welte6752fa42010-05-02 09:23:16 +020042void *bssgp_tall_ctx = NULL;
43
Harald Welte25de8112010-05-13 21:26:28 +020044static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Welte16c8dbb2010-05-17 23:30:01 +020045 { "packets.in", "Packets at BSSGP Level ( In)" },
46 { "packets.out","Packets at BSSGP Level (Out)" },
47 { "bytes.in", "Bytes at BSSGP Level ( In)" },
48 { "bytes.out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020049 { "blocked", "BVC Blocking count" },
50 { "discarded", "BVC LLC Discarded count" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010051 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020052};
53
54static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
55 .group_name_prefix = "bssgp.bss_ctx",
56 .group_description = "BSSGP Peer Statistics",
57 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
58 .ctr_desc = bssgp_ctr_description,
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +010059 .class_id = OSMO_STATS_CLASS_PEER,
Harald Welte25de8112010-05-13 21:26:28 +020060};
61
Harald Weltea78b9c22010-05-17 23:02:42 +020062LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020063
Harald Welted11c0592012-09-06 21:57:11 +020064static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
65 uint32_t llc_pdu_len, void *priv);
66
Harald Welte6752fa42010-05-02 09:23:16 +020067/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020068struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020069{
Harald Welte8a521132010-05-17 22:59:29 +020070 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020071
Harald Weltea78b9c22010-05-17 23:02:42 +020072 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020073 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
74 bctx->cell_id == cid)
75 return bctx;
76 }
77 return NULL;
78}
79
80/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +020081struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020082{
Harald Welte8a521132010-05-17 22:59:29 +020083 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020084
Harald Weltea78b9c22010-05-17 23:02:42 +020085 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020086 if (bctx->nsei == nsei && bctx->bvci == bvci)
87 return bctx;
88 }
89 return NULL;
90}
91
Harald Welte8a521132010-05-17 22:59:29 +020092struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020093{
Harald Welte8a521132010-05-17 22:59:29 +020094 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +020095
Harald Welte8a521132010-05-17 22:59:29 +020096 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +020097 if (!ctx)
98 return NULL;
99 ctx->bvci = bvci;
100 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200101 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
102 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Harald Welted8b47692012-09-07 11:29:32 +0200103 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
104 /* cofigure for 2Mbit, 30 packets in queue */
105 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200106
Harald Weltea78b9c22010-05-17 23:02:42 +0200107 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200108
109 return ctx;
110}
111
Harald Welte9ba50052010-03-14 15:45:01 +0800112/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200113static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800114{
115 struct msgb *msg = bssgp_msgb_alloc();
116 struct bssgp_normal_hdr *bgph =
117 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
118
Harald Welte24a655f2010-04-30 19:54:29 +0200119 msgb_nsei(msg) = nsei;
120 msgb_bvci(msg) = ns_bvci;
121
Harald Welte9ba50052010-03-14 15:45:01 +0800122 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
123 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
124
Harald Welte24a655f2010-04-30 19:54:29 +0200125 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800126}
127
Harald Weltea8aa4df2010-05-30 22:00:53 +0200128/* 10.3.7 SUSPEND-ACK PDU */
129int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
130 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
131{
132 struct msgb *msg = bssgp_msgb_alloc();
133 struct bssgp_normal_hdr *bgph =
134 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
135 uint32_t _tlli;
136 uint8_t ra[6];
137
138 msgb_nsei(msg) = nsei;
139 msgb_bvci(msg) = 0; /* Signalling */
140 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
141
Harald Weltebfe62e52017-05-15 12:48:30 +0200142 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200143 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
144 gsm48_construct_ra(ra, ra_id);
145 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
146 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
147
148 return gprs_ns_sendmsg(bssgp_nsi, msg);
149}
150
151/* 10.3.8 SUSPEND-NACK PDU */
152int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100153 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200154 uint8_t *cause)
155{
156 struct msgb *msg = bssgp_msgb_alloc();
157 struct bssgp_normal_hdr *bgph =
158 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
159 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100160 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200161
162 msgb_nsei(msg) = nsei;
163 msgb_bvci(msg) = 0; /* Signalling */
164 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
165
Harald Weltebfe62e52017-05-15 12:48:30 +0200166 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200167 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100168 gsm48_construct_ra(ra, ra_id);
169 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200170 if (cause)
171 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
172
173 return gprs_ns_sendmsg(bssgp_nsi, msg);
174}
175
176/* 10.3.10 RESUME-ACK PDU */
177int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
178 const struct gprs_ra_id *ra_id)
179{
180 struct msgb *msg = bssgp_msgb_alloc();
181 struct bssgp_normal_hdr *bgph =
182 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
183 uint32_t _tlli;
184 uint8_t ra[6];
185
186 msgb_nsei(msg) = nsei;
187 msgb_bvci(msg) = 0; /* Signalling */
188 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
189
Harald Weltebfe62e52017-05-15 12:48:30 +0200190 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200191 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
192 gsm48_construct_ra(ra, ra_id);
193 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
194
195 return gprs_ns_sendmsg(bssgp_nsi, msg);
196}
197
198/* 10.3.11 RESUME-NACK PDU */
199int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
200 const struct gprs_ra_id *ra_id, uint8_t *cause)
201{
202 struct msgb *msg = bssgp_msgb_alloc();
203 struct bssgp_normal_hdr *bgph =
204 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
205 uint32_t _tlli;
206 uint8_t ra[6];
207
208 msgb_nsei(msg) = nsei;
209 msgb_bvci(msg) = 0; /* Signalling */
210 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
211
Harald Weltebfe62e52017-05-15 12:48:30 +0200212 _tlli = osmo_htonl(tlli);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200213 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
214 gsm48_construct_ra(ra, ra_id);
215 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
216 if (cause)
217 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
218
219 return gprs_ns_sendmsg(bssgp_nsi, msg);
220}
221
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200222uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200223{
224 /* 6 octets RAC */
225 gsm48_parse_ra(raid, buf);
226 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200227 return osmo_load16be(buf+6);
Harald Welte6752fa42010-05-02 09:23:16 +0200228}
229
Harald Welte28610072011-11-24 21:32:07 +0100230int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
231 uint16_t cid)
232{
Harald Welte28610072011-11-24 21:32:07 +0100233 /* 6 octets RAC */
234 gsm48_construct_ra(buf, raid);
235 /* 2 octets CID */
Harald Weltebfe62e52017-05-15 12:48:30 +0200236 osmo_store16be(cid, buf+6);
Harald Welte28610072011-11-24 21:32:07 +0100237
238 return 8;
239}
240
Harald Welte3fddf3c2010-05-01 16:48:27 +0200241/* Chapter 8.4 BVC-Reset Procedure */
242static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
243 uint16_t ns_bvci)
244{
Harald Welte15a36432012-06-17 12:16:31 +0800245 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200246 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200247 uint16_t nsei = msgb_nsei(msg);
248 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200249
Harald Weltebfe62e52017-05-15 12:48:30 +0200250 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Weltee9686b62010-05-31 18:07:17 +0200251 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200252 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
253
Harald Welte6752fa42010-05-02 09:23:16 +0200254 /* look-up or create the BTS context for this BVC */
255 bctx = btsctx_by_bvci_nsei(bvci, nsei);
256 if (!bctx)
257 bctx = btsctx_alloc(bvci, nsei);
258
Harald Welte25de8112010-05-13 21:26:28 +0200259 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
260 bctx->state &= ~BVC_S_BLOCKED;
261
Harald Welte3fddf3c2010-05-01 16:48:27 +0200262 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
263 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200264 if (bvci != 0 && bvci != 1) {
265 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200266 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200267 "missing mandatory IE\n", bvci);
268 return -EINVAL;
269 }
270 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200271 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
272 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200273 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200274 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
275 bctx->ra_id.rac, bctx->cell_id, bvci);
276 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200277
Harald Welte15a36432012-06-17 12:16:31 +0800278 /* Send NM_BVC_RESET.ind to NM */
279 memset(&nmp, 0, sizeof(nmp));
280 nmp.nsei = nsei;
281 nmp.bvci = bvci;
282 nmp.tp = tp;
283 nmp.ra_id = &bctx->ra_id;
284 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
285 PRIM_OP_INDICATION, msg);
286 bssgp_prim_cb(&nmp.oph, NULL);
287
Harald Welte6752fa42010-05-02 09:23:16 +0200288 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200289 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
290 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200291 return 0;
292}
293
Harald Welte25de8112010-05-13 21:26:28 +0200294static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
295{
Harald Welte15a36432012-06-17 12:16:31 +0800296 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200297 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200298 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200299
Harald Weltebfe62e52017-05-15 12:48:30 +0200300 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200301 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200302 /* 8.3.2: Signalling BVC shall never be blocked */
303 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
304 "received block for signalling BVC!?!\n",
305 msgb_nsei(msg), msgb_bvci(msg));
306 return 0;
307 }
Harald Welte25de8112010-05-13 21:26:28 +0200308
Harald Welte086fe322011-08-19 16:45:19 +0200309 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200310
311 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
312 if (!ptp_ctx)
313 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
314
315 ptp_ctx->state |= BVC_S_BLOCKED;
316 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
317
Harald Welte15a36432012-06-17 12:16:31 +0800318 /* Send NM_BVC_BLOCK.ind to NM */
319 memset(&nmp, 0, sizeof(nmp));
320 nmp.nsei = msgb_nsei(msg);
321 nmp.bvci = bvci;
322 nmp.tp = tp;
323 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
324 PRIM_OP_INDICATION, msg);
325 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200326
327 /* We always acknowledge the BLOCKing */
328 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
329 bvci, msgb_bvci(msg));
330};
331
332static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
333{
Harald Welte15a36432012-06-17 12:16:31 +0800334 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200335 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200336 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200337
Harald Weltebfe62e52017-05-15 12:48:30 +0200338 bvci = tlvp_val16be(tp, BSSGP_IE_BVCI);
Harald Welte61c07842010-05-18 11:57:08 +0200339 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200340 /* 8.3.2: Signalling BVC shall never be blocked */
341 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
342 "received unblock for signalling BVC!?!\n",
343 msgb_nsei(msg), msgb_bvci(msg));
344 return 0;
345 }
Harald Welte25de8112010-05-13 21:26:28 +0200346
Harald Weltee9686b62010-05-31 18:07:17 +0200347 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200348
349 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
350 if (!ptp_ctx)
351 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
352
353 ptp_ctx->state &= ~BVC_S_BLOCKED;
354
Harald Welte15a36432012-06-17 12:16:31 +0800355 /* Send NM_BVC_UNBLOCK.ind to NM */
356 memset(&nmp, 0, sizeof(nmp));
357 nmp.nsei = msgb_nsei(msg);
358 nmp.bvci = bvci;
359 nmp.tp = tp;
360 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
361 PRIM_OP_INDICATION, msg);
362 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200363
364 /* We always acknowledge the unBLOCKing */
365 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
366 bvci, msgb_bvci(msg));
367};
368
Harald Welte9ba50052010-03-14 15:45:01 +0800369/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200370static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200371 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800372{
Harald Welte15a36432012-06-17 12:16:31 +0800373 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200374 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800375
Harald Welte6752fa42010-05-02 09:23:16 +0200376 /* extract TLLI and parse TLV IEs */
Harald Weltebfe62e52017-05-15 12:48:30 +0200377 msgb_tlli(msg) = osmo_ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800378
Harald Welte086fe322011-08-19 16:45:19 +0200379 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200380
Harald Welte9ba50052010-03-14 15:45:01 +0800381 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200382 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200383 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
384 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
385 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200386 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200387 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200388
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200389 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800390 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
391 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800392
Harald Welte15a36432012-06-17 12:16:31 +0800393 /* Send BSSGP_UL_UD.ind to NM */
394 memset(&gbp, 0, sizeof(gbp));
395 gbp.nsei = ctx->nsei;
396 gbp.bvci = ctx->bvci;
397 gbp.tlli = msgb_tlli(msg);
398 gbp.tp = tp;
399 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
400 PRIM_OP_INDICATION, msg);
401 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800402}
403
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200404static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800405{
Harald Welte15a36432012-06-17 12:16:31 +0800406 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200407 struct gprs_ra_id raid;
408 uint32_t tlli;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200409 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200410 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800411
Harald Welte25de8112010-05-13 21:26:28 +0200412 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200413 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
414 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200415 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200416 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200417 }
Harald Welte9ba50052010-03-14 15:45:01 +0800418
Harald Weltebfe62e52017-05-15 12:48:30 +0200419 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200420
Harald Welte17925322010-05-31 20:18:35 +0200421 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200422 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200423
Harald Weltea8aa4df2010-05-30 22:00:53 +0200424 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
425
Harald Welte313cccf2010-06-09 11:22:47 +0200426 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800427 memset(&gbp, 0, sizeof(gbp));
428 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200429 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800430 gbp.tlli = tlli;
431 gbp.ra_id = &raid;
432 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
433 PRIM_OP_REQUEST, msg);
434
435 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200436 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100437 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200438
Harald Weltea8aa4df2010-05-30 22:00:53 +0200439 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
440
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800441 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800442}
443
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200444static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800445{
Harald Welte15a36432012-06-17 12:16:31 +0800446 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200447 struct gprs_ra_id raid;
448 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200449 uint8_t suspend_ref;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200450 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200451 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800452
Harald Welte25de8112010-05-13 21:26:28 +0200453 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
454 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200455 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
456 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200457 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200458 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200459 }
Harald Welte9ba50052010-03-14 15:45:01 +0800460
Harald Weltebfe62e52017-05-15 12:48:30 +0200461 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Welte313cccf2010-06-09 11:22:47 +0200462 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200463
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200464 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200465
Harald Weltea8aa4df2010-05-30 22:00:53 +0200466 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
467
Harald Welte313cccf2010-06-09 11:22:47 +0200468 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800469 memset(&gbp, 0, sizeof(gbp));
470 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200471 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800472 gbp.tlli = tlli;
473 gbp.ra_id = &raid;
474 gbp.u.resume.suspend_ref = suspend_ref;
475 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
476 PRIM_OP_REQUEST, msg);
477
478 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200479 if (rc < 0)
480 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
481 NULL);
482
Harald Weltea8aa4df2010-05-30 22:00:53 +0200483 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800484 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800485}
486
Harald Weltee9686b62010-05-31 18:07:17 +0200487
488static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
489 struct bssgp_bvc_ctx *ctx)
490{
Harald Welte15a36432012-06-17 12:16:31 +0800491 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200492 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200493
494 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
495 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
496 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
497 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
498 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
499 "missing mandatory IE\n", ctx->bvci);
500 }
501
Harald Welteb7363142010-07-23 21:59:29 +0200502 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
Harald Weltebfe62e52017-05-15 12:48:30 +0200503 tlli = tlvp_val32be(tp, BSSGP_IE_TLLI);
Harald Weltee9686b62010-05-31 18:07:17 +0200504
Harald Welte086fe322011-08-19 16:45:19 +0200505 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200506 ctx->bvci, tlli);
507
508 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
509
Harald Welte15a36432012-06-17 12:16:31 +0800510 /* send NM_LLC_DISCARDED to NM */
511 memset(&nmp, 0, sizeof(nmp));
512 nmp.nsei = msgb_nsei(msg);
513 nmp.bvci = ctx->bvci;
514 nmp.tlli = tlli;
515 nmp.tp = tp;
516 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
517 PRIM_OP_INDICATION, msg);
518
519 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200520}
521
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100522int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
523 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
524{
525 struct osmo_bssgp_prim nmp;
526 enum gprs_bssgp_cause cause;
527
528 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
529 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
530 "missing mandatory IE\n", bvci);
531 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
532 } else {
533 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
534 }
535
536 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
537 bvci, bssgp_cause_str(cause));
538
539 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
540 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
541 LOGP(DBSSGP, LOGL_ERROR,
542 "BSSGP BVCI=%u Rx STATUS cause=%s "
543 "missing conditional BVCI IE\n",
544 bvci, bssgp_cause_str(cause));
545 }
546
547 if (bctx)
548 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
549
550 /* send NM_STATUS to NM */
551 memset(&nmp, 0, sizeof(nmp));
552 nmp.nsei = msgb_nsei(msg);
553 nmp.bvci = bvci;
554 nmp.tp = tp;
555 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
556 PRIM_OP_INDICATION, msg);
557
558 return bssgp_prim_cb(&nmp.oph, NULL);
559}
560
561
Harald Welted11c0592012-09-06 21:57:11 +0200562/* One element (msgb) in a BSSGP Flow Control queue */
563struct bssgp_fc_queue_element {
564 /* linked list of queue elements */
565 struct llist_head list;
566 /* The message that we have enqueued */
567 struct msgb *msg;
568 /* Length of the LLC PDU part of the contained message */
569 uint32_t llc_pdu_len;
570 /* private pointer passed to the flow control out_cb function */
571 void *priv;
572};
573
574static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
575static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
576
577static void fc_timer_cb(void *data)
578{
579 struct bssgp_flow_control *fc = data;
580 struct bssgp_fc_queue_element *fcqe;
581 struct timeval time_now;
582
583 /* if the queue is empty, we return without sending something
584 * and without re-starting the timer */
585 if (llist_empty(&fc->queue))
586 return;
587
588 /* get the first entry from the queue */
589 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
590 list);
591
592 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
593 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
594 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
595 /* make sure we re-start the timer */
596 fc_queue_timer_cfg(fc);
597 return;
598 }
599
600 /* remove from the queue */
601 llist_del(&fcqe->list);
602
603 fc->queue_depth--;
604
605 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200606 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200607 fc->time_last_pdu = time_now;
608
609 /* call the output callback for this FC instance */
610 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
611
612 /* we expect that out_cb will in the end free the msgb once
613 * it is no longer needed */
614
615 /* but we have to free the queue element ourselves */
616 talloc_free(fcqe);
617
618 /* re-configure the timer for the next PDU */
619 fc_queue_timer_cfg(fc);
620}
621
622/* configure/schedule the flow control timer to expire once the bucket
623 * will have leaked a sufficient number of bytes to transmit the next
624 * PDU in the queue */
625static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
626{
627 struct bssgp_fc_queue_element *fcqe;
628 uint32_t msecs;
629
630 if (llist_empty(&fc->queue))
631 return 0;
632
Jacob Erlbeck97319352015-04-30 19:28:03 +0200633 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
Harald Welted11c0592012-09-06 21:57:11 +0200634 list);
635
Harald Welte27b2bb72013-06-22 09:44:00 +0200636 if (fc->bucket_leak_rate != 0) {
637 /* Calculate the point in time at which we will have leaked
638 * a sufficient number of bytes from the bucket to transmit
639 * the first PDU in the queue */
640 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
641 /* FIXME: add that time to fc->time_last_pdu and subtract it from
642 * current time */
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200643 osmo_timer_setup(&fc->timer, fc_timer_cb, fc);
Harald Welte27b2bb72013-06-22 09:44:00 +0200644 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
645 } else {
646 /* If the PCU is telling us to not send any more data at all,
647 * there's no point starting a timer. */
648 }
Harald Welted11c0592012-09-06 21:57:11 +0200649
650 return 0;
651}
652
653/* Enqueue a PDU in the flow control queue for delayed transmission */
654static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
655 uint32_t llc_pdu_len, void *priv)
656{
657 struct bssgp_fc_queue_element *fcqe;
658
659 if (fc->queue_depth >= fc->max_queue_depth)
660 return -ENOSPC;
661
662 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
663 if (!fcqe)
664 return -ENOMEM;
665 fcqe->msg = msg;
666 fcqe->llc_pdu_len = llc_pdu_len;
667 fcqe->priv = priv;
668
669 llist_add_tail(&fcqe->list, &fc->queue);
670
671 fc->queue_depth++;
672
673 /* re-configure the timer for dequeueing the pdu */
674 fc_queue_timer_cfg(fc);
675
676 return 0;
677}
678
679/* According to Section 8.2 */
680static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
681{
682 struct timeval time_now, time_diff;
683 int64_t bucket_predicted;
684 uint32_t csecs_elapsed, leaked;
685
686 /* B' = B + L(p) - (Tc - Tp)*R */
687
688 /* compute number of centi-seconds that have elapsed since transmitting
689 * the last PDU (Tc - Tp) */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200690 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200691 timersub(&time_now, &fc->time_last_pdu, &time_diff);
692 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
693
694 /* compute number of bytes that have leaked in the elapsed number
695 * of centi-seconds */
696 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
697 /* add the current PDU length to the last bucket level */
698 bucket_predicted = fc->bucket_counter + pdu_len;
699 /* ... and subtract the number of leaked bytes */
700 bucket_predicted -= leaked;
701
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700702 if (bucket_predicted < pdu_len)
703 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200704
705 if (bucket_predicted <= fc->bucket_size_max) {
706 /* the bucket is not full yet, we can pass the packet */
707 fc->bucket_counter = bucket_predicted;
Vadim Yanitskiyf1786952017-06-12 03:41:35 +0700708 return 0;
Harald Welted11c0592012-09-06 21:57:11 +0200709 }
710
711 /* bucket is full, PDU needs to be delayed */
712 return 1;
Harald Welted11c0592012-09-06 21:57:11 +0200713}
714
715/* output callback for BVC flow control */
716static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
717 uint32_t llc_pdu_len, void *priv)
718{
719 return gprs_ns_sendmsg(bssgp_nsi, msg);
720}
721
722/* input function of the flow control implementation, called first
723 * for the MM flow control, and then as the MM flow control output
724 * callback in order to perform BVC flow control */
725int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
726 uint32_t llc_pdu_len, void *priv)
727{
728 struct timeval time_now;
729
Harald Weltebb826222012-09-07 10:22:01 +0200730 if (llc_pdu_len > fc->bucket_size_max) {
731 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
732 "than maximum bucket size (%u)!\n", llc_pdu_len,
733 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200734 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200735 return -EIO;
736 }
737
Harald Welted11c0592012-09-06 21:57:11 +0200738 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
739 return fc_enqueue(fc, msg, llc_pdu_len, priv);
740 } else {
741 /* record the time we transmitted this PDU */
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200742 osmo_gettimeofday(&time_now, NULL);
Harald Welted11c0592012-09-06 21:57:11 +0200743 fc->time_last_pdu = time_now;
744 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
745 }
746}
747
Harald Weltebb826222012-09-07 10:22:01 +0200748
749/* Initialize the Flow Control structure */
750void bssgp_fc_init(struct bssgp_flow_control *fc,
751 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
752 uint32_t max_queue_depth,
753 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
754 uint32_t llc_pdu_len, void *priv))
755{
756 fc->out_cb = out_cb;
757 fc->bucket_size_max = bucket_size_max;
758 fc->bucket_leak_rate = bucket_leak_rate;
759 fc->max_queue_depth = max_queue_depth;
760 INIT_LLIST_HEAD(&fc->queue);
Neels Hofmeyr8e2f7e82016-09-22 03:58:13 +0200761 osmo_gettimeofday(&fc->time_last_pdu, NULL);
Harald Weltebb826222012-09-07 10:22:01 +0200762}
763
Harald Welted11c0592012-09-06 21:57:11 +0200764/* Initialize the Flow Control parameters for a new MS according to
765 * default values for the BVC specified by BVCI and NSEI */
766int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200767 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200768{
769 struct bssgp_bvc_ctx *ctx;
770
771 ctx = btsctx_by_bvci_nsei(bvci, nsei);
772 if (!ctx)
773 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200774
775 /* output call-back of per-MS FC is per-CTX FC */
776 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
777 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200778
779 return 0;
780}
781
Harald Welte25de8112010-05-13 21:26:28 +0200782static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200783 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800784{
Harald Welte27b2bb72013-06-22 09:44:00 +0200785 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
786 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800787
Harald Weltee9686b62010-05-31 18:07:17 +0200788 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
789 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800790
791 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
792 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
793 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
794 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200795 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
796 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
797 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800798 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200799 }
Harald Welte9ba50052010-03-14 15:45:01 +0800800
Harald Weltebb826222012-09-07 10:22:01 +0200801 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200802 bctx->fc->bucket_size_max = 100 * tlvp_val16be(tp, BSSGP_IE_BVC_BUCKET_SIZE);
Harald Weltebb826222012-09-07 10:22:01 +0200803 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200804 bctx->fc->bucket_leak_rate = 100 * tlvp_val16be(tp, BSSGP_IE_BUCKET_LEAK_RATE) / 8;
Harald Weltebb826222012-09-07 10:22:01 +0200805 /* 11.3.2 in octets */
Harald Weltebfe62e52017-05-15 12:48:30 +0200806 bctx->bmax_default_ms = tlvp_val16be(tp, BSSGP_IE_BMAX_DEFAULT_MS);
Harald Weltebb826222012-09-07 10:22:01 +0200807 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Weltebfe62e52017-05-15 12:48:30 +0200808 bctx->r_default_ms = 100 * tlvp_val16be(tp, BSSGP_IE_R_DEFAULT_MS) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200809
Harald Welte27b2bb72013-06-22 09:44:00 +0200810 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
811 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
812 "rate of 0, stopping all DL GPRS!\n");
813 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
814 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
815 "rate of != 0, restarting all DL GPRS!\n");
816
817 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
818 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
819 "bucket leak rate of 0, stopping DL GPRS!\n");
820 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
821 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
822 "bucket leak rate != 0, restarting DL GPRS!\n");
823
824 /* reconfigure the timer for flow control based on new values */
825 fc_queue_timer_cfg(bctx->fc);
826
Harald Welte9ba50052010-03-14 15:45:01 +0800827 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200828 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200829 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800830}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200831
Harald Welte25de8112010-05-13 21:26:28 +0200832/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800833static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
834 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800835{
Harald Welteec19c102010-05-02 09:50:42 +0200836 struct bssgp_normal_hdr *bgph =
837 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200838 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800839 int rc = 0;
840
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100841 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
842
Harald Welte58e65c92010-05-13 21:45:23 +0200843 /* If traffic is received on a BVC that is marked as blocked, the
844 * received PDU shall not be accepted and a STATUS PDU (Cause value:
845 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100846 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200847 uint16_t bvci = msgb_bvci(msg);
848 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
849 }
850
Harald Welte9ba50052010-03-14 15:45:01 +0800851 switch (pdu_type) {
852 case BSSGP_PDUT_UL_UNITDATA:
853 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200854 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800855 break;
856 case BSSGP_PDUT_RA_CAPABILITY:
857 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200858 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
859 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200860 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800861 /* FIXME: send RA_CAPA_UPDATE_ACK */
862 break;
863 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200864 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800865 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200866 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800867 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800868 case BSSGP_PDUT_FLOW_CONTROL_BVC:
869 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200870 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800871 break;
872 case BSSGP_PDUT_FLOW_CONTROL_MS:
873 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200874 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
875 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200876 /* FIXME: actually implement flow control */
877 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800878 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800879 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100880 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100881 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800882 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
883 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
884 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
885 case BSSGP_PDUT_MODIFY_BSS_PFC:
886 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Max2c34ab42016-03-17 15:42:26 +0100887 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s not [yet] "
888 "implemented\n", bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200889 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800890 break;
891 /* those only exist in the SGSN -> BSS direction */
892 case BSSGP_PDUT_DL_UNITDATA:
893 case BSSGP_PDUT_PAGING_PS:
894 case BSSGP_PDUT_PAGING_CS:
895 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200896 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
897 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Max2c34ab42016-03-17 15:42:26 +0100898 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s only exists in DL\n",
899 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200900 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
901 rc = -EINVAL;
902 break;
903 default:
Max2c34ab42016-03-17 15:42:26 +0100904 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type %s unknown\n",
905 bctx->bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200906 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
907 break;
908 }
909
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800910 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200911}
912
913/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800914static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
915 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200916{
917 struct bssgp_normal_hdr *bgph =
918 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
919 uint8_t pdu_type = bgph->pdu_type;
920 int rc = 0;
921 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200922 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200923
924 switch (bgph->pdu_type) {
925 case BSSGP_PDUT_SUSPEND:
926 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200927 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200928 break;
929 case BSSGP_PDUT_RESUME:
930 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200931 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200932 break;
933 case BSSGP_PDUT_FLUSH_LL_ACK:
934 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200935 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200936 /* FIXME: send NM_FLUSH_LL.res to NM */
937 break;
938 case BSSGP_PDUT_LLC_DISCARD:
939 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200940 if (!bctx) {
941 LOGP(DBSSGP, LOGL_ERROR,
942 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
943 goto err_mand_ie;
944 }
Harald Weltee9686b62010-05-31 18:07:17 +0200945 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200946 break;
947 case BSSGP_PDUT_BVC_BLOCK:
948 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200949 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200950 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
951 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
952 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200953 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200954 }
Harald Welte2677ea52010-05-31 17:16:36 +0200955 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200956 break;
957 case BSSGP_PDUT_BVC_UNBLOCK:
958 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200959 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
960 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
961 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200962 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200963 }
Harald Welte25de8112010-05-13 21:26:28 +0200964 rc = bssgp_rx_bvc_unblock(msg, tp);
965 break;
Max590c4022017-06-28 14:29:24 +0200966 case BSSGP_PDUT_BVC_RESET_ACK:
967 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx BVC-RESET-ACK\n", bvci);
968 break;
Harald Welte25de8112010-05-13 21:26:28 +0200969 case BSSGP_PDUT_BVC_RESET:
970 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200971 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200972 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
973 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
974 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200975 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200976 }
Harald Welte25de8112010-05-13 21:26:28 +0200977 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
978 break;
979 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100980 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +0200981 break;
982 /* those only exist in the SGSN -> BSS direction */
983 case BSSGP_PDUT_PAGING_PS:
984 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800985 case BSSGP_PDUT_SUSPEND_ACK:
986 case BSSGP_PDUT_SUSPEND_NACK:
987 case BSSGP_PDUT_RESUME_ACK:
988 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200989 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800990 case BSSGP_PDUT_BVC_BLOCK_ACK:
991 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
992 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Max2c34ab42016-03-17 15:42:26 +0100993 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s only exists in DL\n",
994 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +0200995 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800996 rc = -EINVAL;
997 break;
998 default:
Max2c34ab42016-03-17 15:42:26 +0100999 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type %s unknown\n",
1000 bvci, bssgp_pdu_str(pdu_type));
Harald Welte25de8112010-05-13 21:26:28 +02001001 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001002 break;
1003 }
1004
1005 return rc;
1006err_mand_ie:
1007 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1008}
1009
Harald Welte25de8112010-05-13 21:26:28 +02001010/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001011int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001012{
1013 struct bssgp_normal_hdr *bgph =
1014 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1015 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1016 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001017 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001018 uint8_t pdu_type = bgph->pdu_type;
1019 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001020 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001021 int data_len;
1022 int rc = 0;
1023
1024 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1025
1026 /* UNITDATA BSSGP headers have TLLI in front */
1027 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1028 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1029 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1030 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1031 } else {
1032 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1033 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1034 }
1035
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001036 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
Harald Weltebfe62e52017-05-15 12:48:30 +02001037 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001038
Harald Welte25de8112010-05-13 21:26:28 +02001039 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001040 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Harald Welte25de8112010-05-13 21:26:28 +02001041
Harald Welte16c8dbb2010-05-17 23:30:01 +02001042 if (bctx) {
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +01001043 log_set_context(LOG_CTX_GB_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001044 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1045 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1046 msgb_bssgp_len(msg));
1047 }
1048
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001049 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1050 * are otherwise unexpected */
1051 if (pdu_type == BSSGP_PDUT_STATUS)
1052 /* Some exception has occurred */
1053 return bssgp_rx_status(msg, &tp, bvci, bctx);
1054
1055 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1056 * registered if a BVCI is given. */
1057 if (!bctx && bvci != BVCI_SIGNALLING &&
1058 pdu_type != BSSGP_PDUT_BVC_RESET) {
1059 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
Max2c34ab42016-03-17 15:42:26 +01001060 "type %s for unknown BVCI\n", msgb_nsei(msg), bvci,
1061 bssgp_pdu_str(pdu_type));
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001062 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1063 }
1064
Harald Welte61c07842010-05-18 11:57:08 +02001065 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001066 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001067 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001068 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001069 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001070 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001071 else
1072 LOGP(DBSSGP, LOGL_NOTICE,
Max2c34ab42016-03-17 15:42:26 +01001073 "NSEI=%u/BVCI=%u Cannot handle PDU type %s for "
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001074 "unknown BVCI, NS BVCI %u\n",
Max2c34ab42016-03-17 15:42:26 +01001075 msgb_nsei(msg), bvci, bssgp_pdu_str(pdu_type), ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001076
1077 return rc;
1078}
1079
Harald Weltede4599c2012-06-17 13:04:02 +08001080int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1081 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001082{
Harald Welte8a521132010-05-17 22:59:29 +02001083 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001084 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001085 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001086 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001087 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001088 uint16_t bvci = msgb_bvci(msg);
1089 uint16_t nsei = msgb_nsei(msg);
Harald Weltebfe62e52017-05-15 12:48:30 +02001090 uint16_t _pdu_lifetime = osmo_htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001091 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001092
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001093 OSMO_ASSERT(dup != NULL);
1094
Harald Welte30bc19a2010-05-02 11:19:37 +02001095 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001096 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001097 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001098 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001099 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001100 return -EINVAL;
1101 }
1102
1103 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001104 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001105 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1106 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001107 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001108 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001109 }
Harald Welte9ba50052010-03-14 15:45:01 +08001110
1111 if (msg->len > TVLV_MAX_ONEBYTE)
1112 llc_pdu_tlv_hdr_len += 1;
1113
1114 /* prepend the tag and length of the LLC-PDU TLV */
1115 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1116 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1117 if (llc_pdu_tlv_hdr_len > 2) {
1118 llc_pdu_tlv[1] = msg_len >> 8;
1119 llc_pdu_tlv[2] = msg_len & 0xff;
1120 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001121 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001122 llc_pdu_tlv[1] |= 0x80;
1123 }
1124
Harald Welte2f946832010-05-31 22:12:30 +02001125 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1126
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001127 /* Old TLLI to help BSS map from old->new */
1128 if (dup->tlli) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001129 uint32_t tlli = osmo_htonl(*dup->tlli);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001130 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001131 }
Harald Welte9ba50052010-03-14 15:45:01 +08001132
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001133 /* IMSI */
1134 if (dup->imsi && strlen(dup->imsi)) {
1135 uint8_t mi[10];
1136 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1137 if (imsi_len > 2)
1138 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1139 imsi_len-2, mi+2);
1140 }
1141
1142 /* DRX parameters */
Harald Weltebfe62e52017-05-15 12:48:30 +02001143 drx_params = osmo_htons(dup->drx_parms);
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001144 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1145 (uint8_t *) &drx_params);
1146
1147 /* FIXME: Priority */
1148
1149 /* MS Radio Access Capability */
1150 if (dup->ms_ra_cap.len)
1151 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1152 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1153
Harald Welte9ba50052010-03-14 15:45:01 +08001154 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001155 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001156
1157 /* prepend the QoS profile, TLLI and pdu type */
1158 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001159 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Weltebfe62e52017-05-15 12:48:30 +02001160 budh->tlli = osmo_htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001161 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1162
Harald Welte16c8dbb2010-05-17 23:30:01 +02001163 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1164 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1165
Harald Welte30bc19a2010-05-02 11:19:37 +02001166 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001167
Harald Welted11c0592012-09-06 21:57:11 +02001168 /* check if we have to go through per-ms flow control or can go
1169 * directly to the per-BSS flow control */
1170 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001171 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001172 else
Harald Welted8b47692012-09-07 11:29:32 +02001173 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001174}
Harald Welte68b4f032010-06-09 16:22:28 +02001175
1176/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001177int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1178 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001179{
1180 struct msgb *msg = bssgp_msgb_alloc();
1181 struct bssgp_normal_hdr *bgph =
1182 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +02001183 uint16_t drx_params = osmo_htons(pinfo->drx_params);
Harald Welte68b4f032010-06-09 16:22:28 +02001184 uint8_t mi[10];
1185 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1186 uint8_t ra[6];
1187
1188 if (imsi_len < 2)
1189 return -EINVAL;
1190
1191 msgb_nsei(msg) = nsei;
1192 msgb_bvci(msg) = ns_bvci;
1193
1194 if (pinfo->mode == BSSGP_PAGING_PS)
1195 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1196 else
1197 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1198 /* IMSI */
1199 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1200 /* DRX Parameters */
1201 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1202 (uint8_t *) &drx_params);
1203 /* Scope */
1204 switch (pinfo->scope) {
1205 case BSSGP_PAGING_BSS_AREA:
1206 {
1207 uint8_t null = 0;
1208 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1209 }
1210 break;
1211 case BSSGP_PAGING_LOCATION_AREA:
1212 gsm48_construct_ra(ra, &pinfo->raid);
1213 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1214 break;
1215 case BSSGP_PAGING_ROUTEING_AREA:
1216 gsm48_construct_ra(ra, &pinfo->raid);
1217 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1218 break;
1219 case BSSGP_PAGING_BVCI:
1220 {
Harald Weltebfe62e52017-05-15 12:48:30 +02001221 uint16_t bvci = osmo_htons(pinfo->bvci);
Harald Welte68b4f032010-06-09 16:22:28 +02001222 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1223 }
1224 break;
1225 }
1226 /* QoS profile mandatory for PS */
1227 if (pinfo->mode == BSSGP_PAGING_PS)
1228 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1229
1230 /* Optional (P-)TMSI */
1231 if (pinfo->ptmsi) {
Harald Weltebfe62e52017-05-15 12:48:30 +02001232 uint32_t ptmsi = osmo_htonl(*pinfo->ptmsi);
Harald Welte68b4f032010-06-09 16:22:28 +02001233 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1234 }
1235
1236 return gprs_ns_sendmsg(bssgp_nsi, msg);
1237}
Harald Weltecca49632012-06-16 17:45:59 +08001238
Harald Weltede4599c2012-06-17 13:04:02 +08001239void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001240{
1241 DBSSGP = ss;
1242}