blob: 55d4efda12958ac79041295fd57e6f7bca94e6a9 [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
50 new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
51 new_msg->l2h = new_msg->_data + (msg->l2h - msg->_data);
52 new_msg->l3h = new_msg->_data + (msg->l3h - msg->_data);
53 new_msg->l4h = new_msg->_data + (msg->l4h - msg->_data);
54
55 /* copy GB specific data */
56 old_cb = LIBGB_MSGB_CB(msg);
57 new_cb = LIBGB_MSGB_CB(new_msg);
58
59 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
60 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
61
62 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
63 * it a pointer into the new msgb */
64 new_cb->bssgp_cell_id = new_msg->_data + (old_cb->bssgp_cell_id - msg->_data);
65 new_cb->nsei = old_cb->nsei;
66 new_cb->bvci = old_cb->bvci;
67 new_cb->tlli = old_cb->tlli;
68
69 return new_msg;
70}
71
72/* TODO: Move this to libosmocore/msgb.c */
73int gprs_msgb_resize_area(struct msgb *msg, uint8_t *area,
74 size_t old_size, size_t new_size)
75{
76 int rc;
77 uint8_t *rest = area + old_size;
78 int rest_len = msg->len - old_size - (area - msg->data);
79 int delta_size = (int)new_size - (int)old_size;
80
81 if (delta_size == 0)
82 return 0;
83
84 if (delta_size > 0) {
85 rc = msgb_trim(msg, msg->len + delta_size);
86 if (rc < 0)
87 return rc;
88 }
89
90 memmove(area + new_size, area + old_size, rest_len);
91
92 if (msg->l1h >= rest)
93 msg->l1h += delta_size;
94 if (msg->l2h >= rest)
95 msg->l2h += delta_size;
96 if (msg->l3h >= rest)
97 msg->l3h += delta_size;
98 if (msg->l4h >= rest)
99 msg->l4h += delta_size;
100
101 if (delta_size < 0)
102 msgb_trim(msg, msg->len + delta_size);
103
104 return 0;
105}
106
107/* TODO: Move these conversion functions to a utils file. */
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200108/**
109 * out_str needs to have rest_chars amount of bytes or 1 whatever is bigger.
110 */
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200111char * gprs_apn_to_str(char *out_str, const uint8_t *apn_enc, size_t rest_chars)
112{
113 char *str = out_str;
114
115 while (rest_chars > 0 && apn_enc[0]) {
116 size_t label_size = apn_enc[0];
117 if (label_size + 1 > rest_chars)
118 return NULL;
119
120 memmove(str, apn_enc + 1, label_size);
121 str += label_size;
122 rest_chars -= label_size + 1;
123 apn_enc += label_size + 1;
124
125 if (rest_chars)
126 *(str++) = '.';
127 }
128 str[0] = '\0';
129
130 return out_str;
131}
132
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200133int gprs_str_to_apn(uint8_t *apn_enc, size_t max_len, const char *str)
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200134{
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200135 uint8_t *last_len_field;
136 int len;
137
138 /* Can we even write the length field to the output? */
139 if (max_len == 0)
140 return -1;
141
142 /* Remember where we need to put the length once we know it */
143 last_len_field = apn_enc;
144 len = 1;
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200145 apn_enc += 1;
146
147 while (str[0]) {
Holger Hans Peter Freytherce1b22e2014-08-04 14:22:13 +0200148 if (len >= max_len)
149 return -1;
150
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200151 if (str[0] == '.') {
152 *last_len_field = (apn_enc - last_len_field) - 1;
153 last_len_field = apn_enc;
154 } else {
155 *apn_enc = str[0];
156 }
157 apn_enc += 1;
158 str += 1;
159 len += 1;
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200160 }
161
162 *last_len_field = (apn_enc - last_len_field) - 1;
163
164 return len;
165}
166
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200167/* GSM 04.08, 10.5.1.4 */
168int gprs_is_mi_tmsi(const uint8_t *value, size_t value_len)
169{
170 if (value_len != GSM48_TMSI_LEN)
171 return 0;
172
173 if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_TMSI)
174 return 0;
175
176 return 1;
177}
178
179/* GSM 04.08, 10.5.1.4 */
180int gprs_is_mi_imsi(const uint8_t *value, size_t value_len)
181{
182 if (value_len == 0)
183 return 0;
184
185 if (!value || (value[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI)
186 return 0;
187
188 return 1;
189}
190
191int gprs_parse_mi_tmsi(const uint8_t *value, size_t value_len, uint32_t *tmsi)
192{
193 uint32_t tmsi_be;
194
195 if (!gprs_is_mi_tmsi(value, value_len))
196 return 0;
197
198 memcpy(&tmsi_be, value + 1, sizeof(tmsi_be));
199
200 *tmsi = ntohl(tmsi_be);
201 return 1;
202}
203