blob: f6b02bada638f33667e637cf68d85821c449ec19 [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 *
21 */
22
23#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020024#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080025
26#include <netinet/in.h>
27
28#include <osmocore/msgb.h>
29#include <osmocore/tlv.h>
Harald Welte6752fa42010-05-02 09:23:16 +020030#include <osmocore/talloc.h>
Harald Welte25de8112010-05-13 21:26:28 +020031#include <osmocore/rate_ctr.h>
Harald Welte6752fa42010-05-02 09:23:16 +020032
Harald Welte9ba50052010-03-14 15:45:01 +080033#include <openbsc/debug.h>
34#include <openbsc/gsm_data.h>
35#include <openbsc/gsm_04_08_gprs.h>
36#include <openbsc/gprs_bssgp.h>
37#include <openbsc/gprs_llc.h>
38#include <openbsc/gprs_ns.h>
39
Harald Welte6752fa42010-05-02 09:23:16 +020040void *bssgp_tall_ctx = NULL;
41
Harald Welte6752fa42010-05-02 09:23:16 +020042#define BVC_F_BLOCKED 0x0001
43
Harald Welte25de8112010-05-13 21:26:28 +020044enum bssgp_ctr {
45 BSSGP_CTR_BLOCKED,
46 BSSGP_CTR_DISCARDED,
47};
48
49static const struct rate_ctr_desc bssgp_ctr_description[] = {
50 { "blocked", "BVC Blocking count" },
51 { "discarded", "BVC LLC Discarded count" },
52};
53
54static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
55 .group_name_prefix = "bssgp.bss_ctx",
56 .group_description = "BSSGP Peer Statistics",
57 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
58 .ctr_desc = bssgp_ctr_description,
59};
60
61#define BVC_S_BLOCKED 0x0001
62
Harald Welte6752fa42010-05-02 09:23:16 +020063/* The per-BTS context that we keep on the SGSN side of the BSSGP link */
Harald Welte8a521132010-05-17 22:59:29 +020064struct bssgp_bvc_ctx {
Harald Welte6752fa42010-05-02 09:23:16 +020065 struct llist_head list;
66
67 /* parsed RA ID and Cell ID of the remote BTS */
68 struct gprs_ra_id ra_id;
69 uint16_t cell_id;
70
71 /* NSEI and BVCI of underlying Gb link. Together they
72 * uniquely identify a link to a BTS (5.4.4) */
73 uint16_t bvci;
74 uint16_t nsei;
75
Harald Welte25de8112010-05-13 21:26:28 +020076 uint32_t state;
77
78 struct rate_ctr_group *ctrg;
Harald Welte6752fa42010-05-02 09:23:16 +020079
80 /* we might want to add this as a shortcut later, avoiding the NSVC
81 * lookup for every packet, similar to a routing cache */
82 //struct gprs_nsvc *nsvc;
83};
Harald Weltefa270b92010-05-11 10:05:12 +020084static LLIST_HEAD(bts_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020085
86/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020087struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020088{
Harald Welte8a521132010-05-17 22:59:29 +020089 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020090
91 llist_for_each_entry(bctx, &bts_ctxts, list) {
92 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
93 bctx->cell_id == cid)
94 return bctx;
95 }
96 return NULL;
97}
98
99/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +0200100struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200101{
Harald Welte8a521132010-05-17 22:59:29 +0200102 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200103
104 llist_for_each_entry(bctx, &bts_ctxts, list) {
105 if (bctx->nsei == nsei && bctx->bvci == bvci)
106 return bctx;
107 }
108 return NULL;
109}
110
Harald Welte8a521132010-05-17 22:59:29 +0200111struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200112{
Harald Welte8a521132010-05-17 22:59:29 +0200113 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200114
Harald Welte8a521132010-05-17 22:59:29 +0200115 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200116 if (!ctx)
117 return NULL;
118 ctx->bvci = bvci;
119 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200120 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
121 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
122
Harald Welte6752fa42010-05-02 09:23:16 +0200123 llist_add(&ctx->list, &bts_ctxts);
124
125 return ctx;
126}
127
Harald Welte9ba50052010-03-14 15:45:01 +0800128/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200129static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800130{
131 struct msgb *msg = bssgp_msgb_alloc();
132 struct bssgp_normal_hdr *bgph =
133 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
134
Harald Welte24a655f2010-04-30 19:54:29 +0200135 msgb_nsei(msg) = nsei;
136 msgb_bvci(msg) = ns_bvci;
137
Harald Welte9ba50052010-03-14 15:45:01 +0800138 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
139 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
140
Harald Welte24a655f2010-04-30 19:54:29 +0200141 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800142}
143
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200144uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200145{
146 /* 6 octets RAC */
147 gsm48_parse_ra(raid, buf);
148 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200149 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200150}
151
Harald Welte3fddf3c2010-05-01 16:48:27 +0200152/* Chapter 8.4 BVC-Reset Procedure */
153static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
154 uint16_t ns_bvci)
155{
Harald Welte8a521132010-05-17 22:59:29 +0200156 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200157 uint16_t nsei = msgb_nsei(msg);
158 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200159 int rc;
160
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200161 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte25de8112010-05-13 21:26:28 +0200162 DEBUGPC(DBSSGP, "BVCI=%u RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200163 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
164
Harald Welte6752fa42010-05-02 09:23:16 +0200165 /* look-up or create the BTS context for this BVC */
166 bctx = btsctx_by_bvci_nsei(bvci, nsei);
167 if (!bctx)
168 bctx = btsctx_alloc(bvci, nsei);
169
Harald Welte25de8112010-05-13 21:26:28 +0200170 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
171 bctx->state &= ~BVC_S_BLOCKED;
172
Harald Welte3fddf3c2010-05-01 16:48:27 +0200173 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
174 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200175 if (bvci != 0 && bvci != 1) {
176 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200177 LOGP(DBSSGP, LOGL_ERROR, "BSSGP RESET BVCI=%u "
Harald Welte6752fa42010-05-02 09:23:16 +0200178 "missing mandatory IE\n", bvci);
179 return -EINVAL;
180 }
181 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200182 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
183 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200184 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200185 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
186 bctx->ra_id.rac, bctx->cell_id, bvci);
187 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200188
Harald Welte6752fa42010-05-02 09:23:16 +0200189 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200190 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200191 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200192 return 0;
193}
194
Harald Welte25de8112010-05-13 21:26:28 +0200195static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
196{
197 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200198 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200199
200 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte58e65c92010-05-13 21:45:23 +0200201 if (bvci == 0) {
202 /* 8.3.2: Signalling BVC shall never be blocked */
203 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
204 "received block for signalling BVC!?!\n",
205 msgb_nsei(msg), msgb_bvci(msg));
206 return 0;
207 }
Harald Welte25de8112010-05-13 21:26:28 +0200208
209 LOGP(DBSSGP, LOGL_INFO, "BVCI=%u BVC-BLOCK\n", bvci);
210
211 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
212 if (!ptp_ctx)
213 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
214
215 ptp_ctx->state |= BVC_S_BLOCKED;
216 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
217
218 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
219
220 /* We always acknowledge the BLOCKing */
221 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
222 bvci, msgb_bvci(msg));
223};
224
225static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
226{
227 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200228 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200229
230 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte58e65c92010-05-13 21:45:23 +0200231 if (bvci == 0) {
232 /* 8.3.2: Signalling BVC shall never be blocked */
233 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
234 "received unblock for signalling BVC!?!\n",
235 msgb_nsei(msg), msgb_bvci(msg));
236 return 0;
237 }
Harald Welte25de8112010-05-13 21:26:28 +0200238
239 DEBUGP(DBSSGP, "BVCI=%u BVC-UNBLOCK\n", bvci);
240
241 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
242 if (!ptp_ctx)
243 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
244
245 ptp_ctx->state &= ~BVC_S_BLOCKED;
246
247 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
248
249 /* We always acknowledge the unBLOCKing */
250 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
251 bvci, msgb_bvci(msg));
252};
253
Harald Welte9ba50052010-03-14 15:45:01 +0800254/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200255static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200256 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800257{
Harald Welteec19c102010-05-02 09:50:42 +0200258 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800259
Harald Welteb8a6a832010-05-11 05:54:22 +0200260 DEBUGP(DBSSGP, "BSSGP UL-UD\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800261
Harald Welte6752fa42010-05-02 09:23:16 +0200262 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200263 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800264
265 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200266 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
267 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
268 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200269
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200270 /* store pointer to LLC header and CELL ID in msgb->cb */
Harald Welte25de8112010-05-13 21:26:28 +0200271 msgb_llch(msg) = TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
272 msgb_bcid(msg) = TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800273
Harald Welte25de8112010-05-13 21:26:28 +0200274 return gprs_llc_rcvmsg(msg, tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800275}
276
Harald Welte25de8112010-05-13 21:26:28 +0200277static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200278 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800279{
Harald Welteec19c102010-05-02 09:50:42 +0200280 struct bssgp_normal_hdr *bgph =
281 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800282
Harald Welteb8a6a832010-05-11 05:54:22 +0200283 DEBUGP(DBSSGP, "BSSGP SUSPEND\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800284
Harald Welte25de8112010-05-13 21:26:28 +0200285 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
286 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA))
287 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800288
Harald Welte30bc19a2010-05-02 11:19:37 +0200289 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800290 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800291}
292
Harald Welte25de8112010-05-13 21:26:28 +0200293static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200294 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800295{
Harald Welteec19c102010-05-02 09:50:42 +0200296 struct bssgp_normal_hdr *bgph =
297 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800298
Harald Welteb8a6a832010-05-11 05:54:22 +0200299 DEBUGP(DBSSGP, "BSSGP RESUME\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800300
Harald Welte25de8112010-05-13 21:26:28 +0200301 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
302 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
303 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR))
304 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800305
Harald Welte30bc19a2010-05-02 11:19:37 +0200306 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800307 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800308}
309
Harald Welte25de8112010-05-13 21:26:28 +0200310static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200311 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800312{
313
Harald Welteb8a6a832010-05-11 05:54:22 +0200314 DEBUGP(DBSSGP, "BSSGP FC BVC\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800315
316 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
317 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
318 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
319 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
320 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
321 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
322
Harald Welte30bc19a2010-05-02 11:19:37 +0200323 /* FIXME: actually implement flow control */
324
Harald Welte9ba50052010-03-14 15:45:01 +0800325 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200326 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200327 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800328}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200329
Harald Welte25de8112010-05-13 21:26:28 +0200330/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
331static int gprs_bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200332 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800333{
Harald Welteec19c102010-05-02 09:50:42 +0200334 struct bssgp_normal_hdr *bgph =
335 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200336 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800337 int rc = 0;
338
Harald Welte58e65c92010-05-13 21:45:23 +0200339 /* If traffic is received on a BVC that is marked as blocked, the
340 * received PDU shall not be accepted and a STATUS PDU (Cause value:
341 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
342 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
343 uint16_t bvci = msgb_bvci(msg);
344 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
345 }
346
Harald Welte9ba50052010-03-14 15:45:01 +0800347 switch (pdu_type) {
348 case BSSGP_PDUT_UL_UNITDATA:
349 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200350 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800351 break;
352 case BSSGP_PDUT_RA_CAPABILITY:
353 /* BSS requests RA capability or IMSI */
Harald Welteb8a6a832010-05-11 05:54:22 +0200354 DEBUGP(DBSSGP, "BSSGP RA CAPABILITY UPDATE\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200355 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800356 /* FIXME: send RA_CAPA_UPDATE_ACK */
357 break;
358 case BSSGP_PDUT_RADIO_STATUS:
Harald Welteb8a6a832010-05-11 05:54:22 +0200359 DEBUGP(DBSSGP, "BSSGP RADIO STATUS\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800360 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200361 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800362 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800363 case BSSGP_PDUT_FLOW_CONTROL_BVC:
364 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200365 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800366 break;
367 case BSSGP_PDUT_FLOW_CONTROL_MS:
368 /* BSS informs us of available bandwidth to one MS */
Harald Welteb8a6a832010-05-11 05:54:22 +0200369 DEBUGP(DBSSGP, "BSSGP FC MS\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200370 /* FIXME: actually implement flow control */
371 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800372 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800373 case BSSGP_PDUT_STATUS:
374 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200375 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800376 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
377 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
378 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
379 case BSSGP_PDUT_MODIFY_BSS_PFC:
380 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200381 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x not [yet] implemented\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800382 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200383 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800384 break;
385 /* those only exist in the SGSN -> BSS direction */
386 case BSSGP_PDUT_DL_UNITDATA:
387 case BSSGP_PDUT_PAGING_PS:
388 case BSSGP_PDUT_PAGING_CS:
389 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200390 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
391 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
392 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
393 pdu_type);
394 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
395 rc = -EINVAL;
396 break;
397 default:
398 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
399 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
400 break;
401 }
402
403
404}
405
406/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
407static int gprs_bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200408 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200409{
410 struct bssgp_normal_hdr *bgph =
411 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
412 uint8_t pdu_type = bgph->pdu_type;
413 int rc = 0;
414 uint16_t ns_bvci = msgb_bvci(msg);
415 uint16_t bvci;
416
417 switch (bgph->pdu_type) {
418 case BSSGP_PDUT_SUSPEND:
419 /* MS wants to suspend */
420 rc = bssgp_rx_suspend(msg, tp, bctx);
421 break;
422 case BSSGP_PDUT_RESUME:
423 /* MS wants to resume */
424 rc = bssgp_rx_resume(msg, tp, bctx);
425 break;
426 case BSSGP_PDUT_FLUSH_LL_ACK:
427 /* BSS informs us it has performed LL FLUSH */
428 DEBUGP(DBSSGP, "BSSGP FLUSH LL\n");
429 /* FIXME: send NM_FLUSH_LL.res to NM */
430 break;
431 case BSSGP_PDUT_LLC_DISCARD:
432 /* BSS informs that some LLC PDU's have been discarded */
433 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
434 DEBUGP(DBSSGP, "BSSGP LLC DISCARDED\n");
435 /* FIXME: send NM_LLC_DISCARDED to NM */
436 break;
437 case BSSGP_PDUT_BVC_BLOCK:
438 /* BSS tells us that BVC shall be blocked */
439 DEBUGP(DBSSGP, "BSSGP BVC BLOCK ");
440 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
441 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
442 goto err_mand_ie;
443 rc = bssgp_rx_bvc_unblock(msg, tp);
444 break;
445 case BSSGP_PDUT_BVC_UNBLOCK:
446 /* BSS tells us that BVC shall be unblocked */
447 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
448 goto err_mand_ie;
449 rc = bssgp_rx_bvc_unblock(msg, tp);
450 break;
451 case BSSGP_PDUT_BVC_RESET:
452 /* BSS tells us that BVC init is required */
453 DEBUGP(DBSSGP, "BSSGP BVC RESET ");
454 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
455 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
456 goto err_mand_ie;
457 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
458 break;
459 case BSSGP_PDUT_STATUS:
460 /* Some exception has occurred */
461 /* FIXME: send NM_STATUS.ind to NM */
462 break;
463 /* those only exist in the SGSN -> BSS direction */
464 case BSSGP_PDUT_PAGING_PS:
465 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800466 case BSSGP_PDUT_SUSPEND_ACK:
467 case BSSGP_PDUT_SUSPEND_NACK:
468 case BSSGP_PDUT_RESUME_ACK:
469 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200470 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800471 case BSSGP_PDUT_BVC_BLOCK_ACK:
472 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
473 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welteb8a6a832010-05-11 05:54:22 +0200474 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800475 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200476 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800477 rc = -EINVAL;
478 break;
479 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200480 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200481 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800482 break;
483 }
484
485 return rc;
486err_mand_ie:
487 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
488}
489
Harald Welte25de8112010-05-13 21:26:28 +0200490/* We expect msgb_bssgph() to point to the BSSGP header */
491int gprs_bssgp_rcvmsg(struct msgb *msg)
492{
493 struct bssgp_normal_hdr *bgph =
494 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
495 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
496 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200497 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200498 uint8_t pdu_type = bgph->pdu_type;
499 uint16_t ns_bvci = msgb_bvci(msg);
500 int data_len;
501 int rc = 0;
502
503 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
504
505 /* UNITDATA BSSGP headers have TLLI in front */
506 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
507 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
508 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
509 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
510 } else {
511 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
512 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
513 }
514
515 /* look-up or create the BTS context for this BVC */
516 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
517 /* Only a RESET PDU can create a new BVC context */
518 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
519 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
520 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
521 pdu_type);
522 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
523 }
524
525 if (ns_bvci == 1)
526 rc = gprs_bssgp_rx_sign(msg, &tp, bctx);
527 else if (ns_bvci == 2)
528 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
529 else
530 rc = gprs_bssgp_rx_ptp(msg, &tp, bctx);
531
532 return rc;
533}
534
Harald Welte6752fa42010-05-02 09:23:16 +0200535/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200536 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
537int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800538{
Harald Welte8a521132010-05-17 22:59:29 +0200539 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800540 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200541 uint8_t llc_pdu_tlv_hdr_len = 2;
542 uint8_t *llc_pdu_tlv, *qos_profile;
543 uint16_t pdu_lifetime = 1000; /* centi-seconds */
544 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
545 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200546 uint16_t bvci = msgb_bvci(msg);
547 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800548
Harald Welte30bc19a2010-05-02 11:19:37 +0200549 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
550 if (bvci < 2) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200551 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200552 bvci);
553 return -EINVAL;
554 }
555
556 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200557 if (!bctx) {
558 /* FIXME: don't simply create missing context, but reject message */
Harald Welte30bc19a2010-05-02 11:19:37 +0200559 bctx = btsctx_alloc(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200560 }
Harald Welte9ba50052010-03-14 15:45:01 +0800561
562 if (msg->len > TVLV_MAX_ONEBYTE)
563 llc_pdu_tlv_hdr_len += 1;
564
565 /* prepend the tag and length of the LLC-PDU TLV */
566 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
567 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
568 if (llc_pdu_tlv_hdr_len > 2) {
569 llc_pdu_tlv[1] = msg_len >> 8;
570 llc_pdu_tlv[2] = msg_len & 0xff;
571 } else {
572 llc_pdu_tlv[1] = msg_len & 0x3f;
573 llc_pdu_tlv[1] |= 0x80;
574 }
575
576 /* FIXME: optional elements */
577
578 /* prepend the pdu lifetime */
579 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200580 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800581
582 /* prepend the QoS profile, TLLI and pdu type */
583 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
584 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200585 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800586 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
587
Harald Welte30bc19a2010-05-02 11:19:37 +0200588 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200589
590 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800591}