blob: 56f78fd12ba38d4332da0ba29075de5d9ca39b0f [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
97 default:
98 LOGP(DGPRS, LOGL_ERROR,
99 "GSUP IE type %d not expected in PDP info\n", iei);
100 continue;
101 }
102 }
103
104 return 0;
105}
106
107static int decode_auth_info(uint8_t *data, size_t data_len,
108 struct gsm_auth_tuple *auth_tuple)
109{
110 int rc;
111 uint8_t tag;
112 uint8_t *value;
113 size_t value_len;
114 enum gprs_gsup_iei iei;
115
116 /* specific parts */
117 while (data_len > 0) {
118 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
119 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +0100120 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100121
122 iei = tag;
123
124 switch (iei) {
125 case GPRS_GSUP_RAND_IE:
126 if (value_len != sizeof(auth_tuple->rand))
127 goto parse_error;
128
129 memcpy(auth_tuple->rand, value, value_len);
130 break;
131
132 case GPRS_GSUP_SRES_IE:
133 if (value_len != sizeof(auth_tuple->sres))
134 goto parse_error;
135
136 memcpy(auth_tuple->sres, value, value_len);
137 break;
138
139 case GPRS_GSUP_KC_IE:
140 if (value_len != sizeof(auth_tuple->kc))
141 goto parse_error;
142
143 memcpy(auth_tuple->kc, value, value_len);
144 break;
145
146 default:
147 LOGP(DGPRS, LOGL_ERROR,
148 "GSUP IE type %d not expected in PDP info\n", iei);
149 continue;
150 }
151 }
152
153 return 0;
154
155parse_error:
156 LOGP(DGPRS, LOGL_ERROR,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400157 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100158
159 return -1;
160}
161
162int gprs_gsup_decode(const uint8_t *const_data, size_t data_len,
163 struct gprs_gsup_message *gsup_msg)
164{
165 int rc;
166 uint8_t tag;
167 /* the shift/match functions expect non-const pointers, but we'll
168 * either copy the data or cast pointers back to const before returning
169 * them
170 */
171 uint8_t *data = (uint8_t *)const_data;
172 uint8_t *value;
173 size_t value_len;
174 static const struct gprs_gsup_pdp_info empty_pdp_info = {0};
175 static const struct gsm_auth_tuple empty_auth_info = {0};
Jacob Erlbeck16106262015-01-12 13:54:39 +0100176 static const struct gprs_gsup_message empty_gsup_message = {0};
177
178 *gsup_msg = empty_gsup_message;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100179
180 /* generic part */
Jacob Erlbeck424ffa42015-01-12 13:23:05 +0100181 rc = gprs_shift_v_fixed(&data, &data_len, 1, &value);
182 if (rc < 0)
183 return -GMM_CAUSE_INV_MAND_INFO;
184
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100185 gsup_msg->message_type = decode_big_endian(value, 1);
186
187 rc = gprs_match_tlv(&data, &data_len, GPRS_GSUP_IMSI_IE,
188 &value, &value_len);
189
190 if (rc <= 0)
191 return -GMM_CAUSE_INV_MAND_INFO;
192
193 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
194 return -GMM_CAUSE_INV_MAND_INFO;
195
196 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
197 * octets in the first octet. By coincidence (the TLV encoding) the byte
198 * before the value part already contains this length so we can use it
199 * here.
200 */
201 OSMO_ASSERT(value[-1] == value_len);
202 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
203 value - 1, 0);
204
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100205 /* specific parts */
206 while (data_len > 0) {
207 enum gprs_gsup_iei iei;
208 struct gprs_gsup_pdp_info pdp_info;
209 struct gsm_auth_tuple auth_info;
210
211 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
212 if (rc < 0)
213 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
214
215 iei = tag;
216
217 switch (iei) {
218 case GPRS_GSUP_IMSI_IE:
219 case GPRS_GSUP_PDP_TYPE_IE:
220 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
221 case GPRS_GSUP_RAND_IE:
222 case GPRS_GSUP_SRES_IE:
223 case GPRS_GSUP_KC_IE:
224 LOGP(DGPRS, LOGL_NOTICE,
225 "GSUP IE type %d not expected (ignored)\n", iei);
226 continue;
227
228 case GPRS_GSUP_CAUSE_IE:
229 gsup_msg->cause = decode_big_endian(value, value_len);
230 break;
231
232 case GPRS_GSUP_CANCEL_TYPE_IE:
233 gsup_msg->cancel_type =
234 decode_big_endian(value, value_len) + 1;
235 break;
236
237 case GPRS_GSUP_PDP_INFO_COMPL_IE:
238 gsup_msg->pdp_info_compl = 1;
239 break;
240
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100241 case GPRS_GSUP_FREEZE_PTMSI_IE:
242 gsup_msg->freeze_ptmsi = 1;
243 break;
244
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100245 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
246 /* When these IE appear in the top-level part of the
247 * message, they are used by Delete Subscr Info to delete
248 * single entries. We don't have an extra list for
249 * these but use the PDP info list instead */
250
251 /* fall through */
252
253 case GPRS_GSUP_PDP_INFO_IE:
254 if (gsup_msg->num_pdp_infos >= GPRS_GSUP_MAX_NUM_PDP_INFO) {
255 LOGP(DGPRS, LOGL_ERROR,
256 "GSUP IE type %d (PDP_INFO) max exceeded\n",
257 iei);
258 return -GMM_CAUSE_COND_IE_ERR;
259 }
260
Jacob Erlbeck16106262015-01-12 13:54:39 +0100261 pdp_info = empty_pdp_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100262
263 if (iei == GPRS_GSUP_PDP_INFO_IE) {
264 rc = decode_pdp_info(value, value_len, &pdp_info);
265 if (rc < 0)
266 return rc;
267 pdp_info.have_info = 1;
268 } else {
269 pdp_info.context_id =
270 decode_big_endian(value, value_len);
271 }
272
273 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
274 pdp_info;
275 break;
276
277 case GPRS_GSUP_AUTH_TUPLE_IE:
278 if (gsup_msg->num_auth_tuples >= GPRS_GSUP_MAX_NUM_AUTH_INFO) {
279 LOGP(DGPRS, LOGL_ERROR,
280 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
281 iei);
282 return -GMM_CAUSE_INV_MAND_INFO;
283 }
284
Jacob Erlbeck16106262015-01-12 13:54:39 +0100285 auth_info = empty_auth_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100286 auth_info.key_seq = gsup_msg->num_auth_tuples;
287
288 rc = decode_auth_info(value, value_len, &auth_info);
289 if (rc < 0)
290 return rc;
291
292 gsup_msg->auth_tuples[gsup_msg->num_auth_tuples++] =
293 auth_info;
294 break;
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400295
296 case GPRS_GSUP_MSISDN_IE:
297 gsup_msg->msisdn_enc = value;
298 gsup_msg->msisdn_enc_len = value_len;
299 break;
300
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100301 default:
302 LOGP(DGPRS, LOGL_NOTICE,
303 "GSUP IE type %d unknown\n", iei);
304 continue;
305 }
306 }
307
308 return 0;
309}
310
311static void encode_pdp_info(struct msgb *msg, enum gprs_gsup_iei iei,
312 const struct gprs_gsup_pdp_info *pdp_info)
313{
314 uint8_t *len_field;
315 size_t old_len;
316 uint8_t u8;
317
318 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
319 old_len = msgb_length(msg);
320
321 u8 = pdp_info->context_id;
322 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
323
324 if (pdp_info->pdp_type) {
325 msgb_tlv_put(msg, GPRS_GSUP_PDP_TYPE_IE,
326 GPRS_GSUP_PDP_TYPE_SIZE,
327 encode_big_endian(pdp_info->pdp_type | 0xf000,
328 GPRS_GSUP_PDP_TYPE_SIZE));
329 }
330
331 if (pdp_info->apn_enc) {
332 msgb_tlv_put(msg, GPRS_GSUP_ACCESS_POINT_NAME_IE,
333 pdp_info->apn_enc_len, pdp_info->apn_enc);
334 }
335
336 /* Update length field */
337 *len_field = msgb_length(msg) - old_len;
338}
339
340static void encode_auth_info(struct msgb *msg, enum gprs_gsup_iei iei,
341 const struct gsm_auth_tuple *auth_tuple)
342{
343 uint8_t *len_field;
344 size_t old_len;
345
346 len_field = msgb_tlv_put(msg, iei, 0, NULL) - 1;
347 old_len = msgb_length(msg);
348
349 msgb_tlv_put(msg, GPRS_GSUP_RAND_IE,
350 sizeof(auth_tuple->rand), auth_tuple->rand);
351
352 msgb_tlv_put(msg, GPRS_GSUP_SRES_IE,
353 sizeof(auth_tuple->sres), auth_tuple->sres);
354
355 msgb_tlv_put(msg, GPRS_GSUP_KC_IE,
356 sizeof(auth_tuple->kc), auth_tuple->kc);
357
358 /* Update length field */
359 *len_field = msgb_length(msg) - old_len;
360}
361
362void gprs_gsup_encode(struct msgb *msg, const struct gprs_gsup_message *gsup_msg)
363{
364 uint8_t u8;
365 int idx;
366 uint8_t bcd_buf[GSM48_MI_SIZE] = {0};
367 size_t bcd_len;
368
369 /* generic part */
370 OSMO_ASSERT(gsup_msg->message_type);
371 msgb_v_put(msg, gsup_msg->message_type);
372
373 bcd_len = gsm48_encode_bcd_number(bcd_buf, sizeof(bcd_buf), 0,
374 gsup_msg->imsi);
375
376 OSMO_ASSERT(bcd_len > 1);
377
378 /* Note that gsm48_encode_bcd_number puts the length into the first
379 * octet. Since msgb_tlv_put will add this length byte, we'll have to
380 * skip it */
381 msgb_tlv_put(msg, GPRS_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
382
383 /* specific parts */
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400384 if (gsup_msg->msisdn_enc)
385 msgb_tlv_put(msg, GPRS_GSUP_MSISDN_IE,
386 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
387
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100388 if ((u8 = gsup_msg->cause))
389 msgb_tlv_put(msg, GPRS_GSUP_CAUSE_IE, sizeof(u8), &u8);
390
391 if ((u8 = gsup_msg->cancel_type)) {
392 u8 -= 1;
393 msgb_tlv_put(msg, GPRS_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
394 }
395
396 if (gsup_msg->pdp_info_compl)
397 msgb_tlv_put(msg, GPRS_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
398
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100399 if (gsup_msg->freeze_ptmsi)
400 msgb_tlv_put(msg, GPRS_GSUP_FREEZE_PTMSI_IE, 0, &u8);
401
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100402 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
403 const struct gprs_gsup_pdp_info *pdp_info;
404
405 pdp_info = &gsup_msg->pdp_infos[idx];
406
407 if (pdp_info->context_id == 0)
408 continue;
409
410 if (pdp_info->have_info) {
411 encode_pdp_info(msg, GPRS_GSUP_PDP_INFO_IE, pdp_info);
412 } else {
413 u8 = pdp_info->context_id;
414 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE,
415 sizeof(u8), &u8);
416 }
417 }
418
419 for (idx = 0; idx < gsup_msg->num_auth_tuples; idx++) {
420 const struct gsm_auth_tuple *auth_info;
421
422 auth_info = &gsup_msg->auth_tuples[idx];
423
424 if (auth_info->key_seq == GSM_KEY_SEQ_INVAL)
425 continue;
426
427 encode_auth_info(msg, GPRS_GSUP_AUTH_TUPLE_IE, auth_info);
428 }
429}