blob: 4c93b694462e454e723884e55b80d5a4e39a2733 [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 Welte7fa89c22014-10-26 20:33:09 +01008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 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 Welte7fa89c22014-10-26 20:33:09 +010015 * GNU General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080016 *
Harald Welte7fa89c22014-10-26 20:33:09 +010017 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010018 * 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" },
Jacob Erlbeck36153dc2015-03-17 10:21:17 +010049 { "status", "BVC Status count" },
Harald Welte25de8112010-05-13 21:26:28 +020050};
51
52static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
53 .group_name_prefix = "bssgp.bss_ctx",
54 .group_description = "BSSGP Peer Statistics",
55 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
56 .ctr_desc = bssgp_ctr_description,
57};
58
Harald Weltea78b9c22010-05-17 23:02:42 +020059LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020060
Harald Welted11c0592012-09-06 21:57:11 +020061static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
62 uint32_t llc_pdu_len, void *priv);
63
Harald Welte6752fa42010-05-02 09:23:16 +020064/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020065struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020066{
Harald Welte8a521132010-05-17 22:59:29 +020067 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020068
Harald Weltea78b9c22010-05-17 23:02:42 +020069 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020070 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
71 bctx->cell_id == cid)
72 return bctx;
73 }
74 return NULL;
75}
76
77/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +020078struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020079{
Harald Welte8a521132010-05-17 22:59:29 +020080 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020081
Harald Weltea78b9c22010-05-17 23:02:42 +020082 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020083 if (bctx->nsei == nsei && bctx->bvci == bvci)
84 return bctx;
85 }
86 return NULL;
87}
88
Harald Welte8a521132010-05-17 22:59:29 +020089struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020090{
Harald Welte8a521132010-05-17 22:59:29 +020091 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +020092
Harald Welte8a521132010-05-17 22:59:29 +020093 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +020094 if (!ctx)
95 return NULL;
96 ctx->bvci = bvci;
97 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +020098 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
99 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
Harald Welted8b47692012-09-07 11:29:32 +0200100 ctx->fc = talloc_zero(ctx, struct bssgp_flow_control);
101 /* cofigure for 2Mbit, 30 packets in queue */
102 bssgp_fc_init(ctx->fc, 100000, 2*1024*1024/8, 30, &_bssgp_tx_dl_ud);
Harald Welte25de8112010-05-13 21:26:28 +0200103
Harald Weltea78b9c22010-05-17 23:02:42 +0200104 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200105
106 return ctx;
107}
108
Harald Welte9ba50052010-03-14 15:45:01 +0800109/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200110static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800111{
112 struct msgb *msg = bssgp_msgb_alloc();
113 struct bssgp_normal_hdr *bgph =
114 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
115
Harald Welte24a655f2010-04-30 19:54:29 +0200116 msgb_nsei(msg) = nsei;
117 msgb_bvci(msg) = ns_bvci;
118
Harald Welte9ba50052010-03-14 15:45:01 +0800119 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
120 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
121
Harald Welte24a655f2010-04-30 19:54:29 +0200122 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800123}
124
Harald Weltea8aa4df2010-05-30 22:00:53 +0200125/* 10.3.7 SUSPEND-ACK PDU */
126int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
127 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
128{
129 struct msgb *msg = bssgp_msgb_alloc();
130 struct bssgp_normal_hdr *bgph =
131 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
132 uint32_t _tlli;
133 uint8_t ra[6];
134
135 msgb_nsei(msg) = nsei;
136 msgb_bvci(msg) = 0; /* Signalling */
137 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
138
139 _tlli = htonl(tlli);
140 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
141 gsm48_construct_ra(ra, ra_id);
142 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
143 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
144
145 return gprs_ns_sendmsg(bssgp_nsi, msg);
146}
147
148/* 10.3.8 SUSPEND-NACK PDU */
149int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100150 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200151 uint8_t *cause)
152{
153 struct msgb *msg = bssgp_msgb_alloc();
154 struct bssgp_normal_hdr *bgph =
155 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
156 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100157 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200158
159 msgb_nsei(msg) = nsei;
160 msgb_bvci(msg) = 0; /* Signalling */
161 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
162
163 _tlli = htonl(tlli);
164 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100165 gsm48_construct_ra(ra, ra_id);
166 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200167 if (cause)
168 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
169
170 return gprs_ns_sendmsg(bssgp_nsi, msg);
171}
172
173/* 10.3.10 RESUME-ACK PDU */
174int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
175 const struct gprs_ra_id *ra_id)
176{
177 struct msgb *msg = bssgp_msgb_alloc();
178 struct bssgp_normal_hdr *bgph =
179 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
180 uint32_t _tlli;
181 uint8_t ra[6];
182
183 msgb_nsei(msg) = nsei;
184 msgb_bvci(msg) = 0; /* Signalling */
185 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
186
187 _tlli = htonl(tlli);
188 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
189 gsm48_construct_ra(ra, ra_id);
190 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
191
192 return gprs_ns_sendmsg(bssgp_nsi, msg);
193}
194
195/* 10.3.11 RESUME-NACK PDU */
196int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
197 const struct gprs_ra_id *ra_id, uint8_t *cause)
198{
199 struct msgb *msg = bssgp_msgb_alloc();
200 struct bssgp_normal_hdr *bgph =
201 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
202 uint32_t _tlli;
203 uint8_t ra[6];
204
205 msgb_nsei(msg) = nsei;
206 msgb_bvci(msg) = 0; /* Signalling */
207 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
208
209 _tlli = htonl(tlli);
210 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
211 gsm48_construct_ra(ra, ra_id);
212 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
213 if (cause)
214 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
215
216 return gprs_ns_sendmsg(bssgp_nsi, msg);
217}
218
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200219uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200220{
221 /* 6 octets RAC */
222 gsm48_parse_ra(raid, buf);
223 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200224 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200225}
226
Harald Welte28610072011-11-24 21:32:07 +0100227int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
228 uint16_t cid)
229{
230 uint16_t *out_cid = (uint16_t *) (buf + 6);
231 /* 6 octets RAC */
232 gsm48_construct_ra(buf, raid);
233 /* 2 octets CID */
234 *out_cid = htons(cid);
235
236 return 8;
237}
238
Harald Welte3fddf3c2010-05-01 16:48:27 +0200239/* Chapter 8.4 BVC-Reset Procedure */
240static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
241 uint16_t ns_bvci)
242{
Harald Welte15a36432012-06-17 12:16:31 +0800243 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200244 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200245 uint16_t nsei = msgb_nsei(msg);
246 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200247
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200248 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Weltee9686b62010-05-31 18:07:17 +0200249 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200250 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
251
Harald Welte6752fa42010-05-02 09:23:16 +0200252 /* look-up or create the BTS context for this BVC */
253 bctx = btsctx_by_bvci_nsei(bvci, nsei);
254 if (!bctx)
255 bctx = btsctx_alloc(bvci, nsei);
256
Harald Welte25de8112010-05-13 21:26:28 +0200257 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
258 bctx->state &= ~BVC_S_BLOCKED;
259
Harald Welte3fddf3c2010-05-01 16:48:27 +0200260 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
261 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200262 if (bvci != 0 && bvci != 1) {
263 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200264 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200265 "missing mandatory IE\n", bvci);
266 return -EINVAL;
267 }
268 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200269 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
270 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200271 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200272 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
273 bctx->ra_id.rac, bctx->cell_id, bvci);
274 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200275
Harald Welte15a36432012-06-17 12:16:31 +0800276 /* Send NM_BVC_RESET.ind to NM */
277 memset(&nmp, 0, sizeof(nmp));
278 nmp.nsei = nsei;
279 nmp.bvci = bvci;
280 nmp.tp = tp;
281 nmp.ra_id = &bctx->ra_id;
282 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
283 PRIM_OP_INDICATION, msg);
284 bssgp_prim_cb(&nmp.oph, NULL);
285
Harald Welte6752fa42010-05-02 09:23:16 +0200286 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200287 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
288 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200289 return 0;
290}
291
Harald Welte25de8112010-05-13 21:26:28 +0200292static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
293{
Harald Welte15a36432012-06-17 12:16:31 +0800294 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200295 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200296 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200297
298 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200299 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200300 /* 8.3.2: Signalling BVC shall never be blocked */
301 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
302 "received block for signalling BVC!?!\n",
303 msgb_nsei(msg), msgb_bvci(msg));
304 return 0;
305 }
Harald Welte25de8112010-05-13 21:26:28 +0200306
Harald Welte086fe322011-08-19 16:45:19 +0200307 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200308
309 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
310 if (!ptp_ctx)
311 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
312
313 ptp_ctx->state |= BVC_S_BLOCKED;
314 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
315
Harald Welte15a36432012-06-17 12:16:31 +0800316 /* Send NM_BVC_BLOCK.ind to NM */
317 memset(&nmp, 0, sizeof(nmp));
318 nmp.nsei = msgb_nsei(msg);
319 nmp.bvci = bvci;
320 nmp.tp = tp;
321 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
322 PRIM_OP_INDICATION, msg);
323 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200324
325 /* We always acknowledge the BLOCKing */
326 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
327 bvci, msgb_bvci(msg));
328};
329
330static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
331{
Harald Welte15a36432012-06-17 12:16:31 +0800332 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200333 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200334 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200335
336 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200337 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200338 /* 8.3.2: Signalling BVC shall never be blocked */
339 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
340 "received unblock for signalling BVC!?!\n",
341 msgb_nsei(msg), msgb_bvci(msg));
342 return 0;
343 }
Harald Welte25de8112010-05-13 21:26:28 +0200344
Harald Weltee9686b62010-05-31 18:07:17 +0200345 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200346
347 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
348 if (!ptp_ctx)
349 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
350
351 ptp_ctx->state &= ~BVC_S_BLOCKED;
352
Harald Welte15a36432012-06-17 12:16:31 +0800353 /* Send NM_BVC_UNBLOCK.ind to NM */
354 memset(&nmp, 0, sizeof(nmp));
355 nmp.nsei = msgb_nsei(msg);
356 nmp.bvci = bvci;
357 nmp.tp = tp;
358 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
359 PRIM_OP_INDICATION, msg);
360 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200361
362 /* We always acknowledge the unBLOCKing */
363 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
364 bvci, msgb_bvci(msg));
365};
366
Harald Welte9ba50052010-03-14 15:45:01 +0800367/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200368static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200369 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800370{
Harald Welte15a36432012-06-17 12:16:31 +0800371 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200372 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800373
Harald Welte6752fa42010-05-02 09:23:16 +0200374 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200375 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800376
Harald Welte086fe322011-08-19 16:45:19 +0200377 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200378
Harald Welte9ba50052010-03-14 15:45:01 +0800379 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200380 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200381 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
382 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
383 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200384 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200385 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200386
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200387 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800388 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
389 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800390
Harald Welte15a36432012-06-17 12:16:31 +0800391 /* Send BSSGP_UL_UD.ind to NM */
392 memset(&gbp, 0, sizeof(gbp));
393 gbp.nsei = ctx->nsei;
394 gbp.bvci = ctx->bvci;
395 gbp.tlli = msgb_tlli(msg);
396 gbp.tp = tp;
397 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
398 PRIM_OP_INDICATION, msg);
399 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800400}
401
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200402static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800403{
Harald Welte15a36432012-06-17 12:16:31 +0800404 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200405 struct gprs_ra_id raid;
406 uint32_t tlli;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200407 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200408 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800409
Harald Welte25de8112010-05-13 21:26:28 +0200410 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200411 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
412 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200413 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200414 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200415 }
Harald Welte9ba50052010-03-14 15:45:01 +0800416
Harald Weltea8aa4df2010-05-30 22:00:53 +0200417 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200418
Harald Welte17925322010-05-31 20:18:35 +0200419 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200420 ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200421
Harald Weltea8aa4df2010-05-30 22:00:53 +0200422 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
423
Harald Welte313cccf2010-06-09 11:22:47 +0200424 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800425 memset(&gbp, 0, sizeof(gbp));
426 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200427 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800428 gbp.tlli = tlli;
429 gbp.ra_id = &raid;
430 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
431 PRIM_OP_REQUEST, msg);
432
433 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200434 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100435 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200436
Harald Weltea8aa4df2010-05-30 22:00:53 +0200437 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
438
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800439 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800440}
441
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200442static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800443{
Harald Welte15a36432012-06-17 12:16:31 +0800444 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200445 struct gprs_ra_id raid;
446 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200447 uint8_t suspend_ref;
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200448 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte313cccf2010-06-09 11:22:47 +0200449 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800450
Harald Welte25de8112010-05-13 21:26:28 +0200451 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
452 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200453 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
454 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200455 "missing mandatory IE\n", ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200456 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200457 }
Harald Welte9ba50052010-03-14 15:45:01 +0800458
Harald Weltea8aa4df2010-05-30 22:00:53 +0200459 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Welte313cccf2010-06-09 11:22:47 +0200460 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200461
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200462 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ns_bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200463
Harald Weltea8aa4df2010-05-30 22:00:53 +0200464 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
465
Harald Welte313cccf2010-06-09 11:22:47 +0200466 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800467 memset(&gbp, 0, sizeof(gbp));
468 gbp.nsei = msgb_nsei(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200469 gbp.bvci = ns_bvci;
Harald Welte15a36432012-06-17 12:16:31 +0800470 gbp.tlli = tlli;
471 gbp.ra_id = &raid;
472 gbp.u.resume.suspend_ref = suspend_ref;
473 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
474 PRIM_OP_REQUEST, msg);
475
476 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200477 if (rc < 0)
478 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
479 NULL);
480
Harald Weltea8aa4df2010-05-30 22:00:53 +0200481 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800482 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800483}
484
Harald Weltee9686b62010-05-31 18:07:17 +0200485
486static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
487 struct bssgp_bvc_ctx *ctx)
488{
Harald Welte15a36432012-06-17 12:16:31 +0800489 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200490 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200491
492 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
493 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
494 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
495 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
496 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
497 "missing mandatory IE\n", ctx->bvci);
498 }
499
Harald Welteb7363142010-07-23 21:59:29 +0200500 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
501 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200502
Harald Welte086fe322011-08-19 16:45:19 +0200503 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200504 ctx->bvci, tlli);
505
506 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
507
Harald Welte15a36432012-06-17 12:16:31 +0800508 /* send NM_LLC_DISCARDED to NM */
509 memset(&nmp, 0, sizeof(nmp));
510 nmp.nsei = msgb_nsei(msg);
511 nmp.bvci = ctx->bvci;
512 nmp.tlli = tlli;
513 nmp.tp = tp;
514 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
515 PRIM_OP_INDICATION, msg);
516
517 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200518}
519
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100520int bssgp_rx_status(struct msgb *msg, struct tlv_parsed *tp,
521 uint16_t bvci, struct bssgp_bvc_ctx *bctx)
522{
523 struct osmo_bssgp_prim nmp;
524 enum gprs_bssgp_cause cause;
525
526 if (!TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
527 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx STATUS "
528 "missing mandatory IE\n", bvci);
529 cause = BSSGP_CAUSE_PROTO_ERR_UNSPEC;
530 } else {
531 cause = *TLVP_VAL(tp, BSSGP_IE_CAUSE);
532 }
533
534 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Rx BVC STATUS, cause=%s\n",
535 bvci, bssgp_cause_str(cause));
536
537 if (cause == BSSGP_CAUSE_BVCI_BLOCKED || cause == BSSGP_CAUSE_UNKNOWN_BVCI) {
538 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
539 LOGP(DBSSGP, LOGL_ERROR,
540 "BSSGP BVCI=%u Rx STATUS cause=%s "
541 "missing conditional BVCI IE\n",
542 bvci, bssgp_cause_str(cause));
543 }
544
545 if (bctx)
546 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_STATUS]);
547
548 /* send NM_STATUS to NM */
549 memset(&nmp, 0, sizeof(nmp));
550 nmp.nsei = msgb_nsei(msg);
551 nmp.bvci = bvci;
552 nmp.tp = tp;
553 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_STATUS,
554 PRIM_OP_INDICATION, msg);
555
556 return bssgp_prim_cb(&nmp.oph, NULL);
557}
558
559
Harald Welted11c0592012-09-06 21:57:11 +0200560/* One element (msgb) in a BSSGP Flow Control queue */
561struct bssgp_fc_queue_element {
562 /* linked list of queue elements */
563 struct llist_head list;
564 /* The message that we have enqueued */
565 struct msgb *msg;
566 /* Length of the LLC PDU part of the contained message */
567 uint32_t llc_pdu_len;
568 /* private pointer passed to the flow control out_cb function */
569 void *priv;
570};
571
572static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
573static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
574
575static void fc_timer_cb(void *data)
576{
577 struct bssgp_flow_control *fc = data;
578 struct bssgp_fc_queue_element *fcqe;
579 struct timeval time_now;
580
581 /* if the queue is empty, we return without sending something
582 * and without re-starting the timer */
583 if (llist_empty(&fc->queue))
584 return;
585
586 /* get the first entry from the queue */
587 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
588 list);
589
590 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
591 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
592 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
593 /* make sure we re-start the timer */
594 fc_queue_timer_cfg(fc);
595 return;
596 }
597
598 /* remove from the queue */
599 llist_del(&fcqe->list);
600
601 fc->queue_depth--;
602
603 /* record the time we transmitted this PDU */
604 gettimeofday(&time_now, NULL);
605 fc->time_last_pdu = time_now;
606
607 /* call the output callback for this FC instance */
608 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
609
610 /* we expect that out_cb will in the end free the msgb once
611 * it is no longer needed */
612
613 /* but we have to free the queue element ourselves */
614 talloc_free(fcqe);
615
616 /* re-configure the timer for the next PDU */
617 fc_queue_timer_cfg(fc);
618}
619
620/* configure/schedule the flow control timer to expire once the bucket
621 * will have leaked a sufficient number of bytes to transmit the next
622 * PDU in the queue */
623static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
624{
625 struct bssgp_fc_queue_element *fcqe;
626 uint32_t msecs;
627
628 if (llist_empty(&fc->queue))
629 return 0;
630
631 fcqe = llist_entry(&fc->queue.next, struct bssgp_fc_queue_element,
632 list);
633
Harald Welte27b2bb72013-06-22 09:44:00 +0200634 if (fc->bucket_leak_rate != 0) {
635 /* Calculate the point in time at which we will have leaked
636 * a sufficient number of bytes from the bucket to transmit
637 * the first PDU in the queue */
638 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
639 /* FIXME: add that time to fc->time_last_pdu and subtract it from
640 * current time */
641 fc->timer.data = fc;
642 fc->timer.cb = &fc_timer_cb;
643 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
644 } else {
645 /* If the PCU is telling us to not send any more data at all,
646 * there's no point starting a timer. */
647 }
Harald Welted11c0592012-09-06 21:57:11 +0200648
649 return 0;
650}
651
652/* Enqueue a PDU in the flow control queue for delayed transmission */
653static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
654 uint32_t llc_pdu_len, void *priv)
655{
656 struct bssgp_fc_queue_element *fcqe;
657
658 if (fc->queue_depth >= fc->max_queue_depth)
659 return -ENOSPC;
660
661 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
662 if (!fcqe)
663 return -ENOMEM;
664 fcqe->msg = msg;
665 fcqe->llc_pdu_len = llc_pdu_len;
666 fcqe->priv = priv;
667
668 llist_add_tail(&fcqe->list, &fc->queue);
669
670 fc->queue_depth++;
671
672 /* re-configure the timer for dequeueing the pdu */
673 fc_queue_timer_cfg(fc);
674
675 return 0;
676}
677
678/* According to Section 8.2 */
679static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
680{
681 struct timeval time_now, time_diff;
682 int64_t bucket_predicted;
683 uint32_t csecs_elapsed, leaked;
684
685 /* B' = B + L(p) - (Tc - Tp)*R */
686
687 /* compute number of centi-seconds that have elapsed since transmitting
688 * the last PDU (Tc - Tp) */
689 gettimeofday(&time_now, NULL);
690 timersub(&time_now, &fc->time_last_pdu, &time_diff);
691 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
692
693 /* compute number of bytes that have leaked in the elapsed number
694 * of centi-seconds */
695 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
696 /* add the current PDU length to the last bucket level */
697 bucket_predicted = fc->bucket_counter + pdu_len;
698 /* ... and subtract the number of leaked bytes */
699 bucket_predicted -= leaked;
700
701 if (bucket_predicted < pdu_len) {
702 /* this is just to make sure the bucket doesn't underflow */
703 bucket_predicted = pdu_len;
704 goto pass;
705 }
706
707 if (bucket_predicted <= fc->bucket_size_max) {
708 /* the bucket is not full yet, we can pass the packet */
709 fc->bucket_counter = bucket_predicted;
710 goto pass;
711 }
712
713 /* bucket is full, PDU needs to be delayed */
714 return 1;
715
716pass:
717 /* if we reach here, the PDU can pass */
718 return 0;
719}
720
721/* output callback for BVC flow control */
722static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
723 uint32_t llc_pdu_len, void *priv)
724{
725 return gprs_ns_sendmsg(bssgp_nsi, msg);
726}
727
728/* input function of the flow control implementation, called first
729 * for the MM flow control, and then as the MM flow control output
730 * callback in order to perform BVC flow control */
731int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
732 uint32_t llc_pdu_len, void *priv)
733{
734 struct timeval time_now;
735
Harald Weltebb826222012-09-07 10:22:01 +0200736 if (llc_pdu_len > fc->bucket_size_max) {
737 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
738 "than maximum bucket size (%u)!\n", llc_pdu_len,
739 fc->bucket_size_max);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +0200740 msgb_free(msg);
Harald Weltebb826222012-09-07 10:22:01 +0200741 return -EIO;
742 }
743
Harald Welted11c0592012-09-06 21:57:11 +0200744 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
745 return fc_enqueue(fc, msg, llc_pdu_len, priv);
746 } else {
747 /* record the time we transmitted this PDU */
748 gettimeofday(&time_now, NULL);
749 fc->time_last_pdu = time_now;
750 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
751 }
752}
753
Harald Weltebb826222012-09-07 10:22:01 +0200754
755/* Initialize the Flow Control structure */
756void bssgp_fc_init(struct bssgp_flow_control *fc,
757 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
758 uint32_t max_queue_depth,
759 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
760 uint32_t llc_pdu_len, void *priv))
761{
762 fc->out_cb = out_cb;
763 fc->bucket_size_max = bucket_size_max;
764 fc->bucket_leak_rate = bucket_leak_rate;
765 fc->max_queue_depth = max_queue_depth;
766 INIT_LLIST_HEAD(&fc->queue);
767 gettimeofday(&fc->time_last_pdu, NULL);
768}
769
Harald Welted11c0592012-09-06 21:57:11 +0200770/* Initialize the Flow Control parameters for a new MS according to
771 * default values for the BVC specified by BVCI and NSEI */
772int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200773 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200774{
775 struct bssgp_bvc_ctx *ctx;
776
777 ctx = btsctx_by_bvci_nsei(bvci, nsei);
778 if (!ctx)
779 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200780
781 /* output call-back of per-MS FC is per-CTX FC */
782 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
783 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200784
785 return 0;
786}
787
Harald Welte25de8112010-05-13 21:26:28 +0200788static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200789 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800790{
Harald Welte27b2bb72013-06-22 09:44:00 +0200791 uint32_t old_leak_rate = bctx->fc->bucket_leak_rate;
792 uint32_t old_r_def_ms = bctx->r_default_ms;
Harald Welte9ba50052010-03-14 15:45:01 +0800793
Harald Weltee9686b62010-05-31 18:07:17 +0200794 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
795 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800796
797 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
798 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
799 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
800 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200801 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
802 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
803 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800804 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200805 }
Harald Welte9ba50052010-03-14 15:45:01 +0800806
Harald Weltebb826222012-09-07 10:22:01 +0200807 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Welted8b47692012-09-07 11:29:32 +0200808 bctx->fc->bucket_size_max = 100 *
Harald Welted11c0592012-09-06 21:57:11 +0200809 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVC_BUCKET_SIZE));
Harald Weltebb826222012-09-07 10:22:01 +0200810 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Welted8b47692012-09-07 11:29:32 +0200811 bctx->fc->bucket_leak_rate = 100 *
Harald Weltebb826222012-09-07 10:22:01 +0200812 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BUCKET_LEAK_RATE)) / 8;
813 /* 11.3.2 in octets */
814 bctx->bmax_default_ms =
Harald Welted11c0592012-09-06 21:57:11 +0200815 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BMAX_DEFAULT_MS));
Harald Weltebb826222012-09-07 10:22:01 +0200816 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Welted11c0592012-09-06 21:57:11 +0200817 bctx->r_default_ms = 100 *
Harald Weltebb826222012-09-07 10:22:01 +0200818 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_R_DEFAULT_MS)) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200819
Harald Welte27b2bb72013-06-22 09:44:00 +0200820 if (old_leak_rate != 0 && bctx->fc->bucket_leak_rate == 0)
821 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
822 "rate of 0, stopping all DL GPRS!\n");
823 else if (old_leak_rate == 0 && bctx->fc->bucket_leak_rate != 0)
824 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to bucket leak "
825 "rate of != 0, restarting all DL GPRS!\n");
826
827 if (old_r_def_ms != 0 && bctx->r_default_ms == 0)
828 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
829 "bucket leak rate of 0, stopping DL GPRS!\n");
830 else if (old_r_def_ms == 0 && bctx->r_default_ms != 0)
831 LOGP(DBSSGP, LOGL_NOTICE, "BSS instructs us to MS default "
832 "bucket leak rate != 0, restarting DL GPRS!\n");
833
834 /* reconfigure the timer for flow control based on new values */
835 fc_queue_timer_cfg(bctx->fc);
836
Harald Welte9ba50052010-03-14 15:45:01 +0800837 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200838 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200839 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800840}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200841
Harald Welte25de8112010-05-13 21:26:28 +0200842/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800843static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
844 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800845{
Harald Welteec19c102010-05-02 09:50:42 +0200846 struct bssgp_normal_hdr *bgph =
847 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200848 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800849 int rc = 0;
850
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100851 OSMO_ASSERT(pdu_type != BSSGP_PDUT_STATUS);
852
Harald Welte58e65c92010-05-13 21:45:23 +0200853 /* If traffic is received on a BVC that is marked as blocked, the
854 * received PDU shall not be accepted and a STATUS PDU (Cause value:
855 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100856 if (bctx->state & BVC_S_BLOCKED) {
Harald Welte58e65c92010-05-13 21:45:23 +0200857 uint16_t bvci = msgb_bvci(msg);
858 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
859 }
860
Harald Welte9ba50052010-03-14 15:45:01 +0800861 switch (pdu_type) {
862 case BSSGP_PDUT_UL_UNITDATA:
863 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200864 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800865 break;
866 case BSSGP_PDUT_RA_CAPABILITY:
867 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200868 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
869 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200870 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800871 /* FIXME: send RA_CAPA_UPDATE_ACK */
872 break;
873 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200874 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800875 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200876 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800877 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800878 case BSSGP_PDUT_FLOW_CONTROL_BVC:
879 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200880 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800881 break;
882 case BSSGP_PDUT_FLOW_CONTROL_MS:
883 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200884 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
885 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200886 /* FIXME: actually implement flow control */
887 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800888 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800889 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100890 /* This is already handled in bssgp_rcvmsg() */
Jacob Erlbeck49ed9be2015-03-17 10:21:16 +0100891 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800892 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
893 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
894 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
895 case BSSGP_PDUT_MODIFY_BSS_PFC:
896 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200897 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x not [yet] "
898 "implemented\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200899 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800900 break;
901 /* those only exist in the SGSN -> BSS direction */
902 case BSSGP_PDUT_DL_UNITDATA:
903 case BSSGP_PDUT_PAGING_PS:
904 case BSSGP_PDUT_PAGING_CS:
905 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200906 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
907 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200908 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x only exists "
909 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200910 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
911 rc = -EINVAL;
912 break;
913 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200914 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n",
915 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200916 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
917 break;
918 }
919
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800920 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200921}
922
923/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800924static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
925 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200926{
927 struct bssgp_normal_hdr *bgph =
928 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
929 uint8_t pdu_type = bgph->pdu_type;
930 int rc = 0;
931 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200932 uint16_t bvci = bctx ? bctx->bvci : ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +0200933
934 switch (bgph->pdu_type) {
935 case BSSGP_PDUT_SUSPEND:
936 /* MS wants to suspend */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200937 rc = bssgp_rx_suspend(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200938 break;
939 case BSSGP_PDUT_RESUME:
940 /* MS wants to resume */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200941 rc = bssgp_rx_resume(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200942 break;
943 case BSSGP_PDUT_FLUSH_LL_ACK:
944 /* BSS informs us it has performed LL FLUSH */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200945 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200946 /* FIXME: send NM_FLUSH_LL.res to NM */
947 break;
948 case BSSGP_PDUT_LLC_DISCARD:
949 /* BSS informs that some LLC PDU's have been discarded */
Jacob Erlbeckb43baf22014-09-10 12:43:28 +0200950 if (!bctx) {
951 LOGP(DBSSGP, LOGL_ERROR,
952 "BSSGP Rx LLC-DISCARD missing mandatory BVCI\n");
953 goto err_mand_ie;
954 }
Harald Weltee9686b62010-05-31 18:07:17 +0200955 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200956 break;
957 case BSSGP_PDUT_BVC_BLOCK:
958 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200959 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200960 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
961 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
962 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200963 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200964 }
Harald Welte2677ea52010-05-31 17:16:36 +0200965 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200966 break;
967 case BSSGP_PDUT_BVC_UNBLOCK:
968 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200969 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
970 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
971 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200972 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200973 }
Harald Welte25de8112010-05-13 21:26:28 +0200974 rc = bssgp_rx_bvc_unblock(msg, tp);
975 break;
976 case BSSGP_PDUT_BVC_RESET:
977 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200978 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200979 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
980 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
981 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200982 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200983 }
Harald Welte25de8112010-05-13 21:26:28 +0200984 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
985 break;
986 case BSSGP_PDUT_STATUS:
Jacob Erlbeck36153dc2015-03-17 10:21:17 +0100987 /* This is already handled in bssgp_rcvmsg() */
Harald Welte25de8112010-05-13 21:26:28 +0200988 break;
989 /* those only exist in the SGSN -> BSS direction */
990 case BSSGP_PDUT_PAGING_PS:
991 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800992 case BSSGP_PDUT_SUSPEND_ACK:
993 case BSSGP_PDUT_SUSPEND_NACK:
994 case BSSGP_PDUT_RESUME_ACK:
995 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200996 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800997 case BSSGP_PDUT_BVC_BLOCK_ACK:
998 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
999 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltee9686b62010-05-31 18:07:17 +02001000 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x only exists "
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001001 "in DL\n", bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +02001002 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001003 rc = -EINVAL;
1004 break;
1005 default:
Harald Weltee9686b62010-05-31 18:07:17 +02001006 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n",
Jacob Erlbeckb43baf22014-09-10 12:43:28 +02001007 bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +02001008 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +08001009 break;
1010 }
1011
1012 return rc;
1013err_mand_ie:
1014 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
1015}
1016
Harald Welte25de8112010-05-13 21:26:28 +02001017/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +08001018int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +02001019{
1020 struct bssgp_normal_hdr *bgph =
1021 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
1022 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
1023 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +02001024 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +02001025 uint8_t pdu_type = bgph->pdu_type;
1026 uint16_t ns_bvci = msgb_bvci(msg);
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001027 uint16_t bvci = ns_bvci;
Harald Welte25de8112010-05-13 21:26:28 +02001028 int data_len;
1029 int rc = 0;
1030
1031 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
1032
1033 /* UNITDATA BSSGP headers have TLLI in front */
1034 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
1035 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
1036 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
1037 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
1038 } else {
1039 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
1040 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
1041 }
1042
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001043 if (bvci == BVCI_SIGNALLING && TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
1044 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
1045
Harald Welte25de8112010-05-13 21:26:28 +02001046 /* look-up or create the BTS context for this BVC */
Jacob Erlbeckb83b8382014-09-23 13:28:22 +02001047 bctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
Harald Welte25de8112010-05-13 21:26:28 +02001048
Harald Welte16c8dbb2010-05-17 23:30:01 +02001049 if (bctx) {
Harald Weltecca49632012-06-16 17:45:59 +08001050 log_set_context(GPRS_CTX_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +02001051 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
1052 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
1053 msgb_bssgp_len(msg));
1054 }
1055
Jacob Erlbeck36153dc2015-03-17 10:21:17 +01001056 /* Always handle STATUS PDUs, even if they contain an invalid BVCI or
1057 * are otherwise unexpected */
1058 if (pdu_type == BSSGP_PDUT_STATUS)
1059 /* Some exception has occurred */
1060 return bssgp_rx_status(msg, &tp, bvci, bctx);
1061
1062 /* Only a RESET PDU can create a new BVC context, otherwise it must be
1063 * registered if a BVCI is given. */
1064 if (!bctx && bvci != BVCI_SIGNALLING &&
1065 pdu_type != BSSGP_PDUT_BVC_RESET) {
1066 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
1067 "type %u for unknown BVCI\n", msgb_nsei(msg), bvci,
1068 pdu_type);
1069 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
1070 }
1071
Harald Welte61c07842010-05-18 11:57:08 +02001072 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +08001073 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +02001074 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +02001075 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001076 else if (bctx)
Harald Weltede4599c2012-06-17 13:04:02 +08001077 rc = bssgp_rx_ptp(msg, &tp, bctx);
Jacob Erlbeckb535e392015-04-07 17:52:44 +02001078 else
1079 LOGP(DBSSGP, LOGL_NOTICE,
1080 "NSEI=%u/BVCI=%u Cannot handle PDU type %u for "
1081 "unknown BVCI, NS BVCI %u\n",
1082 msgb_nsei(msg), bvci, pdu_type, ns_bvci);
Harald Welte25de8112010-05-13 21:26:28 +02001083
1084 return rc;
1085}
1086
Harald Weltede4599c2012-06-17 13:04:02 +08001087int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
1088 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +08001089{
Harald Welte8a521132010-05-17 22:59:29 +02001090 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001091 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001092 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001093 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001094 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001095 uint16_t bvci = msgb_bvci(msg);
1096 uint16_t nsei = msgb_nsei(msg);
Harald Welte8ef54d12012-06-17 09:31:16 +08001097 uint16_t _pdu_lifetime = htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001098 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001099
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001100 OSMO_ASSERT(dup != NULL);
1101
Harald Welte30bc19a2010-05-02 11:19:37 +02001102 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001103 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001104 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001105 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001106 msgb_free(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +02001107 return -EINVAL;
1108 }
1109
1110 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001111 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001112 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1113 bvci);
Holger Hans Peter Freyther10dd73c2014-10-10 17:24:34 +02001114 msgb_free(msg);
Harald Welted11c0592012-09-06 21:57:11 +02001115 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001116 }
Harald Welte9ba50052010-03-14 15:45:01 +08001117
1118 if (msg->len > TVLV_MAX_ONEBYTE)
1119 llc_pdu_tlv_hdr_len += 1;
1120
1121 /* prepend the tag and length of the LLC-PDU TLV */
1122 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1123 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1124 if (llc_pdu_tlv_hdr_len > 2) {
1125 llc_pdu_tlv[1] = msg_len >> 8;
1126 llc_pdu_tlv[2] = msg_len & 0xff;
1127 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001128 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001129 llc_pdu_tlv[1] |= 0x80;
1130 }
1131
Harald Welte2f946832010-05-31 22:12:30 +02001132 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1133
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001134 /* Old TLLI to help BSS map from old->new */
1135 if (dup->tlli) {
1136 uint32_t tlli = htonl(*dup->tlli);
1137 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
Harald Welte2f946832010-05-31 22:12:30 +02001138 }
Harald Welte9ba50052010-03-14 15:45:01 +08001139
Jacob Erlbeckc6415912015-04-07 17:52:43 +02001140 /* IMSI */
1141 if (dup->imsi && strlen(dup->imsi)) {
1142 uint8_t mi[10];
1143 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
1144 if (imsi_len > 2)
1145 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
1146 imsi_len-2, mi+2);
1147 }
1148
1149 /* DRX parameters */
1150 drx_params = htons(dup->drx_parms);
1151 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1152 (uint8_t *) &drx_params);
1153
1154 /* FIXME: Priority */
1155
1156 /* MS Radio Access Capability */
1157 if (dup->ms_ra_cap.len)
1158 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
1159 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1160
Harald Welte9ba50052010-03-14 15:45:01 +08001161 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001162 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001163
1164 /* prepend the QoS profile, TLLI and pdu type */
1165 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001166 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Welte510c3922010-04-30 16:33:12 +02001167 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001168 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1169
Harald Welte16c8dbb2010-05-17 23:30:01 +02001170 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1171 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1172
Harald Welte30bc19a2010-05-02 11:19:37 +02001173 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001174
Harald Welted11c0592012-09-06 21:57:11 +02001175 /* check if we have to go through per-ms flow control or can go
1176 * directly to the per-BSS flow control */
1177 if (dup->fc)
Harald Welted8b47692012-09-07 11:29:32 +02001178 return bssgp_fc_in(dup->fc, msg, msg_len, bctx->fc);
Harald Welted11c0592012-09-06 21:57:11 +02001179 else
Harald Welted8b47692012-09-07 11:29:32 +02001180 return bssgp_fc_in(bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001181}
Harald Welte68b4f032010-06-09 16:22:28 +02001182
1183/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001184int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1185 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001186{
1187 struct msgb *msg = bssgp_msgb_alloc();
1188 struct bssgp_normal_hdr *bgph =
1189 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
1190 uint16_t drx_params = htons(pinfo->drx_params);
1191 uint8_t mi[10];
1192 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1193 uint8_t ra[6];
1194
1195 if (imsi_len < 2)
1196 return -EINVAL;
1197
1198 msgb_nsei(msg) = nsei;
1199 msgb_bvci(msg) = ns_bvci;
1200
1201 if (pinfo->mode == BSSGP_PAGING_PS)
1202 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1203 else
1204 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1205 /* IMSI */
1206 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1207 /* DRX Parameters */
1208 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1209 (uint8_t *) &drx_params);
1210 /* Scope */
1211 switch (pinfo->scope) {
1212 case BSSGP_PAGING_BSS_AREA:
1213 {
1214 uint8_t null = 0;
1215 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1216 }
1217 break;
1218 case BSSGP_PAGING_LOCATION_AREA:
1219 gsm48_construct_ra(ra, &pinfo->raid);
1220 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1221 break;
1222 case BSSGP_PAGING_ROUTEING_AREA:
1223 gsm48_construct_ra(ra, &pinfo->raid);
1224 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1225 break;
1226 case BSSGP_PAGING_BVCI:
1227 {
1228 uint16_t bvci = htons(pinfo->bvci);
1229 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1230 }
1231 break;
1232 }
1233 /* QoS profile mandatory for PS */
1234 if (pinfo->mode == BSSGP_PAGING_PS)
1235 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1236
1237 /* Optional (P-)TMSI */
1238 if (pinfo->ptmsi) {
1239 uint32_t ptmsi = htonl(*pinfo->ptmsi);
1240 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1241 }
1242
1243 return gprs_ns_sendmsg(bssgp_nsi, msg);
1244}
Harald Weltecca49632012-06-16 17:45:59 +08001245
Harald Weltede4599c2012-06-17 13:04:02 +08001246void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001247{
1248 DBSSGP = ss;
1249}