blob: 44e0a35c009b6b9e59a0eb93e88f10487181e157 [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 Welte9ba50052010-03-14 15:45:01 +080046/* Chapter 11.3.9 / Table 11.10: Cause coding */
47static const char *bssgp_cause_strings[] = {
48 [BSSGP_CAUSE_PROC_OVERLOAD] = "Processor overload",
49 [BSSGP_CAUSE_EQUIP_FAIL] = "Equipment Failure",
50 [BSSGP_CAUSE_TRASIT_NET_FAIL] = "Transit netowkr service failure",
51 [BSSGP_CAUSE_CAPA_GREATER_0KPBS]= "Transmission capacity modified",
52 [BSSGP_CAUSE_UNKNOWN_MS] = "Unknown MS",
53 [BSSGP_CAUSE_UNKNOWN_BVCI] = "Unknown BVCI",
54 [BSSGP_CAUSE_CELL_TRAF_CONG] = "Cell traffic congestion",
55 [BSSGP_CAUSE_SGSN_CONG] = "SGSN congestion",
56 [BSSGP_CAUSE_OML_INTERV] = "O&M intervention",
57 [BSSGP_CAUSE_BVCI_BLOCKED] = "BVCI blocked",
58 [BSSGP_CAUSE_PFC_CREATE_FAIL] = "PFC create failure",
59 [BSSGP_CAUSE_SEM_INCORR_PDU] = "Semantically incorrect PDU",
60 [BSSGP_CAUSE_INV_MAND_INF] = "Invalid mandatory information",
61 [BSSGP_CAUSE_MISSING_MAND_IE] = "Missing mandatory IE",
62 [BSSGP_CAUSE_MISSING_COND_IE] = "Missing conditional IE",
63 [BSSGP_CAUSE_UNEXP_COND_IE] = "Unexpected conditional IE",
64 [BSSGP_CAUSE_COND_IE_ERR] = "Conditional IE error",
65 [BSSGP_CAUSE_PDU_INCOMP_STATE] = "PDU incompatible with protocol state",
66 [BSSGP_CAUSE_PROTO_ERR_UNSPEC] = "Protocol error - unspecified",
67 [BSSGP_CAUSE_PDU_INCOMP_FEAT] = "PDU not compatible with feature set",
68};
69
70static const char *bssgp_cause_str(enum gprs_bssgp_cause cause)
71{
72 if (cause >= ARRAY_SIZE(bssgp_cause_strings))
73 return "undefined";
74
75 if (bssgp_cause_strings[cause])
76 return bssgp_cause_strings[cause];
77
78 return "undefined";
79}
80
Harald Welte6752fa42010-05-02 09:23:16 +020081#define BVC_F_BLOCKED 0x0001
82
83/* The per-BTS context that we keep on the SGSN side of the BSSGP link */
84struct bssgp_bts_ctx {
85 struct llist_head list;
86
87 /* parsed RA ID and Cell ID of the remote BTS */
88 struct gprs_ra_id ra_id;
89 uint16_t cell_id;
90
91 /* NSEI and BVCI of underlying Gb link. Together they
92 * uniquely identify a link to a BTS (5.4.4) */
93 uint16_t bvci;
94 uint16_t nsei;
95
96 uint32_t bvc_state;
97
98 /* we might want to add this as a shortcut later, avoiding the NSVC
99 * lookup for every packet, similar to a routing cache */
100 //struct gprs_nsvc *nsvc;
101};
102LLIST_HEAD(bts_ctxts);
103
104/* Find a BTS Context based on parsed RA ID and Cell ID */
105struct bssgp_bts_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
106{
107 struct bssgp_bts_ctx *bctx;
108
109 llist_for_each_entry(bctx, &bts_ctxts, list) {
110 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
111 bctx->cell_id == cid)
112 return bctx;
113 }
114 return NULL;
115}
116
117/* Find a BTS context based on BVCI+NSEI tuple */
118struct bssgp_bts_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
119{
120 struct bssgp_bts_ctx *bctx;
121
122 llist_for_each_entry(bctx, &bts_ctxts, list) {
123 if (bctx->nsei == nsei && bctx->bvci == bvci)
124 return bctx;
125 }
126 return NULL;
127}
128
129struct bssgp_btx_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
130{
131 struct bssgp_bts_ctx *ctx;
132
133 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bts_ctx);
134 if (!ctx)
135 return NULL;
136 ctx->bvci = bvci;
137 ctx->nsei = nsei;
138 llist_add(&ctx->list, &bts_ctxts);
139
140 return ctx;
141}
142
Harald Welte9ba50052010-03-14 15:45:01 +0800143static inline struct msgb *bssgp_msgb_alloc(void)
144{
145 return msgb_alloc_headroom(4096, 128, "BSSGP");
146}
147
148/* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200149static int bssgp_tx_simple_bvci(u_int8_t pdu_type, u_int16_t nsei,
150 u_int16_t bvci, u_int16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800151{
152 struct msgb *msg = bssgp_msgb_alloc();
153 struct bssgp_normal_hdr *bgph =
154 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
155 u_int16_t _bvci;
156
Harald Welte24a655f2010-04-30 19:54:29 +0200157 msgb_nsei(msg) = nsei;
158 msgb_bvci(msg) = ns_bvci;
159
Harald Welte9ba50052010-03-14 15:45:01 +0800160 bgph->pdu_type = pdu_type;
161 _bvci = htons(bvci);
162 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (u_int8_t *) &_bvci);
163
Harald Welte24a655f2010-04-30 19:54:29 +0200164 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800165}
166
167/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200168static 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 +0800169{
170 struct msgb *msg = bssgp_msgb_alloc();
171 struct bssgp_normal_hdr *bgph =
172 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
173
Harald Welte24a655f2010-04-30 19:54:29 +0200174 msgb_nsei(msg) = nsei;
175 msgb_bvci(msg) = ns_bvci;
176
Harald Welte9ba50052010-03-14 15:45:01 +0800177 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
178 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
179
Harald Welte24a655f2010-04-30 19:54:29 +0200180 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800181}
182
183/* Chapter 10.4.14: Status */
Harald Welte3771d092010-04-30 20:26:32 +0200184int bssgp_tx_status(u_int8_t cause, u_int16_t *bvci, struct msgb *orig_msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800185{
186 struct msgb *msg = bssgp_msgb_alloc();
187 struct bssgp_normal_hdr *bgph =
188 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
189
190 DEBUGPC(DGPRS, "BSSGP: TX STATUS, cause=%s\n", bssgp_cause_str(cause));
Harald Welte24a655f2010-04-30 19:54:29 +0200191 msgb_nsei(msg) = msgb_nsei(orig_msg);
192 msgb_bvci(msg) = 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800193
194 bgph->pdu_type = BSSGP_PDUT_STATUS;
195 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
196 if (bvci) {
197 u_int16_t _bvci = htons(*bvci);
198 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (u_int8_t *) &_bvci);
199 }
200 if (orig_msg)
201 msgb_tvlv_put(msg, BSSGP_IE_PDU_IN_ERROR,
202 msgb_l3len(orig_msg), orig_msg->l3h);
203
Harald Welte24a655f2010-04-30 19:54:29 +0200204 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800205}
206
Harald Welte6752fa42010-05-02 09:23:16 +0200207static void bssgp_parse_cell_id(struct gprs_ra_id *raid, uint16_t *cid,
208 const uint8_t *buf)
209{
210 /* 6 octets RAC */
211 gsm48_parse_ra(raid, buf);
212 /* 2 octets CID */
213 *cid = ntohs(*(uint16_t *) (buf+6));
214}
215
Harald Welte3fddf3c2010-05-01 16:48:27 +0200216/* Chapter 8.4 BVC-Reset Procedure */
217static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
218 uint16_t ns_bvci)
219{
Harald Welte6752fa42010-05-02 09:23:16 +0200220 struct bssgp_bts_ctx *bctx;
221 uint16_t nsei = msgb_nsei(msg);
222 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200223 int rc;
224
225 bvci = ntohs(*(u_int16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
226 DEBUGPC(DGPRS, "BVCI=%u, cause=%s\n", bvci,
227 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
228
Harald Welte6752fa42010-05-02 09:23:16 +0200229 /* look-up or create the BTS context for this BVC */
230 bctx = btsctx_by_bvci_nsei(bvci, nsei);
231 if (!bctx)
232 bctx = btsctx_alloc(bvci, nsei);
233
Harald Welte3fddf3c2010-05-01 16:48:27 +0200234 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
235 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200236 if (bvci != 0 && bvci != 1) {
237 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
238 LOGP(DGPRS, LOGL_ERROR, "BSSGP RESET BVCI=%u "
239 "missing mandatory IE\n", bvci);
240 return -EINVAL;
241 }
242 /* actually extract RAC / CID */
243 bssgp_parse_cell_id(&bctx->ra_id, &bctx->cell_id,
244 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
245 LOGP(DGPRS, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
246 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
247 bctx->ra_id.rac, bctx->cell_id, bvci);
248 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200249
Harald Welte6752fa42010-05-02 09:23:16 +0200250 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200251 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200252 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200253 return 0;
254}
255
Harald Welte9ba50052010-03-14 15:45:01 +0800256/* Uplink unit-data */
257static int bssgp_rx_ul_ud(struct msgb *msg, u_int16_t bvci)
258{
259 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msg->l3h;
Harald Welte9ba50052010-03-14 15:45:01 +0800260 int data_len = msgb_l3len(msg) - sizeof(*budh);
261 struct tlv_parsed tp;
262 int rc;
263
264 DEBUGP(DGPRS, "BSSGP UL-UD\n");
265
Harald Welte6752fa42010-05-02 09:23:16 +0200266 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200267 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800268 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
269
270 /* Cell ID and LLC_PDU are the only mandatory IE */
271 if (!TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID) ||
272 !TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU))
273 return -EIO;
274
Harald Welte510c3922010-04-30 16:33:12 +0200275 msgb_llch(msg) = TLVP_VAL(&tp, BSSGP_IE_LLC_PDU);
Harald Welte9ba50052010-03-14 15:45:01 +0800276
277 return gprs_llc_rcvmsg(msg, &tp);
278}
279
280static int bssgp_rx_suspend(struct msgb *msg, u_int16_t bvci)
281{
282 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msg->l3h;
283 int data_len = msgb_l3len(msg) - sizeof(*bgph);
284 struct tlv_parsed tp;
285 int rc;
286
287 DEBUGP(DGPRS, "BSSGP SUSPEND\n");
288
289 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
290 if (rc < 0)
291 return rc;
292
293 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
294 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
295 return -EIO;
296
297 /* SEND SUSPEND_ACK or SUSPEND_NACK */
298 /* FIXME */
299}
300
301static int bssgp_rx_resume(struct msgb *msg, u_int16_t bvci)
302{
303 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msg->l3h;
304 int data_len = msgb_l3len(msg) - sizeof(*bgph);
305 struct tlv_parsed tp;
306 int rc;
307
308 DEBUGP(DGPRS, "BSSGP RESUME\n");
309
310 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
311 if (rc < 0)
312 return rc;
313
314 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
315 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA) ||
316 !TLVP_PRESENT(&tp, BSSGP_IE_SUSPEND_REF_NR))
317 return -EIO;
318
319 /* SEND RESUME_ACK or RESUME_NACK */
320 /* FIXME */
321}
322
323static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
324 u_int16_t ns_bvci)
325{
326
327 DEBUGP(DGPRS, "BSSGP FC BVC\n");
328
329 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
330 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
331 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
332 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
333 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
334 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
335
336 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200337 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
338 ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800339}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200340
Harald Welte9ba50052010-03-14 15:45:01 +0800341/* We expect msg->l3h to point to the BSSGP header */
342int gprs_bssgp_rcvmsg(struct msgb *msg, u_int16_t ns_bvci)
343{
344 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msg->l3h;
345 struct tlv_parsed tp;
346 u_int8_t pdu_type = bgph->pdu_type;
347 int data_len = msgb_l3len(msg) - sizeof(*bgph);
348 u_int16_t bvci;
349 int rc = 0;
350
Harald Welte6752fa42010-05-02 09:23:16 +0200351 /* UNITDATA BSSGP headers have TLLI in front */
Harald Welte9ba50052010-03-14 15:45:01 +0800352 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
353 pdu_type != BSSGP_PDUT_DL_UNITDATA)
354 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
355
356 switch (pdu_type) {
357 case BSSGP_PDUT_UL_UNITDATA:
358 /* some LLC data from the MS */
359 rc = bssgp_rx_ul_ud(msg, ns_bvci);
360 break;
361 case BSSGP_PDUT_RA_CAPABILITY:
362 /* BSS requests RA capability or IMSI */
363 DEBUGP(DGPRS, "BSSGP RA CAPABILITY UPDATE\n");
364 /* FIXME: send RA_CAPA_UPDATE_ACK */
365 break;
366 case BSSGP_PDUT_RADIO_STATUS:
367 DEBUGP(DGPRS, "BSSGP RADIO STATUS\n");
368 /* BSS informs us of some exception */
369 break;
370 case BSSGP_PDUT_SUSPEND:
371 /* MS wants to suspend */
372 rc = bssgp_rx_suspend(msg, ns_bvci);
373 break;
374 case BSSGP_PDUT_RESUME:
375 /* MS wants to resume */
376 rc = bssgp_rx_resume(msg, ns_bvci);
377 break;
378 case BSSGP_PDUT_FLUSH_LL:
379 /* BSS informs MS has moved to one cell to other cell */
380 DEBUGP(DGPRS, "BSSGP FLUSH LL\n");
381 /* Send FLUSH_LL_ACK */
382 break;
383 case BSSGP_PDUT_LLC_DISCARD:
384 /* BSS informs that some LLC PDU's have been discarded */
385 DEBUGP(DGPRS, "BSSGP LLC DISCARDED\n");
386 break;
387 case BSSGP_PDUT_FLOW_CONTROL_BVC:
388 /* BSS informs us of available bandwidth in Gb interface */
389 rc = bssgp_rx_fc_bvc(msg, &tp, ns_bvci);
390 break;
391 case BSSGP_PDUT_FLOW_CONTROL_MS:
392 /* BSS informs us of available bandwidth to one MS */
393 DEBUGP(DGPRS, "BSSGP FC MS\n");
394 /* Send FLOW_CONTROL_MS_ACK */
395 break;
396 case BSSGP_PDUT_BVC_BLOCK:
397 /* BSS tells us that BVC shall be blocked */
398 DEBUGP(DGPRS, "BSSGP BVC BLOCK ");
399 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
400 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
401 goto err_mand_ie;
402 bvci = ntohs(*(u_int16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
403 DEBUGPC(DGPRS, "BVCI=%u, cause=%s\n", bvci,
404 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
Harald Welte6752fa42010-05-02 09:23:16 +0200405 /* We always acknowledge the BLOCKing */
Harald Welte9ba50052010-03-14 15:45:01 +0800406 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK,
Harald Welte24a655f2010-04-30 19:54:29 +0200407 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800408 break;
409 case BSSGP_PDUT_BVC_UNBLOCK:
410 /* BSS tells us that BVC shall be unblocked */
411 DEBUGP(DGPRS, "BSSGP BVC UNBLOCK ");
412 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
413 goto err_mand_ie;
414 bvci = ntohs(*(u_int16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
415 DEBUGPC(DGPRS, "BVCI=%u\n", bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200416 /* We always acknowledge the unBLOCKing */
Harald Welte9ba50052010-03-14 15:45:01 +0800417 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK,
Harald Welte24a655f2010-04-30 19:54:29 +0200418 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800419 break;
420 case BSSGP_PDUT_BVC_RESET:
421 /* BSS tells us that BVC init is required */
422 DEBUGP(DGPRS, "BSSGP BVC RESET ");
423 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
424 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
425 goto err_mand_ie;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200426 rc = bssgp_rx_bvc_reset(msg, &tp, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800427 break;
428 case BSSGP_PDUT_STATUS:
429 /* Some exception has occurred */
430 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
431 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
432 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
433 case BSSGP_PDUT_MODIFY_BSS_PFC:
434 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
435 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x not [yet] implemented\n",
436 pdu_type);
437 break;
438 /* those only exist in the SGSN -> BSS direction */
439 case BSSGP_PDUT_DL_UNITDATA:
440 case BSSGP_PDUT_PAGING_PS:
441 case BSSGP_PDUT_PAGING_CS:
442 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
443 case BSSGP_PDUT_SUSPEND_ACK:
444 case BSSGP_PDUT_SUSPEND_NACK:
445 case BSSGP_PDUT_RESUME_ACK:
446 case BSSGP_PDUT_RESUME_NACK:
447 case BSSGP_PDUT_FLUSH_LL_ACK:
448 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
449 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
450 case BSSGP_PDUT_BVC_BLOCK_ACK:
451 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
452 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
453 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x only exists in DL\n",
454 pdu_type);
455 rc = -EINVAL;
456 break;
457 default:
458 DEBUGP(DGPRS, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
459 break;
460 }
461
462 return rc;
463err_mand_ie:
464 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
465}
466
Harald Welte6752fa42010-05-02 09:23:16 +0200467/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
468 * to a remote MS (identified by TLLI) at a BTS identified by its RAC and CID */
469int gprs_bssgp_tx_dl_ud(struct msgb *msg, const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte9ba50052010-03-14 15:45:01 +0800470{
Harald Welte6752fa42010-05-02 09:23:16 +0200471 struct bssgp_bts_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800472 struct bssgp_ud_hdr *budh;
473 u_int8_t llc_pdu_tlv_hdr_len = 2;
474 u_int8_t *llc_pdu_tlv, *qos_profile;
475 u_int16_t pdu_lifetime = 1000; /* centi-seconds */
476 u_int8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
477 u_int16_t msg_len = msg->len;
478
Harald Welte6752fa42010-05-02 09:23:16 +0200479 bctx = btsctx_by_raid_cid(raid, cid);
Harald Welte9ba50052010-03-14 15:45:01 +0800480
481 if (msg->len > TVLV_MAX_ONEBYTE)
482 llc_pdu_tlv_hdr_len += 1;
483
484 /* prepend the tag and length of the LLC-PDU TLV */
485 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
486 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
487 if (llc_pdu_tlv_hdr_len > 2) {
488 llc_pdu_tlv[1] = msg_len >> 8;
489 llc_pdu_tlv[2] = msg_len & 0xff;
490 } else {
491 llc_pdu_tlv[1] = msg_len & 0x3f;
492 llc_pdu_tlv[1] |= 0x80;
493 }
494
495 /* FIXME: optional elements */
496
497 /* prepend the pdu lifetime */
498 pdu_lifetime = htons(pdu_lifetime);
499 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (u_int8_t *)&pdu_lifetime);
500
501 /* prepend the QoS profile, TLLI and pdu type */
502 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
503 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200504 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800505 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
506
Harald Welte6752fa42010-05-02 09:23:16 +0200507 msgb_nsei(msg) = bctx->nsei;
508 msgb_bvci(msg) = bctx->bvci;
Harald Welte24a655f2010-04-30 19:54:29 +0200509
510 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800511}