blob: 70e2a1c551cb4afad8fd54d7befb4ec6658cef7b [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 Welted11c0592012-09-06 21:57:11 +020099 ctx->fc.out_cb = &_bssgp_tx_dl_ud;
Harald Welte25de8112010-05-13 21:26:28 +0200100
Harald Weltea78b9c22010-05-17 23:02:42 +0200101 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200102
103 return ctx;
104}
105
Harald Welte9ba50052010-03-14 15:45:01 +0800106/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200107static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800108{
109 struct msgb *msg = bssgp_msgb_alloc();
110 struct bssgp_normal_hdr *bgph =
111 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
112
Harald Welte24a655f2010-04-30 19:54:29 +0200113 msgb_nsei(msg) = nsei;
114 msgb_bvci(msg) = ns_bvci;
115
Harald Welte9ba50052010-03-14 15:45:01 +0800116 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
117 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
118
Harald Welte24a655f2010-04-30 19:54:29 +0200119 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800120}
121
Harald Weltea8aa4df2010-05-30 22:00:53 +0200122/* 10.3.7 SUSPEND-ACK PDU */
123int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
124 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
125{
126 struct msgb *msg = bssgp_msgb_alloc();
127 struct bssgp_normal_hdr *bgph =
128 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
129 uint32_t _tlli;
130 uint8_t ra[6];
131
132 msgb_nsei(msg) = nsei;
133 msgb_bvci(msg) = 0; /* Signalling */
134 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
135
136 _tlli = htonl(tlli);
137 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
138 gsm48_construct_ra(ra, ra_id);
139 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
140 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
141
142 return gprs_ns_sendmsg(bssgp_nsi, msg);
143}
144
145/* 10.3.8 SUSPEND-NACK PDU */
146int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100147 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200148 uint8_t *cause)
149{
150 struct msgb *msg = bssgp_msgb_alloc();
151 struct bssgp_normal_hdr *bgph =
152 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
153 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100154 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200155
156 msgb_nsei(msg) = nsei;
157 msgb_bvci(msg) = 0; /* Signalling */
158 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
159
160 _tlli = htonl(tlli);
161 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100162 gsm48_construct_ra(ra, ra_id);
163 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200164 if (cause)
165 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
166
167 return gprs_ns_sendmsg(bssgp_nsi, msg);
168}
169
170/* 10.3.10 RESUME-ACK PDU */
171int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
172 const struct gprs_ra_id *ra_id)
173{
174 struct msgb *msg = bssgp_msgb_alloc();
175 struct bssgp_normal_hdr *bgph =
176 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
177 uint32_t _tlli;
178 uint8_t ra[6];
179
180 msgb_nsei(msg) = nsei;
181 msgb_bvci(msg) = 0; /* Signalling */
182 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
183
184 _tlli = htonl(tlli);
185 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
186 gsm48_construct_ra(ra, ra_id);
187 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
188
189 return gprs_ns_sendmsg(bssgp_nsi, msg);
190}
191
192/* 10.3.11 RESUME-NACK PDU */
193int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
194 const struct gprs_ra_id *ra_id, uint8_t *cause)
195{
196 struct msgb *msg = bssgp_msgb_alloc();
197 struct bssgp_normal_hdr *bgph =
198 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
199 uint32_t _tlli;
200 uint8_t ra[6];
201
202 msgb_nsei(msg) = nsei;
203 msgb_bvci(msg) = 0; /* Signalling */
204 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
205
206 _tlli = htonl(tlli);
207 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
208 gsm48_construct_ra(ra, ra_id);
209 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
210 if (cause)
211 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
212
213 return gprs_ns_sendmsg(bssgp_nsi, msg);
214}
215
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200216uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200217{
218 /* 6 octets RAC */
219 gsm48_parse_ra(raid, buf);
220 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200221 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200222}
223
Harald Welte28610072011-11-24 21:32:07 +0100224int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
225 uint16_t cid)
226{
227 uint16_t *out_cid = (uint16_t *) (buf + 6);
228 /* 6 octets RAC */
229 gsm48_construct_ra(buf, raid);
230 /* 2 octets CID */
231 *out_cid = htons(cid);
232
233 return 8;
234}
235
Harald Welte3fddf3c2010-05-01 16:48:27 +0200236/* Chapter 8.4 BVC-Reset Procedure */
237static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
238 uint16_t ns_bvci)
239{
Harald Welte15a36432012-06-17 12:16:31 +0800240 struct osmo_bssgp_prim nmp;
Harald Welte8a521132010-05-17 22:59:29 +0200241 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200242 uint16_t nsei = msgb_nsei(msg);
243 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200244
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200245 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Weltee9686b62010-05-31 18:07:17 +0200246 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200247 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
248
Harald Welte6752fa42010-05-02 09:23:16 +0200249 /* look-up or create the BTS context for this BVC */
250 bctx = btsctx_by_bvci_nsei(bvci, nsei);
251 if (!bctx)
252 bctx = btsctx_alloc(bvci, nsei);
253
Harald Welte25de8112010-05-13 21:26:28 +0200254 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
255 bctx->state &= ~BVC_S_BLOCKED;
256
Harald Welte3fddf3c2010-05-01 16:48:27 +0200257 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
258 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200259 if (bvci != 0 && bvci != 1) {
260 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200261 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200262 "missing mandatory IE\n", bvci);
263 return -EINVAL;
264 }
265 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200266 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
267 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200268 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200269 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
270 bctx->ra_id.rac, bctx->cell_id, bvci);
271 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200272
Harald Welte15a36432012-06-17 12:16:31 +0800273 /* Send NM_BVC_RESET.ind to NM */
274 memset(&nmp, 0, sizeof(nmp));
275 nmp.nsei = nsei;
276 nmp.bvci = bvci;
277 nmp.tp = tp;
278 nmp.ra_id = &bctx->ra_id;
279 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_RESET,
280 PRIM_OP_INDICATION, msg);
281 bssgp_prim_cb(&nmp.oph, NULL);
282
Harald Welte6752fa42010-05-02 09:23:16 +0200283 /* Acknowledge the RESET to the BTS */
Harald Welte5b3bffb2012-09-07 12:03:40 +0200284 bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
285 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200286 return 0;
287}
288
Harald Welte25de8112010-05-13 21:26:28 +0200289static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
290{
Harald Welte15a36432012-06-17 12:16:31 +0800291 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200292 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200293 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200294
295 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200296 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200297 /* 8.3.2: Signalling BVC shall never be blocked */
298 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
299 "received block for signalling BVC!?!\n",
300 msgb_nsei(msg), msgb_bvci(msg));
301 return 0;
302 }
Harald Welte25de8112010-05-13 21:26:28 +0200303
Harald Welte086fe322011-08-19 16:45:19 +0200304 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200305
306 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
307 if (!ptp_ctx)
308 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
309
310 ptp_ctx->state |= BVC_S_BLOCKED;
311 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
312
Harald Welte15a36432012-06-17 12:16:31 +0800313 /* Send NM_BVC_BLOCK.ind to NM */
314 memset(&nmp, 0, sizeof(nmp));
315 nmp.nsei = msgb_nsei(msg);
316 nmp.bvci = bvci;
317 nmp.tp = tp;
318 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_BLOCK,
319 PRIM_OP_INDICATION, msg);
320 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200321
322 /* We always acknowledge the BLOCKing */
323 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
324 bvci, msgb_bvci(msg));
325};
326
327static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
328{
Harald Welte15a36432012-06-17 12:16:31 +0800329 struct osmo_bssgp_prim nmp;
Harald Welte25de8112010-05-13 21:26:28 +0200330 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200331 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200332
333 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200334 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200335 /* 8.3.2: Signalling BVC shall never be blocked */
336 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
337 "received unblock for signalling BVC!?!\n",
338 msgb_nsei(msg), msgb_bvci(msg));
339 return 0;
340 }
Harald Welte25de8112010-05-13 21:26:28 +0200341
Harald Weltee9686b62010-05-31 18:07:17 +0200342 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200343
344 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
345 if (!ptp_ctx)
346 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
347
348 ptp_ctx->state &= ~BVC_S_BLOCKED;
349
Harald Welte15a36432012-06-17 12:16:31 +0800350 /* Send NM_BVC_UNBLOCK.ind to NM */
351 memset(&nmp, 0, sizeof(nmp));
352 nmp.nsei = msgb_nsei(msg);
353 nmp.bvci = bvci;
354 nmp.tp = tp;
355 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_BVC_UNBLOCK,
356 PRIM_OP_INDICATION, msg);
357 bssgp_prim_cb(&nmp.oph, NULL);
Harald Welte25de8112010-05-13 21:26:28 +0200358
359 /* We always acknowledge the unBLOCKing */
360 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
361 bvci, msgb_bvci(msg));
362};
363
Harald Welte9ba50052010-03-14 15:45:01 +0800364/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200365static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200366 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800367{
Harald Welte15a36432012-06-17 12:16:31 +0800368 struct osmo_bssgp_prim gbp;
Harald Welteec19c102010-05-02 09:50:42 +0200369 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800370
Harald Welte6752fa42010-05-02 09:23:16 +0200371 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200372 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800373
Harald Welte086fe322011-08-19 16:45:19 +0200374 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200375
Harald Welte9ba50052010-03-14 15:45:01 +0800376 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200377 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200378 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
379 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
380 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200381 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200382 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200383
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200384 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800385 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
386 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800387
Harald Welte15a36432012-06-17 12:16:31 +0800388 /* Send BSSGP_UL_UD.ind to NM */
389 memset(&gbp, 0, sizeof(gbp));
390 gbp.nsei = ctx->nsei;
391 gbp.bvci = ctx->bvci;
392 gbp.tlli = msgb_tlli(msg);
393 gbp.tp = tp;
394 osmo_prim_init(&gbp.oph, SAP_BSSGP_LL, PRIM_BSSGP_UL_UD,
395 PRIM_OP_INDICATION, msg);
396 return bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +0800397}
398
Harald Welte25de8112010-05-13 21:26:28 +0200399static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200400 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800401{
Harald Welte15a36432012-06-17 12:16:31 +0800402 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200403 struct gprs_ra_id raid;
404 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200405 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800406
Harald Welte25de8112010-05-13 21:26:28 +0200407 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200408 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
409 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
410 "missing mandatory IE\n", ctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200411 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200412 }
Harald Welte9ba50052010-03-14 15:45:01 +0800413
Harald Weltea8aa4df2010-05-30 22:00:53 +0200414 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200415
Harald Welte17925322010-05-31 20:18:35 +0200416 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200417 ctx->bvci, tlli);
418
Harald Weltea8aa4df2010-05-30 22:00:53 +0200419 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
420
Harald Welte313cccf2010-06-09 11:22:47 +0200421 /* Inform GMM about the SUSPEND request */
Harald Welte15a36432012-06-17 12:16:31 +0800422 memset(&gbp, 0, sizeof(gbp));
423 gbp.nsei = msgb_nsei(msg);
424 gbp.bvci = ctx->bvci;
425 gbp.tlli = tlli;
426 gbp.ra_id = &raid;
427 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_SUSPEND,
428 PRIM_OP_REQUEST, msg);
429
430 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200431 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100432 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200433
Harald Weltea8aa4df2010-05-30 22:00:53 +0200434 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
435
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800436 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800437}
438
Harald Welte25de8112010-05-13 21:26:28 +0200439static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200440 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800441{
Harald Welte15a36432012-06-17 12:16:31 +0800442 struct osmo_bssgp_prim gbp;
Harald Weltea8aa4df2010-05-30 22:00:53 +0200443 struct gprs_ra_id raid;
444 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200445 uint8_t suspend_ref;
446 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800447
Harald Welte25de8112010-05-13 21:26:28 +0200448 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
449 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200450 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
451 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
452 "missing mandatory IE\n", ctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200453 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200454 }
Harald Welte9ba50052010-03-14 15:45:01 +0800455
Harald Weltea8aa4df2010-05-30 22:00:53 +0200456 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Welte313cccf2010-06-09 11:22:47 +0200457 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200458
Harald Welte086fe322011-08-19 16:45:19 +0200459 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ctx->bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200460
Harald Weltea8aa4df2010-05-30 22:00:53 +0200461 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
462
Harald Welte313cccf2010-06-09 11:22:47 +0200463 /* Inform GMM about the RESUME request */
Harald Welte15a36432012-06-17 12:16:31 +0800464 memset(&gbp, 0, sizeof(gbp));
465 gbp.nsei = msgb_nsei(msg);
466 gbp.bvci = ctx->bvci;
467 gbp.tlli = tlli;
468 gbp.ra_id = &raid;
469 gbp.u.resume.suspend_ref = suspend_ref;
470 osmo_prim_init(&gbp.oph, SAP_BSSGP_GMM, PRIM_BSSGP_GMM_RESUME,
471 PRIM_OP_REQUEST, msg);
472
473 rc = bssgp_prim_cb(&gbp.oph, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200474 if (rc < 0)
475 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
476 NULL);
477
Harald Weltea8aa4df2010-05-30 22:00:53 +0200478 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800479 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800480}
481
Harald Weltee9686b62010-05-31 18:07:17 +0200482
483static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
484 struct bssgp_bvc_ctx *ctx)
485{
Harald Welte15a36432012-06-17 12:16:31 +0800486 struct osmo_bssgp_prim nmp;
Harald Welteb7363142010-07-23 21:59:29 +0200487 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200488
489 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
490 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
491 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
492 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
493 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
494 "missing mandatory IE\n", ctx->bvci);
495 }
496
Harald Welteb7363142010-07-23 21:59:29 +0200497 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
498 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200499
Harald Welte086fe322011-08-19 16:45:19 +0200500 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200501 ctx->bvci, tlli);
502
503 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
504
Harald Welte15a36432012-06-17 12:16:31 +0800505 /* send NM_LLC_DISCARDED to NM */
506 memset(&nmp, 0, sizeof(nmp));
507 nmp.nsei = msgb_nsei(msg);
508 nmp.bvci = ctx->bvci;
509 nmp.tlli = tlli;
510 nmp.tp = tp;
511 osmo_prim_init(&nmp.oph, SAP_BSSGP_NM, PRIM_NM_LLC_DISCARDED,
512 PRIM_OP_INDICATION, msg);
513
514 return bssgp_prim_cb(&nmp.oph, NULL);
Harald Weltee9686b62010-05-31 18:07:17 +0200515}
516
Harald Welted11c0592012-09-06 21:57:11 +0200517/* One element (msgb) in a BSSGP Flow Control queue */
518struct bssgp_fc_queue_element {
519 /* linked list of queue elements */
520 struct llist_head list;
521 /* The message that we have enqueued */
522 struct msgb *msg;
523 /* Length of the LLC PDU part of the contained message */
524 uint32_t llc_pdu_len;
525 /* private pointer passed to the flow control out_cb function */
526 void *priv;
527};
528
529static int fc_queue_timer_cfg(struct bssgp_flow_control *fc);
530static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len);
531
532static void fc_timer_cb(void *data)
533{
534 struct bssgp_flow_control *fc = data;
535 struct bssgp_fc_queue_element *fcqe;
536 struct timeval time_now;
537
538 /* if the queue is empty, we return without sending something
539 * and without re-starting the timer */
540 if (llist_empty(&fc->queue))
541 return;
542
543 /* get the first entry from the queue */
544 fcqe = llist_entry(fc->queue.next, struct bssgp_fc_queue_element,
545 list);
546
547 if (bssgp_fc_needs_queueing(fc, fcqe->llc_pdu_len)) {
548 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP-FC: fc_timer_cb() but still "
549 "not able to send PDU of %u bytes\n", fcqe->llc_pdu_len);
550 /* make sure we re-start the timer */
551 fc_queue_timer_cfg(fc);
552 return;
553 }
554
555 /* remove from the queue */
556 llist_del(&fcqe->list);
557
558 fc->queue_depth--;
559
560 /* record the time we transmitted this PDU */
561 gettimeofday(&time_now, NULL);
562 fc->time_last_pdu = time_now;
563
564 /* call the output callback for this FC instance */
565 fc->out_cb(fcqe->priv, fcqe->msg, fcqe->llc_pdu_len, NULL);
566
567 /* we expect that out_cb will in the end free the msgb once
568 * it is no longer needed */
569
570 /* but we have to free the queue element ourselves */
571 talloc_free(fcqe);
572
573 /* re-configure the timer for the next PDU */
574 fc_queue_timer_cfg(fc);
575}
576
577/* configure/schedule the flow control timer to expire once the bucket
578 * will have leaked a sufficient number of bytes to transmit the next
579 * PDU in the queue */
580static int fc_queue_timer_cfg(struct bssgp_flow_control *fc)
581{
582 struct bssgp_fc_queue_element *fcqe;
583 uint32_t msecs;
584
585 if (llist_empty(&fc->queue))
586 return 0;
587
588 fcqe = llist_entry(&fc->queue.next, struct bssgp_fc_queue_element,
589 list);
590
591 /* Calculate the point in time at which we will have leaked
592 * a sufficient number of bytes from the bucket to transmit
593 * the first PDU in the queue */
594 msecs = (fcqe->llc_pdu_len * 1000) / fc->bucket_leak_rate;
595 /* FIXME: add that time to fc->time_last_pdu and subtract it from
596 * current time */
597
598 fc->timer.data = fc;
599 fc->timer.cb = &fc_timer_cb;
600 osmo_timer_schedule(&fc->timer, msecs / 1000, (msecs % 1000) * 1000);
601
602 return 0;
603}
604
605/* Enqueue a PDU in the flow control queue for delayed transmission */
606static int fc_enqueue(struct bssgp_flow_control *fc, struct msgb *msg,
607 uint32_t llc_pdu_len, void *priv)
608{
609 struct bssgp_fc_queue_element *fcqe;
610
611 if (fc->queue_depth >= fc->max_queue_depth)
612 return -ENOSPC;
613
614 fcqe = talloc_zero(fc, struct bssgp_fc_queue_element);
615 if (!fcqe)
616 return -ENOMEM;
617 fcqe->msg = msg;
618 fcqe->llc_pdu_len = llc_pdu_len;
619 fcqe->priv = priv;
620
621 llist_add_tail(&fcqe->list, &fc->queue);
622
623 fc->queue_depth++;
624
625 /* re-configure the timer for dequeueing the pdu */
626 fc_queue_timer_cfg(fc);
627
628 return 0;
629}
630
631/* According to Section 8.2 */
632static int bssgp_fc_needs_queueing(struct bssgp_flow_control *fc, uint32_t pdu_len)
633{
634 struct timeval time_now, time_diff;
635 int64_t bucket_predicted;
636 uint32_t csecs_elapsed, leaked;
637
638 /* B' = B + L(p) - (Tc - Tp)*R */
639
640 /* compute number of centi-seconds that have elapsed since transmitting
641 * the last PDU (Tc - Tp) */
642 gettimeofday(&time_now, NULL);
643 timersub(&time_now, &fc->time_last_pdu, &time_diff);
644 csecs_elapsed = time_diff.tv_sec*100 + time_diff.tv_usec/10000;
645
646 /* compute number of bytes that have leaked in the elapsed number
647 * of centi-seconds */
648 leaked = csecs_elapsed * (fc->bucket_leak_rate / 100);
649 /* add the current PDU length to the last bucket level */
650 bucket_predicted = fc->bucket_counter + pdu_len;
651 /* ... and subtract the number of leaked bytes */
652 bucket_predicted -= leaked;
653
654 if (bucket_predicted < pdu_len) {
655 /* this is just to make sure the bucket doesn't underflow */
656 bucket_predicted = pdu_len;
657 goto pass;
658 }
659
660 if (bucket_predicted <= fc->bucket_size_max) {
661 /* the bucket is not full yet, we can pass the packet */
662 fc->bucket_counter = bucket_predicted;
663 goto pass;
664 }
665
666 /* bucket is full, PDU needs to be delayed */
667 return 1;
668
669pass:
670 /* if we reach here, the PDU can pass */
671 return 0;
672}
673
674/* output callback for BVC flow control */
675static int _bssgp_tx_dl_ud(struct bssgp_flow_control *fc, struct msgb *msg,
676 uint32_t llc_pdu_len, void *priv)
677{
678 return gprs_ns_sendmsg(bssgp_nsi, msg);
679}
680
681/* input function of the flow control implementation, called first
682 * for the MM flow control, and then as the MM flow control output
683 * callback in order to perform BVC flow control */
684int bssgp_fc_in(struct bssgp_flow_control *fc, struct msgb *msg,
685 uint32_t llc_pdu_len, void *priv)
686{
687 struct timeval time_now;
688
Harald Weltebb826222012-09-07 10:22:01 +0200689 if (llc_pdu_len > fc->bucket_size_max) {
690 LOGP(DBSSGP, LOGL_NOTICE, "Single PDU (size=%u) is larger "
691 "than maximum bucket size (%u)!\n", llc_pdu_len,
692 fc->bucket_size_max);
693 return -EIO;
694 }
695
Harald Welted11c0592012-09-06 21:57:11 +0200696 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
697 return fc_enqueue(fc, msg, llc_pdu_len, priv);
698 } else {
699 /* record the time we transmitted this PDU */
700 gettimeofday(&time_now, NULL);
701 fc->time_last_pdu = time_now;
702 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
703 }
704}
705
Harald Weltebb826222012-09-07 10:22:01 +0200706
707/* Initialize the Flow Control structure */
708void bssgp_fc_init(struct bssgp_flow_control *fc,
709 uint32_t bucket_size_max, uint32_t bucket_leak_rate,
710 uint32_t max_queue_depth,
711 int (*out_cb)(struct bssgp_flow_control *fc, struct msgb *msg,
712 uint32_t llc_pdu_len, void *priv))
713{
714 fc->out_cb = out_cb;
715 fc->bucket_size_max = bucket_size_max;
716 fc->bucket_leak_rate = bucket_leak_rate;
717 fc->max_queue_depth = max_queue_depth;
718 INIT_LLIST_HEAD(&fc->queue);
719 gettimeofday(&fc->time_last_pdu, NULL);
720}
721
Harald Welted11c0592012-09-06 21:57:11 +0200722/* Initialize the Flow Control parameters for a new MS according to
723 * default values for the BVC specified by BVCI and NSEI */
724int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
Harald Weltebb826222012-09-07 10:22:01 +0200725 uint16_t nsei, uint32_t max_queue_depth)
Harald Welted11c0592012-09-06 21:57:11 +0200726{
727 struct bssgp_bvc_ctx *ctx;
728
729 ctx = btsctx_by_bvci_nsei(bvci, nsei);
730 if (!ctx)
731 return -ENODEV;
Harald Weltebb826222012-09-07 10:22:01 +0200732
733 /* output call-back of per-MS FC is per-CTX FC */
734 bssgp_fc_init(fc_ms, ctx->bmax_default_ms, ctx->r_default_ms,
735 max_queue_depth, bssgp_fc_in);
Harald Welted11c0592012-09-06 21:57:11 +0200736
737 return 0;
738}
739
Harald Welte25de8112010-05-13 21:26:28 +0200740static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200741 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800742{
743
Harald Weltee9686b62010-05-31 18:07:17 +0200744 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
745 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800746
747 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
748 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
749 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
750 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200751 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
752 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
753 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800754 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200755 }
Harald Welte9ba50052010-03-14 15:45:01 +0800756
Harald Weltebb826222012-09-07 10:22:01 +0200757 /* 11.3.5 Bucket Size in 100 octets unit */
Harald Welted11c0592012-09-06 21:57:11 +0200758 bctx->fc.bucket_size_max = 100 *
759 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVC_BUCKET_SIZE));
Harald Weltebb826222012-09-07 10:22:01 +0200760 /* 11.3.4 Bucket Leak Rate in 100 bits/sec unit */
Harald Welted11c0592012-09-06 21:57:11 +0200761 bctx->fc.bucket_leak_rate = 100 *
Harald Weltebb826222012-09-07 10:22:01 +0200762 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BUCKET_LEAK_RATE)) / 8;
763 /* 11.3.2 in octets */
764 bctx->bmax_default_ms =
Harald Welted11c0592012-09-06 21:57:11 +0200765 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BMAX_DEFAULT_MS));
Harald Weltebb826222012-09-07 10:22:01 +0200766 /* 11.3.32 Bucket Leak rate in 100bits/sec unit */
Harald Welted11c0592012-09-06 21:57:11 +0200767 bctx->r_default_ms = 100 *
Harald Weltebb826222012-09-07 10:22:01 +0200768 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_R_DEFAULT_MS)) / 8;
Harald Welte30bc19a2010-05-02 11:19:37 +0200769
Harald Welte9ba50052010-03-14 15:45:01 +0800770 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200771 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200772 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800773}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200774
Harald Welte25de8112010-05-13 21:26:28 +0200775/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800776static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
777 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800778{
Harald Welteec19c102010-05-02 09:50:42 +0200779 struct bssgp_normal_hdr *bgph =
780 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200781 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800782 int rc = 0;
783
Harald Welte58e65c92010-05-13 21:45:23 +0200784 /* If traffic is received on a BVC that is marked as blocked, the
785 * received PDU shall not be accepted and a STATUS PDU (Cause value:
786 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
787 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
788 uint16_t bvci = msgb_bvci(msg);
789 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
790 }
791
Harald Welte9ba50052010-03-14 15:45:01 +0800792 switch (pdu_type) {
793 case BSSGP_PDUT_UL_UNITDATA:
794 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200795 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800796 break;
797 case BSSGP_PDUT_RA_CAPABILITY:
798 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200799 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
800 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200801 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800802 /* FIXME: send RA_CAPA_UPDATE_ACK */
803 break;
804 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200805 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800806 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200807 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800808 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800809 case BSSGP_PDUT_FLOW_CONTROL_BVC:
810 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200811 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800812 break;
813 case BSSGP_PDUT_FLOW_CONTROL_MS:
814 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200815 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
816 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200817 /* FIXME: actually implement flow control */
818 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800819 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800820 case BSSGP_PDUT_STATUS:
821 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200822 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800823 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
824 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
825 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
826 case BSSGP_PDUT_MODIFY_BSS_PFC:
827 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200828 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x not [yet] "
829 "implemented\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200830 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800831 break;
832 /* those only exist in the SGSN -> BSS direction */
833 case BSSGP_PDUT_DL_UNITDATA:
834 case BSSGP_PDUT_PAGING_PS:
835 case BSSGP_PDUT_PAGING_CS:
836 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200837 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
838 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200839 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x only exists "
840 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200841 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
842 rc = -EINVAL;
843 break;
844 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200845 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n",
846 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200847 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
848 break;
849 }
850
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800851 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200852}
853
854/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800855static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
856 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200857{
858 struct bssgp_normal_hdr *bgph =
859 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
860 uint8_t pdu_type = bgph->pdu_type;
861 int rc = 0;
862 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte25de8112010-05-13 21:26:28 +0200863
864 switch (bgph->pdu_type) {
865 case BSSGP_PDUT_SUSPEND:
866 /* MS wants to suspend */
867 rc = bssgp_rx_suspend(msg, tp, bctx);
868 break;
869 case BSSGP_PDUT_RESUME:
870 /* MS wants to resume */
871 rc = bssgp_rx_resume(msg, tp, bctx);
872 break;
873 case BSSGP_PDUT_FLUSH_LL_ACK:
874 /* BSS informs us it has performed LL FLUSH */
Harald Welte086fe322011-08-19 16:45:19 +0200875 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200876 /* FIXME: send NM_FLUSH_LL.res to NM */
877 break;
878 case BSSGP_PDUT_LLC_DISCARD:
879 /* BSS informs that some LLC PDU's have been discarded */
Harald Weltee9686b62010-05-31 18:07:17 +0200880 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200881 break;
882 case BSSGP_PDUT_BVC_BLOCK:
883 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200884 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200885 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
886 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
887 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200888 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200889 }
Harald Welte2677ea52010-05-31 17:16:36 +0200890 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200891 break;
892 case BSSGP_PDUT_BVC_UNBLOCK:
893 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200894 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
895 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
896 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200897 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200898 }
Harald Welte25de8112010-05-13 21:26:28 +0200899 rc = bssgp_rx_bvc_unblock(msg, tp);
900 break;
901 case BSSGP_PDUT_BVC_RESET:
902 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200903 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200904 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
905 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
906 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200907 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200908 }
Harald Welte25de8112010-05-13 21:26:28 +0200909 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
910 break;
911 case BSSGP_PDUT_STATUS:
912 /* Some exception has occurred */
Harald Weltee9686b62010-05-31 18:07:17 +0200913 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200914 /* FIXME: send NM_STATUS.ind to NM */
915 break;
916 /* those only exist in the SGSN -> BSS direction */
917 case BSSGP_PDUT_PAGING_PS:
918 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800919 case BSSGP_PDUT_SUSPEND_ACK:
920 case BSSGP_PDUT_SUSPEND_NACK:
921 case BSSGP_PDUT_RESUME_ACK:
922 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200923 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800924 case BSSGP_PDUT_BVC_BLOCK_ACK:
925 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
926 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltee9686b62010-05-31 18:07:17 +0200927 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x only exists "
928 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200929 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800930 rc = -EINVAL;
931 break;
932 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200933 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n",
934 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200935 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800936 break;
937 }
938
939 return rc;
940err_mand_ie:
941 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
942}
943
Harald Welte25de8112010-05-13 21:26:28 +0200944/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +0800945int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +0200946{
947 struct bssgp_normal_hdr *bgph =
948 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
949 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
950 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200951 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200952 uint8_t pdu_type = bgph->pdu_type;
953 uint16_t ns_bvci = msgb_bvci(msg);
954 int data_len;
955 int rc = 0;
956
957 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
958
959 /* UNITDATA BSSGP headers have TLLI in front */
960 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
961 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
962 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
963 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
964 } else {
965 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
966 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
967 }
968
969 /* look-up or create the BTS context for this BVC */
970 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
971 /* Only a RESET PDU can create a new BVC context */
972 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
973 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
974 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
975 pdu_type);
976 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
977 }
978
Harald Welte16c8dbb2010-05-17 23:30:01 +0200979 if (bctx) {
Harald Weltecca49632012-06-16 17:45:59 +0800980 log_set_context(GPRS_CTX_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +0200981 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
982 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
983 msgb_bssgp_len(msg));
984 }
985
Harald Welte61c07842010-05-18 11:57:08 +0200986 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +0800987 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +0200988 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +0200989 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
990 else
Harald Weltede4599c2012-06-17 13:04:02 +0800991 rc = bssgp_rx_ptp(msg, &tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200992
993 return rc;
994}
995
Harald Weltede4599c2012-06-17 13:04:02 +0800996int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
997 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +0800998{
Harald Welte8a521132010-05-17 22:59:29 +0200999 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +08001000 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001001 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +08001002 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +02001003 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +02001004 uint16_t bvci = msgb_bvci(msg);
1005 uint16_t nsei = msgb_nsei(msg);
Harald Welte8ef54d12012-06-17 09:31:16 +08001006 uint16_t _pdu_lifetime = htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +02001007 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +08001008
Harald Welte30bc19a2010-05-02 11:19:37 +02001009 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +02001010 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +02001011 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +02001012 bvci);
1013 return -EINVAL;
1014 }
1015
1016 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +02001017 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +02001018 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
1019 bvci);
1020 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +02001021 }
Harald Welte9ba50052010-03-14 15:45:01 +08001022
1023 if (msg->len > TVLV_MAX_ONEBYTE)
1024 llc_pdu_tlv_hdr_len += 1;
1025
1026 /* prepend the tag and length of the LLC-PDU TLV */
1027 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1028 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1029 if (llc_pdu_tlv_hdr_len > 2) {
1030 llc_pdu_tlv[1] = msg_len >> 8;
1031 llc_pdu_tlv[2] = msg_len & 0xff;
1032 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001033 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001034 llc_pdu_tlv[1] |= 0x80;
1035 }
1036
Harald Welte2f946832010-05-31 22:12:30 +02001037 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1038
Harald Welte8ef54d12012-06-17 09:31:16 +08001039 if (dup) {
Harald Welte2f946832010-05-31 22:12:30 +02001040 /* Old TLLI to help BSS map from old->new */
Harald Welte8ef54d12012-06-17 09:31:16 +08001041 if (dup->tlli) {
1042 uint32_t tlli = htonl(*dup->tlli);
1043 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
1044 }
Harald Welte2f946832010-05-31 22:12:30 +02001045
1046 /* IMSI */
Harald Welte2d956a82012-07-04 21:55:23 +02001047 if (dup->imsi && strlen(dup->imsi)) {
Harald Welte2f946832010-05-31 22:12:30 +02001048 uint8_t mi[10];
Harald Welte8ef54d12012-06-17 09:31:16 +08001049 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
Harald Welte2f946832010-05-31 22:12:30 +02001050 if (imsi_len > 2)
1051 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
Harald Welte8ef54d12012-06-17 09:31:16 +08001052 imsi_len-2, mi+2);
Harald Welte2f946832010-05-31 22:12:30 +02001053 }
1054
1055 /* DRX parameters */
Harald Welte8ef54d12012-06-17 09:31:16 +08001056 drx_params = htons(dup->drx_parms);
Harald Welte2f946832010-05-31 22:12:30 +02001057 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1058 (uint8_t *) &drx_params);
1059
1060 /* FIXME: Priority */
1061
1062 /* MS Radio Access Capability */
Harald Welte8ef54d12012-06-17 09:31:16 +08001063 if (dup->ms_ra_cap.len)
Harald Welte2f946832010-05-31 22:12:30 +02001064 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
Harald Welte8ef54d12012-06-17 09:31:16 +08001065 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1066
Harald Welte2f946832010-05-31 22:12:30 +02001067 }
Harald Welte9ba50052010-03-14 15:45:01 +08001068
1069 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001070 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001071
1072 /* prepend the QoS profile, TLLI and pdu type */
1073 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001074 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Welte510c3922010-04-30 16:33:12 +02001075 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001076 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1077
Harald Welte16c8dbb2010-05-17 23:30:01 +02001078 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1079 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1080
Harald Welte30bc19a2010-05-02 11:19:37 +02001081 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001082
Harald Welted11c0592012-09-06 21:57:11 +02001083 /* check if we have to go through per-ms flow control or can go
1084 * directly to the per-BSS flow control */
1085 if (dup->fc)
1086 return bssgp_fc_in(dup->fc, msg, msg_len, &bctx->fc);
1087 else
1088 return bssgp_fc_in(&bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001089}
Harald Welte68b4f032010-06-09 16:22:28 +02001090
1091/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001092int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1093 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001094{
1095 struct msgb *msg = bssgp_msgb_alloc();
1096 struct bssgp_normal_hdr *bgph =
1097 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
1098 uint16_t drx_params = htons(pinfo->drx_params);
1099 uint8_t mi[10];
1100 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1101 uint8_t ra[6];
1102
1103 if (imsi_len < 2)
1104 return -EINVAL;
1105
1106 msgb_nsei(msg) = nsei;
1107 msgb_bvci(msg) = ns_bvci;
1108
1109 if (pinfo->mode == BSSGP_PAGING_PS)
1110 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1111 else
1112 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1113 /* IMSI */
1114 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1115 /* DRX Parameters */
1116 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1117 (uint8_t *) &drx_params);
1118 /* Scope */
1119 switch (pinfo->scope) {
1120 case BSSGP_PAGING_BSS_AREA:
1121 {
1122 uint8_t null = 0;
1123 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1124 }
1125 break;
1126 case BSSGP_PAGING_LOCATION_AREA:
1127 gsm48_construct_ra(ra, &pinfo->raid);
1128 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1129 break;
1130 case BSSGP_PAGING_ROUTEING_AREA:
1131 gsm48_construct_ra(ra, &pinfo->raid);
1132 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1133 break;
1134 case BSSGP_PAGING_BVCI:
1135 {
1136 uint16_t bvci = htons(pinfo->bvci);
1137 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1138 }
1139 break;
1140 }
1141 /* QoS profile mandatory for PS */
1142 if (pinfo->mode == BSSGP_PAGING_PS)
1143 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1144
1145 /* Optional (P-)TMSI */
1146 if (pinfo->ptmsi) {
1147 uint32_t ptmsi = htonl(*pinfo->ptmsi);
1148 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1149 }
1150
1151 return gprs_ns_sendmsg(bssgp_nsi, msg);
1152}
Harald Weltecca49632012-06-16 17:45:59 +08001153
Harald Weltede4599c2012-06-17 13:04:02 +08001154void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001155{
1156 DBSSGP = ss;
1157}