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), |
Vadim Yanitskiy | 36c7b33 | 2018-03-31 05:23:09 +0700 | [diff] [blame] | 65 | |
| 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 | |
Vadim Yanitskiy | c262831 | 2018-09-25 23:03:13 +0700 | [diff] [blame] | 70 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST), |
| 71 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR), |
| 72 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT), |
| 73 | |
| 74 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST), |
| 75 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR), |
| 76 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT), |
| 77 | |
Vadim Yanitskiy | f9ee8da | 2018-11-13 02:06:15 +0700 | [diff] [blame] | 78 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST), |
| 79 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_READY_FOR_SM_ERROR), |
| 80 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_READY_FOR_SM_RESULT), |
| 81 | |
Oliver Smith | 10db281 | 2018-12-14 16:14:34 +0100 | [diff] [blame] | 82 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST), |
| 83 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_CHECK_IMEI_ERROR), |
| 84 | OSMO_VALUE_STRING(OSMO_GSUP_MSGT_CHECK_IMEI_RESULT), |
| 85 | |
Neels Hofmeyr | 10f5fb4 | 2017-02-09 02:09:09 +0100 | [diff] [blame] | 86 | { 0, NULL } |
| 87 | }; |
| 88 | |
Harald Welte | 6a6a609 | 2018-06-16 11:10:12 +0200 | [diff] [blame] | 89 | const struct value_string osmo_gsup_session_state_names[] = { |
| 90 | { OSMO_GSUP_SESSION_STATE_NONE, "NONE" }, |
| 91 | { OSMO_GSUP_SESSION_STATE_BEGIN, "BEGIN" }, |
| 92 | { OSMO_GSUP_SESSION_STATE_CONTINUE, "CONTINUE" }, |
| 93 | { OSMO_GSUP_SESSION_STATE_END, "END" }, |
| 94 | { 0, NULL } |
| 95 | }; |
| 96 | |
Harald Welte | 520ebc1 | 2018-06-11 20:27:27 +0200 | [diff] [blame] | 97 | |
Oliver Smith | d6ff9c8 | 2019-02-01 15:21:25 +0100 | [diff] [blame] | 98 | /*! return the error message type corresponding to \a type_in. |
| 99 | * Deprecated, use OSMO_GSUP_TO_MSGT_ERROR() instead. */ |
Harald Welte | 520ebc1 | 2018-06-11 20:27:27 +0200 | [diff] [blame] | 100 | int osmo_gsup_get_err_msg_type(enum osmo_gsup_message_type type_in) |
| 101 | { |
Oliver Smith | d6ff9c8 | 2019-02-01 15:21:25 +0100 | [diff] [blame] | 102 | return OSMO_GSUP_TO_MSGT_ERROR(type_in); |
Harald Welte | 520ebc1 | 2018-06-11 20:27:27 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 105 | static int decode_pdp_info(uint8_t *data, size_t data_len, |
| 106 | struct osmo_gsup_pdp_info *pdp_info) |
| 107 | { |
| 108 | int rc; |
| 109 | uint8_t tag; |
| 110 | uint8_t *value; |
| 111 | size_t value_len; |
| 112 | |
| 113 | /* specific parts */ |
| 114 | while (data_len > 0) { |
| 115 | enum osmo_gsup_iei iei; |
| 116 | |
| 117 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
| 118 | if (rc < 0) |
| 119 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 120 | |
| 121 | iei = tag; |
| 122 | |
| 123 | switch (iei) { |
| 124 | case OSMO_GSUP_PDP_CONTEXT_ID_IE: |
| 125 | pdp_info->context_id = osmo_decode_big_endian(value, value_len); |
| 126 | break; |
| 127 | |
| 128 | case OSMO_GSUP_PDP_TYPE_IE: |
| 129 | pdp_info->pdp_type = |
| 130 | osmo_decode_big_endian(value, value_len) & 0x0fff; |
| 131 | break; |
| 132 | |
| 133 | case OSMO_GSUP_ACCESS_POINT_NAME_IE: |
| 134 | pdp_info->apn_enc = value; |
| 135 | pdp_info->apn_enc_len = value_len; |
| 136 | break; |
| 137 | |
| 138 | case OSMO_GSUP_PDP_QOS_IE: |
| 139 | pdp_info->qos_enc = value; |
| 140 | pdp_info->qos_enc_len = value_len; |
| 141 | break; |
| 142 | |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 143 | case OSMO_GSUP_CHARG_CHAR_IE: |
| 144 | pdp_info->pdp_charg_enc = value; |
| 145 | pdp_info->pdp_charg_enc_len = value_len; |
| 146 | break; |
| 147 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 148 | default: |
| 149 | LOGP(DLGSUP, LOGL_ERROR, |
| 150 | "GSUP IE type %d not expected in PDP info\n", iei); |
| 151 | continue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int decode_auth_info(uint8_t *data, size_t data_len, |
| 159 | struct osmo_auth_vector *auth_vector) |
| 160 | { |
| 161 | int rc; |
| 162 | uint8_t tag; |
| 163 | uint8_t *value; |
| 164 | size_t value_len; |
| 165 | enum osmo_gsup_iei iei; |
| 166 | uint8_t presence = 0; |
| 167 | |
| 168 | /* specific parts */ |
| 169 | while (data_len > 0) { |
| 170 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
| 171 | if (rc < 0) |
| 172 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 173 | |
| 174 | iei = tag; |
| 175 | |
| 176 | switch (iei) { |
| 177 | case OSMO_GSUP_RAND_IE: |
| 178 | if (value_len != sizeof(auth_vector->rand)) |
| 179 | goto parse_error; |
| 180 | |
| 181 | memcpy(auth_vector->rand, value, value_len); |
| 182 | presence |= (1 << 0); |
| 183 | break; |
| 184 | |
| 185 | case OSMO_GSUP_SRES_IE: |
| 186 | if (value_len != sizeof(auth_vector->sres)) |
| 187 | goto parse_error; |
| 188 | |
| 189 | memcpy(auth_vector->sres, value, value_len); |
| 190 | presence |= (1 << 1); |
| 191 | break; |
| 192 | |
| 193 | case OSMO_GSUP_KC_IE: |
| 194 | if (value_len != sizeof(auth_vector->kc)) |
| 195 | goto parse_error; |
| 196 | |
| 197 | memcpy(auth_vector->kc, value, value_len); |
| 198 | presence |= (1 << 2); |
| 199 | break; |
| 200 | |
| 201 | case OSMO_GSUP_IK_IE: |
| 202 | if (value_len != sizeof(auth_vector->ik)) |
| 203 | goto parse_error; |
| 204 | memcpy(auth_vector->ik, value, value_len); |
| 205 | presence |= (1 << 4); |
| 206 | break; |
| 207 | |
| 208 | case OSMO_GSUP_CK_IE: |
| 209 | if (value_len != sizeof(auth_vector->ck)) |
| 210 | goto parse_error; |
| 211 | memcpy(auth_vector->ck, value, value_len); |
| 212 | presence |= (1 << 5); |
| 213 | break; |
| 214 | |
| 215 | case OSMO_GSUP_AUTN_IE: |
| 216 | if (value_len != sizeof(auth_vector->autn)) |
| 217 | goto parse_error; |
| 218 | memcpy(auth_vector->autn, value, value_len); |
| 219 | presence |= (1 << 6); |
| 220 | break; |
| 221 | case OSMO_GSUP_RES_IE: |
| 222 | if (value_len > sizeof(auth_vector->res)) |
| 223 | goto parse_error; |
| 224 | memcpy(auth_vector->res, value, value_len); |
| 225 | auth_vector->res_len = value_len; |
| 226 | presence |= (1 << 7); |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | LOGP(DLGSUP, LOGL_ERROR, |
| 231 | "GSUP IE type %d not expected in PDP info\n", iei); |
| 232 | continue; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (presence & 0x07) |
| 237 | auth_vector->auth_types |= OSMO_AUTH_TYPE_GSM; |
| 238 | if (presence & 0xf0) |
| 239 | auth_vector->auth_types |= OSMO_AUTH_TYPE_UMTS; |
| 240 | |
| 241 | return 0; |
| 242 | |
| 243 | parse_error: |
| 244 | LOGP(DLGSUP, LOGL_ERROR, |
| 245 | "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len); |
| 246 | |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | /*! Decode (parse) a GSUP message |
| 251 | * \param[in] const_data input data to be parsed |
| 252 | * \param[in] data_len length of input (\a const_data) |
| 253 | * \param[out] gsup_msg callee-allocated output data structure |
| 254 | * \returns 0 on success; negative otherwise |
| 255 | */ |
| 256 | int osmo_gsup_decode(const uint8_t *const_data, size_t data_len, |
| 257 | struct osmo_gsup_message *gsup_msg) |
| 258 | { |
| 259 | int rc; |
| 260 | uint8_t tag; |
| 261 | /* the shift/match functions expect non-const pointers, but we'll |
| 262 | * either copy the data or cast pointers back to const before returning |
| 263 | * them |
| 264 | */ |
| 265 | uint8_t *data = (uint8_t *)const_data; |
| 266 | uint8_t *value; |
| 267 | size_t value_len; |
| 268 | static const struct osmo_gsup_pdp_info empty_pdp_info = {0}; |
Neels Hofmeyr | 505adee | 2016-07-13 16:55:43 +0200 | [diff] [blame] | 269 | static const struct osmo_auth_vector empty_auth_info = {{0}}; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 270 | static const struct osmo_gsup_message empty_gsup_message = {0}; |
| 271 | |
| 272 | *gsup_msg = empty_gsup_message; |
| 273 | |
| 274 | /* generic part */ |
| 275 | rc = osmo_shift_v_fixed(&data, &data_len, 1, &value); |
| 276 | if (rc < 0) |
| 277 | return -GMM_CAUSE_INV_MAND_INFO; |
| 278 | |
| 279 | gsup_msg->message_type = osmo_decode_big_endian(value, 1); |
| 280 | |
| 281 | rc = osmo_match_shift_tlv(&data, &data_len, OSMO_GSUP_IMSI_IE, |
| 282 | &value, &value_len); |
| 283 | |
| 284 | if (rc <= 0) |
| 285 | return -GMM_CAUSE_INV_MAND_INFO; |
| 286 | |
| 287 | if (value_len * 2 + 1 > sizeof(gsup_msg->imsi)) |
| 288 | return -GMM_CAUSE_INV_MAND_INFO; |
| 289 | |
| 290 | /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI |
| 291 | * octets in the first octet. By coincidence (the TLV encoding) the byte |
| 292 | * before the value part already contains this length so we can use it |
| 293 | * here. |
| 294 | */ |
| 295 | OSMO_ASSERT(value[-1] == value_len); |
| 296 | gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi), |
| 297 | value - 1, 0); |
| 298 | |
| 299 | /* specific parts */ |
| 300 | while (data_len > 0) { |
| 301 | enum osmo_gsup_iei iei; |
| 302 | struct osmo_gsup_pdp_info pdp_info; |
| 303 | struct osmo_auth_vector auth_info; |
| 304 | |
| 305 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
| 306 | if (rc < 0) |
| 307 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 308 | |
| 309 | iei = tag; |
| 310 | |
| 311 | switch (iei) { |
| 312 | case OSMO_GSUP_IMSI_IE: |
| 313 | case OSMO_GSUP_PDP_TYPE_IE: |
| 314 | case OSMO_GSUP_ACCESS_POINT_NAME_IE: |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 315 | case OSMO_GSUP_SRES_IE: |
| 316 | case OSMO_GSUP_KC_IE: |
| 317 | LOGP(DLGSUP, LOGL_NOTICE, |
| 318 | "GSUP IE type %d not expected (ignored)\n", iei); |
| 319 | continue; |
| 320 | |
| 321 | case OSMO_GSUP_CAUSE_IE: |
| 322 | gsup_msg->cause = osmo_decode_big_endian(value, value_len); |
| 323 | break; |
| 324 | |
| 325 | case OSMO_GSUP_CANCEL_TYPE_IE: |
| 326 | gsup_msg->cancel_type = |
| 327 | osmo_decode_big_endian(value, value_len) + 1; |
| 328 | break; |
| 329 | |
| 330 | case OSMO_GSUP_PDP_INFO_COMPL_IE: |
| 331 | gsup_msg->pdp_info_compl = 1; |
| 332 | break; |
| 333 | |
| 334 | case OSMO_GSUP_FREEZE_PTMSI_IE: |
| 335 | gsup_msg->freeze_ptmsi = 1; |
| 336 | break; |
| 337 | |
| 338 | case OSMO_GSUP_PDP_CONTEXT_ID_IE: |
| 339 | /* When these IE appear in the top-level part of the |
| 340 | * message, they are used by Delete Subscr Info to delete |
| 341 | * single entries. We don't have an extra list for |
| 342 | * these but use the PDP info list instead */ |
| 343 | |
| 344 | /* fall through */ |
| 345 | |
| 346 | case OSMO_GSUP_PDP_INFO_IE: |
| 347 | if (gsup_msg->num_pdp_infos >= OSMO_GSUP_MAX_NUM_PDP_INFO) { |
| 348 | LOGP(DLGSUP, LOGL_ERROR, |
| 349 | "GSUP IE type %d (PDP_INFO) max exceeded\n", |
| 350 | iei); |
| 351 | return -GMM_CAUSE_COND_IE_ERR; |
| 352 | } |
| 353 | |
| 354 | pdp_info = empty_pdp_info; |
| 355 | |
| 356 | if (iei == OSMO_GSUP_PDP_INFO_IE) { |
| 357 | rc = decode_pdp_info(value, value_len, &pdp_info); |
| 358 | if (rc < 0) |
| 359 | return rc; |
| 360 | pdp_info.have_info = 1; |
| 361 | } else { |
| 362 | pdp_info.context_id = |
| 363 | osmo_decode_big_endian(value, value_len); |
| 364 | } |
| 365 | |
| 366 | gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] = |
| 367 | pdp_info; |
| 368 | break; |
| 369 | |
| 370 | case OSMO_GSUP_AUTH_TUPLE_IE: |
| 371 | if (gsup_msg->num_auth_vectors >= OSMO_GSUP_MAX_NUM_AUTH_INFO) { |
| 372 | LOGP(DLGSUP, LOGL_ERROR, |
| 373 | "GSUP IE type %d (AUTH_INFO) max exceeded\n", |
| 374 | iei); |
| 375 | return -GMM_CAUSE_INV_MAND_INFO; |
| 376 | } |
| 377 | |
| 378 | auth_info = empty_auth_info; |
| 379 | |
| 380 | rc = decode_auth_info(value, value_len, &auth_info); |
| 381 | if (rc < 0) |
| 382 | return rc; |
| 383 | |
| 384 | gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] = |
| 385 | auth_info; |
| 386 | break; |
| 387 | |
| 388 | case OSMO_GSUP_AUTS_IE: |
Neels Hofmeyr | 3a5ca64 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 389 | if (value_len != 14) { |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 390 | LOGP(DLGSUP, LOGL_ERROR, |
Neels Hofmeyr | 3a5ca64 | 2017-02-21 15:53:20 +0100 | [diff] [blame] | 391 | "AUTS length != 14 received\n"); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 392 | return -GMM_CAUSE_COND_IE_ERR; |
| 393 | } |
| 394 | gsup_msg->auts = value; |
| 395 | break; |
| 396 | |
Harald Welte | 766da86 | 2016-05-06 11:18:15 +0200 | [diff] [blame] | 397 | case OSMO_GSUP_RAND_IE: |
| 398 | if (value_len != 16) { |
| 399 | LOGP(DLGSUP, LOGL_ERROR, |
| 400 | "RAND length != 16 received\n"); |
| 401 | return -GMM_CAUSE_COND_IE_ERR; |
| 402 | } |
| 403 | gsup_msg->rand = value; |
| 404 | break; |
| 405 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 406 | case OSMO_GSUP_MSISDN_IE: |
| 407 | gsup_msg->msisdn_enc = value; |
| 408 | gsup_msg->msisdn_enc_len = value_len; |
| 409 | break; |
| 410 | |
| 411 | case OSMO_GSUP_HLR_NUMBER_IE: |
| 412 | gsup_msg->hlr_enc = value; |
| 413 | gsup_msg->hlr_enc_len = value_len; |
| 414 | break; |
| 415 | |
Harald Welte | 48dc1a5 | 2016-05-05 18:46:42 +0200 | [diff] [blame] | 416 | case OSMO_GSUP_CN_DOMAIN_IE: |
| 417 | gsup_msg->cn_domain = *value; |
| 418 | break; |
| 419 | |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 420 | case OSMO_GSUP_CHARG_CHAR_IE: |
| 421 | gsup_msg->pdp_charg_enc = value; |
| 422 | gsup_msg->pdp_charg_enc_len = value_len; |
| 423 | break; |
| 424 | |
Vadim Yanitskiy | 7269604 | 2018-04-07 02:34:55 +0700 | [diff] [blame] | 425 | case OSMO_GSUP_SESSION_ID_IE: |
| 426 | gsup_msg->session_id = osmo_decode_big_endian(value, value_len); |
| 427 | break; |
| 428 | |
| 429 | case OSMO_GSUP_SESSION_STATE_IE: |
| 430 | gsup_msg->session_state = *value; |
| 431 | break; |
| 432 | |
Vadim Yanitskiy | 36c7b33 | 2018-03-31 05:23:09 +0700 | [diff] [blame] | 433 | case OSMO_GSUP_SS_INFO_IE: |
| 434 | gsup_msg->ss_info = value; |
| 435 | gsup_msg->ss_info_len = value_len; |
| 436 | break; |
| 437 | |
Vadim Yanitskiy | c262831 | 2018-09-25 23:03:13 +0700 | [diff] [blame] | 438 | case OSMO_GSUP_SM_RP_MR_IE: |
| 439 | gsup_msg->sm_rp_mr = value; |
| 440 | break; |
| 441 | |
| 442 | case OSMO_GSUP_SM_RP_DA_IE: |
| 443 | rc = osmo_gsup_sms_decode_sm_rp_da(gsup_msg, value, value_len); |
| 444 | if (rc) |
| 445 | return rc; |
| 446 | break; |
| 447 | |
| 448 | case OSMO_GSUP_SM_RP_OA_IE: |
| 449 | rc = osmo_gsup_sms_decode_sm_rp_oa(gsup_msg, value, value_len); |
| 450 | if (rc) |
| 451 | return rc; |
| 452 | break; |
| 453 | |
| 454 | case OSMO_GSUP_SM_RP_UI_IE: |
| 455 | gsup_msg->sm_rp_ui = value; |
| 456 | gsup_msg->sm_rp_ui_len = value_len; |
| 457 | break; |
| 458 | |
| 459 | case OSMO_GSUP_SM_RP_MMS_IE: |
| 460 | gsup_msg->sm_rp_mms = value; |
| 461 | break; |
| 462 | |
| 463 | case OSMO_GSUP_SM_RP_CAUSE_IE: |
| 464 | gsup_msg->sm_rp_cause = value; |
| 465 | break; |
| 466 | |
Vadim Yanitskiy | f9ee8da | 2018-11-13 02:06:15 +0700 | [diff] [blame] | 467 | case OSMO_GSUP_SM_ALERT_RSN_IE: |
| 468 | gsup_msg->sm_alert_rsn = *value; |
| 469 | break; |
| 470 | |
Oliver Smith | 10db281 | 2018-12-14 16:14:34 +0100 | [diff] [blame] | 471 | case OSMO_GSUP_IMEI_IE: |
| 472 | gsup_msg->imei_enc = value; |
| 473 | gsup_msg->imei_enc_len = value_len; |
| 474 | break; |
| 475 | |
| 476 | case OSMO_GSUP_IMEI_RESULT_IE: |
| 477 | gsup_msg->imei_result = osmo_decode_big_endian(value, value_len) + 1; |
| 478 | break; |
| 479 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 480 | default: |
| 481 | LOGP(DLGSUP, LOGL_NOTICE, |
| 482 | "GSUP IE type %d unknown\n", iei); |
| 483 | continue; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static void encode_pdp_info(struct msgb *msg, enum osmo_gsup_iei iei, |
| 491 | const struct osmo_gsup_pdp_info *pdp_info) |
| 492 | { |
| 493 | uint8_t *len_field; |
| 494 | size_t old_len; |
| 495 | uint8_t u8; |
| 496 | |
| 497 | len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1; |
| 498 | old_len = msgb_length(msg); |
| 499 | |
| 500 | u8 = pdp_info->context_id; |
| 501 | msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8); |
| 502 | |
| 503 | if (pdp_info->pdp_type) { |
| 504 | msgb_tlv_put(msg, OSMO_GSUP_PDP_TYPE_IE, |
| 505 | OSMO_GSUP_PDP_TYPE_SIZE, |
| 506 | osmo_encode_big_endian(pdp_info->pdp_type | 0xf000, |
| 507 | OSMO_GSUP_PDP_TYPE_SIZE)); |
| 508 | } |
| 509 | |
| 510 | if (pdp_info->apn_enc) { |
| 511 | msgb_tlv_put(msg, OSMO_GSUP_ACCESS_POINT_NAME_IE, |
| 512 | pdp_info->apn_enc_len, pdp_info->apn_enc); |
| 513 | } |
| 514 | |
| 515 | if (pdp_info->qos_enc) { |
| 516 | msgb_tlv_put(msg, OSMO_GSUP_PDP_QOS_IE, |
| 517 | pdp_info->qos_enc_len, pdp_info->qos_enc); |
| 518 | } |
| 519 | |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 520 | if (pdp_info->pdp_charg_enc) { |
| 521 | msgb_tlv_put(msg, OSMO_GSUP_CHARG_CHAR_IE, |
| 522 | pdp_info->pdp_charg_enc_len, pdp_info->pdp_charg_enc); |
| 523 | } |
| 524 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 525 | /* Update length field */ |
| 526 | *len_field = msgb_length(msg) - old_len; |
| 527 | } |
| 528 | |
| 529 | static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei, |
| 530 | const struct osmo_auth_vector *auth_vector) |
| 531 | { |
| 532 | uint8_t *len_field; |
| 533 | size_t old_len; |
| 534 | |
| 535 | len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1; |
| 536 | old_len = msgb_length(msg); |
| 537 | |
Harald Welte | db78d21 | 2016-06-06 13:47:07 +0200 | [diff] [blame] | 538 | if (auth_vector->auth_types & OSMO_AUTH_TYPE_GSM) { |
| 539 | msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, |
| 540 | sizeof(auth_vector->rand), auth_vector->rand); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 541 | |
Harald Welte | db78d21 | 2016-06-06 13:47:07 +0200 | [diff] [blame] | 542 | msgb_tlv_put(msg, OSMO_GSUP_SRES_IE, |
| 543 | sizeof(auth_vector->sres), auth_vector->sres); |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 544 | |
Harald Welte | db78d21 | 2016-06-06 13:47:07 +0200 | [diff] [blame] | 545 | msgb_tlv_put(msg, OSMO_GSUP_KC_IE, |
| 546 | sizeof(auth_vector->kc), auth_vector->kc); |
| 547 | } |
| 548 | |
| 549 | if (auth_vector->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 550 | msgb_tlv_put(msg, OSMO_GSUP_IK_IE, |
| 551 | sizeof(auth_vector->ik), auth_vector->ik); |
| 552 | |
| 553 | msgb_tlv_put(msg, OSMO_GSUP_CK_IE, |
| 554 | sizeof(auth_vector->ck), auth_vector->ck); |
| 555 | |
| 556 | msgb_tlv_put(msg, OSMO_GSUP_AUTN_IE, |
| 557 | sizeof(auth_vector->autn), auth_vector->autn); |
| 558 | |
| 559 | msgb_tlv_put(msg, OSMO_GSUP_RES_IE, |
| 560 | auth_vector->res_len, auth_vector->res); |
| 561 | } |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 562 | |
| 563 | /* Update length field */ |
| 564 | *len_field = msgb_length(msg) - old_len; |
| 565 | } |
| 566 | |
| 567 | /*! Encode a GSUP message |
| 568 | * \param[out] msg message buffer to which encoded message is written |
| 569 | * \param[in] gsup_msg \ref osmo_gsup_message data to be encoded |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 570 | * \returns 0 on success; negative otherwise |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 571 | */ |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 572 | 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] | 573 | { |
| 574 | uint8_t u8; |
Vadim Yanitskiy | c262831 | 2018-09-25 23:03:13 +0700 | [diff] [blame] | 575 | int idx, rc; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 576 | uint8_t bcd_buf[GSM48_MI_SIZE] = {0}; |
| 577 | size_t bcd_len; |
| 578 | |
| 579 | /* generic part */ |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 580 | if(!gsup_msg->message_type) |
Vadim Yanitskiy | ee86b04 | 2018-09-25 18:09:01 +0700 | [diff] [blame] | 581 | return -EINVAL; |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 582 | |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 583 | msgb_v_put(msg, gsup_msg->message_type); |
| 584 | |
| 585 | bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0, |
| 586 | gsup_msg->imsi); |
| 587 | |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 588 | if (bcd_len <= 0 || bcd_len > sizeof(bcd_buf)) |
| 589 | return -EINVAL; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 590 | |
| 591 | /* Note that gsm48_encode_bcd_number puts the length into the first |
| 592 | * octet. Since msgb_tlv_put will add this length byte, we'll have to |
| 593 | * skip it */ |
| 594 | msgb_tlv_put(msg, OSMO_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]); |
| 595 | |
| 596 | /* specific parts */ |
| 597 | if (gsup_msg->msisdn_enc) |
| 598 | msgb_tlv_put(msg, OSMO_GSUP_MSISDN_IE, |
| 599 | gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc); |
| 600 | if (gsup_msg->hlr_enc) |
| 601 | msgb_tlv_put(msg, OSMO_GSUP_HLR_NUMBER_IE, |
| 602 | gsup_msg->hlr_enc_len, gsup_msg->hlr_enc); |
| 603 | |
| 604 | if ((u8 = gsup_msg->cause)) |
| 605 | msgb_tlv_put(msg, OSMO_GSUP_CAUSE_IE, sizeof(u8), &u8); |
| 606 | |
| 607 | if ((u8 = gsup_msg->cancel_type)) { |
| 608 | u8 -= 1; |
| 609 | msgb_tlv_put(msg, OSMO_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8); |
| 610 | } |
| 611 | |
| 612 | if (gsup_msg->pdp_info_compl) |
| 613 | msgb_tlv_put(msg, OSMO_GSUP_PDP_INFO_COMPL_IE, 0, &u8); |
| 614 | |
| 615 | if (gsup_msg->freeze_ptmsi) |
| 616 | msgb_tlv_put(msg, OSMO_GSUP_FREEZE_PTMSI_IE, 0, &u8); |
| 617 | |
| 618 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
| 619 | const struct osmo_gsup_pdp_info *pdp_info; |
| 620 | |
| 621 | pdp_info = &gsup_msg->pdp_infos[idx]; |
| 622 | |
| 623 | if (pdp_info->context_id == 0) |
| 624 | continue; |
| 625 | |
| 626 | if (pdp_info->have_info) { |
| 627 | encode_pdp_info(msg, OSMO_GSUP_PDP_INFO_IE, pdp_info); |
| 628 | } else { |
| 629 | u8 = pdp_info->context_id; |
| 630 | msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, |
| 631 | sizeof(u8), &u8); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) { |
| 636 | const struct osmo_auth_vector *auth_vector; |
| 637 | |
| 638 | auth_vector = &gsup_msg->auth_vectors[idx]; |
| 639 | |
| 640 | encode_auth_info(msg, OSMO_GSUP_AUTH_TUPLE_IE, auth_vector); |
| 641 | } |
| 642 | |
| 643 | if (gsup_msg->auts) |
Neels Hofmeyr | 8352d31 | 2017-02-02 20:05:14 +0100 | [diff] [blame] | 644 | msgb_tlv_put(msg, OSMO_GSUP_AUTS_IE, 14, gsup_msg->auts); |
Harald Welte | 48dc1a5 | 2016-05-05 18:46:42 +0200 | [diff] [blame] | 645 | |
Harald Welte | 766da86 | 2016-05-06 11:18:15 +0200 | [diff] [blame] | 646 | if (gsup_msg->rand) |
| 647 | msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, 16, gsup_msg->rand); |
| 648 | |
Harald Welte | 48dc1a5 | 2016-05-05 18:46:42 +0200 | [diff] [blame] | 649 | if (gsup_msg->cn_domain) { |
| 650 | uint8_t dn = gsup_msg->cn_domain; |
| 651 | msgb_tlv_put(msg, OSMO_GSUP_CN_DOMAIN_IE, 1, &dn); |
| 652 | } |
Holger Hans Peter Freyther | eb55c0d | 2017-07-07 16:53:30 +0200 | [diff] [blame] | 653 | |
| 654 | if (gsup_msg->pdp_charg_enc) { |
| 655 | msgb_tlv_put(msg, OSMO_GSUP_CHARG_CHAR_IE, |
| 656 | gsup_msg->pdp_charg_enc_len, gsup_msg->pdp_charg_enc); |
| 657 | } |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 658 | |
Vadim Yanitskiy | 7269604 | 2018-04-07 02:34:55 +0700 | [diff] [blame] | 659 | if ((u8 = gsup_msg->session_state)) { |
| 660 | size_t len = sizeof(gsup_msg->session_id); |
| 661 | uint8_t *sid = osmo_encode_big_endian(gsup_msg->session_id, len); |
| 662 | |
| 663 | msgb_tlv_put(msg, OSMO_GSUP_SESSION_ID_IE, len, sid); |
| 664 | msgb_tlv_put(msg, OSMO_GSUP_SESSION_STATE_IE, sizeof(u8), &u8); |
| 665 | } |
| 666 | |
Vadim Yanitskiy | 36c7b33 | 2018-03-31 05:23:09 +0700 | [diff] [blame] | 667 | if (gsup_msg->ss_info) { |
| 668 | msgb_tlv_put(msg, OSMO_GSUP_SS_INFO_IE, |
| 669 | gsup_msg->ss_info_len, gsup_msg->ss_info); |
| 670 | } |
| 671 | |
Vadim Yanitskiy | c262831 | 2018-09-25 23:03:13 +0700 | [diff] [blame] | 672 | if (gsup_msg->sm_rp_mr) { |
| 673 | msgb_tlv_put(msg, OSMO_GSUP_SM_RP_MR_IE, |
| 674 | sizeof(*gsup_msg->sm_rp_mr), gsup_msg->sm_rp_mr); |
| 675 | } |
| 676 | |
| 677 | if (gsup_msg->sm_rp_da_type) { |
| 678 | rc = osmo_gsup_sms_encode_sm_rp_da(msg, gsup_msg); |
| 679 | if (rc) { |
| 680 | LOGP(DLGSUP, LOGL_ERROR, "Failed to encode SM-RP-DA IE\n"); |
| 681 | return -EINVAL; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if (gsup_msg->sm_rp_oa_type) { |
| 686 | rc = osmo_gsup_sms_encode_sm_rp_oa(msg, gsup_msg); |
| 687 | if (rc) { |
| 688 | LOGP(DLGSUP, LOGL_ERROR, "Failed to encode SM-RP-OA IE\n"); |
| 689 | return -EINVAL; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (gsup_msg->sm_rp_ui) { |
| 694 | msgb_tlv_put(msg, OSMO_GSUP_SM_RP_UI_IE, |
| 695 | gsup_msg->sm_rp_ui_len, gsup_msg->sm_rp_ui); |
| 696 | } |
| 697 | |
| 698 | if (gsup_msg->sm_rp_mms) { |
| 699 | msgb_tlv_put(msg, OSMO_GSUP_SM_RP_MMS_IE, |
| 700 | sizeof(*gsup_msg->sm_rp_mms), gsup_msg->sm_rp_mms); |
| 701 | } |
| 702 | |
| 703 | if (gsup_msg->sm_rp_cause) { |
| 704 | msgb_tlv_put(msg, OSMO_GSUP_SM_RP_CAUSE_IE, |
| 705 | sizeof(*gsup_msg->sm_rp_cause), gsup_msg->sm_rp_cause); |
| 706 | } |
| 707 | |
Vadim Yanitskiy | f9ee8da | 2018-11-13 02:06:15 +0700 | [diff] [blame] | 708 | if ((u8 = gsup_msg->sm_alert_rsn)) { |
| 709 | msgb_tlv_put(msg, OSMO_GSUP_SM_ALERT_RSN_IE, |
| 710 | sizeof(u8), &u8); |
| 711 | } |
| 712 | |
Oliver Smith | 10db281 | 2018-12-14 16:14:34 +0100 | [diff] [blame] | 713 | if (gsup_msg->imei_enc) |
| 714 | msgb_tlv_put(msg, OSMO_GSUP_IMEI_IE, gsup_msg->imei_enc_len, gsup_msg->imei_enc); |
| 715 | |
| 716 | if ((u8 = gsup_msg->imei_result)) { |
| 717 | u8 -= 1; |
| 718 | msgb_tlv_put(msg, OSMO_GSUP_IMEI_RESULT_IE, sizeof(u8), &u8); |
| 719 | } |
| 720 | |
Max | 80f4c4e | 2018-01-24 12:33:05 +0100 | [diff] [blame] | 721 | return 0; |
Harald Welte | 3b6fb08 | 2016-04-25 18:46:22 +0200 | [diff] [blame] | 722 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 723 | |
| 724 | /*! @} */ |