blob: c3d187db694081b0c6dcfbb9224832b7037109ec [file] [log] [blame]
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +01001/* GPRS Subscriber Update Protocol message encoder/decoder */
2
3/*
4 * (C) 2014 by Sysmocom s.f.m.c. GmbH
5 * All Rights Reserved
6 *
7 * Author: Jacob Erlbeck
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <openbsc/gprs_gsup_messages.h>
25
26#include <openbsc/debug.h>
27#include <openbsc/gprs_utils.h>
28
29#include <osmocom/gsm/tlv.h>
30#include <osmocom/core/msgb.h>
31
32#include <stdint.h>
33
34
35static uint64_t decode_big_endian(const uint8_t *data, size_t data_len)
36{
37 uint64_t value = 0;
38
39 while (data_len > 0) {
40 value = (value << 8) + *data;
41 data += 1;
42 data_len -= 1;
43 }
44
45 return value;
46}
47
48static uint8_t *encode_big_endian(uint64_t value, size_t data_len)
49{
50 static uint8_t buf[sizeof(uint64_t)];
51 int idx;
52
53 OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
54
55 for (idx = data_len - 1; idx >= 0; idx--) {
56 buf[idx] = (uint8_t)value;
57 value = value >> 8;
58 }
59
60 return buf;
61}
62
63static int decode_pdp_info(uint8_t *data, size_t data_len,
64 struct gprs_gsup_pdp_info *pdp_info)
65{
66 int rc;
67 uint8_t tag;
68 uint8_t *value;
69 size_t value_len;
70
71 /* specific parts */
72 while (data_len > 0) {
73 enum gprs_gsup_iei iei;
74
75 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
76 if (rc < 0)
77 return rc;
78
79 iei = tag;
80
81 switch (iei) {
82 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
83 pdp_info->context_id = decode_big_endian(value, value_len);
84 break;
85
86 case GPRS_GSUP_PDP_TYPE_IE:
87 pdp_info->pdp_type =
88 decode_big_endian(value, value_len) & 0x0fff;
89 break;
90
91 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
92 pdp_info->apn_enc = value;
93 pdp_info->apn_enc_len = value_len;
94 break;
95
96 default:
97 LOGP(DGPRS, LOGL_ERROR,
98 "GSUP IE type %d not expected in PDP info\n", iei);
99 continue;
100 }
101 }
102
103 return 0;
104}
105
106static int decode_auth_info(uint8_t *data, size_t data_len,
107 struct gsm_auth_tuple *auth_tuple)
108{
109 int rc;
110 uint8_t tag;
111 uint8_t *value;
112 size_t value_len;
113 enum gprs_gsup_iei iei;
114
115 /* specific parts */
116 while (data_len > 0) {
117 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
118 if (rc < 0)
119 return rc;
120
121 iei = tag;
122
123 switch (iei) {
124 case GPRS_GSUP_RAND_IE:
125 if (value_len != sizeof(auth_tuple->rand))
126 goto parse_error;
127
128 memcpy(auth_tuple->rand, value, value_len);
129 break;
130
131 case GPRS_GSUP_SRES_IE:
132 if (value_len != sizeof(auth_tuple->sres))
133 goto parse_error;
134
135 memcpy(auth_tuple->sres, value, value_len);
136 break;
137
138 case GPRS_GSUP_KC_IE:
139 if (value_len != sizeof(auth_tuple->kc))
140 goto parse_error;
141
142 memcpy(auth_tuple->kc, value, value_len);
143 break;
144
145 default:
146 LOGP(DGPRS, LOGL_ERROR,
147 "GSUP IE type %d not expected in PDP info\n", iei);
148 continue;
149 }
150 }
151
152 return 0;
153
154parse_error:
155 LOGP(DGPRS, LOGL_ERROR,
156 "GSUP IE type %d, length %d invalid in PDP info\n", iei, value_len);
157
158 return -1;
159}
160
161int gprs_gsup_decode(const uint8_t *const_data, size_t data_len,
162 struct gprs_gsup_message *gsup_msg)
163{
164 int rc;
165 uint8_t tag;
166 /* the shift/match functions expect non-const pointers, but we'll
167 * either copy the data or cast pointers back to const before returning
168 * them
169 */
170 uint8_t *data = (uint8_t *)const_data;
171 uint8_t *value;
172 size_t value_len;
173 static const struct gprs_gsup_pdp_info empty_pdp_info = {0};
174 static const struct gsm_auth_tuple empty_auth_info = {0};
175
176 /* generic part */
177 gprs_shift_v_fixed(&data, &data_len, 1, &value);
178 gsup_msg->message_type = decode_big_endian(value, 1);
179
180 rc = gprs_match_tlv(&data, &data_len, GPRS_GSUP_IMSI_IE,
181 &value, &value_len);
182
183 if (rc <= 0)
184 return -GMM_CAUSE_INV_MAND_INFO;
185
186 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
187 return -GMM_CAUSE_INV_MAND_INFO;
188
189 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
190 * octets in the first octet. By coincidence (the TLV encoding) the byte
191 * before the value part already contains this length so we can use it
192 * here.
193 */
194 OSMO_ASSERT(value[-1] == value_len);
195 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
196 value - 1, 0);
197
198 /* specific parts */
199 while (data_len > 0) {
200 enum gprs_gsup_iei iei;
201 struct gprs_gsup_pdp_info pdp_info;
202 struct gsm_auth_tuple auth_info;
203
204 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
205 if (rc < 0)
206 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
207
208 iei = tag;
209
210 switch (iei) {
211 case GPRS_GSUP_IMSI_IE:
212 case GPRS_GSUP_PDP_TYPE_IE:
213 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
214 case GPRS_GSUP_RAND_IE:
215 case GPRS_GSUP_SRES_IE:
216 case GPRS_GSUP_KC_IE:
217 LOGP(DGPRS, LOGL_NOTICE,
218 "GSUP IE type %d not expected (ignored)\n", iei);
219 continue;
220
221 case GPRS_GSUP_CAUSE_IE:
222 gsup_msg->cause = decode_big_endian(value, value_len);
223 break;
224
225 case GPRS_GSUP_CANCEL_TYPE_IE:
226 gsup_msg->cancel_type =
227 decode_big_endian(value, value_len) + 1;
228 break;
229
230 case GPRS_GSUP_PDP_INFO_COMPL_IE:
231 gsup_msg->pdp_info_compl = 1;
232 break;
233
234 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
235 /* When these IE appear in the top-level part of the
236 * message, they are used by Delete Subscr Info to delete
237 * single entries. We don't have an extra list for
238 * these but use the PDP info list instead */
239
240 /* fall through */
241
242 case GPRS_GSUP_PDP_INFO_IE:
243 if (gsup_msg->num_pdp_infos >= GPRS_GSUP_MAX_NUM_PDP_INFO) {
244 LOGP(DGPRS, LOGL_ERROR,
245 "GSUP IE type %d (PDP_INFO) max exceeded\n",
246 iei);
247 return -GMM_CAUSE_COND_IE_ERR;
248 }
249
250 memcpy(&pdp_info, &empty_pdp_info, sizeof(pdp_info));
251
252 if (iei == GPRS_GSUP_PDP_INFO_IE) {
253 rc = decode_pdp_info(value, value_len, &pdp_info);
254 if (rc < 0)
255 return rc;
256 pdp_info.have_info = 1;
257 } else {
258 pdp_info.context_id =
259 decode_big_endian(value, value_len);
260 }
261
262 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
263 pdp_info;
264 break;
265
266 case GPRS_GSUP_AUTH_TUPLE_IE:
267 if (gsup_msg->num_auth_tuples >= GPRS_GSUP_MAX_NUM_AUTH_INFO) {
268 LOGP(DGPRS, LOGL_ERROR,
269 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
270 iei);
271 return -GMM_CAUSE_INV_MAND_INFO;
272 }
273
274 memcpy(&auth_info, &empty_auth_info, sizeof(auth_info));
275 auth_info.key_seq = gsup_msg->num_auth_tuples;
276
277 rc = decode_auth_info(value, value_len, &auth_info);
278 if (rc < 0)
279 return rc;
280
281 gsup_msg->auth_tuples[gsup_msg->num_auth_tuples++] =
282 auth_info;
283 break;
284 default:
285 LOGP(DGPRS, LOGL_NOTICE,
286 "GSUP IE type %d unknown\n", iei);
287 continue;
288 }
289 }
290
291 return 0;
292}
293
294static void encode_pdp_info(struct msgb *msg, enum gprs_gsup_iei iei,
295 const struct gprs_gsup_pdp_info *pdp_info)
296{
297 uint8_t *len_field;
298 size_t old_len;
299 uint8_t u8;
300
301 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
302 old_len = msgb_length(msg);
303
304 u8 = pdp_info->context_id;
305 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
306
307 if (pdp_info->pdp_type) {
308 msgb_tlv_put(msg, GPRS_GSUP_PDP_TYPE_IE,
309 GPRS_GSUP_PDP_TYPE_SIZE,
310 encode_big_endian(pdp_info->pdp_type | 0xf000,
311 GPRS_GSUP_PDP_TYPE_SIZE));
312 }
313
314 if (pdp_info->apn_enc) {
315 msgb_tlv_put(msg, GPRS_GSUP_ACCESS_POINT_NAME_IE,
316 pdp_info->apn_enc_len, pdp_info->apn_enc);
317 }
318
319 /* Update length field */
320 *len_field = msgb_length(msg) - old_len;
321}
322
323static void encode_auth_info(struct msgb *msg, enum gprs_gsup_iei iei,
324 const struct gsm_auth_tuple *auth_tuple)
325{
326 uint8_t *len_field;
327 size_t old_len;
328
329 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
330 old_len = msgb_length(msg);
331
332 msgb_tlv_put(msg, GPRS_GSUP_RAND_IE,
333 sizeof(auth_tuple->rand), auth_tuple->rand);
334
335 msgb_tlv_put(msg, GPRS_GSUP_SRES_IE,
336 sizeof(auth_tuple->sres), auth_tuple->sres);
337
338 msgb_tlv_put(msg, GPRS_GSUP_KC_IE,
339 sizeof(auth_tuple->kc), auth_tuple->kc);
340
341 /* Update length field */
342 *len_field = msgb_length(msg) - old_len;
343}
344
345void gprs_gsup_encode(struct msgb *msg, const struct gprs_gsup_message *gsup_msg)
346{
347 uint8_t u8;
348 int idx;
349 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
350 size_t bcd_len;
351
352 /* generic part */
353 OSMO_ASSERT(gsup_msg->message_type);
354 msgb_v_put(msg, gsup_msg->message_type);
355
356 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
357 gsup_msg->imsi);
358
359 OSMO_ASSERT(bcd_len > 1);
360
361 /* Note that gsm48_encode_bcd_number puts the length into the first
362 * octet. Since msgb_tlv_put will add this length byte, we'll have to
363 * skip it */
364 msgb_tlv_put(msg, GPRS_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
365
366 /* specific parts */
367 if ((u8 = gsup_msg->cause))
368 msgb_tlv_put(msg, GPRS_GSUP_CAUSE_IE, sizeof(u8), &u8);
369
370 if ((u8 = gsup_msg->cancel_type)) {
371 u8 -= 1;
372 msgb_tlv_put(msg, GPRS_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
373 }
374
375 if (gsup_msg->pdp_info_compl)
376 msgb_tlv_put(msg, GPRS_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
377
378 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
379 const struct gprs_gsup_pdp_info *pdp_info;
380
381 pdp_info = &gsup_msg->pdp_infos[idx];
382
383 if (pdp_info->context_id == 0)
384 continue;
385
386 if (pdp_info->have_info) {
387 encode_pdp_info(msg, GPRS_GSUP_PDP_INFO_IE, pdp_info);
388 } else {
389 u8 = pdp_info->context_id;
390 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE,
391 sizeof(u8), &u8);
392 }
393 }
394
395 for (idx = 0; idx < gsup_msg->num_auth_tuples; idx++) {
396 const struct gsm_auth_tuple *auth_info;
397
398 auth_info = &gsup_msg->auth_tuples[idx];
399
400 if (auth_info->key_seq == GSM_KEY_SEQ_INVAL)
401 continue;
402
403 encode_auth_info(msg, GPRS_GSUP_AUTH_TUPLE_IE, auth_info);
404 }
405}