blob: a7acaf5df4e7bacb19ee4628c1d84ee7b5a5a6d8 [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>
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
Harald Welte6752fa42010-05-02 09:23:16 +020039void *bssgp_tall_ctx = NULL;
40
Harald Welte6752fa42010-05-02 09:23:16 +020041#define BVC_F_BLOCKED 0x0001
42
43/* The per-BTS context that we keep on the SGSN side of the BSSGP link */
44struct bssgp_bts_ctx {
45 struct llist_head list;
46
47 /* parsed RA ID and Cell ID of the remote BTS */
48 struct gprs_ra_id ra_id;
49 uint16_t cell_id;
50
51 /* NSEI and BVCI of underlying Gb link. Together they
52 * uniquely identify a link to a BTS (5.4.4) */
53 uint16_t bvci;
54 uint16_t nsei;
55
56 uint32_t bvc_state;
57
58 /* we might want to add this as a shortcut later, avoiding the NSVC
59 * lookup for every packet, similar to a routing cache */
60 //struct gprs_nsvc *nsvc;
61};
Harald Weltefa270b92010-05-11 10:05:12 +020062static LLIST_HEAD(bts_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020063
64/* Find a BTS Context based on parsed RA ID and Cell ID */
65struct bssgp_bts_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
66{
67 struct bssgp_bts_ctx *bctx;
68
69 llist_for_each_entry(bctx, &bts_ctxts, list) {
70 if (!memcmp(&bctx->ra_id, raid, sizeof(bctx->ra_id)) &&
71 bctx->cell_id == cid)
72 return bctx;
73 }
74 return NULL;
75}
76
77/* Find a BTS context based on BVCI+NSEI tuple */
78struct bssgp_bts_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
79{
80 struct bssgp_bts_ctx *bctx;
81
82 llist_for_each_entry(bctx, &bts_ctxts, list) {
83 if (bctx->nsei == nsei && bctx->bvci == bvci)
84 return bctx;
85 }
86 return NULL;
87}
88
Harald Weltefa270b92010-05-11 10:05:12 +020089struct bssgp_bts_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020090{
91 struct bssgp_bts_ctx *ctx;
92
93 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bts_ctx);
94 if (!ctx)
95 return NULL;
96 ctx->bvci = bvci;
97 ctx->nsei = nsei;
98 llist_add(&ctx->list, &bts_ctxts);
99
100 return ctx;
101}
102
Harald Welte9ba50052010-03-14 15:45:01 +0800103/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200104static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800105{
106 struct msgb *msg = bssgp_msgb_alloc();
107 struct bssgp_normal_hdr *bgph =
108 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
109
Harald Welte24a655f2010-04-30 19:54:29 +0200110 msgb_nsei(msg) = nsei;
111 msgb_bvci(msg) = ns_bvci;
112
Harald Welte9ba50052010-03-14 15:45:01 +0800113 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
114 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
115
Harald Welte24a655f2010-04-30 19:54:29 +0200116 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800117}
118
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200119uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200120{
121 /* 6 octets RAC */
122 gsm48_parse_ra(raid, buf);
123 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200124 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200125}
126
Harald Welte3fddf3c2010-05-01 16:48:27 +0200127/* Chapter 8.4 BVC-Reset Procedure */
128static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
129 uint16_t ns_bvci)
130{
Harald Welte6752fa42010-05-02 09:23:16 +0200131 struct bssgp_bts_ctx *bctx;
132 uint16_t nsei = msgb_nsei(msg);
133 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200134 int rc;
135
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200136 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welteb8a6a832010-05-11 05:54:22 +0200137 DEBUGPC(DBSSGP, "BVCI=%u, cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200138 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
139
Harald Welte6752fa42010-05-02 09:23:16 +0200140 /* look-up or create the BTS context for this BVC */
141 bctx = btsctx_by_bvci_nsei(bvci, nsei);
142 if (!bctx)
143 bctx = btsctx_alloc(bvci, nsei);
144
Harald Welte3fddf3c2010-05-01 16:48:27 +0200145 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
146 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200147 if (bvci != 0 && bvci != 1) {
148 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200149 LOGP(DBSSGP, LOGL_ERROR, "BSSGP RESET BVCI=%u "
Harald Welte6752fa42010-05-02 09:23:16 +0200150 "missing mandatory IE\n", bvci);
151 return -EINVAL;
152 }
153 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200154 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
155 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200156 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200157 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
158 bctx->ra_id.rac, bctx->cell_id, bvci);
159 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200160
Harald Welte6752fa42010-05-02 09:23:16 +0200161 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200162 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200163 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200164 return 0;
165}
166
Harald Welte9ba50052010-03-14 15:45:01 +0800167/* Uplink unit-data */
Harald Welte30bc19a2010-05-02 11:19:37 +0200168static int bssgp_rx_ul_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800169{
Harald Welteec19c102010-05-02 09:50:42 +0200170 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Weltef76aee42010-05-02 21:29:36 +0200171 int data_len = msgb_bssgp_len(msg) - sizeof(*budh);
Harald Welte9ba50052010-03-14 15:45:01 +0800172 struct tlv_parsed tp;
173 int rc;
174
Harald Welteb8a6a832010-05-11 05:54:22 +0200175 DEBUGP(DBSSGP, "BSSGP UL-UD\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800176
Harald Welte6752fa42010-05-02 09:23:16 +0200177 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200178 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800179 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
180
181 /* Cell ID and LLC_PDU are the only mandatory IE */
182 if (!TLVP_PRESENT(&tp, BSSGP_IE_CELL_ID) ||
183 !TLVP_PRESENT(&tp, BSSGP_IE_LLC_PDU))
184 return -EIO;
185
Harald Welte30bc19a2010-05-02 11:19:37 +0200186 /* FIXME: lookup bssgp_bts_ctx based on BVCI + NSEI */
187
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200188 /* store pointer to LLC header and CELL ID in msgb->cb */
Harald Welte510c3922010-04-30 16:33:12 +0200189 msgb_llch(msg) = TLVP_VAL(&tp, BSSGP_IE_LLC_PDU);
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200190 msgb_bcid(msg) = TLVP_VAL(&tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800191
192 return gprs_llc_rcvmsg(msg, &tp);
193}
194
Harald Welte30bc19a2010-05-02 11:19:37 +0200195static int bssgp_rx_suspend(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800196{
Harald Welteec19c102010-05-02 09:50:42 +0200197 struct bssgp_normal_hdr *bgph =
198 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltef76aee42010-05-02 21:29:36 +0200199 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9ba50052010-03-14 15:45:01 +0800200 struct tlv_parsed tp;
201 int rc;
202
Harald Welteb8a6a832010-05-11 05:54:22 +0200203 DEBUGP(DBSSGP, "BSSGP SUSPEND\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800204
205 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
206 if (rc < 0)
207 return rc;
208
209 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
210 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA))
211 return -EIO;
212
Harald Welte30bc19a2010-05-02 11:19:37 +0200213 /* FIXME: pass the SUSPEND request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800214 /* SEND SUSPEND_ACK or SUSPEND_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800215}
216
Harald Welte30bc19a2010-05-02 11:19:37 +0200217static int bssgp_rx_resume(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800218{
Harald Welteec19c102010-05-02 09:50:42 +0200219 struct bssgp_normal_hdr *bgph =
220 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltef76aee42010-05-02 21:29:36 +0200221 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte9ba50052010-03-14 15:45:01 +0800222 struct tlv_parsed tp;
223 int rc;
224
Harald Welteb8a6a832010-05-11 05:54:22 +0200225 DEBUGP(DBSSGP, "BSSGP RESUME\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800226
227 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
228 if (rc < 0)
229 return rc;
230
231 if (!TLVP_PRESENT(&tp, BSSGP_IE_TLLI) ||
232 !TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA) ||
233 !TLVP_PRESENT(&tp, BSSGP_IE_SUSPEND_REF_NR))
234 return -EIO;
235
Harald Welte30bc19a2010-05-02 11:19:37 +0200236 /* FIXME: pass the RESUME request to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800237 /* SEND RESUME_ACK or RESUME_NACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800238}
239
Harald Welte30bc19a2010-05-02 11:19:37 +0200240static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp)
Harald Welte9ba50052010-03-14 15:45:01 +0800241{
242
Harald Welteb8a6a832010-05-11 05:54:22 +0200243 DEBUGP(DBSSGP, "BSSGP FC BVC\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800244
245 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
246 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
247 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
248 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
249 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS))
250 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
251
Harald Welte30bc19a2010-05-02 11:19:37 +0200252 /* FIXME: actually implement flow control */
253
Harald Welte9ba50052010-03-14 15:45:01 +0800254 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200255 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200256 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800257}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200258
Harald Welteec19c102010-05-02 09:50:42 +0200259/* We expect msgb_bssgph() to point to the BSSGP header */
Harald Welte30bc19a2010-05-02 11:19:37 +0200260int gprs_bssgp_rcvmsg(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800261{
Harald Welteec19c102010-05-02 09:50:42 +0200262 struct bssgp_normal_hdr *bgph =
263 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800264 struct tlv_parsed tp;
Harald Welte30bc19a2010-05-02 11:19:37 +0200265 uint8_t pdu_type = bgph->pdu_type;
Harald Weltef76aee42010-05-02 21:29:36 +0200266 int data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
Harald Welte30bc19a2010-05-02 11:19:37 +0200267 uint16_t bvci; /* PTP BVCI */
268 uint16_t ns_bvci = msgb_bvci(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800269 int rc = 0;
270
Harald Welte30bc19a2010-05-02 11:19:37 +0200271 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
272
Harald Welte6752fa42010-05-02 09:23:16 +0200273 /* UNITDATA BSSGP headers have TLLI in front */
Harald Welte9ba50052010-03-14 15:45:01 +0800274 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
275 pdu_type != BSSGP_PDUT_DL_UNITDATA)
276 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
277
278 switch (pdu_type) {
279 case BSSGP_PDUT_UL_UNITDATA:
280 /* some LLC data from the MS */
Harald Welte30bc19a2010-05-02 11:19:37 +0200281 rc = bssgp_rx_ul_ud(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800282 break;
283 case BSSGP_PDUT_RA_CAPABILITY:
284 /* BSS requests RA capability or IMSI */
Harald Welteb8a6a832010-05-11 05:54:22 +0200285 DEBUGP(DBSSGP, "BSSGP RA CAPABILITY UPDATE\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200286 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800287 /* FIXME: send RA_CAPA_UPDATE_ACK */
288 break;
289 case BSSGP_PDUT_RADIO_STATUS:
Harald Welteb8a6a832010-05-11 05:54:22 +0200290 DEBUGP(DBSSGP, "BSSGP RADIO STATUS\n");
Harald Welte9ba50052010-03-14 15:45:01 +0800291 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200292 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800293 break;
294 case BSSGP_PDUT_SUSPEND:
295 /* MS wants to suspend */
Harald Welte30bc19a2010-05-02 11:19:37 +0200296 rc = bssgp_rx_suspend(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800297 break;
298 case BSSGP_PDUT_RESUME:
299 /* MS wants to resume */
Harald Welte30bc19a2010-05-02 11:19:37 +0200300 rc = bssgp_rx_resume(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800301 break;
Harald Welte6b7cf252010-05-13 19:41:31 +0200302 case BSSGP_PDUT_FLUSH_LL_ACK:
303 /* BSS informs us it has performed LL FLUSH */
Harald Welteb8a6a832010-05-11 05:54:22 +0200304 DEBUGP(DBSSGP, "BSSGP FLUSH LL\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200305 /* FIXME: send NM_FLUSH_LL.res to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800306 break;
307 case BSSGP_PDUT_LLC_DISCARD:
308 /* BSS informs that some LLC PDU's have been discarded */
Harald Welteb8a6a832010-05-11 05:54:22 +0200309 DEBUGP(DBSSGP, "BSSGP LLC DISCARDED\n");
Harald Welte6b7cf252010-05-13 19:41:31 +0200310 /* FIXME: send NM_LLC_DISCARDED to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800311 break;
312 case BSSGP_PDUT_FLOW_CONTROL_BVC:
313 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte30bc19a2010-05-02 11:19:37 +0200314 rc = bssgp_rx_fc_bvc(msg, &tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800315 break;
316 case BSSGP_PDUT_FLOW_CONTROL_MS:
317 /* BSS informs us of available bandwidth to one MS */
Harald Welteb8a6a832010-05-11 05:54:22 +0200318 DEBUGP(DBSSGP, "BSSGP FC MS\n");
Harald Welte30bc19a2010-05-02 11:19:37 +0200319 /* FIXME: actually implement flow control */
320 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800321 break;
322 case BSSGP_PDUT_BVC_BLOCK:
323 /* BSS tells us that BVC shall be blocked */
Harald Welteb8a6a832010-05-11 05:54:22 +0200324 DEBUGP(DBSSGP, "BSSGP BVC BLOCK ");
Harald Welte9ba50052010-03-14 15:45:01 +0800325 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
326 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
327 goto err_mand_ie;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200328 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welteb8a6a832010-05-11 05:54:22 +0200329 DEBUGPC(DBSSGP, "BVCI=%u, cause=%s\n", bvci,
Harald Welte9ba50052010-03-14 15:45:01 +0800330 bssgp_cause_str(*TLVP_VAL(&tp, BSSGP_IE_CAUSE)));
Harald Welte6752fa42010-05-02 09:23:16 +0200331 /* We always acknowledge the BLOCKing */
Harald Welte9ba50052010-03-14 15:45:01 +0800332 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK,
Harald Welte24a655f2010-04-30 19:54:29 +0200333 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200334 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800335 break;
336 case BSSGP_PDUT_BVC_UNBLOCK:
337 /* BSS tells us that BVC shall be unblocked */
Harald Welteb8a6a832010-05-11 05:54:22 +0200338 DEBUGP(DBSSGP, "BSSGP BVC UNBLOCK ");
Harald Welte9ba50052010-03-14 15:45:01 +0800339 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI))
340 goto err_mand_ie;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200341 bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
Harald Welteb8a6a832010-05-11 05:54:22 +0200342 DEBUGPC(DBSSGP, "BVCI=%u\n", bvci);
Harald Welte6752fa42010-05-02 09:23:16 +0200343 /* We always acknowledge the unBLOCKing */
Harald Welte9ba50052010-03-14 15:45:01 +0800344 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK,
Harald Welte24a655f2010-04-30 19:54:29 +0200345 msgb_nsei(msg), bvci, ns_bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200346 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800347 break;
348 case BSSGP_PDUT_BVC_RESET:
349 /* BSS tells us that BVC init is required */
Harald Welteb8a6a832010-05-11 05:54:22 +0200350 DEBUGP(DBSSGP, "BSSGP BVC RESET ");
Harald Welte9ba50052010-03-14 15:45:01 +0800351 if (!TLVP_PRESENT(&tp, BSSGP_IE_BVCI) ||
352 !TLVP_PRESENT(&tp, BSSGP_IE_CAUSE))
353 goto err_mand_ie;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200354 rc = bssgp_rx_bvc_reset(msg, &tp, ns_bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800355 break;
356 case BSSGP_PDUT_STATUS:
357 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200358 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800359 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
360 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
361 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
362 case BSSGP_PDUT_MODIFY_BSS_PFC:
363 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Welteb8a6a832010-05-11 05:54:22 +0200364 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x not [yet] implemented\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800365 pdu_type);
366 break;
367 /* those only exist in the SGSN -> BSS direction */
368 case BSSGP_PDUT_DL_UNITDATA:
369 case BSSGP_PDUT_PAGING_PS:
370 case BSSGP_PDUT_PAGING_CS:
371 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
372 case BSSGP_PDUT_SUSPEND_ACK:
373 case BSSGP_PDUT_SUSPEND_NACK:
374 case BSSGP_PDUT_RESUME_ACK:
375 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200376 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800377 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
378 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
379 case BSSGP_PDUT_BVC_BLOCK_ACK:
380 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
381 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welteb8a6a832010-05-11 05:54:22 +0200382 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x only exists in DL\n",
Harald Welte9ba50052010-03-14 15:45:01 +0800383 pdu_type);
384 rc = -EINVAL;
385 break;
386 default:
Harald Welteb8a6a832010-05-11 05:54:22 +0200387 DEBUGP(DBSSGP, "BSSGP PDU type 0x%02x unknown\n", pdu_type);
Harald Welte9ba50052010-03-14 15:45:01 +0800388 break;
389 }
390
391 return rc;
392err_mand_ie:
393 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
394}
395
Harald Welte6752fa42010-05-02 09:23:16 +0200396/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200397 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
398int gprs_bssgp_tx_dl_ud(struct msgb *msg)
Harald Welte9ba50052010-03-14 15:45:01 +0800399{
Harald Welte6752fa42010-05-02 09:23:16 +0200400 struct bssgp_bts_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800401 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200402 uint8_t llc_pdu_tlv_hdr_len = 2;
403 uint8_t *llc_pdu_tlv, *qos_profile;
404 uint16_t pdu_lifetime = 1000; /* centi-seconds */
405 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x21 };
406 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200407 uint16_t bvci = msgb_bvci(msg);
408 uint16_t nsei = msgb_nsei(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800409
Harald Welte30bc19a2010-05-02 11:19:37 +0200410 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
411 if (bvci < 2) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200412 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200413 bvci);
414 return -EINVAL;
415 }
416
417 bctx = btsctx_by_bvci_nsei(bvci, nsei);
418 if (!bctx)
419 bctx = btsctx_alloc(bvci, nsei);
Harald Welte9ba50052010-03-14 15:45:01 +0800420
421 if (msg->len > TVLV_MAX_ONEBYTE)
422 llc_pdu_tlv_hdr_len += 1;
423
424 /* prepend the tag and length of the LLC-PDU TLV */
425 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
426 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
427 if (llc_pdu_tlv_hdr_len > 2) {
428 llc_pdu_tlv[1] = msg_len >> 8;
429 llc_pdu_tlv[2] = msg_len & 0xff;
430 } else {
431 llc_pdu_tlv[1] = msg_len & 0x3f;
432 llc_pdu_tlv[1] |= 0x80;
433 }
434
435 /* FIXME: optional elements */
436
437 /* prepend the pdu lifetime */
438 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200439 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800440
441 /* prepend the QoS profile, TLLI and pdu type */
442 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
443 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200444 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800445 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
446
Harald Welte30bc19a2010-05-02 11:19:37 +0200447 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200448
449 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800450}