blob: 9cec844e14bff91d613265e4ec71233f10386615 [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
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (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
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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
308 * \param[in] bucket_size Maximum bucket size in octets
309 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
310 * \param[in] bmax_default_ms Maximum bucket size default for MS
311 * \param[in] r_default_ms Bucket leak rate default for MS in octets/sec
312 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
313 * \param[in] queue_delay_ms Average queuing delay in milliseconds
314 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200315int bssgp_tx_fc_bvc(struct bssgp_bvc_ctx *bctx, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200316 uint32_t bucket_size, uint32_t bucket_leak_rate,
317 uint16_t bmax_default_ms, uint32_t r_default_ms,
318 uint8_t *bucket_full_ratio, uint32_t *queue_delay_ms)
319{
320 struct msgb *msg;
321 struct bssgp_normal_hdr *bgph;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200322 uint16_t e_bucket_size, e_leak_rate, e_bmax_default_ms, e_r_default_ms;
Harald Weltee92866b2012-09-10 08:56:04 +0200323 uint16_t e_queue_delay = 0; /* to make gcc happy */
324
325 if ((bucket_size / 100) > 0xffff)
326 return -EINVAL;
327 e_bucket_size = bucket_size / 100;
328
329 if ((bucket_leak_rate * 8 / 100) > 0xffff)
330 return -EINVAL;
331 e_leak_rate = (bucket_leak_rate * 8) / 100;
332
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200333 if ((bmax_default_ms / 100) > 0xffff)
334 return -EINVAL;
335 e_bmax_default_ms = bmax_default_ms / 100;
336
Harald Weltee92866b2012-09-10 08:56:04 +0200337 if ((r_default_ms * 8 / 100) > 0xffff)
338 return -EINVAL;
339 e_r_default_ms = (r_default_ms * 8) / 100;
340
341 if (queue_delay_ms) {
342 if ((*queue_delay_ms / 10) > 60000)
343 return -EINVAL;
344 else if (*queue_delay_ms == 0xFFFFFFFF)
345 e_queue_delay = 0xFFFF;
346 else
347 e_queue_delay = *queue_delay_ms / 10;
348 }
349
350 msg = bssgp_msgb_alloc();
351 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
352 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200353 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200354 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_BVC;
355
356 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
357 msgb_tvlv_put(msg, BSSGP_IE_BVC_BUCKET_SIZE,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200358 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
Harald Weltee92866b2012-09-10 08:56:04 +0200359 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
360 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
361 msgb_tvlv_put(msg, BSSGP_IE_BMAX_DEFAULT_MS,
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200362 sizeof(e_bmax_default_ms),
363 (uint8_t *) &e_bmax_default_ms);
Harald Weltee92866b2012-09-10 08:56:04 +0200364 msgb_tvlv_put(msg, BSSGP_IE_R_DEFAULT_MS,
365 sizeof(e_r_default_ms), (uint8_t *) &e_r_default_ms);
366 if (bucket_full_ratio)
367 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
368 1, bucket_full_ratio);
369 if (queue_delay_ms)
370 msgb_tvlv_put(msg, BSSGP_IE_BVC_MEASUREMENT,
371 sizeof(e_queue_delay),
372 (uint8_t *) &e_queue_delay);
373
374 return gprs_ns_sendmsg(bssgp_nsi, msg);
375}
376
377/*! \brief Transmit a FLOW_CONTROL-MS (Chapter 10.4.6)
378 * \param[in] bctx BVC Context
379 * \param[in] ms_bucket_size Maximum bucket size in octets
380 * \param[in] bucket_leak_rate Bucket leak rate in octets/sec
381 * \param[in] bucket_full_ratio Ratio (in percent) of queue filling
382 */
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200383int bssgp_tx_fc_ms(struct bssgp_bvc_ctx *bctx, uint32_t tlli, uint8_t tag,
Harald Weltee92866b2012-09-10 08:56:04 +0200384 uint32_t ms_bucket_size, uint32_t bucket_leak_rate,
385 uint8_t *bucket_full_ratio)
386{
387 struct msgb *msg;
388 struct bssgp_normal_hdr *bgph;
389 uint16_t e_bucket_size, e_leak_rate;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200390 uint32_t e_tlli;
Harald Weltee92866b2012-09-10 08:56:04 +0200391
392 if ((ms_bucket_size / 100) > 0xffff)
393 return -EINVAL;
394 e_bucket_size = ms_bucket_size / 100;
395
396 if ((bucket_leak_rate * 8 / 100) > 0xffff)
397 return -EINVAL;
398 e_leak_rate = (bucket_leak_rate * 8) / 100;
399
400 msg = bssgp_msgb_alloc();
401 bgph = (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
402 msgb_nsei(msg) = bctx->nsei;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200403 msgb_bvci(msg) = bctx->bvci;
Harald Weltee92866b2012-09-10 08:56:04 +0200404 bgph->pdu_type = BSSGP_PDUT_FLOW_CONTROL_MS;
405
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200406 e_tlli = htonl(tlli);
407 msgb_tvlv_put(msg, BSSGP_IE_TLLI, sizeof(e_tlli), (uint8_t *)&e_tlli);
Harald Weltee92866b2012-09-10 08:56:04 +0200408 msgb_tvlv_put(msg, BSSGP_IE_TAG, sizeof(tag), (uint8_t *)&tag);
409 msgb_tvlv_put(msg, BSSGP_IE_MS_BUCKET_SIZE,
410 sizeof(e_bucket_size), (uint8_t *) &e_bucket_size);
411 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_LEAK_RATE,
412 sizeof(e_leak_rate), (uint8_t *) &e_leak_rate);
413 if (bucket_full_ratio)
414 msgb_tvlv_put(msg, BSSGP_IE_BUCKET_FULL_RATIO,
415 1, bucket_full_ratio);
416
417 return gprs_ns_sendmsg(bssgp_nsi, msg);
418}
Harald Welte85fc3142011-11-25 08:58:40 +0100419
420/*! \brief RL-UL-UNITDATA.req (Chapter 10.2.2) */
421int bssgp_tx_ul_ud(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
422 const uint8_t *qos_profile, struct msgb *llc_pdu)
423{
424 struct msgb *msg = llc_pdu;
425 uint8_t bssgp_cid[8];
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200426 uint8_t bssgp_align[3] = {0, 0, 0};
Harald Welte85fc3142011-11-25 08:58:40 +0100427 struct bssgp_ud_hdr *budh;
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200428 int align = sizeof(*budh);
Harald Welte85fc3142011-11-25 08:58:40 +0100429
430 /* FIXME: Optional LSA Identifier List, PFI */
431
432 /* Cell Identifier */
433 bssgp_create_cell_id(bssgp_cid, &bctx->ra_id, bctx->cell_id);
Andreas Eversbergf44ed8c2012-09-23 06:05:20 +0200434 align += 2; /* add T+L */
435 align += sizeof(bssgp_cid);
436
437 /* First push alignment IE */
438 align += 2; /* add T+L */
439 align = (4 - align) & 3; /* how many octest are required to align? */
440 msgb_tvlv_push(msg, BSSGP_IE_ALIGNMENT, align, bssgp_align);
441
442 /* Push other IEs */
Harald Welte85fc3142011-11-25 08:58:40 +0100443 msgb_tvlv_push(msg, BSSGP_IE_CELL_ID, sizeof(bssgp_cid), bssgp_cid);
444
445 /* User Data Header */
446 budh = (struct bssgp_ud_hdr *) msgb_push(msg, sizeof(*budh));
447 budh->tlli = htonl(tlli);
448 memcpy(budh->qos_profile, qos_profile, 3);
449 budh->pdu_type = BSSGP_PDUT_UL_UNITDATA;
450
451 /* set NSEI and BVCI in msgb cb */
452 msgb_nsei(msg) = bctx->nsei;
453 msgb_bvci(msg) = bctx->bvci;
454
455 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_OUT]);
456 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_OUT], msg->len);
457
458 return gprs_ns_sendmsg(bssgp_nsi, msg);
459}
460
461/* Parse a single GMM-PAGING.req to a given NSEI/NS-BVCI */
Harald Weltede4599c2012-06-17 13:04:02 +0800462int bssgp_rx_paging(struct bssgp_paging_info *pinfo,
463 struct msgb *msg)
Harald Welte85fc3142011-11-25 08:58:40 +0100464{
465 struct bssgp_normal_hdr *bgph =
466 (struct bssgp_normal_hdr *) msgb_bssgph(msg);
467 struct tlv_parsed tp;
468 uint8_t ra[6];
469 int rc, data_len;
470
471 memset(ra, 0, sizeof(ra));
472
473 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
474 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
475 if (rc < 0)
476 goto err_mand_ie;
477
478 switch (bgph->pdu_type) {
479 case BSSGP_PDUT_PAGING_PS:
480 pinfo->mode = BSSGP_PAGING_PS;
481 break;
482 case BSSGP_PDUT_PAGING_CS:
483 pinfo->mode = BSSGP_PAGING_CS;
484 break;
485 default:
486 return -EINVAL;
487 }
488
489 /* IMSI */
490 if (!TLVP_PRESENT(&tp, BSSGP_IE_IMSI))
491 goto err_mand_ie;
492 if (!pinfo->imsi)
493 pinfo->imsi = talloc_zero_size(pinfo, 16);
494 gsm48_mi_to_string(pinfo->imsi, sizeof(pinfo->imsi),
495 TLVP_VAL(&tp, BSSGP_IE_IMSI),
496 TLVP_LEN(&tp, BSSGP_IE_IMSI));
497
498 /* DRX Parameters */
499 if (!TLVP_PRESENT(&tp, BSSGP_IE_DRX_PARAMS))
500 goto err_mand_ie;
501 pinfo->drx_params = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_DRX_PARAMS));
502
503 /* Scope */
504 if (TLVP_PRESENT(&tp, BSSGP_IE_BSS_AREA_ID)) {
505 pinfo->scope = BSSGP_PAGING_BSS_AREA;
506 } else if (TLVP_PRESENT(&tp, BSSGP_IE_LOCATION_AREA)) {
507 pinfo->scope = BSSGP_PAGING_LOCATION_AREA;
508 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_LOCATION_AREA),
509 TLVP_LEN(&tp, BSSGP_IE_LOCATION_AREA));
510 gsm48_parse_ra(&pinfo->raid, ra);
511 } else if (TLVP_PRESENT(&tp, BSSGP_IE_ROUTEING_AREA)) {
512 pinfo->scope = BSSGP_PAGING_ROUTEING_AREA;
513 memcpy(ra, TLVP_VAL(&tp, BSSGP_IE_ROUTEING_AREA),
514 TLVP_LEN(&tp, BSSGP_IE_ROUTEING_AREA));
515 gsm48_parse_ra(&pinfo->raid, ra);
516 } else if (TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
517 pinfo->scope = BSSGP_PAGING_BVCI;
518 pinfo->bvci = ntohs(*(uint16_t *)TLVP_VAL(&tp, BSSGP_IE_BVCI));
519 } else
520 return -EINVAL;
521
522 /* QoS profile mandatory for PS */
523 if (pinfo->mode == BSSGP_PAGING_PS) {
524 if (!TLVP_PRESENT(&tp, BSSGP_IE_QOS_PROFILE))
525 goto err_cond_ie;
526 if (TLVP_LEN(&tp, BSSGP_IE_QOS_PROFILE) < 3)
527 goto err;
528
529 memcpy(&pinfo->qos, TLVP_VAL(&tp, BSSGP_IE_QOS_PROFILE),
530 3);
531 }
532
533 /* Optional (P-)TMSI */
534 if (TLVP_PRESENT(&tp, BSSGP_IE_TMSI) &&
535 TLVP_LEN(&tp, BSSGP_IE_TMSI) >= 4)
536 if (!pinfo->ptmsi)
537 pinfo->ptmsi = talloc_zero_size(pinfo, sizeof(uint32_t));
538 *(pinfo->ptmsi) = ntohl(*(uint32_t *)
539 TLVP_VAL(&tp, BSSGP_IE_TMSI));
540
541 return 0;
542
543err_mand_ie:
544err_cond_ie:
545err:
546 /* FIXME */
547 return 0;
548}