blob: 36abf1693f910cbf9ed5824b0385b5a305e4f9da [file] [log] [blame]
Harald Welte9ba50052010-03-14 15:45:01 +08001/* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */
2
Harald Welte6752fa42010-05-02 09:23:16 +02003/* (C) 2009-2010 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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
Harald Welte4e5721d2010-05-17 23:41:43 +020021 * TODO:
22 * o properly count incoming BVC-RESET packets in counter group
23 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080024 */
25
26#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020027#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080028
29#include <netinet/in.h>
30
31#include <osmocore/msgb.h>
32#include <osmocore/tlv.h>
Harald Welte6752fa42010-05-02 09:23:16 +020033#include <osmocore/talloc.h>
Harald Welte25de8112010-05-13 21:26:28 +020034#include <osmocore/rate_ctr.h>
Harald Welte6752fa42010-05-02 09:23:16 +020035
Harald Welte9ba50052010-03-14 15:45:01 +080036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_04_08_gprs.h>
39#include <openbsc/gprs_bssgp.h>
40#include <openbsc/gprs_llc.h>
41#include <openbsc/gprs_ns.h>
42
Harald Welte6752fa42010-05-02 09:23:16 +020043void *bssgp_tall_ctx = NULL;
44
Harald Welte6752fa42010-05-02 09:23:16 +020045#define BVC_F_BLOCKED 0x0001
46
Harald Welte25de8112010-05-13 21:26:28 +020047enum bssgp_ctr {
Harald Welte16c8dbb2010-05-17 23:30:01 +020048 BSSGP_CTR_PKTS_IN,
49 BSSGP_CTR_PKTS_OUT,
50 BSSGP_CTR_BYTES_IN,
51 BSSGP_CTR_BYTES_OUT,
Harald Welte25de8112010-05-13 21:26:28 +020052 BSSGP_CTR_BLOCKED,
53 BSSGP_CTR_DISCARDED,
54};
55
56static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Welte16c8dbb2010-05-17 23:30:01 +020057 { "packets.in", "Packets at BSSGP Level ( In)" },
58 { "packets.out","Packets at BSSGP Level (Out)" },
59 { "bytes.in", "Bytes at BSSGP Level ( In)" },
60 { "bytes.out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020061 { "blocked", "BVC Blocking count" },
62 { "discarded", "BVC LLC Discarded count" },
63};
64
65static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
66 .group_name_prefix = "bssgp.bss_ctx",
67 .group_description = "BSSGP Peer Statistics",
68 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
69 .ctr_desc = bssgp_ctr_description,
70};
71
Harald Weltea78b9c22010-05-17 23:02:42 +020072LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020073
74/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020075struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020076{
Harald Welte8a521132010-05-17 22:59:29 +020077 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020078
Harald Weltea78b9c22010-05-17 23:02:42 +020079 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020080 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
81 bctx->cell_id == cid)
82 return bctx;
83 }
84 return NULL;
85}
86
87/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +020088struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(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 *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020091
Harald Weltea78b9c22010-05-17 23:02:42 +020092 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020093 if (bctx->nsei == nsei && bctx->bvci == bvci)
94 return bctx;
95 }
96 return NULL;
97}
98
Harald Welte8a521132010-05-17 22:59:29 +020099struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200100{
Harald Welte8a521132010-05-17 22:59:29 +0200101 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200102
Harald Welte8a521132010-05-17 22:59:29 +0200103 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200104 if (!ctx)
105 return NULL;
106 ctx->bvci = bvci;
107 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200108 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
109 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
110
Harald Weltea78b9c22010-05-17 23:02:42 +0200111 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200112
113 return ctx;
114}
115
Harald Welte9ba50052010-03-14 15:45:01 +0800116/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200117static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800118{
119 struct msgb *msg = bssgp_msgb_alloc();
120 struct bssgp_normal_hdr *bgph =
121 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
122
Harald Welte24a655f2010-04-30 19:54:29 +0200123 msgb_nsei(msg) = nsei;
124 msgb_bvci(msg) = ns_bvci;
125
Harald Welte9ba50052010-03-14 15:45:01 +0800126 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
127 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
128
Harald Welte24a655f2010-04-30 19:54:29 +0200129 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800130}
131
Harald Weltea8aa4df2010-05-30 22:00:53 +0200132/* 10.3.7 SUSPEND-ACK PDU */
133int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
134 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
135{
136 struct msgb *msg = bssgp_msgb_alloc();
137 struct bssgp_normal_hdr *bgph =
138 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
139 uint32_t _tlli;
140 uint8_t ra[6];
141
142 msgb_nsei(msg) = nsei;
143 msgb_bvci(msg) = 0; /* Signalling */
144 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
145
146 _tlli = htonl(tlli);
147 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
148 gsm48_construct_ra(ra, ra_id);
149 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
150 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
151
152 return gprs_ns_sendmsg(bssgp_nsi, msg);
153}
154
155/* 10.3.8 SUSPEND-NACK PDU */
156int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
157 uint8_t *cause)
158{
159 struct msgb *msg = bssgp_msgb_alloc();
160 struct bssgp_normal_hdr *bgph =
161 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
162 uint32_t _tlli;
163
164 msgb_nsei(msg) = nsei;
165 msgb_bvci(msg) = 0; /* Signalling */
166 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
167
168 _tlli = htonl(tlli);
169 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
170 if (cause)
171 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
172
173 return gprs_ns_sendmsg(bssgp_nsi, msg);
174}
175
176/* 10.3.10 RESUME-ACK PDU */
177int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
178 const struct gprs_ra_id *ra_id)
179{
180 struct msgb *msg = bssgp_msgb_alloc();
181 struct bssgp_normal_hdr *bgph =
182 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
183 uint32_t _tlli;
184 uint8_t ra[6];
185
186 msgb_nsei(msg) = nsei;
187 msgb_bvci(msg) = 0; /* Signalling */
188 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
189
190 _tlli = htonl(tlli);
191 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
192 gsm48_construct_ra(ra, ra_id);
193 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
194
195 return gprs_ns_sendmsg(bssgp_nsi, msg);
196}
197
198/* 10.3.11 RESUME-NACK PDU */
199int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
200 const struct gprs_ra_id *ra_id, uint8_t *cause)
201{
202 struct msgb *msg = bssgp_msgb_alloc();
203 struct bssgp_normal_hdr *bgph =
204 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
205 uint32_t _tlli;
206 uint8_t ra[6];
207
208 msgb_nsei(msg) = nsei;
209 msgb_bvci(msg) = 0; /* Signalling */
210 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
211
212 _tlli = htonl(tlli);
213 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
214 gsm48_construct_ra(ra, ra_id);
215 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
216 if (cause)
217 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
218
219 return gprs_ns_sendmsg(bssgp_nsi, msg);
220}
221
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200222uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200223{
224 /* 6 octets RAC */
225 gsm48_parse_ra(raid, buf);
226 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200227 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200228}
229
Harald Welte3fddf3c2010-05-01 16:48:27 +0200230/* Chapter 8.4 BVC-Reset Procedure */
231static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
232 uint16_t ns_bvci)
233{
Harald Welte8a521132010-05-17 22:59:29 +0200234 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200235 uint16_t nsei = msgb_nsei(msg);
236 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200237 int rc;
238
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200239 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Weltee9686b62010-05-31 18:07:17 +0200240 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200241 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
242
Harald Welte6752fa42010-05-02 09:23:16 +0200243 /* look-up or create the BTS context for this BVC */
244 bctx = btsctx_by_bvci_nsei(bvci, nsei);
245 if (!bctx)
246 bctx = btsctx_alloc(bvci, nsei);
247
Harald Welte25de8112010-05-13 21:26:28 +0200248 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
249 bctx->state &= ~BVC_S_BLOCKED;
250
Harald Welte3fddf3c2010-05-01 16:48:27 +0200251 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
252 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200253 if (bvci != 0 && bvci != 1) {
254 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Weltee9686b62010-05-31 18:07:17 +0200255 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200256 "missing mandatory IE\n", bvci);
257 return -EINVAL;
258 }
259 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200260 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
261 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200262 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200263 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
264 bctx->ra_id.rac, bctx->cell_id, bvci);
265 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200266
Harald Welte6752fa42010-05-02 09:23:16 +0200267 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200268 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200269 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200270 return 0;
271}
272
Harald Welte25de8112010-05-13 21:26:28 +0200273static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
274{
275 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200276 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200277
278 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200279 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200280 /* 8.3.2: Signalling BVC shall never be blocked */
281 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
282 "received block for signalling BVC!?!\n",
283 msgb_nsei(msg), msgb_bvci(msg));
284 return 0;
285 }
Harald Welte25de8112010-05-13 21:26:28 +0200286
Harald Weltee9686b62010-05-31 18:07:17 +0200287 LOGP(DBSSGP, LOGL_INFO, "BSSGP BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200288
289 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
290 if (!ptp_ctx)
291 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
292
293 ptp_ctx->state |= BVC_S_BLOCKED;
294 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
295
296 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
297
298 /* We always acknowledge the BLOCKing */
299 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
300 bvci, msgb_bvci(msg));
301};
302
303static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
304{
305 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200306 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200307
308 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200309 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200310 /* 8.3.2: Signalling BVC shall never be blocked */
311 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
312 "received unblock for signalling BVC!?!\n",
313 msgb_nsei(msg), msgb_bvci(msg));
314 return 0;
315 }
Harald Welte25de8112010-05-13 21:26:28 +0200316
Harald Weltee9686b62010-05-31 18:07:17 +0200317 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200318
319 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
320 if (!ptp_ctx)
321 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
322
323 ptp_ctx->state &= ~BVC_S_BLOCKED;
324
325 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
326
327 /* We always acknowledge the unBLOCKing */
328 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
329 bvci, msgb_bvci(msg));
330};
331
Harald Welte9ba50052010-03-14 15:45:01 +0800332/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200333static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200334 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800335{
Harald Welteec19c102010-05-02 09:50:42 +0200336 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800337
Harald Welte6752fa42010-05-02 09:23:16 +0200338 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200339 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800340
Harald Weltee9686b62010-05-31 18:07:17 +0200341 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x UPLINK-UNITDATA\n", msgb_tlli(msg));
342
Harald Welte9ba50052010-03-14 15:45:01 +0800343 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200344 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200345 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
346 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
347 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200348 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200349 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200350
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200351 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800352 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
353 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800354
Harald Welte25de8112010-05-13 21:26:28 +0200355 return gprs_llc_rcvmsg(msg, tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800356}
357
Harald Welte25de8112010-05-13 21:26:28 +0200358static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200359 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800360{
Harald Welteec19c102010-05-02 09:50:42 +0200361 struct bssgp_normal_hdr *bgph =
362 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200363 struct gprs_ra_id raid;
364 uint32_t tlli;
Harald Welte9ba50052010-03-14 15:45:01 +0800365
Harald Welte25de8112010-05-13 21:26:28 +0200366 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200367 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
368 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
369 "missing mandatory IE\n", ctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200370 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200371 }
Harald Welte9ba50052010-03-14 15:45:01 +0800372
Harald Weltea8aa4df2010-05-30 22:00:53 +0200373 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200374
375 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%082x Rx SUSPEND\n",
376 ctx->bvci, tlli);
377
Harald Weltea8aa4df2010-05-30 22:00:53 +0200378 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
379
Harald Welte30bc19a2010-05-02 11:19:37 +0200380 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800381 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Weltea8aa4df2010-05-30 22:00:53 +0200382 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
383
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800384 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800385}
386
Harald Welte25de8112010-05-13 21:26:28 +0200387static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200388 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800389{
Harald Welteec19c102010-05-02 09:50:42 +0200390 struct bssgp_normal_hdr *bgph =
391 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200392 struct gprs_ra_id raid;
393 uint32_t tlli;
Harald Welte9ba50052010-03-14 15:45:01 +0800394
Harald Welte25de8112010-05-13 21:26:28 +0200395 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
396 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200397 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
398 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
399 "missing mandatory IE\n", ctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200400 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200401 }
Harald Welte9ba50052010-03-14 15:45:01 +0800402
Harald Weltea8aa4df2010-05-30 22:00:53 +0200403 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200404
405 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x RESUME\n", ctx->bvci, tlli);
406
Harald Weltea8aa4df2010-05-30 22:00:53 +0200407 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
408
Harald Welte30bc19a2010-05-02 11:19:37 +0200409 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800410 /* SEND RESUME_ACK or RESUME_NACK */
Harald Weltea8aa4df2010-05-30 22:00:53 +0200411 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800412 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800413}
414
Harald Weltee9686b62010-05-31 18:07:17 +0200415
416static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
417 struct bssgp_bvc_ctx *ctx)
418{
419 uint32_t tlli;
420
421 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
422 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
423 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
424 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
425 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
426 "missing mandatory IE\n", ctx->bvci);
427 }
428
429 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
430
431 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%u LLC DISCARDED\n",
432 ctx->bvci, tlli);
433
434 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
435
436 /* FIXME: send NM_LLC_DISCARDED to NM */
437 return 0;
438}
439
Harald Welte25de8112010-05-13 21:26:28 +0200440static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200441 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800442{
443
Harald Weltee9686b62010-05-31 18:07:17 +0200444 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
445 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800446
447 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
448 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
449 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
450 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200451 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
452 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
453 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800454 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200455 }
Harald Welte9ba50052010-03-14 15:45:01 +0800456
Harald Welte30bc19a2010-05-02 11:19:37 +0200457 /* FIXME: actually implement flow control */
458
Harald Welte9ba50052010-03-14 15:45:01 +0800459 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200460 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200461 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800462}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200463
Harald Welte25de8112010-05-13 21:26:28 +0200464/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
465static int gprs_bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200466 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800467{
Harald Welteec19c102010-05-02 09:50:42 +0200468 struct bssgp_normal_hdr *bgph =
469 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200470 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800471 int rc = 0;
472
Harald Welte58e65c92010-05-13 21:45:23 +0200473 /* If traffic is received on a BVC that is marked as blocked, the
474 * received PDU shall not be accepted and a STATUS PDU (Cause value:
475 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
476 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
477 uint16_t bvci = msgb_bvci(msg);
478 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
479 }
480
Harald Welte9ba50052010-03-14 15:45:01 +0800481 switch (pdu_type) {
482 case BSSGP_PDUT_UL_UNITDATA:
483 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200484 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800485 break;
486 case BSSGP_PDUT_RA_CAPABILITY:
487 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200488 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
489 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200490 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800491 /* FIXME: send RA_CAPA_UPDATE_ACK */
492 break;
493 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200494 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800495 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200496 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800497 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800498 case BSSGP_PDUT_FLOW_CONTROL_BVC:
499 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200500 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800501 break;
502 case BSSGP_PDUT_FLOW_CONTROL_MS:
503 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200504 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
505 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200506 /* FIXME: actually implement flow control */
507 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800508 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800509 case BSSGP_PDUT_STATUS:
510 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200511 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800512 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
513 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
514 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
515 case BSSGP_PDUT_MODIFY_BSS_PFC:
516 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200517 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x not [yet] "
518 "implemented\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200519 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800520 break;
521 /* those only exist in the SGSN -> BSS direction */
522 case BSSGP_PDUT_DL_UNITDATA:
523 case BSSGP_PDUT_PAGING_PS:
524 case BSSGP_PDUT_PAGING_CS:
525 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200526 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
527 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200528 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x only exists "
529 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200530 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
531 rc = -EINVAL;
532 break;
533 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200534 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n",
535 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200536 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
537 break;
538 }
539
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800540 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200541}
542
543/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
544static int gprs_bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200545 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200546{
547 struct bssgp_normal_hdr *bgph =
548 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
549 uint8_t pdu_type = bgph->pdu_type;
550 int rc = 0;
551 uint16_t ns_bvci = msgb_bvci(msg);
552 uint16_t bvci;
553
554 switch (bgph->pdu_type) {
555 case BSSGP_PDUT_SUSPEND:
556 /* MS wants to suspend */
557 rc = bssgp_rx_suspend(msg, tp, bctx);
558 break;
559 case BSSGP_PDUT_RESUME:
560 /* MS wants to resume */
561 rc = bssgp_rx_resume(msg, tp, bctx);
562 break;
563 case BSSGP_PDUT_FLUSH_LL_ACK:
564 /* BSS informs us it has performed LL FLUSH */
Harald Weltee9686b62010-05-31 18:07:17 +0200565 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx FLUSH LL ACK\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200566 /* FIXME: send NM_FLUSH_LL.res to NM */
567 break;
568 case BSSGP_PDUT_LLC_DISCARD:
569 /* BSS informs that some LLC PDU's have been discarded */
Harald Weltee9686b62010-05-31 18:07:17 +0200570 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200571 break;
572 case BSSGP_PDUT_BVC_BLOCK:
573 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200574 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200575 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
576 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
577 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200578 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200579 }
Harald Welte2677ea52010-05-31 17:16:36 +0200580 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200581 break;
582 case BSSGP_PDUT_BVC_UNBLOCK:
583 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200584 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
585 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
586 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200587 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200588 }
Harald Welte25de8112010-05-13 21:26:28 +0200589 rc = bssgp_rx_bvc_unblock(msg, tp);
590 break;
591 case BSSGP_PDUT_BVC_RESET:
592 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200593 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200594 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
595 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
596 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200597 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200598 }
Harald Welte25de8112010-05-13 21:26:28 +0200599 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
600 break;
601 case BSSGP_PDUT_STATUS:
602 /* Some exception has occurred */
Harald Weltee9686b62010-05-31 18:07:17 +0200603 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200604 /* FIXME: send NM_STATUS.ind to NM */
605 break;
606 /* those only exist in the SGSN -> BSS direction */
607 case BSSGP_PDUT_PAGING_PS:
608 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800609 case BSSGP_PDUT_SUSPEND_ACK:
610 case BSSGP_PDUT_SUSPEND_NACK:
611 case BSSGP_PDUT_RESUME_ACK:
612 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200613 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800614 case BSSGP_PDUT_BVC_BLOCK_ACK:
615 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
616 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltee9686b62010-05-31 18:07:17 +0200617 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x only exists "
618 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200619 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800620 rc = -EINVAL;
621 break;
622 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200623 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n",
624 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200625 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800626 break;
627 }
628
629 return rc;
630err_mand_ie:
631 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
632}
633
Harald Welte25de8112010-05-13 21:26:28 +0200634/* We expect msgb_bssgph() to point to the BSSGP header */
635int gprs_bssgp_rcvmsg(struct msgb *msg)
636{
637 struct bssgp_normal_hdr *bgph =
638 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
639 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
640 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200641 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200642 uint8_t pdu_type = bgph->pdu_type;
643 uint16_t ns_bvci = msgb_bvci(msg);
644 int data_len;
645 int rc = 0;
646
647 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
648
649 /* UNITDATA BSSGP headers have TLLI in front */
650 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
651 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
652 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
653 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
654 } else {
655 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
656 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
657 }
658
659 /* look-up or create the BTS context for this BVC */
660 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
661 /* Only a RESET PDU can create a new BVC context */
662 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
663 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
664 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
665 pdu_type);
666 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
667 }
668
Harald Welte16c8dbb2010-05-17 23:30:01 +0200669 if (bctx) {
Harald Welte4e5721d2010-05-17 23:41:43 +0200670 log_set_context(BSC_CTX_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +0200671 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
672 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
673 msgb_bssgp_len(msg));
674 }
675
Harald Welte61c07842010-05-18 11:57:08 +0200676 if (ns_bvci == BVCI_SIGNALLING)
Harald Welte25de8112010-05-13 21:26:28 +0200677 rc = gprs_bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +0200678 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +0200679 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
680 else
681 rc = gprs_bssgp_rx_ptp(msg, &tp, bctx);
682
683 return rc;
684}
685
Harald Welte6752fa42010-05-02 09:23:16 +0200686/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200687 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
688int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800689{
Harald Welte8a521132010-05-17 22:59:29 +0200690 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800691 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200692 uint8_t llc_pdu_tlv_hdr_len = 2;
693 uint8_t *llc_pdu_tlv, *qos_profile;
694 uint16_t pdu_lifetime = 1000; /* centi-seconds */
695 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
696 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200697 uint16_t bvci = msgb_bvci(msg);
698 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800699
Harald Welte30bc19a2010-05-02 11:19:37 +0200700 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +0200701 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200702 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200703 bvci);
704 return -EINVAL;
705 }
706
707 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200708 if (!bctx) {
709 /* FIXME: don't simply create missing context, but reject message */
Harald Welte30bc19a2010-05-02 11:19:37 +0200710 bctx = btsctx_alloc(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200711 }
Harald Welte9ba50052010-03-14 15:45:01 +0800712
713 if (msg->len > TVLV_MAX_ONEBYTE)
714 llc_pdu_tlv_hdr_len += 1;
715
716 /* prepend the tag and length of the LLC-PDU TLV */
717 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
718 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
719 if (llc_pdu_tlv_hdr_len > 2) {
720 llc_pdu_tlv[1] = msg_len >> 8;
721 llc_pdu_tlv[2] = msg_len & 0xff;
722 } else {
723 llc_pdu_tlv[1] = msg_len & 0x3f;
724 llc_pdu_tlv[1] |= 0x80;
725 }
726
727 /* FIXME: optional elements */
728
729 /* prepend the pdu lifetime */
730 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200731 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800732
733 /* prepend the QoS profile, TLLI and pdu type */
734 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
735 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200736 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800737 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
738
Harald Welte16c8dbb2010-05-17 23:30:01 +0200739 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
740 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
741
Harald Welte30bc19a2010-05-02 11:19:37 +0200742 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200743
744 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800745}