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