blob: b2f292858aeebb1b81b21fc8dda075cf0b9d711d [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */
2
Harald Welte6557cd02010-05-02 09:23:16 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9b455bf2010-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 Welteeaa614c2010-05-02 11:26:34 +020024#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080025
26#include <netinet/in.h>
27
28#include <osmocore/msgb.h>
29#include <osmocore/tlv.h>
Harald Welte6557cd02010-05-02 09:23:16 +020030#include <osmocore/talloc.h>
31
Harald Welte9b455bf2010-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;
42
Harald Welte6557cd02010-05-02 09:23:16 +020043void *bssgp_tall_ctx = NULL;
44
Harald Weltedbab1c72010-05-02 09:34:20 +020045
46/* Our actual implementation */
47
Harald Welte6557cd02010-05-02 09:23:16 +020048#define BVC_F_BLOCKED 0x0001
49
50/* The per-BTS context that we keep on the SGSN side of the BSSGP link */
51struct bssgp_bts_ctx {
52 struct llist_head list;
53
54 /* parsed RA ID and Cell ID of the remote BTS */
55 struct gprs_ra_id ra_id;
56 uint16_t cell_id;
57
58 /* NSEI and BVCI of underlying Gb link. Together they
59 * uniquely identify a link to a BTS (5.4.4) */
60 uint16_t bvci;
61 uint16_t nsei;
62
63 uint32_t bvc_state;
64
65 /* we might want to add this as a shortcut later, avoiding the NSVC
66 * lookup for every packet, similar to a routing cache */
67 //struct gprs_nsvc *nsvc;
68};
69LLIST_HEAD(bts_ctxts);
70
71/* Find a BTS Context based on parsed RA ID and Cell ID */
72struct bssgp_bts_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
73{
74 struct bssgp_bts_ctx *bctx;
75
76 llist_for_each_entry(bctx, &bts_ctxts, list) {
77 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
78 bctx->cell_id == cid)
79 return bctx;
80 }
81 return NULL;
82}
83
84/* Find a BTS context based on BVCI+NSEI tuple */
85struct bssgp_bts_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
86{
87 struct bssgp_bts_ctx *bctx;
88
89 llist_for_each_entry(bctx, &bts_ctxts, list) {
90 if (bctx->nsei == nsei && bctx->bvci == bvci)
91 return bctx;
92 }
93 return NULL;
94}
95
96struct bssgp_btx_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
97{
98 struct bssgp_bts_ctx *ctx;
99
100 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bts_ctx);
101 if (!ctx)
102 return NULL;
103 ctx->bvci = bvci;
104 ctx->nsei = nsei;
105 llist_add(&ctx->list, &bts_ctxts);
106
107 return ctx;
108}
109
Harald Welte9b455bf2010-03-14 15:45:01 +0800110/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welteeaa614c2010-05-02 11:26:34 +0200111static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9b455bf2010-03-14 15:45:01 +0800112{
113 struct msgb *msg = bssgp_msgb_alloc();
114 struct bssgp_normal_hdr *bgph =
115 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
116
Harald Welte44f1c272010-04-30 19:54:29 +0200117 msgb_nsei(msg) = nsei;
118 msgb_bvci(msg) = ns_bvci;
119
Harald Welte9b455bf2010-03-14 15:45:01 +0800120 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
121 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
122
Harald Welte44f1c272010-04-30 19:54:29 +0200123 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800124}
125
Harald Welte11d7c102010-05-02 11:54:55 +0200126uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6557cd02010-05-02 09:23:16 +0200127{
128 /* 6 octets RAC */
129 gsm48_parse_ra(raid, buf);
130 /* 2 octets CID */
Harald Welte11d7c102010-05-02 11:54:55 +0200131 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6557cd02010-05-02 09:23:16 +0200132}
133
Harald Welte288be162010-05-01 16:48:27 +0200134/* Chapter 8.4 BVC-Reset Procedure */
135static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
136 uint16_t ns_bvci)
137{
Harald Welte6557cd02010-05-02 09:23:16 +0200138 struct bssgp_bts_ctx *bctx;
139 uint16_t nsei = msgb_nsei(msg);
140 uint16_t bvci;
Harald Welte288be162010-05-01 16:48:27 +0200141 int rc;
142
Harald Welteeaa614c2010-05-02 11:26:34 +0200143 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte6b72cdf2010-05-11 05:54:22 +0200144 DEBUGPC(DBSSGP, "BVCI=%u, cause=%s\n", bvci,
Harald Welte288be162010-05-01 16:48:27 +0200145 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
146
Harald Welte6557cd02010-05-02 09:23:16 +0200147 /* look-up or create the BTS context for this BVC */
148 bctx = btsctx_by_bvci_nsei(bvci, nsei);
149 if (!bctx)
150 bctx = btsctx_alloc(bvci, nsei);
151
Harald Welte288be162010-05-01 16:48:27 +0200152 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
153 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6557cd02010-05-02 09:23:16 +0200154 if (bvci != 0 && bvci != 1) {
155 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200156 LOGP(DBSSGP, LOGL_ERROR, "BSSGP RESET BVCI=%u "
Harald Welte6557cd02010-05-02 09:23:16 +0200157 "missing mandatory IE\n", bvci);
158 return -EINVAL;
159 }
160 /* actually extract RAC / CID */
Harald Welte11d7c102010-05-02 11:54:55 +0200161 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
162 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welte6b72cdf2010-05-11 05:54:22 +0200163 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6557cd02010-05-02 09:23:16 +0200164 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
165 bctx->ra_id.rac, bctx->cell_id, bvci);
166 }
Harald Welte288be162010-05-01 16:48:27 +0200167
Harald Welte6557cd02010-05-02 09:23:16 +0200168 /* Acknowledge the RESET to the BTS */
Harald Welte288be162010-05-01 16:48:27 +0200169 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6557cd02010-05-02 09:23:16 +0200170 nsei, bvci, ns_bvci);
Harald Welte288be162010-05-01 16:48:27 +0200171 return 0;
172}
173
Harald Welte9b455bf2010-03-14 15:45:01 +0800174/* Uplink unit-data */
Harald Weltee6afd602010-05-02 11:19:37 +0200175static int bssgp_rx_ul_ud(struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800176{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200177 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte721961c2010-05-02 21:29:36 +0200178 int data_len = msgb_bssgp_len(msg) - sizeof(*budh);
Harald Welte9b455bf2010-03-14 15:45:01 +0800179 struct tlv_parsed tp;
180 int rc;
181
Harald Welte6b72cdf2010-05-11 05:54:22 +0200182 DEBUGP(DBSSGP, "BSSGP UL-UD\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800183
Harald Welte6557cd02010-05-02 09:23:16 +0200184 /* extract TLLI and parse TLV IEs */
Harald Welte943c5bc2010-04-30 16:33:12 +0200185 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9b455bf2010-03-14 15:45:01 +0800186 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
187
188 /* Cell ID and LLC_PDU are the only mandatory IE */
189 if (!TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID) ||
190 !TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU))
191 return -EIO;
192
Harald Weltee6afd602010-05-02 11:19:37 +0200193 /* FIXME: lookup bssgp_bts_ctx based on BVCI + NSEI */
194
Harald Welte11d7c102010-05-02 11:54:55 +0200195 /* store pointer to LLC header and CELL ID in msgb->cb */
Harald Welte943c5bc2010-04-30 16:33:12 +0200196 msgb_llch(msg) = TLVP_VAL(&tp, BSSGP_IE_LLC_PDU);
Harald Welte11d7c102010-05-02 11:54:55 +0200197 msgb_bcid(msg) = TLVP_VAL(&tp, BSSGP_IE_CELL_ID);
Harald Welte9b455bf2010-03-14 15:45:01 +0800198
199 return gprs_llc_rcvmsg(msg, &tp);
200}
201
Harald Weltee6afd602010-05-02 11:19:37 +0200202static int bssgp_rx_suspend(struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800203{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200204 struct bssgp_normal_hdr *bgph =
205 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte721961c2010-05-02 21:29:36 +0200206 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800207 struct tlv_parsed tp;
208 int rc;
209
Harald Welte6b72cdf2010-05-11 05:54:22 +0200210 DEBUGP(DBSSGP, "BSSGP SUSPEND\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800211
212 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
213 if (rc < 0)
214 return rc;
215
216 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
217 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
218 return -EIO;
219
Harald Weltee6afd602010-05-02 11:19:37 +0200220 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800221 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9b455bf2010-03-14 15:45:01 +0800222}
223
Harald Weltee6afd602010-05-02 11:19:37 +0200224static int bssgp_rx_resume(struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800225{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200226 struct bssgp_normal_hdr *bgph =
227 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte721961c2010-05-02 21:29:36 +0200228 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800229 struct tlv_parsed tp;
230 int rc;
231
Harald Welte6b72cdf2010-05-11 05:54:22 +0200232 DEBUGP(DBSSGP, "BSSGP RESUME\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800233
234 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
235 if (rc < 0)
236 return rc;
237
238 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
239 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA) ||
240 !TLVP_PRESENT(&tp, BSSGP_IE_SUSPEND_REF_NR))
241 return -EIO;
242
Harald Weltee6afd602010-05-02 11:19:37 +0200243 /* FIXME: pass the RESUME request to GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800244 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9b455bf2010-03-14 15:45:01 +0800245}
246
Harald Weltee6afd602010-05-02 11:19:37 +0200247static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9b455bf2010-03-14 15:45:01 +0800248{
249
Harald Welte6b72cdf2010-05-11 05:54:22 +0200250 DEBUGP(DBSSGP, "BSSGP FC BVC\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800251
252 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
253 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
254 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
255 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
256 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
257 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
258
Harald Weltee6afd602010-05-02 11:19:37 +0200259 /* FIXME: actually implement flow control */
260
Harald Welte9b455bf2010-03-14 15:45:01 +0800261 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte44f1c272010-04-30 19:54:29 +0200262 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Weltee6afd602010-05-02 11:19:37 +0200263 msgb_bvci(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800264}
Harald Welte288be162010-05-01 16:48:27 +0200265
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200266/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Weltee6afd602010-05-02 11:19:37 +0200267int gprs_bssgp_rcvmsg(struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800268{
Harald Weltefd3fa1d2010-05-02 09:50:42 +0200269 struct bssgp_normal_hdr *bgph =
270 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800271 struct tlv_parsed tp;
Harald Weltee6afd602010-05-02 11:19:37 +0200272 uint8_t pdu_type = bgph->pdu_type;
Harald Welte721961c2010-05-02 21:29:36 +0200273 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Weltee6afd602010-05-02 11:19:37 +0200274 uint16_t bvci; /* PTP BVCI */
275 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800276 int rc = 0;
277
Harald Weltee6afd602010-05-02 11:19:37 +0200278 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
279
Harald Welte6557cd02010-05-02 09:23:16 +0200280 /* UNITDATA BSSGP headers have TLLI in front */
Harald Welte9b455bf2010-03-14 15:45:01 +0800281 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
282 pdu_type != BSSGP_PDUT_DL_UNITDATA)
283 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
284
285 switch (pdu_type) {
286 case BSSGP_PDUT_UL_UNITDATA:
287 /* some LLC data from the MS */
Harald Weltee6afd602010-05-02 11:19:37 +0200288 rc = bssgp_rx_ul_ud(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800289 break;
290 case BSSGP_PDUT_RA_CAPABILITY:
291 /* BSS requests RA capability or IMSI */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200292 DEBUGP(DBSSGP, "BSSGP RA CAPABILITY UPDATE\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800293 /* FIXME: send RA_CAPA_UPDATE_ACK */
294 break;
295 case BSSGP_PDUT_RADIO_STATUS:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200296 DEBUGP(DBSSGP, "BSSGP RADIO STATUS\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800297 /* BSS informs us of some exception */
Harald Weltee6afd602010-05-02 11:19:37 +0200298 /* FIXME: notify GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800299 break;
300 case BSSGP_PDUT_SUSPEND:
301 /* MS wants to suspend */
Harald Weltee6afd602010-05-02 11:19:37 +0200302 rc = bssgp_rx_suspend(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800303 break;
304 case BSSGP_PDUT_RESUME:
305 /* MS wants to resume */
Harald Weltee6afd602010-05-02 11:19:37 +0200306 rc = bssgp_rx_resume(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800307 break;
308 case BSSGP_PDUT_FLUSH_LL:
309 /* BSS informs MS has moved to one cell to other cell */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200310 DEBUGP(DBSSGP, "BSSGP FLUSH LL\n");
Harald Weltee6afd602010-05-02 11:19:37 +0200311 /* FIXME: notify GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800312 /* Send FLUSH_LL_ACK */
313 break;
314 case BSSGP_PDUT_LLC_DISCARD:
315 /* BSS informs that some LLC PDU's have been discarded */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200316 DEBUGP(DBSSGP, "BSSGP LLC DISCARDED\n");
Harald Weltee6afd602010-05-02 11:19:37 +0200317 /* FIXME: notify GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800318 break;
319 case BSSGP_PDUT_FLOW_CONTROL_BVC:
320 /* BSS informs us of available bandwidth in Gb interface */
Harald Weltee6afd602010-05-02 11:19:37 +0200321 rc = bssgp_rx_fc_bvc(msg, &tp);
Harald Welte9b455bf2010-03-14 15:45:01 +0800322 break;
323 case BSSGP_PDUT_FLOW_CONTROL_MS:
324 /* BSS informs us of available bandwidth to one MS */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200325 DEBUGP(DBSSGP, "BSSGP FC MS\n");
Harald Weltee6afd602010-05-02 11:19:37 +0200326 /* FIXME: actually implement flow control */
327 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9b455bf2010-03-14 15:45:01 +0800328 break;
329 case BSSGP_PDUT_BVC_BLOCK:
330 /* BSS tells us that BVC shall be blocked */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200331 DEBUGP(DBSSGP, "BSSGP BVC BLOCK ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800332 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
333 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
334 goto err_mand_ie;
Harald Welteeaa614c2010-05-02 11:26:34 +0200335 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte6b72cdf2010-05-11 05:54:22 +0200336 DEBUGPC(DBSSGP, "BVCI=%u, cause=%s\n", bvci,
Harald Welte9b455bf2010-03-14 15:45:01 +0800337 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
Harald Welte6557cd02010-05-02 09:23:16 +0200338 /* We always acknowledge the BLOCKing */
Harald Welte9b455bf2010-03-14 15:45:01 +0800339 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK,
Harald Welte44f1c272010-04-30 19:54:29 +0200340 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800341 break;
342 case BSSGP_PDUT_BVC_UNBLOCK:
343 /* BSS tells us that BVC shall be unblocked */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200344 DEBUGP(DBSSGP, "BSSGP BVC UNBLOCK ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800345 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
346 goto err_mand_ie;
Harald Welteeaa614c2010-05-02 11:26:34 +0200347 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welte6b72cdf2010-05-11 05:54:22 +0200348 DEBUGPC(DBSSGP, "BVCI=%u\n", bvci);
Harald Welte6557cd02010-05-02 09:23:16 +0200349 /* We always acknowledge the unBLOCKing */
Harald Welte9b455bf2010-03-14 15:45:01 +0800350 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK,
Harald Welte44f1c272010-04-30 19:54:29 +0200351 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800352 break;
353 case BSSGP_PDUT_BVC_RESET:
354 /* BSS tells us that BVC init is required */
Harald Welte6b72cdf2010-05-11 05:54:22 +0200355 DEBUGP(DBSSGP, "BSSGP BVC RESET ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800356 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
357 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
358 goto err_mand_ie;
Harald Welte288be162010-05-01 16:48:27 +0200359 rc = bssgp_rx_bvc_reset(msg, &tp, ns_bvci);
Harald Welte9b455bf2010-03-14 15:45:01 +0800360 break;
361 case BSSGP_PDUT_STATUS:
362 /* Some exception has occurred */
Harald Weltee6afd602010-05-02 11:19:37 +0200363 /* FIXME: notify GMM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800364 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
365 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
366 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
367 case BSSGP_PDUT_MODIFY_BSS_PFC:
368 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200369 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x not [yet] implemented\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800370 pdu_type);
371 break;
372 /* those only exist in the SGSN -> BSS direction */
373 case BSSGP_PDUT_DL_UNITDATA:
374 case BSSGP_PDUT_PAGING_PS:
375 case BSSGP_PDUT_PAGING_CS:
376 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
377 case BSSGP_PDUT_SUSPEND_ACK:
378 case BSSGP_PDUT_SUSPEND_NACK:
379 case BSSGP_PDUT_RESUME_ACK:
380 case BSSGP_PDUT_RESUME_NACK:
381 case BSSGP_PDUT_FLUSH_LL_ACK:
382 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
383 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
384 case BSSGP_PDUT_BVC_BLOCK_ACK:
385 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
386 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200387 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800388 pdu_type);
389 rc = -EINVAL;
390 break;
391 default:
Harald Welte6b72cdf2010-05-11 05:54:22 +0200392 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800393 break;
394 }
395
396 return rc;
397err_mand_ie:
398 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
399}
400
Harald Welte6557cd02010-05-02 09:23:16 +0200401/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Weltee6afd602010-05-02 11:19:37 +0200402 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
403int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800404{
Harald Welte6557cd02010-05-02 09:23:16 +0200405 struct bssgp_bts_ctx *bctx;
Harald Welte9b455bf2010-03-14 15:45:01 +0800406 struct bssgp_ud_hdr *budh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200407 uint8_t llc_pdu_tlv_hdr_len = 2;
408 uint8_t *llc_pdu_tlv, *qos_profile;
409 uint16_t pdu_lifetime = 1000; /* centi-seconds */
410 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
411 uint16_t msg_len = msg->len;
Harald Weltee6afd602010-05-02 11:19:37 +0200412 uint16_t bvci = msgb_bvci(msg);
413 uint16_t nsei = msgb_nsei(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800414
Harald Weltee6afd602010-05-02 11:19:37 +0200415 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
416 if (bvci < 2) {
Harald Welte6b72cdf2010-05-11 05:54:22 +0200417 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Weltee6afd602010-05-02 11:19:37 +0200418 bvci);
419 return -EINVAL;
420 }
421
422 bctx = btsctx_by_bvci_nsei(bvci, nsei);
423 if (!bctx)
424 bctx = btsctx_alloc(bvci, nsei);
Harald Welte9b455bf2010-03-14 15:45:01 +0800425
426 if (msg->len > TVLV_MAX_ONEBYTE)
427 llc_pdu_tlv_hdr_len += 1;
428
429 /* prepend the tag and length of the LLC-PDU TLV */
430 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
431 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
432 if (llc_pdu_tlv_hdr_len > 2) {
433 llc_pdu_tlv[1] = msg_len >> 8;
434 llc_pdu_tlv[2] = msg_len & 0xff;
435 } else {
436 llc_pdu_tlv[1] = msg_len & 0x3f;
437 llc_pdu_tlv[1] |= 0x80;
438 }
439
440 /* FIXME: optional elements */
441
442 /* prepend the pdu lifetime */
443 pdu_lifetime = htons(pdu_lifetime);
Harald Welteeaa614c2010-05-02 11:26:34 +0200444 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9b455bf2010-03-14 15:45:01 +0800445
446 /* prepend the QoS profile, TLLI and pdu type */
447 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
448 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte943c5bc2010-04-30 16:33:12 +0200449 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800450 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
451
Harald Weltee6afd602010-05-02 11:19:37 +0200452 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte44f1c272010-04-30 19:54:29 +0200453
454 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800455}