Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 1 | /* Osmocom Subscriber Update Protocol message encoder/decoder */ |
Jacob Erlbeck | f3a271f | 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 | b927f1c | 2015-04-22 23:09:41 -0400 | [diff] [blame] | 5 | * (C) 2015 by Holger Hans Peter Freyther |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 6 | * (C) 2016 by Harald Welte <laforge@gnumonks.org> |
Jacob Erlbeck | f3a271f | 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 | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 26 | #include <openbsc/osmo_gsup_messages.h> |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 27 | |
| 28 | #include <openbsc/debug.h> |
Neels Hofmeyr | d48f057 | 2015-10-12 11:57:33 +0200 | [diff] [blame] | 29 | #include <openbsc/utils.h> |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 30 | |
| 31 | #include <osmocom/gsm/tlv.h> |
| 32 | #include <osmocom/core/msgb.h> |
Harald Welte | d3fa84d | 2016-04-20 17:50:17 +0200 | [diff] [blame] | 33 | #include <osmocom/gsm/gsm48_ie.h> |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 34 | |
| 35 | #include <stdint.h> |
| 36 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 37 | static int decode_pdp_info(uint8_t *data, size_t data_len, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 38 | struct osmo_gsup_pdp_info *pdp_info) |
Jacob Erlbeck | f3a271f | 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 | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 47 | enum osmo_gsup_iei iei; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 48 | |
Harald Welte | 842674b | 2016-04-25 15:14:01 +0200 | [diff] [blame] | 49 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 50 | if (rc < 0) |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 51 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 52 | |
| 53 | iei = tag; |
| 54 | |
| 55 | switch (iei) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 56 | case OSMO_GSUP_PDP_CONTEXT_ID_IE: |
Jacob Erlbeck | f3a271f | 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 | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 60 | case OSMO_GSUP_PDP_TYPE_IE: |
Jacob Erlbeck | f3a271f | 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 | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 65 | case OSMO_GSUP_ACCESS_POINT_NAME_IE: |
Jacob Erlbeck | f3a271f | 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 | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 70 | case OSMO_GSUP_PDP_QOS_IE: |
Holger Hans Peter Freyther | 49c1a71 | 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 | f3a271f | 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 | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 86 | struct osmo_auth_vector *auth_vector) |
Jacob Erlbeck | f3a271f | 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 | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 92 | enum osmo_gsup_iei iei; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 93 | |
| 94 | /* specific parts */ |
| 95 | while (data_len > 0) { |
Harald Welte | 842674b | 2016-04-25 15:14:01 +0200 | [diff] [blame] | 96 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 97 | if (rc < 0) |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 98 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 99 | |
| 100 | iei = tag; |
| 101 | |
| 102 | switch (iei) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 103 | case OSMO_GSUP_RAND_IE: |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 104 | if (value_len != sizeof(auth_vector->rand)) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 105 | goto parse_error; |
| 106 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 107 | memcpy(auth_vector->rand, value, value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 108 | break; |
| 109 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 110 | case OSMO_GSUP_SRES_IE: |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 111 | if (value_len != sizeof(auth_vector->sres)) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 112 | goto parse_error; |
| 113 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 114 | memcpy(auth_vector->sres, value, value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 115 | break; |
| 116 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 117 | case OSMO_GSUP_KC_IE: |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 118 | if (value_len != sizeof(auth_vector->kc)) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 119 | goto parse_error; |
| 120 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 121 | memcpy(auth_vector->kc, value, value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 122 | break; |
| 123 | |
| 124 | default: |
| 125 | LOGP(DGPRS, LOGL_ERROR, |
| 126 | "GSUP IE type %d not expected in PDP info\n", iei); |
| 127 | continue; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | |
| 133 | parse_error: |
| 134 | LOGP(DGPRS, LOGL_ERROR, |
Holger Hans Peter Freyther | 8e6ecc9 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 135 | "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 136 | |
| 137 | return -1; |
| 138 | } |
| 139 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 140 | int osmo_gsup_decode(const uint8_t *const_data, size_t data_len, |
| 141 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 142 | { |
| 143 | int rc; |
| 144 | uint8_t tag; |
| 145 | /* the shift/match functions expect non-const pointers, but we'll |
| 146 | * either copy the data or cast pointers back to const before returning |
| 147 | * them |
| 148 | */ |
| 149 | uint8_t *data = (uint8_t *)const_data; |
| 150 | uint8_t *value; |
| 151 | size_t value_len; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 152 | static const struct osmo_gsup_pdp_info empty_pdp_info = {0}; |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 153 | static const struct osmo_auth_vector empty_auth_info = {0}; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 154 | static const struct osmo_gsup_message empty_gsup_message = {0}; |
Jacob Erlbeck | 1610626 | 2015-01-12 13:54:39 +0100 | [diff] [blame] | 155 | |
| 156 | *gsup_msg = empty_gsup_message; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 157 | |
| 158 | /* generic part */ |
Harald Welte | 842674b | 2016-04-25 15:14:01 +0200 | [diff] [blame] | 159 | rc = osmo_shift_v_fixed(&data, &data_len, 1, &value); |
Jacob Erlbeck | 424ffa4 | 2015-01-12 13:23:05 +0100 | [diff] [blame] | 160 | if (rc < 0) |
| 161 | return -GMM_CAUSE_INV_MAND_INFO; |
| 162 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 163 | gsup_msg->message_type = decode_big_endian(value, 1); |
| 164 | |
Harald Welte | 842674b | 2016-04-25 15:14:01 +0200 | [diff] [blame] | 165 | rc = osmo_match_shift_tlv(&data, &data_len, OSMO_GSUP_IMSI_IE, |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 166 | &value, &value_len); |
| 167 | |
| 168 | if (rc <= 0) |
| 169 | return -GMM_CAUSE_INV_MAND_INFO; |
| 170 | |
| 171 | if (value_len * 2 + 1 > sizeof(gsup_msg->imsi)) |
| 172 | return -GMM_CAUSE_INV_MAND_INFO; |
| 173 | |
| 174 | /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI |
| 175 | * octets in the first octet. By coincidence (the TLV encoding) the byte |
| 176 | * before the value part already contains this length so we can use it |
| 177 | * here. |
| 178 | */ |
| 179 | OSMO_ASSERT(value[-1] == value_len); |
| 180 | gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi), |
| 181 | value - 1, 0); |
| 182 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 183 | /* specific parts */ |
| 184 | while (data_len > 0) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 185 | enum osmo_gsup_iei iei; |
| 186 | struct osmo_gsup_pdp_info pdp_info; |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 187 | struct osmo_auth_vector auth_info; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 188 | |
Harald Welte | 842674b | 2016-04-25 15:14:01 +0200 | [diff] [blame] | 189 | rc = osmo_shift_tlv(&data, &data_len, &tag, &value, &value_len); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 190 | if (rc < 0) |
| 191 | return -GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 192 | |
| 193 | iei = tag; |
| 194 | |
| 195 | switch (iei) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 196 | case OSMO_GSUP_IMSI_IE: |
| 197 | case OSMO_GSUP_PDP_TYPE_IE: |
| 198 | case OSMO_GSUP_ACCESS_POINT_NAME_IE: |
| 199 | case OSMO_GSUP_RAND_IE: |
| 200 | case OSMO_GSUP_SRES_IE: |
| 201 | case OSMO_GSUP_KC_IE: |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 202 | LOGP(DGPRS, LOGL_NOTICE, |
| 203 | "GSUP IE type %d not expected (ignored)\n", iei); |
| 204 | continue; |
| 205 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 206 | case OSMO_GSUP_CAUSE_IE: |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 207 | gsup_msg->cause = decode_big_endian(value, value_len); |
| 208 | break; |
| 209 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 210 | case OSMO_GSUP_CANCEL_TYPE_IE: |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 211 | gsup_msg->cancel_type = |
| 212 | decode_big_endian(value, value_len) + 1; |
| 213 | break; |
| 214 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 215 | case OSMO_GSUP_PDP_INFO_COMPL_IE: |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 216 | gsup_msg->pdp_info_compl = 1; |
| 217 | break; |
| 218 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 219 | case OSMO_GSUP_FREEZE_PTMSI_IE: |
Jacob Erlbeck | 69d2713 | 2015-01-15 11:50:08 +0100 | [diff] [blame] | 220 | gsup_msg->freeze_ptmsi = 1; |
| 221 | break; |
| 222 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 223 | case OSMO_GSUP_PDP_CONTEXT_ID_IE: |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 224 | /* When these IE appear in the top-level part of the |
| 225 | * message, they are used by Delete Subscr Info to delete |
| 226 | * single entries. We don't have an extra list for |
| 227 | * these but use the PDP info list instead */ |
| 228 | |
| 229 | /* fall through */ |
| 230 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 231 | case OSMO_GSUP_PDP_INFO_IE: |
| 232 | if (gsup_msg->num_pdp_infos >= OSMO_GSUP_MAX_NUM_PDP_INFO) { |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 233 | LOGP(DGPRS, LOGL_ERROR, |
| 234 | "GSUP IE type %d (PDP_INFO) max exceeded\n", |
| 235 | iei); |
| 236 | return -GMM_CAUSE_COND_IE_ERR; |
| 237 | } |
| 238 | |
Jacob Erlbeck | 1610626 | 2015-01-12 13:54:39 +0100 | [diff] [blame] | 239 | pdp_info = empty_pdp_info; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 240 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 241 | if (iei == OSMO_GSUP_PDP_INFO_IE) { |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 242 | rc = decode_pdp_info(value, value_len, &pdp_info); |
| 243 | if (rc < 0) |
| 244 | return rc; |
| 245 | pdp_info.have_info = 1; |
| 246 | } else { |
| 247 | pdp_info.context_id = |
| 248 | decode_big_endian(value, value_len); |
| 249 | } |
| 250 | |
| 251 | gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] = |
| 252 | pdp_info; |
| 253 | break; |
| 254 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 255 | case OSMO_GSUP_AUTH_TUPLE_IE: |
| 256 | if (gsup_msg->num_auth_vectors >= OSMO_GSUP_MAX_NUM_AUTH_INFO) { |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 257 | LOGP(DGPRS, LOGL_ERROR, |
| 258 | "GSUP IE type %d (AUTH_INFO) max exceeded\n", |
| 259 | iei); |
| 260 | return -GMM_CAUSE_INV_MAND_INFO; |
| 261 | } |
| 262 | |
Jacob Erlbeck | 1610626 | 2015-01-12 13:54:39 +0100 | [diff] [blame] | 263 | auth_info = empty_auth_info; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 264 | |
| 265 | rc = decode_auth_info(value, value_len, &auth_info); |
| 266 | if (rc < 0) |
| 267 | return rc; |
| 268 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 269 | gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] = |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 270 | auth_info; |
| 271 | break; |
Holger Hans Peter Freyther | b927f1c | 2015-04-22 23:09:41 -0400 | [diff] [blame] | 272 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 273 | case OSMO_GSUP_MSISDN_IE: |
Holger Hans Peter Freyther | b927f1c | 2015-04-22 23:09:41 -0400 | [diff] [blame] | 274 | gsup_msg->msisdn_enc = value; |
| 275 | gsup_msg->msisdn_enc_len = value_len; |
| 276 | break; |
| 277 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 278 | case OSMO_GSUP_HLR_NUMBER_IE: |
Holger Hans Peter Freyther | 0bb5674 | 2015-05-17 19:56:38 +0200 | [diff] [blame] | 279 | gsup_msg->hlr_enc = value; |
| 280 | gsup_msg->hlr_enc_len = value_len; |
| 281 | break; |
| 282 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 283 | default: |
| 284 | LOGP(DGPRS, LOGL_NOTICE, |
| 285 | "GSUP IE type %d unknown\n", iei); |
| 286 | continue; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 293 | static void encode_pdp_info(struct msgb *msg, enum osmo_gsup_iei iei, |
| 294 | const struct osmo_gsup_pdp_info *pdp_info) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 295 | { |
| 296 | uint8_t *len_field; |
| 297 | size_t old_len; |
| 298 | uint8_t u8; |
| 299 | |
| 300 | len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1; |
| 301 | old_len = msgb_length(msg); |
| 302 | |
| 303 | u8 = pdp_info->context_id; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 304 | msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 305 | |
| 306 | if (pdp_info->pdp_type) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 307 | msgb_tlv_put(msg, OSMO_GSUP_PDP_TYPE_IE, |
| 308 | OSMO_GSUP_PDP_TYPE_SIZE, |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 309 | encode_big_endian(pdp_info->pdp_type | 0xf000, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 310 | OSMO_GSUP_PDP_TYPE_SIZE)); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if (pdp_info->apn_enc) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 314 | msgb_tlv_put(msg, OSMO_GSUP_ACCESS_POINT_NAME_IE, |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 315 | pdp_info->apn_enc_len, pdp_info->apn_enc); |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | 49c1a71 | 2015-04-23 09:13:01 -0400 | [diff] [blame] | 318 | if (pdp_info->qos_enc) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 319 | msgb_tlv_put(msg, OSMO_GSUP_PDP_QOS_IE, |
Holger Hans Peter Freyther | 49c1a71 | 2015-04-23 09:13:01 -0400 | [diff] [blame] | 320 | pdp_info->qos_enc_len, pdp_info->qos_enc); |
| 321 | } |
| 322 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 323 | /* Update length field */ |
| 324 | *len_field = msgb_length(msg) - old_len; |
| 325 | } |
| 326 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 327 | static void encode_auth_info(struct msgb *msg, enum osmo_gsup_iei iei, |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 328 | const struct osmo_auth_vector *auth_vector) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 329 | { |
| 330 | uint8_t *len_field; |
| 331 | size_t old_len; |
| 332 | |
| 333 | len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1; |
| 334 | old_len = msgb_length(msg); |
| 335 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 336 | msgb_tlv_put(msg, OSMO_GSUP_RAND_IE, |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 337 | sizeof(auth_vector->rand), auth_vector->rand); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 338 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 339 | msgb_tlv_put(msg, OSMO_GSUP_SRES_IE, |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 340 | sizeof(auth_vector->sres), auth_vector->sres); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 341 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 342 | msgb_tlv_put(msg, OSMO_GSUP_KC_IE, |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 343 | sizeof(auth_vector->kc), auth_vector->kc); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 344 | |
| 345 | /* Update length field */ |
| 346 | *len_field = msgb_length(msg) - old_len; |
| 347 | } |
| 348 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 349 | void osmo_gsup_encode(struct msgb *msg, const struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 350 | { |
| 351 | uint8_t u8; |
| 352 | int idx; |
| 353 | uint8_t bcd_buf[GSM48_MI_SIZE] = {0}; |
| 354 | size_t bcd_len; |
| 355 | |
| 356 | /* generic part */ |
| 357 | OSMO_ASSERT(gsup_msg->message_type); |
| 358 | msgb_v_put(msg, gsup_msg->message_type); |
| 359 | |
| 360 | bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0, |
| 361 | gsup_msg->imsi); |
| 362 | |
| 363 | OSMO_ASSERT(bcd_len > 1); |
| 364 | |
| 365 | /* Note that gsm48_encode_bcd_number puts the length into the first |
| 366 | * octet. Since msgb_tlv_put will add this length byte, we'll have to |
| 367 | * skip it */ |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 368 | msgb_tlv_put(msg, OSMO_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 369 | |
| 370 | /* specific parts */ |
Holger Hans Peter Freyther | b927f1c | 2015-04-22 23:09:41 -0400 | [diff] [blame] | 371 | if (gsup_msg->msisdn_enc) |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 372 | msgb_tlv_put(msg, OSMO_GSUP_MSISDN_IE, |
Holger Hans Peter Freyther | b927f1c | 2015-04-22 23:09:41 -0400 | [diff] [blame] | 373 | gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc); |
Holger Hans Peter Freyther | 0bb5674 | 2015-05-17 19:56:38 +0200 | [diff] [blame] | 374 | if (gsup_msg->hlr_enc) |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 375 | msgb_tlv_put(msg, OSMO_GSUP_HLR_NUMBER_IE, |
Holger Hans Peter Freyther | 0bb5674 | 2015-05-17 19:56:38 +0200 | [diff] [blame] | 376 | gsup_msg->hlr_enc_len, gsup_msg->hlr_enc); |
Holger Hans Peter Freyther | b927f1c | 2015-04-22 23:09:41 -0400 | [diff] [blame] | 377 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 378 | if ((u8 = gsup_msg->cause)) |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 379 | msgb_tlv_put(msg, OSMO_GSUP_CAUSE_IE, sizeof(u8), &u8); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 380 | |
| 381 | if ((u8 = gsup_msg->cancel_type)) { |
| 382 | u8 -= 1; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 383 | msgb_tlv_put(msg, OSMO_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (gsup_msg->pdp_info_compl) |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 387 | msgb_tlv_put(msg, OSMO_GSUP_PDP_INFO_COMPL_IE, 0, &u8); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 388 | |
Jacob Erlbeck | 69d2713 | 2015-01-15 11:50:08 +0100 | [diff] [blame] | 389 | if (gsup_msg->freeze_ptmsi) |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 390 | msgb_tlv_put(msg, OSMO_GSUP_FREEZE_PTMSI_IE, 0, &u8); |
Jacob Erlbeck | 69d2713 | 2015-01-15 11:50:08 +0100 | [diff] [blame] | 391 | |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 392 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 393 | const struct osmo_gsup_pdp_info *pdp_info; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 394 | |
| 395 | pdp_info = &gsup_msg->pdp_infos[idx]; |
| 396 | |
| 397 | if (pdp_info->context_id == 0) |
| 398 | continue; |
| 399 | |
| 400 | if (pdp_info->have_info) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 401 | encode_pdp_info(msg, OSMO_GSUP_PDP_INFO_IE, pdp_info); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 402 | } else { |
| 403 | u8 = pdp_info->context_id; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 404 | msgb_tlv_put(msg, OSMO_GSUP_PDP_CONTEXT_ID_IE, |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 405 | sizeof(u8), &u8); |
| 406 | } |
| 407 | } |
| 408 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 409 | for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) { |
| 410 | const struct osmo_auth_vector *auth_vector; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 411 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 412 | auth_vector = &gsup_msg->auth_vectors[idx]; |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 413 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 414 | encode_auth_info(msg, OSMO_GSUP_AUTH_TUPLE_IE, auth_vector); |
Jacob Erlbeck | f3a271f | 2014-12-11 16:54:14 +0100 | [diff] [blame] | 415 | } |
| 416 | } |