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