blob: c6204548020ad8e4112f97edc3ea2ac409f187ba [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