blob: 02e14e79471b99034c5a26e5c01ab82cbe0f8061 [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 */
Jacob Erlbeck424ffa42015-01-12 13:23:05 +0100177 rc = gprs_shift_v_fixed(&data, &data_len, 1, &value);
178 if (rc < 0)
179 return -GMM_CAUSE_INV_MAND_INFO;
180
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100181 gsup_msg->message_type = decode_big_endian(value, 1);
182
183 rc = gprs_match_tlv(&data, &data_len, GPRS_GSUP_IMSI_IE,
184 &value, &value_len);
185
186 if (rc <= 0)
187 return -GMM_CAUSE_INV_MAND_INFO;
188
189 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
190 return -GMM_CAUSE_INV_MAND_INFO;
191
192 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
193 * octets in the first octet. By coincidence (the TLV encoding) the byte
194 * before the value part already contains this length so we can use it
195 * here.
196 */
197 OSMO_ASSERT(value[-1] == value_len);
198 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
199 value - 1, 0);
200
201 /* specific parts */
202 while (data_len > 0) {
203 enum gprs_gsup_iei iei;
204 struct gprs_gsup_pdp_info pdp_info;
205 struct gsm_auth_tuple auth_info;
206
207 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
208 if (rc < 0)
209 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
210
211 iei = tag;
212
213 switch (iei) {
214 case GPRS_GSUP_IMSI_IE:
215 case GPRS_GSUP_PDP_TYPE_IE:
216 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
217 case GPRS_GSUP_RAND_IE:
218 case GPRS_GSUP_SRES_IE:
219 case GPRS_GSUP_KC_IE:
220 LOGP(DGPRS, LOGL_NOTICE,
221 "GSUP IE type %d not expected (ignored)\n", iei);
222 continue;
223
224 case GPRS_GSUP_CAUSE_IE:
225 gsup_msg->cause = decode_big_endian(value, value_len);
226 break;
227
228 case GPRS_GSUP_CANCEL_TYPE_IE:
229 gsup_msg->cancel_type =
230 decode_big_endian(value, value_len) + 1;
231 break;
232
233 case GPRS_GSUP_PDP_INFO_COMPL_IE:
234 gsup_msg->pdp_info_compl = 1;
235 break;
236
237 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
238 /* When these IE appear in the top-level part of the
239 * message, they are used by Delete Subscr Info to delete
240 * single entries. We don't have an extra list for
241 * these but use the PDP info list instead */
242
243 /* fall through */
244
245 case GPRS_GSUP_PDP_INFO_IE:
246 if (gsup_msg->num_pdp_infos >= GPRS_GSUP_MAX_NUM_PDP_INFO) {
247 LOGP(DGPRS, LOGL_ERROR,
248 "GSUP IE type %d (PDP_INFO) max exceeded\n",
249 iei);
250 return -GMM_CAUSE_COND_IE_ERR;
251 }
252
253 memcpy(&pdp_info, &empty_pdp_info, sizeof(pdp_info));
254
255 if (iei == GPRS_GSUP_PDP_INFO_IE) {
256 rc = decode_pdp_info(value, value_len, &pdp_info);
257 if (rc < 0)
258 return rc;
259 pdp_info.have_info = 1;
260 } else {
261 pdp_info.context_id =
262 decode_big_endian(value, value_len);
263 }
264
265 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
266 pdp_info;
267 break;
268
269 case GPRS_GSUP_AUTH_TUPLE_IE:
270 if (gsup_msg->num_auth_tuples >= GPRS_GSUP_MAX_NUM_AUTH_INFO) {
271 LOGP(DGPRS, LOGL_ERROR,
272 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
273 iei);
274 return -GMM_CAUSE_INV_MAND_INFO;
275 }
276
277 memcpy(&auth_info, &empty_auth_info, sizeof(auth_info));
278 auth_info.key_seq = gsup_msg->num_auth_tuples;
279
280 rc = decode_auth_info(value, value_len, &auth_info);
281 if (rc < 0)
282 return rc;
283
284 gsup_msg->auth_tuples[gsup_msg->num_auth_tuples++] =
285 auth_info;
286 break;
287 default:
288 LOGP(DGPRS, LOGL_NOTICE,
289 "GSUP IE type %d unknown\n", iei);
290 continue;
291 }
292 }
293
294 return 0;
295}
296
297static void encode_pdp_info(struct msgb *msg, enum gprs_gsup_iei iei,
298 const struct gprs_gsup_pdp_info *pdp_info)
299{
300 uint8_t *len_field;
301 size_t old_len;
302 uint8_t u8;
303
304 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
305 old_len = msgb_length(msg);
306
307 u8 = pdp_info->context_id;
308 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
309
310 if (pdp_info->pdp_type) {
311 msgb_tlv_put(msg, GPRS_GSUP_PDP_TYPE_IE,
312 GPRS_GSUP_PDP_TYPE_SIZE,
313 encode_big_endian(pdp_info->pdp_type | 0xf000,
314 GPRS_GSUP_PDP_TYPE_SIZE));
315 }
316
317 if (pdp_info->apn_enc) {
318 msgb_tlv_put(msg, GPRS_GSUP_ACCESS_POINT_NAME_IE,
319 pdp_info->apn_enc_len, pdp_info->apn_enc);
320 }
321
322 /* Update length field */
323 *len_field = msgb_length(msg) - old_len;
324}
325
326static void encode_auth_info(struct msgb *msg, enum gprs_gsup_iei iei,
327 const struct gsm_auth_tuple *auth_tuple)
328{
329 uint8_t *len_field;
330 size_t old_len;
331
332 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
333 old_len = msgb_length(msg);
334
335 msgb_tlv_put(msg, GPRS_GSUP_RAND_IE,
336 sizeof(auth_tuple->rand), auth_tuple->rand);
337
338 msgb_tlv_put(msg, GPRS_GSUP_SRES_IE,
339 sizeof(auth_tuple->sres), auth_tuple->sres);
340
341 msgb_tlv_put(msg, GPRS_GSUP_KC_IE,
342 sizeof(auth_tuple->kc), auth_tuple->kc);
343
344 /* Update length field */
345 *len_field = msgb_length(msg) - old_len;
346}
347
348void gprs_gsup_encode(struct msgb *msg, const struct gprs_gsup_message *gsup_msg)
349{
350 uint8_t u8;
351 int idx;
352 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
353 size_t bcd_len;
354
355 /* generic part */
356 OSMO_ASSERT(gsup_msg->message_type);
357 msgb_v_put(msg, gsup_msg->message_type);
358
359 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
360 gsup_msg->imsi);
361
362 OSMO_ASSERT(bcd_len > 1);
363
364 /* Note that gsm48_encode_bcd_number puts the length into the first
365 * octet. Since msgb_tlv_put will add this length byte, we'll have to
366 * skip it */
367 msgb_tlv_put(msg, GPRS_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
368
369 /* specific parts */
370 if ((u8 = gsup_msg->cause))
371 msgb_tlv_put(msg, GPRS_GSUP_CAUSE_IE, sizeof(u8), &u8);
372
373 if ((u8 = gsup_msg->cancel_type)) {
374 u8 -= 1;
375 msgb_tlv_put(msg, GPRS_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
376 }
377
378 if (gsup_msg->pdp_info_compl)
379 msgb_tlv_put(msg, GPRS_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
380
381 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
382 const struct gprs_gsup_pdp_info *pdp_info;
383
384 pdp_info = &gsup_msg->pdp_infos[idx];
385
386 if (pdp_info->context_id == 0)
387 continue;
388
389 if (pdp_info->have_info) {
390 encode_pdp_info(msg, GPRS_GSUP_PDP_INFO_IE, pdp_info);
391 } else {
392 u8 = pdp_info->context_id;
393 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE,
394 sizeof(u8), &u8);
395 }
396 }
397
398 for (idx = 0; idx < gsup_msg->num_auth_tuples; idx++) {
399 const struct gsm_auth_tuple *auth_info;
400
401 auth_info = &gsup_msg->auth_tuples[idx];
402
403 if (auth_info->key_seq == GSM_KEY_SEQ_INVAL)
404 continue;
405
406 encode_auth_info(msg, GPRS_GSUP_AUTH_TUPLE_IE, auth_info);
407 }
408}