blob: 07485f7f9dbcf5332608c3223b18817c02ae17d4 [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>
Neels Hofmeyrd48f0572015-10-12 11:57:33 +020029#include <openbsc/utils.h>
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010030
31#include <osmocom/gsm/tlv.h>
32#include <osmocom/core/msgb.h>
33
34#include <stdint.h>
35
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010036static int decode_pdp_info(uint8_t *data, size_t data_len,
37 struct gprs_gsup_pdp_info *pdp_info)
38{
39 int rc;
40 uint8_t tag;
41 uint8_t *value;
42 size_t value_len;
43
44 /* specific parts */
45 while (data_len > 0) {
46 enum gprs_gsup_iei iei;
47
48 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
49 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +010050 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010051
52 iei = tag;
53
54 switch (iei) {
55 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
56 pdp_info->context_id = decode_big_endian(value, value_len);
57 break;
58
59 case GPRS_GSUP_PDP_TYPE_IE:
60 pdp_info->pdp_type =
61 decode_big_endian(value, value_len) & 0x0fff;
62 break;
63
64 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
65 pdp_info->apn_enc = value;
66 pdp_info->apn_enc_len = value_len;
67 break;
68
Holger Hans Peter Freyther49c1a712015-04-23 09:13:01 -040069 case GPRS_GSUP_PDP_QOS_IE:
70 pdp_info->qos_enc = value;
71 pdp_info->qos_enc_len = value_len;
72 break;
73
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010074 default:
75 LOGP(DGPRS, LOGL_ERROR,
76 "GSUP IE type %d not expected in PDP info\n", iei);
77 continue;
78 }
79 }
80
81 return 0;
82}
83
84static int decode_auth_info(uint8_t *data, size_t data_len,
85 struct gsm_auth_tuple *auth_tuple)
86{
87 int rc;
88 uint8_t tag;
89 uint8_t *value;
90 size_t value_len;
91 enum gprs_gsup_iei iei;
92
93 /* specific parts */
94 while (data_len > 0) {
95 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
96 if (rc < 0)
Jacob Erlbeckbce20612015-01-05 18:57:32 +010097 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +010098
99 iei = tag;
100
101 switch (iei) {
102 case GPRS_GSUP_RAND_IE:
103 if (value_len != sizeof(auth_tuple->rand))
104 goto parse_error;
105
106 memcpy(auth_tuple->rand, value, value_len);
107 break;
108
109 case GPRS_GSUP_SRES_IE:
110 if (value_len != sizeof(auth_tuple->sres))
111 goto parse_error;
112
113 memcpy(auth_tuple->sres, value, value_len);
114 break;
115
116 case GPRS_GSUP_KC_IE:
117 if (value_len != sizeof(auth_tuple->kc))
118 goto parse_error;
119
120 memcpy(auth_tuple->kc, value, value_len);
121 break;
122
123 default:
124 LOGP(DGPRS, LOGL_ERROR,
125 "GSUP IE type %d not expected in PDP info\n", iei);
126 continue;
127 }
128 }
129
130 return 0;
131
132parse_error:
133 LOGP(DGPRS, LOGL_ERROR,
Holger Hans Peter Freyther8e6ecc92015-04-23 11:55:23 -0400134 "GSUP IE type %d, length %zu invalid in PDP info\n", iei, value_len);
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100135
136 return -1;
137}
138
139int gprs_gsup_decode(const uint8_t *const_data, size_t data_len,
140 struct gprs_gsup_message *gsup_msg)
141{
142 int rc;
143 uint8_t tag;
144 /* the shift/match functions expect non-const pointers, but we'll
145 * either copy the data or cast pointers back to const before returning
146 * them
147 */
148 uint8_t *data = (uint8_t *)const_data;
149 uint8_t *value;
150 size_t value_len;
151 static const struct gprs_gsup_pdp_info empty_pdp_info = {0};
152 static const struct gsm_auth_tuple empty_auth_info = {0};
Jacob Erlbeck16106262015-01-12 13:54:39 +0100153 static const struct gprs_gsup_message empty_gsup_message = {0};
154
155 *gsup_msg = empty_gsup_message;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100156
157 /* generic part */
Jacob Erlbeck424ffa42015-01-12 13:23:05 +0100158 rc = gprs_shift_v_fixed(&data, &data_len, 1, &value);
159 if (rc < 0)
160 return -GMM_CAUSE_INV_MAND_INFO;
161
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100162 gsup_msg->message_type = decode_big_endian(value, 1);
163
164 rc = gprs_match_tlv(&data, &data_len, GPRS_GSUP_IMSI_IE,
165 &value, &value_len);
166
167 if (rc <= 0)
168 return -GMM_CAUSE_INV_MAND_INFO;
169
170 if (value_len * 2 + 1 > sizeof(gsup_msg->imsi))
171 return -GMM_CAUSE_INV_MAND_INFO;
172
173 /* Note that gsm48_decode_bcd_number expects the number of encoded IMSI
174 * octets in the first octet. By coincidence (the TLV encoding) the byte
175 * before the value part already contains this length so we can use it
176 * here.
177 */
178 OSMO_ASSERT(value[-1] == value_len);
179 gsm48_decode_bcd_number(gsup_msg->imsi, sizeof(gsup_msg->imsi),
180 value - 1, 0);
181
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100182 /* specific parts */
183 while (data_len > 0) {
184 enum gprs_gsup_iei iei;
185 struct gprs_gsup_pdp_info pdp_info;
186 struct gsm_auth_tuple auth_info;
187
188 rc = gprs_shift_tlv(&data, &data_len, &tag, &value, &value_len);
189 if (rc < 0)
190 return -GMM_CAUSE_PROTO_ERR_UNSPEC;
191
192 iei = tag;
193
194 switch (iei) {
195 case GPRS_GSUP_IMSI_IE:
196 case GPRS_GSUP_PDP_TYPE_IE:
197 case GPRS_GSUP_ACCESS_POINT_NAME_IE:
198 case GPRS_GSUP_RAND_IE:
199 case GPRS_GSUP_SRES_IE:
200 case GPRS_GSUP_KC_IE:
201 LOGP(DGPRS, LOGL_NOTICE,
202 "GSUP IE type %d not expected (ignored)\n", iei);
203 continue;
204
205 case GPRS_GSUP_CAUSE_IE:
206 gsup_msg->cause = decode_big_endian(value, value_len);
207 break;
208
209 case GPRS_GSUP_CANCEL_TYPE_IE:
210 gsup_msg->cancel_type =
211 decode_big_endian(value, value_len) + 1;
212 break;
213
214 case GPRS_GSUP_PDP_INFO_COMPL_IE:
215 gsup_msg->pdp_info_compl = 1;
216 break;
217
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100218 case GPRS_GSUP_FREEZE_PTMSI_IE:
219 gsup_msg->freeze_ptmsi = 1;
220 break;
221
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100222 case GPRS_GSUP_PDP_CONTEXT_ID_IE:
223 /* When these IE appear in the top-level part of the
224 * message, they are used by Delete Subscr Info to delete
225 * single entries. We don't have an extra list for
226 * these but use the PDP info list instead */
227
228 /* fall through */
229
230 case GPRS_GSUP_PDP_INFO_IE:
231 if (gsup_msg->num_pdp_infos >= GPRS_GSUP_MAX_NUM_PDP_INFO) {
232 LOGP(DGPRS, LOGL_ERROR,
233 "GSUP IE type %d (PDP_INFO) max exceeded\n",
234 iei);
235 return -GMM_CAUSE_COND_IE_ERR;
236 }
237
Jacob Erlbeck16106262015-01-12 13:54:39 +0100238 pdp_info = empty_pdp_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100239
240 if (iei == GPRS_GSUP_PDP_INFO_IE) {
241 rc = decode_pdp_info(value, value_len, &pdp_info);
242 if (rc < 0)
243 return rc;
244 pdp_info.have_info = 1;
245 } else {
246 pdp_info.context_id =
247 decode_big_endian(value, value_len);
248 }
249
250 gsup_msg->pdp_infos[gsup_msg->num_pdp_infos++] =
251 pdp_info;
252 break;
253
254 case GPRS_GSUP_AUTH_TUPLE_IE:
255 if (gsup_msg->num_auth_tuples >= GPRS_GSUP_MAX_NUM_AUTH_INFO) {
256 LOGP(DGPRS, LOGL_ERROR,
257 "GSUP IE type %d (AUTH_INFO) max exceeded\n",
258 iei);
259 return -GMM_CAUSE_INV_MAND_INFO;
260 }
261
Jacob Erlbeck16106262015-01-12 13:54:39 +0100262 auth_info = empty_auth_info;
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100263 auth_info.key_seq = gsup_msg->num_auth_tuples;
264
265 rc = decode_auth_info(value, value_len, &auth_info);
266 if (rc < 0)
267 return rc;
268
269 gsup_msg->auth_tuples[gsup_msg->num_auth_tuples++] =
270 auth_info;
271 break;
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400272
273 case GPRS_GSUP_MSISDN_IE:
274 gsup_msg->msisdn_enc = value;
275 gsup_msg->msisdn_enc_len = value_len;
276 break;
277
Holger Hans Peter Freyther0bb56742015-05-17 19:56:38 +0200278 case GPRS_GSUP_HLR_NUMBER_IE:
279 gsup_msg->hlr_enc = value;
280 gsup_msg->hlr_enc_len = value_len;
281 break;
282
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100283 default:
284 LOGP(DGPRS, LOGL_NOTICE,
285 "GSUP IE type %d unknown\n", iei);
286 continue;
287 }
288 }
289
290 return 0;
291}
292
293static void encode_pdp_info(struct msgb *msg, enum gprs_gsup_iei iei,
294 const struct gprs_gsup_pdp_info *pdp_info)
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;
304 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE, sizeof(u8), &u8);
305
306 if (pdp_info->pdp_type) {
307 msgb_tlv_put(msg, GPRS_GSUP_PDP_TYPE_IE,
308 GPRS_GSUP_PDP_TYPE_SIZE,
309 encode_big_endian(pdp_info->pdp_type | 0xf000,
310 GPRS_GSUP_PDP_TYPE_SIZE));
311 }
312
313 if (pdp_info->apn_enc) {
314 msgb_tlv_put(msg, GPRS_GSUP_ACCESS_POINT_NAME_IE,
315 pdp_info->apn_enc_len, pdp_info->apn_enc);
316 }
317
Holger Hans Peter Freyther49c1a712015-04-23 09:13:01 -0400318 if (pdp_info->qos_enc) {
319 msgb_tlv_put(msg, GPRS_GSUP_PDP_QOS_IE,
320 pdp_info->qos_enc_len, pdp_info->qos_enc);
321 }
322
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100323 /* Update length field */
324 *len_field = msgb_length(msg) - old_len;
325}
326
327static void encode_auth_info(struct msgb *msg, enum gprs_gsup_iei iei,
328 const struct gsm_auth_tuple *auth_tuple)
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
336 msgb_tlv_put(msg, GPRS_GSUP_RAND_IE,
337 sizeof(auth_tuple->rand), auth_tuple->rand);
338
339 msgb_tlv_put(msg, GPRS_GSUP_SRES_IE,
340 sizeof(auth_tuple->sres), auth_tuple->sres);
341
342 msgb_tlv_put(msg, GPRS_GSUP_KC_IE,
343 sizeof(auth_tuple->kc), auth_tuple->kc);
344
345 /* Update length field */
346 *len_field = msgb_length(msg) - old_len;
347}
348
349void gprs_gsup_encode(struct msgb *msg, const struct gprs_gsup_message *gsup_msg)
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 */
368 msgb_tlv_put(msg, GPRS_GSUP_IMSI_IE, bcd_len - 1, &bcd_buf[1]);
369
370 /* specific parts */
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400371 if (gsup_msg->msisdn_enc)
372 msgb_tlv_put(msg, GPRS_GSUP_MSISDN_IE,
373 gsup_msg->msisdn_enc_len, gsup_msg->msisdn_enc);
Holger Hans Peter Freyther0bb56742015-05-17 19:56:38 +0200374 if (gsup_msg->hlr_enc)
375 msgb_tlv_put(msg, GPRS_GSUP_HLR_NUMBER_IE,
376 gsup_msg->hlr_enc_len, gsup_msg->hlr_enc);
Holger Hans Peter Freytherb927f1c2015-04-22 23:09:41 -0400377
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100378 if ((u8 = gsup_msg->cause))
379 msgb_tlv_put(msg, GPRS_GSUP_CAUSE_IE, sizeof(u8), &u8);
380
381 if ((u8 = gsup_msg->cancel_type)) {
382 u8 -= 1;
383 msgb_tlv_put(msg, GPRS_GSUP_CANCEL_TYPE_IE, sizeof(u8), &u8);
384 }
385
386 if (gsup_msg->pdp_info_compl)
387 msgb_tlv_put(msg, GPRS_GSUP_PDP_INFO_COMPL_IE, 0, &u8);
388
Jacob Erlbeck69d27132015-01-15 11:50:08 +0100389 if (gsup_msg->freeze_ptmsi)
390 msgb_tlv_put(msg, GPRS_GSUP_FREEZE_PTMSI_IE, 0, &u8);
391
Jacob Erlbeckf3a271f2014-12-11 16:54:14 +0100392 for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) {
393 const struct gprs_gsup_pdp_info *pdp_info;
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) {
401 encode_pdp_info(msg, GPRS_GSUP_PDP_INFO_IE, pdp_info);
402 } else {
403 u8 = pdp_info->context_id;
404 msgb_tlv_put(msg, GPRS_GSUP_PDP_CONTEXT_ID_IE,
405 sizeof(u8), &u8);
406 }
407 }
408
409 for (idx = 0; idx < gsup_msg->num_auth_tuples; idx++) {
410 const struct gsm_auth_tuple *auth_info;
411
412 auth_info = &gsup_msg->auth_tuples[idx];
413
414 if (auth_info->key_seq == GSM_KEY_SEQ_INVAL)
415 continue;
416
417 encode_auth_info(msg, GPRS_GSUP_AUTH_TUPLE_IE, auth_info);
418 }
419}