blob: b9c3c7830a5b50f431b215de7245cced46ba2d8e [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
Harald Weltea78b9c22010-05-17 23:02:42 +020061LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020062
63/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020064struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020065{
Harald Welte8a521132010-05-17 22:59:29 +020066 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020067
Harald Weltea78b9c22010-05-17 23:02:42 +020068 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020069 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
70 bctx->cell_id == cid)
71 return bctx;
72 }
73 return NULL;
74}
75
76/* Find a BTS context based on BVCI+NSEI tuple */
Harald Welte8a521132010-05-17 22:59:29 +020077struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020078{
Harald Welte8a521132010-05-17 22:59:29 +020079 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020080
Harald Weltea78b9c22010-05-17 23:02:42 +020081 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020082 if (bctx->nsei == nsei && bctx->bvci == bvci)
83 return bctx;
84 }
85 return NULL;
86}
87
Harald Welte8a521132010-05-17 22:59:29 +020088struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020089{
Harald Welte8a521132010-05-17 22:59:29 +020090 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +020091
Harald Welte8a521132010-05-17 22:59:29 +020092 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +020093 if (!ctx)
94 return NULL;
95 ctx->bvci = bvci;
96 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +020097 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
98 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
99
Harald Weltea78b9c22010-05-17 23:02:42 +0200100 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200101
102 return ctx;
103}
104
Harald Welte9ba50052010-03-14 15:45:01 +0800105/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200106static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800107{
108 struct msgb *msg = bssgp_msgb_alloc();
109 struct bssgp_normal_hdr *bgph =
110 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
111
Harald Welte24a655f2010-04-30 19:54:29 +0200112 msgb_nsei(msg) = nsei;
113 msgb_bvci(msg) = ns_bvci;
114
Harald Welte9ba50052010-03-14 15:45:01 +0800115 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
116 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
117
Harald Welte24a655f2010-04-30 19:54:29 +0200118 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800119}
120
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200121uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200122{
123 /* 6 octets RAC */
124 gsm48_parse_ra(raid, buf);
125 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200126 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200127}
128
Harald Welte3fddf3c2010-05-01 16:48:27 +0200129/* Chapter 8.4 BVC-Reset Procedure */
130static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
131 uint16_t ns_bvci)
132{
Harald Welte8a521132010-05-17 22:59:29 +0200133 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200134 uint16_t nsei = msgb_nsei(msg);
135 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200136 int rc;
137
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200138 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte25de8112010-05-13 21:26:28 +0200139 DEBUGPC(DBSSGP, "BVCI=%u RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200140 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
141
Harald Welte6752fa42010-05-02 09:23:16 +0200142 /* look-up or create the BTS context for this BVC */
143 bctx = btsctx_by_bvci_nsei(bvci, nsei);
144 if (!bctx)
145 bctx = btsctx_alloc(bvci, nsei);
146
Harald Welte25de8112010-05-13 21:26:28 +0200147 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
148 bctx->state &= ~BVC_S_BLOCKED;
149
Harald Welte3fddf3c2010-05-01 16:48:27 +0200150 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
151 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200152 if (bvci != 0 && bvci != 1) {
153 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200154 LOGP(DBSSGP, LOGL_ERROR, "BSSGP RESET BVCI=%u "
Harald Welte6752fa42010-05-02 09:23:16 +0200155 "missing mandatory IE\n", bvci);
156 return -EINVAL;
157 }
158 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200159 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
160 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200161 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200162 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
163 bctx->ra_id.rac, bctx->cell_id, bvci);
164 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200165
Harald Welte6752fa42010-05-02 09:23:16 +0200166 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200167 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200168 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200169 return 0;
170}
171
Harald Welte25de8112010-05-13 21:26:28 +0200172static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
173{
174 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200175 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200176
177 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte58e65c92010-05-13 21:45:23 +0200178 if (bvci == 0) {
179 /* 8.3.2: Signalling BVC shall never be blocked */
180 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
181 "received block for signalling BVC!?!\n",
182 msgb_nsei(msg), msgb_bvci(msg));
183 return 0;
184 }
Harald Welte25de8112010-05-13 21:26:28 +0200185
186 LOGP(DBSSGP, LOGL_INFO, "BVCI=%u BVC-BLOCK\n", bvci);
187
188 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
189 if (!ptp_ctx)
190 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
191
192 ptp_ctx->state |= BVC_S_BLOCKED;
193 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
194
195 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
196
197 /* We always acknowledge the BLOCKing */
198 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
199 bvci, msgb_bvci(msg));
200};
201
202static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
203{
204 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200205 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200206
207 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte58e65c92010-05-13 21:45:23 +0200208 if (bvci == 0) {
209 /* 8.3.2: Signalling BVC shall never be blocked */
210 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
211 "received unblock for signalling BVC!?!\n",
212 msgb_nsei(msg), msgb_bvci(msg));
213 return 0;
214 }
Harald Welte25de8112010-05-13 21:26:28 +0200215
216 DEBUGP(DBSSGP, "BVCI=%u BVC-UNBLOCK\n", bvci);
217
218 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
219 if (!ptp_ctx)
220 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
221
222 ptp_ctx->state &= ~BVC_S_BLOCKED;
223
224 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
225
226 /* We always acknowledge the unBLOCKing */
227 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
228 bvci, msgb_bvci(msg));
229};
230
Harald Welte9ba50052010-03-14 15:45:01 +0800231/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200232static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200233 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800234{
Harald Welteec19c102010-05-02 09:50:42 +0200235 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800236
Harald Welteb8a6a832010-05-11 05:54:22 +0200237 DEBUGP(DBSSGP, "BSSGP UL-UD\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800238
Harald Welte6752fa42010-05-02 09:23:16 +0200239 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200240 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800241
242 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200243 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
244 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
245 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200246
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200247 /* store pointer to LLC header and CELL ID in msgb->cb */
Harald Welte25de8112010-05-13 21:26:28 +0200248 msgb_llch(msg) = TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
249 msgb_bcid(msg) = TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800250
Harald Welte25de8112010-05-13 21:26:28 +0200251 return gprs_llc_rcvmsg(msg, tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800252}
253
Harald Welte25de8112010-05-13 21:26:28 +0200254static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200255 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800256{
Harald Welteec19c102010-05-02 09:50:42 +0200257 struct bssgp_normal_hdr *bgph =
258 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800259
Harald Welteb8a6a832010-05-11 05:54:22 +0200260 DEBUGP(DBSSGP, "BSSGP SUSPEND\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800261
Harald Welte25de8112010-05-13 21:26:28 +0200262 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
263 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA))
264 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800265
Harald Welte30bc19a2010-05-02 11:19:37 +0200266 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800267 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800268}
269
Harald Welte25de8112010-05-13 21:26:28 +0200270static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200271 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800272{
Harald Welteec19c102010-05-02 09:50:42 +0200273 struct bssgp_normal_hdr *bgph =
274 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800275
Harald Welteb8a6a832010-05-11 05:54:22 +0200276 DEBUGP(DBSSGP, "BSSGP RESUME\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800277
Harald Welte25de8112010-05-13 21:26:28 +0200278 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
279 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
280 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR))
281 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800282
Harald Welte30bc19a2010-05-02 11:19:37 +0200283 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800284 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800285}
286
Harald Welte25de8112010-05-13 21:26:28 +0200287static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200288 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800289{
290
Harald Welteb8a6a832010-05-11 05:54:22 +0200291 DEBUGP(DBSSGP, "BSSGP FC BVC\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800292
293 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
294 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
295 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
296 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
297 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
298 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
299
Harald Welte30bc19a2010-05-02 11:19:37 +0200300 /* FIXME: actually implement flow control */
301
Harald Welte9ba50052010-03-14 15:45:01 +0800302 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200303 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200304 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800305}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200306
Harald Welte25de8112010-05-13 21:26:28 +0200307/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
308static int gprs_bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200309 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800310{
Harald Welteec19c102010-05-02 09:50:42 +0200311 struct bssgp_normal_hdr *bgph =
312 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200313 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800314 int rc = 0;
315
Harald Welte58e65c92010-05-13 21:45:23 +0200316 /* If traffic is received on a BVC that is marked as blocked, the
317 * received PDU shall not be accepted and a STATUS PDU (Cause value:
318 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
319 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
320 uint16_t bvci = msgb_bvci(msg);
321 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
322 }
323
Harald Welte9ba50052010-03-14 15:45:01 +0800324 switch (pdu_type) {
325 case BSSGP_PDUT_UL_UNITDATA:
326 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200327 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800328 break;
329 case BSSGP_PDUT_RA_CAPABILITY:
330 /* BSS requests RA capability or IMSI */
Harald Welteb8a6a832010-05-11 05:54:22 +0200331 DEBUGP(DBSSGP, "BSSGP RA CAPABILITY UPDATE\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200332 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800333 /* FIXME: send RA_CAPA_UPDATE_ACK */
334 break;
335 case BSSGP_PDUT_RADIO_STATUS:
Harald Welteb8a6a832010-05-11 05:54:22 +0200336 DEBUGP(DBSSGP, "BSSGP RADIO STATUS\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800337 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200338 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800339 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800340 case BSSGP_PDUT_FLOW_CONTROL_BVC:
341 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200342 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800343 break;
344 case BSSGP_PDUT_FLOW_CONTROL_MS:
345 /* BSS informs us of available bandwidth to one MS */
Harald Welteb8a6a832010-05-11 05:54:22 +0200346 DEBUGP(DBSSGP, "BSSGP FC MS\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200347 /* FIXME: actually implement flow control */
348 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800349 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800350 case BSSGP_PDUT_STATUS:
351 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200352 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800353 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
354 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
355 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
356 case BSSGP_PDUT_MODIFY_BSS_PFC:
357 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200358 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x not [yet] implemented\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800359 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200360 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800361 break;
362 /* those only exist in the SGSN -> BSS direction */
363 case BSSGP_PDUT_DL_UNITDATA:
364 case BSSGP_PDUT_PAGING_PS:
365 case BSSGP_PDUT_PAGING_CS:
366 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200367 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
368 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
369 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
370 pdu_type);
371 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
372 rc = -EINVAL;
373 break;
374 default:
375 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
376 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
377 break;
378 }
379
380
381}
382
383/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
384static int gprs_bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200385 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200386{
387 struct bssgp_normal_hdr *bgph =
388 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
389 uint8_t pdu_type = bgph->pdu_type;
390 int rc = 0;
391 uint16_t ns_bvci = msgb_bvci(msg);
392 uint16_t bvci;
393
394 switch (bgph->pdu_type) {
395 case BSSGP_PDUT_SUSPEND:
396 /* MS wants to suspend */
397 rc = bssgp_rx_suspend(msg, tp, bctx);
398 break;
399 case BSSGP_PDUT_RESUME:
400 /* MS wants to resume */
401 rc = bssgp_rx_resume(msg, tp, bctx);
402 break;
403 case BSSGP_PDUT_FLUSH_LL_ACK:
404 /* BSS informs us it has performed LL FLUSH */
405 DEBUGP(DBSSGP, "BSSGP FLUSH LL\n");
406 /* FIXME: send NM_FLUSH_LL.res to NM */
407 break;
408 case BSSGP_PDUT_LLC_DISCARD:
409 /* BSS informs that some LLC PDU's have been discarded */
410 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
411 DEBUGP(DBSSGP, "BSSGP LLC DISCARDED\n");
412 /* FIXME: send NM_LLC_DISCARDED to NM */
413 break;
414 case BSSGP_PDUT_BVC_BLOCK:
415 /* BSS tells us that BVC shall be blocked */
416 DEBUGP(DBSSGP, "BSSGP BVC BLOCK ");
417 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
418 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
419 goto err_mand_ie;
420 rc = bssgp_rx_bvc_unblock(msg, tp);
421 break;
422 case BSSGP_PDUT_BVC_UNBLOCK:
423 /* BSS tells us that BVC shall be unblocked */
424 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
425 goto err_mand_ie;
426 rc = bssgp_rx_bvc_unblock(msg, tp);
427 break;
428 case BSSGP_PDUT_BVC_RESET:
429 /* BSS tells us that BVC init is required */
430 DEBUGP(DBSSGP, "BSSGP BVC RESET ");
431 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
432 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
433 goto err_mand_ie;
434 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
435 break;
436 case BSSGP_PDUT_STATUS:
437 /* Some exception has occurred */
438 /* FIXME: send NM_STATUS.ind to NM */
439 break;
440 /* those only exist in the SGSN -> BSS direction */
441 case BSSGP_PDUT_PAGING_PS:
442 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800443 case BSSGP_PDUT_SUSPEND_ACK:
444 case BSSGP_PDUT_SUSPEND_NACK:
445 case BSSGP_PDUT_RESUME_ACK:
446 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200447 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800448 case BSSGP_PDUT_BVC_BLOCK_ACK:
449 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
450 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welteb8a6a832010-05-11 05:54:22 +0200451 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800452 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200453 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800454 rc = -EINVAL;
455 break;
456 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200457 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200458 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800459 break;
460 }
461
462 return rc;
463err_mand_ie:
464 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
465}
466
Harald Welte25de8112010-05-13 21:26:28 +0200467/* We expect msgb_bssgph() to point to the BSSGP header */
468int gprs_bssgp_rcvmsg(struct msgb *msg)
469{
470 struct bssgp_normal_hdr *bgph =
471 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
472 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
473 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200474 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200475 uint8_t pdu_type = bgph->pdu_type;
476 uint16_t ns_bvci = msgb_bvci(msg);
477 int data_len;
478 int rc = 0;
479
480 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
481
482 /* UNITDATA BSSGP headers have TLLI in front */
483 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
484 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
485 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
486 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
487 } else {
488 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
489 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
490 }
491
492 /* look-up or create the BTS context for this BVC */
493 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
494 /* Only a RESET PDU can create a new BVC context */
495 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
496 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
497 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
498 pdu_type);
499 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
500 }
501
502 if (ns_bvci == 1)
503 rc = gprs_bssgp_rx_sign(msg, &tp, bctx);
504 else if (ns_bvci == 2)
505 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
506 else
507 rc = gprs_bssgp_rx_ptp(msg, &tp, bctx);
508
509 return rc;
510}
511
Harald Welte6752fa42010-05-02 09:23:16 +0200512/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200513 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
514int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800515{
Harald Welte8a521132010-05-17 22:59:29 +0200516 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800517 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200518 uint8_t llc_pdu_tlv_hdr_len = 2;
519 uint8_t *llc_pdu_tlv, *qos_profile;
520 uint16_t pdu_lifetime = 1000; /* centi-seconds */
521 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
522 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200523 uint16_t bvci = msgb_bvci(msg);
524 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800525
Harald Welte30bc19a2010-05-02 11:19:37 +0200526 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
527 if (bvci < 2) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200528 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200529 bvci);
530 return -EINVAL;
531 }
532
533 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200534 if (!bctx) {
535 /* FIXME: don't simply create missing context, but reject message */
Harald Welte30bc19a2010-05-02 11:19:37 +0200536 bctx = btsctx_alloc(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200537 }
Harald Welte9ba50052010-03-14 15:45:01 +0800538
539 if (msg->len > TVLV_MAX_ONEBYTE)
540 llc_pdu_tlv_hdr_len += 1;
541
542 /* prepend the tag and length of the LLC-PDU TLV */
543 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
544 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
545 if (llc_pdu_tlv_hdr_len > 2) {
546 llc_pdu_tlv[1] = msg_len >> 8;
547 llc_pdu_tlv[2] = msg_len & 0xff;
548 } else {
549 llc_pdu_tlv[1] = msg_len & 0x3f;
550 llc_pdu_tlv[1] |= 0x80;
551 }
552
553 /* FIXME: optional elements */
554
555 /* prepend the pdu lifetime */
556 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200557 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800558
559 /* prepend the QoS profile, TLLI and pdu type */
560 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
561 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200562 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800563 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
564
Harald Welte30bc19a2010-05-02 11:19:37 +0200565 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200566
567 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800568}