blob: 3939e258348eabc6afd23b5273516c82b5fb28da [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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047/*! GMM-SUSPEND.req (Chapter 10.3.6) */
Harald Welte85fc3142011-11-25 08:58:40 +010048int bssgp_tx_suspend(uint16_t nsei, uint32_t tlli,
49 const struct gprs_ra_id *ra_id)
50{
51 struct msgb *msg = bssgp_msgb_alloc();
52 struct bssgp_normal_hdr *bgph =
53 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
54 uint8_t ra[6];
55
56 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx SUSPEND (TLLI=0x%04x)\n",
57 tlli);
58 msgb_nsei(msg) = nsei;
59 msgb_bvci(msg) = 0; /* Signalling */
60 bgph->pdu_type = BSSGP_PDUT_SUSPEND;
61
62 bssgp_msgb_tlli_put(msg, tlli);
63
64 gsm48_construct_ra(ra, ra_id);
65 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
66
67 return gprs_ns_sendmsg(bssgp_nsi, msg);
68}
69
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070/*! GMM-RESUME.req (Chapter 10.3.9) */
Harald Welte85fc3142011-11-25 08:58:40 +010071int bssgp_tx_resume(uint16_t nsei, uint32_t tlli,
72 const struct gprs_ra_id *ra_id, uint8_t suspend_ref)
73{
74 struct msgb *msg = bssgp_msgb_alloc();
75 struct bssgp_normal_hdr *bgph =
76 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
77 uint8_t ra[6];
78
79 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=0) Tx RESUME (TLLI=0x%04x)\n",
80 tlli);
81 msgb_nsei(msg) = nsei;
82 msgb_bvci(msg) = 0; /* Signalling */
83 bgph->pdu_type = BSSGP_PDUT_RESUME;
84
85 bssgp_msgb_tlli_put(msg, tlli);
86
87 gsm48_construct_ra(ra, ra_id);
88 msgb_tvlv_put(msg, BSSGP_IE_ROUTEING_AREA, 6, ra);
89
90 msgb_tvlv_put(msg, BSSGP_IE_SUSPEND_REF_NR, 1, &suspend_ref);
91
92 return gprs_ns_sendmsg(bssgp_nsi, msg);
93}
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! Transmit RA-CAPABILITY-UPDATE (10.3.3) */
Harald Welte85fc3142011-11-25 08:58:40 +010096int bssgp_tx_ra_capa_upd(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag)
97{
98 struct msgb *msg = bssgp_msgb_alloc();
99 struct bssgp_normal_hdr *bgph =
100 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
101
102 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RA-CAPA-UPD (TLLI=0x%04x)\n",
103 bctx->bvci, tlli);
104
105 /* set NSEI and BVCI in msgb cb */
106 msgb_nsei(msg) = bctx->nsei;
107 msgb_bvci(msg) = bctx->bvci;
108
109 bgph->pdu_type = BSSGP_PDUT_RA_CAPA_UDPATE;
110 bssgp_msgb_tlli_put(msg, tlli);
111
112 msgb_tvlv_put(msg, BSSGP_IE_TAG, 1, &tag);
113
114 return gprs_ns_sendmsg(bssgp_nsi, msg);
115}
116
117/* first common part of RADIO-STATUS */
118static struct msgb *common_tx_radio_status(struct bssgp_bvc_ctx *bctx)
119{
120 struct msgb *msg = bssgp_msgb_alloc();
121 struct bssgp_normal_hdr *bgph =
122 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
123
124 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx RADIO-STATUS ",
125 bctx->bvci);
126
127 /* set NSEI and BVCI in msgb cb */
128 msgb_nsei(msg) = bctx->nsei;
129 msgb_bvci(msg) = bctx->bvci;
130
131 bgph->pdu_type = BSSGP_PDUT_RADIO_STATUS;
132
133 return msg;
134}
135
136/* second common part of RADIO-STATUS */
137static int common_tx_radio_status2(struct msgb *msg, uint8_t cause)
138{
139 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
Max2c34ab42016-03-17 15:42:26 +0100140 LOGPC(DBSSGP, LOGL_NOTICE, "CAUSE=%s\n", bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100141
142 return gprs_ns_sendmsg(bssgp_nsi, msg);
143}
144
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200145/*! Transmit RADIO-STATUS for TLLI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100146int bssgp_tx_radio_status_tlli(struct bssgp_bvc_ctx *bctx, uint8_t cause,
147 uint32_t tlli)
148{
149 struct msgb *msg = common_tx_radio_status(bctx);
150
151 if (!msg)
152 return -ENOMEM;
153 bssgp_msgb_tlli_put(msg, tlli);
154 LOGPC(DBSSGP, LOGL_NOTICE, "TLLI=0x%08x ", tlli);
155
156 return common_tx_radio_status2(msg, cause);
157}
158
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159/*! Transmit RADIO-STATUS for TMSI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100160int bssgp_tx_radio_status_tmsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
161 uint32_t tmsi)
162{
163 struct msgb *msg = common_tx_radio_status(bctx);
Harald Weltebfe62e52017-05-15 12:48:30 +0200164 uint32_t _tmsi = osmo_htonl(tmsi);
Harald Welte85fc3142011-11-25 08:58:40 +0100165
166 if (!msg)
167 return -ENOMEM;
168 msgb_tvlv_put(msg, BSSGP_IE_TMSI, 4, (uint8_t *)&_tmsi);
169 LOGPC(DBSSGP, LOGL_NOTICE, "TMSI=0x%08x ", tmsi);
170
171 return common_tx_radio_status2(msg, cause);
172}
173
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200174/*! Transmit RADIO-STATUS for IMSI (10.3.5) */
Harald Welte85fc3142011-11-25 08:58:40 +0100175int bssgp_tx_radio_status_imsi(struct bssgp_bvc_ctx *bctx, uint8_t cause,
176 const char *imsi)
177{
178 struct msgb *msg = common_tx_radio_status(bctx);
179 uint8_t mi[10];
180 int imsi_len = gsm48_generate_mid_from_imsi(mi, imsi);
181
182 if (!msg)
183 return -ENOMEM;
184
185 /* strip the MI type and length values (2 bytes) */
186 if (imsi_len > 2)
187 msgb_tvlv_put(msg, BSSGP_IE_IMSI, imsi_len-2, mi+2);
188 LOGPC(DBSSGP, LOGL_NOTICE, "IMSI=%s ", imsi);
189
190 return common_tx_radio_status2(msg, cause);
191}
192
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200193/*! Transmit FLUSH-LL-ACK (Chapter 10.4.2) */
Harald Welte85fc3142011-11-25 08:58:40 +0100194int bssgp_tx_flush_ll_ack(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
195 uint8_t action, uint16_t bvci_new,
196 uint32_t num_octets)
197{
198 struct msgb *msg = bssgp_msgb_alloc();
199 struct bssgp_normal_hdr *bgph =
200 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200201 uint16_t _bvci_new = osmo_htons(bvci_new);
202 uint32_t _oct_aff = osmo_htonl(num_octets & 0xFFFFFF);
Harald Welte85fc3142011-11-25 08:58:40 +0100203
204 msgb_nsei(msg) = bctx->nsei;
205 msgb_bvci(msg) = 0; /* Signalling */
206 bgph->pdu_type = BSSGP_PDUT_FLUSH_LL_ACK;
207
208 bssgp_msgb_tlli_put(msg, tlli);
209 msgb_tvlv_put(msg, BSSGP_IE_FLUSH_ACTION, 1, &action);
210 if (action == 1) /* transferred */
211 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci_new);
212 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, (uint8_t *) &_oct_aff);
213
214 return gprs_ns_sendmsg(bssgp_nsi, msg);
215}
216
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200217/*! Transmit LLC-DISCARDED (Chapter 10.4.3) */
Harald Welte85fc3142011-11-25 08:58:40 +0100218int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
219 uint8_t num_frames, uint32_t num_octets)
220{
221 struct msgb *msg = bssgp_msgb_alloc();
222 struct bssgp_normal_hdr *bgph =
223 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200224 uint16_t _bvci = osmo_htons(bctx->bvci);
225 uint32_t _oct_aff = osmo_htonl(num_octets & 0xFFFFFF);
Harald Welte85fc3142011-11-25 08:58:40 +0100226
227 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx LLC-DISCARDED "
228 "TLLI=0x%04x, FRAMES=%u, OCTETS=%u\n", bctx->bvci, tlli,
229 num_frames, num_octets);
230 msgb_nsei(msg) = bctx->nsei;
231 msgb_bvci(msg) = 0; /* Signalling */
232 bgph->pdu_type = BSSGP_PDUT_LLC_DISCARD;
233
234 bssgp_msgb_tlli_put(msg, tlli);
235
236 msgb_tvlv_put(msg, BSSGP_IE_LLC_FRAMES_DISCARDED, 1, &num_frames);
237 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
Andreas Eversbergaa5d0e82012-07-21 13:33:39 +0200238 msgb_tvlv_put(msg, BSSGP_IE_NUM_OCT_AFF, 3, ((uint8_t *) &_oct_aff) + 1);
Harald Welte85fc3142011-11-25 08:58:40 +0100239
240 return gprs_ns_sendmsg(bssgp_nsi, msg);
241}
242
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200243/*! Transmit a BVC-BLOCK message (Chapter 10.4.8) */
Harald Welte85fc3142011-11-25 08:58:40 +0100244int bssgp_tx_bvc_block(struct bssgp_bvc_ctx *bctx, uint8_t cause)
245{
246 struct msgb *msg = bssgp_msgb_alloc();
247 struct bssgp_normal_hdr *bgph =
248 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200249 uint16_t _bvci = osmo_htons(bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100250
251 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK "
Max2c34ab42016-03-17 15:42:26 +0100252 "CAUSE=%s\n", bctx->bvci, bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100253
254 msgb_nsei(msg) = bctx->nsei;
255 msgb_bvci(msg) = 0; /* Signalling */
256 bgph->pdu_type = BSSGP_PDUT_BVC_BLOCK;
257
258 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
259 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
260
261 return gprs_ns_sendmsg(bssgp_nsi, msg);
262}
263
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200264/*! Transmit a BVC-UNBLOCK message (Chapter 10.4.10) */
Harald Welte85fc3142011-11-25 08:58:40 +0100265int bssgp_tx_bvc_unblock(struct bssgp_bvc_ctx *bctx)
266{
267 struct msgb *msg = bssgp_msgb_alloc();
268 struct bssgp_normal_hdr *bgph =
269 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200270 uint16_t _bvci = osmo_htons(bctx->bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100271
272 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-BLOCK\n", bctx->bvci);
273
274 msgb_nsei(msg) = bctx->nsei;
275 msgb_bvci(msg) = 0; /* Signalling */
276 bgph->pdu_type = BSSGP_PDUT_BVC_UNBLOCK;
277
278 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
279
280 return gprs_ns_sendmsg(bssgp_nsi, msg);
281}
282
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200283/*! Transmit a BVC-RESET message (Chapter 10.4.12) */
Harald Welte85fc3142011-11-25 08:58:40 +0100284int bssgp_tx_bvc_reset(struct bssgp_bvc_ctx *bctx, uint16_t bvci, uint8_t cause)
285{
286 struct msgb *msg = bssgp_msgb_alloc();
287 struct bssgp_normal_hdr *bgph =
288 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
Harald Weltebfe62e52017-05-15 12:48:30 +0200289 uint16_t _bvci = osmo_htons(bvci);
Harald Welte85fc3142011-11-25 08:58:40 +0100290
291 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP (BVCI=%u) Tx BVC-RESET "
Max2c34ab42016-03-17 15:42:26 +0100292 "CAUSE=%s\n", bvci, bssgp_cause_str(cause));
Harald Welte85fc3142011-11-25 08:58:40 +0100293
294 msgb_nsei(msg) = bctx->nsei;
295 msgb_bvci(msg) = 0; /* Signalling */
296 bgph->pdu_type = BSSGP_PDUT_BVC_RESET;
297
298 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
299 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
300 if (bvci != BVCI_PTM) {
301 uint8_t bssgp_cid[8];
302 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
303 msgb_tvlv_put(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
304 }
305 /* Optional: Feature Bitmap */
306
307 return gprs_ns_sendmsg(bssgp_nsi, msg);
308}
309
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200310/*! Transmit a FLOW_CONTROL-BVC (Chapter 10.4.4)
Harald Weltee92866b2012-09-10 08:56:04 +0200311 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200312 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200313 * \param[in] bucket_size Maximum bucket size in octets
314 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
315 * \param[in] bmax_default_ms Maximum bucket size default for MS
316 * \param[in] r_default_ms Bucket leak rate default for MS in octets/sec
317 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
318 * \param[in] queue_delay_ms Average queuing delay in milliseconds
319 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200320int bssgp_tx_fc_bvc(struct bssgp_bvc_ctx *bctx, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200321 uint32_t bucket_size, uint32_t bucket_leak_rate,
Jacob Erlbeck9385d1e2015-05-06 09:29:32 +0200322 uint32_t bmax_default_ms, uint32_t r_default_ms,
Harald Weltee92866b2012-09-10 08:56:04 +0200323 uint8_t *bucket_full_ratio, uint32_t *queue_delay_ms)
324{
325 struct msgb *msg;
326 struct bssgp_normal_hdr *bgph;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200327 uint16_t e_bucket_size, e_leak_rate, e_bmax_default_ms, e_r_default_ms;
Harald Weltee92866b2012-09-10 08:56:04 +0200328 uint16_t e_queue_delay = 0; /* to make gcc happy */
329
330 if ((bucket_size / 100) > 0xffff)
331 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200332 e_bucket_size = osmo_htons(bucket_size / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200333
334 if ((bucket_leak_rate * 8 / 100) > 0xffff)
335 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200336 e_leak_rate = osmo_htons((bucket_leak_rate * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200337
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200338 if ((bmax_default_ms / 100) > 0xffff)
339 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200340 e_bmax_default_ms = osmo_htons(bmax_default_ms / 100);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200341
Harald Weltee92866b2012-09-10 08:56:04 +0200342 if ((r_default_ms * 8 / 100) > 0xffff)
343 return -EINVAL;
Harald Weltebfe62e52017-05-15 12:48:30 +0200344 e_r_default_ms = osmo_htons((r_default_ms * 8) / 100);
Harald Weltee92866b2012-09-10 08:56:04 +0200345
346 if (queue_delay_ms) {
347 if ((*queue_delay_ms / 10) > 60000)
348 return -EINVAL;
349 else if (*queue_delay_ms == 0xFFFFFFFF)
350 e_queue_delay = 0xFFFF;
351 else
Harald Weltebfe62e52017-05-15 12:48:30 +0200352 e_queue_delay = osmo_htons(*queue_delay_ms / 10);
Harald Weltee92866b2012-09-10 08:56:04 +0200353 }
354
355 msg = bssgp_msgb_alloc();
356 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
357 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200358 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200359 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC;
360
361 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
362 msgb_tvlv_put(msg, BSSGP_IE_BVC_BUCKET_SIZE,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200363 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
Harald Weltee92866b2012-09-10 08:56:04 +0200364 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
365 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
366 msgb_tvlv_put(msg, BSSGP_IE_BMAX_DEFAULT_MS,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200367 sizeof(e_bmax_default_ms),
368 (uint8_t *) &e_bmax_default_ms);
Harald Weltee92866b2012-09-10 08:56:04 +0200369 msgb_tvlv_put(msg, BSSGP_IE_R_DEFAULT_MS,
370 sizeof(e_r_default_ms), (uint8_t *) &e_r_default_ms);
371 if (bucket_full_ratio)
372 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
373 1, bucket_full_ratio);
374 if (queue_delay_ms)
375 msgb_tvlv_put(msg, BSSGP_IE_BVC_MEASUREMENT,
376 sizeof(e_queue_delay),
377 (uint8_t *) &e_queue_delay);
378
379 return gprs_ns_sendmsg(bssgp_nsi, msg);
380}
381
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200382/*! Transmit a FLOW_CONTROL-MS (Chapter 10.4.6)
Harald Weltee92866b2012-09-10 08:56:04 +0200383 * \param[in] bctx BVC Context
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200384 * \param[in] tlli TLLI to identify MS
385 * \param[in] tag Additional tag to identify acknowledge
Harald Weltee92866b2012-09-10 08:56:04 +0200386 * \param[in] ms_bucket_size Maximum bucket size in octets
387 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
388 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
389 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200390int bssgp_tx_fc_ms(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200391 uint32_t ms_bucket_size, uint32_t bucket_leak_rate,
392 uint8_t *bucket_full_ratio)
393{
394 struct msgb *msg;
395 struct bssgp_normal_hdr *bgph;
396 uint16_t e_bucket_size, e_leak_rate;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200397 uint32_t e_tlli;
Harald Weltee92866b2012-09-10 08:56:04 +0200398
399 if ((ms_bucket_size / 100) > 0xffff)
400 return -EINVAL;
401 e_bucket_size = ms_bucket_size / 100;
402
403 if ((bucket_leak_rate * 8 / 100) > 0xffff)
404 return -EINVAL;
405 e_leak_rate = (bucket_leak_rate * 8) / 100;
406
407 msg = bssgp_msgb_alloc();
408 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
409 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200410 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200411 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_MS;
412
Harald Weltebfe62e52017-05-15 12:48:30 +0200413 e_tlli = osmo_htonl(tlli);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200414 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(e_tlli), (uint8_t *)&e_tlli);
Harald Weltee92866b2012-09-10 08:56:04 +0200415 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
416 msgb_tvlv_put(msg, BSSGP_IE_MS_BUCKET_SIZE,
417 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
418 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
419 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
420 if (bucket_full_ratio)
421 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
422 1, bucket_full_ratio);
423
424 return gprs_ns_sendmsg(bssgp_nsi, msg);
425}
Harald Welte85fc3142011-11-25 08:58:40 +0100426
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200427/*! RL-UL-UNITDATA.req (Chapter 10.2.2)
Andreas Eversbergb8d18f32012-09-30 16:59:33 +0200428 * \param[in] bctx BVC Context
429 * \param[in] tlli TLLI to identify MS
430 * \param[in] qos_profile Pointer to three octests of QoS profile
431 * \param[in] llc_pdu msgb pointer containing UL Unitdata IE payload
432 */
Harald Welte85fc3142011-11-25 08:58:40 +0100433int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
434 const uint8_t *qos_profile, struct msgb *llc_pdu)
435{
436 struct msgb *msg = llc_pdu;
437 uint8_t bssgp_cid[8];
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200438 uint8_t bssgp_align[3] = {0, 0, 0};
Harald Welte85fc3142011-11-25 08:58:40 +0100439 struct bssgp_ud_hdr *budh;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200440 int align = sizeof(*budh);
Harald Welte85fc3142011-11-25 08:58:40 +0100441
442 /* FIXME: Optional LSA Identifier List, PFI */
443
444 /* Cell Identifier */
445 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200446 align += 2; /* add T+L */
447 align += sizeof(bssgp_cid);
448
449 /* First push alignment IE */
450 align += 2; /* add T+L */
451 align = (4 - align) & 3; /* how many octest are required to align? */
452 msgb_tvlv_push(msg, BSSGP_IE_ALIGNMENT, align, bssgp_align);
453
454 /* Push other IEs */
Harald Welte85fc3142011-11-25 08:58:40 +0100455 msgb_tvlv_push(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
456
457 /* User Data Header */
458 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
Harald Weltebfe62e52017-05-15 12:48:30 +0200459 budh->tlli = osmo_htonl(tlli);
Harald Welte85fc3142011-11-25 08:58:40 +0100460 memcpy(budh->qos_profile, qos_profile, 3);
461 budh->pdu_type = BSSGP_PDUT_UL_UNITDATA;
462
463 /* set NSEI and BVCI in msgb cb */
464 msgb_nsei(msg) = bctx->nsei;
465 msgb_bvci(msg) = bctx->bvci;
466
467 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
468 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
469
470 return gprs_ns_sendmsg(bssgp_nsi, msg);
471}
472
473/* Parse a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800474int bssgp_rx_paging(struct bssgp_paging_info *pinfo,
475 struct msgb *msg)
Harald Welte85fc3142011-11-25 08:58:40 +0100476{
477 struct bssgp_normal_hdr *bgph =
478 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
479 struct tlv_parsed tp;
480 uint8_t ra[6];
481 int rc, data_len;
482
483 memset(ra, 0, sizeof(ra));
484
485 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
486 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
487 if (rc < 0)
488 goto err_mand_ie;
489
490 switch (bgph->pdu_type) {
491 case BSSGP_PDUT_PAGING_PS:
492 pinfo->mode = BSSGP_PAGING_PS;
493 break;
494 case BSSGP_PDUT_PAGING_CS:
495 pinfo->mode = BSSGP_PAGING_CS;
496 break;
497 default:
498 return -EINVAL;
499 }
500
501 /* IMSI */
502 if (!TLVP_PRESENT(&tp, BSSGP_IE_IMSI))
503 goto err_mand_ie;
504 if (!pinfo->imsi)
Jacob Erlbeckc1cb75e2015-06-18 13:21:30 +0200505 pinfo->imsi = talloc_zero_size(pinfo, GSM_IMSI_LENGTH);
506 gsm48_mi_to_string(pinfo->imsi, GSM_IMSI_LENGTH,
Harald Welte85fc3142011-11-25 08:58:40 +0100507 TLVP_VAL(&tp, BSSGP_IE_IMSI),
508 TLVP_LEN(&tp, BSSGP_IE_IMSI));
509
510 /* DRX Parameters */
511 if (!TLVP_PRESENT(&tp, BSSGP_IE_DRX_PARAMS))
512 goto err_mand_ie;
Harald Weltebfe62e52017-05-15 12:48:30 +0200513 pinfo->drx_params = tlvp_val16be(&tp, BSSGP_IE_DRX_PARAMS);
Harald Welte85fc3142011-11-25 08:58:40 +0100514
515 /* Scope */
516 if (TLVP_PRESENT(&tp, BSSGP_IE_BSS_AREA_ID)) {
517 pinfo->scope = BSSGP_PAGING_BSS_AREA;
518 } else if (TLVP_PRESENT(&tp, BSSGP_IE_LOCATION_AREA)) {
519 pinfo->scope = BSSGP_PAGING_LOCATION_AREA;
520 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_LOCATION_AREA),
521 TLVP_LEN(&tp, BSSGP_IE_LOCATION_AREA));
522 gsm48_parse_ra(&pinfo->raid, ra);
523 } else if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) {
524 pinfo->scope = BSSGP_PAGING_ROUTEING_AREA;
525 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
526 TLVP_LEN(&tp, BSSGP_IE_ROUTEING_AREA));
527 gsm48_parse_ra(&pinfo->raid, ra);
528 } else if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
529 pinfo->scope = BSSGP_PAGING_BVCI;
Harald Weltebfe62e52017-05-15 12:48:30 +0200530 pinfo->bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
Harald Welte85fc3142011-11-25 08:58:40 +0100531 } else
532 return -EINVAL;
533
534 /* QoS profile mandatory for PS */
535 if (pinfo->mode == BSSGP_PAGING_PS) {
536 if (!TLVP_PRESENT(&tp, BSSGP_IE_QOS_PROFILE))
537 goto err_cond_ie;
538 if (TLVP_LEN(&tp, BSSGP_IE_QOS_PROFILE) < 3)
539 goto err;
540
541 memcpy(&pinfo->qos, TLVP_VAL(&tp, BSSGP_IE_QOS_PROFILE),
542 3);
543 }
544
545 /* Optional (P-)TMSI */
546 if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI) &&
Harald Welte6176b6e2016-11-11 15:10:33 +0100547 TLVP_LEN(&tp, BSSGP_IE_TMSI) >= 4) {
Harald Welte85fc3142011-11-25 08:58:40 +0100548 if (!pinfo->ptmsi)
549 pinfo->ptmsi = talloc_zero_size(pinfo, sizeof(uint32_t));
Harald Weltebfe62e52017-05-15 12:48:30 +0200550 *(pinfo->ptmsi) = osmo_load32be(TLVP_VAL(&tp, BSSGP_IE_TMSI));
Harald Welte6176b6e2016-11-11 15:10:33 +0100551 }
Harald Welte85fc3142011-11-25 08:58:40 +0100552
553 return 0;
554
555err_mand_ie:
556err_cond_ie:
557err:
558 /* FIXME */
559 return 0;
560}