blob: e93b3de9a5c599eb433ad3132b9d2921b0756b69 [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
Harald Weltee4cbb3f2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9ba50052010-03-14 15:45:01 +080010 * (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
Harald Weltee4cbb3f2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9ba50052010-03-14 15:45:01 +080016 *
Harald Weltee4cbb3f2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9ba50052010-03-14 15:45:01 +080019 *
Harald Welte4e5721d2010-05-17 23:41:43 +020020 * TODO:
21 * o properly count incoming BVC-RESET packets in counter group
22 * o set log context as early as possible for outgoing packets
Harald Welte9ba50052010-03-14 15:45:01 +080023 */
24
25#include <errno.h>
Harald Welte8f9a3ee2010-05-02 11:26:34 +020026#include <stdint.h>
Harald Welte9ba50052010-03-14 15:45:01 +080027
28#include <netinet/in.h>
29
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010030#include <osmocom/core/msgb.h>
31#include <osmocom/gsm/tlv.h>
32#include <osmocom/core/talloc.h>
33#include <osmocom/core/rate_ctr.h>
Harald Welte6752fa42010-05-02 09:23:16 +020034
Harald Welte9ba50052010-03-14 15:45:01 +080035#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/gsm_04_08_gprs.h>
Harald Welte73952e32012-06-16 14:59:56 +080038
39#include <osmocom/gprs/gprs_bssgp.h>
40#include <osmocom/gprs/gprs_ns.h>
41
Harald Welte313cccf2010-06-09 11:22:47 +020042#include <openbsc/gprs_gmm.h>
Harald Welte9ba50052010-03-14 15:45:01 +080043
Harald Welte6752fa42010-05-02 09:23:16 +020044void *bssgp_tall_ctx = NULL;
45
Harald Welte25de8112010-05-13 21:26:28 +020046static const struct rate_ctr_desc bssgp_ctr_description[] = {
Harald Welte16c8dbb2010-05-17 23:30:01 +020047 { "packets.in", "Packets at BSSGP Level ( In)" },
48 { "packets.out","Packets at BSSGP Level (Out)" },
49 { "bytes.in", "Bytes at BSSGP Level ( In)" },
50 { "bytes.out", "Bytes at BSSGP Level (Out)" },
Harald Welte25de8112010-05-13 21:26:28 +020051 { "blocked", "BVC Blocking count" },
52 { "discarded", "BVC LLC Discarded count" },
53};
54
55static const struct rate_ctr_group_desc bssgp_ctrg_desc = {
56 .group_name_prefix = "bssgp.bss_ctx",
57 .group_description = "BSSGP Peer Statistics",
58 .num_ctr = ARRAY_SIZE(bssgp_ctr_description),
59 .ctr_desc = bssgp_ctr_description,
60};
61
Harald Weltea78b9c22010-05-17 23:02:42 +020062LLIST_HEAD(bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +020063
64/* Find a BTS Context based on parsed RA ID and Cell ID */
Harald Welte8a521132010-05-17 22:59:29 +020065struct bssgp_bvc_ctx *btsctx_by_raid_cid(const struct gprs_ra_id *raid, uint16_t cid)
Harald Welte6752fa42010-05-02 09:23:16 +020066{
Harald Welte8a521132010-05-17 22:59:29 +020067 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020068
Harald Weltea78b9c22010-05-17 23:02:42 +020069 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020070 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 */
Harald Welte8a521132010-05-17 22:59:29 +020078struct bssgp_bvc_ctx *btsctx_by_bvci_nsei(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020079{
Harald Welte8a521132010-05-17 22:59:29 +020080 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +020081
Harald Weltea78b9c22010-05-17 23:02:42 +020082 llist_for_each_entry(bctx, &bssgp_bvc_ctxts, list) {
Harald Welte6752fa42010-05-02 09:23:16 +020083 if (bctx->nsei == nsei && bctx->bvci == bvci)
84 return bctx;
85 }
86 return NULL;
87}
88
Harald Welte8a521132010-05-17 22:59:29 +020089struct bssgp_bvc_ctx *btsctx_alloc(uint16_t bvci, uint16_t nsei)
Harald Welte6752fa42010-05-02 09:23:16 +020090{
Harald Welte8a521132010-05-17 22:59:29 +020091 struct bssgp_bvc_ctx *ctx;
Harald Welte6752fa42010-05-02 09:23:16 +020092
Harald Welte8a521132010-05-17 22:59:29 +020093 ctx = talloc_zero(bssgp_tall_ctx, struct bssgp_bvc_ctx);
Harald Welte6752fa42010-05-02 09:23:16 +020094 if (!ctx)
95 return NULL;
96 ctx->bvci = bvci;
97 ctx->nsei = nsei;
Harald Welte25de8112010-05-13 21:26:28 +020098 /* FIXME: BVCI is not unique, only BVCI+NSEI ?!? */
99 ctx->ctrg = rate_ctr_group_alloc(ctx, &bssgp_ctrg_desc, bvci);
100
Harald Weltea78b9c22010-05-17 23:02:42 +0200101 llist_add(&ctx->list, &bssgp_bvc_ctxts);
Harald Welte6752fa42010-05-02 09:23:16 +0200102
103 return ctx;
104}
105
Harald Welte9ba50052010-03-14 15:45:01 +0800106/* Chapter 10.4.5: Flow Control BVC ACK */
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200107static int bssgp_tx_fc_bvc_ack(uint16_t nsei, uint8_t tag, uint16_t ns_bvci)
Harald Welte9ba50052010-03-14 15:45:01 +0800108{
109 struct msgb *msg = bssgp_msgb_alloc();
110 struct bssgp_normal_hdr *bgph =
111 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
112
Harald Welte24a655f2010-04-30 19:54:29 +0200113 msgb_nsei(msg) = nsei;
114 msgb_bvci(msg) = ns_bvci;
115
Harald Welte9ba50052010-03-14 15:45:01 +0800116 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC_ACK;
117 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
118
Harald Welte24a655f2010-04-30 19:54:29 +0200119 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800120}
121
Harald Weltea8aa4df2010-05-30 22:00:53 +0200122/* 10.3.7 SUSPEND-ACK PDU */
123int bssgp_tx_suspend_ack(uint16_t nsei, uint32_t tlli,
124 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
125{
126 struct msgb *msg = bssgp_msgb_alloc();
127 struct bssgp_normal_hdr *bgph =
128 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
129 uint32_t _tlli;
130 uint8_t ra[6];
131
132 msgb_nsei(msg) = nsei;
133 msgb_bvci(msg) = 0; /* Signalling */
134 bgph->pdu_type = BSSGP_PDUT_SUSPEND_ACK;
135
136 _tlli = htonl(tlli);
137 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
138 gsm48_construct_ra(ra, ra_id);
139 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
140 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
141
142 return gprs_ns_sendmsg(bssgp_nsi, msg);
143}
144
145/* 10.3.8 SUSPEND-NACK PDU */
146int bssgp_tx_suspend_nack(uint16_t nsei, uint32_t tlli,
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100147 const struct gprs_ra_id *ra_id,
Harald Weltea8aa4df2010-05-30 22:00:53 +0200148 uint8_t *cause)
149{
150 struct msgb *msg = bssgp_msgb_alloc();
151 struct bssgp_normal_hdr *bgph =
152 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
153 uint32_t _tlli;
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100154 uint8_t ra[6];
Harald Weltea8aa4df2010-05-30 22:00:53 +0200155
156 msgb_nsei(msg) = nsei;
157 msgb_bvci(msg) = 0; /* Signalling */
158 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
159
160 _tlli = htonl(tlli);
161 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100162 gsm48_construct_ra(ra, ra_id);
163 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200164 if (cause)
165 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
166
167 return gprs_ns_sendmsg(bssgp_nsi, msg);
168}
169
170/* 10.3.10 RESUME-ACK PDU */
171int bssgp_tx_resume_ack(uint16_t nsei, uint32_t tlli,
172 const struct gprs_ra_id *ra_id)
173{
174 struct msgb *msg = bssgp_msgb_alloc();
175 struct bssgp_normal_hdr *bgph =
176 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
177 uint32_t _tlli;
178 uint8_t ra[6];
179
180 msgb_nsei(msg) = nsei;
181 msgb_bvci(msg) = 0; /* Signalling */
182 bgph->pdu_type = BSSGP_PDUT_RESUME_ACK;
183
184 _tlli = htonl(tlli);
185 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
186 gsm48_construct_ra(ra, ra_id);
187 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
188
189 return gprs_ns_sendmsg(bssgp_nsi, msg);
190}
191
192/* 10.3.11 RESUME-NACK PDU */
193int bssgp_tx_resume_nack(uint16_t nsei, uint32_t tlli,
194 const struct gprs_ra_id *ra_id, uint8_t *cause)
195{
196 struct msgb *msg = bssgp_msgb_alloc();
197 struct bssgp_normal_hdr *bgph =
198 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
199 uint32_t _tlli;
200 uint8_t ra[6];
201
202 msgb_nsei(msg) = nsei;
203 msgb_bvci(msg) = 0; /* Signalling */
204 bgph->pdu_type = BSSGP_PDUT_SUSPEND_NACK;
205
206 _tlli = htonl(tlli);
207 msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
208 gsm48_construct_ra(ra, ra_id);
209 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
210 if (cause)
211 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, cause);
212
213 return gprs_ns_sendmsg(bssgp_nsi, msg);
214}
215
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200216uint16_t bssgp_parse_cell_id(struct gprs_ra_id *raid, const uint8_t *buf)
Harald Welte6752fa42010-05-02 09:23:16 +0200217{
218 /* 6 octets RAC */
219 gsm48_parse_ra(raid, buf);
220 /* 2 octets CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200221 return ntohs(*(uint16_t *) (buf+6));
Harald Welte6752fa42010-05-02 09:23:16 +0200222}
223
Harald Welte28610072011-11-24 21:32:07 +0100224int bssgp_create_cell_id(uint8_t *buf, const struct gprs_ra_id *raid,
225 uint16_t cid)
226{
227 uint16_t *out_cid = (uint16_t *) (buf + 6);
228 /* 6 octets RAC */
229 gsm48_construct_ra(buf, raid);
230 /* 2 octets CID */
231 *out_cid = htons(cid);
232
233 return 8;
234}
235
Harald Welte3fddf3c2010-05-01 16:48:27 +0200236/* Chapter 8.4 BVC-Reset Procedure */
237static int bssgp_rx_bvc_reset(struct msgb *msg, struct tlv_parsed *tp,
238 uint16_t ns_bvci)
239{
Harald Welte8a521132010-05-17 22:59:29 +0200240 struct bssgp_bvc_ctx *bctx;
Harald Welte6752fa42010-05-02 09:23:16 +0200241 uint16_t nsei = msgb_nsei(msg);
242 uint16_t bvci;
Harald Welte3fddf3c2010-05-01 16:48:27 +0200243 int rc;
244
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200245 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Weltee9686b62010-05-31 18:07:17 +0200246 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RESET cause=%s\n", bvci,
Harald Welte3fddf3c2010-05-01 16:48:27 +0200247 bssgp_cause_str(*TLVP_VAL(tp, BSSGP_IE_CAUSE)));
248
Harald Welte6752fa42010-05-02 09:23:16 +0200249 /* look-up or create the BTS context for this BVC */
250 bctx = btsctx_by_bvci_nsei(bvci, nsei);
251 if (!bctx)
252 bctx = btsctx_alloc(bvci, nsei);
253
Harald Welte25de8112010-05-13 21:26:28 +0200254 /* As opposed to NS-VCs, BVCs are NOT blocked after RESET */
255 bctx->state &= ~BVC_S_BLOCKED;
256
Harald Welte3fddf3c2010-05-01 16:48:27 +0200257 /* When we receive a BVC-RESET PDU (at least of a PTP BVCI), the BSS
258 * informs us about its RAC + Cell ID, so we can create a mapping */
Harald Welte6752fa42010-05-02 09:23:16 +0200259 if (bvci != 0 && bvci != 1) {
260 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) {
Harald Welte086fe322011-08-19 16:45:19 +0200261 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESET "
Harald Welte6752fa42010-05-02 09:23:16 +0200262 "missing mandatory IE\n", bvci);
263 return -EINVAL;
264 }
265 /* actually extract RAC / CID */
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200266 bctx->cell_id = bssgp_parse_cell_id(&bctx->ra_id,
267 TLVP_VAL(tp, BSSGP_IE_CELL_ID));
Harald Welteb8a6a832010-05-11 05:54:22 +0200268 LOGP(DBSSGP, LOGL_NOTICE, "Cell %u-%u-%u-%u CI %u on BVCI %u\n",
Harald Welte6752fa42010-05-02 09:23:16 +0200269 bctx->ra_id.mcc, bctx->ra_id.mnc, bctx->ra_id.lac,
270 bctx->ra_id.rac, bctx->cell_id, bvci);
271 }
Harald Welte3fddf3c2010-05-01 16:48:27 +0200272
Harald Welte6752fa42010-05-02 09:23:16 +0200273 /* Acknowledge the RESET to the BTS */
Harald Welte3fddf3c2010-05-01 16:48:27 +0200274 rc = bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_RESET_ACK,
Harald Welte6752fa42010-05-02 09:23:16 +0200275 nsei, bvci, ns_bvci);
Harald Welte3fddf3c2010-05-01 16:48:27 +0200276 return 0;
277}
278
Harald Welte25de8112010-05-13 21:26:28 +0200279static int bssgp_rx_bvc_block(struct msgb *msg, struct tlv_parsed *tp)
280{
281 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200282 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200283
284 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200285 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200286 /* 8.3.2: Signalling BVC shall never be blocked */
287 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
288 "received block for signalling BVC!?!\n",
289 msgb_nsei(msg), msgb_bvci(msg));
290 return 0;
291 }
Harald Welte25de8112010-05-13 21:26:28 +0200292
Harald Welte086fe322011-08-19 16:45:19 +0200293 LOGP(DBSSGP, LOGL_INFO, "BSSGP Rx BVCI=%u BVC-BLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200294
295 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
296 if (!ptp_ctx)
297 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
298
299 ptp_ctx->state |= BVC_S_BLOCKED;
300 rate_ctr_inc(&ptp_ctx->ctrg->ctr[BSSGP_CTR_BLOCKED]);
301
302 /* FIXME: Send NM_BVC_BLOCK.ind to NM */
303
304 /* We always acknowledge the BLOCKing */
305 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_BLOCK_ACK, msgb_nsei(msg),
306 bvci, msgb_bvci(msg));
307};
308
309static int bssgp_rx_bvc_unblock(struct msgb *msg, struct tlv_parsed *tp)
310{
311 uint16_t bvci;
Harald Welte8a521132010-05-17 22:59:29 +0200312 struct bssgp_bvc_ctx *ptp_ctx;
Harald Welte25de8112010-05-13 21:26:28 +0200313
314 bvci = ntohs(*(uint16_t *)TLVP_VAL(tp, BSSGP_IE_BVCI));
Harald Welte61c07842010-05-18 11:57:08 +0200315 if (bvci == BVCI_SIGNALLING) {
Harald Welte58e65c92010-05-13 21:45:23 +0200316 /* 8.3.2: Signalling BVC shall never be blocked */
317 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u/BVCI=%u "
318 "received unblock for signalling BVC!?!\n",
319 msgb_nsei(msg), msgb_bvci(msg));
320 return 0;
321 }
Harald Welte25de8112010-05-13 21:26:28 +0200322
Harald Weltee9686b62010-05-31 18:07:17 +0200323 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC-UNBLOCK\n", bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200324
325 ptp_ctx = btsctx_by_bvci_nsei(bvci, msgb_nsei(msg));
326 if (!ptp_ctx)
327 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
328
329 ptp_ctx->state &= ~BVC_S_BLOCKED;
330
331 /* FIXME: Send NM_BVC_UNBLOCK.ind to NM */
332
333 /* We always acknowledge the unBLOCKing */
334 return bssgp_tx_simple_bvci(BSSGP_PDUT_BVC_UNBLOCK_ACK, msgb_nsei(msg),
335 bvci, msgb_bvci(msg));
336};
337
Harald Welte9ba50052010-03-14 15:45:01 +0800338/* Uplink unit-data */
Harald Welte25de8112010-05-13 21:26:28 +0200339static int bssgp_rx_ul_ud(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200340 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800341{
Harald Welteec19c102010-05-02 09:50:42 +0200342 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800343
Harald Welte6752fa42010-05-02 09:23:16 +0200344 /* extract TLLI and parse TLV IEs */
Harald Welte510c3922010-04-30 16:33:12 +0200345 msgb_tlli(msg) = ntohl(budh->tlli);
Harald Welte9ba50052010-03-14 15:45:01 +0800346
Harald Welte086fe322011-08-19 16:45:19 +0200347 DEBUGP(DBSSGP, "BSSGP TLLI=0x%08x Rx UPLINK-UNITDATA\n", msgb_tlli(msg));
Harald Weltee9686b62010-05-31 18:07:17 +0200348
Harald Welte9ba50052010-03-14 15:45:01 +0800349 /* Cell ID and LLC_PDU are the only mandatory IE */
Harald Welte25de8112010-05-13 21:26:28 +0200350 if (!TLVP_PRESENT(tp, BSSGP_IE_CELL_ID) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200351 !TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
352 LOGP(DBSSGP, LOGL_ERROR, "BSSGP TLLI=0x%08x Rx UL-UD "
353 "missing mandatory IE\n", msgb_tlli(msg));
Harald Welte25de8112010-05-13 21:26:28 +0200354 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200355 }
Harald Welte30bc19a2010-05-02 11:19:37 +0200356
Harald Weltea2ca4ed2010-05-02 11:54:55 +0200357 /* store pointer to LLC header and CELL ID in msgb->cb */
Holger Hans Peter Freytherb6eded82010-05-23 21:11:19 +0800358 msgb_llch(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
359 msgb_bcid(msg) = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_CELL_ID);
Harald Welte9ba50052010-03-14 15:45:01 +0800360
Harald Welte25de8112010-05-13 21:26:28 +0200361 return gprs_llc_rcvmsg(msg, tp);
Harald Welte9ba50052010-03-14 15:45:01 +0800362}
363
Harald Welte25de8112010-05-13 21:26:28 +0200364static int bssgp_rx_suspend(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200365 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800366{
Harald Welteec19c102010-05-02 09:50:42 +0200367 struct bssgp_normal_hdr *bgph =
368 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200369 struct gprs_ra_id raid;
370 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200371 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800372
Harald Welte25de8112010-05-13 21:26:28 +0200373 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200374 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) {
375 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx SUSPEND "
376 "missing mandatory IE\n", ctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200377 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200378 }
Harald Welte9ba50052010-03-14 15:45:01 +0800379
Harald Weltea8aa4df2010-05-30 22:00:53 +0200380 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200381
Harald Welte17925322010-05-31 20:18:35 +0200382 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx SUSPEND\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200383 ctx->bvci, tlli);
384
Harald Weltea8aa4df2010-05-30 22:00:53 +0200385 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
386
Harald Welte313cccf2010-06-09 11:22:47 +0200387 /* Inform GMM about the SUSPEND request */
388 rc = gprs_gmm_rx_suspend(&raid, tlli);
389 if (rc < 0)
Dieter Spaard2b13fc2010-12-12 12:45:08 +0100390 return bssgp_tx_suspend_nack(msgb_nsei(msg), tlli, &raid, NULL);
Harald Welte313cccf2010-06-09 11:22:47 +0200391
Harald Weltea8aa4df2010-05-30 22:00:53 +0200392 bssgp_tx_suspend_ack(msgb_nsei(msg), tlli, &raid, 0);
393
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800394 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800395}
396
Harald Welte25de8112010-05-13 21:26:28 +0200397static int bssgp_rx_resume(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200398 struct bssgp_bvc_ctx *ctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800399{
Harald Welteec19c102010-05-02 09:50:42 +0200400 struct bssgp_normal_hdr *bgph =
401 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Weltea8aa4df2010-05-30 22:00:53 +0200402 struct gprs_ra_id raid;
403 uint32_t tlli;
Harald Welte313cccf2010-06-09 11:22:47 +0200404 uint8_t suspend_ref;
405 int rc;
Harald Welte9ba50052010-03-14 15:45:01 +0800406
Harald Welte25de8112010-05-13 21:26:28 +0200407 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
408 !TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200409 !TLVP_PRESENT(tp, BSSGP_IE_SUSPEND_REF_NR)) {
410 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx RESUME "
411 "missing mandatory IE\n", ctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200412 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200413 }
Harald Welte9ba50052010-03-14 15:45:01 +0800414
Harald Weltea8aa4df2010-05-30 22:00:53 +0200415 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Welte313cccf2010-06-09 11:22:47 +0200416 suspend_ref = *TLVP_VAL(tp, BSSGP_IE_SUSPEND_REF_NR);
Harald Weltee9686b62010-05-31 18:07:17 +0200417
Harald Welte086fe322011-08-19 16:45:19 +0200418 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=0x%08x Rx RESUME\n", ctx->bvci, tlli);
Harald Weltee9686b62010-05-31 18:07:17 +0200419
Harald Weltea8aa4df2010-05-30 22:00:53 +0200420 gsm48_parse_ra(&raid, TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA));
421
Harald Welte313cccf2010-06-09 11:22:47 +0200422 /* Inform GMM about the RESUME request */
423 rc = gprs_gmm_rx_resume(&raid, tlli, suspend_ref);
424 if (rc < 0)
425 return bssgp_tx_resume_nack(msgb_nsei(msg), tlli, &raid,
426 NULL);
427
Harald Weltea8aa4df2010-05-30 22:00:53 +0200428 bssgp_tx_resume_ack(msgb_nsei(msg), tlli, &raid);
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800429 return 0;
Harald Welte9ba50052010-03-14 15:45:01 +0800430}
431
Harald Weltee9686b62010-05-31 18:07:17 +0200432
433static int bssgp_rx_llc_disc(struct msgb *msg, struct tlv_parsed *tp,
434 struct bssgp_bvc_ctx *ctx)
435{
Harald Welteb7363142010-07-23 21:59:29 +0200436 uint32_t tlli = 0;
Harald Weltee9686b62010-05-31 18:07:17 +0200437
438 if (!TLVP_PRESENT(tp, BSSGP_IE_TLLI) ||
439 !TLVP_PRESENT(tp, BSSGP_IE_LLC_FRAMES_DISCARDED) ||
440 !TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
441 !TLVP_PRESENT(tp, BSSGP_IE_NUM_OCT_AFF)) {
442 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx LLC DISCARDED "
443 "missing mandatory IE\n", ctx->bvci);
444 }
445
Harald Welteb7363142010-07-23 21:59:29 +0200446 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
447 tlli = ntohl(*(uint32_t *)TLVP_VAL(tp, BSSGP_IE_TLLI));
Harald Weltee9686b62010-05-31 18:07:17 +0200448
Harald Welte086fe322011-08-19 16:45:19 +0200449 DEBUGP(DBSSGP, "BSSGP BVCI=%u TLLI=%08x Rx LLC DISCARDED\n",
Harald Weltee9686b62010-05-31 18:07:17 +0200450 ctx->bvci, tlli);
451
452 rate_ctr_inc(&ctx->ctrg->ctr[BSSGP_CTR_DISCARDED]);
453
454 /* FIXME: send NM_LLC_DISCARDED to NM */
455 return 0;
456}
457
Harald Welte25de8112010-05-13 21:26:28 +0200458static int bssgp_rx_fc_bvc(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200459 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800460{
461
Harald Weltee9686b62010-05-31 18:07:17 +0200462 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control BVC\n",
463 bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800464
465 if (!TLVP_PRESENT(tp, BSSGP_IE_TAG) ||
466 !TLVP_PRESENT(tp, BSSGP_IE_BVC_BUCKET_SIZE) ||
467 !TLVP_PRESENT(tp, BSSGP_IE_BUCKET_LEAK_RATE) ||
468 !TLVP_PRESENT(tp, BSSGP_IE_BMAX_DEFAULT_MS) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200469 !TLVP_PRESENT(tp, BSSGP_IE_R_DEFAULT_MS)) {
470 LOGP(DBSSGP, LOGL_ERROR, "BSSGP BVCI=%u Rx FC BVC "
471 "missing mandatory IE\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800472 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Harald Weltee9686b62010-05-31 18:07:17 +0200473 }
Harald Welte9ba50052010-03-14 15:45:01 +0800474
Harald Welte30bc19a2010-05-02 11:19:37 +0200475 /* FIXME: actually implement flow control */
476
Harald Welte9ba50052010-03-14 15:45:01 +0800477 /* Send FLOW_CONTROL_BVC_ACK */
Harald Welte24a655f2010-04-30 19:54:29 +0200478 return bssgp_tx_fc_bvc_ack(msgb_nsei(msg), *TLVP_VAL(tp, BSSGP_IE_TAG),
Harald Welte30bc19a2010-05-02 11:19:37 +0200479 msgb_bvci(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800480}
Harald Welte3fddf3c2010-05-01 16:48:27 +0200481
Harald Welte25de8112010-05-13 21:26:28 +0200482/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
483static int gprs_bssgp_rx_ptp(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200484 struct bssgp_bvc_ctx *bctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800485{
Harald Welteec19c102010-05-02 09:50:42 +0200486 struct bssgp_normal_hdr *bgph =
487 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Harald Welte30bc19a2010-05-02 11:19:37 +0200488 uint8_t pdu_type = bgph->pdu_type;
Harald Welte9ba50052010-03-14 15:45:01 +0800489 int rc = 0;
490
Harald Welte58e65c92010-05-13 21:45:23 +0200491 /* If traffic is received on a BVC that is marked as blocked, the
492 * received PDU shall not be accepted and a STATUS PDU (Cause value:
493 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
494 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) {
495 uint16_t bvci = msgb_bvci(msg);
496 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
497 }
498
Harald Welte9ba50052010-03-14 15:45:01 +0800499 switch (pdu_type) {
500 case BSSGP_PDUT_UL_UNITDATA:
501 /* some LLC data from the MS */
Harald Welte25de8112010-05-13 21:26:28 +0200502 rc = bssgp_rx_ul_ud(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800503 break;
504 case BSSGP_PDUT_RA_CAPABILITY:
505 /* BSS requests RA capability or IMSI */
Harald Weltee9686b62010-05-31 18:07:17 +0200506 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RA CAPABILITY UPDATE\n",
507 bctx->bvci);
Harald Welte6b7cf252010-05-13 19:41:31 +0200508 /* FIXME: send GMM_RA_CAPABILITY_UPDATE.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800509 /* FIXME: send RA_CAPA_UPDATE_ACK */
510 break;
511 case BSSGP_PDUT_RADIO_STATUS:
Harald Weltee9686b62010-05-31 18:07:17 +0200512 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx RADIO STATUS\n", bctx->bvci);
Harald Welte9ba50052010-03-14 15:45:01 +0800513 /* BSS informs us of some exception */
Harald Welte6b7cf252010-05-13 19:41:31 +0200514 /* FIXME: send GMM_RADIO_STATUS.ind to GMM */
Harald Welte9ba50052010-03-14 15:45:01 +0800515 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800516 case BSSGP_PDUT_FLOW_CONTROL_BVC:
517 /* BSS informs us of available bandwidth in Gb interface */
Harald Welte25de8112010-05-13 21:26:28 +0200518 rc = bssgp_rx_fc_bvc(msg, tp, bctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800519 break;
520 case BSSGP_PDUT_FLOW_CONTROL_MS:
521 /* BSS informs us of available bandwidth to one MS */
Harald Weltee9686b62010-05-31 18:07:17 +0200522 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx Flow Control MS\n",
523 bctx->bvci);
Harald Welte30bc19a2010-05-02 11:19:37 +0200524 /* FIXME: actually implement flow control */
525 /* FIXME: Send FLOW_CONTROL_MS_ACK */
Harald Welte9ba50052010-03-14 15:45:01 +0800526 break;
Harald Welte9ba50052010-03-14 15:45:01 +0800527 case BSSGP_PDUT_STATUS:
528 /* Some exception has occurred */
Harald Welte6b7cf252010-05-13 19:41:31 +0200529 /* FIXME: send NM_STATUS.ind to NM */
Harald Welte9ba50052010-03-14 15:45:01 +0800530 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
531 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
532 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
533 case BSSGP_PDUT_MODIFY_BSS_PFC:
534 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200535 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x not [yet] "
536 "implemented\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200537 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800538 break;
539 /* those only exist in the SGSN -> BSS direction */
540 case BSSGP_PDUT_DL_UNITDATA:
541 case BSSGP_PDUT_PAGING_PS:
542 case BSSGP_PDUT_PAGING_CS:
543 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
Harald Welte25de8112010-05-13 21:26:28 +0200544 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
545 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Weltee9686b62010-05-31 18:07:17 +0200546 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x only exists "
547 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200548 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
549 rc = -EINVAL;
550 break;
551 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200552 DEBUGP(DBSSGP, "BSSGP BVCI=%u PDU type 0x%02x unknown\n",
553 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200554 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
555 break;
556 }
557
Holger Hans Peter Freytherd30cefa2010-05-23 21:12:15 +0800558 return rc;
Harald Welte25de8112010-05-13 21:26:28 +0200559}
560
561/* Receive a BSSGP PDU from a BSS on a SIGNALLING BVCI */
562static int gprs_bssgp_rx_sign(struct msgb *msg, struct tlv_parsed *tp,
Harald Welte8a521132010-05-17 22:59:29 +0200563 struct bssgp_bvc_ctx *bctx)
Harald Welte25de8112010-05-13 21:26:28 +0200564{
565 struct bssgp_normal_hdr *bgph =
566 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
567 uint8_t pdu_type = bgph->pdu_type;
568 int rc = 0;
569 uint16_t ns_bvci = msgb_bvci(msg);
570 uint16_t bvci;
571
572 switch (bgph->pdu_type) {
573 case BSSGP_PDUT_SUSPEND:
574 /* MS wants to suspend */
575 rc = bssgp_rx_suspend(msg, tp, bctx);
576 break;
577 case BSSGP_PDUT_RESUME:
578 /* MS wants to resume */
579 rc = bssgp_rx_resume(msg, tp, bctx);
580 break;
581 case BSSGP_PDUT_FLUSH_LL_ACK:
582 /* BSS informs us it has performed LL FLUSH */
Harald Welte086fe322011-08-19 16:45:19 +0200583 DEBUGP(DBSSGP, "BSSGP Rx BVCI=%u FLUSH LL ACK\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200584 /* FIXME: send NM_FLUSH_LL.res to NM */
585 break;
586 case BSSGP_PDUT_LLC_DISCARD:
587 /* BSS informs that some LLC PDU's have been discarded */
Harald Weltee9686b62010-05-31 18:07:17 +0200588 rc = bssgp_rx_llc_disc(msg, tp, bctx);
Harald Welte25de8112010-05-13 21:26:28 +0200589 break;
590 case BSSGP_PDUT_BVC_BLOCK:
591 /* BSS tells us that BVC shall be blocked */
Harald Welte25de8112010-05-13 21:26:28 +0200592 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200593 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
594 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-BLOCK "
595 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200596 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200597 }
Harald Welte2677ea52010-05-31 17:16:36 +0200598 rc = bssgp_rx_bvc_block(msg, tp);
Harald Welte25de8112010-05-13 21:26:28 +0200599 break;
600 case BSSGP_PDUT_BVC_UNBLOCK:
601 /* BSS tells us that BVC shall be unblocked */
Harald Weltee9686b62010-05-31 18:07:17 +0200602 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI)) {
603 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-UNBLOCK "
604 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200605 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200606 }
Harald Welte25de8112010-05-13 21:26:28 +0200607 rc = bssgp_rx_bvc_unblock(msg, tp);
608 break;
609 case BSSGP_PDUT_BVC_RESET:
610 /* BSS tells us that BVC init is required */
Harald Welte25de8112010-05-13 21:26:28 +0200611 if (!TLVP_PRESENT(tp, BSSGP_IE_BVCI) ||
Harald Weltee9686b62010-05-31 18:07:17 +0200612 !TLVP_PRESENT(tp, BSSGP_IE_CAUSE)) {
613 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Rx BVC-RESET "
614 "missing mandatory IE\n");
Harald Welte25de8112010-05-13 21:26:28 +0200615 goto err_mand_ie;
Harald Weltee9686b62010-05-31 18:07:17 +0200616 }
Harald Welte25de8112010-05-13 21:26:28 +0200617 rc = bssgp_rx_bvc_reset(msg, tp, ns_bvci);
618 break;
619 case BSSGP_PDUT_STATUS:
620 /* Some exception has occurred */
Harald Weltee9686b62010-05-31 18:07:17 +0200621 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx BVC STATUS\n", bctx->bvci);
Harald Welte25de8112010-05-13 21:26:28 +0200622 /* FIXME: send NM_STATUS.ind to NM */
623 break;
624 /* those only exist in the SGSN -> BSS direction */
625 case BSSGP_PDUT_PAGING_PS:
626 case BSSGP_PDUT_PAGING_CS:
Harald Welte9ba50052010-03-14 15:45:01 +0800627 case BSSGP_PDUT_SUSPEND_ACK:
628 case BSSGP_PDUT_SUSPEND_NACK:
629 case BSSGP_PDUT_RESUME_ACK:
630 case BSSGP_PDUT_RESUME_NACK:
Harald Welte6b7cf252010-05-13 19:41:31 +0200631 case BSSGP_PDUT_FLUSH_LL:
Harald Welte9ba50052010-03-14 15:45:01 +0800632 case BSSGP_PDUT_BVC_BLOCK_ACK:
633 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
634 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Weltee9686b62010-05-31 18:07:17 +0200635 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x only exists "
636 "in DL\n", bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200637 bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800638 rc = -EINVAL;
639 break;
640 default:
Harald Weltee9686b62010-05-31 18:07:17 +0200641 DEBUGP(DBSSGP, "BSSGP BVCI=%u Rx PDU type 0x%02x unknown\n",
642 bctx->bvci, pdu_type);
Harald Welte25de8112010-05-13 21:26:28 +0200643 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800644 break;
645 }
646
647 return rc;
648err_mand_ie:
649 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
650}
651
Harald Welte25de8112010-05-13 21:26:28 +0200652/* We expect msgb_bssgph() to point to the BSSGP header */
653int gprs_bssgp_rcvmsg(struct msgb *msg)
654{
655 struct bssgp_normal_hdr *bgph =
656 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
657 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
658 struct tlv_parsed tp;
Harald Welte8a521132010-05-17 22:59:29 +0200659 struct bssgp_bvc_ctx *bctx;
Harald Welte25de8112010-05-13 21:26:28 +0200660 uint8_t pdu_type = bgph->pdu_type;
661 uint16_t ns_bvci = msgb_bvci(msg);
662 int data_len;
663 int rc = 0;
664
665 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
666
667 /* UNITDATA BSSGP headers have TLLI in front */
668 if (pdu_type != BSSGP_PDUT_UL_UNITDATA &&
669 pdu_type != BSSGP_PDUT_DL_UNITDATA) {
670 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
671 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
672 } else {
673 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
674 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
675 }
676
677 /* look-up or create the BTS context for this BVC */
678 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
679 /* Only a RESET PDU can create a new BVC context */
680 if (!bctx && pdu_type != BSSGP_PDUT_BVC_RESET) {
681 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU "
682 "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci,
683 pdu_type);
684 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
685 }
686
Harald Welte16c8dbb2010-05-17 23:30:01 +0200687 if (bctx) {
Harald Welte4e5721d2010-05-17 23:41:43 +0200688 log_set_context(BSC_CTX_BVC, bctx);
Harald Welte16c8dbb2010-05-17 23:30:01 +0200689 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
690 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN],
691 msgb_bssgp_len(msg));
692 }
693
Harald Welte61c07842010-05-18 11:57:08 +0200694 if (ns_bvci == BVCI_SIGNALLING)
Harald Welte25de8112010-05-13 21:26:28 +0200695 rc = gprs_bssgp_rx_sign(msg, &tp, bctx);
Harald Welte61c07842010-05-18 11:57:08 +0200696 else if (ns_bvci == BVCI_PTM)
Harald Welte25de8112010-05-13 21:26:28 +0200697 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
698 else
699 rc = gprs_bssgp_rx_ptp(msg, &tp, bctx);
700
701 return rc;
702}
703
Harald Welte6752fa42010-05-02 09:23:16 +0200704/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
Harald Welte30bc19a2010-05-02 11:19:37 +0200705 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
Harald Welte2f946832010-05-31 22:12:30 +0200706int gprs_bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
Harald Welte9ba50052010-03-14 15:45:01 +0800707{
Harald Welte8a521132010-05-17 22:59:29 +0200708 struct bssgp_bvc_ctx *bctx;
Harald Welte9ba50052010-03-14 15:45:01 +0800709 struct bssgp_ud_hdr *budh;
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200710 uint8_t llc_pdu_tlv_hdr_len = 2;
711 uint8_t *llc_pdu_tlv, *qos_profile;
712 uint16_t pdu_lifetime = 1000; /* centi-seconds */
Harald Welte02f73252010-06-01 11:53:01 +0200713 uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200714 uint16_t msg_len = msg->len;
Harald Welte30bc19a2010-05-02 11:19:37 +0200715 uint16_t bvci = msgb_bvci(msg);
716 uint16_t nsei = msgb_nsei(msg);
Harald Welte2f946832010-05-31 22:12:30 +0200717 uint16_t drx_params;
Harald Welte9ba50052010-03-14 15:45:01 +0800718
Harald Welte30bc19a2010-05-02 11:19:37 +0200719 /* Identifiers from UP: TLLI, BVCI, NSEI (all in msgb->cb) */
Harald Welte61c07842010-05-18 11:57:08 +0200720 if (bvci <= BVCI_PTM ) {
Harald Welteb8a6a832010-05-11 05:54:22 +0200721 LOGP(DBSSGP, LOGL_ERROR, "Cannot send DL-UD to BVCI %u\n",
Harald Welte30bc19a2010-05-02 11:19:37 +0200722 bvci);
723 return -EINVAL;
724 }
725
726 bctx = btsctx_by_bvci_nsei(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200727 if (!bctx) {
728 /* FIXME: don't simply create missing context, but reject message */
Harald Welte30bc19a2010-05-02 11:19:37 +0200729 bctx = btsctx_alloc(bvci, nsei);
Harald Welte25de8112010-05-13 21:26:28 +0200730 }
Harald Welte9ba50052010-03-14 15:45:01 +0800731
732 if (msg->len > TVLV_MAX_ONEBYTE)
733 llc_pdu_tlv_hdr_len += 1;
734
735 /* prepend the tag and length of the LLC-PDU TLV */
736 llc_pdu_tlv = msgb_push(msg, llc_pdu_tlv_hdr_len);
737 llc_pdu_tlv[0] = BSSGP_IE_LLC_PDU;
738 if (llc_pdu_tlv_hdr_len > 2) {
739 llc_pdu_tlv[1] = msg_len >> 8;
740 llc_pdu_tlv[2] = msg_len & 0xff;
741 } else {
Sylvain Munautb00d1ad2010-06-09 21:13:13 +0200742 llc_pdu_tlv[1] = msg_len & 0x7f;
Harald Welte9ba50052010-03-14 15:45:01 +0800743 llc_pdu_tlv[1] |= 0x80;
744 }
745
Harald Welte2f946832010-05-31 22:12:30 +0200746 /* FIXME: optional elements: Alignment, UTRAN CCO, LSA, PFI */
747
748 if (mmctx) {
749 /* Old TLLI to help BSS map from old->new */
750#if 0
751 if (mmctx->tlli_old)
752 msgb_tvlv_push(msg, BSSGP_IE_TLLI, 4, htonl(*tlli_old));
753#endif
754
755 /* IMSI */
756 if (strlen(mmctx->imsi)) {
757 uint8_t mi[10];
758 int imsi_len = gsm48_generate_mid_from_imsi(mi, mmctx->imsi);
759 if (imsi_len > 2)
760 msgb_tvlv_push(msg, BSSGP_IE_IMSI,
761 imsi_len-2, mi+2);
762 }
763
764 /* DRX parameters */
765 drx_params = htons(mmctx->drx_parms);
766 msgb_tvlv_push(msg, BSSGP_IE_DRX_PARAMS, 2,
767 (uint8_t *) &drx_params);
768
769 /* FIXME: Priority */
770
771 /* MS Radio Access Capability */
772 if (mmctx->ms_radio_access_capa.len)
773 msgb_tvlv_push(msg, BSSGP_IE_MS_RADIO_ACCESS_CAP,
774 mmctx->ms_radio_access_capa.len,
775 mmctx->ms_radio_access_capa.buf);
776 }
Harald Welte9ba50052010-03-14 15:45:01 +0800777
778 /* prepend the pdu lifetime */
779 pdu_lifetime = htons(pdu_lifetime);
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200780 msgb_tvlv_push(msg, BSSGP_IE_PDU_LIFETIME, 2, (uint8_t *)&pdu_lifetime);
Harald Welte9ba50052010-03-14 15:45:01 +0800781
782 /* prepend the QoS profile, TLLI and pdu type */
783 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
784 memcpy(budh->qos_profile, qos_profile_default, sizeof(qos_profile_default));
Harald Welte510c3922010-04-30 16:33:12 +0200785 budh->tlli = htonl(msgb_tlli(msg));
Harald Welte9ba50052010-03-14 15:45:01 +0800786 budh->pdu_type = BSSGP_PDUT_DL_UNITDATA;
787
Harald Welte16c8dbb2010-05-17 23:30:01 +0200788 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
789 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
790
Harald Welte30bc19a2010-05-02 11:19:37 +0200791 /* Identifiers down: BVCI, NSEI (in msgb->cb) */
Harald Welte24a655f2010-04-30 19:54:29 +0200792
793 return gprs_ns_sendmsg(bssgp_nsi, msg);
Harald Welte9ba50052010-03-14 15:45:01 +0800794}
Harald Welte68b4f032010-06-09 16:22:28 +0200795
796/* Send a single GMM-PAGING.req to a given NSEI/NS-BVCI */
797int gprs_bssgp_tx_paging(uint16_t nsei, uint16_t ns_bvci,
798 struct bssgp_paging_info *pinfo)
799{
800 struct msgb *msg = bssgp_msgb_alloc();
801 struct bssgp_normal_hdr *bgph =
802 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
803 uint16_t drx_params = htons(pinfo->drx_params);
804 uint8_t mi[10];
805 int imsi_len = gsm48_generate_mid_from_imsi(mi, pinfo->imsi);
806 uint8_t ra[6];
807
808 if (imsi_len < 2)
809 return -EINVAL;
810
811 msgb_nsei(msg) = nsei;
812 msgb_bvci(msg) = ns_bvci;
813
814 if (pinfo->mode == BSSGP_PAGING_PS)
815 bgph->pdu_type = BSSGP_PDUT_PAGING_PS;
816 else
817 bgph->pdu_type = BSSGP_PDUT_PAGING_CS;
818 /* IMSI */
819 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
820 /* DRX Parameters */
821 msgb_tvlv_put(msg, BSSGP_IE_DRX_PARAMS, 2,
822 (uint8_t *) &drx_params);
823 /* Scope */
824 switch (pinfo->scope) {
825 case BSSGP_PAGING_BSS_AREA:
826 {
827 uint8_t null = 0;
828 msgb_tvlv_put(msg, BSSGP_IE_BSS_AREA_ID, 1, &null);
829 }
830 break;
831 case BSSGP_PAGING_LOCATION_AREA:
832 gsm48_construct_ra(ra, &pinfo->raid);
833 msgb_tvlv_put(msg, BSSGP_IE_LOCATION_AREA, 4, ra);
834 break;
835 case BSSGP_PAGING_ROUTEING_AREA:
836 gsm48_construct_ra(ra, &pinfo->raid);
837 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
838 break;
839 case BSSGP_PAGING_BVCI:
840 {
841 uint16_t bvci = htons(pinfo->bvci);
842 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *)&bvci);
843 }
844 break;
845 }
846 /* QoS profile mandatory for PS */
847 if (pinfo->mode == BSSGP_PAGING_PS)
848 msgb_tvlv_put(msg, BSSGP_IE_QOS_PROFILE, 3, pinfo->qos);
849
850 /* Optional (P-)TMSI */
851 if (pinfo->ptmsi) {
852 uint32_t ptmsi = htonl(*pinfo->ptmsi);
853 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *) &ptmsi);
854 }
855
856 return gprs_ns_sendmsg(bssgp_nsi, msg);
857}