blob: e3eef02e6a93e9c2a890f7e3acdf89ab40502fe7 [file] [log] [blame]
Harald Welte3b6fb082016-04-25 18:46:22 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2014 by sysmocom - s.f.m.c. GmbH
3 * Author: Jacob Erlbeck
Harald Welte3b6fb082016-04-25 18:46:22 +02004 * (C) 2015 by Holger Hans Peter Freyther
5 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
Harald Welte3b6fb082016-04-25 18:46:22 +02009 *
10 * This program is free software; you can redistribute it and/or modify
Neels Hofmeyr5f460de2016-12-08 16:23:05 +010011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Harald Welte3b6fb082016-04-25 18:46:22 +020013 * (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
Neels Hofmeyr5f460de2016-12-08 16:23:05 +010018 * GNU General Public License for more details.
Harald Welte3b6fb082016-04-25 18:46:22 +020019 *
Neels Hofmeyr5f460de2016-12-08 16:23:05 +010020 * You should have received a copy of the GNU General Public License
Harald Welte3b6fb082016-04-25 18:46:22 +020021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <osmocom/gsm/tlv.h>
26#include <osmocom/core/msgb.h>
27#include <osmocom/core/logging.h>
28#include <osmocom/gsm/gsm48_ie.h>
29#include <osmocom/gsm/gsup.h>
30
31#include <stdint.h>
32
Harald Welte96e2a002017-06-12 21:44:18 +020033/*! \addtogroup gsup
34 * @{
Harald Welte381a1aa2017-10-16 18:31:20 +020035 * \file gsup.c
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),
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +070065
66 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PROC_SS_REQUEST),
67 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PROC_SS_ERROR),
68 OSMO_VALUE_STRING(OSMO_GSUP_MSGT_PROC_SS_RESULT),
69
Neels Hofmeyr10f5fb42017-02-09 02:09:09 +010070 { 0, NULL }
71};
72
Harald Welte520ebc12018-06-11 20:27:27 +020073
74/*! return the error message type corresponding to \a type_in
75 * \returns matching error message type; -1 on error */
76int osmo_gsup_get_err_msg_type(enum osmo_gsup_message_type type_in)
77{
78 switch (type_in) {
79 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
80 return OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR;
81 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
82 return OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
83 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
84 return OSMO_GSUP_MSGT_PURGE_MS_ERROR;
85 case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST:
86 return OSMO_GSUP_MSGT_INSERT_DATA_ERROR;
87 case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST:
88 return OSMO_GSUP_MSGT_DELETE_DATA_ERROR;
89 case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST:
90 return OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR;
91 default:
92 return -1;
93 }
94}
95
Harald Welte3b6fb082016-04-25 18:46:22 +020096static int decode_pdp_info(uint8_t *data, size_t data_len,
97 struct osmo_gsup_pdp_info *pdp_info)
98{
99 int rc;
100 uint8_t tag;
101 uint8_t *value;
102 size_t value_len;
103
104 /* specific parts */
105 while (data_len > 0) {
106 enum osmo_gsup_iei iei;
107
108 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
109 if (rc < 0)
110 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
111
112 iei = tag;
113
114 switch (iei) {
115 case OSMO_GSUP_PDP_CONTEXT_ID_IE:
116 pdp_info->context_id = osmo_decode_big_endian(value, value_len);
117 break;
118
119 case OSMO_GSUP_PDP_TYPE_IE:
120 pdp_info->pdp_type =
121 osmo_decode_big_endian(value, value_len) & 0x0fff;
122 break;
123
124 case OSMO_GSUP_ACCESS_POINT_NAME_IE:
125 pdp_info->apn_enc = value;
126 pdp_info->apn_enc_len = value_len;
127 break;
128
129 case OSMO_GSUP_PDP_QOS_IE:
130 pdp_info->qos_enc = value;
131 pdp_info->qos_enc_len = value_len;
132 break;
133
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +0200134 case OSMO_GSUP_CHARG_CHAR_IE:
135 pdp_info->pdp_charg_enc = value;
136 pdp_info->pdp_charg_enc_len = value_len;
137 break;
138
Harald Welte3b6fb082016-04-25 18:46:22 +0200139 default:
140 LOGP(DLGSUP, LOGL_ERROR,
141 "GSUP IE type %d not expected in PDP info\n", iei);
142 continue;
143 }
144 }
145
146 return 0;
147}
148
149static int decode_auth_info(uint8_t *data, size_t data_len,
150 struct osmo_auth_vector *auth_vector)
151{
152 int rc;
153 uint8_t tag;
154 uint8_t *value;
155 size_t value_len;
156 enum osmo_gsup_iei iei;
157 uint8_t presence = 0;
158
159 /* specific parts */
160 while (data_len > 0) {
161 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
162 if (rc < 0)
163 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
164
165 iei = tag;
166
167 switch (iei) {
168 case OSMO_GSUP_RAND_IE:
169 if (value_len != sizeof(auth_vector->rand))
170 goto parse_error;
171
172 memcpy(auth_vector->rand, value, value_len);
173 presence |= (1 << 0);
174 break;
175
176 case OSMO_GSUP_SRES_IE:
177 if (value_len != sizeof(auth_vector->sres))
178 goto parse_error;
179
180 memcpy(auth_vector->sres, value, value_len);
181 presence |= (1 << 1);
182 break;
183
184 case OSMO_GSUP_KC_IE:
185 if (value_len != sizeof(auth_vector->kc))
186 goto parse_error;
187
188 memcpy(auth_vector->kc, value, value_len);
189 presence |= (1 << 2);
190 break;
191
192 case OSMO_GSUP_IK_IE:
193 if (value_len != sizeof(auth_vector->ik))
194 goto parse_error;
195 memcpy(auth_vector->ik, value, value_len);
196 presence |= (1 << 4);
197 break;
198
199 case OSMO_GSUP_CK_IE:
200 if (value_len != sizeof(auth_vector->ck))
201 goto parse_error;
202 memcpy(auth_vector->ck, value, value_len);
203 presence |= (1 << 5);
204 break;
205
206 case OSMO_GSUP_AUTN_IE:
207 if (value_len != sizeof(auth_vector->autn))
208 goto parse_error;
209 memcpy(auth_vector->autn, value, value_len);
210 presence |= (1 << 6);
211 break;
212 case OSMO_GSUP_RES_IE:
213 if (value_len > sizeof(auth_vector->res))
214 goto parse_error;
215 memcpy(auth_vector->res, value, value_len);
216 auth_vector->res_len = value_len;
217 presence |= (1 << 7);
218 break;
219
220 default:
221 LOGP(DLGSUP, LOGL_ERROR,
222 "GSUP IE type %d not expected in PDP info\n", iei);
223 continue;
224 }
225 }
226
227 if (presence & 0x07)
228 auth_vector->auth_types |= OSMO_AUTH_TYPE_GSM;
229 if (presence & 0xf0)
230 auth_vector->auth_types |= OSMO_AUTH_TYPE_UMTS;
231
232 return 0;
233
234parse_error:
235 LOGP(DLGSUP, LOGL_ERROR,
236 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
237
238 return -1;
239}
240
241/*! Decode (parse) a GSUP message
242 * \param[in] const_data input data to be parsed
243 * \param[in] data_len length of input (\a const_data)
244 * \param[out] gsup_msg callee-allocated output data structure
245 * \returns 0 on success; negative otherwise
246 */
247int osmo_gsup_decode(const uint8_t *const_data, size_t data_len,
248 struct osmo_gsup_message *gsup_msg)
249{
250 int rc;
251 uint8_t tag;
252 /* the shift/match functions expect non-const pointers, but we'll
253 * either copy the data or cast pointers back to const before returning
254 * them
255 */
256 uint8_t *data = (uint8_t *)const_data;
257 uint8_t *value;
258 size_t value_len;
259 static const struct osmo_gsup_pdp_info empty_pdp_info = {0};
Neels Hofmeyr505adee2016-07-13 16:55:43 +0200260 static const struct osmo_auth_vector empty_auth_info = {{0}};
Harald Welte3b6fb082016-04-25 18:46:22 +0200261 static const struct osmo_gsup_message empty_gsup_message = {0};
262
263 *gsup_msg = empty_gsup_message;
264
265 /* generic part */
266 rc = osmo_shift_v_fixed(&data, &data_len, 1, &value);
267 if (rc < 0)
268 return -GMM_CAUSE_INV_MAND_INFO;
269
270 gsup_msg->message_type = osmo_decode_big_endian(value, 1);
271
272 rc = osmo_match_shift_tlv(&data, &data_len, OSMO_GSUP_IMSI_IE,
273 &value, &value_len);
274
275 if (rc <= 0)
276 return -GMM_CAUSE_INV_MAND_INFO;
277
278 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
279 return -GMM_CAUSE_INV_MAND_INFO;
280
281 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
282 * octets in the first octet. By coincidence (the TLV encoding) the byte
283 * before the value part already contains this length so we can use it
284 * here.
285 */
286 OSMO_ASSERT(value[-1] == value_len);
287 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
288 value - 1, 0);
289
290 /* specific parts */
291 while (data_len > 0) {
292 enum osmo_gsup_iei iei;
293 struct osmo_gsup_pdp_info pdp_info;
294 struct osmo_auth_vector auth_info;
295
296 rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len);
297 if (rc < 0)
298 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
299
300 iei = tag;
301
302 switch (iei) {
303 case OSMO_GSUP_IMSI_IE:
304 case OSMO_GSUP_PDP_TYPE_IE:
305 case OSMO_GSUP_ACCESS_POINT_NAME_IE:
Harald Welte3b6fb082016-04-25 18:46:22 +0200306 case OSMO_GSUP_SRES_IE:
307 case OSMO_GSUP_KC_IE:
308 LOGP(DLGSUP, LOGL_NOTICE,
309 "GSUP IE type %d not expected (ignored)\n", iei);
310 continue;
311
312 case OSMO_GSUP_CAUSE_IE:
313 gsup_msg->cause = osmo_decode_big_endian(value, value_len);
314 break;
315
316 case OSMO_GSUP_CANCEL_TYPE_IE:
317 gsup_msg->cancel_type =
318 osmo_decode_big_endian(value, value_len) + 1;
319 break;
320
321 case OSMO_GSUP_PDP_INFO_COMPL_IE:
322 gsup_msg->pdp_info_compl = 1;
323 break;
324
325 case OSMO_GSUP_FREEZE_PTMSI_IE:
326 gsup_msg->freeze_ptmsi = 1;
327 break;
328
329 case OSMO_GSUP_PDP_CONTEXT_ID_IE:
330 /* When these IE appear in the top-level part of the
331 * message, they are used by Delete Subscr Info to delete
332 * single entries. We don't have an extra list for
333 * these but use the PDP info list instead */
334
335 /* fall through */
336
337 case OSMO_GSUP_PDP_INFO_IE:
338 if (gsup_msg->num_pdp_infos >= OSMO_GSUP_MAX_NUM_PDP_INFO) {
339 LOGP(DLGSUP, LOGL_ERROR,
340 "GSUP IE type %d (PDP_INFO) max exceeded\n",
341 iei);
342 return -GMM_CAUSE_COND_IE_ERR;
343 }
344
345 pdp_info = empty_pdp_info;
346
347 if (iei == OSMO_GSUP_PDP_INFO_IE) {
348 rc = decode_pdp_info(value, value_len, &pdp_info);
349 if (rc < 0)
350 return rc;
351 pdp_info.have_info = 1;
352 } else {
353 pdp_info.context_id =
354 osmo_decode_big_endian(value, value_len);
355 }
356
357 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
358 pdp_info;
359 break;
360
361 case OSMO_GSUP_AUTH_TUPLE_IE:
362 if (gsup_msg->num_auth_vectors >= OSMO_GSUP_MAX_NUM_AUTH_INFO) {
363 LOGP(DLGSUP, LOGL_ERROR,
364 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
365 iei);
366 return -GMM_CAUSE_INV_MAND_INFO;
367 }
368
369 auth_info = empty_auth_info;
370
371 rc = decode_auth_info(value, value_len, &auth_info);
372 if (rc < 0)
373 return rc;
374
375 gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] =
376 auth_info;
377 break;
378
379 case OSMO_GSUP_AUTS_IE:
Neels Hofmeyr3a5ca642017-02-21 15:53:20 +0100380 if (value_len != 14) {
Harald Welte3b6fb082016-04-25 18:46:22 +0200381 LOGP(DLGSUP, LOGL_ERROR,
Neels Hofmeyr3a5ca642017-02-21 15:53:20 +0100382 "AUTS length != 14 received\n");
Harald Welte3b6fb082016-04-25 18:46:22 +0200383 return -GMM_CAUSE_COND_IE_ERR;
384 }
385 gsup_msg->auts = value;
386 break;
387
Harald Welte766da862016-05-06 11:18:15 +0200388 case OSMO_GSUP_RAND_IE:
389 if (value_len != 16) {
390 LOGP(DLGSUP, LOGL_ERROR,
391 "RAND length != 16 received\n");
392 return -GMM_CAUSE_COND_IE_ERR;
393 }
394 gsup_msg->rand = value;
395 break;
396
Harald Welte3b6fb082016-04-25 18:46:22 +0200397 case OSMO_GSUP_MSISDN_IE:
398 gsup_msg->msisdn_enc = value;
399 gsup_msg->msisdn_enc_len = value_len;
400 break;
401
402 case OSMO_GSUP_HLR_NUMBER_IE:
403 gsup_msg->hlr_enc = value;
404 gsup_msg->hlr_enc_len = value_len;
405 break;
406
Harald Welte48dc1a52016-05-05 18:46:42 +0200407 case OSMO_GSUP_CN_DOMAIN_IE:
408 gsup_msg->cn_domain = *value;
409 break;
410
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +0200411 case OSMO_GSUP_CHARG_CHAR_IE:
412 gsup_msg->pdp_charg_enc = value;
413 gsup_msg->pdp_charg_enc_len = value_len;
414 break;
415
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700416 case OSMO_GSUP_SESSION_ID_IE:
417 gsup_msg->session_id = osmo_decode_big_endian(value, value_len);
418 break;
419
420 case OSMO_GSUP_SESSION_STATE_IE:
421 gsup_msg->session_state = *value;
422 break;
423
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700424 case OSMO_GSUP_SS_INFO_IE:
425 gsup_msg->ss_info = value;
426 gsup_msg->ss_info_len = value_len;
427 break;
428
Harald Welte3b6fb082016-04-25 18:46:22 +0200429 default:
430 LOGP(DLGSUP, LOGL_NOTICE,
431 "GSUP IE type %d unknown\n", iei);
432 continue;
433 }
434 }
435
436 return 0;
437}
438
439static void encode_pdp_info(struct msgb *msg, enum osmo_gsup_iei iei,
440 const struct osmo_gsup_pdp_info *pdp_info)
441{
442 uint8_t *len_field;
443 size_t old_len;
444 uint8_t u8;
445
446 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
447 old_len = msgb_length(msg);
448
449 u8 = pdp_info->context_id;
450 msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
451
452 if (pdp_info->pdp_type) {
453 msgb_tlv_put(msg, OSMO_GSUP_PDP_TYPE_IE,
454 OSMO_GSUP_PDP_TYPE_SIZE,
455 osmo_encode_big_endian(pdp_info->pdp_type | 0xf000,
456 OSMO_GSUP_PDP_TYPE_SIZE));
457 }
458
459 if (pdp_info->apn_enc) {
460 msgb_tlv_put(msg, OSMO_GSUP_ACCESS_POINT_NAME_IE,
461 pdp_info->apn_enc_len, pdp_info->apn_enc);
462 }
463
464 if (pdp_info->qos_enc) {
465 msgb_tlv_put(msg, OSMO_GSUP_PDP_QOS_IE,
466 pdp_info->qos_enc_len, pdp_info->qos_enc);
467 }
468
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +0200469 if (pdp_info->pdp_charg_enc) {
470 msgb_tlv_put(msg, OSMO_GSUP_CHARG_CHAR_IE,
471 pdp_info->pdp_charg_enc_len, pdp_info->pdp_charg_enc);
472 }
473
Harald Welte3b6fb082016-04-25 18:46:22 +0200474 /* Update length field */
475 *len_field = msgb_length(msg) - old_len;
476}
477
478static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei,
479 const struct osmo_auth_vector *auth_vector)
480{
481 uint8_t *len_field;
482 size_t old_len;
483
484 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
485 old_len = msgb_length(msg);
486
Harald Weltedb78d212016-06-06 13:47:07 +0200487 if (auth_vector->auth_types & OSMO_AUTH_TYPE_GSM) {
488 msgb_tlv_put(msg, OSMO_GSUP_RAND_IE,
489 sizeof(auth_vector->rand), auth_vector->rand);
Harald Welte3b6fb082016-04-25 18:46:22 +0200490
Harald Weltedb78d212016-06-06 13:47:07 +0200491 msgb_tlv_put(msg, OSMO_GSUP_SRES_IE,
492 sizeof(auth_vector->sres), auth_vector->sres);
Harald Welte3b6fb082016-04-25 18:46:22 +0200493
Harald Weltedb78d212016-06-06 13:47:07 +0200494 msgb_tlv_put(msg, OSMO_GSUP_KC_IE,
495 sizeof(auth_vector->kc), auth_vector->kc);
496 }
497
498 if (auth_vector->auth_types & OSMO_AUTH_TYPE_UMTS) {
499 msgb_tlv_put(msg, OSMO_GSUP_IK_IE,
500 sizeof(auth_vector->ik), auth_vector->ik);
501
502 msgb_tlv_put(msg, OSMO_GSUP_CK_IE,
503 sizeof(auth_vector->ck), auth_vector->ck);
504
505 msgb_tlv_put(msg, OSMO_GSUP_AUTN_IE,
506 sizeof(auth_vector->autn), auth_vector->autn);
507
508 msgb_tlv_put(msg, OSMO_GSUP_RES_IE,
509 auth_vector->res_len, auth_vector->res);
510 }
Harald Welte3b6fb082016-04-25 18:46:22 +0200511
512 /* Update length field */
513 *len_field = msgb_length(msg) - old_len;
514}
515
516/*! Encode a GSUP message
517 * \param[out] msg message buffer to which encoded message is written
518 * \param[in] gsup_msg \ref osmo_gsup_message data to be encoded
Max80f4c4e2018-01-24 12:33:05 +0100519 * \returns 0 on success; negative otherwise
Harald Welte3b6fb082016-04-25 18:46:22 +0200520 */
Max80f4c4e2018-01-24 12:33:05 +0100521int osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg)
Harald Welte3b6fb082016-04-25 18:46:22 +0200522{
523 uint8_t u8;
524 int idx;
525 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
526 size_t bcd_len;
527
528 /* generic part */
Max80f4c4e2018-01-24 12:33:05 +0100529 if(!gsup_msg->message_type)
530 return -ENOMEM;
531
Harald Welte3b6fb082016-04-25 18:46:22 +0200532 msgb_v_put(msg, gsup_msg->message_type);
533
534 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
535 gsup_msg->imsi);
536
Max80f4c4e2018-01-24 12:33:05 +0100537 if (bcd_len <= 0 || bcd_len > sizeof(bcd_buf))
538 return -EINVAL;
Harald Welte3b6fb082016-04-25 18:46:22 +0200539
540 /* Note that gsm48_encode_bcd_number puts the length into the first
541 * octet. Since msgb_tlv_put will add this length byte, we'll have to
542 * skip it */
543 msgb_tlv_put(msg, OSMO_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
544
545 /* specific parts */
546 if (gsup_msg->msisdn_enc)
547 msgb_tlv_put(msg, OSMO_GSUP_MSISDN_IE,
548 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
549 if (gsup_msg->hlr_enc)
550 msgb_tlv_put(msg, OSMO_GSUP_HLR_NUMBER_IE,
551 gsup_msg->hlr_enc_len, gsup_msg->hlr_enc);
552
553 if ((u8 = gsup_msg->cause))
554 msgb_tlv_put(msg, OSMO_GSUP_CAUSE_IE, sizeof(u8), &u8);
555
556 if ((u8 = gsup_msg->cancel_type)) {
557 u8 -= 1;
558 msgb_tlv_put(msg, OSMO_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
559 }
560
561 if (gsup_msg->pdp_info_compl)
562 msgb_tlv_put(msg, OSMO_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
563
564 if (gsup_msg->freeze_ptmsi)
565 msgb_tlv_put(msg, OSMO_GSUP_FREEZE_PTMSI_IE, 0, &u8);
566
567 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
568 const struct osmo_gsup_pdp_info *pdp_info;
569
570 pdp_info = &gsup_msg->pdp_infos[idx];
571
572 if (pdp_info->context_id == 0)
573 continue;
574
575 if (pdp_info->have_info) {
576 encode_pdp_info(msg, OSMO_GSUP_PDP_INFO_IE, pdp_info);
577 } else {
578 u8 = pdp_info->context_id;
579 msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE,
580 sizeof(u8), &u8);
581 }
582 }
583
584 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
585 const struct osmo_auth_vector *auth_vector;
586
587 auth_vector = &gsup_msg->auth_vectors[idx];
588
589 encode_auth_info(msg, OSMO_GSUP_AUTH_TUPLE_IE, auth_vector);
590 }
591
592 if (gsup_msg->auts)
Neels Hofmeyr8352d312017-02-02 20:05:14 +0100593 msgb_tlv_put(msg, OSMO_GSUP_AUTS_IE, 14, gsup_msg->auts);
Harald Welte48dc1a52016-05-05 18:46:42 +0200594
Harald Welte766da862016-05-06 11:18:15 +0200595 if (gsup_msg->rand)
596 msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, 16, gsup_msg->rand);
597
Harald Welte48dc1a52016-05-05 18:46:42 +0200598 if (gsup_msg->cn_domain) {
599 uint8_t dn = gsup_msg->cn_domain;
600 msgb_tlv_put(msg, OSMO_GSUP_CN_DOMAIN_IE, 1, &dn);
601 }
Holger Hans Peter Freythereb55c0d2017-07-07 16:53:30 +0200602
603 if (gsup_msg->pdp_charg_enc) {
604 msgb_tlv_put(msg, OSMO_GSUP_CHARG_CHAR_IE,
605 gsup_msg->pdp_charg_enc_len, gsup_msg->pdp_charg_enc);
606 }
Max80f4c4e2018-01-24 12:33:05 +0100607
Vadim Yanitskiy72696042018-04-07 02:34:55 +0700608 if ((u8 = gsup_msg->session_state)) {
609 size_t len = sizeof(gsup_msg->session_id);
610 uint8_t *sid = osmo_encode_big_endian(gsup_msg->session_id, len);
611
612 msgb_tlv_put(msg, OSMO_GSUP_SESSION_ID_IE, len, sid);
613 msgb_tlv_put(msg, OSMO_GSUP_SESSION_STATE_IE, sizeof(u8), &u8);
614 }
615
Vadim Yanitskiy36c7b332018-03-31 05:23:09 +0700616 if (gsup_msg->ss_info) {
617 msgb_tlv_put(msg, OSMO_GSUP_SS_INFO_IE,
618 gsup_msg->ss_info_len, gsup_msg->ss_info);
619 }
620
Max80f4c4e2018-01-24 12:33:05 +0100621 return 0;
Harald Welte3b6fb082016-04-25 18:46:22 +0200622}
Harald Welte96e2a002017-06-12 21:44:18 +0200623
624/*! @} */