blob: 6bba1af8a9656ea682148383a53f88077e0f7db5 [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>
24#include <sys/types.h>
25
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>
31
Harald Welte9ba50052010-03-14 15:45:01 +080032#include <openbsc/debug.h>
33#include <openbsc/gsm_data.h>
34#include <openbsc/gsm_04_08_gprs.h>
35#include <openbsc/gprs_bssgp.h>
36#include <openbsc/gprs_llc.h>
37#include <openbsc/gprs_ns.h>
38
39/* global pointer to the gsm network data structure */
40/* FIXME: this must go! */
41extern struct gsm_network *bsc_gsmnet;
Harald Welte24a655f2010-04-30 19:54:29 +020042struct gprs_ns_inst *bssgp_nsi;
Harald Welte9ba50052010-03-14 15:45:01 +080043
Harald Welte6752fa42010-05-02 09:23:16 +020044void *bssgp_tall_ctx = NULL;
45
Harald Welte290671d2010-05-02 09:34:20 +020046/* BSSGP Protocol specific, not implementation specific */
47/* FIXME: This needs to go into libosmocore after finished */
48
Harald Welte9ba50052010-03-14 15:45:01 +080049/* Chapter 11.3.9 / Table 11.10: Cause coding */
Harald Welte290671d2010-05-02 09:34:20 +020050static const struct value_string bssgp_cause_strings[] = {
51 { BSSGP_CAUSE_PROC_OVERLOAD, "Processor overload" },
52 { BSSGP_CAUSE_EQUIP_FAIL, "Equipment Failure" },
53 { BSSGP_CAUSE_TRASIT_NET_FAIL, "Transit netowkr service failure" },
54 { BSSGP_CAUSE_CAPA_GREATER_0KPBS,"Transmission capacity modified" },
55 { BSSGP_CAUSE_UNKNOWN_MS, "Unknown MS" },
56 { BSSGP_CAUSE_UNKNOWN_BVCI, "Unknown BVCI" },
57 { BSSGP_CAUSE_CELL_TRAF_CONG, "Cell traffic congestion" },
58 { BSSGP_CAUSE_SGSN_CONG, "SGSN congestion" },
59 { BSSGP_CAUSE_OML_INTERV, "O&M intervention" },
60 { BSSGP_CAUSE_BVCI_BLOCKED, "BVCI blocked" },
61 { BSSGP_CAUSE_PFC_CREATE_FAIL, "PFC create failure" },
62 { BSSGP_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
63 { BSSGP_CAUSE_INV_MAND_INF, "Invalid mandatory information" },
64 { BSSGP_CAUSE_MISSING_MAND_IE, "Missing mandatory IE" },
65 { BSSGP_CAUSE_MISSING_COND_IE, "Missing conditional IE" },
66 { BSSGP_CAUSE_UNEXP_COND_IE, "Unexpected conditional IE" },
67 { BSSGP_CAUSE_COND_IE_ERR, "Conditional IE error" },
68 { BSSGP_CAUSE_PDU_INCOMP_STATE, "PDU incompatible with protocol state" },
69 { BSSGP_CAUSE_PROTO_ERR_UNSPEC, "Protocol error - unspecified" },
70 { BSSGP_CAUSE_PDU_INCOMP_FEAT, "PDU not compatible with feature set" },
71 { 0, NULL },
Harald Welte9ba50052010-03-14 15:45:01 +080072};
73
Harald Welte290671d2010-05-02 09:34:20 +020074const char *bssgp_cause_str(enum gprs_bssgp_cause cause)
Harald Welte9ba50052010-03-14 15:45:01 +080075{
Harald Welte290671d2010-05-02 09:34:20 +020076 return get_value_string(bssgp_cause_strings, cause);
Harald Welte9ba50052010-03-14 15:45:01 +080077}
78
Harald Welte290671d2010-05-02 09:34:20 +020079
80/* Our actual implementation */
81
Harald Welte6752fa42010-05-02 09:23:16 +020082#define BVC_F_BLOCKED 0x0001
83
84/* The per-BTS context that we keep on the SGSN side of the BSSGP link */
85struct bssgp_bts_ctx {
86 struct llist_head list;
87
88 /* parsed RA ID and Cell ID of the remote BTS */
89 struct gprs_ra_id ra_id;
90 uint16_t cell_id;
91
92 /* NSEI and BVCI of underlying Gb link. Together they
93 * uniquely identify a link to a BTS (5.4.4) */
94 uint16_t bvci;
95 uint16_t nsei;
96
97 uint32_t bvc_state;
98
99 /* we might want to add this as a shortcut later, avoiding the NSVC
100 * lookup for every packet, similar to a routing cache */
101 //struct gprs_nsvc *nsvc;
102};
103LLIST_HEAD(bts_ctxts);
104
105/* Find a BTS Context based on parsed RA ID and Cell ID */
106struct bssgp_bts_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
107{
108 struct bssgp_bts_ctx *bctx;
109
110 llist_for_each_entry(bctx, &bts_ctxts, list) {
111 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
112 bctx->cell_id == cid)
113 return bctx;
114 }
115 return NULL;
116}
117
118/* Find a BTS context based on BVCI+NSEI tuple */
119struct bssgp_bts_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
120{
121 struct bssgp_bts_ctx *bctx;
122
123 llist_for_each_entry(bctx, &bts_ctxts, list) {
124 if (bctx->nsei == nsei && bctx->bvci == bvci)
125 return bctx;
126 }
127 return NULL;
128}
129
130struct bssgp_btx_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
131{
132 struct bssgp_bts_ctx *ctx;
133
134 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bts_ctx);
135 if (!ctx)
136 return NULL;
137 ctx->bvci = bvci;
138 ctx->nsei = nsei;
139 llist_add(&ctx->list, &bts_ctxts);
140
141 return ctx;
142}
143
Harald Welte9ba50052010-03-14 15:45:01 +0800144static inline struct msgb *bssgp_msgb_alloc(void)
145{
146 return msgb_alloc_headroom(4096, 128, "BSSGP");
147}
148
149/* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200150static int bssgp_tx_simple_bvci(u_int8_t pdu_type, u_int16_t nsei,
151 u_int16_t bvci, u_int16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800152{
153 struct msgb *msg = bssgp_msgb_alloc();
154 struct bssgp_normal_hdr *bgph =
155 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
156 u_int16_t _bvci;
157
Harald Welte24a655f2010-04-30 19:54:29 +0200158 msgb_nsei(msg) = nsei;
159 msgb_bvci(msg) = ns_bvci;
160
Harald Welte9ba50052010-03-14 15:45:01 +0800161 bgph->pdu_type = pdu_type;
162 _bvci = htons(bvci);
163 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (u_int8_t *) &_bvci);
164
Harald Welte24a655f2010-04-30 19:54:29 +0200165 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800166}
167
168/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200169static int bssgp_tx_fc_bvc_ack(u_int16_t nsei, u_int8_t tag, u_int16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800170{
171 struct msgb *msg = bssgp_msgb_alloc();
172 struct bssgp_normal_hdr *bgph =
173 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
174
Harald Welte24a655f2010-04-30 19:54:29 +0200175 msgb_nsei(msg) = nsei;
176 msgb_bvci(msg) = ns_bvci;
177
Harald Welte9ba50052010-03-14 15:45:01 +0800178 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
179 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
180
Harald Welte24a655f2010-04-30 19:54:29 +0200181 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800182}
183
184/* Chapter 10.4.14: Status */
Harald Welte3771d092010-04-30 20:26:32 +0200185int bssgp_tx_status(u_int8_t cause, u_int16_t *bvci, struct msgb *orig_msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800186{
187 struct msgb *msg = bssgp_msgb_alloc();
188 struct bssgp_normal_hdr *bgph =
189 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
190
191 DEBUGPC(DGPRS, "BSSGP: TX STATUS, cause=%s\n", bssgp_cause_str(cause));
Harald Welte24a655f2010-04-30 19:54:29 +0200192 msgb_nsei(msg) = msgb_nsei(orig_msg);
193 msgb_bvci(msg) = 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800194
195 bgph->pdu_type = BSSGP_PDUT_STATUS;
196 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
197 if (bvci) {
198 u_int16_t _bvci = htons(*bvci);
199 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (u_int8_t *) &_bvci);
200 }
201 if (orig_msg)
202 msgb_tvlv_put(msg, BSSGP_IE_PDU_IN_ERROR,
Harald Welteec19c102010-05-02 09:50:42 +0200203 msgb_l3len(orig_msg), msgb_bssgph(orig_msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800204
Harald Welte24a655f2010-04-30 19:54:29 +0200205 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800206}
207
Harald Welte6752fa42010-05-02 09:23:16 +0200208static void bssgp_parse_cell_id(struct gprs_ra_id *raid, uint16_t *cid,
209 const uint8_t *buf)
210{
211 /* 6 octets RAC */
212 gsm48_parse_ra(raid, buf);
213 /* 2 octets CID */
214 *cid = ntohs(*(uint16_t *) (buf+6));
215}
216
Harald Welte3fddf3c2010-05-01 16:48:27 +0200217/* Chapter 8.4 BVC-Reset Procedure */
218static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
219 uint16_t ns_bvci)
220{
Harald Welte6752fa42010-05-02 09:23:16 +0200221 struct bssgp_bts_ctx *bctx;
222 uint16_t nsei = msgb_nsei(msg);
223 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200224 int rc;
225
226 bvci = ntohs(*(u_int16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
227 DEBUGPC(DGPRS, "BVCI=%u, cause=%s\n", bvci,
228 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
229
Harald Welte6752fa42010-05-02 09:23:16 +0200230 /* look-up or create the BTS context for this BVC */
231 bctx = btsctx_by_bvci_nsei(bvci, nsei);
232 if (!bctx)
233 bctx = btsctx_alloc(bvci, nsei);
234
Harald Welte3fddf3c2010-05-01 16:48:27 +0200235 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
236 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200237 if (bvci != 0 && bvci != 1) {
238 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
239 LOGP(DGPRS, LOGL_ERROR, "BSSGP RESET BVCI=%u "
240 "missing mandatory IE\n", bvci);
241 return -EINVAL;
242 }
243 /* actually extract RAC / CID */
244 bssgp_parse_cell_id(&bctx->ra_id, &bctx->cell_id,
245 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
246 LOGP(DGPRS, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
247 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
248 bctx->ra_id.rac, bctx->cell_id, bvci);
249 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200250
Harald Welte6752fa42010-05-02 09:23:16 +0200251 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200252 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200253 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200254 return 0;
255}
256
Harald Welte9ba50052010-03-14 15:45:01 +0800257/* Uplink unit-data */
Harald Welte30bc19a2010-05-02 11:19:37 +0200258static int bssgp_rx_ul_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800259{
Harald Welteec19c102010-05-02 09:50:42 +0200260 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800261 int data_len = msgb_l3len(msg) - sizeof(*budh);
262 struct tlv_parsed tp;
263 int rc;
264
265 DEBUGP(DGPRS, "BSSGP UL-UD\n");
266
Harald Welte6752fa42010-05-02 09:23:16 +0200267 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200268 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800269 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
270
271 /* Cell ID and LLC_PDU are the only mandatory IE */
272 if (!TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID) ||
273 !TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU))
274 return -EIO;
275
Harald Welte30bc19a2010-05-02 11:19:37 +0200276 /* FIXME: lookup bssgp_bts_ctx based on BVCI + NSEI */
277
Harald Welte510c3922010-04-30 16:33:12 +0200278 msgb_llch(msg) = TLVP_VAL(&tp, BSSGP_IE_LLC_PDU);
Harald Welte9ba50052010-03-14 15:45:01 +0800279
280 return gprs_llc_rcvmsg(msg, &tp);
281}
282
Harald Welte30bc19a2010-05-02 11:19:37 +0200283static int bssgp_rx_suspend(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800284{
Harald Welteec19c102010-05-02 09:50:42 +0200285 struct bssgp_normal_hdr *bgph =
286 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800287 int data_len = msgb_l3len(msg) - sizeof(*bgph);
288 struct tlv_parsed tp;
289 int rc;
290
291 DEBUGP(DGPRS, "BSSGP SUSPEND\n");
292
293 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
294 if (rc < 0)
295 return rc;
296
297 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
298 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
299 return -EIO;
300
Harald Welte30bc19a2010-05-02 11:19:37 +0200301 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800302 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800303}
304
Harald Welte30bc19a2010-05-02 11:19:37 +0200305static int bssgp_rx_resume(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800306{
Harald Welteec19c102010-05-02 09:50:42 +0200307 struct bssgp_normal_hdr *bgph =
308 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800309 int data_len = msgb_l3len(msg) - sizeof(*bgph);
310 struct tlv_parsed tp;
311 int rc;
312
313 DEBUGP(DGPRS, "BSSGP RESUME\n");
314
315 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
316 if (rc < 0)
317 return rc;
318
319 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
320 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA) ||
321 !TLVP_PRESENT(&tp, BSSGP_IE_SUSPEND_REF_NR))
322 return -EIO;
323
Harald Welte30bc19a2010-05-02 11:19:37 +0200324 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800325 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800326}
327
Harald Welte30bc19a2010-05-02 11:19:37 +0200328static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800329{
330
331 DEBUGP(DGPRS, "BSSGP FC BVC\n");
332
333 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
334 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
335 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
336 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
337 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
338 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
339
Harald Welte30bc19a2010-05-02 11:19:37 +0200340 /* FIXME: actually implement flow control */
341
Harald Welte9ba50052010-03-14 15:45:01 +0800342 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200343 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200344 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800345}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200346
Harald Welteec19c102010-05-02 09:50:42 +0200347/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Welte30bc19a2010-05-02 11:19:37 +0200348int gprs_bssgp_rcvmsg(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800349{
Harald Welteec19c102010-05-02 09:50:42 +0200350 struct bssgp_normal_hdr *bgph =
351 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800352 struct tlv_parsed tp;
Harald Welte30bc19a2010-05-02 11:19:37 +0200353 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800354 int data_len = msgb_l3len(msg) - sizeof(*bgph);
Harald Welte30bc19a2010-05-02 11:19:37 +0200355 uint16_t bvci; /* PTP BVCI */
356 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800357 int rc = 0;
358
Harald Welte30bc19a2010-05-02 11:19:37 +0200359 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
360
Harald Welte6752fa42010-05-02 09:23:16 +0200361 /* UNITDATA BSSGP headers have TLLI in front */
Harald Welte9ba50052010-03-14 15:45:01 +0800362 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
363 pdu_type != BSSGP_PDUT_DL_UNITDATA)
364 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
365
366 switch (pdu_type) {
367 case BSSGP_PDUT_UL_UNITDATA:
368 /* some LLC data from the MS */
Harald Welte30bc19a2010-05-02 11:19:37 +0200369 rc = bssgp_rx_ul_ud(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800370 break;
371 case BSSGP_PDUT_RA_CAPABILITY:
372 /* BSS requests RA capability or IMSI */
373 DEBUGP(DGPRS, "BSSGP RA CAPABILITY UPDATE\n");
374 /* FIXME: send RA_CAPA_UPDATE_ACK */
375 break;
376 case BSSGP_PDUT_RADIO_STATUS:
377 DEBUGP(DGPRS, "BSSGP RADIO STATUS\n");
378 /* BSS informs us of some exception */
Harald Welte30bc19a2010-05-02 11:19:37 +0200379 /* FIXME: notify GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800380 break;
381 case BSSGP_PDUT_SUSPEND:
382 /* MS wants to suspend */
Harald Welte30bc19a2010-05-02 11:19:37 +0200383 rc = bssgp_rx_suspend(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800384 break;
385 case BSSGP_PDUT_RESUME:
386 /* MS wants to resume */
Harald Welte30bc19a2010-05-02 11:19:37 +0200387 rc = bssgp_rx_resume(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800388 break;
389 case BSSGP_PDUT_FLUSH_LL:
390 /* BSS informs MS has moved to one cell to other cell */
391 DEBUGP(DGPRS, "BSSGP FLUSH LL\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200392 /* FIXME: notify GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800393 /* Send FLUSH_LL_ACK */
394 break;
395 case BSSGP_PDUT_LLC_DISCARD:
396 /* BSS informs that some LLC PDU's have been discarded */
397 DEBUGP(DGPRS, "BSSGP LLC DISCARDED\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200398 /* FIXME: notify GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800399 break;
400 case BSSGP_PDUT_FLOW_CONTROL_BVC:
401 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte30bc19a2010-05-02 11:19:37 +0200402 rc = bssgp_rx_fc_bvc(msg, &tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800403 break;
404 case BSSGP_PDUT_FLOW_CONTROL_MS:
405 /* BSS informs us of available bandwidth to one MS */
406 DEBUGP(DGPRS, "BSSGP FC MS\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200407 /* FIXME: actually implement flow control */
408 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800409 break;
410 case BSSGP_PDUT_BVC_BLOCK:
411 /* BSS tells us that BVC shall be blocked */
412 DEBUGP(DGPRS, "BSSGP BVC BLOCK ");
413 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
414 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
415 goto err_mand_ie;
416 bvci = ntohs(*(u_int16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
417 DEBUGPC(DGPRS, "BVCI=%u, cause=%s\n", bvci,
418 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
Harald Welte6752fa42010-05-02 09:23:16 +0200419 /* We always acknowledge the BLOCKing */
Harald Welte9ba50052010-03-14 15:45:01 +0800420 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK,
Harald Welte24a655f2010-04-30 19:54:29 +0200421 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800422 break;
423 case BSSGP_PDUT_BVC_UNBLOCK:
424 /* BSS tells us that BVC shall be unblocked */
425 DEBUGP(DGPRS, "BSSGP BVC UNBLOCK ");
426 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
427 goto err_mand_ie;
428 bvci = ntohs(*(u_int16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
429 DEBUGPC(DGPRS, "BVCI=%u\n", bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200430 /* We always acknowledge the unBLOCKing */
Harald Welte9ba50052010-03-14 15:45:01 +0800431 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK,
Harald Welte24a655f2010-04-30 19:54:29 +0200432 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800433 break;
434 case BSSGP_PDUT_BVC_RESET:
435 /* BSS tells us that BVC init is required */
436 DEBUGP(DGPRS, "BSSGP BVC RESET ");
437 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
438 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
439 goto err_mand_ie;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200440 rc = bssgp_rx_bvc_reset(msg, &tp, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800441 break;
442 case BSSGP_PDUT_STATUS:
443 /* Some exception has occurred */
Harald Welte30bc19a2010-05-02 11:19:37 +0200444 /* FIXME: notify GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800445 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
446 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
447 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
448 case BSSGP_PDUT_MODIFY_BSS_PFC:
449 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
450 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x not [yet] implemented\n",
451 pdu_type);
452 break;
453 /* those only exist in the SGSN -> BSS direction */
454 case BSSGP_PDUT_DL_UNITDATA:
455 case BSSGP_PDUT_PAGING_PS:
456 case BSSGP_PDUT_PAGING_CS:
457 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
458 case BSSGP_PDUT_SUSPEND_ACK:
459 case BSSGP_PDUT_SUSPEND_NACK:
460 case BSSGP_PDUT_RESUME_ACK:
461 case BSSGP_PDUT_RESUME_NACK:
462 case BSSGP_PDUT_FLUSH_LL_ACK:
463 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
464 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
465 case BSSGP_PDUT_BVC_BLOCK_ACK:
466 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
467 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
468 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x only exists in DL\n",
469 pdu_type);
470 rc = -EINVAL;
471 break;
472 default:
473 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
474 break;
475 }
476
477 return rc;
478err_mand_ie:
479 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
480}
481
Harald Welte6752fa42010-05-02 09:23:16 +0200482/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200483 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
484int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800485{
Harald Welte6752fa42010-05-02 09:23:16 +0200486 struct bssgp_bts_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800487 struct bssgp_ud_hdr *budh;
488 u_int8_t llc_pdu_tlv_hdr_len = 2;
489 u_int8_t *llc_pdu_tlv, *qos_profile;
490 u_int16_t pdu_lifetime = 1000; /* centi-seconds */
491 u_int8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
492 u_int16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200493 uint16_t bvci = msgb_bvci(msg);
494 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800495
Harald Welte30bc19a2010-05-02 11:19:37 +0200496 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
497 if (bvci < 2) {
498 LOGP(DGPRS, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
499 bvci);
500 return -EINVAL;
501 }
502
503 bctx = btsctx_by_bvci_nsei(bvci, nsei);
504 if (!bctx)
505 bctx = btsctx_alloc(bvci, nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800506
507 if (msg->len > TVLV_MAX_ONEBYTE)
508 llc_pdu_tlv_hdr_len += 1;
509
510 /* prepend the tag and length of the LLC-PDU TLV */
511 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
512 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
513 if (llc_pdu_tlv_hdr_len > 2) {
514 llc_pdu_tlv[1] = msg_len >> 8;
515 llc_pdu_tlv[2] = msg_len & 0xff;
516 } else {
517 llc_pdu_tlv[1] = msg_len & 0x3f;
518 llc_pdu_tlv[1] |= 0x80;
519 }
520
521 /* FIXME: optional elements */
522
523 /* prepend the pdu lifetime */
524 pdu_lifetime = htons(pdu_lifetime);
525 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (u_int8_t *)&pdu_lifetime);
526
527 /* prepend the QoS profile, TLLI and pdu type */
528 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
529 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200530 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800531 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
532
Harald Welte30bc19a2010-05-02 11:19:37 +0200533 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200534
535 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800536}