blob: 2293f025494920e73ceab169de7f77d123bc6786 [file] [log] [blame]
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +02001/* GPRS utility functions */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010-2014 by On-Waves
5 * (C) 2013 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (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
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22#include <openbsc/gprs_utils.h>
Jacob Erlbeck79af67d2015-01-19 08:27:34 +010023#include <openbsc/gsm_04_08_gprs.h>
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020024
25#include <osmocom/core/msgb.h>
26#include <osmocom/gprs/gprs_ns.h>
27
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020028#include <osmocom/gsm/protocol/gsm_04_08.h>
29
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020030#include <string.h>
31
32/* FIXME: this needs to go to libosmocore/msgb.c */
33struct msgb *gprs_msgb_copy(const struct msgb *msg, const char *name)
34{
35 struct libgb_msgb_cb *old_cb, *new_cb;
36 struct msgb *new_msg;
37
38 new_msg = msgb_alloc(msg->data_len, name);
39 if (!new_msg)
40 return NULL;
41
42 /* copy data */
43 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
44
45 /* copy header */
46 new_msg->len = msg->len;
47 new_msg->data += msg->data - msg->_data;
48 new_msg->head += msg->head - msg->_data;
49 new_msg->tail += msg->tail - msg->_data;
50
Jacob Erlbeck7e31f842014-09-22 18:50:08 +020051 if (msg->l1h)
52 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
53 if (msg->l2h)
54 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
55 if (msg->l3h)
56 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
57 if (msg->l4h)
58 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020059
60 /* copy GB specific data */
61 old_cb = LIBGB_MSGB_CB(msg);
62 new_cb = LIBGB_MSGB_CB(new_msg);
63
Jacob Erlbeck7e31f842014-09-22 18:50:08 +020064 if (old_cb->bssgph)
65 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
66 if (old_cb->llch)
67 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020068
69 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
70 * it a pointer into the new msgb */
Jacob Erlbeck7e31f842014-09-22 18:50:08 +020071 if (old_cb->bssgp_cell_id)
72 new_cb->bssgp_cell_id = new_msg->_data +
73 (old_cb->bssgp_cell_id - msg->_data);
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020074 new_cb->nsei = old_cb->nsei;
75 new_cb->bvci = old_cb->bvci;
76 new_cb->tlli = old_cb->tlli;
77
78 return new_msg;
79}
80
81/* TODO: Move this to libosmocore/msgb.c */
82int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
83 size_t old_size, size_t new_size)
84{
85 int rc;
86 uint8_t *rest = area + old_size;
87 int rest_len = msg->len - old_size - (area - msg->data);
88 int delta_size = (int)new_size - (int)old_size;
89
90 if (delta_size == 0)
91 return 0;
92
93 if (delta_size > 0) {
94 rc = msgb_trim(msg, msg->len + delta_size);
95 if (rc < 0)
96 return rc;
97 }
98
99 memmove(area + new_size, area + old_size, rest_len);
100
101 if (msg->l1h >= rest)
102 msg->l1h += delta_size;
103 if (msg->l2h >= rest)
104 msg->l2h += delta_size;
105 if (msg->l3h >= rest)
106 msg->l3h += delta_size;
107 if (msg->l4h >= rest)
108 msg->l4h += delta_size;
109
110 if (delta_size < 0)
111 msgb_trim(msg, msg->len + delta_size);
112
113 return 0;
114}
115
116/* TODO: Move these conversion functions to a utils file. */
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200117/**
118 * out_str needs to have rest_chars amount of bytes or 1 whatever is bigger.
119 */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200120char * gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t rest_chars)
121{
122 char *str = out_str;
123
124 while (rest_chars > 0 && apn_enc[0]) {
125 size_t label_size = apn_enc[0];
126 if (label_size + 1 > rest_chars)
127 return NULL;
128
129 memmove(str, apn_enc + 1, label_size);
130 str += label_size;
131 rest_chars -= label_size + 1;
132 apn_enc += label_size + 1;
133
134 if (rest_chars)
135 *(str++) = '.';
136 }
137 str[0] = '\0';
138
139 return out_str;
140}
141
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200142int gprs_str_to_apn(uint8_t *apn_enc, size_t max_len, const char *str)
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200143{
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200144 uint8_t *last_len_field;
145 int len;
146
147 /* Can we even write the length field to the output? */
148 if (max_len == 0)
149 return -1;
150
151 /* Remember where we need to put the length once we know it */
152 last_len_field = apn_enc;
153 len = 1;
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200154 apn_enc += 1;
155
156 while (str[0]) {
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200157 if (len >= max_len)
158 return -1;
159
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200160 if (str[0] == '.') {
161 *last_len_field = (apn_enc - last_len_field) - 1;
162 last_len_field = apn_enc;
163 } else {
164 *apn_enc = str[0];
165 }
166 apn_enc += 1;
167 str += 1;
168 len += 1;
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200169 }
170
171 *last_len_field = (apn_enc - last_len_field) - 1;
172
173 return len;
174}
175
Jacob Erlbeck79af67d2015-01-19 08:27:34 +0100176/* GSM 04.08, 10.5.7.3 GPRS Timer */
177int gprs_tmr_to_secs(uint8_t tmr)
178{
179 switch (tmr & GPRS_TMR_UNIT_MASK) {
180 case GPRS_TMR_2SECONDS:
181 return 2 * (tmr & GPRS_TMR_FACT_MASK);
182 default:
183 case GPRS_TMR_MINUTE:
184 return 60 * (tmr & GPRS_TMR_FACT_MASK);
185 case GPRS_TMR_6MINUTE:
186 return 360 * (tmr & GPRS_TMR_FACT_MASK);
187 case GPRS_TMR_DEACTIVATED:
188 return -1;
189 }
190}
191
192/* This functions returns a tmr value such that
193 * - f is monotonic
194 * - f(s) <= s
195 * - f(s) == s if a tmr exists with s = gprs_tmr_to_secs(tmr)
196 * - the best possible resolution is used
197 * where
198 * f(s) = gprs_tmr_to_secs(gprs_secs_to_tmr_floor(s))
199 */
200uint8_t gprs_secs_to_tmr_floor(int secs)
201{
202 if (secs < 0)
203 return GPRS_TMR_DEACTIVATED;
204 if (secs < 2 * 32)
205 return GPRS_TMR_2SECONDS | (secs / 2);
206 if (secs < 60 * 2)
207 /* Ensure monotonicity */
208 return GPRS_TMR_2SECONDS | GPRS_TMR_FACT_MASK;
209 if (secs < 60 * 32)
210 return GPRS_TMR_MINUTE | (secs / 60);
211 if (secs < 360 * 6)
212 /* Ensure monotonicity */
213 return GPRS_TMR_MINUTE | GPRS_TMR_FACT_MASK;
214 if (secs < 360 * 32)
215 return GPRS_TMR_6MINUTE | (secs / 360);
216
217 return GPRS_TMR_6MINUTE | GPRS_TMR_FACT_MASK;
218}
219
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200220/* GSM 04.08, 10.5.1.4 */
221int gprs_is_mi_tmsi(const uint8_t *value, size_t value_len)
222{
223 if (value_len != GSM48_TMSI_LEN)
224 return 0;
225
226 if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_TMSI)
227 return 0;
228
229 return 1;
230}
231
232/* GSM 04.08, 10.5.1.4 */
233int gprs_is_mi_imsi(const uint8_t *value, size_t value_len)
234{
235 if (value_len == 0)
236 return 0;
237
238 if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI)
239 return 0;
240
241 return 1;
242}
243
244int gprs_parse_mi_tmsi(const uint8_t *value, size_t value_len, uint32_t *tmsi)
245{
246 uint32_t tmsi_be;
247
248 if (!gprs_is_mi_tmsi(value, value_len))
249 return 0;
250
251 memcpy(&tmsi_be, value + 1, sizeof(tmsi_be));
252
253 *tmsi = ntohl(tmsi_be);
254 return 1;
255}
256
Jacob Erlbeck49389172014-10-02 16:14:47 +0200257void gprs_parse_tmsi(const uint8_t *value, uint32_t *tmsi)
258{
259 uint32_t tmsi_be;
260
261 memcpy(&tmsi_be, value, sizeof(tmsi_be));
262
263 *tmsi = ntohl(tmsi_be);
264}
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100265
266/* TODO: Move shift functions to libosmocore */
267
268int gprs_shift_v_fixed(uint8_t **data, size_t *data_len,
269 size_t len, uint8_t **value)
270{
271 if (len > *data_len)
272 goto fail;
273
274 if (value)
275 *value = *data;
276
277 *data += len;
278 *data_len -= len;
279
280 return len;
281
282fail:
283 *data += *data_len;
284 *data_len = 0;
285 return -1;
286}
287
288int gprs_match_tv_fixed(uint8_t **data, size_t *data_len,
289 uint8_t tag, size_t len,
290 uint8_t **value)
291{
292 size_t ie_len;
293
294 if (*data_len == 0)
295 goto fail;
296
297 if ((*data)[0] != tag)
298 return 0;
299
300 if (len > *data_len - 1)
301 goto fail;
302
303 if (value)
304 *value = *data + 1;
305
306 ie_len = len + 1;
307 *data += ie_len;
308 *data_len -= ie_len;
309
310 return ie_len;
311
312fail:
313 *data += *data_len;
314 *data_len = 0;
315 return -1;
316}
317
318int gprs_match_tlv(uint8_t **data, size_t *data_len,
Jacob Erlbeck697a5342014-12-11 12:05:29 +0100319 uint8_t expected_tag, uint8_t **value, size_t *value_len)
320{
321 int rc;
322 uint8_t tag;
323 uint8_t *old_data = *data;
324 size_t old_data_len = *data_len;
325
326 rc = gprs_shift_tlv(data, data_len, &tag, value, value_len);
327
328 if (rc > 0 && tag != expected_tag) {
329 *data = old_data;
330 *data_len = old_data_len;
331 return 0;
332 }
333
334 return rc;
335}
336
337int gprs_shift_tlv(uint8_t **data, size_t *data_len,
338 uint8_t *tag, uint8_t **value, size_t *value_len)
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100339{
340 size_t len;
341 size_t ie_len;
342
343 if (*data_len < 2)
344 goto fail;
345
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100346 len = (*data)[1];
347 if (len > *data_len - 2)
348 goto fail;
349
Jacob Erlbeck697a5342014-12-11 12:05:29 +0100350 if (tag)
351 *tag = (*data)[0];
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100352 if (value)
353 *value = *data + 2;
354 if (value_len)
355 *value_len = len;
356
357 ie_len = len + 2;
358
359 *data += ie_len;
360 *data_len -= ie_len;
361
362 return ie_len;
363
364fail:
365 *data += *data_len;
366 *data_len = 0;
367 return -1;
368}
369
370int gprs_shift_lv(uint8_t **data, size_t *data_len,
371 uint8_t **value, size_t *value_len)
372{
373 size_t len;
374 size_t ie_len;
375
376 if (*data_len < 1)
377 goto fail;
378
379 len = (*data)[0];
380 if (len > *data_len - 1)
381 goto fail;
382
383 if (value)
384 *value = *data + 1;
385 if (value_len)
386 *value_len = len;
387
388 ie_len = len + 1;
389 *data += ie_len;
390 *data_len -= ie_len;
391
392 return ie_len;
393
394fail:
395 *data += *data_len;
396 *data_len = 0;
397 return -1;
398}
399