blob: 962bf2e8dc1d3b7e985f559906c4259dfe854a24 [file] [log] [blame]
Harald Welte85fc3142011-11-25 08:58:40 +01001/* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */
2
Harald Welte605ac5d2012-06-16 16:09:52 +08003/* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welte85fc3142011-11-25 08:58:40 +01004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +01008 * 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
Harald Welte85fc3142011-11-25 08:58:40 +010010 * (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 Welte7fa89c22014-10-26 20:33:09 +010015 * GNU General Public License for more details.
Harald Welte85fc3142011-11-25 08:58:40 +010016 *
Harald Welte7fa89c22014-10-26 20:33:09 +010017 * You should have received a copy of the GNU General Public License
Harald Welte85fc3142011-11-25 08:58:40 +010018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <errno.h>
23#include <stdint.h>
24
25#include <netinet/in.h>
26
27#include <osmocom/core/msgb.h>
28#include <osmocom/core/rate_ctr.h>
29#include <osmocom/gsm/tlv.h>
30#include <osmocom/core/talloc.h>
Harald Welte73952e32012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte30fabdf2012-09-10 08:54:35 +020032#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte73952e32012-06-16 14:59:56 +080033#include <osmocom/gprs/gprs_ns.h>
Harald Welte85fc3142011-11-25 08:58:40 +010034
Harald Weltecca49632012-06-16 17:45:59 +080035#include "common_vty.h"
Harald Welte85fc3142011-11-25 08:58:40 +010036
37uint8_t *bssgp_msgb_tlli_put(struct msgb *msg, uint32_t tlli)
38{
39 uint32_t _tlli = htonl(tlli);
40 return msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
41}
42
43/*! \brief GMM-SUSPEND.req (Chapter 10.3.6) */
44int bssgp_tx_suspend(uint16_t nsei, uint32_t tlli,
45 const struct gprs_ra_id *ra_id)
46{
47 struct msgb *msg = bssgp_msgb_alloc();
48 struct bssgp_normal_hdr *bgph =
49 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
50 uint8_t ra[6];
51
52 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx SUSPEND (TLLI=0x%04x)\n",
53 tlli);
54 msgb_nsei(msg) = nsei;
55 msgb_bvci(msg) = 0; /* Signalling */
56 bgph->pdu_type = BSSGP_PDUT_SUSPEND;
57
58 bssgp_msgb_tlli_put(msg, tlli);
59
60 gsm48_construct_ra(ra, ra_id);
61 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
62
63 return gprs_ns_sendmsg(bssgp_nsi, msg);
64}
65
66/*! \brief GMM-RESUME.req (Chapter 10.3.9) */
67int bssgp_tx_resume(uint16_t nsei, uint32_t tlli,
68 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
69{
70 struct msgb *msg = bssgp_msgb_alloc();
71 struct bssgp_normal_hdr *bgph =
72 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
73 uint8_t ra[6];
74
75 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx RESUME (TLLI=0x%04x)\n",
76 tlli);
77 msgb_nsei(msg) = nsei;
78 msgb_bvci(msg) = 0; /* Signalling */
79 bgph->pdu_type = BSSGP_PDUT_RESUME;
80
81 bssgp_msgb_tlli_put(msg, tlli);
82
83 gsm48_construct_ra(ra, ra_id);
84 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
85
86 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
87
88 return gprs_ns_sendmsg(bssgp_nsi, msg);
89}
90
91/*! \brief Transmit RA-CAPABILITY-UPDATE (10.3.3) */
92int bssgp_tx_ra_capa_upd(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag)
93{
94 struct msgb *msg = bssgp_msgb_alloc();
95 struct bssgp_normal_hdr *bgph =
96 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
97
98 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RA-CAPA-UPD (TLLI=0x%04x)\n",
99 bctx->bvci, tlli);
100
101 /* set NSEI and BVCI in msgb cb */
102 msgb_nsei(msg) = bctx->nsei;
103 msgb_bvci(msg) = bctx->bvci;
104
105 bgph->pdu_type = BSSGP_PDUT_RA_CAPA_UDPATE;
106 bssgp_msgb_tlli_put(msg, tlli);
107
108 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
109
110 return gprs_ns_sendmsg(bssgp_nsi, msg);
111}
112
113/* first common part of RADIO-STATUS */
114static struct msgb *common_tx_radio_status(struct bssgp_bvc_ctx *bctx)
115{
116 struct msgb *msg = bssgp_msgb_alloc();
117 struct bssgp_normal_hdr *bgph =
118 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
119
120 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RADIO-STATUS ",
121 bctx->bvci);
122
123 /* set NSEI and BVCI in msgb cb */
124 msgb_nsei(msg) = bctx->nsei;
125 msgb_bvci(msg) = bctx->bvci;
126
127 bgph->pdu_type = BSSGP_PDUT_RADIO_STATUS;
128
129 return msg;
130}
131
132/* second common part of RADIO-STATUS */
133static int common_tx_radio_status2(struct msgb *msg, uint8_t cause)
134{
135 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
136 LOGPC(DBSSGP, LOGL_NOTICE, "CAUSE=%u\n", cause);
137
138 return gprs_ns_sendmsg(bssgp_nsi, msg);
139}
140
141/*! \brief Transmit RADIO-STATUS for TLLI (10.3.5) */
142int bssgp_tx_radio_status_tlli(struct bssgp_bvc_ctx *bctx, uint8_t cause,
143 uint32_t tlli)
144{
145 struct msgb *msg = common_tx_radio_status(bctx);
146
147 if (!msg)
148 return -ENOMEM;
149 bssgp_msgb_tlli_put(msg, tlli);
150 LOGPC(DBSSGP, LOGL_NOTICE, "TLLI=0x%08x ", tlli);
151
152 return common_tx_radio_status2(msg, cause);
153}
154
155/*! \brief Transmit RADIO-STATUS for TMSI (10.3.5) */
156int bssgp_tx_radio_status_tmsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
157 uint32_t tmsi)
158{
159 struct msgb *msg = common_tx_radio_status(bctx);
160 uint32_t _tmsi = htonl(tmsi);
161
162 if (!msg)
163 return -ENOMEM;
164 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *)&_tmsi);
165 LOGPC(DBSSGP, LOGL_NOTICE, "TMSI=0x%08x ", tmsi);
166
167 return common_tx_radio_status2(msg, cause);
168}
169
170/*! \brief Transmit RADIO-STATUS for IMSI (10.3.5) */
171int bssgp_tx_radio_status_imsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
172 const char *imsi)
173{
174 struct msgb *msg = common_tx_radio_status(bctx);
175 uint8_t mi[10];
176 int imsi_len = gsm48_generate_mid_from_imsi(mi, imsi);
177
178 if (!msg)
179 return -ENOMEM;
180
181 /* strip the MI type and length values (2 bytes) */
182 if (imsi_len > 2)
183 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
184 LOGPC(DBSSGP, LOGL_NOTICE, "IMSI=%s ", imsi);
185
186 return common_tx_radio_status2(msg, cause);
187}
188
189/*! \brief Transmit FLUSH-LL-ACK (Chapter 10.4.2) */
190int bssgp_tx_flush_ll_ack(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
191 uint8_t action, uint16_t bvci_new,
192 uint32_t num_octets)
193{
194 struct msgb *msg = bssgp_msgb_alloc();
195 struct bssgp_normal_hdr *bgph =
196 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
197 uint16_t _bvci_new = htons(bvci_new);
198 uint32_t _oct_aff = htonl(num_octets & 0xFFFFFF);
199
200 msgb_nsei(msg) = bctx->nsei;
201 msgb_bvci(msg) = 0; /* Signalling */
202 bgph->pdu_type = BSSGP_PDUT_FLUSH_LL_ACK;
203
204 bssgp_msgb_tlli_put(msg, tlli);
205 msgb_tvlv_put(msg, BSSGP_IE_FLUSH_ACTION, 1, &action);
206 if (action == 1) /* transferred */
207 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci_new);
208 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, (uint8_t *) &_oct_aff);
209
210 return gprs_ns_sendmsg(bssgp_nsi, msg);
211}
212
213/*! \brief Transmit LLC-DISCARDED (Chapter 10.4.3) */
214int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
215 uint8_t num_frames, uint32_t num_octets)
216{
217 struct msgb *msg = bssgp_msgb_alloc();
218 struct bssgp_normal_hdr *bgph =
219 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
220 uint16_t _bvci = htons(bctx->bvci);
221 uint32_t _oct_aff = htonl(num_octets & 0xFFFFFF);
222
223 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx LLC-DISCARDED "
224 "TLLI=0x%04x, FRAMES=%u, OCTETS=%u\n", bctx->bvci, tlli,
225 num_frames, num_octets);
226 msgb_nsei(msg) = bctx->nsei;
227 msgb_bvci(msg) = 0; /* Signalling */
228 bgph->pdu_type = BSSGP_PDUT_LLC_DISCARD;
229
230 bssgp_msgb_tlli_put(msg, tlli);
231
232 msgb_tvlv_put(msg, BSSGP_IE_LLC_FRAMES_DISCARDED, 1, &num_frames);
233 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
Andreas Eversbergaa5d0e82012-07-21 13:33:39 +0200234 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, ((uint8_t *) &_oct_aff) + 1);
Harald Welte85fc3142011-11-25 08:58:40 +0100235
236 return gprs_ns_sendmsg(bssgp_nsi, msg);
237}
238
239/*! \brief Transmit a BVC-BLOCK message (Chapter 10.4.8) */
240int bssgp_tx_bvc_block(struct bssgp_bvc_ctx *bctx, uint8_t cause)
241{
242 struct msgb *msg = bssgp_msgb_alloc();
243 struct bssgp_normal_hdr *bgph =
244 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
245 uint16_t _bvci = htons(bctx->bvci);
246
247 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK "
248 "CAUSE=%u\n", bctx->bvci, cause);
249
250 msgb_nsei(msg) = bctx->nsei;
251 msgb_bvci(msg) = 0; /* Signalling */
252 bgph->pdu_type = BSSGP_PDUT_BVC_BLOCK;
253
254 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
255 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
256
257 return gprs_ns_sendmsg(bssgp_nsi, msg);
258}
259
260/*! \brief Transmit a BVC-UNBLOCK message (Chapter 10.4.10) */
261int bssgp_tx_bvc_unblock(struct bssgp_bvc_ctx *bctx)
262{
263 struct msgb *msg = bssgp_msgb_alloc();
264 struct bssgp_normal_hdr *bgph =
265 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
266 uint16_t _bvci = htons(bctx->bvci);
267
268 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK\n", bctx->bvci);
269
270 msgb_nsei(msg) = bctx->nsei;
271 msgb_bvci(msg) = 0; /* Signalling */
272 bgph->pdu_type = BSSGP_PDUT_BVC_UNBLOCK;
273
274 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
275
276 return gprs_ns_sendmsg(bssgp_nsi, msg);
277}
278
279/*! \brief Transmit a BVC-RESET message (Chapter 10.4.12) */
280int bssgp_tx_bvc_reset(struct bssgp_bvc_ctx *bctx, uint16_t bvci, uint8_t cause)
281{
282 struct msgb *msg = bssgp_msgb_alloc();
283 struct bssgp_normal_hdr *bgph =
284 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
285 uint16_t _bvci = htons(bvci);
286
287 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
288 "CAUSE=%u\n", bvci, cause);
289
290 msgb_nsei(msg) = bctx->nsei;
291 msgb_bvci(msg) = 0; /* Signalling */
292 bgph->pdu_type = BSSGP_PDUT_BVC_RESET;
293
294 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
295 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
296 if (bvci != BVCI_PTM) {
297 uint8_t bssgp_cid[8];
298 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
299 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
300 }
301 /* Optional: Feature Bitmap */
302
303 return gprs_ns_sendmsg(bssgp_nsi, msg);
304}
305
Harald Weltee92866b2012-09-10 08:56:04 +0200306/*! \brief Transmit a FLOW_CONTROL-BVC (Chapter 10.4.4)
307 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200308 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200309 * \param[in] bucket_size Maximum bucket size in octets
310 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
311 * \param[in] bmax_default_ms Maximum bucket size default for MS
312 * \param[in] r_default_ms Bucket leak rate default for MS in octets/sec
313 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
314 * \param[in] queue_delay_ms Average queuing delay in milliseconds
315 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200316int bssgp_tx_fc_bvc(struct bssgp_bvc_ctx *bctx, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200317 uint32_t bucket_size, uint32_t bucket_leak_rate,
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +0200318 uint32_t bmax_default_ms, uint32_t r_default_ms,
Harald Weltee92866b2012-09-10 08:56:04 +0200319 uint8_t *bucket_full_ratio, uint32_t *queue_delay_ms)
320{
321 struct msgb *msg;
322 struct bssgp_normal_hdr *bgph;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200323 uint16_t e_bucket_size, e_leak_rate, e_bmax_default_ms, e_r_default_ms;
Harald Weltee92866b2012-09-10 08:56:04 +0200324 uint16_t e_queue_delay = 0; /* to make gcc happy */
325
326 if ((bucket_size / 100) > 0xffff)
327 return -EINVAL;
Jacob Erlbeck580af4b2015-04-29 15:00:30 +0200328 e_bucket_size = htons(bucket_size / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200329
330 if ((bucket_leak_rate * 8 / 100) > 0xffff)
331 return -EINVAL;
Jacob Erlbeck580af4b2015-04-29 15:00:30 +0200332 e_leak_rate = htons((bucket_leak_rate * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200333
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200334 if ((bmax_default_ms / 100) > 0xffff)
335 return -EINVAL;
Jacob Erlbeck580af4b2015-04-29 15:00:30 +0200336 e_bmax_default_ms = htons(bmax_default_ms / 100);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200337
Harald Weltee92866b2012-09-10 08:56:04 +0200338 if ((r_default_ms * 8 / 100) > 0xffff)
339 return -EINVAL;
Jacob Erlbeck580af4b2015-04-29 15:00:30 +0200340 e_r_default_ms = htons((r_default_ms * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200341
342 if (queue_delay_ms) {
343 if ((*queue_delay_ms / 10) > 60000)
344 return -EINVAL;
345 else if (*queue_delay_ms == 0xFFFFFFFF)
346 e_queue_delay = 0xFFFF;
347 else
Jacob Erlbeck580af4b2015-04-29 15:00:30 +0200348 e_queue_delay = htons(*queue_delay_ms / 10);
Harald Weltee92866b2012-09-10 08:56:04 +0200349 }
350
351 msg = bssgp_msgb_alloc();
352 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
353 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200354 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200355 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC;
356
357 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
358 msgb_tvlv_put(msg, BSSGP_IE_BVC_BUCKET_SIZE,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200359 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
Harald Weltee92866b2012-09-10 08:56:04 +0200360 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
361 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
362 msgb_tvlv_put(msg, BSSGP_IE_BMAX_DEFAULT_MS,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200363 sizeof(e_bmax_default_ms),
364 (uint8_t *) &e_bmax_default_ms);
Harald Weltee92866b2012-09-10 08:56:04 +0200365 msgb_tvlv_put(msg, BSSGP_IE_R_DEFAULT_MS,
366 sizeof(e_r_default_ms), (uint8_t *) &e_r_default_ms);
367 if (bucket_full_ratio)
368 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
369 1, bucket_full_ratio);
370 if (queue_delay_ms)
371 msgb_tvlv_put(msg, BSSGP_IE_BVC_MEASUREMENT,
372 sizeof(e_queue_delay),
373 (uint8_t *) &e_queue_delay);
374
375 return gprs_ns_sendmsg(bssgp_nsi, msg);
376}
377
378/*! \brief Transmit a FLOW_CONTROL-MS (Chapter 10.4.6)
379 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200380 * \param[in] tlli TLLI to identify MS
381 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200382 * \param[in] ms_bucket_size Maximum bucket size in octets
383 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
384 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
385 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200386int bssgp_tx_fc_ms(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200387 uint32_t ms_bucket_size, uint32_t bucket_leak_rate,
388 uint8_t *bucket_full_ratio)
389{
390 struct msgb *msg;
391 struct bssgp_normal_hdr *bgph;
392 uint16_t e_bucket_size, e_leak_rate;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200393 uint32_t e_tlli;
Harald Weltee92866b2012-09-10 08:56:04 +0200394
395 if ((ms_bucket_size / 100) > 0xffff)
396 return -EINVAL;
397 e_bucket_size = ms_bucket_size / 100;
398
399 if ((bucket_leak_rate * 8 / 100) > 0xffff)
400 return -EINVAL;
401 e_leak_rate = (bucket_leak_rate * 8) / 100;
402
403 msg = bssgp_msgb_alloc();
404 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
405 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200406 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200407 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_MS;
408
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200409 e_tlli = htonl(tlli);
410 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(e_tlli), (uint8_t *)&e_tlli);
Harald Weltee92866b2012-09-10 08:56:04 +0200411 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
412 msgb_tvlv_put(msg, BSSGP_IE_MS_BUCKET_SIZE,
413 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
414 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
415 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
416 if (bucket_full_ratio)
417 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
418 1, bucket_full_ratio);
419
420 return gprs_ns_sendmsg(bssgp_nsi, msg);
421}
Harald Welte85fc3142011-11-25 08:58:40 +0100422
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200423/*! \brief RL-UL-UNITDATA.req (Chapter 10.2.2)
424 * \param[in] bctx BVC Context
425 * \param[in] tlli TLLI to identify MS
426 * \param[in] qos_profile Pointer to three octests of QoS profile
427 * \param[in] llc_pdu msgb pointer containing UL Unitdata IE payload
428 */
Harald Welte85fc3142011-11-25 08:58:40 +0100429int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
430 const uint8_t *qos_profile, struct msgb *llc_pdu)
431{
432 struct msgb *msg = llc_pdu;
433 uint8_t bssgp_cid[8];
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200434 uint8_t bssgp_align[3] = {0, 0, 0};
Harald Welte85fc3142011-11-25 08:58:40 +0100435 struct bssgp_ud_hdr *budh;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200436 int align = sizeof(*budh);
Harald Welte85fc3142011-11-25 08:58:40 +0100437
438 /* FIXME: Optional LSA Identifier List, PFI */
439
440 /* Cell Identifier */
441 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200442 align += 2; /* add T+L */
443 align += sizeof(bssgp_cid);
444
445 /* First push alignment IE */
446 align += 2; /* add T+L */
447 align = (4 - align) & 3; /* how many octest are required to align? */
448 msgb_tvlv_push(msg, BSSGP_IE_ALIGNMENT, align, bssgp_align);
449
450 /* Push other IEs */
Harald Welte85fc3142011-11-25 08:58:40 +0100451 msgb_tvlv_push(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
452
453 /* User Data Header */
454 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
455 budh->tlli = htonl(tlli);
456 memcpy(budh->qos_profile, qos_profile, 3);
457 budh->pdu_type = BSSGP_PDUT_UL_UNITDATA;
458
459 /* set NSEI and BVCI in msgb cb */
460 msgb_nsei(msg) = bctx->nsei;
461 msgb_bvci(msg) = bctx->bvci;
462
463 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
464 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
465
466 return gprs_ns_sendmsg(bssgp_nsi, msg);
467}
468
469/* Parse a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800470int bssgp_rx_paging(struct bssgp_paging_info *pinfo,
471 struct msgb *msg)
Harald Welte85fc3142011-11-25 08:58:40 +0100472{
473 struct bssgp_normal_hdr *bgph =
474 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
475 struct tlv_parsed tp;
476 uint8_t ra[6];
477 int rc, data_len;
478
479 memset(ra, 0, sizeof(ra));
480
481 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
482 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
483 if (rc < 0)
484 goto err_mand_ie;
485
486 switch (bgph->pdu_type) {
487 case BSSGP_PDUT_PAGING_PS:
488 pinfo->mode = BSSGP_PAGING_PS;
489 break;
490 case BSSGP_PDUT_PAGING_CS:
491 pinfo->mode = BSSGP_PAGING_CS;
492 break;
493 default:
494 return -EINVAL;
495 }
496
497 /* IMSI */
498 if (!TLVP_PRESENT(&tp, BSSGP_IE_IMSI))
499 goto err_mand_ie;
500 if (!pinfo->imsi)
501 pinfo->imsi = talloc_zero_size(pinfo, 16);
502 gsm48_mi_to_string(pinfo->imsi, sizeof(pinfo->imsi),
503 TLVP_VAL(&tp, BSSGP_IE_IMSI),
504 TLVP_LEN(&tp, BSSGP_IE_IMSI));
505
506 /* DRX Parameters */
507 if (!TLVP_PRESENT(&tp, BSSGP_IE_DRX_PARAMS))
508 goto err_mand_ie;
509 pinfo->drx_params = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_DRX_PARAMS));
510
511 /* Scope */
512 if (TLVP_PRESENT(&tp, BSSGP_IE_BSS_AREA_ID)) {
513 pinfo->scope = BSSGP_PAGING_BSS_AREA;
514 } else if (TLVP_PRESENT(&tp, BSSGP_IE_LOCATION_AREA)) {
515 pinfo->scope = BSSGP_PAGING_LOCATION_AREA;
516 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_LOCATION_AREA),
517 TLVP_LEN(&tp, BSSGP_IE_LOCATION_AREA));
518 gsm48_parse_ra(&pinfo->raid, ra);
519 } else if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) {
520 pinfo->scope = BSSGP_PAGING_ROUTEING_AREA;
521 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
522 TLVP_LEN(&tp, BSSGP_IE_ROUTEING_AREA));
523 gsm48_parse_ra(&pinfo->raid, ra);
524 } else if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
525 pinfo->scope = BSSGP_PAGING_BVCI;
526 pinfo->bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
527 } else
528 return -EINVAL;
529
530 /* QoS profile mandatory for PS */
531 if (pinfo->mode == BSSGP_PAGING_PS) {
532 if (!TLVP_PRESENT(&tp, BSSGP_IE_QOS_PROFILE))
533 goto err_cond_ie;
534 if (TLVP_LEN(&tp, BSSGP_IE_QOS_PROFILE) < 3)
535 goto err;
536
537 memcpy(&pinfo->qos, TLVP_VAL(&tp, BSSGP_IE_QOS_PROFILE),
538 3);
539 }
540
541 /* Optional (P-)TMSI */
542 if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI) &&
543 TLVP_LEN(&tp, BSSGP_IE_TMSI) >= 4)
544 if (!pinfo->ptmsi)
545 pinfo->ptmsi = talloc_zero_size(pinfo, sizeof(uint32_t));
546 *(pinfo->ptmsi) = ntohl(*(uint32_t *)
547 TLVP_VAL(&tp, BSSGP_IE_TMSI));
548
549 return 0;
550
551err_mand_ie:
552err_cond_ie:
553err:
554 /* FIXME */
555 return 0;
556}