blob: 45e3de514b0d168d1d913136687f1fb3c3aee297 [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 */
64struct bssgp_bts_ctx {
65 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 */
87struct bssgp_bts_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
88{
89 struct bssgp_bts_ctx *bctx;
90
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 */
100struct bssgp_bts_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
101{
102 struct bssgp_bts_ctx *bctx;
103
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 Weltefa270b92010-05-11 10:05:12 +0200111struct bssgp_bts_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +0200112{
113 struct bssgp_bts_ctx *ctx;
114
115 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bts_ctx);
116 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 Welte6752fa42010-05-02 09:23:16 +0200156 struct bssgp_bts_ctx *bctx;
157 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;
198 struct bssgp_bts_ctx *ptp_ctx;
199
200 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
201
202 LOGP(DBSSGP, LOGL_INFO, "BVCI=%u BVC-BLOCK\n", bvci);
203
204 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
205 if (!ptp_ctx)
206 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
207
208 ptp_ctx->state |= BVC_S_BLOCKED;
209 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
210
211 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
212
213 /* We always acknowledge the BLOCKing */
214 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
215 bvci, msgb_bvci(msg));
216};
217
218static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
219{
220 uint16_t bvci;
221 struct bssgp_bts_ctx *ptp_ctx;
222
223 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
224
225 DEBUGP(DBSSGP, "BVCI=%u BVC-UNBLOCK\n", bvci);
226
227 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
228 if (!ptp_ctx)
229 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
230
231 ptp_ctx->state &= ~BVC_S_BLOCKED;
232
233 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
234
235 /* We always acknowledge the unBLOCKing */
236 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
237 bvci, msgb_bvci(msg));
238};
239
Harald Welte9ba50052010-03-14 15:45:01 +0800240/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200241static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
242 struct bssgp_bts_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800243{
Harald Welteec19c102010-05-02 09:50:42 +0200244 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800245
Harald Welteb8a6a832010-05-11 05:54:22 +0200246 DEBUGP(DBSSGP, "BSSGP UL-UD\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800247
Harald Welte6752fa42010-05-02 09:23:16 +0200248 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200249 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800250
251 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200252 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
253 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
254 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200255
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200256 /* store pointer to LLC header and CELL ID in msgb->cb */
Harald Welte25de8112010-05-13 21:26:28 +0200257 msgb_llch(msg) = TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
258 msgb_bcid(msg) = TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800259
Harald Welte25de8112010-05-13 21:26:28 +0200260 return gprs_llc_rcvmsg(msg, tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800261}
262
Harald Welte25de8112010-05-13 21:26:28 +0200263static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
264 struct bssgp_bts_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800265{
Harald Welteec19c102010-05-02 09:50:42 +0200266 struct bssgp_normal_hdr *bgph =
267 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800268
Harald Welteb8a6a832010-05-11 05:54:22 +0200269 DEBUGP(DBSSGP, "BSSGP SUSPEND\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800270
Harald Welte25de8112010-05-13 21:26:28 +0200271 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
272 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA))
273 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800274
Harald Welte30bc19a2010-05-02 11:19:37 +0200275 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800276 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800277}
278
Harald Welte25de8112010-05-13 21:26:28 +0200279static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
280 struct bssgp_bts_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800281{
Harald Welteec19c102010-05-02 09:50:42 +0200282 struct bssgp_normal_hdr *bgph =
283 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800284
Harald Welteb8a6a832010-05-11 05:54:22 +0200285 DEBUGP(DBSSGP, "BSSGP RESUME\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800286
Harald Welte25de8112010-05-13 21:26:28 +0200287 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
288 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
289 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR))
290 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800291
Harald Welte30bc19a2010-05-02 11:19:37 +0200292 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800293 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800294}
295
Harald Welte25de8112010-05-13 21:26:28 +0200296static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
297 struct bssgp_bts_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800298{
299
Harald Welteb8a6a832010-05-11 05:54:22 +0200300 DEBUGP(DBSSGP, "BSSGP FC BVC\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800301
302 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
303 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
304 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
305 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
306 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
307 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
308
Harald Welte30bc19a2010-05-02 11:19:37 +0200309 /* FIXME: actually implement flow control */
310
Harald Welte9ba50052010-03-14 15:45:01 +0800311 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200312 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200313 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800314}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200315
Harald Welte25de8112010-05-13 21:26:28 +0200316/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
317static int gprs_bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
318 struct bssgp_bts_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800319{
Harald Welteec19c102010-05-02 09:50:42 +0200320 struct bssgp_normal_hdr *bgph =
321 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200322 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800323 int rc = 0;
324
Harald Welte9ba50052010-03-14 15:45:01 +0800325 switch (pdu_type) {
326 case BSSGP_PDUT_UL_UNITDATA:
327 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200328 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800329 break;
330 case BSSGP_PDUT_RA_CAPABILITY:
331 /* BSS requests RA capability or IMSI */
Harald Welteb8a6a832010-05-11 05:54:22 +0200332 DEBUGP(DBSSGP, "BSSGP RA CAPABILITY UPDATE\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200333 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800334 /* FIXME: send RA_CAPA_UPDATE_ACK */
335 break;
336 case BSSGP_PDUT_RADIO_STATUS:
Harald Welteb8a6a832010-05-11 05:54:22 +0200337 DEBUGP(DBSSGP, "BSSGP RADIO STATUS\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800338 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200339 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800340 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800341 case BSSGP_PDUT_FLOW_CONTROL_BVC:
342 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200343 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800344 break;
345 case BSSGP_PDUT_FLOW_CONTROL_MS:
346 /* BSS informs us of available bandwidth to one MS */
Harald Welteb8a6a832010-05-11 05:54:22 +0200347 DEBUGP(DBSSGP, "BSSGP FC MS\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200348 /* FIXME: actually implement flow control */
349 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800350 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800351 case BSSGP_PDUT_STATUS:
352 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200353 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800354 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
355 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
356 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
357 case BSSGP_PDUT_MODIFY_BSS_PFC:
358 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200359 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x not [yet] implemented\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800360 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200361 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800362 break;
363 /* those only exist in the SGSN -> BSS direction */
364 case BSSGP_PDUT_DL_UNITDATA:
365 case BSSGP_PDUT_PAGING_PS:
366 case BSSGP_PDUT_PAGING_CS:
367 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200368 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
369 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
370 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
371 pdu_type);
372 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
373 rc = -EINVAL;
374 break;
375 default:
376 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
377 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
378 break;
379 }
380
381
382}
383
384/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
385static int gprs_bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
386 struct bssgp_bts_ctx *bctx)
387{
388 struct bssgp_normal_hdr *bgph =
389 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
390 uint8_t pdu_type = bgph->pdu_type;
391 int rc = 0;
392 uint16_t ns_bvci = msgb_bvci(msg);
393 uint16_t bvci;
394
395 switch (bgph->pdu_type) {
396 case BSSGP_PDUT_SUSPEND:
397 /* MS wants to suspend */
398 rc = bssgp_rx_suspend(msg, tp, bctx);
399 break;
400 case BSSGP_PDUT_RESUME:
401 /* MS wants to resume */
402 rc = bssgp_rx_resume(msg, tp, bctx);
403 break;
404 case BSSGP_PDUT_FLUSH_LL_ACK:
405 /* BSS informs us it has performed LL FLUSH */
406 DEBUGP(DBSSGP, "BSSGP FLUSH LL\n");
407 /* FIXME: send NM_FLUSH_LL.res to NM */
408 break;
409 case BSSGP_PDUT_LLC_DISCARD:
410 /* BSS informs that some LLC PDU's have been discarded */
411 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
412 DEBUGP(DBSSGP, "BSSGP LLC DISCARDED\n");
413 /* FIXME: send NM_LLC_DISCARDED to NM */
414 break;
415 case BSSGP_PDUT_BVC_BLOCK:
416 /* BSS tells us that BVC shall be blocked */
417 DEBUGP(DBSSGP, "BSSGP BVC BLOCK ");
418 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
419 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
420 goto err_mand_ie;
421 rc = bssgp_rx_bvc_unblock(msg, tp);
422 break;
423 case BSSGP_PDUT_BVC_UNBLOCK:
424 /* BSS tells us that BVC shall be unblocked */
425 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI))
426 goto err_mand_ie;
427 rc = bssgp_rx_bvc_unblock(msg, tp);
428 break;
429 case BSSGP_PDUT_BVC_RESET:
430 /* BSS tells us that BVC init is required */
431 DEBUGP(DBSSGP, "BSSGP BVC RESET ");
432 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
433 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE))
434 goto err_mand_ie;
435 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
436 break;
437 case BSSGP_PDUT_STATUS:
438 /* Some exception has occurred */
439 /* FIXME: send NM_STATUS.ind to NM */
440 break;
441 /* those only exist in the SGSN -> BSS direction */
442 case BSSGP_PDUT_PAGING_PS:
443 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800444 case BSSGP_PDUT_SUSPEND_ACK:
445 case BSSGP_PDUT_SUSPEND_NACK:
446 case BSSGP_PDUT_RESUME_ACK:
447 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200448 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800449 case BSSGP_PDUT_BVC_BLOCK_ACK:
450 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
451 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welteb8a6a832010-05-11 05:54:22 +0200452 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800453 pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200454 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800455 rc = -EINVAL;
456 break;
457 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200458 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200459 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800460 break;
461 }
462
463 return rc;
464err_mand_ie:
465 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
466}
467
Harald Welte25de8112010-05-13 21:26:28 +0200468/* We expect msgb_bssgph() to point to the BSSGP header */
469int gprs_bssgp_rcvmsg(struct msgb *msg)
470{
471 struct bssgp_normal_hdr *bgph =
472 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
473 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
474 struct tlv_parsed tp;
475 struct bssgp_bts_ctx *bctx;
476 uint8_t pdu_type = bgph->pdu_type;
477 uint16_t ns_bvci = msgb_bvci(msg);
478 int data_len;
479 int rc = 0;
480
481 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
482
483 /* UNITDATA BSSGP headers have TLLI in front */
484 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
485 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
486 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
487 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
488 } else {
489 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
490 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
491 }
492
493 /* look-up or create the BTS context for this BVC */
494 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
495 /* Only a RESET PDU can create a new BVC context */
496 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
497 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
498 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
499 pdu_type);
500 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
501 }
502
503 if (ns_bvci == 1)
504 rc = gprs_bssgp_rx_sign(msg, &tp, bctx);
505 else if (ns_bvci == 2)
506 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
507 else
508 rc = gprs_bssgp_rx_ptp(msg, &tp, bctx);
509
510 return rc;
511}
512
Harald Welte6752fa42010-05-02 09:23:16 +0200513/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200514 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
515int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800516{
Harald Welte6752fa42010-05-02 09:23:16 +0200517 struct bssgp_bts_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800518 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200519 uint8_t llc_pdu_tlv_hdr_len = 2;
520 uint8_t *llc_pdu_tlv, *qos_profile;
521 uint16_t pdu_lifetime = 1000; /* centi-seconds */
522 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
523 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200524 uint16_t bvci = msgb_bvci(msg);
525 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800526
Harald Welte30bc19a2010-05-02 11:19:37 +0200527 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
528 if (bvci < 2) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200529 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200530 bvci);
531 return -EINVAL;
532 }
533
534 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200535 if (!bctx) {
536 /* FIXME: don't simply create missing context, but reject message */
Harald Welte30bc19a2010-05-02 11:19:37 +0200537 bctx = btsctx_alloc(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200538 }
Harald Welte9ba50052010-03-14 15:45:01 +0800539
540 if (msg->len > TVLV_MAX_ONEBYTE)
541 llc_pdu_tlv_hdr_len += 1;
542
543 /* prepend the tag and length of the LLC-PDU TLV */
544 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
545 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
546 if (llc_pdu_tlv_hdr_len > 2) {
547 llc_pdu_tlv[1] = msg_len >> 8;
548 llc_pdu_tlv[2] = msg_len & 0xff;
549 } else {
550 llc_pdu_tlv[1] = msg_len & 0x3f;
551 llc_pdu_tlv[1] |= 0x80;
552 }
553
554 /* FIXME: optional elements */
555
556 /* prepend the pdu lifetime */
557 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200558 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800559
560 /* prepend the QoS profile, TLLI and pdu type */
561 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
562 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200563 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800564 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
565
Harald Welte30bc19a2010-05-02 11:19:37 +0200566 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200567
568 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800569}