blob: 780198c3f1b3f08310dd8523d07373d429c3ceb5 [file] [log] [blame]
Neels Hofmeyr665d48b2016-11-14 14:17:14 +01001/* Osmocom Generic Subscriber Update Protocol message encoder/decoder */
Harald Welte3b6fb082016-04-25 18:46:22 +02002
3/*
Neels Hofmeyr5f460de2016-12-08 16:23:05 +01004 * (C) 2014 by sysmocom s.f.m.c. GmbH
Harald Welte3b6fb082016-04-25 18:46:22 +02005 * (C) 2015 by Holger Hans Peter Freyther
6 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
7 * All Rights Reserved
8 *
9 * Author: Jacob Erlbeck
10 *
11 * This program is free software; you can redistribute it and/or modify
Neels Hofmeyr5f460de2016-12-08 16:23:05 +010012 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
Harald Welte3b6fb082016-04-25 18:46:22 +020014 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Neels Hofmeyr5f460de2016-12-08 16:23:05 +010019 * GNU General Public License for more details.
Harald Welte3b6fb082016-04-25 18:46:22 +020020 *
Neels Hofmeyr5f460de2016-12-08 16:23:05 +010021 * You should have received a copy of the GNU General Public License
Harald Welte3b6fb082016-04-25 18:46:22 +020022 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#include <osmocom/gsm/tlv.h>
27#include <osmocom/core/msgb.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/gsm/gsm48_ie.h>
30#include <osmocom/gsm/gsup.h>
31
32#include <stdint.h>
33
Harald Welte96e2a002017-06-12 21:44:18 +020034/*! \addtogroup gsup
35 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036 * Osmocom Generic Subscriber Update Protocol
Harald Welte96e2a002017-06-12 21:44:18 +020037 */
38
Neels Hofmeyr10f5fb42017-02-09 02:09:09 +010039const struct value_string osmo_gsup_message_type_names[] = {
40 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST),
41 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR),
42 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT),
43
44 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST),
45 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR),
46 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT),
47
48 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_AUTH_FAIL_REPORT),
49
50 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PURGE_MS_REQUEST),
51 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PURGE_MS_ERROR),
52 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PURGE_MS_RESULT),
53
54 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_INSERT_DATA_REQUEST),
55 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_INSERT_DATA_ERROR),
56 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_INSERT_DATA_RESULT),
57
58 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_DELETE_DATA_REQUEST),
59 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_DELETE_DATA_ERROR),
60 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_DELETE_DATA_RESULT),
61
62 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST),
63 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR),
64 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT),
65 { 0, NULL }
66};
67
Harald Welte3b6fb082016-04-25 18:46:22 +020068static int decode_pdp_info(uint8_t *data, size_t data_len,
69 struct osmo_gsup_pdp_info *pdp_info)
70{
71 int rc;
72 uint8_t tag;
73 uint8_t *value;
74 size_t value_len;
75
76 /* specific parts */
77 while (data_len > 0) {
78 enum osmo_gsup_iei iei;
79
80 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
81 if (rc < 0)
82 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
83
84 iei = tag;
85
86 switch (iei) {
87 case OSMO_GSUP_PDP_CONTEXT_ID_IE:
88 pdp_info->context_id = osmo_decode_big_endian(value, value_len);
89 break;
90
91 case OSMO_GSUP_PDP_TYPE_IE:
92 pdp_info->pdp_type =
93 osmo_decode_big_endian(value, value_len) & 0x0fff;
94 break;
95
96 case OSMO_GSUP_ACCESS_POINT_NAME_IE:
97 pdp_info->apn_enc = value;
98 pdp_info->apn_enc_len = value_len;
99 break;
100
101 case OSMO_GSUP_PDP_QOS_IE:
102 pdp_info->qos_enc = value;
103 pdp_info->qos_enc_len = value_len;
104 break;
105
106 default:
107 LOGP(DLGSUP, LOGL_ERROR,
108 "GSUP IE type %d not expected in PDP info\n", iei);
109 continue;
110 }
111 }
112
113 return 0;
114}
115
116static int decode_auth_info(uint8_t *data, size_t data_len,
117 struct osmo_auth_vector *auth_vector)
118{
119 int rc;
120 uint8_t tag;
121 uint8_t *value;
122 size_t value_len;
123 enum osmo_gsup_iei iei;
124 uint8_t presence = 0;
125
126 /* specific parts */
127 while (data_len > 0) {
128 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
129 if (rc < 0)
130 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
131
132 iei = tag;
133
134 switch (iei) {
135 case OSMO_GSUP_RAND_IE:
136 if (value_len != sizeof(auth_vector->rand))
137 goto parse_error;
138
139 memcpy(auth_vector->rand, value, value_len);
140 presence |= (1 << 0);
141 break;
142
143 case OSMO_GSUP_SRES_IE:
144 if (value_len != sizeof(auth_vector->sres))
145 goto parse_error;
146
147 memcpy(auth_vector->sres, value, value_len);
148 presence |= (1 << 1);
149 break;
150
151 case OSMO_GSUP_KC_IE:
152 if (value_len != sizeof(auth_vector->kc))
153 goto parse_error;
154
155 memcpy(auth_vector->kc, value, value_len);
156 presence |= (1 << 2);
157 break;
158
159 case OSMO_GSUP_IK_IE:
160 if (value_len != sizeof(auth_vector->ik))
161 goto parse_error;
162 memcpy(auth_vector->ik, value, value_len);
163 presence |= (1 << 4);
164 break;
165
166 case OSMO_GSUP_CK_IE:
167 if (value_len != sizeof(auth_vector->ck))
168 goto parse_error;
169 memcpy(auth_vector->ck, value, value_len);
170 presence |= (1 << 5);
171 break;
172
173 case OSMO_GSUP_AUTN_IE:
174 if (value_len != sizeof(auth_vector->autn))
175 goto parse_error;
176 memcpy(auth_vector->autn, value, value_len);
177 presence |= (1 << 6);
178 break;
179 case OSMO_GSUP_RES_IE:
180 if (value_len > sizeof(auth_vector->res))
181 goto parse_error;
182 memcpy(auth_vector->res, value, value_len);
183 auth_vector->res_len = value_len;
184 presence |= (1 << 7);
185 break;
186
187 default:
188 LOGP(DLGSUP, LOGL_ERROR,
189 "GSUP IE type %d not expected in PDP info\n", iei);
190 continue;
191 }
192 }
193
194 if (presence & 0x07)
195 auth_vector->auth_types |= OSMO_AUTH_TYPE_GSM;
196 if (presence & 0xf0)
197 auth_vector->auth_types |= OSMO_AUTH_TYPE_UMTS;
198
199 return 0;
200
201parse_error:
202 LOGP(DLGSUP, LOGL_ERROR,
203 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
204
205 return -1;
206}
207
208/*! Decode (parse) a GSUP message
209 * \param[in] const_data input data to be parsed
210 * \param[in] data_len length of input (\a const_data)
211 * \param[out] gsup_msg callee-allocated output data structure
212 * \returns 0 on success; negative otherwise
213 */
214int osmo_gsup_decode(const uint8_t *const_data, size_t data_len,
215 struct osmo_gsup_message *gsup_msg)
216{
217 int rc;
218 uint8_t tag;
219 /* the shift/match functions expect non-const pointers, but we'll
220 * either copy the data or cast pointers back to const before returning
221 * them
222 */
223 uint8_t *data = (uint8_t *)const_data;
224 uint8_t *value;
225 size_t value_len;
226 static const struct osmo_gsup_pdp_info empty_pdp_info = {0};
Neels Hofmeyr505adee2016-07-13 16:55:43 +0200227 static const struct osmo_auth_vector empty_auth_info = {{0}};
Harald Welte3b6fb082016-04-25 18:46:22 +0200228 static const struct osmo_gsup_message empty_gsup_message = {0};
229
230 *gsup_msg = empty_gsup_message;
231
232 /* generic part */
233 rc = osmo_shift_v_fixed(&data, &data_len, 1, &value);
234 if (rc < 0)
235 return -GMM_CAUSE_INV_MAND_INFO;
236
237 gsup_msg->message_type = osmo_decode_big_endian(value, 1);
238
239 rc = osmo_match_shift_tlv(&data, &data_len, OSMO_GSUP_IMSI_IE,
240 &value, &value_len);
241
242 if (rc <= 0)
243 return -GMM_CAUSE_INV_MAND_INFO;
244
245 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
246 return -GMM_CAUSE_INV_MAND_INFO;
247
248 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
249 * octets in the first octet. By coincidence (the TLV encoding) the byte
250 * before the value part already contains this length so we can use it
251 * here.
252 */
253 OSMO_ASSERT(value[-1] == value_len);
254 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
255 value - 1, 0);
256
257 /* specific parts */
258 while (data_len > 0) {
259 enum osmo_gsup_iei iei;
260 struct osmo_gsup_pdp_info pdp_info;
261 struct osmo_auth_vector auth_info;
262
263 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
264 if (rc < 0)
265 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
266
267 iei = tag;
268
269 switch (iei) {
270 case OSMO_GSUP_IMSI_IE:
271 case OSMO_GSUP_PDP_TYPE_IE:
272 case OSMO_GSUP_ACCESS_POINT_NAME_IE:
Harald Welte3b6fb082016-04-25 18:46:22 +0200273 case OSMO_GSUP_SRES_IE:
274 case OSMO_GSUP_KC_IE:
275 LOGP(DLGSUP, LOGL_NOTICE,
276 "GSUP IE type %d not expected (ignored)\n", iei);
277 continue;
278
279 case OSMO_GSUP_CAUSE_IE:
280 gsup_msg->cause = osmo_decode_big_endian(value, value_len);
281 break;
282
283 case OSMO_GSUP_CANCEL_TYPE_IE:
284 gsup_msg->cancel_type =
285 osmo_decode_big_endian(value, value_len) + 1;
286 break;
287
288 case OSMO_GSUP_PDP_INFO_COMPL_IE:
289 gsup_msg->pdp_info_compl = 1;
290 break;
291
292 case OSMO_GSUP_FREEZE_PTMSI_IE:
293 gsup_msg->freeze_ptmsi = 1;
294 break;
295
296 case OSMO_GSUP_PDP_CONTEXT_ID_IE:
297 /* When these IE appear in the top-level part of the
298 * message, they are used by Delete Subscr Info to delete
299 * single entries. We don't have an extra list for
300 * these but use the PDP info list instead */
301
302 /* fall through */
303
304 case OSMO_GSUP_PDP_INFO_IE:
305 if (gsup_msg->num_pdp_infos >= OSMO_GSUP_MAX_NUM_PDP_INFO) {
306 LOGP(DLGSUP, LOGL_ERROR,
307 "GSUP IE type %d (PDP_INFO) max exceeded\n",
308 iei);
309 return -GMM_CAUSE_COND_IE_ERR;
310 }
311
312 pdp_info = empty_pdp_info;
313
314 if (iei == OSMO_GSUP_PDP_INFO_IE) {
315 rc = decode_pdp_info(value, value_len, &pdp_info);
316 if (rc < 0)
317 return rc;
318 pdp_info.have_info = 1;
319 } else {
320 pdp_info.context_id =
321 osmo_decode_big_endian(value, value_len);
322 }
323
324 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
325 pdp_info;
326 break;
327
328 case OSMO_GSUP_AUTH_TUPLE_IE:
329 if (gsup_msg->num_auth_vectors >= OSMO_GSUP_MAX_NUM_AUTH_INFO) {
330 LOGP(DLGSUP, LOGL_ERROR,
331 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
332 iei);
333 return -GMM_CAUSE_INV_MAND_INFO;
334 }
335
336 auth_info = empty_auth_info;
337
338 rc = decode_auth_info(value, value_len, &auth_info);
339 if (rc < 0)
340 return rc;
341
342 gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] =
343 auth_info;
344 break;
345
346 case OSMO_GSUP_AUTS_IE:
Neels Hofmeyr3a5ca642017-02-21 15:53:20 +0100347 if (value_len != 14) {
Harald Welte3b6fb082016-04-25 18:46:22 +0200348 LOGP(DLGSUP, LOGL_ERROR,
Neels Hofmeyr3a5ca642017-02-21 15:53:20 +0100349 "AUTS length != 14 received\n");
Harald Welte3b6fb082016-04-25 18:46:22 +0200350 return -GMM_CAUSE_COND_IE_ERR;
351 }
352 gsup_msg->auts = value;
353 break;
354
Harald Welte766da862016-05-06 11:18:15 +0200355 case OSMO_GSUP_RAND_IE:
356 if (value_len != 16) {
357 LOGP(DLGSUP, LOGL_ERROR,
358 "RAND length != 16 received\n");
359 return -GMM_CAUSE_COND_IE_ERR;
360 }
361 gsup_msg->rand = value;
362 break;
363
Harald Welte3b6fb082016-04-25 18:46:22 +0200364 case OSMO_GSUP_MSISDN_IE:
365 gsup_msg->msisdn_enc = value;
366 gsup_msg->msisdn_enc_len = value_len;
367 break;
368
369 case OSMO_GSUP_HLR_NUMBER_IE:
370 gsup_msg->hlr_enc = value;
371 gsup_msg->hlr_enc_len = value_len;
372 break;
373
Harald Welte48dc1a52016-05-05 18:46:42 +0200374 case OSMO_GSUP_CN_DOMAIN_IE:
375 gsup_msg->cn_domain = *value;
376 break;
377
Harald Welte3b6fb082016-04-25 18:46:22 +0200378 default:
379 LOGP(DLGSUP, LOGL_NOTICE,
380 "GSUP IE type %d unknown\n", iei);
381 continue;
382 }
383 }
384
385 return 0;
386}
387
388static void encode_pdp_info(struct msgb *msg, enum osmo_gsup_iei iei,
389 const struct osmo_gsup_pdp_info *pdp_info)
390{
391 uint8_t *len_field;
392 size_t old_len;
393 uint8_t u8;
394
395 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
396 old_len = msgb_length(msg);
397
398 u8 = pdp_info->context_id;
399 msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
400
401 if (pdp_info->pdp_type) {
402 msgb_tlv_put(msg, OSMO_GSUP_PDP_TYPE_IE,
403 OSMO_GSUP_PDP_TYPE_SIZE,
404 osmo_encode_big_endian(pdp_info->pdp_type | 0xf000,
405 OSMO_GSUP_PDP_TYPE_SIZE));
406 }
407
408 if (pdp_info->apn_enc) {
409 msgb_tlv_put(msg, OSMO_GSUP_ACCESS_POINT_NAME_IE,
410 pdp_info->apn_enc_len, pdp_info->apn_enc);
411 }
412
413 if (pdp_info->qos_enc) {
414 msgb_tlv_put(msg, OSMO_GSUP_PDP_QOS_IE,
415 pdp_info->qos_enc_len, pdp_info->qos_enc);
416 }
417
418 /* Update length field */
419 *len_field = msgb_length(msg) - old_len;
420}
421
422static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei,
423 const struct osmo_auth_vector *auth_vector)
424{
425 uint8_t *len_field;
426 size_t old_len;
427
428 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
429 old_len = msgb_length(msg);
430
Harald Weltedb78d212016-06-06 13:47:07 +0200431 if (auth_vector->auth_types & OSMO_AUTH_TYPE_GSM) {
432 msgb_tlv_put(msg, OSMO_GSUP_RAND_IE,
433 sizeof(auth_vector->rand), auth_vector->rand);
Harald Welte3b6fb082016-04-25 18:46:22 +0200434
Harald Weltedb78d212016-06-06 13:47:07 +0200435 msgb_tlv_put(msg, OSMO_GSUP_SRES_IE,
436 sizeof(auth_vector->sres), auth_vector->sres);
Harald Welte3b6fb082016-04-25 18:46:22 +0200437
Harald Weltedb78d212016-06-06 13:47:07 +0200438 msgb_tlv_put(msg, OSMO_GSUP_KC_IE,
439 sizeof(auth_vector->kc), auth_vector->kc);
440 }
441
442 if (auth_vector->auth_types & OSMO_AUTH_TYPE_UMTS) {
443 msgb_tlv_put(msg, OSMO_GSUP_IK_IE,
444 sizeof(auth_vector->ik), auth_vector->ik);
445
446 msgb_tlv_put(msg, OSMO_GSUP_CK_IE,
447 sizeof(auth_vector->ck), auth_vector->ck);
448
449 msgb_tlv_put(msg, OSMO_GSUP_AUTN_IE,
450 sizeof(auth_vector->autn), auth_vector->autn);
451
452 msgb_tlv_put(msg, OSMO_GSUP_RES_IE,
453 auth_vector->res_len, auth_vector->res);
454 }
Harald Welte3b6fb082016-04-25 18:46:22 +0200455
456 /* Update length field */
457 *len_field = msgb_length(msg) - old_len;
458}
459
460/*! Encode a GSUP message
461 * \param[out] msg message buffer to which encoded message is written
462 * \param[in] gsup_msg \ref osmo_gsup_message data to be encoded
463 */
464void osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg)
465{
466 uint8_t u8;
467 int idx;
468 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
469 size_t bcd_len;
470
471 /* generic part */
472 OSMO_ASSERT(gsup_msg->message_type);
473 msgb_v_put(msg, gsup_msg->message_type);
474
475 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
476 gsup_msg->imsi);
477
478 OSMO_ASSERT(bcd_len > 1);
Harald Welte1fa78562016-11-26 10:14:53 +0100479 OSMO_ASSERT(bcd_len <= sizeof(bcd_buf));
Harald Welte3b6fb082016-04-25 18:46:22 +0200480
481 /* Note that gsm48_encode_bcd_number puts the length into the first
482 * octet. Since msgb_tlv_put will add this length byte, we'll have to
483 * skip it */
484 msgb_tlv_put(msg, OSMO_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
485
486 /* specific parts */
487 if (gsup_msg->msisdn_enc)
488 msgb_tlv_put(msg, OSMO_GSUP_MSISDN_IE,
489 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
490 if (gsup_msg->hlr_enc)
491 msgb_tlv_put(msg, OSMO_GSUP_HLR_NUMBER_IE,
492 gsup_msg->hlr_enc_len, gsup_msg->hlr_enc);
493
494 if ((u8 = gsup_msg->cause))
495 msgb_tlv_put(msg, OSMO_GSUP_CAUSE_IE, sizeof(u8), &u8);
496
497 if ((u8 = gsup_msg->cancel_type)) {
498 u8 -= 1;
499 msgb_tlv_put(msg, OSMO_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
500 }
501
502 if (gsup_msg->pdp_info_compl)
503 msgb_tlv_put(msg, OSMO_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
504
505 if (gsup_msg->freeze_ptmsi)
506 msgb_tlv_put(msg, OSMO_GSUP_FREEZE_PTMSI_IE, 0, &u8);
507
508 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
509 const struct osmo_gsup_pdp_info *pdp_info;
510
511 pdp_info = &gsup_msg->pdp_infos[idx];
512
513 if (pdp_info->context_id == 0)
514 continue;
515
516 if (pdp_info->have_info) {
517 encode_pdp_info(msg, OSMO_GSUP_PDP_INFO_IE, pdp_info);
518 } else {
519 u8 = pdp_info->context_id;
520 msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE,
521 sizeof(u8), &u8);
522 }
523 }
524
525 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
526 const struct osmo_auth_vector *auth_vector;
527
528 auth_vector = &gsup_msg->auth_vectors[idx];
529
530 encode_auth_info(msg, OSMO_GSUP_AUTH_TUPLE_IE, auth_vector);
531 }
532
533 if (gsup_msg->auts)
Neels Hofmeyr8352d312017-02-02 20:05:14 +0100534 msgb_tlv_put(msg, OSMO_GSUP_AUTS_IE, 14, gsup_msg->auts);
Harald Welte48dc1a52016-05-05 18:46:42 +0200535
Harald Welte766da862016-05-06 11:18:15 +0200536 if (gsup_msg->rand)
537 msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, 16, gsup_msg->rand);
538
Harald Welte48dc1a52016-05-05 18:46:42 +0200539 if (gsup_msg->cn_domain) {
540 uint8_t dn = gsup_msg->cn_domain;
541 msgb_tlv_put(msg, OSMO_GSUP_CN_DOMAIN_IE, 1, &dn);
542 }
Harald Welte3b6fb082016-04-25 18:46:22 +0200543}
Harald Welte96e2a002017-06-12 21:44:18 +0200544
545/*! @} */