blob: 506efdf6cd03a32c1b6eba89b11f74f2c3163675 [file] [log] [blame]
Harald Welte9ba50052010-03-14 15:45:01 +08001/* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */
2
Harald Welte605ac5d2012-06-16 16:09:52 +08003/* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte9ba50052010-03-14 15:45:01 +08004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Weltee4cbb3f2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee4cbb3f2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080016 *
Harald Weltee4cbb3f2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080019 *
Harald Welte4e5721d2010-05-17 23:41:43 +020020 * TODO:
21 * o properly count incoming BVC-RESET packets in counter group
22 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080023 */
24
25#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020026#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080027
28#include <netinet/in.h>
29
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010030#include <osmocom/core/msgb.h>
31#include <osmocom/gsm/tlv.h>
32#include <osmocom/core/talloc.h>
33#include <osmocom/core/rate_ctr.h>
Harald Welte6752fa42010-05-02 09:23:16 +020034
Harald Welte73952e32012-06-16 14:59:56 +080035#include <osmocom/gprs/gprs_bssgp.h>
36#include <osmocom/gprs/gprs_ns.h>
37
Harald Weltecca49632012-06-16 17:45:59 +080038#include "common_vty.h"
39
Harald Welte6752fa42010-05-02 09:23:16 +020040void *bssgp_tall_ctx = NULL;
41
Harald Welte25de8112010-05-13 21:26:28 +020042static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Welte16c8dbb2010-05-17 23:30:01 +020043 { "packets.in", "Packets at BSSGP Level ( In)" },
44 { "packets.out","Packets at BSSGP Level (Out)" },
45 { "bytes.in", "Bytes at BSSGP Level ( In)" },
46 { "bytes.out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020047 { "blocked", "BVC Blocking count" },
48 { "discarded", "BVC LLC Discarded count" },
49};
50
51static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
52 .group_name_prefix = "bssgp.bss_ctx",
53 .group_description = "BSSGP Peer Statistics",
54 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
55 .ctr_desc = bssgp_ctr_description,
56};
57
Harald Weltea78b9c22010-05-17 23:02:42 +020058LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020059
Harald Welted11c0592012-09-06 21:57:11 +020060static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
61 uint32_t llc_pdu_len, void *priv);
62
Harald Welte6752fa42010-05-02 09:23:16 +020063/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020064struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020065{
Harald Welte8a521132010-05-17 22:59:29 +020066 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020067
Harald Weltea78b9c22010-05-17 23:02:42 +020068 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020069 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
70 bctx->cell_id == cid)
71 return bctx;
72 }
73 return NULL;
74}
75
76/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +020077struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020078{
Harald Welte8a521132010-05-17 22:59:29 +020079 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020080
Harald Weltea78b9c22010-05-17 23:02:42 +020081 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020082 if (bctx->nsei == nsei && bctx->bvci == bvci)
83 return bctx;
84 }
85 return NULL;
86}
87
Harald Welte8a521132010-05-17 22:59:29 +020088struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020089{
Harald Welte8a521132010-05-17 22:59:29 +020090 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +020091
Harald Welte8a521132010-05-17 22:59:29 +020092 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +020093 if (!ctx)
94 return NULL;
95 ctx->bvci = bvci;
96 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +020097 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
98 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Harald Welted8b47692012-09-07 11:29:32 +020099 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
100 /* cofigure for 2Mbit, 30 packets in queue */
101 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200102
Harald Weltea78b9c22010-05-17 23:02:42 +0200103 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200104
105 return ctx;
106}
107
Harald Welte9ba50052010-03-14 15:45:01 +0800108/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200109static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800110{
111 struct msgb *msg = bssgp_msgb_alloc();
112 struct bssgp_normal_hdr *bgph =
113 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
114
Harald Welte24a655f2010-04-30 19:54:29 +0200115 msgb_nsei(msg) = nsei;
116 msgb_bvci(msg) = ns_bvci;
117
Harald Welte9ba50052010-03-14 15:45:01 +0800118 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
119 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
120
Harald Welte24a655f2010-04-30 19:54:29 +0200121 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800122}
123
Harald Weltea8aa4df2010-05-30 22:00:53 +0200124/* 10.3.7 SUSPEND-ACK PDU */
125int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
126 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
127{
128 struct msgb *msg = bssgp_msgb_alloc();
129 struct bssgp_normal_hdr *bgph =
130 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
131 uint32_t _tlli;
132 uint8_t ra[6];
133
134 msgb_nsei(msg) = nsei;
135 msgb_bvci(msg) = 0; /* Signalling */
136 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
137
138 _tlli = htonl(tlli);
139 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
140 gsm48_construct_ra(ra, ra_id);
141 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
142 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
143
144 return gprs_ns_sendmsg(bssgp_nsi, msg);
145}
146
147/* 10.3.8 SUSPEND-NACK PDU */
148int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100149 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200150 uint8_t *cause)
151{
152 struct msgb *msg = bssgp_msgb_alloc();
153 struct bssgp_normal_hdr *bgph =
154 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
155 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100156 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200157
158 msgb_nsei(msg) = nsei;
159 msgb_bvci(msg) = 0; /* Signalling */
160 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
161
162 _tlli = htonl(tlli);
163 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100164 gsm48_construct_ra(ra, ra_id);
165 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200166 if (cause)
167 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
168
169 return gprs_ns_sendmsg(bssgp_nsi, msg);
170}
171
172/* 10.3.10 RESUME-ACK PDU */
173int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
174 const struct gprs_ra_id *ra_id)
175{
176 struct msgb *msg = bssgp_msgb_alloc();
177 struct bssgp_normal_hdr *bgph =
178 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
179 uint32_t _tlli;
180 uint8_t ra[6];
181
182 msgb_nsei(msg) = nsei;
183 msgb_bvci(msg) = 0; /* Signalling */
184 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
185
186 _tlli = htonl(tlli);
187 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
188 gsm48_construct_ra(ra, ra_id);
189 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
190
191 return gprs_ns_sendmsg(bssgp_nsi, msg);
192}
193
194/* 10.3.11 RESUME-NACK PDU */
195int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
196 const struct gprs_ra_id *ra_id, uint8_t *cause)
197{
198 struct msgb *msg = bssgp_msgb_alloc();
199 struct bssgp_normal_hdr *bgph =
200 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
201 uint32_t _tlli;
202 uint8_t ra[6];
203
204 msgb_nsei(msg) = nsei;
205 msgb_bvci(msg) = 0; /* Signalling */
206 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
207
208 _tlli = htonl(tlli);
209 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
210 gsm48_construct_ra(ra, ra_id);
211 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
212 if (cause)
213 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
214
215 return gprs_ns_sendmsg(bssgp_nsi, msg);
216}
217
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200218uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200219{
220 /* 6 octets RAC */
221 gsm48_parse_ra(raid, buf);
222 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200223 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200224}
225
Harald Welte28610072011-11-24 21:32:07 +0100226int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
227 uint16_t cid)
228{
229 uint16_t *out_cid = (uint16_t *) (buf + 6);
230 /* 6 octets RAC */
231 gsm48_construct_ra(buf, raid);
232 /* 2 octets CID */
233 *out_cid = htons(cid);
234
235 return 8;
236}
237
Harald Welte3fddf3c2010-05-01 16:48:27 +0200238/* Chapter 8.4 BVC-Reset Procedure */
239static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
240 uint16_t ns_bvci)
241{
Harald Welte15a36432012-06-17 12:16:31 +0800242 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200243 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200244 uint16_t nsei = msgb_nsei(msg);
245 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200246
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200247 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Weltee9686b62010-05-31 18:07:17 +0200248 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200249 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
250
Harald Welte6752fa42010-05-02 09:23:16 +0200251 /* look-up or create the BTS context for this BVC */
252 bctx = btsctx_by_bvci_nsei(bvci, nsei);
253 if (!bctx)
254 bctx = btsctx_alloc(bvci, nsei);
255
Harald Welte25de8112010-05-13 21:26:28 +0200256 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
257 bctx->state &= ~BVC_S_BLOCKED;
258
Harald Welte3fddf3c2010-05-01 16:48:27 +0200259 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
260 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200261 if (bvci != 0 && bvci != 1) {
262 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200263 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200264 "missing mandatory IE\n", bvci);
265 return -EINVAL;
266 }
267 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200268 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
269 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200270 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200271 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
272 bctx->ra_id.rac, bctx->cell_id, bvci);
273 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200274
Harald Welte15a36432012-06-17 12:16:31 +0800275 /* Send NM_BVC_RESET.ind to NM */
276 memset(&nmp, 0, sizeof(nmp));
277 nmp.nsei = nsei;
278 nmp.bvci = bvci;
279 nmp.tp = tp;
280 nmp.ra_id = &bctx->ra_id;
281 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
282 PRIM_OP_INDICATION, msg);
283 bssgp_prim_cb(&nmp.oph, NULL);
284
Harald Welte6752fa42010-05-02 09:23:16 +0200285 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200286 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
287 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200288 return 0;
289}
290
Harald Welte25de8112010-05-13 21:26:28 +0200291static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
292{
Harald Welte15a36432012-06-17 12:16:31 +0800293 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200294 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200295 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200296
297 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200298 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200299 /* 8.3.2: Signalling BVC shall never be blocked */
300 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
301 "received block for signalling BVC!?!\n",
302 msgb_nsei(msg), msgb_bvci(msg));
303 return 0;
304 }
Harald Welte25de8112010-05-13 21:26:28 +0200305
Harald Welte086fe322011-08-19 16:45:19 +0200306 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200307
308 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
309 if (!ptp_ctx)
310 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
311
312 ptp_ctx->state |= BVC_S_BLOCKED;
313 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
314
Harald Welte15a36432012-06-17 12:16:31 +0800315 /* Send NM_BVC_BLOCK.ind to NM */
316 memset(&nmp, 0, sizeof(nmp));
317 nmp.nsei = msgb_nsei(msg);
318 nmp.bvci = bvci;
319 nmp.tp = tp;
320 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
321 PRIM_OP_INDICATION, msg);
322 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200323
324 /* We always acknowledge the BLOCKing */
325 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
326 bvci, msgb_bvci(msg));
327};
328
329static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
330{
Harald Welte15a36432012-06-17 12:16:31 +0800331 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200332 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200333 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200334
335 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200336 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200337 /* 8.3.2: Signalling BVC shall never be blocked */
338 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
339 "received unblock for signalling BVC!?!\n",
340 msgb_nsei(msg), msgb_bvci(msg));
341 return 0;
342 }
Harald Welte25de8112010-05-13 21:26:28 +0200343
Harald Weltee9686b62010-05-31 18:07:17 +0200344 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200345
346 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
347 if (!ptp_ctx)
348 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
349
350 ptp_ctx->state &= ~BVC_S_BLOCKED;
351
Harald Welte15a36432012-06-17 12:16:31 +0800352 /* Send NM_BVC_UNBLOCK.ind to NM */
353 memset(&nmp, 0, sizeof(nmp));
354 nmp.nsei = msgb_nsei(msg);
355 nmp.bvci = bvci;
356 nmp.tp = tp;
357 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
358 PRIM_OP_INDICATION, msg);
359 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200360
361 /* We always acknowledge the unBLOCKing */
362 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
363 bvci, msgb_bvci(msg));
364};
365
Harald Welte9ba50052010-03-14 15:45:01 +0800366/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200367static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200368 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800369{
Harald Welte15a36432012-06-17 12:16:31 +0800370 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200371 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800372
Harald Welte6752fa42010-05-02 09:23:16 +0200373 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200374 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800375
Harald Welte086fe322011-08-19 16:45:19 +0200376 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200377
Harald Welte9ba50052010-03-14 15:45:01 +0800378 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200379 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200380 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
381 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
382 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200383 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200384 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200385
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200386 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800387 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
388 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800389
Harald Welte15a36432012-06-17 12:16:31 +0800390 /* Send BSSGP_UL_UD.ind to NM */
391 memset(&gbp, 0, sizeof(gbp));
392 gbp.nsei = ctx->nsei;
393 gbp.bvci = ctx->bvci;
394 gbp.tlli = msgb_tlli(msg);
395 gbp.tp = tp;
396 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
397 PRIM_OP_INDICATION, msg);
398 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800399}
400
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200401static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800402{
Harald Welte15a36432012-06-17 12:16:31 +0800403 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200404 struct gprs_ra_id raid;
405 uint32_t tlli;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200406 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200407 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800408
Harald Welte25de8112010-05-13 21:26:28 +0200409 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200410 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
411 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200412 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200413 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200414 }
Harald Welte9ba50052010-03-14 15:45:01 +0800415
Harald Weltea8aa4df2010-05-30 22:00:53 +0200416 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200417
Harald Welte17925322010-05-31 20:18:35 +0200418 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200419 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200420
Harald Weltea8aa4df2010-05-30 22:00:53 +0200421 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
422
Harald Welte313cccf2010-06-09 11:22:47 +0200423 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800424 memset(&gbp, 0, sizeof(gbp));
425 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200426 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800427 gbp.tlli = tlli;
428 gbp.ra_id = &raid;
429 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
430 PRIM_OP_REQUEST, msg);
431
432 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200433 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100434 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200435
Harald Weltea8aa4df2010-05-30 22:00:53 +0200436 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
437
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800438 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800439}
440
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200441static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800442{
Harald Welte15a36432012-06-17 12:16:31 +0800443 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200444 struct gprs_ra_id raid;
445 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200446 uint8_t suspend_ref;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200447 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200448 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800449
Harald Welte25de8112010-05-13 21:26:28 +0200450 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
451 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200452 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
453 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200454 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200455 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200456 }
Harald Welte9ba50052010-03-14 15:45:01 +0800457
Harald Weltea8aa4df2010-05-30 22:00:53 +0200458 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Welte313cccf2010-06-09 11:22:47 +0200459 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200460
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200461 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200462
Harald Weltea8aa4df2010-05-30 22:00:53 +0200463 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
464
Harald Welte313cccf2010-06-09 11:22:47 +0200465 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800466 memset(&gbp, 0, sizeof(gbp));
467 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200468 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800469 gbp.tlli = tlli;
470 gbp.ra_id = &raid;
471 gbp.u.resume.suspend_ref = suspend_ref;
472 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
473 PRIM_OP_REQUEST, msg);
474
475 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200476 if (rc < 0)
477 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
478 NULL);
479
Harald Weltea8aa4df2010-05-30 22:00:53 +0200480 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800481 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800482}
483
Harald Weltee9686b62010-05-31 18:07:17 +0200484
485static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
486 struct bssgp_bvc_ctx *ctx)
487{
Harald Welte15a36432012-06-17 12:16:31 +0800488 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200489 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200490
491 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
492 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
493 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
494 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
495 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
496 "missing mandatory IE\n", ctx->bvci);
497 }
498
Harald Welteb7363142010-07-23 21:59:29 +0200499 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
500 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200501
Harald Welte086fe322011-08-19 16:45:19 +0200502 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200503 ctx->bvci, tlli);
504
505 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
506
Harald Welte15a36432012-06-17 12:16:31 +0800507 /* send NM_LLC_DISCARDED to NM */
508 memset(&nmp, 0, sizeof(nmp));
509 nmp.nsei = msgb_nsei(msg);
510 nmp.bvci = ctx->bvci;
511 nmp.tlli = tlli;
512 nmp.tp = tp;
513 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
514 PRIM_OP_INDICATION, msg);
515
516 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200517}
518
Harald Welted11c0592012-09-06 21:57:11 +0200519/* One element (msgb) in a BSSGP Flow Control queue */
520struct bssgp_fc_queue_element {
521 /* linked list of queue elements */
522 struct llist_head list;
523 /* The message that we have enqueued */
524 struct msgb *msg;
525 /* Length of the LLC PDU part of the contained message */
526 uint32_t llc_pdu_len;
527 /* private pointer passed to the flow control out_cb function */
528 void *priv;
529};
530
531static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
532static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
533
534static void fc_timer_cb(void *data)
535{
536 struct bssgp_flow_control *fc = data;
537 struct bssgp_fc_queue_element *fcqe;
538 struct timeval time_now;
539
540 /* if the queue is empty, we return without sending something
541 * and without re-starting the timer */
542 if (llist_empty(&fc->queue))
543 return;
544
545 /* get the first entry from the queue */
546 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
547 list);
548
549 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
550 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
551 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
552 /* make sure we re-start the timer */
553 fc_queue_timer_cfg(fc);
554 return;
555 }
556
557 /* remove from the queue */
558 llist_del(&fcqe->list);
559
560 fc->queue_depth--;
561
562 /* record the time we transmitted this PDU */
563 gettimeofday(&time_now, NULL);
564 fc->time_last_pdu = time_now;
565
566 /* call the output callback for this FC instance */
567 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
568
569 /* we expect that out_cb will in the end free the msgb once
570 * it is no longer needed */
571
572 /* but we have to free the queue element ourselves */
573 talloc_free(fcqe);
574
575 /* re-configure the timer for the next PDU */
576 fc_queue_timer_cfg(fc);
577}
578
579/* configure/schedule the flow control timer to expire once the bucket
580 * will have leaked a sufficient number of bytes to transmit the next
581 * PDU in the queue */
582static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
583{
584 struct bssgp_fc_queue_element *fcqe;
585 uint32_t msecs;
586
587 if (llist_empty(&fc->queue))
588 return 0;
589
590 fcqe = llist_entry(&fc->queue.next, struct bssgp_fc_queue_element,
591 list);
592
Harald Welte27b2bb72013-06-22 09:44:00 +0200593 if (fc->bucket_leak_rate != 0) {
594 /* Calculate the point in time at which we will have leaked
595 * a sufficient number of bytes from the bucket to transmit
596 * the first PDU in the queue */
597 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
598 /* FIXME: add that time to fc->time_last_pdu and subtract it from
599 * current time */
600 fc->timer.data = fc;
601 fc->timer.cb = &fc_timer_cb;
602 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
603 } else {
604 /* If the PCU is telling us to not send any more data at all,
605 * there's no point starting a timer. */
606 }
Harald Welted11c0592012-09-06 21:57:11 +0200607
608 return 0;
609}
610
611/* Enqueue a PDU in the flow control queue for delayed transmission */
612static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
613 uint32_t llc_pdu_len, void *priv)
614{
615 struct bssgp_fc_queue_element *fcqe;
616
617 if (fc->queue_depth >= fc->max_queue_depth)
618 return -ENOSPC;
619
620 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
621 if (!fcqe)
622 return -ENOMEM;
623 fcqe->msg = msg;
624 fcqe->llc_pdu_len = llc_pdu_len;
625 fcqe->priv = priv;
626
627 llist_add_tail(&fcqe->list, &fc->queue);
628
629 fc->queue_depth++;
630
631 /* re-configure the timer for dequeueing the pdu */
632 fc_queue_timer_cfg(fc);
633
634 return 0;
635}
636
637/* According to Section 8.2 */
638static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
639{
640 struct timeval time_now, time_diff;
641 int64_t bucket_predicted;
642 uint32_t csecs_elapsed, leaked;
643
644 /* B' = B + L(p) - (Tc - Tp)*R */
645
646 /* compute number of centi-seconds that have elapsed since transmitting
647 * the last PDU (Tc - Tp) */
648 gettimeofday(&time_now, NULL);
649 timersub(&time_now, &fc->time_last_pdu, &time_diff);
650 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
651
652 /* compute number of bytes that have leaked in the elapsed number
653 * of centi-seconds */
654 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
655 /* add the current PDU length to the last bucket level */
656 bucket_predicted = fc->bucket_counter + pdu_len;
657 /* ... and subtract the number of leaked bytes */
658 bucket_predicted -= leaked;
659
660 if (bucket_predicted < pdu_len) {
661 /* this is just to make sure the bucket doesn't underflow */
662 bucket_predicted = pdu_len;
663 goto pass;
664 }
665
666 if (bucket_predicted <= fc->bucket_size_max) {
667 /* the bucket is not full yet, we can pass the packet */
668 fc->bucket_counter = bucket_predicted;
669 goto pass;
670 }
671
672 /* bucket is full, PDU needs to be delayed */
673 return 1;
674
675pass:
676 /* if we reach here, the PDU can pass */
677 return 0;
678}
679
680/* output callback for BVC flow control */
681static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
682 uint32_t llc_pdu_len, void *priv)
683{
684 return gprs_ns_sendmsg(bssgp_nsi, msg);
685}
686
687/* input function of the flow control implementation, called first
688 * for the MM flow control, and then as the MM flow control output
689 * callback in order to perform BVC flow control */
690int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
691 uint32_t llc_pdu_len, void *priv)
692{
693 struct timeval time_now;
694
Harald Weltebb826222012-09-07 10:22:01 +0200695 if (llc_pdu_len > fc->bucket_size_max) {
696 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
697 "than maximum bucket size (%u)!\n", llc_pdu_len,
698 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200699 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200700 return -EIO;
701 }
702
Harald Welted11c0592012-09-06 21:57:11 +0200703 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
704 return fc_enqueue(fc, msg, llc_pdu_len, priv);
705 } else {
706 /* record the time we transmitted this PDU */
707 gettimeofday(&time_now, NULL);
708 fc->time_last_pdu = time_now;
709 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
710 }
711}
712
Harald Weltebb826222012-09-07 10:22:01 +0200713
714/* Initialize the Flow Control structure */
715void bssgp_fc_init(struct bssgp_flow_control *fc,
716 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
717 uint32_t max_queue_depth,
718 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
719 uint32_t llc_pdu_len, void *priv))
720{
721 fc->out_cb = out_cb;
722 fc->bucket_size_max = bucket_size_max;
723 fc->bucket_leak_rate = bucket_leak_rate;
724 fc->max_queue_depth = max_queue_depth;
725 INIT_LLIST_HEAD(&fc->queue);
726 gettimeofday(&fc->time_last_pdu, NULL);
727}
728
Harald Welted11c0592012-09-06 21:57:11 +0200729/* Initialize the Flow Control parameters for a new MS according to
730 * default values for the BVC specified by BVCI and NSEI */
731int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200732 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200733{
734 struct bssgp_bvc_ctx *ctx;
735
736 ctx = btsctx_by_bvci_nsei(bvci, nsei);
737 if (!ctx)
738 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200739
740 /* output call-back of per-MS FC is per-CTX FC */
741 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
742 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200743
744 return 0;
745}
746
Harald Welte25de8112010-05-13 21:26:28 +0200747static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200748 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800749{
Harald Welte27b2bb72013-06-22 09:44:00 +0200750 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
751 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800752
Harald Weltee9686b62010-05-31 18:07:17 +0200753 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
754 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800755
756 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
757 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
758 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
759 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200760 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
761 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
762 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800763 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200764 }
Harald Welte9ba50052010-03-14 15:45:01 +0800765
Harald Weltebb826222012-09-07 10:22:01 +0200766 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Welted8b47692012-09-07 11:29:32 +0200767 bctx->fc->bucket_size_max = 100 *
Harald Welted11c0592012-09-06 21:57:11 +0200768 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVC_BUCKET_SIZE));
Harald Weltebb826222012-09-07 10:22:01 +0200769 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Welted8b47692012-09-07 11:29:32 +0200770 bctx->fc->bucket_leak_rate = 100 *
Harald Weltebb826222012-09-07 10:22:01 +0200771 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BUCKET_LEAK_RATE)) / 8;
772 /* 11.3.2 in octets */
773 bctx->bmax_default_ms =
Harald Welted11c0592012-09-06 21:57:11 +0200774 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BMAX_DEFAULT_MS));
Harald Weltebb826222012-09-07 10:22:01 +0200775 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Welted11c0592012-09-06 21:57:11 +0200776 bctx->r_default_ms = 100 *
Harald Weltebb826222012-09-07 10:22:01 +0200777 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_R_DEFAULT_MS)) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200778
Harald Welte27b2bb72013-06-22 09:44:00 +0200779 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
780 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
781 "rate of 0, stopping all DL GPRS!\n");
782 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
783 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
784 "rate of != 0, restarting all DL GPRS!\n");
785
786 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
787 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
788 "bucket leak rate of 0, stopping DL GPRS!\n");
789 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
790 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
791 "bucket leak rate != 0, restarting DL GPRS!\n");
792
793 /* reconfigure the timer for flow control based on new values */
794 fc_queue_timer_cfg(bctx->fc);
795
Harald Welte9ba50052010-03-14 15:45:01 +0800796 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200797 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200798 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800799}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200800
Harald Welte25de8112010-05-13 21:26:28 +0200801/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800802static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
803 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800804{
Harald Welteec19c102010-05-02 09:50:42 +0200805 struct bssgp_normal_hdr *bgph =
806 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200807 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800808 int rc = 0;
809
Harald Welte58e65c92010-05-13 21:45:23 +0200810 /* If traffic is received on a BVC that is marked as blocked, the
811 * received PDU shall not be accepted and a STATUS PDU (Cause value:
812 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
813 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
814 uint16_t bvci = msgb_bvci(msg);
815 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
816 }
817
Harald Welte9ba50052010-03-14 15:45:01 +0800818 switch (pdu_type) {
819 case BSSGP_PDUT_UL_UNITDATA:
820 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200821 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800822 break;
823 case BSSGP_PDUT_RA_CAPABILITY:
824 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200825 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
826 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200827 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800828 /* FIXME: send RA_CAPA_UPDATE_ACK */
829 break;
830 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200831 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800832 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200833 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800834 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800835 case BSSGP_PDUT_FLOW_CONTROL_BVC:
836 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200837 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800838 break;
839 case BSSGP_PDUT_FLOW_CONTROL_MS:
840 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200841 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
842 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200843 /* FIXME: actually implement flow control */
844 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800845 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800846 case BSSGP_PDUT_STATUS:
847 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200848 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800849 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
850 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
851 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
852 case BSSGP_PDUT_MODIFY_BSS_PFC:
853 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200854 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x not [yet] "
855 "implemented\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200856 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800857 break;
858 /* those only exist in the SGSN -> BSS direction */
859 case BSSGP_PDUT_DL_UNITDATA:
860 case BSSGP_PDUT_PAGING_PS:
861 case BSSGP_PDUT_PAGING_CS:
862 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200863 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
864 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200865 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x only exists "
866 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200867 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
868 rc = -EINVAL;
869 break;
870 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200871 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n",
872 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200873 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
874 break;
875 }
876
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800877 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200878}
879
880/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800881static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
882 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200883{
884 struct bssgp_normal_hdr *bgph =
885 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
886 uint8_t pdu_type = bgph->pdu_type;
887 int rc = 0;
888 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200889 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200890
891 switch (bgph->pdu_type) {
892 case BSSGP_PDUT_SUSPEND:
893 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200894 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200895 break;
896 case BSSGP_PDUT_RESUME:
897 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200898 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200899 break;
900 case BSSGP_PDUT_FLUSH_LL_ACK:
901 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200902 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200903 /* FIXME: send NM_FLUSH_LL.res to NM */
904 break;
905 case BSSGP_PDUT_LLC_DISCARD:
906 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200907 if (!bctx) {
908 LOGP(DBSSGP, LOGL_ERROR,
909 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
910 goto err_mand_ie;
911 }
Harald Weltee9686b62010-05-31 18:07:17 +0200912 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200913 break;
914 case BSSGP_PDUT_BVC_BLOCK:
915 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200916 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200917 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
918 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
919 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200920 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200921 }
Harald Welte2677ea52010-05-31 17:16:36 +0200922 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200923 break;
924 case BSSGP_PDUT_BVC_UNBLOCK:
925 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200926 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
927 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
928 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200929 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200930 }
Harald Welte25de8112010-05-13 21:26:28 +0200931 rc = bssgp_rx_bvc_unblock(msg, tp);
932 break;
933 case BSSGP_PDUT_BVC_RESET:
934 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200935 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200936 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
937 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
938 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200939 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200940 }
Harald Welte25de8112010-05-13 21:26:28 +0200941 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
942 break;
943 case BSSGP_PDUT_STATUS:
944 /* Some exception has occurred */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200945 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200946 /* FIXME: send NM_STATUS.ind to NM */
947 break;
948 /* those only exist in the SGSN -> BSS direction */
949 case BSSGP_PDUT_PAGING_PS:
950 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800951 case BSSGP_PDUT_SUSPEND_ACK:
952 case BSSGP_PDUT_SUSPEND_NACK:
953 case BSSGP_PDUT_RESUME_ACK:
954 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200955 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800956 case BSSGP_PDUT_BVC_BLOCK_ACK:
957 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
958 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltee9686b62010-05-31 18:07:17 +0200959 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x only exists "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200960 "in DL\n", bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200961 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800962 rc = -EINVAL;
963 break;
964 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200965 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200966 bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200967 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800968 break;
969 }
970
971 return rc;
972err_mand_ie:
973 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
974}
975
Harald Welte25de8112010-05-13 21:26:28 +0200976/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +0800977int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +0200978{
979 struct bssgp_normal_hdr *bgph =
980 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
981 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
982 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200983 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200984 uint8_t pdu_type = bgph->pdu_type;
985 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +0200986 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200987 int data_len;
988 int rc = 0;
989
990 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
991
992 /* UNITDATA BSSGP headers have TLLI in front */
993 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
994 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
995 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
996 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
997 } else {
998 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
999 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1000 }
1001
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001002 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1003 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
1004
Harald Welte25de8112010-05-13 21:26:28 +02001005 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001006 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001007 /* Only a RESET PDU can create a new BVC context,
1008 * otherwise it must be registered if a BVCI is given */
1009 if (!bctx && bvci != BVCI_SIGNALLING &&
1010 pdu_type != BSSGP_PDUT_BVC_RESET) {
Harald Welte25de8112010-05-13 21:26:28 +02001011 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001012 "type %u for unknown BVCI\n", msgb_nsei(msg), bvci,
Harald Welte25de8112010-05-13 21:26:28 +02001013 pdu_type);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001014 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
Harald Welte25de8112010-05-13 21:26:28 +02001015 }
1016
Harald Welte16c8dbb2010-05-17 23:30:01 +02001017 if (bctx) {
Harald Weltecca49632012-06-16 17:45:59 +08001018 log_set_context(GPRS_CTX_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001019 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1020 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1021 msgb_bssgp_len(msg));
1022 }
1023
Harald Welte61c07842010-05-18 11:57:08 +02001024 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001025 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001026 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001027 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
1028 else
Harald Weltede4599c2012-06-17 13:04:02 +08001029 rc = bssgp_rx_ptp(msg, &tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +02001030
1031 return rc;
1032}
1033
Harald Weltede4599c2012-06-17 13:04:02 +08001034int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1035 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001036{
Harald Welte8a521132010-05-17 22:59:29 +02001037 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001038 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001039 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001040 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001041 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001042 uint16_t bvci = msgb_bvci(msg);
1043 uint16_t nsei = msgb_nsei(msg);
Harald Welte8ef54d12012-06-17 09:31:16 +08001044 uint16_t _pdu_lifetime = htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001045 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001046
Harald Welte30bc19a2010-05-02 11:19:37 +02001047 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001048 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001049 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001050 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001051 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001052 return -EINVAL;
1053 }
1054
1055 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001056 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001057 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1058 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001059 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001060 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001061 }
Harald Welte9ba50052010-03-14 15:45:01 +08001062
1063 if (msg->len > TVLV_MAX_ONEBYTE)
1064 llc_pdu_tlv_hdr_len += 1;
1065
1066 /* prepend the tag and length of the LLC-PDU TLV */
1067 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1068 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1069 if (llc_pdu_tlv_hdr_len > 2) {
1070 llc_pdu_tlv[1] = msg_len >> 8;
1071 llc_pdu_tlv[2] = msg_len & 0xff;
1072 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001073 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001074 llc_pdu_tlv[1] |= 0x80;
1075 }
1076
Harald Welte2f946832010-05-31 22:12:30 +02001077 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1078
Harald Welte8ef54d12012-06-17 09:31:16 +08001079 if (dup) {
Harald Welte2f946832010-05-31 22:12:30 +02001080 /* Old TLLI to help BSS map from old->new */
Harald Welte8ef54d12012-06-17 09:31:16 +08001081 if (dup->tlli) {
1082 uint32_t tlli = htonl(*dup->tlli);
1083 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
1084 }
Harald Welte2f946832010-05-31 22:12:30 +02001085
1086 /* IMSI */
Harald Welte2d956a82012-07-04 21:55:23 +02001087 if (dup->imsi && strlen(dup->imsi)) {
Harald Welte2f946832010-05-31 22:12:30 +02001088 uint8_t mi[10];
Harald Welte8ef54d12012-06-17 09:31:16 +08001089 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
Harald Welte2f946832010-05-31 22:12:30 +02001090 if (imsi_len > 2)
1091 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
Harald Welte8ef54d12012-06-17 09:31:16 +08001092 imsi_len-2, mi+2);
Harald Welte2f946832010-05-31 22:12:30 +02001093 }
1094
1095 /* DRX parameters */
Harald Welte8ef54d12012-06-17 09:31:16 +08001096 drx_params = htons(dup->drx_parms);
Harald Welte2f946832010-05-31 22:12:30 +02001097 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1098 (uint8_t *) &drx_params);
1099
1100 /* FIXME: Priority */
1101
1102 /* MS Radio Access Capability */
Harald Welte8ef54d12012-06-17 09:31:16 +08001103 if (dup->ms_ra_cap.len)
Harald Welte2f946832010-05-31 22:12:30 +02001104 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
Harald Welte8ef54d12012-06-17 09:31:16 +08001105 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1106
Harald Welte2f946832010-05-31 22:12:30 +02001107 }
Harald Welte9ba50052010-03-14 15:45:01 +08001108
1109 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001110 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001111
1112 /* prepend the QoS profile, TLLI and pdu type */
1113 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001114 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Welte510c3922010-04-30 16:33:12 +02001115 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001116 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1117
Harald Welte16c8dbb2010-05-17 23:30:01 +02001118 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1119 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1120
Harald Welte30bc19a2010-05-02 11:19:37 +02001121 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001122
Harald Welted11c0592012-09-06 21:57:11 +02001123 /* check if we have to go through per-ms flow control or can go
1124 * directly to the per-BSS flow control */
1125 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001126 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001127 else
Harald Welted8b47692012-09-07 11:29:32 +02001128 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001129}
Harald Welte68b4f032010-06-09 16:22:28 +02001130
1131/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001132int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1133 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001134{
1135 struct msgb *msg = bssgp_msgb_alloc();
1136 struct bssgp_normal_hdr *bgph =
1137 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
1138 uint16_t drx_params = htons(pinfo->drx_params);
1139 uint8_t mi[10];
1140 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1141 uint8_t ra[6];
1142
1143 if (imsi_len < 2)
1144 return -EINVAL;
1145
1146 msgb_nsei(msg) = nsei;
1147 msgb_bvci(msg) = ns_bvci;
1148
1149 if (pinfo->mode == BSSGP_PAGING_PS)
1150 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1151 else
1152 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1153 /* IMSI */
1154 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1155 /* DRX Parameters */
1156 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1157 (uint8_t *) &drx_params);
1158 /* Scope */
1159 switch (pinfo->scope) {
1160 case BSSGP_PAGING_BSS_AREA:
1161 {
1162 uint8_t null = 0;
1163 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1164 }
1165 break;
1166 case BSSGP_PAGING_LOCATION_AREA:
1167 gsm48_construct_ra(ra, &pinfo->raid);
1168 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1169 break;
1170 case BSSGP_PAGING_ROUTEING_AREA:
1171 gsm48_construct_ra(ra, &pinfo->raid);
1172 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1173 break;
1174 case BSSGP_PAGING_BVCI:
1175 {
1176 uint16_t bvci = htons(pinfo->bvci);
1177 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1178 }
1179 break;
1180 }
1181 /* QoS profile mandatory for PS */
1182 if (pinfo->mode == BSSGP_PAGING_PS)
1183 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1184
1185 /* Optional (P-)TMSI */
1186 if (pinfo->ptmsi) {
1187 uint32_t ptmsi = htonl(*pinfo->ptmsi);
1188 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1189 }
1190
1191 return gprs_ns_sendmsg(bssgp_nsi, msg);
1192}
Harald Weltecca49632012-06-16 17:45:59 +08001193
Harald Weltede4599c2012-06-17 13:04:02 +08001194void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001195{
1196 DBSSGP = ss;
1197}