blob: 244c5fa8e212b9ab87803deb61bfa73cd69ad8a9 [file] [log] [blame]
Harald Welte3b6fb082016-04-25 18:46:22 +02001/* Osmocom Subscriber Update Protocol message encoder/decoder */
2
3/*
4 * (C) 2014 by Sysmocom s.f.m.c. GmbH
5 * (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
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (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
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * 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
34static int decode_pdp_info(uint8_t *data, size_t data_len,
35 struct osmo_gsup_pdp_info *pdp_info)
36{
37 int rc;
38 uint8_t tag;
39 uint8_t *value;
40 size_t value_len;
41
42 /* specific parts */
43 while (data_len > 0) {
44 enum osmo_gsup_iei iei;
45
46 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
47 if (rc < 0)
48 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
49
50 iei = tag;
51
52 switch (iei) {
53 case OSMO_GSUP_PDP_CONTEXT_ID_IE:
54 pdp_info->context_id = osmo_decode_big_endian(value, value_len);
55 break;
56
57 case OSMO_GSUP_PDP_TYPE_IE:
58 pdp_info->pdp_type =
59 osmo_decode_big_endian(value, value_len) & 0x0fff;
60 break;
61
62 case OSMO_GSUP_ACCESS_POINT_NAME_IE:
63 pdp_info->apn_enc = value;
64 pdp_info->apn_enc_len = value_len;
65 break;
66
67 case OSMO_GSUP_PDP_QOS_IE:
68 pdp_info->qos_enc = value;
69 pdp_info->qos_enc_len = value_len;
70 break;
71
72 default:
73 LOGP(DLGSUP, LOGL_ERROR,
74 "GSUP IE type %d not expected in PDP info\n", iei);
75 continue;
76 }
77 }
78
79 return 0;
80}
81
82static int decode_auth_info(uint8_t *data, size_t data_len,
83 struct osmo_auth_vector *auth_vector)
84{
85 int rc;
86 uint8_t tag;
87 uint8_t *value;
88 size_t value_len;
89 enum osmo_gsup_iei iei;
90 uint8_t presence = 0;
91
92 /* specific parts */
93 while (data_len > 0) {
94 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
95 if (rc < 0)
96 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
97
98 iei = tag;
99
100 switch (iei) {
101 case OSMO_GSUP_RAND_IE:
102 if (value_len != sizeof(auth_vector->rand))
103 goto parse_error;
104
105 memcpy(auth_vector->rand, value, value_len);
106 presence |= (1 << 0);
107 break;
108
109 case OSMO_GSUP_SRES_IE:
110 if (value_len != sizeof(auth_vector->sres))
111 goto parse_error;
112
113 memcpy(auth_vector->sres, value, value_len);
114 presence |= (1 << 1);
115 break;
116
117 case OSMO_GSUP_KC_IE:
118 if (value_len != sizeof(auth_vector->kc))
119 goto parse_error;
120
121 memcpy(auth_vector->kc, value, value_len);
122 presence |= (1 << 2);
123 break;
124
125 case OSMO_GSUP_IK_IE:
126 if (value_len != sizeof(auth_vector->ik))
127 goto parse_error;
128 memcpy(auth_vector->ik, value, value_len);
129 presence |= (1 << 4);
130 break;
131
132 case OSMO_GSUP_CK_IE:
133 if (value_len != sizeof(auth_vector->ck))
134 goto parse_error;
135 memcpy(auth_vector->ck, value, value_len);
136 presence |= (1 << 5);
137 break;
138
139 case OSMO_GSUP_AUTN_IE:
140 if (value_len != sizeof(auth_vector->autn))
141 goto parse_error;
142 memcpy(auth_vector->autn, value, value_len);
143 presence |= (1 << 6);
144 break;
145 case OSMO_GSUP_RES_IE:
146 if (value_len > sizeof(auth_vector->res))
147 goto parse_error;
148 memcpy(auth_vector->res, value, value_len);
149 auth_vector->res_len = value_len;
150 presence |= (1 << 7);
151 break;
152
153 default:
154 LOGP(DLGSUP, LOGL_ERROR,
155 "GSUP IE type %d not expected in PDP info\n", iei);
156 continue;
157 }
158 }
159
160 if (presence & 0x07)
161 auth_vector->auth_types |= OSMO_AUTH_TYPE_GSM;
162 if (presence & 0xf0)
163 auth_vector->auth_types |= OSMO_AUTH_TYPE_UMTS;
164
165 return 0;
166
167parse_error:
168 LOGP(DLGSUP, LOGL_ERROR,
169 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
170
171 return -1;
172}
173
174/*! Decode (parse) a GSUP message
175 * \param[in] const_data input data to be parsed
176 * \param[in] data_len length of input (\a const_data)
177 * \param[out] gsup_msg callee-allocated output data structure
178 * \returns 0 on success; negative otherwise
179 */
180int osmo_gsup_decode(const uint8_t *const_data, size_t data_len,
181 struct osmo_gsup_message *gsup_msg)
182{
183 int rc;
184 uint8_t tag;
185 /* the shift/match functions expect non-const pointers, but we'll
186 * either copy the data or cast pointers back to const before returning
187 * them
188 */
189 uint8_t *data = (uint8_t *)const_data;
190 uint8_t *value;
191 size_t value_len;
192 static const struct osmo_gsup_pdp_info empty_pdp_info = {0};
193 static const struct osmo_auth_vector empty_auth_info = {0};
194 static const struct osmo_gsup_message empty_gsup_message = {0};
195
196 *gsup_msg = empty_gsup_message;
197
198 /* generic part */
199 rc = osmo_shift_v_fixed(&data, &data_len, 1, &value);
200 if (rc < 0)
201 return -GMM_CAUSE_INV_MAND_INFO;
202
203 gsup_msg->message_type = osmo_decode_big_endian(value, 1);
204
205 rc = osmo_match_shift_tlv(&data, &data_len, OSMO_GSUP_IMSI_IE,
206 &value, &value_len);
207
208 if (rc <= 0)
209 return -GMM_CAUSE_INV_MAND_INFO;
210
211 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
212 return -GMM_CAUSE_INV_MAND_INFO;
213
214 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
215 * octets in the first octet. By coincidence (the TLV encoding) the byte
216 * before the value part already contains this length so we can use it
217 * here.
218 */
219 OSMO_ASSERT(value[-1] == value_len);
220 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
221 value - 1, 0);
222
223 /* specific parts */
224 while (data_len > 0) {
225 enum osmo_gsup_iei iei;
226 struct osmo_gsup_pdp_info pdp_info;
227 struct osmo_auth_vector auth_info;
228
229 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
230 if (rc < 0)
231 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
232
233 iei = tag;
234
235 switch (iei) {
236 case OSMO_GSUP_IMSI_IE:
237 case OSMO_GSUP_PDP_TYPE_IE:
238 case OSMO_GSUP_ACCESS_POINT_NAME_IE:
239 case OSMO_GSUP_RAND_IE:
240 case OSMO_GSUP_SRES_IE:
241 case OSMO_GSUP_KC_IE:
242 LOGP(DLGSUP, LOGL_NOTICE,
243 "GSUP IE type %d not expected (ignored)\n", iei);
244 continue;
245
246 case OSMO_GSUP_CAUSE_IE:
247 gsup_msg->cause = osmo_decode_big_endian(value, value_len);
248 break;
249
250 case OSMO_GSUP_CANCEL_TYPE_IE:
251 gsup_msg->cancel_type =
252 osmo_decode_big_endian(value, value_len) + 1;
253 break;
254
255 case OSMO_GSUP_PDP_INFO_COMPL_IE:
256 gsup_msg->pdp_info_compl = 1;
257 break;
258
259 case OSMO_GSUP_FREEZE_PTMSI_IE:
260 gsup_msg->freeze_ptmsi = 1;
261 break;
262
263 case OSMO_GSUP_PDP_CONTEXT_ID_IE:
264 /* When these IE appear in the top-level part of the
265 * message, they are used by Delete Subscr Info to delete
266 * single entries. We don't have an extra list for
267 * these but use the PDP info list instead */
268
269 /* fall through */
270
271 case OSMO_GSUP_PDP_INFO_IE:
272 if (gsup_msg->num_pdp_infos >= OSMO_GSUP_MAX_NUM_PDP_INFO) {
273 LOGP(DLGSUP, LOGL_ERROR,
274 "GSUP IE type %d (PDP_INFO) max exceeded\n",
275 iei);
276 return -GMM_CAUSE_COND_IE_ERR;
277 }
278
279 pdp_info = empty_pdp_info;
280
281 if (iei == OSMO_GSUP_PDP_INFO_IE) {
282 rc = decode_pdp_info(value, value_len, &pdp_info);
283 if (rc < 0)
284 return rc;
285 pdp_info.have_info = 1;
286 } else {
287 pdp_info.context_id =
288 osmo_decode_big_endian(value, value_len);
289 }
290
291 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
292 pdp_info;
293 break;
294
295 case OSMO_GSUP_AUTH_TUPLE_IE:
296 if (gsup_msg->num_auth_vectors >= OSMO_GSUP_MAX_NUM_AUTH_INFO) {
297 LOGP(DLGSUP, LOGL_ERROR,
298 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
299 iei);
300 return -GMM_CAUSE_INV_MAND_INFO;
301 }
302
303 auth_info = empty_auth_info;
304
305 rc = decode_auth_info(value, value_len, &auth_info);
306 if (rc < 0)
307 return rc;
308
309 gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] =
310 auth_info;
311 break;
312
313 case OSMO_GSUP_AUTS_IE:
314 if (value_len != 16) {
315 LOGP(DLGSUP, LOGL_ERROR,
316 "AUTS length != 16 received\n");
317 return -GMM_CAUSE_COND_IE_ERR;
318 }
319 gsup_msg->auts = value;
320 break;
321
322 case OSMO_GSUP_MSISDN_IE:
323 gsup_msg->msisdn_enc = value;
324 gsup_msg->msisdn_enc_len = value_len;
325 break;
326
327 case OSMO_GSUP_HLR_NUMBER_IE:
328 gsup_msg->hlr_enc = value;
329 gsup_msg->hlr_enc_len = value_len;
330 break;
331
Harald Welte48dc1a52016-05-05 18:46:42 +0200332 case OSMO_GSUP_CN_DOMAIN_IE:
333 gsup_msg->cn_domain = *value;
334 break;
335
Harald Welte3b6fb082016-04-25 18:46:22 +0200336 default:
337 LOGP(DLGSUP, LOGL_NOTICE,
338 "GSUP IE type %d unknown\n", iei);
339 continue;
340 }
341 }
342
343 return 0;
344}
345
346static void encode_pdp_info(struct msgb *msg, enum osmo_gsup_iei iei,
347 const struct osmo_gsup_pdp_info *pdp_info)
348{
349 uint8_t *len_field;
350 size_t old_len;
351 uint8_t u8;
352
353 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
354 old_len = msgb_length(msg);
355
356 u8 = pdp_info->context_id;
357 msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
358
359 if (pdp_info->pdp_type) {
360 msgb_tlv_put(msg, OSMO_GSUP_PDP_TYPE_IE,
361 OSMO_GSUP_PDP_TYPE_SIZE,
362 osmo_encode_big_endian(pdp_info->pdp_type | 0xf000,
363 OSMO_GSUP_PDP_TYPE_SIZE));
364 }
365
366 if (pdp_info->apn_enc) {
367 msgb_tlv_put(msg, OSMO_GSUP_ACCESS_POINT_NAME_IE,
368 pdp_info->apn_enc_len, pdp_info->apn_enc);
369 }
370
371 if (pdp_info->qos_enc) {
372 msgb_tlv_put(msg, OSMO_GSUP_PDP_QOS_IE,
373 pdp_info->qos_enc_len, pdp_info->qos_enc);
374 }
375
376 /* Update length field */
377 *len_field = msgb_length(msg) - old_len;
378}
379
380static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei,
381 const struct osmo_auth_vector *auth_vector)
382{
383 uint8_t *len_field;
384 size_t old_len;
385
386 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
387 old_len = msgb_length(msg);
388
389 msgb_tlv_put(msg, OSMO_GSUP_RAND_IE,
390 sizeof(auth_vector->rand), auth_vector->rand);
391
392 msgb_tlv_put(msg, OSMO_GSUP_SRES_IE,
393 sizeof(auth_vector->sres), auth_vector->sres);
394
395 msgb_tlv_put(msg, OSMO_GSUP_KC_IE,
396 sizeof(auth_vector->kc), auth_vector->kc);
397
398 /* Update length field */
399 *len_field = msgb_length(msg) - old_len;
400}
401
402/*! Encode a GSUP message
403 * \param[out] msg message buffer to which encoded message is written
404 * \param[in] gsup_msg \ref osmo_gsup_message data to be encoded
405 */
406void osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg)
407{
408 uint8_t u8;
409 int idx;
410 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
411 size_t bcd_len;
412
413 /* generic part */
414 OSMO_ASSERT(gsup_msg->message_type);
415 msgb_v_put(msg, gsup_msg->message_type);
416
417 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
418 gsup_msg->imsi);
419
420 OSMO_ASSERT(bcd_len > 1);
421
422 /* Note that gsm48_encode_bcd_number puts the length into the first
423 * octet. Since msgb_tlv_put will add this length byte, we'll have to
424 * skip it */
425 msgb_tlv_put(msg, OSMO_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
426
427 /* specific parts */
428 if (gsup_msg->msisdn_enc)
429 msgb_tlv_put(msg, OSMO_GSUP_MSISDN_IE,
430 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
431 if (gsup_msg->hlr_enc)
432 msgb_tlv_put(msg, OSMO_GSUP_HLR_NUMBER_IE,
433 gsup_msg->hlr_enc_len, gsup_msg->hlr_enc);
434
435 if ((u8 = gsup_msg->cause))
436 msgb_tlv_put(msg, OSMO_GSUP_CAUSE_IE, sizeof(u8), &u8);
437
438 if ((u8 = gsup_msg->cancel_type)) {
439 u8 -= 1;
440 msgb_tlv_put(msg, OSMO_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
441 }
442
443 if (gsup_msg->pdp_info_compl)
444 msgb_tlv_put(msg, OSMO_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
445
446 if (gsup_msg->freeze_ptmsi)
447 msgb_tlv_put(msg, OSMO_GSUP_FREEZE_PTMSI_IE, 0, &u8);
448
449 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
450 const struct osmo_gsup_pdp_info *pdp_info;
451
452 pdp_info = &gsup_msg->pdp_infos[idx];
453
454 if (pdp_info->context_id == 0)
455 continue;
456
457 if (pdp_info->have_info) {
458 encode_pdp_info(msg, OSMO_GSUP_PDP_INFO_IE, pdp_info);
459 } else {
460 u8 = pdp_info->context_id;
461 msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE,
462 sizeof(u8), &u8);
463 }
464 }
465
466 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
467 const struct osmo_auth_vector *auth_vector;
468
469 auth_vector = &gsup_msg->auth_vectors[idx];
470
471 encode_auth_info(msg, OSMO_GSUP_AUTH_TUPLE_IE, auth_vector);
472 }
473
474 if (gsup_msg->auts)
475 msgb_tlv_put(msg, OSMO_GSUP_AUTS_IE, 16, gsup_msg->auts);
Harald Welte48dc1a52016-05-05 18:46:42 +0200476
477 if (gsup_msg->cn_domain) {
478 uint8_t dn = gsup_msg->cn_domain;
479 msgb_tlv_put(msg, OSMO_GSUP_CN_DOMAIN_IE, 1, &dn);
480 }
Harald Welte3b6fb082016-04-25 18:46:22 +0200481}