blob: 93aa5aaa61ce4679e115468c966a15baf21edc4d [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
689 if (bssgp_fc_needs_queueing(fc, llc_pdu_len)) {
690 return fc_enqueue(fc, msg, llc_pdu_len, priv);
691 } else {
692 /* record the time we transmitted this PDU */
693 gettimeofday(&time_now, NULL);
694 fc->time_last_pdu = time_now;
695 return fc->out_cb(priv, msg, llc_pdu_len, NULL);
696 }
697}
698
699/* Initialize the Flow Control parameters for a new MS according to
700 * default values for the BVC specified by BVCI and NSEI */
701int bssgp_fc_ms_init(struct bssgp_flow_control *fc_ms, uint16_t bvci,
702 uint16_t nsei)
703{
704 struct bssgp_bvc_ctx *ctx;
705
706 ctx = btsctx_by_bvci_nsei(bvci, nsei);
707 if (!ctx)
708 return -ENODEV;
709 fc_ms->bucket_size_max = ctx->bmax_default_ms;
710 fc_ms->bucket_leak_rate = ctx->r_default_ms;
711
712 return 0;
713}
714
Harald Welte25de8112010-05-13 21:26:28 +0200715static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200716 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800717{
718
Harald Weltee9686b62010-05-31 18:07:17 +0200719 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
720 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800721
722 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
723 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
724 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
725 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200726 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
727 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
728 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800729 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200730 }
Harald Welte9ba50052010-03-14 15:45:01 +0800731
Harald Welted11c0592012-09-06 21:57:11 +0200732 /* 11.3.5 */
733 bctx->fc.bucket_size_max = 100 *
734 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVC_BUCKET_SIZE));
735 /* 11.3.4 */
736 bctx->fc.bucket_leak_rate = 100 *
737 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BUCKET_LEAK_RATE));
738 /* 11.3.2 */
739 bctx->bmax_default_ms = 100 *
740 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BMAX_DEFAULT_MS));
741 /* 11.3.32 */
742 bctx->r_default_ms = 100 *
743 ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_R_DEFAULT_MS));
Harald Welte30bc19a2010-05-02 11:19:37 +0200744
Harald Welte9ba50052010-03-14 15:45:01 +0800745 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200746 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200747 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800748}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200749
Harald Welte25de8112010-05-13 21:26:28 +0200750/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800751static int bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
752 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800753{
Harald Welteec19c102010-05-02 09:50:42 +0200754 struct bssgp_normal_hdr *bgph =
755 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200756 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800757 int rc = 0;
758
Harald Welte58e65c92010-05-13 21:45:23 +0200759 /* If traffic is received on a BVC that is marked as blocked, the
760 * received PDU shall not be accepted and a STATUS PDU (Cause value:
761 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
762 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
763 uint16_t bvci = msgb_bvci(msg);
764 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
765 }
766
Harald Welte9ba50052010-03-14 15:45:01 +0800767 switch (pdu_type) {
768 case BSSGP_PDUT_UL_UNITDATA:
769 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200770 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800771 break;
772 case BSSGP_PDUT_RA_CAPABILITY:
773 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200774 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
775 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200776 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800777 /* FIXME: send RA_CAPA_UPDATE_ACK */
778 break;
779 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200780 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800781 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200782 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800783 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800784 case BSSGP_PDUT_FLOW_CONTROL_BVC:
785 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200786 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800787 break;
788 case BSSGP_PDUT_FLOW_CONTROL_MS:
789 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200790 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
791 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200792 /* FIXME: actually implement flow control */
793 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800794 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800795 case BSSGP_PDUT_STATUS:
796 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200797 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800798 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
799 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
800 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
801 case BSSGP_PDUT_MODIFY_BSS_PFC:
802 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200803 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x not [yet] "
804 "implemented\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200805 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800806 break;
807 /* those only exist in the SGSN -> BSS direction */
808 case BSSGP_PDUT_DL_UNITDATA:
809 case BSSGP_PDUT_PAGING_PS:
810 case BSSGP_PDUT_PAGING_CS:
811 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200812 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
813 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200814 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x only exists "
815 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200816 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
817 rc = -EINVAL;
818 break;
819 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200820 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n",
821 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200822 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
823 break;
824 }
825
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800826 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200827}
828
829/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800830static int bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
831 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200832{
833 struct bssgp_normal_hdr *bgph =
834 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
835 uint8_t pdu_type = bgph->pdu_type;
836 int rc = 0;
837 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte25de8112010-05-13 21:26:28 +0200838
839 switch (bgph->pdu_type) {
840 case BSSGP_PDUT_SUSPEND:
841 /* MS wants to suspend */
842 rc = bssgp_rx_suspend(msg, tp, bctx);
843 break;
844 case BSSGP_PDUT_RESUME:
845 /* MS wants to resume */
846 rc = bssgp_rx_resume(msg, tp, bctx);
847 break;
848 case BSSGP_PDUT_FLUSH_LL_ACK:
849 /* BSS informs us it has performed LL FLUSH */
Harald Welte086fe322011-08-19 16:45:19 +0200850 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200851 /* FIXME: send NM_FLUSH_LL.res to NM */
852 break;
853 case BSSGP_PDUT_LLC_DISCARD:
854 /* BSS informs that some LLC PDU's have been discarded */
Harald Weltee9686b62010-05-31 18:07:17 +0200855 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200856 break;
857 case BSSGP_PDUT_BVC_BLOCK:
858 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200859 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200860 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
861 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
862 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200863 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200864 }
Harald Welte2677ea52010-05-31 17:16:36 +0200865 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200866 break;
867 case BSSGP_PDUT_BVC_UNBLOCK:
868 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200869 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
870 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
871 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200872 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200873 }
Harald Welte25de8112010-05-13 21:26:28 +0200874 rc = bssgp_rx_bvc_unblock(msg, tp);
875 break;
876 case BSSGP_PDUT_BVC_RESET:
877 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200878 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200879 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
880 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
881 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200882 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200883 }
Harald Welte25de8112010-05-13 21:26:28 +0200884 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
885 break;
886 case BSSGP_PDUT_STATUS:
887 /* Some exception has occurred */
Harald Weltee9686b62010-05-31 18:07:17 +0200888 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200889 /* FIXME: send NM_STATUS.ind to NM */
890 break;
891 /* those only exist in the SGSN -> BSS direction */
892 case BSSGP_PDUT_PAGING_PS:
893 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800894 case BSSGP_PDUT_SUSPEND_ACK:
895 case BSSGP_PDUT_SUSPEND_NACK:
896 case BSSGP_PDUT_RESUME_ACK:
897 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200898 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800899 case BSSGP_PDUT_BVC_BLOCK_ACK:
900 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
901 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltee9686b62010-05-31 18:07:17 +0200902 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x only exists "
903 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200904 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800905 rc = -EINVAL;
906 break;
907 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200908 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n",
909 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200910 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800911 break;
912 }
913
914 return rc;
915err_mand_ie:
916 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
917}
918
Harald Welte25de8112010-05-13 21:26:28 +0200919/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltede4599c2012-06-17 13:04:02 +0800920int bssgp_rcvmsg(struct msgb *msg)
Harald Welte25de8112010-05-13 21:26:28 +0200921{
922 struct bssgp_normal_hdr *bgph =
923 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
924 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
925 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200926 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200927 uint8_t pdu_type = bgph->pdu_type;
928 uint16_t ns_bvci = msgb_bvci(msg);
929 int data_len;
930 int rc = 0;
931
932 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
933
934 /* UNITDATA BSSGP headers have TLLI in front */
935 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
936 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
937 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
938 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
939 } else {
940 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
941 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
942 }
943
944 /* look-up or create the BTS context for this BVC */
945 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
946 /* Only a RESET PDU can create a new BVC context */
947 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
948 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
949 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
950 pdu_type);
951 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
952 }
953
Harald Welte16c8dbb2010-05-17 23:30:01 +0200954 if (bctx) {
Harald Weltecca49632012-06-16 17:45:59 +0800955 log_set_context(GPRS_CTX_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +0200956 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
957 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
958 msgb_bssgp_len(msg));
959 }
960
Harald Welte61c07842010-05-18 11:57:08 +0200961 if (ns_bvci == BVCI_SIGNALLING)
Harald Weltede4599c2012-06-17 13:04:02 +0800962 rc = bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +0200963 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +0200964 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
965 else
Harald Weltede4599c2012-06-17 13:04:02 +0800966 rc = bssgp_rx_ptp(msg, &tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200967
968 return rc;
969}
970
Harald Weltede4599c2012-06-17 13:04:02 +0800971int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime,
972 struct bssgp_dl_ud_par *dup)
Harald Welte9ba50052010-03-14 15:45:01 +0800973{
Harald Welte8a521132010-05-17 22:59:29 +0200974 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800975 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200976 uint8_t llc_pdu_tlv_hdr_len = 2;
Harald Welte8ef54d12012-06-17 09:31:16 +0800977 uint8_t *llc_pdu_tlv;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200978 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200979 uint16_t bvci = msgb_bvci(msg);
980 uint16_t nsei = msgb_nsei(msg);
Harald Welte8ef54d12012-06-17 09:31:16 +0800981 uint16_t _pdu_lifetime = htons(pdu_lifetime); /* centi-seconds */
Harald Welte2f946832010-05-31 22:12:30 +0200982 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +0800983
Harald Welte30bc19a2010-05-02 11:19:37 +0200984 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +0200985 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200986 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200987 bvci);
988 return -EINVAL;
989 }
990
991 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200992 if (!bctx) {
Harald Welted11c0592012-09-06 21:57:11 +0200993 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to unknown BVCI %u\n",
994 bvci);
995 return -ENODEV;
Harald Welte25de8112010-05-13 21:26:28 +0200996 }
Harald Welte9ba50052010-03-14 15:45:01 +0800997
998 if (msg->len > TVLV_MAX_ONEBYTE)
999 llc_pdu_tlv_hdr_len += 1;
1000
1001 /* prepend the tag and length of the LLC-PDU TLV */
1002 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
1003 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
1004 if (llc_pdu_tlv_hdr_len > 2) {
1005 llc_pdu_tlv[1] = msg_len >> 8;
1006 llc_pdu_tlv[2] = msg_len & 0xff;
1007 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +02001008 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +08001009 llc_pdu_tlv[1] |= 0x80;
1010 }
1011
Harald Welte2f946832010-05-31 22:12:30 +02001012 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
1013
Harald Welte8ef54d12012-06-17 09:31:16 +08001014 if (dup) {
Harald Welte2f946832010-05-31 22:12:30 +02001015 /* Old TLLI to help BSS map from old->new */
Harald Welte8ef54d12012-06-17 09:31:16 +08001016 if (dup->tlli) {
1017 uint32_t tlli = htonl(*dup->tlli);
1018 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &tlli);
1019 }
Harald Welte2f946832010-05-31 22:12:30 +02001020
1021 /* IMSI */
Harald Welte2d956a82012-07-04 21:55:23 +02001022 if (dup->imsi && strlen(dup->imsi)) {
Harald Welte2f946832010-05-31 22:12:30 +02001023 uint8_t mi[10];
Harald Welte8ef54d12012-06-17 09:31:16 +08001024 int imsi_len = gsm48_generate_mid_from_imsi(mi, dup->imsi);
Harald Welte2f946832010-05-31 22:12:30 +02001025 if (imsi_len > 2)
1026 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
Harald Welte8ef54d12012-06-17 09:31:16 +08001027 imsi_len-2, mi+2);
Harald Welte2f946832010-05-31 22:12:30 +02001028 }
1029
1030 /* DRX parameters */
Harald Welte8ef54d12012-06-17 09:31:16 +08001031 drx_params = htons(dup->drx_parms);
Harald Welte2f946832010-05-31 22:12:30 +02001032 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
1033 (uint8_t *) &drx_params);
1034
1035 /* FIXME: Priority */
1036
1037 /* MS Radio Access Capability */
Harald Welte8ef54d12012-06-17 09:31:16 +08001038 if (dup->ms_ra_cap.len)
Harald Welte2f946832010-05-31 22:12:30 +02001039 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
Harald Welte8ef54d12012-06-17 09:31:16 +08001040 dup->ms_ra_cap.len, dup->ms_ra_cap.v);
1041
Harald Welte2f946832010-05-31 22:12:30 +02001042 }
Harald Welte9ba50052010-03-14 15:45:01 +08001043
1044 /* prepend the pdu lifetime */
Harald Welte8ef54d12012-06-17 09:31:16 +08001045 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&_pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +08001046
1047 /* prepend the QoS profile, TLLI and pdu type */
1048 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Welte8ef54d12012-06-17 09:31:16 +08001049 memcpy(budh->qos_profile, dup->qos_profile, sizeof(budh->qos_profile));
Harald Welte510c3922010-04-30 16:33:12 +02001050 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +08001051 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
1052
Harald Welte16c8dbb2010-05-17 23:30:01 +02001053 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
1054 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
1055
Harald Welte30bc19a2010-05-02 11:19:37 +02001056 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +02001057
Harald Welted11c0592012-09-06 21:57:11 +02001058 /* check if we have to go through per-ms flow control or can go
1059 * directly to the per-BSS flow control */
1060 if (dup->fc)
1061 return bssgp_fc_in(dup->fc, msg, msg_len, &bctx->fc);
1062 else
1063 return bssgp_fc_in(&bctx->fc, msg, msg_len, NULL);
Harald Welte9ba50052010-03-14 15:45:01 +08001064}
Harald Welte68b4f032010-06-09 16:22:28 +02001065
1066/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +08001067int bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
1068 struct bssgp_paging_info *pinfo)
Harald Welte68b4f032010-06-09 16:22:28 +02001069{
1070 struct msgb *msg = bssgp_msgb_alloc();
1071 struct bssgp_normal_hdr *bgph =
1072 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
1073 uint16_t drx_params = htons(pinfo->drx_params);
1074 uint8_t mi[10];
1075 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
1076 uint8_t ra[6];
1077
1078 if (imsi_len < 2)
1079 return -EINVAL;
1080
1081 msgb_nsei(msg) = nsei;
1082 msgb_bvci(msg) = ns_bvci;
1083
1084 if (pinfo->mode == BSSGP_PAGING_PS)
1085 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
1086 else
1087 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
1088 /* IMSI */
1089 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
1090 /* DRX Parameters */
1091 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
1092 (uint8_t *) &drx_params);
1093 /* Scope */
1094 switch (pinfo->scope) {
1095 case BSSGP_PAGING_BSS_AREA:
1096 {
1097 uint8_t null = 0;
1098 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
1099 }
1100 break;
1101 case BSSGP_PAGING_LOCATION_AREA:
1102 gsm48_construct_ra(ra, &pinfo->raid);
1103 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
1104 break;
1105 case BSSGP_PAGING_ROUTEING_AREA:
1106 gsm48_construct_ra(ra, &pinfo->raid);
1107 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
1108 break;
1109 case BSSGP_PAGING_BVCI:
1110 {
1111 uint16_t bvci = htons(pinfo->bvci);
1112 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
1113 }
1114 break;
1115 }
1116 /* QoS profile mandatory for PS */
1117 if (pinfo->mode == BSSGP_PAGING_PS)
1118 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
1119
1120 /* Optional (P-)TMSI */
1121 if (pinfo->ptmsi) {
1122 uint32_t ptmsi = htonl(*pinfo->ptmsi);
1123 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
1124 }
1125
1126 return gprs_ns_sendmsg(bssgp_nsi, msg);
1127}
Harald Weltecca49632012-06-16 17:45:59 +08001128
Harald Weltede4599c2012-06-17 13:04:02 +08001129void bssgp_set_log_ss(int ss)
Harald Weltecca49632012-06-16 17:45:59 +08001130{
1131 DBSSGP = ss;
1132}