blob: cec07d7ecdb23565447a485588b72bc725b94c51 [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>
23
24#include <osmocom/core/msgb.h>
25#include <osmocom/gprs/gprs_ns.h>
26
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020027#include <osmocom/gsm/protocol/gsm_04_08.h>
28
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020029#include <string.h>
30
31/* FIXME: this needs to go to libosmocore/msgb.c */
32struct msgb *gprs_msgb_copy(const struct msgb *msg, const char *name)
33{
34 struct libgb_msgb_cb *old_cb, *new_cb;
35 struct msgb *new_msg;
36
37 new_msg = msgb_alloc(msg->data_len, name);
38 if (!new_msg)
39 return NULL;
40
41 /* copy data */
42 memcpy(new_msg->_data, msg->_data, new_msg->data_len);
43
44 /* copy header */
45 new_msg->len = msg->len;
46 new_msg->data += msg->data - msg->_data;
47 new_msg->head += msg->head - msg->_data;
48 new_msg->tail += msg->tail - msg->_data;
49
Jacob Erlbeck7e31f842014-09-22 18:50:08 +020050 if (msg->l1h)
51 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
52 if (msg->l2h)
53 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
54 if (msg->l3h)
55 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
56 if (msg->l4h)
57 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020058
59 /* copy GB specific data */
60 old_cb = LIBGB_MSGB_CB(msg);
61 new_cb = LIBGB_MSGB_CB(new_msg);
62
Jacob Erlbeck7e31f842014-09-22 18:50:08 +020063 if (old_cb->bssgph)
64 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
65 if (old_cb->llch)
66 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020067
68 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
69 * it a pointer into the new msgb */
Jacob Erlbeck7e31f842014-09-22 18:50:08 +020070 if (old_cb->bssgp_cell_id)
71 new_cb->bssgp_cell_id = new_msg->_data +
72 (old_cb->bssgp_cell_id - msg->_data);
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020073 new_cb->nsei = old_cb->nsei;
74 new_cb->bvci = old_cb->bvci;
75 new_cb->tlli = old_cb->tlli;
76
77 return new_msg;
78}
79
80/* TODO: Move this to libosmocore/msgb.c */
81int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
82 size_t old_size, size_t new_size)
83{
84 int rc;
85 uint8_t *rest = area + old_size;
86 int rest_len = msg->len - old_size - (area - msg->data);
87 int delta_size = (int)new_size - (int)old_size;
88
89 if (delta_size == 0)
90 return 0;
91
92 if (delta_size > 0) {
93 rc = msgb_trim(msg, msg->len + delta_size);
94 if (rc < 0)
95 return rc;
96 }
97
98 memmove(area + new_size, area + old_size, rest_len);
99
100 if (msg->l1h >= rest)
101 msg->l1h += delta_size;
102 if (msg->l2h >= rest)
103 msg->l2h += delta_size;
104 if (msg->l3h >= rest)
105 msg->l3h += delta_size;
106 if (msg->l4h >= rest)
107 msg->l4h += delta_size;
108
109 if (delta_size < 0)
110 msgb_trim(msg, msg->len + delta_size);
111
112 return 0;
113}
114
115/* TODO: Move these conversion functions to a utils file. */
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200116/**
117 * out_str needs to have rest_chars amount of bytes or 1 whatever is bigger.
118 */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200119char * gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t rest_chars)
120{
121 char *str = out_str;
122
123 while (rest_chars > 0 && apn_enc[0]) {
124 size_t label_size = apn_enc[0];
125 if (label_size + 1 > rest_chars)
126 return NULL;
127
128 memmove(str, apn_enc + 1, label_size);
129 str += label_size;
130 rest_chars -= label_size + 1;
131 apn_enc += label_size + 1;
132
133 if (rest_chars)
134 *(str++) = '.';
135 }
136 str[0] = '\0';
137
138 return out_str;
139}
140
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200141int gprs_str_to_apn(uint8_t *apn_enc, size_t max_len, const char *str)
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200142{
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200143 uint8_t *last_len_field;
144 int len;
145
146 /* Can we even write the length field to the output? */
147 if (max_len == 0)
148 return -1;
149
150 /* Remember where we need to put the length once we know it */
151 last_len_field = apn_enc;
152 len = 1;
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200153 apn_enc += 1;
154
155 while (str[0]) {
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200156 if (len >= max_len)
157 return -1;
158
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200159 if (str[0] == '.') {
160 *last_len_field = (apn_enc - last_len_field) - 1;
161 last_len_field = apn_enc;
162 } else {
163 *apn_enc = str[0];
164 }
165 apn_enc += 1;
166 str += 1;
167 len += 1;
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200168 }
169
170 *last_len_field = (apn_enc - last_len_field) - 1;
171
172 return len;
173}
174
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200175/* GSM 04.08, 10.5.1.4 */
176int gprs_is_mi_tmsi(const uint8_t *value, size_t value_len)
177{
178 if (value_len != GSM48_TMSI_LEN)
179 return 0;
180
181 if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_TMSI)
182 return 0;
183
184 return 1;
185}
186
187/* GSM 04.08, 10.5.1.4 */
188int gprs_is_mi_imsi(const uint8_t *value, size_t value_len)
189{
190 if (value_len == 0)
191 return 0;
192
193 if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI)
194 return 0;
195
196 return 1;
197}
198
199int gprs_parse_mi_tmsi(const uint8_t *value, size_t value_len, uint32_t *tmsi)
200{
201 uint32_t tmsi_be;
202
203 if (!gprs_is_mi_tmsi(value, value_len))
204 return 0;
205
206 memcpy(&tmsi_be, value + 1, sizeof(tmsi_be));
207
208 *tmsi = ntohl(tmsi_be);
209 return 1;
210}
211
Jacob Erlbeck49389172014-10-02 16:14:47 +0200212void gprs_parse_tmsi(const uint8_t *value, uint32_t *tmsi)
213{
214 uint32_t tmsi_be;
215
216 memcpy(&tmsi_be, value, sizeof(tmsi_be));
217
218 *tmsi = ntohl(tmsi_be);
219}
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100220
221/* TODO: Move shift functions to libosmocore */
222
223int gprs_shift_v_fixed(uint8_t **data, size_t *data_len,
224 size_t len, uint8_t **value)
225{
226 if (len > *data_len)
227 goto fail;
228
229 if (value)
230 *value = *data;
231
232 *data += len;
233 *data_len -= len;
234
235 return len;
236
237fail:
238 *data += *data_len;
239 *data_len = 0;
240 return -1;
241}
242
243int gprs_match_tv_fixed(uint8_t **data, size_t *data_len,
244 uint8_t tag, size_t len,
245 uint8_t **value)
246{
247 size_t ie_len;
248
249 if (*data_len == 0)
250 goto fail;
251
252 if ((*data)[0] != tag)
253 return 0;
254
255 if (len > *data_len - 1)
256 goto fail;
257
258 if (value)
259 *value = *data + 1;
260
261 ie_len = len + 1;
262 *data += ie_len;
263 *data_len -= ie_len;
264
265 return ie_len;
266
267fail:
268 *data += *data_len;
269 *data_len = 0;
270 return -1;
271}
272
273int gprs_match_tlv(uint8_t **data, size_t *data_len,
274 uint8_t tag, uint8_t **value, size_t *value_len)
275{
276 size_t len;
277 size_t ie_len;
278
279 if (*data_len < 2)
280 goto fail;
281
282 if ((*data)[0] != tag)
283 return 0;
284
285 len = (*data)[1];
286 if (len > *data_len - 2)
287 goto fail;
288
289 if (value)
290 *value = *data + 2;
291 if (value_len)
292 *value_len = len;
293
294 ie_len = len + 2;
295
296 *data += ie_len;
297 *data_len -= ie_len;
298
299 return ie_len;
300
301fail:
302 *data += *data_len;
303 *data_len = 0;
304 return -1;
305}
306
307int gprs_shift_lv(uint8_t **data, size_t *data_len,
308 uint8_t **value, size_t *value_len)
309{
310 size_t len;
311 size_t ie_len;
312
313 if (*data_len < 1)
314 goto fail;
315
316 len = (*data)[0];
317 if (len > *data_len - 1)
318 goto fail;
319
320 if (value)
321 *value = *data + 1;
322 if (value_len)
323 *value_len = len;
324
325 ie_len = len + 1;
326 *data += ie_len;
327 *data_len -= ie_len;
328
329 return ie_len;
330
331fail:
332 *data += *data_len;
333 *data_len = 0;
334 return -1;
335}
336