blob: 304fb5e2186efa38d94a2db440430ed4d368a0a3 [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 {
Harald Welte16c8dbb2010-05-17 23:30:01 +020045 BSSGP_CTR_PKTS_IN,
46 BSSGP_CTR_PKTS_OUT,
47 BSSGP_CTR_BYTES_IN,
48 BSSGP_CTR_BYTES_OUT,
Harald Welte25de8112010-05-13 21:26:28 +020049 BSSGP_CTR_BLOCKED,
50 BSSGP_CTR_DISCARDED,
51};
52
53static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Welte16c8dbb2010-05-17 23:30:01 +020054 { "packets.in", "Packets at BSSGP Level ( In)" },
55 { "packets.out","Packets at BSSGP Level (Out)" },
56 { "bytes.in", "Bytes at BSSGP Level ( In)" },
57 { "bytes.out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020058 { "blocked", "BVC Blocking count" },
59 { "discarded", "BVC LLC Discarded count" },
60};
61
62static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
63 .group_name_prefix = "bssgp.bss_ctx",
64 .group_description = "BSSGP Peer Statistics",
65 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
66 .ctr_desc = bssgp_ctr_description,
67};
68
Harald Weltea78b9c22010-05-17 23:02:42 +020069LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020070
71/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020072struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020073{
Harald Welte8a521132010-05-17 22:59:29 +020074 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020075
Harald Weltea78b9c22010-05-17 23:02:42 +020076 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020077 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
78 bctx->cell_id == cid)
79 return bctx;
80 }
81 return NULL;
82}
83
84/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +020085struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020086{
Harald Welte8a521132010-05-17 22:59:29 +020087 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020088
Harald Weltea78b9c22010-05-17 23:02:42 +020089 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020090 if (bctx->nsei == nsei && bctx->bvci == bvci)
91 return bctx;
92 }
93 return NULL;
94}
95
Harald Welte8a521132010-05-17 22:59:29 +020096struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020097{
Harald Welte8a521132010-05-17 22:59:29 +020098 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +020099
Harald Welte8a521132010-05-17 22:59:29 +0200100 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +0200101 if (!ctx)
102 return NULL;
103 ctx->bvci = bvci;
104 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +0200105 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
106 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
107
Harald Weltea78b9c22010-05-17 23:02:42 +0200108 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200109
110 return ctx;
111}
112
Harald Welte9ba50052010-03-14 15:45:01 +0800113/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200114static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800115{
116 struct msgb *msg = bssgp_msgb_alloc();
117 struct bssgp_normal_hdr *bgph =
118 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
119
Harald Welte24a655f2010-04-30 19:54:29 +0200120 msgb_nsei(msg) = nsei;
121 msgb_bvci(msg) = ns_bvci;
122
Harald Welte9ba50052010-03-14 15:45:01 +0800123 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
124 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
125
Harald Welte24a655f2010-04-30 19:54:29 +0200126 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800127}
128
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200129uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200130{
131 /* 6 octets RAC */
132 gsm48_parse_ra(raid, buf);
133 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200134 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200135}
136
Harald Welte3fddf3c2010-05-01 16:48:27 +0200137/* Chapter 8.4 BVC-Reset Procedure */
138static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
139 uint16_t ns_bvci)
140{
Harald Welte8a521132010-05-17 22:59:29 +0200141 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200142 uint16_t nsei = msgb_nsei(msg);
143 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200144 int rc;
145
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200146 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte25de8112010-05-13 21:26:28 +0200147 DEBUGPC(DBSSGP, "BVCI=%u RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200148 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
149
Harald Welte6752fa42010-05-02 09:23:16 +0200150 /* look-up or create the BTS context for this BVC */
151 bctx = btsctx_by_bvci_nsei(bvci, nsei);
152 if (!bctx)
153 bctx = btsctx_alloc(bvci, nsei);
154
Harald Welte25de8112010-05-13 21:26:28 +0200155 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
156 bctx->state &= ~BVC_S_BLOCKED;
157
Harald Welte3fddf3c2010-05-01 16:48:27 +0200158 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
159 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200160 if (bvci != 0 && bvci != 1) {
161 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200162 LOGP(DBSSGP, LOGL_ERROR, "BSSGP RESET BVCI=%u "
Harald Welte6752fa42010-05-02 09:23:16 +0200163 "missing mandatory IE\n", bvci);
164 return -EINVAL;
165 }
166 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200167 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
168 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200169 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200170 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
171 bctx->ra_id.rac, bctx->cell_id, bvci);
172 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200173
Harald Welte6752fa42010-05-02 09:23:16 +0200174 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200175 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200176 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200177 return 0;
178}
179
Harald Welte25de8112010-05-13 21:26:28 +0200180static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
181{
182 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200183 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200184
185 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte58e65c92010-05-13 21:45:23 +0200186 if (bvci == 0) {
187 /* 8.3.2: Signalling BVC shall never be blocked */
188 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
189 "received block for signalling BVC!?!\n",
190 msgb_nsei(msg), msgb_bvci(msg));
191 return 0;
192 }
Harald Welte25de8112010-05-13 21:26:28 +0200193
194 LOGP(DBSSGP, LOGL_INFO, "BVCI=%u BVC-BLOCK\n", bvci);
195
196 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
197 if (!ptp_ctx)
198 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
199
200 ptp_ctx->state |= BVC_S_BLOCKED;
201 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
202
203 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
204
205 /* We always acknowledge the BLOCKing */
206 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
207 bvci, msgb_bvci(msg));
208};
209
210static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
211{
212 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200213 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200214
215 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte58e65c92010-05-13 21:45:23 +0200216 if (bvci == 0) {
217 /* 8.3.2: Signalling BVC shall never be blocked */
218 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
219 "received unblock for signalling BVC!?!\n",
220 msgb_nsei(msg), msgb_bvci(msg));
221 return 0;
222 }
Harald Welte25de8112010-05-13 21:26:28 +0200223
224 DEBUGP(DBSSGP, "BVCI=%u BVC-UNBLOCK\n", bvci);
225
226 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
227 if (!ptp_ctx)
228 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
229
230 ptp_ctx->state &= ~BVC_S_BLOCKED;
231
232 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
233
234 /* We always acknowledge the unBLOCKing */
235 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
236 bvci, msgb_bvci(msg));
237};
238
Harald Welte9ba50052010-03-14 15:45:01 +0800239/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200240static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200241 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800242{
Harald Welteec19c102010-05-02 09:50:42 +0200243 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800244
Harald Welteb8a6a832010-05-11 05:54:22 +0200245 DEBUGP(DBSSGP, "BSSGP UL-UD\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800246
Harald Welte6752fa42010-05-02 09:23:16 +0200247 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200248 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800249
250 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200251 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
252 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
253 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200254
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200255 /* store pointer to LLC header and CELL ID in msgb->cb */
Harald Welte25de8112010-05-13 21:26:28 +0200256 msgb_llch(msg) = TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
257 msgb_bcid(msg) = TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800258
Harald Welte25de8112010-05-13 21:26:28 +0200259 return gprs_llc_rcvmsg(msg, tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800260}
261
Harald Welte25de8112010-05-13 21:26:28 +0200262static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200263 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800264{
Harald Welteec19c102010-05-02 09:50:42 +0200265 struct bssgp_normal_hdr *bgph =
266 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800267
Harald Welteb8a6a832010-05-11 05:54:22 +0200268 DEBUGP(DBSSGP, "BSSGP SUSPEND\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800269
Harald Welte25de8112010-05-13 21:26:28 +0200270 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
271 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA))
272 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800273
Harald Welte30bc19a2010-05-02 11:19:37 +0200274 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800275 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800276}
277
Harald Welte25de8112010-05-13 21:26:28 +0200278static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200279 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800280{
Harald Welteec19c102010-05-02 09:50:42 +0200281 struct bssgp_normal_hdr *bgph =
282 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800283
Harald Welteb8a6a832010-05-11 05:54:22 +0200284 DEBUGP(DBSSGP, "BSSGP RESUME\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800285
Harald Welte25de8112010-05-13 21:26:28 +0200286 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
287 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
288 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR))
289 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800290
Harald Welte30bc19a2010-05-02 11:19:37 +0200291 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800292 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800293}
294
Harald Welte25de8112010-05-13 21:26:28 +0200295static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200296 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800297{
298
Harald Welteb8a6a832010-05-11 05:54:22 +0200299 DEBUGP(DBSSGP, "BSSGP FC BVC\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800300
301 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
302 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
303 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
304 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
305 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
306 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
307
Harald Welte30bc19a2010-05-02 11:19:37 +0200308 /* FIXME: actually implement flow control */
309
Harald Welte9ba50052010-03-14 15:45:01 +0800310 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200311 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200312 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800313}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200314
Harald Welte25de8112010-05-13 21:26:28 +0200315/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
316static int gprs_bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200317 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800318{
Harald Welteec19c102010-05-02 09:50:42 +0200319 struct bssgp_normal_hdr *bgph =
320 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200321 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800322 int rc = 0;
323
Harald Welte58e65c92010-05-13 21:45:23 +0200324 /* If traffic is received on a BVC that is marked as blocked, the
325 * received PDU shall not be accepted and a STATUS PDU (Cause value:
326 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
327 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
328 uint16_t bvci = msgb_bvci(msg);
329 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
330 }
331
Harald Welte9ba50052010-03-14 15:45:01 +0800332 switch (pdu_type) {
333 case BSSGP_PDUT_UL_UNITDATA:
334 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200335 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800336 break;
337 case BSSGP_PDUT_RA_CAPABILITY:
338 /* BSS requests RA capability or IMSI */
Harald Welteb8a6a832010-05-11 05:54:22 +0200339 DEBUGP(DBSSGP, "BSSGP RA CAPABILITY UPDATE\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200340 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800341 /* FIXME: send RA_CAPA_UPDATE_ACK */
342 break;
343 case BSSGP_PDUT_RADIO_STATUS:
Harald Welteb8a6a832010-05-11 05:54:22 +0200344 DEBUGP(DBSSGP, "BSSGP RADIO STATUS\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800345 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200346 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800347 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800348 case BSSGP_PDUT_FLOW_CONTROL_BVC:
349 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200350 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800351 break;
352 case BSSGP_PDUT_FLOW_CONTROL_MS:
353 /* BSS informs us of available bandwidth to one MS */
Harald Welteb8a6a832010-05-11 05:54:22 +0200354 DEBUGP(DBSSGP, "BSSGP FC MS\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200355 /* FIXME: actually implement flow control */
356 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800357 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800358 case BSSGP_PDUT_STATUS:
359 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200360 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800361 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
362 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
363 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
364 case BSSGP_PDUT_MODIFY_BSS_PFC:
365 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200366 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x not [yet] implemented\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800367 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200368 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800369 break;
370 /* those only exist in the SGSN -> BSS direction */
371 case BSSGP_PDUT_DL_UNITDATA:
372 case BSSGP_PDUT_PAGING_PS:
373 case BSSGP_PDUT_PAGING_CS:
374 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200375 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
376 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
377 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
378 pdu_type);
379 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
380 rc = -EINVAL;
381 break;
382 default:
383 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
384 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
385 break;
386 }
387
388
389}
390
391/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
392static int gprs_bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200393 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200394{
395 struct bssgp_normal_hdr *bgph =
396 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
397 uint8_t pdu_type = bgph->pdu_type;
398 int rc = 0;
399 uint16_t ns_bvci = msgb_bvci(msg);
400 uint16_t bvci;
401
402 switch (bgph->pdu_type) {
403 case BSSGP_PDUT_SUSPEND:
404 /* MS wants to suspend */
405 rc = bssgp_rx_suspend(msg, tp, bctx);
406 break;
407 case BSSGP_PDUT_RESUME:
408 /* MS wants to resume */
409 rc = bssgp_rx_resume(msg, tp, bctx);
410 break;
411 case BSSGP_PDUT_FLUSH_LL_ACK:
412 /* BSS informs us it has performed LL FLUSH */
413 DEBUGP(DBSSGP, "BSSGP FLUSH LL\n");
414 /* FIXME: send NM_FLUSH_LL.res to NM */
415 break;
416 case BSSGP_PDUT_LLC_DISCARD:
417 /* BSS informs that some LLC PDU's have been discarded */
418 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
419 DEBUGP(DBSSGP, "BSSGP LLC DISCARDED\n");
420 /* FIXME: send NM_LLC_DISCARDED to NM */
421 break;
422 case BSSGP_PDUT_BVC_BLOCK:
423 /* BSS tells us that BVC shall be blocked */
424 DEBUGP(DBSSGP, "BSSGP BVC BLOCK ");
425 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
426 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
427 goto err_mand_ie;
428 rc = bssgp_rx_bvc_unblock(msg, tp);
429 break;
430 case BSSGP_PDUT_BVC_UNBLOCK:
431 /* BSS tells us that BVC shall be unblocked */
432 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
433 goto err_mand_ie;
434 rc = bssgp_rx_bvc_unblock(msg, tp);
435 break;
436 case BSSGP_PDUT_BVC_RESET:
437 /* BSS tells us that BVC init is required */
438 DEBUGP(DBSSGP, "BSSGP BVC RESET ");
439 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
440 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
441 goto err_mand_ie;
442 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
443 break;
444 case BSSGP_PDUT_STATUS:
445 /* Some exception has occurred */
446 /* FIXME: send NM_STATUS.ind to NM */
447 break;
448 /* those only exist in the SGSN -> BSS direction */
449 case BSSGP_PDUT_PAGING_PS:
450 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800451 case BSSGP_PDUT_SUSPEND_ACK:
452 case BSSGP_PDUT_SUSPEND_NACK:
453 case BSSGP_PDUT_RESUME_ACK:
454 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200455 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800456 case BSSGP_PDUT_BVC_BLOCK_ACK:
457 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
458 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welteb8a6a832010-05-11 05:54:22 +0200459 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800460 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200461 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800462 rc = -EINVAL;
463 break;
464 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200465 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200466 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800467 break;
468 }
469
470 return rc;
471err_mand_ie:
472 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
473}
474
Harald Welte25de8112010-05-13 21:26:28 +0200475/* We expect msgb_bssgph() to point to the BSSGP header */
476int gprs_bssgp_rcvmsg(struct msgb *msg)
477{
478 struct bssgp_normal_hdr *bgph =
479 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
480 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
481 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200482 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200483 uint8_t pdu_type = bgph->pdu_type;
484 uint16_t ns_bvci = msgb_bvci(msg);
485 int data_len;
486 int rc = 0;
487
488 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
489
490 /* UNITDATA BSSGP headers have TLLI in front */
491 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
492 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
493 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
494 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
495 } else {
496 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
497 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
498 }
499
500 /* look-up or create the BTS context for this BVC */
501 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
502 /* Only a RESET PDU can create a new BVC context */
503 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
504 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
505 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
506 pdu_type);
507 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
508 }
509
Harald Welte16c8dbb2010-05-17 23:30:01 +0200510 if (bctx) {
511 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
512 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
513 msgb_bssgp_len(msg));
514 }
515
Harald Welte25de8112010-05-13 21:26:28 +0200516 if (ns_bvci == 1)
517 rc = gprs_bssgp_rx_sign(msg, &tp, bctx);
518 else if (ns_bvci == 2)
519 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
520 else
521 rc = gprs_bssgp_rx_ptp(msg, &tp, bctx);
522
523 return rc;
524}
525
Harald Welte6752fa42010-05-02 09:23:16 +0200526/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200527 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
528int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800529{
Harald Welte8a521132010-05-17 22:59:29 +0200530 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800531 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200532 uint8_t llc_pdu_tlv_hdr_len = 2;
533 uint8_t *llc_pdu_tlv, *qos_profile;
534 uint16_t pdu_lifetime = 1000; /* centi-seconds */
535 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
536 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200537 uint16_t bvci = msgb_bvci(msg);
538 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800539
Harald Welte30bc19a2010-05-02 11:19:37 +0200540 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
541 if (bvci < 2) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200542 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200543 bvci);
544 return -EINVAL;
545 }
546
547 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200548 if (!bctx) {
549 /* FIXME: don't simply create missing context, but reject message */
Harald Welte30bc19a2010-05-02 11:19:37 +0200550 bctx = btsctx_alloc(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200551 }
Harald Welte9ba50052010-03-14 15:45:01 +0800552
553 if (msg->len > TVLV_MAX_ONEBYTE)
554 llc_pdu_tlv_hdr_len += 1;
555
556 /* prepend the tag and length of the LLC-PDU TLV */
557 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
558 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
559 if (llc_pdu_tlv_hdr_len > 2) {
560 llc_pdu_tlv[1] = msg_len >> 8;
561 llc_pdu_tlv[2] = msg_len & 0xff;
562 } else {
563 llc_pdu_tlv[1] = msg_len & 0x3f;
564 llc_pdu_tlv[1] |= 0x80;
565 }
566
567 /* FIXME: optional elements */
568
569 /* prepend the pdu lifetime */
570 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200571 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800572
573 /* prepend the QoS profile, TLLI and pdu type */
574 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
575 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200576 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800577 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
578
Harald Welte16c8dbb2010-05-17 23:30:01 +0200579 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
580 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
581
Harald Welte30bc19a2010-05-02 11:19:37 +0200582 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200583
584 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800585}