Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 2 | * (C) 2014 by sysmocom - s.f.m.c. GmbH |
| 3 | * Author: Jacob Erlbeck |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 4 | * (C) 2015 by Holger Hans Peter Freyther |
| 5 | * (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
Neels Hofmeyr | 5f460de | 2016-12-08 16:23:05 +0100 | [diff] [blame] | 11 | * 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 Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 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 |
Neels Hofmeyr | 5f460de | 2016-12-08 16:23:05 +0100 | [diff] [blame] | 18 | * GNU General Public License for more details. |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 19 | * |
Neels Hofmeyr | 5f460de | 2016-12-08 16:23:05 +0100 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 21 | * 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 Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 33 | /*! \addtogroup gsup |
| 34 | * @{ |
Harald Welte | 381a1aa | 2017-10-16 18:31:20 +0200 | [diff] [blame] | 35 | * \file gsup.c |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 36 | * Osmocom Generic Subscriber Update Protocol |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 37 | */ |
| 38 | |
Neels Hofmeyr | 10f5fb4 | 2017-02-09 02:09:09 +0100 | [diff] [blame] | 39 | const 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 Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 68 | static 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 | |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 106 | case OSMO_GSUP_CHARG_CHAR_IE: |
| 107 | pdp_info->pdp_charg_enc = value; |
| 108 | pdp_info->pdp_charg_enc_len = value_len; |
| 109 | break; |
| 110 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 111 | default: |
| 112 | LOGP(DLGSUP, LOGL_ERROR, |
| 113 | "GSUP IE type %d not expected in PDP info\n", iei); |
| 114 | continue; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int decode_auth_info(uint8_t *data, size_t data_len, |
| 122 | struct osmo_auth_vector *auth_vector) |
| 123 | { |
| 124 | int rc; |
| 125 | uint8_t tag; |
| 126 | uint8_t *value; |
| 127 | size_t value_len; |
| 128 | enum osmo_gsup_iei iei; |
| 129 | uint8_t presence = 0; |
| 130 | |
| 131 | /* specific parts */ |
| 132 | while (data_len > 0) { |
| 133 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
| 134 | if (rc < 0) |
| 135 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 136 | |
| 137 | iei = tag; |
| 138 | |
| 139 | switch (iei) { |
| 140 | case OSMO_GSUP_RAND_IE: |
| 141 | if (value_len != sizeof(auth_vector->rand)) |
| 142 | goto parse_error; |
| 143 | |
| 144 | memcpy(auth_vector->rand, value, value_len); |
| 145 | presence |= (1 << 0); |
| 146 | break; |
| 147 | |
| 148 | case OSMO_GSUP_SRES_IE: |
| 149 | if (value_len != sizeof(auth_vector->sres)) |
| 150 | goto parse_error; |
| 151 | |
| 152 | memcpy(auth_vector->sres, value, value_len); |
| 153 | presence |= (1 << 1); |
| 154 | break; |
| 155 | |
| 156 | case OSMO_GSUP_KC_IE: |
| 157 | if (value_len != sizeof(auth_vector->kc)) |
| 158 | goto parse_error; |
| 159 | |
| 160 | memcpy(auth_vector->kc, value, value_len); |
| 161 | presence |= (1 << 2); |
| 162 | break; |
| 163 | |
| 164 | case OSMO_GSUP_IK_IE: |
| 165 | if (value_len != sizeof(auth_vector->ik)) |
| 166 | goto parse_error; |
| 167 | memcpy(auth_vector->ik, value, value_len); |
| 168 | presence |= (1 << 4); |
| 169 | break; |
| 170 | |
| 171 | case OSMO_GSUP_CK_IE: |
| 172 | if (value_len != sizeof(auth_vector->ck)) |
| 173 | goto parse_error; |
| 174 | memcpy(auth_vector->ck, value, value_len); |
| 175 | presence |= (1 << 5); |
| 176 | break; |
| 177 | |
| 178 | case OSMO_GSUP_AUTN_IE: |
| 179 | if (value_len != sizeof(auth_vector->autn)) |
| 180 | goto parse_error; |
| 181 | memcpy(auth_vector->autn, value, value_len); |
| 182 | presence |= (1 << 6); |
| 183 | break; |
| 184 | case OSMO_GSUP_RES_IE: |
| 185 | if (value_len > sizeof(auth_vector->res)) |
| 186 | goto parse_error; |
| 187 | memcpy(auth_vector->res, value, value_len); |
| 188 | auth_vector->res_len = value_len; |
| 189 | presence |= (1 << 7); |
| 190 | break; |
| 191 | |
| 192 | default: |
| 193 | LOGP(DLGSUP, LOGL_ERROR, |
| 194 | "GSUP IE type %d not expected in PDP info\n", iei); |
| 195 | continue; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (presence & 0x07) |
| 200 | auth_vector->auth_types |= OSMO_AUTH_TYPE_GSM; |
| 201 | if (presence & 0xf0) |
| 202 | auth_vector->auth_types |= OSMO_AUTH_TYPE_UMTS; |
| 203 | |
| 204 | return 0; |
| 205 | |
| 206 | parse_error: |
| 207 | LOGP(DLGSUP, LOGL_ERROR, |
| 208 | "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len); |
| 209 | |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | /*! Decode (parse) a GSUP message |
| 214 | * \param[in] const_data input data to be parsed |
| 215 | * \param[in] data_len length of input (\a const_data) |
| 216 | * \param[out] gsup_msg callee-allocated output data structure |
| 217 | * \returns 0 on success; negative otherwise |
| 218 | */ |
| 219 | int osmo_gsup_decode(const uint8_t *const_data, size_t data_len, |
| 220 | struct osmo_gsup_message *gsup_msg) |
| 221 | { |
| 222 | int rc; |
| 223 | uint8_t tag; |
| 224 | /* the shift/match functions expect non-const pointers, but we'll |
| 225 | * either copy the data or cast pointers back to const before returning |
| 226 | * them |
| 227 | */ |
| 228 | uint8_t *data = (uint8_t *)const_data; |
| 229 | uint8_t *value; |
| 230 | size_t value_len; |
| 231 | static const struct osmo_gsup_pdp_info empty_pdp_info = {0}; |
Neels Hofmeyr | 505adee | 2016-07-13 16:55:43 +0200 | [diff] [blame] | 232 | static const struct osmo_auth_vector empty_auth_info = {{0}}; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 233 | static const struct osmo_gsup_message empty_gsup_message = {0}; |
| 234 | |
| 235 | *gsup_msg = empty_gsup_message; |
| 236 | |
| 237 | /* generic part */ |
| 238 | rc = osmo_shift_v_fixed(&data, &data_len, 1, &value); |
| 239 | if (rc < 0) |
| 240 | return -GMM_CAUSE_INV_MAND_INFO; |
| 241 | |
| 242 | gsup_msg->message_type = osmo_decode_big_endian(value, 1); |
| 243 | |
| 244 | rc = osmo_match_shift_tlv(&data, &data_len, OSMO_GSUP_IMSI_IE, |
| 245 | &value, &value_len); |
| 246 | |
| 247 | if (rc <= 0) |
| 248 | return -GMM_CAUSE_INV_MAND_INFO; |
| 249 | |
| 250 | if (value_len * 2 + 1 > sizeof(gsup_msg->imsi)) |
| 251 | return -GMM_CAUSE_INV_MAND_INFO; |
| 252 | |
| 253 | /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI |
| 254 | * octets in the first octet. By coincidence (the TLV encoding) the byte |
| 255 | * before the value part already contains this length so we can use it |
| 256 | * here. |
| 257 | */ |
| 258 | OSMO_ASSERT(value[-1] == value_len); |
| 259 | gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi), |
| 260 | value - 1, 0); |
| 261 | |
| 262 | /* specific parts */ |
| 263 | while (data_len > 0) { |
| 264 | enum osmo_gsup_iei iei; |
| 265 | struct osmo_gsup_pdp_info pdp_info; |
| 266 | struct osmo_auth_vector auth_info; |
| 267 | |
| 268 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
| 269 | if (rc < 0) |
| 270 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 271 | |
| 272 | iei = tag; |
| 273 | |
| 274 | switch (iei) { |
| 275 | case OSMO_GSUP_IMSI_IE: |
| 276 | case OSMO_GSUP_PDP_TYPE_IE: |
| 277 | case OSMO_GSUP_ACCESS_POINT_NAME_IE: |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 278 | case OSMO_GSUP_SRES_IE: |
| 279 | case OSMO_GSUP_KC_IE: |
| 280 | LOGP(DLGSUP, LOGL_NOTICE, |
| 281 | "GSUP IE type %d not expected (ignored)\n", iei); |
| 282 | continue; |
| 283 | |
| 284 | case OSMO_GSUP_CAUSE_IE: |
| 285 | gsup_msg->cause = osmo_decode_big_endian(value, value_len); |
| 286 | break; |
| 287 | |
| 288 | case OSMO_GSUP_CANCEL_TYPE_IE: |
| 289 | gsup_msg->cancel_type = |
| 290 | osmo_decode_big_endian(value, value_len) + 1; |
| 291 | break; |
| 292 | |
| 293 | case OSMO_GSUP_PDP_INFO_COMPL_IE: |
| 294 | gsup_msg->pdp_info_compl = 1; |
| 295 | break; |
| 296 | |
| 297 | case OSMO_GSUP_FREEZE_PTMSI_IE: |
| 298 | gsup_msg->freeze_ptmsi = 1; |
| 299 | break; |
| 300 | |
| 301 | case OSMO_GSUP_PDP_CONTEXT_ID_IE: |
| 302 | /* When these IE appear in the top-level part of the |
| 303 | * message, they are used by Delete Subscr Info to delete |
| 304 | * single entries. We don't have an extra list for |
| 305 | * these but use the PDP info list instead */ |
| 306 | |
| 307 | /* fall through */ |
| 308 | |
| 309 | case OSMO_GSUP_PDP_INFO_IE: |
| 310 | if (gsup_msg->num_pdp_infos >= OSMO_GSUP_MAX_NUM_PDP_INFO) { |
| 311 | LOGP(DLGSUP, LOGL_ERROR, |
| 312 | "GSUP IE type %d (PDP_INFO) max exceeded\n", |
| 313 | iei); |
| 314 | return -GMM_CAUSE_COND_IE_ERR; |
| 315 | } |
| 316 | |
| 317 | pdp_info = empty_pdp_info; |
| 318 | |
| 319 | if (iei == OSMO_GSUP_PDP_INFO_IE) { |
| 320 | rc = decode_pdp_info(value, value_len, &pdp_info); |
| 321 | if (rc < 0) |
| 322 | return rc; |
| 323 | pdp_info.have_info = 1; |
| 324 | } else { |
| 325 | pdp_info.context_id = |
| 326 | osmo_decode_big_endian(value, value_len); |
| 327 | } |
| 328 | |
| 329 | gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] = |
| 330 | pdp_info; |
| 331 | break; |
| 332 | |
| 333 | case OSMO_GSUP_AUTH_TUPLE_IE: |
| 334 | if (gsup_msg->num_auth_vectors >= OSMO_GSUP_MAX_NUM_AUTH_INFO) { |
| 335 | LOGP(DLGSUP, LOGL_ERROR, |
| 336 | "GSUP IE type %d (AUTH_INFO) max exceeded\n", |
| 337 | iei); |
| 338 | return -GMM_CAUSE_INV_MAND_INFO; |
| 339 | } |
| 340 | |
| 341 | auth_info = empty_auth_info; |
| 342 | |
| 343 | rc = decode_auth_info(value, value_len, &auth_info); |
| 344 | if (rc < 0) |
| 345 | return rc; |
| 346 | |
| 347 | gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] = |
| 348 | auth_info; |
| 349 | break; |
| 350 | |
| 351 | case OSMO_GSUP_AUTS_IE: |
Neels Hofmeyr | 3a5ca64 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 352 | if (value_len != 14) { |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 353 | LOGP(DLGSUP, LOGL_ERROR, |
Neels Hofmeyr | 3a5ca64 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 354 | "AUTS length != 14 received\n"); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 355 | return -GMM_CAUSE_COND_IE_ERR; |
| 356 | } |
| 357 | gsup_msg->auts = value; |
| 358 | break; |
| 359 | |
Harald Welte | 766da86 | 2016-05-06 11:18:15 +0200 | [diff] [blame] | 360 | case OSMO_GSUP_RAND_IE: |
| 361 | if (value_len != 16) { |
| 362 | LOGP(DLGSUP, LOGL_ERROR, |
| 363 | "RAND length != 16 received\n"); |
| 364 | return -GMM_CAUSE_COND_IE_ERR; |
| 365 | } |
| 366 | gsup_msg->rand = value; |
| 367 | break; |
| 368 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 369 | case OSMO_GSUP_MSISDN_IE: |
| 370 | gsup_msg->msisdn_enc = value; |
| 371 | gsup_msg->msisdn_enc_len = value_len; |
| 372 | break; |
| 373 | |
| 374 | case OSMO_GSUP_HLR_NUMBER_IE: |
| 375 | gsup_msg->hlr_enc = value; |
| 376 | gsup_msg->hlr_enc_len = value_len; |
| 377 | break; |
| 378 | |
Harald Welte | 48dc1a5 | 2016-05-05 18:46:42 +0200 | [diff] [blame] | 379 | case OSMO_GSUP_CN_DOMAIN_IE: |
| 380 | gsup_msg->cn_domain = *value; |
| 381 | break; |
| 382 | |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 383 | case OSMO_GSUP_CHARG_CHAR_IE: |
| 384 | gsup_msg->pdp_charg_enc = value; |
| 385 | gsup_msg->pdp_charg_enc_len = value_len; |
| 386 | break; |
| 387 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 388 | default: |
| 389 | LOGP(DLGSUP, LOGL_NOTICE, |
| 390 | "GSUP IE type %d unknown\n", iei); |
| 391 | continue; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static void encode_pdp_info(struct msgb *msg, enum osmo_gsup_iei iei, |
| 399 | const struct osmo_gsup_pdp_info *pdp_info) |
| 400 | { |
| 401 | uint8_t *len_field; |
| 402 | size_t old_len; |
| 403 | uint8_t u8; |
| 404 | |
| 405 | len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1; |
| 406 | old_len = msgb_length(msg); |
| 407 | |
| 408 | u8 = pdp_info->context_id; |
| 409 | msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8); |
| 410 | |
| 411 | if (pdp_info->pdp_type) { |
| 412 | msgb_tlv_put(msg, OSMO_GSUP_PDP_TYPE_IE, |
| 413 | OSMO_GSUP_PDP_TYPE_SIZE, |
| 414 | osmo_encode_big_endian(pdp_info->pdp_type | 0xf000, |
| 415 | OSMO_GSUP_PDP_TYPE_SIZE)); |
| 416 | } |
| 417 | |
| 418 | if (pdp_info->apn_enc) { |
| 419 | msgb_tlv_put(msg, OSMO_GSUP_ACCESS_POINT_NAME_IE, |
| 420 | pdp_info->apn_enc_len, pdp_info->apn_enc); |
| 421 | } |
| 422 | |
| 423 | if (pdp_info->qos_enc) { |
| 424 | msgb_tlv_put(msg, OSMO_GSUP_PDP_QOS_IE, |
| 425 | pdp_info->qos_enc_len, pdp_info->qos_enc); |
| 426 | } |
| 427 | |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 428 | if (pdp_info->pdp_charg_enc) { |
| 429 | msgb_tlv_put(msg, OSMO_GSUP_CHARG_CHAR_IE, |
| 430 | pdp_info->pdp_charg_enc_len, pdp_info->pdp_charg_enc); |
| 431 | } |
| 432 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 433 | /* Update length field */ |
| 434 | *len_field = msgb_length(msg) - old_len; |
| 435 | } |
| 436 | |
| 437 | static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei, |
| 438 | const struct osmo_auth_vector *auth_vector) |
| 439 | { |
| 440 | uint8_t *len_field; |
| 441 | size_t old_len; |
| 442 | |
| 443 | len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1; |
| 444 | old_len = msgb_length(msg); |
| 445 | |
Harald Welte | db78d21 | 2016-06-06 13:47:07 +0200 | [diff] [blame] | 446 | if (auth_vector->auth_types & OSMO_AUTH_TYPE_GSM) { |
| 447 | msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, |
| 448 | sizeof(auth_vector->rand), auth_vector->rand); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 449 | |
Harald Welte | db78d21 | 2016-06-06 13:47:07 +0200 | [diff] [blame] | 450 | msgb_tlv_put(msg, OSMO_GSUP_SRES_IE, |
| 451 | sizeof(auth_vector->sres), auth_vector->sres); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 452 | |
Harald Welte | db78d21 | 2016-06-06 13:47:07 +0200 | [diff] [blame] | 453 | msgb_tlv_put(msg, OSMO_GSUP_KC_IE, |
| 454 | sizeof(auth_vector->kc), auth_vector->kc); |
| 455 | } |
| 456 | |
| 457 | if (auth_vector->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 458 | msgb_tlv_put(msg, OSMO_GSUP_IK_IE, |
| 459 | sizeof(auth_vector->ik), auth_vector->ik); |
| 460 | |
| 461 | msgb_tlv_put(msg, OSMO_GSUP_CK_IE, |
| 462 | sizeof(auth_vector->ck), auth_vector->ck); |
| 463 | |
| 464 | msgb_tlv_put(msg, OSMO_GSUP_AUTN_IE, |
| 465 | sizeof(auth_vector->autn), auth_vector->autn); |
| 466 | |
| 467 | msgb_tlv_put(msg, OSMO_GSUP_RES_IE, |
| 468 | auth_vector->res_len, auth_vector->res); |
| 469 | } |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 470 | |
| 471 | /* Update length field */ |
| 472 | *len_field = msgb_length(msg) - old_len; |
| 473 | } |
| 474 | |
| 475 | /*! Encode a GSUP message |
| 476 | * \param[out] msg message buffer to which encoded message is written |
| 477 | * \param[in] gsup_msg \ref osmo_gsup_message data to be encoded |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 478 | * \returns 0 on success; negative otherwise |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 479 | */ |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 480 | int osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg) |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 481 | { |
| 482 | uint8_t u8; |
| 483 | int idx; |
| 484 | uint8_t bcd_buf[GSM48_MI_SIZE] = {0}; |
| 485 | size_t bcd_len; |
| 486 | |
| 487 | /* generic part */ |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 488 | if(!gsup_msg->message_type) |
| 489 | return -ENOMEM; |
| 490 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 491 | msgb_v_put(msg, gsup_msg->message_type); |
| 492 | |
| 493 | bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0, |
| 494 | gsup_msg->imsi); |
| 495 | |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 496 | if (bcd_len <= 0 || bcd_len > sizeof(bcd_buf)) |
| 497 | return -EINVAL; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 498 | |
| 499 | /* Note that gsm48_encode_bcd_number puts the length into the first |
| 500 | * octet. Since msgb_tlv_put will add this length byte, we'll have to |
| 501 | * skip it */ |
| 502 | msgb_tlv_put(msg, OSMO_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]); |
| 503 | |
| 504 | /* specific parts */ |
| 505 | if (gsup_msg->msisdn_enc) |
| 506 | msgb_tlv_put(msg, OSMO_GSUP_MSISDN_IE, |
| 507 | gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc); |
| 508 | if (gsup_msg->hlr_enc) |
| 509 | msgb_tlv_put(msg, OSMO_GSUP_HLR_NUMBER_IE, |
| 510 | gsup_msg->hlr_enc_len, gsup_msg->hlr_enc); |
| 511 | |
| 512 | if ((u8 = gsup_msg->cause)) |
| 513 | msgb_tlv_put(msg, OSMO_GSUP_CAUSE_IE, sizeof(u8), &u8); |
| 514 | |
| 515 | if ((u8 = gsup_msg->cancel_type)) { |
| 516 | u8 -= 1; |
| 517 | msgb_tlv_put(msg, OSMO_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8); |
| 518 | } |
| 519 | |
| 520 | if (gsup_msg->pdp_info_compl) |
| 521 | msgb_tlv_put(msg, OSMO_GSUP_PDP_INFO_COMPL_IE, 0, &u8); |
| 522 | |
| 523 | if (gsup_msg->freeze_ptmsi) |
| 524 | msgb_tlv_put(msg, OSMO_GSUP_FREEZE_PTMSI_IE, 0, &u8); |
| 525 | |
| 526 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
| 527 | const struct osmo_gsup_pdp_info *pdp_info; |
| 528 | |
| 529 | pdp_info = &gsup_msg->pdp_infos[idx]; |
| 530 | |
| 531 | if (pdp_info->context_id == 0) |
| 532 | continue; |
| 533 | |
| 534 | if (pdp_info->have_info) { |
| 535 | encode_pdp_info(msg, OSMO_GSUP_PDP_INFO_IE, pdp_info); |
| 536 | } else { |
| 537 | u8 = pdp_info->context_id; |
| 538 | msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, |
| 539 | sizeof(u8), &u8); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) { |
| 544 | const struct osmo_auth_vector *auth_vector; |
| 545 | |
| 546 | auth_vector = &gsup_msg->auth_vectors[idx]; |
| 547 | |
| 548 | encode_auth_info(msg, OSMO_GSUP_AUTH_TUPLE_IE, auth_vector); |
| 549 | } |
| 550 | |
| 551 | if (gsup_msg->auts) |
Neels Hofmeyr | 8352d31 | 2017-02-02 20:05:14 +0100 | [diff] [blame] | 552 | msgb_tlv_put(msg, OSMO_GSUP_AUTS_IE, 14, gsup_msg->auts); |
Harald Welte | 48dc1a5 | 2016-05-05 18:46:42 +0200 | [diff] [blame] | 553 | |
Harald Welte | 766da86 | 2016-05-06 11:18:15 +0200 | [diff] [blame] | 554 | if (gsup_msg->rand) |
| 555 | msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, 16, gsup_msg->rand); |
| 556 | |
Harald Welte | 48dc1a5 | 2016-05-05 18:46:42 +0200 | [diff] [blame] | 557 | if (gsup_msg->cn_domain) { |
| 558 | uint8_t dn = gsup_msg->cn_domain; |
| 559 | msgb_tlv_put(msg, OSMO_GSUP_CN_DOMAIN_IE, 1, &dn); |
| 560 | } |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 561 | |
| 562 | if (gsup_msg->pdp_charg_enc) { |
| 563 | msgb_tlv_put(msg, OSMO_GSUP_CHARG_CHAR_IE, |
| 564 | gsup_msg->pdp_charg_enc_len, gsup_msg->pdp_charg_enc); |
| 565 | } |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 566 | |
| 567 | return 0; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 568 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 569 | |
| 570 | /*! @} */ |