blob: b3c8dab5f9733e068862cc37e2e1eaf5be7db7cf [file] [log] [blame]
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +01001/* GPRS Subscriber Update Protocol message encoder/decoder */
2
3/*
4 * (C) 2014 by Sysmocom s.f.m.c. GmbH
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -04005 * (C) 2015 by Holger Hans Peter Freyther
Harald Welte121e9a42016-04-20 13:13:19 +02006 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +01007 * 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
26#include <openbsc/gprs_gsup_messages.h>
27
28#include <openbsc/debug.h>
29#include <openbsc/gprs_utils.h>
Neels Hofmeyrd48f0572015-10-12 11:57:33 +020030#include <openbsc/utils.h>
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010031
32#include <osmocom/gsm/tlv.h>
33#include <osmocom/core/msgb.h>
Harald Welted3fa84d2016-04-20 17:50:17 +020034#include <osmocom/gsm/gsm48_ie.h>
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010035
36#include <stdint.h>
37
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010038static int decode_pdp_info(uint8_t *data, size_t data_len,
39 struct gprs_gsup_pdp_info *pdp_info)
40{
41 int rc;
42 uint8_t tag;
43 uint8_t *value;
44 size_t value_len;
45
46 /* specific parts */
47 while (data_len > 0) {
48 enum gprs_gsup_iei iei;
49
50 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
51 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +010052 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010053
54 iei = tag;
55
56 switch (iei) {
57 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
58 pdp_info->context_id = decode_big_endian(value, value_len);
59 break;
60
61 case GPRS_GSUP_PDP_TYPE_IE:
62 pdp_info->pdp_type =
63 decode_big_endian(value, value_len) & 0x0fff;
64 break;
65
66 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
67 pdp_info->apn_enc = value;
68 pdp_info->apn_enc_len = value_len;
69 break;
70
Holger Hans Peter Freyther49c1a712015-04-23 09:13:01 -040071 case GPRS_GSUP_PDP_QOS_IE:
72 pdp_info->qos_enc = value;
73 pdp_info->qos_enc_len = value_len;
74 break;
75
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010076 default:
77 LOGP(DGPRS, LOGL_ERROR,
78 "GSUP IE type %d not expected in PDP info\n", iei);
79 continue;
80 }
81 }
82
83 return 0;
84}
85
86static int decode_auth_info(uint8_t *data, size_t data_len,
Harald Welte121e9a42016-04-20 13:13:19 +020087 struct osmo_auth_vector *auth_vector)
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010088{
89 int rc;
90 uint8_t tag;
91 uint8_t *value;
92 size_t value_len;
93 enum gprs_gsup_iei iei;
94
95 /* specific parts */
96 while (data_len > 0) {
97 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
98 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +010099 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100100
101 iei = tag;
102
103 switch (iei) {
104 case GPRS_GSUP_RAND_IE:
Harald Welte121e9a42016-04-20 13:13:19 +0200105 if (value_len != sizeof(auth_vector->rand))
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100106 goto parse_error;
107
Harald Welte121e9a42016-04-20 13:13:19 +0200108 memcpy(auth_vector->rand, value, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100109 break;
110
111 case GPRS_GSUP_SRES_IE:
Harald Welte121e9a42016-04-20 13:13:19 +0200112 if (value_len != sizeof(auth_vector->sres))
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100113 goto parse_error;
114
Harald Welte121e9a42016-04-20 13:13:19 +0200115 memcpy(auth_vector->sres, value, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100116 break;
117
118 case GPRS_GSUP_KC_IE:
Harald Welte121e9a42016-04-20 13:13:19 +0200119 if (value_len != sizeof(auth_vector->kc))
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100120 goto parse_error;
121
Harald Welte121e9a42016-04-20 13:13:19 +0200122 memcpy(auth_vector->kc, value, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100123 break;
124
125 default:
126 LOGP(DGPRS, LOGL_ERROR,
127 "GSUP IE type %d not expected in PDP info\n", iei);
128 continue;
129 }
130 }
131
132 return 0;
133
134parse_error:
135 LOGP(DGPRS, LOGL_ERROR,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400136 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100137
138 return -1;
139}
140
141int gprs_gsup_decode(const uint8_t *const_data, size_t data_len,
142 struct gprs_gsup_message *gsup_msg)
143{
144 int rc;
145 uint8_t tag;
146 /* the shift/match functions expect non-const pointers, but we'll
147 * either copy the data or cast pointers back to const before returning
148 * them
149 */
150 uint8_t *data = (uint8_t *)const_data;
151 uint8_t *value;
152 size_t value_len;
153 static const struct gprs_gsup_pdp_info empty_pdp_info = {0};
Harald Welte121e9a42016-04-20 13:13:19 +0200154 static const struct osmo_auth_vector empty_auth_info = {0};
Jacob Erlbeck16106262015-01-12 13:54:39 +0100155 static const struct gprs_gsup_message empty_gsup_message = {0};
156
157 *gsup_msg = empty_gsup_message;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100158
159 /* generic part */
Jacob Erlbeck424ffa42015-01-12 13:23:05 +0100160 rc = gprs_shift_v_fixed(&data, &data_len, 1, &value);
161 if (rc < 0)
162 return -GMM_CAUSE_INV_MAND_INFO;
163
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100164 gsup_msg->message_type = decode_big_endian(value, 1);
165
166 rc = gprs_match_tlv(&data, &data_len, GPRS_GSUP_IMSI_IE,
167 &value, &value_len);
168
169 if (rc <= 0)
170 return -GMM_CAUSE_INV_MAND_INFO;
171
172 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
173 return -GMM_CAUSE_INV_MAND_INFO;
174
175 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
176 * octets in the first octet. By coincidence (the TLV encoding) the byte
177 * before the value part already contains this length so we can use it
178 * here.
179 */
180 OSMO_ASSERT(value[-1] == value_len);
181 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
182 value - 1, 0);
183
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100184 /* specific parts */
185 while (data_len > 0) {
186 enum gprs_gsup_iei iei;
187 struct gprs_gsup_pdp_info pdp_info;
Harald Welte121e9a42016-04-20 13:13:19 +0200188 struct osmo_auth_vector auth_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100189
190 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
191 if (rc < 0)
192 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
193
194 iei = tag;
195
196 switch (iei) {
197 case GPRS_GSUP_IMSI_IE:
198 case GPRS_GSUP_PDP_TYPE_IE:
199 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
200 case GPRS_GSUP_RAND_IE:
201 case GPRS_GSUP_SRES_IE:
202 case GPRS_GSUP_KC_IE:
203 LOGP(DGPRS, LOGL_NOTICE,
204 "GSUP IE type %d not expected (ignored)\n", iei);
205 continue;
206
207 case GPRS_GSUP_CAUSE_IE:
208 gsup_msg->cause = decode_big_endian(value, value_len);
209 break;
210
211 case GPRS_GSUP_CANCEL_TYPE_IE:
212 gsup_msg->cancel_type =
213 decode_big_endian(value, value_len) + 1;
214 break;
215
216 case GPRS_GSUP_PDP_INFO_COMPL_IE:
217 gsup_msg->pdp_info_compl = 1;
218 break;
219
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100220 case GPRS_GSUP_FREEZE_PTMSI_IE:
221 gsup_msg->freeze_ptmsi = 1;
222 break;
223
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100224 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
225 /* When these IE appear in the top-level part of the
226 * message, they are used by Delete Subscr Info to delete
227 * single entries. We don't have an extra list for
228 * these but use the PDP info list instead */
229
230 /* fall through */
231
232 case GPRS_GSUP_PDP_INFO_IE:
233 if (gsup_msg->num_pdp_infos >= GPRS_GSUP_MAX_NUM_PDP_INFO) {
234 LOGP(DGPRS, LOGL_ERROR,
235 "GSUP IE type %d (PDP_INFO) max exceeded\n",
236 iei);
237 return -GMM_CAUSE_COND_IE_ERR;
238 }
239
Jacob Erlbeck16106262015-01-12 13:54:39 +0100240 pdp_info = empty_pdp_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100241
242 if (iei == GPRS_GSUP_PDP_INFO_IE) {
243 rc = decode_pdp_info(value, value_len, &pdp_info);
244 if (rc < 0)
245 return rc;
246 pdp_info.have_info = 1;
247 } else {
248 pdp_info.context_id =
249 decode_big_endian(value, value_len);
250 }
251
252 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
253 pdp_info;
254 break;
255
256 case GPRS_GSUP_AUTH_TUPLE_IE:
Harald Welte121e9a42016-04-20 13:13:19 +0200257 if (gsup_msg->num_auth_vectors >= GPRS_GSUP_MAX_NUM_AUTH_INFO) {
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100258 LOGP(DGPRS, LOGL_ERROR,
259 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
260 iei);
261 return -GMM_CAUSE_INV_MAND_INFO;
262 }
263
Jacob Erlbeck16106262015-01-12 13:54:39 +0100264 auth_info = empty_auth_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100265
266 rc = decode_auth_info(value, value_len, &auth_info);
267 if (rc < 0)
268 return rc;
269
Harald Welte121e9a42016-04-20 13:13:19 +0200270 gsup_msg->auth_vectors[gsup_msg->num_auth_vectors++] =
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100271 auth_info;
272 break;
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400273
274 case GPRS_GSUP_MSISDN_IE:
275 gsup_msg->msisdn_enc = value;
276 gsup_msg->msisdn_enc_len = value_len;
277 break;
278
Holger Hans Peter Freyther0bb56742015-05-17 19:56:38 +0200279 case GPRS_GSUP_HLR_NUMBER_IE:
280 gsup_msg->hlr_enc = value;
281 gsup_msg->hlr_enc_len = value_len;
282 break;
283
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100284 default:
285 LOGP(DGPRS, LOGL_NOTICE,
286 "GSUP IE type %d unknown\n", iei);
287 continue;
288 }
289 }
290
291 return 0;
292}
293
294static void encode_pdp_info(struct msgb *msg, enum gprs_gsup_iei iei,
295 const struct gprs_gsup_pdp_info *pdp_info)
296{
297 uint8_t *len_field;
298 size_t old_len;
299 uint8_t u8;
300
301 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
302 old_len = msgb_length(msg);
303
304 u8 = pdp_info->context_id;
305 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
306
307 if (pdp_info->pdp_type) {
308 msgb_tlv_put(msg, GPRS_GSUP_PDP_TYPE_IE,
309 GPRS_GSUP_PDP_TYPE_SIZE,
310 encode_big_endian(pdp_info->pdp_type | 0xf000,
311 GPRS_GSUP_PDP_TYPE_SIZE));
312 }
313
314 if (pdp_info->apn_enc) {
315 msgb_tlv_put(msg, GPRS_GSUP_ACCESS_POINT_NAME_IE,
316 pdp_info->apn_enc_len, pdp_info->apn_enc);
317 }
318
Holger Hans Peter Freyther49c1a712015-04-23 09:13:01 -0400319 if (pdp_info->qos_enc) {
320 msgb_tlv_put(msg, GPRS_GSUP_PDP_QOS_IE,
321 pdp_info->qos_enc_len, pdp_info->qos_enc);
322 }
323
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100324 /* Update length field */
325 *len_field = msgb_length(msg) - old_len;
326}
327
328static void encode_auth_info(struct msgb *msg, enum gprs_gsup_iei iei,
Harald Welte121e9a42016-04-20 13:13:19 +0200329 const struct osmo_auth_vector *auth_vector)
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100330{
331 uint8_t *len_field;
332 size_t old_len;
333
334 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
335 old_len = msgb_length(msg);
336
337 msgb_tlv_put(msg, GPRS_GSUP_RAND_IE,
Harald Welte121e9a42016-04-20 13:13:19 +0200338 sizeof(auth_vector->rand), auth_vector->rand);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100339
340 msgb_tlv_put(msg, GPRS_GSUP_SRES_IE,
Harald Welte121e9a42016-04-20 13:13:19 +0200341 sizeof(auth_vector->sres), auth_vector->sres);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100342
343 msgb_tlv_put(msg, GPRS_GSUP_KC_IE,
Harald Welte121e9a42016-04-20 13:13:19 +0200344 sizeof(auth_vector->kc), auth_vector->kc);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100345
346 /* Update length field */
347 *len_field = msgb_length(msg) - old_len;
348}
349
350void gprs_gsup_encode(struct msgb *msg, const struct gprs_gsup_message *gsup_msg)
351{
352 uint8_t u8;
353 int idx;
354 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
355 size_t bcd_len;
356
357 /* generic part */
358 OSMO_ASSERT(gsup_msg->message_type);
359 msgb_v_put(msg, gsup_msg->message_type);
360
361 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
362 gsup_msg->imsi);
363
364 OSMO_ASSERT(bcd_len > 1);
365
366 /* Note that gsm48_encode_bcd_number puts the length into the first
367 * octet. Since msgb_tlv_put will add this length byte, we'll have to
368 * skip it */
369 msgb_tlv_put(msg, GPRS_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
370
371 /* specific parts */
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400372 if (gsup_msg->msisdn_enc)
373 msgb_tlv_put(msg, GPRS_GSUP_MSISDN_IE,
374 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
Holger Hans Peter Freyther0bb56742015-05-17 19:56:38 +0200375 if (gsup_msg->hlr_enc)
376 msgb_tlv_put(msg, GPRS_GSUP_HLR_NUMBER_IE,
377 gsup_msg->hlr_enc_len, gsup_msg->hlr_enc);
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400378
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100379 if ((u8 = gsup_msg->cause))
380 msgb_tlv_put(msg, GPRS_GSUP_CAUSE_IE, sizeof(u8), &u8);
381
382 if ((u8 = gsup_msg->cancel_type)) {
383 u8 -= 1;
384 msgb_tlv_put(msg, GPRS_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
385 }
386
387 if (gsup_msg->pdp_info_compl)
388 msgb_tlv_put(msg, GPRS_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
389
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100390 if (gsup_msg->freeze_ptmsi)
391 msgb_tlv_put(msg, GPRS_GSUP_FREEZE_PTMSI_IE, 0, &u8);
392
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100393 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
394 const struct gprs_gsup_pdp_info *pdp_info;
395
396 pdp_info = &gsup_msg->pdp_infos[idx];
397
398 if (pdp_info->context_id == 0)
399 continue;
400
401 if (pdp_info->have_info) {
402 encode_pdp_info(msg, GPRS_GSUP_PDP_INFO_IE, pdp_info);
403 } else {
404 u8 = pdp_info->context_id;
405 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE,
406 sizeof(u8), &u8);
407 }
408 }
409
Harald Welte121e9a42016-04-20 13:13:19 +0200410 for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) {
411 const struct osmo_auth_vector *auth_vector;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100412
Harald Welte121e9a42016-04-20 13:13:19 +0200413 auth_vector = &gsup_msg->auth_vectors[idx];
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100414
Harald Welte121e9a42016-04-20 13:13:19 +0200415 encode_auth_info(msg, GPRS_GSUP_AUTH_TUPLE_IE, auth_vector);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100416 }
417}