blob: 5a1ffb9240bbc680cb543a2bd52022d1c61daf83 [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 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welte85fc3142011-11-25 08:58:40 +010010 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Harald Welte85fc3142011-11-25 08:58:40 +010013 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * GNU General Public License for more details.
Harald Welte85fc3142011-11-25 08:58:40 +010019 *
Harald Welte7fa89c22014-10-26 20:33:09 +010020 * You should have received a copy of the GNU General Public License
Harald Welte85fc3142011-11-25 08:58:40 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <errno.h>
26#include <stdint.h>
27
Harald Welte85fc3142011-11-25 08:58:40 +010028#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020029#include <osmocom/core/byteswap.h>
Harald Welte85fc3142011-11-25 08:58:40 +010030#include <osmocom/core/rate_ctr.h>
31#include <osmocom/gsm/tlv.h>
32#include <osmocom/core/talloc.h>
Harald Welte73952e32012-06-16 14:59:56 +080033#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte30fabdf2012-09-10 08:54:35 +020034#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welte73952e32012-06-16 14:59:56 +080035#include <osmocom/gprs/gprs_ns.h>
Harald Welte85fc3142011-11-25 08:58:40 +010036
Harald Weltecca49632012-06-16 17:45:59 +080037#include "common_vty.h"
Harald Welte85fc3142011-11-25 08:58:40 +010038
Jacob Erlbeckc1cb75e2015-06-18 13:21:30 +020039#define GSM_IMSI_LENGTH 17
40
Harald Welte85fc3142011-11-25 08:58:40 +010041uint8_t *bssgp_msgb_tlli_put(struct msgb *msg, uint32_t tlli)
42{
Harald Weltebfe62e52017-05-15 12:48:30 +020043 uint32_t _tlli = osmo_htonl(tlli);
Harald Welte85fc3142011-11-25 08:58:40 +010044 return msgb_tvlv_put(msg, BSSGP_IE_TLLI, 4, (uint8_t *) &_tlli);
45}
46
Maxf1ad60e2018-01-05 14:19:33 +010047uint8_t *bssgp_msgb_ra_put(struct msgb *msg, const struct gprs_ra_id *ra_id)
48{
49 struct gsm48_ra_id ra;
50
51 gsm48_encode_ra(&ra, ra_id);
52 return msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, sizeof(ra), (const uint8_t *)&ra);
53}
54
Neels Hofmeyr87e45502017-06-20 00:17:59 +020055/*! GMM-SUSPEND.req (Chapter 10.3.6) */
Harald Welte85fc3142011-11-25 08:58:40 +010056int bssgp_tx_suspend(uint16_t nsei, uint32_t tlli,
57 const struct gprs_ra_id *ra_id)
58{
59 struct msgb *msg = bssgp_msgb_alloc();
60 struct bssgp_normal_hdr *bgph =
61 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Welte85fc3142011-11-25 08:58:40 +010062
63 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx SUSPEND (TLLI=0x%04x)\n",
64 tlli);
65 msgb_nsei(msg) = nsei;
66 msgb_bvci(msg) = 0; /* Signalling */
67 bgph->pdu_type = BSSGP_PDUT_SUSPEND;
68
69 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +010070 bssgp_msgb_ra_put(msg, ra_id);
Harald Welte85fc3142011-11-25 08:58:40 +010071
72 return gprs_ns_sendmsg(bssgp_nsi, msg);
73}
74
Neels Hofmeyr87e45502017-06-20 00:17:59 +020075/*! GMM-RESUME.req (Chapter 10.3.9) */
Harald Welte85fc3142011-11-25 08:58:40 +010076int bssgp_tx_resume(uint16_t nsei, uint32_t tlli,
77 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
78{
79 struct msgb *msg = bssgp_msgb_alloc();
80 struct bssgp_normal_hdr *bgph =
81 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Welte85fc3142011-11-25 08:58:40 +010082
83 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx RESUME (TLLI=0x%04x)\n",
84 tlli);
85 msgb_nsei(msg) = nsei;
86 msgb_bvci(msg) = 0; /* Signalling */
87 bgph->pdu_type = BSSGP_PDUT_RESUME;
88
89 bssgp_msgb_tlli_put(msg, tlli);
Maxf1ad60e2018-01-05 14:19:33 +010090 bssgp_msgb_ra_put(msg, ra_id);
Harald Welte85fc3142011-11-25 08:58:40 +010091
92 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
93
94 return gprs_ns_sendmsg(bssgp_nsi, msg);
95}
96
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097/*! Transmit RA-CAPABILITY-UPDATE (10.3.3) */
Harald Welte85fc3142011-11-25 08:58:40 +010098int bssgp_tx_ra_capa_upd(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag)
99{
100 struct msgb *msg = bssgp_msgb_alloc();
101 struct bssgp_normal_hdr *bgph =
102 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
103
104 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RA-CAPA-UPD (TLLI=0x%04x)\n",
105 bctx->bvci, tlli);
106
107 /* set NSEI and BVCI in msgb cb */
108 msgb_nsei(msg) = bctx->nsei;
109 msgb_bvci(msg) = bctx->bvci;
110
111 bgph->pdu_type = BSSGP_PDUT_RA_CAPA_UDPATE;
112 bssgp_msgb_tlli_put(msg, tlli);
113
114 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
115
116 return gprs_ns_sendmsg(bssgp_nsi, msg);
117}
118
119/* first common part of RADIO-STATUS */
120static struct msgb *common_tx_radio_status(struct bssgp_bvc_ctx *bctx)
121{
122 struct msgb *msg = bssgp_msgb_alloc();
123 struct bssgp_normal_hdr *bgph =
124 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
125
126 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RADIO-STATUS ",
127 bctx->bvci);
128
129 /* set NSEI and BVCI in msgb cb */
130 msgb_nsei(msg) = bctx->nsei;
131 msgb_bvci(msg) = bctx->bvci;
132
133 bgph->pdu_type = BSSGP_PDUT_RADIO_STATUS;
134
135 return msg;
136}
137
138/* second common part of RADIO-STATUS */
139static int common_tx_radio_status2(struct msgb *msg, uint8_t cause)
140{
141 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
Max2c34ab42016-03-17 15:42:26 +0100142 LOGPC(DBSSGP, LOGL_NOTICE, "CAUSE=%s\n", bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100143
144 return gprs_ns_sendmsg(bssgp_nsi, msg);
145}
146
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200147/*! Transmit RADIO-STATUS for TLLI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100148int bssgp_tx_radio_status_tlli(struct bssgp_bvc_ctx *bctx, uint8_t cause,
149 uint32_t tlli)
150{
151 struct msgb *msg = common_tx_radio_status(bctx);
152
153 if (!msg)
154 return -ENOMEM;
155 bssgp_msgb_tlli_put(msg, tlli);
156 LOGPC(DBSSGP, LOGL_NOTICE, "TLLI=0x%08x ", tlli);
157
158 return common_tx_radio_status2(msg, cause);
159}
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! Transmit RADIO-STATUS for TMSI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100162int bssgp_tx_radio_status_tmsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
163 uint32_t tmsi)
164{
165 struct msgb *msg = common_tx_radio_status(bctx);
Harald Weltebfe62e52017-05-15 12:48:30 +0200166 uint32_t _tmsi = osmo_htonl(tmsi);
Harald Welte85fc3142011-11-25 08:58:40 +0100167
168 if (!msg)
169 return -ENOMEM;
170 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *)&_tmsi);
171 LOGPC(DBSSGP, LOGL_NOTICE, "TMSI=0x%08x ", tmsi);
172
173 return common_tx_radio_status2(msg, cause);
174}
175
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176/*! Transmit RADIO-STATUS for IMSI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100177int bssgp_tx_radio_status_imsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
178 const char *imsi)
179{
180 struct msgb *msg = common_tx_radio_status(bctx);
Harald Weltea13fb752020-06-16 08:44:42 +0200181 uint8_t mi[GSM48_MID_MAX_SIZE];
182 int imsi_len = gsm48_generate_mid_from_imsi(mi, imsi);
Harald Welte85fc3142011-11-25 08:58:40 +0100183
184 if (!msg)
185 return -ENOMEM;
Harald Weltea13fb752020-06-16 08:44:42 +0200186/* gsm48_generate_mid_from_imsi() is guaranteed to never return more than 11,
187 * but somehow gcc (8.2) is not smart enough to figure this out and claims that
188 * the memcpy in msgb_tvlv_put() below will cause and out-of-bounds access up to
189 * mi[131], which is wrong */
190#pragma GCC diagnostic push
191#pragma GCC diagnostic ignored "-Warray-bounds"
192 OSMO_ASSERT(imsi_len <= GSM48_MID_MAX_SIZE);
193 /* strip the MI type and length values (2 bytes) */
194 if (imsi_len > 2)
195 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
196#pragma GCC diagnostic pop
Harald Welte85fc3142011-11-25 08:58:40 +0100197 LOGPC(DBSSGP, LOGL_NOTICE, "IMSI=%s ", imsi);
198
199 return common_tx_radio_status2(msg, cause);
200}
201
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200202/*! Transmit FLUSH-LL-ACK (Chapter 10.4.2) */
Harald Welte85fc3142011-11-25 08:58:40 +0100203int bssgp_tx_flush_ll_ack(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
204 uint8_t action, uint16_t bvci_new,
205 uint32_t num_octets)
206{
207 struct msgb *msg = bssgp_msgb_alloc();
208 struct bssgp_normal_hdr *bgph =
209 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200210 uint16_t _bvci_new = osmo_htons(bvci_new);
211 uint32_t _oct_aff = osmo_htonl(num_octets & 0xFFFFFF);
Harald Welte85fc3142011-11-25 08:58:40 +0100212
213 msgb_nsei(msg) = bctx->nsei;
214 msgb_bvci(msg) = 0; /* Signalling */
215 bgph->pdu_type = BSSGP_PDUT_FLUSH_LL_ACK;
216
217 bssgp_msgb_tlli_put(msg, tlli);
218 msgb_tvlv_put(msg, BSSGP_IE_FLUSH_ACTION, 1, &action);
219 if (action == 1) /* transferred */
220 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci_new);
221 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, (uint8_t *) &_oct_aff);
222
223 return gprs_ns_sendmsg(bssgp_nsi, msg);
224}
225
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200226/*! Transmit LLC-DISCARDED (Chapter 10.4.3) */
Harald Welte85fc3142011-11-25 08:58:40 +0100227int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
228 uint8_t num_frames, uint32_t num_octets)
229{
230 struct msgb *msg = bssgp_msgb_alloc();
231 struct bssgp_normal_hdr *bgph =
232 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200233 uint16_t _bvci = osmo_htons(bctx->bvci);
234 uint32_t _oct_aff = osmo_htonl(num_octets & 0xFFFFFF);
Harald Welte85fc3142011-11-25 08:58:40 +0100235
236 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx LLC-DISCARDED "
237 "TLLI=0x%04x, FRAMES=%u, OCTETS=%u\n", bctx->bvci, tlli,
238 num_frames, num_octets);
239 msgb_nsei(msg) = bctx->nsei;
240 msgb_bvci(msg) = 0; /* Signalling */
241 bgph->pdu_type = BSSGP_PDUT_LLC_DISCARD;
242
243 bssgp_msgb_tlli_put(msg, tlli);
244
245 msgb_tvlv_put(msg, BSSGP_IE_LLC_FRAMES_DISCARDED, 1, &num_frames);
246 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
Andreas Eversbergaa5d0e82012-07-21 13:33:39 +0200247 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, ((uint8_t *) &_oct_aff) + 1);
Harald Welte85fc3142011-11-25 08:58:40 +0100248
249 return gprs_ns_sendmsg(bssgp_nsi, msg);
250}
251
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200252/*! Transmit a BVC-BLOCK message (Chapter 10.4.8) */
Harald Welte85fc3142011-11-25 08:58:40 +0100253int bssgp_tx_bvc_block(struct bssgp_bvc_ctx *bctx, uint8_t cause)
254{
255 struct msgb *msg = bssgp_msgb_alloc();
256 struct bssgp_normal_hdr *bgph =
257 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200258 uint16_t _bvci = osmo_htons(bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100259
260 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK "
Max2c34ab42016-03-17 15:42:26 +0100261 "CAUSE=%s\n", bctx->bvci, bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100262
263 msgb_nsei(msg) = bctx->nsei;
264 msgb_bvci(msg) = 0; /* Signalling */
265 bgph->pdu_type = BSSGP_PDUT_BVC_BLOCK;
266
267 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
268 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
269
270 return gprs_ns_sendmsg(bssgp_nsi, msg);
271}
272
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200273/*! Transmit a BVC-UNBLOCK message (Chapter 10.4.10) */
Harald Welte85fc3142011-11-25 08:58:40 +0100274int bssgp_tx_bvc_unblock(struct bssgp_bvc_ctx *bctx)
275{
276 struct msgb *msg = bssgp_msgb_alloc();
277 struct bssgp_normal_hdr *bgph =
278 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200279 uint16_t _bvci = osmo_htons(bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100280
Harald Welte6a469e12019-02-19 11:07:50 +0100281 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-UNBLOCK\n", bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100282
283 msgb_nsei(msg) = bctx->nsei;
284 msgb_bvci(msg) = 0; /* Signalling */
285 bgph->pdu_type = BSSGP_PDUT_BVC_UNBLOCK;
286
287 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
288
289 return gprs_ns_sendmsg(bssgp_nsi, msg);
290}
291
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200292/*! Transmit a BVC-RESET message (Chapter 10.4.12) */
Harald Welte2d3465f2020-06-26 15:13:19 +0200293int bssgp_tx_bvc_reset2(struct bssgp_bvc_ctx *bctx, uint16_t bvci, uint8_t cause, bool add_cell_id)
Harald Welte85fc3142011-11-25 08:58:40 +0100294{
295 struct msgb *msg = bssgp_msgb_alloc();
296 struct bssgp_normal_hdr *bgph =
297 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200298 uint16_t _bvci = osmo_htons(bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100299
300 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
Max2c34ab42016-03-17 15:42:26 +0100301 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100302
303 msgb_nsei(msg) = bctx->nsei;
304 msgb_bvci(msg) = 0; /* Signalling */
305 bgph->pdu_type = BSSGP_PDUT_BVC_RESET;
306
307 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
308 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
Harald Welte2d3465f2020-06-26 15:13:19 +0200309 if (add_cell_id) {
Harald Welte85fc3142011-11-25 08:58:40 +0100310 uint8_t bssgp_cid[8];
311 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
312 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
313 }
314 /* Optional: Feature Bitmap */
315
316 return gprs_ns_sendmsg(bssgp_nsi, msg);
317}
Harald Welte2d3465f2020-06-26 15:13:19 +0200318int bssgp_tx_bvc_reset(struct bssgp_bvc_ctx *bctx, uint16_t bvci, uint8_t cause)
319{
320 return bssgp_tx_bvc_reset2(bctx, bvci, cause, bvci != BVCI_PTM);
321}
Harald Welte85fc3142011-11-25 08:58:40 +0100322
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200323/*! Transmit a FLOW_CONTROL-BVC (Chapter 10.4.4)
Harald Weltee92866b2012-09-10 08:56:04 +0200324 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200325 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200326 * \param[in] bucket_size Maximum bucket size in octets
327 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
328 * \param[in] bmax_default_ms Maximum bucket size default for MS
329 * \param[in] r_default_ms Bucket leak rate default for MS in octets/sec
330 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
331 * \param[in] queue_delay_ms Average queuing delay in milliseconds
332 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200333int bssgp_tx_fc_bvc(struct bssgp_bvc_ctx *bctx, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200334 uint32_t bucket_size, uint32_t bucket_leak_rate,
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +0200335 uint32_t bmax_default_ms, uint32_t r_default_ms,
Harald Weltee92866b2012-09-10 08:56:04 +0200336 uint8_t *bucket_full_ratio, uint32_t *queue_delay_ms)
337{
338 struct msgb *msg;
339 struct bssgp_normal_hdr *bgph;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200340 uint16_t e_bucket_size, e_leak_rate, e_bmax_default_ms, e_r_default_ms;
Harald Weltee92866b2012-09-10 08:56:04 +0200341 uint16_t e_queue_delay = 0; /* to make gcc happy */
342
343 if ((bucket_size / 100) > 0xffff)
344 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200345 e_bucket_size = osmo_htons(bucket_size / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200346
347 if ((bucket_leak_rate * 8 / 100) > 0xffff)
348 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200349 e_leak_rate = osmo_htons((bucket_leak_rate * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200350
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200351 if ((bmax_default_ms / 100) > 0xffff)
352 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200353 e_bmax_default_ms = osmo_htons(bmax_default_ms / 100);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200354
Harald Weltee92866b2012-09-10 08:56:04 +0200355 if ((r_default_ms * 8 / 100) > 0xffff)
356 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200357 e_r_default_ms = osmo_htons((r_default_ms * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200358
359 if (queue_delay_ms) {
360 if ((*queue_delay_ms / 10) > 60000)
361 return -EINVAL;
362 else if (*queue_delay_ms == 0xFFFFFFFF)
363 e_queue_delay = 0xFFFF;
364 else
Harald Weltebfe62e52017-05-15 12:48:30 +0200365 e_queue_delay = osmo_htons(*queue_delay_ms / 10);
Harald Weltee92866b2012-09-10 08:56:04 +0200366 }
367
368 msg = bssgp_msgb_alloc();
369 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
370 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200371 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200372 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC;
373
374 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
375 msgb_tvlv_put(msg, BSSGP_IE_BVC_BUCKET_SIZE,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200376 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
Harald Weltee92866b2012-09-10 08:56:04 +0200377 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
378 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
379 msgb_tvlv_put(msg, BSSGP_IE_BMAX_DEFAULT_MS,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200380 sizeof(e_bmax_default_ms),
381 (uint8_t *) &e_bmax_default_ms);
Harald Weltee92866b2012-09-10 08:56:04 +0200382 msgb_tvlv_put(msg, BSSGP_IE_R_DEFAULT_MS,
383 sizeof(e_r_default_ms), (uint8_t *) &e_r_default_ms);
384 if (bucket_full_ratio)
385 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
386 1, bucket_full_ratio);
387 if (queue_delay_ms)
388 msgb_tvlv_put(msg, BSSGP_IE_BVC_MEASUREMENT,
389 sizeof(e_queue_delay),
390 (uint8_t *) &e_queue_delay);
391
392 return gprs_ns_sendmsg(bssgp_nsi, msg);
393}
394
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200395/*! Transmit a FLOW_CONTROL-MS (Chapter 10.4.6)
Harald Weltee92866b2012-09-10 08:56:04 +0200396 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200397 * \param[in] tlli TLLI to identify MS
398 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200399 * \param[in] ms_bucket_size Maximum bucket size in octets
400 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
401 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
402 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200403int bssgp_tx_fc_ms(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200404 uint32_t ms_bucket_size, uint32_t bucket_leak_rate,
405 uint8_t *bucket_full_ratio)
406{
407 struct msgb *msg;
408 struct bssgp_normal_hdr *bgph;
409 uint16_t e_bucket_size, e_leak_rate;
410
411 if ((ms_bucket_size / 100) > 0xffff)
412 return -EINVAL;
413 e_bucket_size = ms_bucket_size / 100;
414
415 if ((bucket_leak_rate * 8 / 100) > 0xffff)
416 return -EINVAL;
417 e_leak_rate = (bucket_leak_rate * 8) / 100;
418
419 msg = bssgp_msgb_alloc();
420 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
421 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200422 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200423 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_MS;
424
Maxe29ec852018-01-05 14:30:22 +0100425 bssgp_msgb_tlli_put(msg, tlli);
Harald Weltee92866b2012-09-10 08:56:04 +0200426 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
427 msgb_tvlv_put(msg, BSSGP_IE_MS_BUCKET_SIZE,
428 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
429 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
430 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
431 if (bucket_full_ratio)
432 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
433 1, bucket_full_ratio);
434
435 return gprs_ns_sendmsg(bssgp_nsi, msg);
436}
Harald Welte85fc3142011-11-25 08:58:40 +0100437
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200438/*! RL-UL-UNITDATA.req (Chapter 10.2.2)
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200439 * \param[in] bctx BVC Context
440 * \param[in] tlli TLLI to identify MS
441 * \param[in] qos_profile Pointer to three octests of QoS profile
442 * \param[in] llc_pdu msgb pointer containing UL Unitdata IE payload
443 */
Harald Welte85fc3142011-11-25 08:58:40 +0100444int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
445 const uint8_t *qos_profile, struct msgb *llc_pdu)
446{
447 struct msgb *msg = llc_pdu;
448 uint8_t bssgp_cid[8];
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200449 uint8_t bssgp_align[3] = {0, 0, 0};
Harald Welte85fc3142011-11-25 08:58:40 +0100450 struct bssgp_ud_hdr *budh;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200451 int align = sizeof(*budh);
Harald Welte85fc3142011-11-25 08:58:40 +0100452
453 /* FIXME: Optional LSA Identifier List, PFI */
454
455 /* Cell Identifier */
456 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200457 align += 2; /* add T+L */
458 align += sizeof(bssgp_cid);
459
460 /* First push alignment IE */
461 align += 2; /* add T+L */
462 align = (4 - align) & 3; /* how many octest are required to align? */
463 msgb_tvlv_push(msg, BSSGP_IE_ALIGNMENT, align, bssgp_align);
464
465 /* Push other IEs */
Harald Welte85fc3142011-11-25 08:58:40 +0100466 msgb_tvlv_push(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
467
468 /* User Data Header */
469 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Weltebfe62e52017-05-15 12:48:30 +0200470 budh->tlli = osmo_htonl(tlli);
Harald Welte85fc3142011-11-25 08:58:40 +0100471 memcpy(budh->qos_profile, qos_profile, 3);
472 budh->pdu_type = BSSGP_PDUT_UL_UNITDATA;
473
474 /* set NSEI and BVCI in msgb cb */
475 msgb_nsei(msg) = bctx->nsei;
476 msgb_bvci(msg) = bctx->bvci;
477
478 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
479 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
480
481 return gprs_ns_sendmsg(bssgp_nsi, msg);
482}
483
484/* Parse a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800485int bssgp_rx_paging(struct bssgp_paging_info *pinfo,
486 struct msgb *msg)
Harald Welte85fc3142011-11-25 08:58:40 +0100487{
488 struct bssgp_normal_hdr *bgph =
489 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
490 struct tlv_parsed tp;
491 uint8_t ra[6];
492 int rc, data_len;
493
494 memset(ra, 0, sizeof(ra));
495
496 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
497 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
498 if (rc < 0)
499 goto err_mand_ie;
500
501 switch (bgph->pdu_type) {
502 case BSSGP_PDUT_PAGING_PS:
503 pinfo->mode = BSSGP_PAGING_PS;
504 break;
505 case BSSGP_PDUT_PAGING_CS:
506 pinfo->mode = BSSGP_PAGING_CS;
507 break;
508 default:
509 return -EINVAL;
510 }
511
512 /* IMSI */
513 if (!TLVP_PRESENT(&tp, BSSGP_IE_IMSI))
514 goto err_mand_ie;
515 if (!pinfo->imsi)
Jacob Erlbeckc1cb75e2015-06-18 13:21:30 +0200516 pinfo->imsi = talloc_zero_size(pinfo, GSM_IMSI_LENGTH);
Harald Weltea13fb752020-06-16 08:44:42 +0200517 gsm48_mi_to_string(pinfo->imsi, GSM_IMSI_LENGTH,
518 TLVP_VAL(&tp, BSSGP_IE_IMSI),
519 TLVP_LEN(&tp, BSSGP_IE_IMSI));
Harald Welte85fc3142011-11-25 08:58:40 +0100520
521 /* DRX Parameters */
522 if (!TLVP_PRESENT(&tp, BSSGP_IE_DRX_PARAMS))
523 goto err_mand_ie;
Harald Weltebfe62e52017-05-15 12:48:30 +0200524 pinfo->drx_params = tlvp_val16be(&tp, BSSGP_IE_DRX_PARAMS);
Harald Welte85fc3142011-11-25 08:58:40 +0100525
526 /* Scope */
527 if (TLVP_PRESENT(&tp, BSSGP_IE_BSS_AREA_ID)) {
528 pinfo->scope = BSSGP_PAGING_BSS_AREA;
529 } else if (TLVP_PRESENT(&tp, BSSGP_IE_LOCATION_AREA)) {
530 pinfo->scope = BSSGP_PAGING_LOCATION_AREA;
531 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_LOCATION_AREA),
532 TLVP_LEN(&tp, BSSGP_IE_LOCATION_AREA));
533 gsm48_parse_ra(&pinfo->raid, ra);
534 } else if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) {
535 pinfo->scope = BSSGP_PAGING_ROUTEING_AREA;
536 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
537 TLVP_LEN(&tp, BSSGP_IE_ROUTEING_AREA));
538 gsm48_parse_ra(&pinfo->raid, ra);
539 } else if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
540 pinfo->scope = BSSGP_PAGING_BVCI;
Harald Weltebfe62e52017-05-15 12:48:30 +0200541 pinfo->bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Harald Welte85fc3142011-11-25 08:58:40 +0100542 } else
543 return -EINVAL;
544
545 /* QoS profile mandatory for PS */
546 if (pinfo->mode == BSSGP_PAGING_PS) {
547 if (!TLVP_PRESENT(&tp, BSSGP_IE_QOS_PROFILE))
548 goto err_cond_ie;
549 if (TLVP_LEN(&tp, BSSGP_IE_QOS_PROFILE) < 3)
550 goto err;
551
552 memcpy(&pinfo->qos, TLVP_VAL(&tp, BSSGP_IE_QOS_PROFILE),
553 3);
554 }
555
556 /* Optional (P-)TMSI */
557 if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI) &&
Harald Welte6176b6e2016-11-11 15:10:33 +0100558 TLVP_LEN(&tp, BSSGP_IE_TMSI) >= 4) {
Harald Welte85fc3142011-11-25 08:58:40 +0100559 if (!pinfo->ptmsi)
560 pinfo->ptmsi = talloc_zero_size(pinfo, sizeof(uint32_t));
Harald Weltebfe62e52017-05-15 12:48:30 +0200561 *(pinfo->ptmsi) = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Harald Welte6176b6e2016-11-11 15:10:33 +0100562 }
Harald Welte85fc3142011-11-25 08:58:40 +0100563
564 return 0;
565
566err_mand_ie:
567err_cond_ie:
568err:
569 /* FIXME */
570 return 0;
571}