blob: bdcff5f6957cc66c15f63dc42f8b4fa4ada4fa30 [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
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +01006 * All Rights Reserved
7 *
8 * Author: Jacob Erlbeck
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
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
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <openbsc/gprs_gsup_messages.h>
26
27#include <openbsc/debug.h>
28#include <openbsc/gprs_utils.h>
29
30#include <osmocom/gsm/tlv.h>
31#include <osmocom/core/msgb.h>
32
33#include <stdint.h>
34
35
36static uint64_t decode_big_endian(const uint8_t *data, size_t data_len)
37{
38 uint64_t value = 0;
39
40 while (data_len > 0) {
41 value = (value << 8) + *data;
42 data += 1;
43 data_len -= 1;
44 }
45
46 return value;
47}
48
49static uint8_t *encode_big_endian(uint64_t value, size_t data_len)
50{
51 static uint8_t buf[sizeof(uint64_t)];
52 int idx;
53
54 OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
55
56 for (idx = data_len - 1; idx >= 0; idx--) {
57 buf[idx] = (uint8_t)value;
58 value = value >> 8;
59 }
60
61 return buf;
62}
63
64static int decode_pdp_info(uint8_t *data, size_t data_len,
65 struct gprs_gsup_pdp_info *pdp_info)
66{
67 int rc;
68 uint8_t tag;
69 uint8_t *value;
70 size_t value_len;
71
72 /* specific parts */
73 while (data_len > 0) {
74 enum gprs_gsup_iei iei;
75
76 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
77 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +010078 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010079
80 iei = tag;
81
82 switch (iei) {
83 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
84 pdp_info->context_id = decode_big_endian(value, value_len);
85 break;
86
87 case GPRS_GSUP_PDP_TYPE_IE:
88 pdp_info->pdp_type =
89 decode_big_endian(value, value_len) & 0x0fff;
90 break;
91
92 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
93 pdp_info->apn_enc = value;
94 pdp_info->apn_enc_len = value_len;
95 break;
96
Holger Hans Peter Freyther49c1a712015-04-23 09:13:01 -040097 case GPRS_GSUP_PDP_QOS_IE:
98 pdp_info->qos_enc = value;
99 pdp_info->qos_enc_len = value_len;
100 break;
101
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100102 default:
103 LOGP(DGPRS, LOGL_ERROR,
104 "GSUP IE type %d not expected in PDP info\n", iei);
105 continue;
106 }
107 }
108
109 return 0;
110}
111
112static int decode_auth_info(uint8_t *data, size_t data_len,
113 struct gsm_auth_tuple *auth_tuple)
114{
115 int rc;
116 uint8_t tag;
117 uint8_t *value;
118 size_t value_len;
119 enum gprs_gsup_iei iei;
120
121 /* specific parts */
122 while (data_len > 0) {
123 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
124 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100125 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100126
127 iei = tag;
128
129 switch (iei) {
130 case GPRS_GSUP_RAND_IE:
131 if (value_len != sizeof(auth_tuple->rand))
132 goto parse_error;
133
134 memcpy(auth_tuple->rand, value, value_len);
135 break;
136
137 case GPRS_GSUP_SRES_IE:
138 if (value_len != sizeof(auth_tuple->sres))
139 goto parse_error;
140
141 memcpy(auth_tuple->sres, value, value_len);
142 break;
143
144 case GPRS_GSUP_KC_IE:
145 if (value_len != sizeof(auth_tuple->kc))
146 goto parse_error;
147
148 memcpy(auth_tuple->kc, value, value_len);
149 break;
150
151 default:
152 LOGP(DGPRS, LOGL_ERROR,
153 "GSUP IE type %d not expected in PDP info\n", iei);
154 continue;
155 }
156 }
157
158 return 0;
159
160parse_error:
161 LOGP(DGPRS, LOGL_ERROR,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400162 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100163
164 return -1;
165}
166
167int gprs_gsup_decode(const uint8_t *const_data, size_t data_len,
168 struct gprs_gsup_message *gsup_msg)
169{
170 int rc;
171 uint8_t tag;
172 /* the shift/match functions expect non-const pointers, but we'll
173 * either copy the data or cast pointers back to const before returning
174 * them
175 */
176 uint8_t *data = (uint8_t *)const_data;
177 uint8_t *value;
178 size_t value_len;
179 static const struct gprs_gsup_pdp_info empty_pdp_info = {0};
180 static const struct gsm_auth_tuple empty_auth_info = {0};
Jacob Erlbeck16106262015-01-12 13:54:39 +0100181 static const struct gprs_gsup_message empty_gsup_message = {0};
182
183 *gsup_msg = empty_gsup_message;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100184
185 /* generic part */
Jacob Erlbeck424ffa42015-01-12 13:23:05 +0100186 rc = gprs_shift_v_fixed(&data, &data_len, 1, &value);
187 if (rc < 0)
188 return -GMM_CAUSE_INV_MAND_INFO;
189
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100190 gsup_msg->message_type = decode_big_endian(value, 1);
191
192 rc = gprs_match_tlv(&data, &data_len, GPRS_GSUP_IMSI_IE,
193 &value, &value_len);
194
195 if (rc <= 0)
196 return -GMM_CAUSE_INV_MAND_INFO;
197
198 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
199 return -GMM_CAUSE_INV_MAND_INFO;
200
201 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
202 * octets in the first octet. By coincidence (the TLV encoding) the byte
203 * before the value part already contains this length so we can use it
204 * here.
205 */
206 OSMO_ASSERT(value[-1] == value_len);
207 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
208 value - 1, 0);
209
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100210 /* specific parts */
211 while (data_len > 0) {
212 enum gprs_gsup_iei iei;
213 struct gprs_gsup_pdp_info pdp_info;
214 struct gsm_auth_tuple auth_info;
215
216 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
217 if (rc < 0)
218 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
219
220 iei = tag;
221
222 switch (iei) {
223 case GPRS_GSUP_IMSI_IE:
224 case GPRS_GSUP_PDP_TYPE_IE:
225 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
226 case GPRS_GSUP_RAND_IE:
227 case GPRS_GSUP_SRES_IE:
228 case GPRS_GSUP_KC_IE:
229 LOGP(DGPRS, LOGL_NOTICE,
230 "GSUP IE type %d not expected (ignored)\n", iei);
231 continue;
232
233 case GPRS_GSUP_CAUSE_IE:
234 gsup_msg->cause = decode_big_endian(value, value_len);
235 break;
236
237 case GPRS_GSUP_CANCEL_TYPE_IE:
238 gsup_msg->cancel_type =
239 decode_big_endian(value, value_len) + 1;
240 break;
241
242 case GPRS_GSUP_PDP_INFO_COMPL_IE:
243 gsup_msg->pdp_info_compl = 1;
244 break;
245
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100246 case GPRS_GSUP_FREEZE_PTMSI_IE:
247 gsup_msg->freeze_ptmsi = 1;
248 break;
249
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100250 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
251 /* When these IE appear in the top-level part of the
252 * message, they are used by Delete Subscr Info to delete
253 * single entries. We don't have an extra list for
254 * these but use the PDP info list instead */
255
256 /* fall through */
257
258 case GPRS_GSUP_PDP_INFO_IE:
259 if (gsup_msg->num_pdp_infos >= GPRS_GSUP_MAX_NUM_PDP_INFO) {
260 LOGP(DGPRS, LOGL_ERROR,
261 "GSUP IE type %d (PDP_INFO) max exceeded\n",
262 iei);
263 return -GMM_CAUSE_COND_IE_ERR;
264 }
265
Jacob Erlbeck16106262015-01-12 13:54:39 +0100266 pdp_info = empty_pdp_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100267
268 if (iei == GPRS_GSUP_PDP_INFO_IE) {
269 rc = decode_pdp_info(value, value_len, &pdp_info);
270 if (rc < 0)
271 return rc;
272 pdp_info.have_info = 1;
273 } else {
274 pdp_info.context_id =
275 decode_big_endian(value, value_len);
276 }
277
278 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
279 pdp_info;
280 break;
281
282 case GPRS_GSUP_AUTH_TUPLE_IE:
283 if (gsup_msg->num_auth_tuples >= GPRS_GSUP_MAX_NUM_AUTH_INFO) {
284 LOGP(DGPRS, LOGL_ERROR,
285 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
286 iei);
287 return -GMM_CAUSE_INV_MAND_INFO;
288 }
289
Jacob Erlbeck16106262015-01-12 13:54:39 +0100290 auth_info = empty_auth_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100291 auth_info.key_seq = gsup_msg->num_auth_tuples;
292
293 rc = decode_auth_info(value, value_len, &auth_info);
294 if (rc < 0)
295 return rc;
296
297 gsup_msg->auth_tuples[gsup_msg->num_auth_tuples++] =
298 auth_info;
299 break;
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400300
301 case GPRS_GSUP_MSISDN_IE:
302 gsup_msg->msisdn_enc = value;
303 gsup_msg->msisdn_enc_len = value_len;
304 break;
305
Holger Hans Peter Freyther0bb56742015-05-17 19:56:38 +0200306 case GPRS_GSUP_HLR_NUMBER_IE:
307 gsup_msg->hlr_enc = value;
308 gsup_msg->hlr_enc_len = value_len;
309 break;
310
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100311 default:
312 LOGP(DGPRS, LOGL_NOTICE,
313 "GSUP IE type %d unknown\n", iei);
314 continue;
315 }
316 }
317
318 return 0;
319}
320
321static void encode_pdp_info(struct msgb *msg, enum gprs_gsup_iei iei,
322 const struct gprs_gsup_pdp_info *pdp_info)
323{
324 uint8_t *len_field;
325 size_t old_len;
326 uint8_t u8;
327
328 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
329 old_len = msgb_length(msg);
330
331 u8 = pdp_info->context_id;
332 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
333
334 if (pdp_info->pdp_type) {
335 msgb_tlv_put(msg, GPRS_GSUP_PDP_TYPE_IE,
336 GPRS_GSUP_PDP_TYPE_SIZE,
337 encode_big_endian(pdp_info->pdp_type | 0xf000,
338 GPRS_GSUP_PDP_TYPE_SIZE));
339 }
340
341 if (pdp_info->apn_enc) {
342 msgb_tlv_put(msg, GPRS_GSUP_ACCESS_POINT_NAME_IE,
343 pdp_info->apn_enc_len, pdp_info->apn_enc);
344 }
345
Holger Hans Peter Freyther49c1a712015-04-23 09:13:01 -0400346 if (pdp_info->qos_enc) {
347 msgb_tlv_put(msg, GPRS_GSUP_PDP_QOS_IE,
348 pdp_info->qos_enc_len, pdp_info->qos_enc);
349 }
350
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100351 /* Update length field */
352 *len_field = msgb_length(msg) - old_len;
353}
354
355static void encode_auth_info(struct msgb *msg, enum gprs_gsup_iei iei,
356 const struct gsm_auth_tuple *auth_tuple)
357{
358 uint8_t *len_field;
359 size_t old_len;
360
361 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
362 old_len = msgb_length(msg);
363
364 msgb_tlv_put(msg, GPRS_GSUP_RAND_IE,
365 sizeof(auth_tuple->rand), auth_tuple->rand);
366
367 msgb_tlv_put(msg, GPRS_GSUP_SRES_IE,
368 sizeof(auth_tuple->sres), auth_tuple->sres);
369
370 msgb_tlv_put(msg, GPRS_GSUP_KC_IE,
371 sizeof(auth_tuple->kc), auth_tuple->kc);
372
373 /* Update length field */
374 *len_field = msgb_length(msg) - old_len;
375}
376
377void gprs_gsup_encode(struct msgb *msg, const struct gprs_gsup_message *gsup_msg)
378{
379 uint8_t u8;
380 int idx;
381 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
382 size_t bcd_len;
383
384 /* generic part */
385 OSMO_ASSERT(gsup_msg->message_type);
386 msgb_v_put(msg, gsup_msg->message_type);
387
388 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
389 gsup_msg->imsi);
390
391 OSMO_ASSERT(bcd_len > 1);
392
393 /* Note that gsm48_encode_bcd_number puts the length into the first
394 * octet. Since msgb_tlv_put will add this length byte, we'll have to
395 * skip it */
396 msgb_tlv_put(msg, GPRS_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
397
398 /* specific parts */
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400399 if (gsup_msg->msisdn_enc)
400 msgb_tlv_put(msg, GPRS_GSUP_MSISDN_IE,
401 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
Holger Hans Peter Freyther0bb56742015-05-17 19:56:38 +0200402 if (gsup_msg->hlr_enc)
403 msgb_tlv_put(msg, GPRS_GSUP_HLR_NUMBER_IE,
404 gsup_msg->hlr_enc_len, gsup_msg->hlr_enc);
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400405
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100406 if ((u8 = gsup_msg->cause))
407 msgb_tlv_put(msg, GPRS_GSUP_CAUSE_IE, sizeof(u8), &u8);
408
409 if ((u8 = gsup_msg->cancel_type)) {
410 u8 -= 1;
411 msgb_tlv_put(msg, GPRS_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
412 }
413
414 if (gsup_msg->pdp_info_compl)
415 msgb_tlv_put(msg, GPRS_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
416
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100417 if (gsup_msg->freeze_ptmsi)
418 msgb_tlv_put(msg, GPRS_GSUP_FREEZE_PTMSI_IE, 0, &u8);
419
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100420 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
421 const struct gprs_gsup_pdp_info *pdp_info;
422
423 pdp_info = &gsup_msg->pdp_infos[idx];
424
425 if (pdp_info->context_id == 0)
426 continue;
427
428 if (pdp_info->have_info) {
429 encode_pdp_info(msg, GPRS_GSUP_PDP_INFO_IE, pdp_info);
430 } else {
431 u8 = pdp_info->context_id;
432 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE,
433 sizeof(u8), &u8);
434 }
435 }
436
437 for (idx = 0; idx < gsup_msg->num_auth_tuples; idx++) {
438 const struct gsm_auth_tuple *auth_info;
439
440 auth_info = &gsup_msg->auth_tuples[idx];
441
442 if (auth_info->key_seq == GSM_KEY_SEQ_INVAL)
443 continue;
444
445 encode_auth_info(msg, GPRS_GSUP_AUTH_TUPLE_IE, auth_info);
446 }
447}