blob: d9f04c5f3e483d44d4ed58b4ef72a959dbff040e [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_bssgp_bss.c
2 * GPRS BSSGP protocol implementation as per 3GPP TS 08.18. */
3/*
4 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welte85fc3142011-11-25 08:58:40 +01005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +01009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
Harald Welte85fc3142011-11-25 08:58:40 +010011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010016 * GNU General Public License for more details.
Harald Welte85fc3142011-11-25 08:58:40 +010017 *
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * You should have received a copy of the GNU General Public License
Harald Welte85fc3142011-11-25 08:58:40 +010019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <errno.h>
24#include <stdint.h>
25
Harald Welte85fc3142011-11-25 08:58:40 +010026#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020027#include <osmocom/core/byteswap.h>
Harald Welte85fc3142011-11-25 08:58:40 +010028#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
Jacob Erlbeckc1cb75e2015-06-18 13:21:30 +020037#define GSM_IMSI_LENGTH 17
38
Harald Welte85fc3142011-11-25 08:58:40 +010039uint8_t *bssgp_msgb_tlli_put(struct msgb *msg, uint32_t tlli)
40{
Harald Weltebfe62e52017-05-15 12:48:30 +020041 uint32_t _tlli = osmo_htonl(tlli);
Harald Welte85fc3142011-11-25 08:58:40 +010042 return msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
43}
44
Neels Hofmeyr87e45502017-06-20 00:17:59 +020045/*! GMM-SUSPEND.req (Chapter 10.3.6) */
Harald Welte85fc3142011-11-25 08:58:40 +010046int bssgp_tx_suspend(uint16_t nsei, uint32_t tlli,
47 const struct gprs_ra_id *ra_id)
48{
49 struct msgb *msg = bssgp_msgb_alloc();
50 struct bssgp_normal_hdr *bgph =
51 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
52 uint8_t ra[6];
53
54 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx SUSPEND (TLLI=0x%04x)\n",
55 tlli);
56 msgb_nsei(msg) = nsei;
57 msgb_bvci(msg) = 0; /* Signalling */
58 bgph->pdu_type = BSSGP_PDUT_SUSPEND;
59
60 bssgp_msgb_tlli_put(msg, tlli);
61
62 gsm48_construct_ra(ra, ra_id);
63 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
64
65 return gprs_ns_sendmsg(bssgp_nsi, msg);
66}
67
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068/*! GMM-RESUME.req (Chapter 10.3.9) */
Harald Welte85fc3142011-11-25 08:58:40 +010069int bssgp_tx_resume(uint16_t nsei, uint32_t tlli,
70 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
71{
72 struct msgb *msg = bssgp_msgb_alloc();
73 struct bssgp_normal_hdr *bgph =
74 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
75 uint8_t ra[6];
76
77 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx RESUME (TLLI=0x%04x)\n",
78 tlli);
79 msgb_nsei(msg) = nsei;
80 msgb_bvci(msg) = 0; /* Signalling */
81 bgph->pdu_type = BSSGP_PDUT_RESUME;
82
83 bssgp_msgb_tlli_put(msg, tlli);
84
85 gsm48_construct_ra(ra, ra_id);
86 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
87
88 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
89
90 return gprs_ns_sendmsg(bssgp_nsi, msg);
91}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! Transmit RA-CAPABILITY-UPDATE (10.3.3) */
Harald Welte85fc3142011-11-25 08:58:40 +010094int bssgp_tx_ra_capa_upd(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag)
95{
96 struct msgb *msg = bssgp_msgb_alloc();
97 struct bssgp_normal_hdr *bgph =
98 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
99
100 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RA-CAPA-UPD (TLLI=0x%04x)\n",
101 bctx->bvci, tlli);
102
103 /* set NSEI and BVCI in msgb cb */
104 msgb_nsei(msg) = bctx->nsei;
105 msgb_bvci(msg) = bctx->bvci;
106
107 bgph->pdu_type = BSSGP_PDUT_RA_CAPA_UDPATE;
108 bssgp_msgb_tlli_put(msg, tlli);
109
110 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
111
112 return gprs_ns_sendmsg(bssgp_nsi, msg);
113}
114
115/* first common part of RADIO-STATUS */
116static struct msgb *common_tx_radio_status(struct bssgp_bvc_ctx *bctx)
117{
118 struct msgb *msg = bssgp_msgb_alloc();
119 struct bssgp_normal_hdr *bgph =
120 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
121
122 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RADIO-STATUS ",
123 bctx->bvci);
124
125 /* set NSEI and BVCI in msgb cb */
126 msgb_nsei(msg) = bctx->nsei;
127 msgb_bvci(msg) = bctx->bvci;
128
129 bgph->pdu_type = BSSGP_PDUT_RADIO_STATUS;
130
131 return msg;
132}
133
134/* second common part of RADIO-STATUS */
135static int common_tx_radio_status2(struct msgb *msg, uint8_t cause)
136{
137 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
Max2c34ab42016-03-17 15:42:26 +0100138 LOGPC(DBSSGP, LOGL_NOTICE, "CAUSE=%s\n", bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100139
140 return gprs_ns_sendmsg(bssgp_nsi, msg);
141}
142
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143/*! Transmit RADIO-STATUS for TLLI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100144int bssgp_tx_radio_status_tlli(struct bssgp_bvc_ctx *bctx, uint8_t cause,
145 uint32_t tlli)
146{
147 struct msgb *msg = common_tx_radio_status(bctx);
148
149 if (!msg)
150 return -ENOMEM;
151 bssgp_msgb_tlli_put(msg, tlli);
152 LOGPC(DBSSGP, LOGL_NOTICE, "TLLI=0x%08x ", tlli);
153
154 return common_tx_radio_status2(msg, cause);
155}
156
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157/*! Transmit RADIO-STATUS for TMSI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100158int bssgp_tx_radio_status_tmsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
159 uint32_t tmsi)
160{
161 struct msgb *msg = common_tx_radio_status(bctx);
Harald Weltebfe62e52017-05-15 12:48:30 +0200162 uint32_t _tmsi = osmo_htonl(tmsi);
Harald Welte85fc3142011-11-25 08:58:40 +0100163
164 if (!msg)
165 return -ENOMEM;
166 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *)&_tmsi);
167 LOGPC(DBSSGP, LOGL_NOTICE, "TMSI=0x%08x ", tmsi);
168
169 return common_tx_radio_status2(msg, cause);
170}
171
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200172/*! Transmit RADIO-STATUS for IMSI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100173int bssgp_tx_radio_status_imsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
174 const char *imsi)
175{
176 struct msgb *msg = common_tx_radio_status(bctx);
177 uint8_t mi[10];
178 int imsi_len = gsm48_generate_mid_from_imsi(mi, imsi);
179
180 if (!msg)
181 return -ENOMEM;
182
183 /* strip the MI type and length values (2 bytes) */
184 if (imsi_len > 2)
185 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
186 LOGPC(DBSSGP, LOGL_NOTICE, "IMSI=%s ", imsi);
187
188 return common_tx_radio_status2(msg, cause);
189}
190
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200191/*! Transmit FLUSH-LL-ACK (Chapter 10.4.2) */
Harald Welte85fc3142011-11-25 08:58:40 +0100192int bssgp_tx_flush_ll_ack(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
193 uint8_t action, uint16_t bvci_new,
194 uint32_t num_octets)
195{
196 struct msgb *msg = bssgp_msgb_alloc();
197 struct bssgp_normal_hdr *bgph =
198 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200199 uint16_t _bvci_new = osmo_htons(bvci_new);
200 uint32_t _oct_aff = osmo_htonl(num_octets & 0xFFFFFF);
Harald Welte85fc3142011-11-25 08:58:40 +0100201
202 msgb_nsei(msg) = bctx->nsei;
203 msgb_bvci(msg) = 0; /* Signalling */
204 bgph->pdu_type = BSSGP_PDUT_FLUSH_LL_ACK;
205
206 bssgp_msgb_tlli_put(msg, tlli);
207 msgb_tvlv_put(msg, BSSGP_IE_FLUSH_ACTION, 1, &action);
208 if (action == 1) /* transferred */
209 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci_new);
210 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, (uint8_t *) &_oct_aff);
211
212 return gprs_ns_sendmsg(bssgp_nsi, msg);
213}
214
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200215/*! Transmit LLC-DISCARDED (Chapter 10.4.3) */
Harald Welte85fc3142011-11-25 08:58:40 +0100216int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
217 uint8_t num_frames, uint32_t num_octets)
218{
219 struct msgb *msg = bssgp_msgb_alloc();
220 struct bssgp_normal_hdr *bgph =
221 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200222 uint16_t _bvci = osmo_htons(bctx->bvci);
223 uint32_t _oct_aff = osmo_htonl(num_octets & 0xFFFFFF);
Harald Welte85fc3142011-11-25 08:58:40 +0100224
225 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx LLC-DISCARDED "
226 "TLLI=0x%04x, FRAMES=%u, OCTETS=%u\n", bctx->bvci, tlli,
227 num_frames, num_octets);
228 msgb_nsei(msg) = bctx->nsei;
229 msgb_bvci(msg) = 0; /* Signalling */
230 bgph->pdu_type = BSSGP_PDUT_LLC_DISCARD;
231
232 bssgp_msgb_tlli_put(msg, tlli);
233
234 msgb_tvlv_put(msg, BSSGP_IE_LLC_FRAMES_DISCARDED, 1, &num_frames);
235 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
Andreas Eversbergaa5d0e82012-07-21 13:33:39 +0200236 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, ((uint8_t *) &_oct_aff) + 1);
Harald Welte85fc3142011-11-25 08:58:40 +0100237
238 return gprs_ns_sendmsg(bssgp_nsi, msg);
239}
240
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200241/*! Transmit a BVC-BLOCK message (Chapter 10.4.8) */
Harald Welte85fc3142011-11-25 08:58:40 +0100242int bssgp_tx_bvc_block(struct bssgp_bvc_ctx *bctx, uint8_t cause)
243{
244 struct msgb *msg = bssgp_msgb_alloc();
245 struct bssgp_normal_hdr *bgph =
246 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200247 uint16_t _bvci = osmo_htons(bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100248
249 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK "
Max2c34ab42016-03-17 15:42:26 +0100250 "CAUSE=%s\n", bctx->bvci, bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100251
252 msgb_nsei(msg) = bctx->nsei;
253 msgb_bvci(msg) = 0; /* Signalling */
254 bgph->pdu_type = BSSGP_PDUT_BVC_BLOCK;
255
256 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
257 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
258
259 return gprs_ns_sendmsg(bssgp_nsi, msg);
260}
261
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200262/*! Transmit a BVC-UNBLOCK message (Chapter 10.4.10) */
Harald Welte85fc3142011-11-25 08:58:40 +0100263int bssgp_tx_bvc_unblock(struct bssgp_bvc_ctx *bctx)
264{
265 struct msgb *msg = bssgp_msgb_alloc();
266 struct bssgp_normal_hdr *bgph =
267 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200268 uint16_t _bvci = osmo_htons(bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100269
270 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK\n", bctx->bvci);
271
272 msgb_nsei(msg) = bctx->nsei;
273 msgb_bvci(msg) = 0; /* Signalling */
274 bgph->pdu_type = BSSGP_PDUT_BVC_UNBLOCK;
275
276 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
277
278 return gprs_ns_sendmsg(bssgp_nsi, msg);
279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! Transmit a BVC-RESET message (Chapter 10.4.12) */
Harald Welte85fc3142011-11-25 08:58:40 +0100282int bssgp_tx_bvc_reset(struct bssgp_bvc_ctx *bctx, uint16_t bvci, uint8_t cause)
283{
284 struct msgb *msg = bssgp_msgb_alloc();
285 struct bssgp_normal_hdr *bgph =
286 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200287 uint16_t _bvci = osmo_htons(bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100288
289 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
Max2c34ab42016-03-17 15:42:26 +0100290 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100291
292 msgb_nsei(msg) = bctx->nsei;
293 msgb_bvci(msg) = 0; /* Signalling */
294 bgph->pdu_type = BSSGP_PDUT_BVC_RESET;
295
296 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
297 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
298 if (bvci != BVCI_PTM) {
299 uint8_t bssgp_cid[8];
300 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
301 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
302 }
303 /* Optional: Feature Bitmap */
304
305 return gprs_ns_sendmsg(bssgp_nsi, msg);
306}
307
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200308/*! Transmit a FLOW_CONTROL-BVC (Chapter 10.4.4)
Harald Weltee92866b2012-09-10 08:56:04 +0200309 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200310 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200311 * \param[in] bucket_size Maximum bucket size in octets
312 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
313 * \param[in] bmax_default_ms Maximum bucket size default for MS
314 * \param[in] r_default_ms Bucket leak rate default for MS in octets/sec
315 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
316 * \param[in] queue_delay_ms Average queuing delay in milliseconds
317 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200318int bssgp_tx_fc_bvc(struct bssgp_bvc_ctx *bctx, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200319 uint32_t bucket_size, uint32_t bucket_leak_rate,
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +0200320 uint32_t bmax_default_ms, uint32_t r_default_ms,
Harald Weltee92866b2012-09-10 08:56:04 +0200321 uint8_t *bucket_full_ratio, uint32_t *queue_delay_ms)
322{
323 struct msgb *msg;
324 struct bssgp_normal_hdr *bgph;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200325 uint16_t e_bucket_size, e_leak_rate, e_bmax_default_ms, e_r_default_ms;
Harald Weltee92866b2012-09-10 08:56:04 +0200326 uint16_t e_queue_delay = 0; /* to make gcc happy */
327
328 if ((bucket_size / 100) > 0xffff)
329 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200330 e_bucket_size = osmo_htons(bucket_size / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200331
332 if ((bucket_leak_rate * 8 / 100) > 0xffff)
333 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200334 e_leak_rate = osmo_htons((bucket_leak_rate * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200335
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200336 if ((bmax_default_ms / 100) > 0xffff)
337 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200338 e_bmax_default_ms = osmo_htons(bmax_default_ms / 100);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200339
Harald Weltee92866b2012-09-10 08:56:04 +0200340 if ((r_default_ms * 8 / 100) > 0xffff)
341 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200342 e_r_default_ms = osmo_htons((r_default_ms * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200343
344 if (queue_delay_ms) {
345 if ((*queue_delay_ms / 10) > 60000)
346 return -EINVAL;
347 else if (*queue_delay_ms == 0xFFFFFFFF)
348 e_queue_delay = 0xFFFF;
349 else
Harald Weltebfe62e52017-05-15 12:48:30 +0200350 e_queue_delay = osmo_htons(*queue_delay_ms / 10);
Harald Weltee92866b2012-09-10 08:56:04 +0200351 }
352
353 msg = bssgp_msgb_alloc();
354 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
355 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200356 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200357 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC;
358
359 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
360 msgb_tvlv_put(msg, BSSGP_IE_BVC_BUCKET_SIZE,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200361 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
Harald Weltee92866b2012-09-10 08:56:04 +0200362 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
363 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
364 msgb_tvlv_put(msg, BSSGP_IE_BMAX_DEFAULT_MS,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200365 sizeof(e_bmax_default_ms),
366 (uint8_t *) &e_bmax_default_ms);
Harald Weltee92866b2012-09-10 08:56:04 +0200367 msgb_tvlv_put(msg, BSSGP_IE_R_DEFAULT_MS,
368 sizeof(e_r_default_ms), (uint8_t *) &e_r_default_ms);
369 if (bucket_full_ratio)
370 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
371 1, bucket_full_ratio);
372 if (queue_delay_ms)
373 msgb_tvlv_put(msg, BSSGP_IE_BVC_MEASUREMENT,
374 sizeof(e_queue_delay),
375 (uint8_t *) &e_queue_delay);
376
377 return gprs_ns_sendmsg(bssgp_nsi, msg);
378}
379
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200380/*! Transmit a FLOW_CONTROL-MS (Chapter 10.4.6)
Harald Weltee92866b2012-09-10 08:56:04 +0200381 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200382 * \param[in] tlli TLLI to identify MS
383 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200384 * \param[in] ms_bucket_size Maximum bucket size in octets
385 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
386 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
387 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200388int bssgp_tx_fc_ms(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200389 uint32_t ms_bucket_size, uint32_t bucket_leak_rate,
390 uint8_t *bucket_full_ratio)
391{
392 struct msgb *msg;
393 struct bssgp_normal_hdr *bgph;
394 uint16_t e_bucket_size, e_leak_rate;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200395 uint32_t e_tlli;
Harald Weltee92866b2012-09-10 08:56:04 +0200396
397 if ((ms_bucket_size / 100) > 0xffff)
398 return -EINVAL;
399 e_bucket_size = ms_bucket_size / 100;
400
401 if ((bucket_leak_rate * 8 / 100) > 0xffff)
402 return -EINVAL;
403 e_leak_rate = (bucket_leak_rate * 8) / 100;
404
405 msg = bssgp_msgb_alloc();
406 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
407 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200408 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200409 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_MS;
410
Harald Weltebfe62e52017-05-15 12:48:30 +0200411 e_tlli = osmo_htonl(tlli);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200412 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(e_tlli), (uint8_t *)&e_tlli);
Harald Weltee92866b2012-09-10 08:56:04 +0200413 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
414 msgb_tvlv_put(msg, BSSGP_IE_MS_BUCKET_SIZE,
415 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
416 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
417 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
418 if (bucket_full_ratio)
419 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
420 1, bucket_full_ratio);
421
422 return gprs_ns_sendmsg(bssgp_nsi, msg);
423}
Harald Welte85fc3142011-11-25 08:58:40 +0100424
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200425/*! RL-UL-UNITDATA.req (Chapter 10.2.2)
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200426 * \param[in] bctx BVC Context
427 * \param[in] tlli TLLI to identify MS
428 * \param[in] qos_profile Pointer to three octests of QoS profile
429 * \param[in] llc_pdu msgb pointer containing UL Unitdata IE payload
430 */
Harald Welte85fc3142011-11-25 08:58:40 +0100431int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
432 const uint8_t *qos_profile, struct msgb *llc_pdu)
433{
434 struct msgb *msg = llc_pdu;
435 uint8_t bssgp_cid[8];
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200436 uint8_t bssgp_align[3] = {0, 0, 0};
Harald Welte85fc3142011-11-25 08:58:40 +0100437 struct bssgp_ud_hdr *budh;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200438 int align = sizeof(*budh);
Harald Welte85fc3142011-11-25 08:58:40 +0100439
440 /* FIXME: Optional LSA Identifier List, PFI */
441
442 /* Cell Identifier */
443 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200444 align += 2; /* add T+L */
445 align += sizeof(bssgp_cid);
446
447 /* First push alignment IE */
448 align += 2; /* add T+L */
449 align = (4 - align) & 3; /* how many octest are required to align? */
450 msgb_tvlv_push(msg, BSSGP_IE_ALIGNMENT, align, bssgp_align);
451
452 /* Push other IEs */
Harald Welte85fc3142011-11-25 08:58:40 +0100453 msgb_tvlv_push(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
454
455 /* User Data Header */
456 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Weltebfe62e52017-05-15 12:48:30 +0200457 budh->tlli = osmo_htonl(tlli);
Harald Welte85fc3142011-11-25 08:58:40 +0100458 memcpy(budh->qos_profile, qos_profile, 3);
459 budh->pdu_type = BSSGP_PDUT_UL_UNITDATA;
460
461 /* set NSEI and BVCI in msgb cb */
462 msgb_nsei(msg) = bctx->nsei;
463 msgb_bvci(msg) = bctx->bvci;
464
465 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
466 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
467
468 return gprs_ns_sendmsg(bssgp_nsi, msg);
469}
470
471/* Parse a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800472int bssgp_rx_paging(struct bssgp_paging_info *pinfo,
473 struct msgb *msg)
Harald Welte85fc3142011-11-25 08:58:40 +0100474{
475 struct bssgp_normal_hdr *bgph =
476 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
477 struct tlv_parsed tp;
478 uint8_t ra[6];
479 int rc, data_len;
480
481 memset(ra, 0, sizeof(ra));
482
483 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
484 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
485 if (rc < 0)
486 goto err_mand_ie;
487
488 switch (bgph->pdu_type) {
489 case BSSGP_PDUT_PAGING_PS:
490 pinfo->mode = BSSGP_PAGING_PS;
491 break;
492 case BSSGP_PDUT_PAGING_CS:
493 pinfo->mode = BSSGP_PAGING_CS;
494 break;
495 default:
496 return -EINVAL;
497 }
498
499 /* IMSI */
500 if (!TLVP_PRESENT(&tp, BSSGP_IE_IMSI))
501 goto err_mand_ie;
502 if (!pinfo->imsi)
Jacob Erlbeckc1cb75e2015-06-18 13:21:30 +0200503 pinfo->imsi = talloc_zero_size(pinfo, GSM_IMSI_LENGTH);
504 gsm48_mi_to_string(pinfo->imsi, GSM_IMSI_LENGTH,
Harald Welte85fc3142011-11-25 08:58:40 +0100505 TLVP_VAL(&tp, BSSGP_IE_IMSI),
506 TLVP_LEN(&tp, BSSGP_IE_IMSI));
507
508 /* DRX Parameters */
509 if (!TLVP_PRESENT(&tp, BSSGP_IE_DRX_PARAMS))
510 goto err_mand_ie;
Harald Weltebfe62e52017-05-15 12:48:30 +0200511 pinfo->drx_params = tlvp_val16be(&tp, BSSGP_IE_DRX_PARAMS);
Harald Welte85fc3142011-11-25 08:58:40 +0100512
513 /* Scope */
514 if (TLVP_PRESENT(&tp, BSSGP_IE_BSS_AREA_ID)) {
515 pinfo->scope = BSSGP_PAGING_BSS_AREA;
516 } else if (TLVP_PRESENT(&tp, BSSGP_IE_LOCATION_AREA)) {
517 pinfo->scope = BSSGP_PAGING_LOCATION_AREA;
518 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_LOCATION_AREA),
519 TLVP_LEN(&tp, BSSGP_IE_LOCATION_AREA));
520 gsm48_parse_ra(&pinfo->raid, ra);
521 } else if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) {
522 pinfo->scope = BSSGP_PAGING_ROUTEING_AREA;
523 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
524 TLVP_LEN(&tp, BSSGP_IE_ROUTEING_AREA));
525 gsm48_parse_ra(&pinfo->raid, ra);
526 } else if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
527 pinfo->scope = BSSGP_PAGING_BVCI;
Harald Weltebfe62e52017-05-15 12:48:30 +0200528 pinfo->bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Harald Welte85fc3142011-11-25 08:58:40 +0100529 } else
530 return -EINVAL;
531
532 /* QoS profile mandatory for PS */
533 if (pinfo->mode == BSSGP_PAGING_PS) {
534 if (!TLVP_PRESENT(&tp, BSSGP_IE_QOS_PROFILE))
535 goto err_cond_ie;
536 if (TLVP_LEN(&tp, BSSGP_IE_QOS_PROFILE) < 3)
537 goto err;
538
539 memcpy(&pinfo->qos, TLVP_VAL(&tp, BSSGP_IE_QOS_PROFILE),
540 3);
541 }
542
543 /* Optional (P-)TMSI */
544 if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI) &&
Harald Welte6176b6e2016-11-11 15:10:33 +0100545 TLVP_LEN(&tp, BSSGP_IE_TMSI) >= 4) {
Harald Welte85fc3142011-11-25 08:58:40 +0100546 if (!pinfo->ptmsi)
547 pinfo->ptmsi = talloc_zero_size(pinfo, sizeof(uint32_t));
Harald Weltebfe62e52017-05-15 12:48:30 +0200548 *(pinfo->ptmsi) = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Harald Welte6176b6e2016-11-11 15:10:33 +0100549 }
Harald Welte85fc3142011-11-25 08:58:40 +0100550
551 return 0;
552
553err_mand_ie:
554err_cond_ie:
555err:
556 /* FIXME */
557 return 0;
558}