blob: 89d43c845d4f739f5792e20cbc2a05dce1400401 [file] [log] [blame]
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +08001/* Format functions for GSM 04.80 */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009 by Mike Haben <michael.haben@btinternet.com>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010025#include <osmocom/gsm/gsm0480.h>
26#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +080027
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/logging.h>
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +080029
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/gsm/protocol/gsm_04_08.h>
31#include <osmocom/gsm/protocol/gsm_04_80.h>
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +080032
33#include <string.h>
34
35static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, uint8_t tag)
36{
37 uint8_t *data = msgb_push(msgb, 2);
38
39 data[0] = tag;
40 data[1] = msgb->len - 2;
41 return data;
42}
43
44static inline unsigned char *msgb_push_TLV1(struct msgb *msgb, uint8_t tag,
45 uint8_t value)
46{
47 uint8_t *data = msgb_push(msgb, 3);
48
49 data[0] = tag;
50 data[1] = 1;
51 data[2] = value;
52 return data;
53}
54
55/* wrap an invoke around it... the other way around
56 *
57 * 1.) Invoke Component tag
58 * 2.) Invoke ID Tag
59 * 3.) Operation
60 * 4.) Data
61 */
62int gsm0480_wrap_invoke(struct msgb *msg, int op, int link_id)
63{
64 /* 3. operation */
65 msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, op);
66
67 /* 2. invoke id tag */
68 msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, link_id);
69
70 /* 1. component tag */
71 msgb_wrap_with_TL(msg, GSM0480_CTYPE_INVOKE);
72
73 return 0;
74}
75
76/* wrap the GSM 04.08 Facility IE around it */
77int gsm0480_wrap_facility(struct msgb *msg)
78{
79 msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
80
81 return 0;
82}
83
84struct msgb *gsm0480_create_unstructuredSS_Notify(int alertPattern, const char *text)
85{
86 struct msgb *msg;
87 uint8_t *seq_len_ptr, *ussd_len_ptr, *data;
Andreas Eversberg2b0cac42013-07-04 09:51:33 +020088 int len, octet_len;
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +080089
90 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
91 if (!msg)
92 return NULL;
93
94 /* SEQUENCE { */
95 msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
96 seq_len_ptr = msgb_put(msg, 1);
97
98 /* DCS { */
99 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
100 msgb_put_u8(msg, 1);
101 msgb_put_u8(msg, 0x0F);
102 /* } DCS */
103
104 /* USSD-String { */
105 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
106 ussd_len_ptr = msgb_put(msg, 1);
107 data = msgb_put(msg, 0);
108 len = gsm_7bit_encode(data, text);
Andreas Eversberg2b0cac42013-07-04 09:51:33 +0200109 octet_len = len*7/8;
110 if (len*7%8 != 0)
111 octet_len++;
112 /* Warning, len indicates the amount of septets
113 * (characters), we need amount of octets occupied */
114 msgb_put(msg, octet_len);
115 ussd_len_ptr[0] = octet_len;
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +0800116 /* USSD-String } */
117
118 /* alertingPattern { */
119 msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
120 msgb_put_u8(msg, 1);
121 msgb_put_u8(msg, alertPattern);
122 /* } alertingPattern */
123
124 seq_len_ptr[0] = 3 + 2 + ussd_len_ptr[0] + 3;
125 /* } SEQUENCE */
126
127 return msg;
128}
129
130struct msgb *gsm0480_create_notifySS(const char *text)
131{
132 struct msgb *msg;
133 uint8_t *data, *tmp_len;
134 uint8_t *seq_len_ptr, *cal_len_ptr, *opt_len_ptr, *nam_len_ptr;
Andreas Eversberg2b0cac42013-07-04 09:51:33 +0200135 int len, octet_len;
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +0800136
137 len = strlen(text);
138 if (len < 1 || len > 160)
139 return NULL;
140
141 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
142 if (!msg)
143 return NULL;
144
145 msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
146 seq_len_ptr = msgb_put(msg, 1);
147
148 /* ss_code for CNAP { */
149 msgb_put_u8(msg, 0x81);
150 msgb_put_u8(msg, 1);
151 msgb_put_u8(msg, 0x19);
152 /* } ss_code */
153
154
155 /* nameIndicator { */
156 msgb_put_u8(msg, 0xB4);
157 nam_len_ptr = msgb_put(msg, 1);
158
159 /* callingName { */
160 msgb_put_u8(msg, 0xA0);
161 opt_len_ptr = msgb_put(msg, 1);
162 msgb_put_u8(msg, 0xA0);
163 cal_len_ptr = msgb_put(msg, 1);
164
165 /* namePresentationAllowed { */
166 /* add the DCS value */
167 msgb_put_u8(msg, 0x80);
168 msgb_put_u8(msg, 1);
169 msgb_put_u8(msg, 0x0F);
170
171 /* add the lengthInCharacters */
172 msgb_put_u8(msg, 0x81);
173 msgb_put_u8(msg, 1);
174 msgb_put_u8(msg, strlen(text));
175
176 /* add the actual string */
177 msgb_put_u8(msg, 0x82);
178 tmp_len = msgb_put(msg, 1);
179 data = msgb_put(msg, 0);
180 len = gsm_7bit_encode(data, text);
Andreas Eversberg2b0cac42013-07-04 09:51:33 +0200181 octet_len = len*7/8;
182 if (len*7%8 != 0)
183 octet_len++;
184 /* Warning, len indicates the amount of septets
185 * (characters), we need amount of octets occupied */
186 tmp_len[0] = octet_len;
187 msgb_put(msg, octet_len);
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +0800188
189 /* }; namePresentationAllowed */
190
Andreas Eversberg2b0cac42013-07-04 09:51:33 +0200191 cal_len_ptr[0] = 3 + 3 + 2 + octet_len;
Holger Hans Peter Freyther55aea502010-09-30 18:30:41 +0800192 opt_len_ptr[0] = cal_len_ptr[0] + 2;
193 /* }; callingName */
194
195 nam_len_ptr[0] = opt_len_ptr[0] + 2;
196 /* ); nameIndicator */
197
198 /* write the lengths... */
199 seq_len_ptr[0] = 3 + nam_len_ptr[0] + 2;
200
201 return msg;
202}
203
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800204/* Forward declarations */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200205static int parse_ussd(const struct gsm48_hdr *hdr,
206 uint16_t len, struct ussd_request *req);
207static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800208 struct ussd_request *req);
Holger Hans Peter Freyther49ad5002010-10-11 09:06:47 +0200209static int parse_facility_ie(const uint8_t *facility_ie, uint16_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800210 struct ussd_request *req);
Holger Hans Peter Freyther49ad5002010-10-11 09:06:47 +0200211static int parse_ss_invoke(const uint8_t *invoke_data, uint16_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800212 struct ussd_request *req);
Holger Hans Peter Freyther49ad5002010-10-11 09:06:47 +0200213static int parse_process_uss_req(const uint8_t *uss_req_data, uint16_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800214 struct ussd_request *req);
215
216/* Decode a mobile-originated USSD-request message */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200217int gsm0480_decode_ussd_request(const struct gsm48_hdr *hdr, uint16_t len,
218 struct ussd_request *req)
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800219{
220 int rc = 0;
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800221
Holger Hans Peter Freyther8ac04862010-10-11 08:08:58 +0200222 if (len < sizeof(*hdr) + 2) {
223 LOGP(0, LOGL_DEBUG, "USSD Request is too short.\n");
224 return 0;
225 }
226
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200227 if ((hdr->proto_discr & 0x0f) == GSM48_PDISC_NC_SS) {
228 req->transaction_id = hdr->proto_discr & 0x70;
229 rc = parse_ussd(hdr, len, req);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800230 }
231
232 if (!rc)
233 LOGP(0, LOGL_DEBUG, "Error occurred while parsing received USSD!\n");
234
235 return rc;
236}
237
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200238static int parse_ussd(const struct gsm48_hdr *hdr, uint16_t len, struct ussd_request *req)
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800239{
240 int rc = 1;
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200241 uint8_t msg_type = hdr->msg_type & 0xBF; /* message-type - section 3.4 */
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800242
243 switch (msg_type) {
244 case GSM0480_MTYPE_RELEASE_COMPLETE:
245 LOGP(0, LOGL_DEBUG, "USS Release Complete\n");
246 /* could also parse out the optional Cause/Facility data */
247 req->text[0] = 0xFF;
248 break;
249 case GSM0480_MTYPE_REGISTER:
250 case GSM0480_MTYPE_FACILITY:
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200251 rc &= parse_ussd_info_elements(&hdr->data[0], len - sizeof(*hdr), req);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800252 break;
253 default:
254 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 message-type field 0x%02x\n",
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200255 hdr->msg_type);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800256 rc = 0;
257 break;
258 }
259
260 return rc;
261}
262
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200263static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
264 struct ussd_request *req)
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800265{
266 int rc = -1;
267 /* Information Element Identifier - table 3.2 & GSM 04.08 section 10.5 */
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +0200268 uint8_t iei;
269 uint8_t iei_length;
270
271 iei = ussd_ie[0];
272 iei_length = ussd_ie[1];
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800273
Holger Hans Peter Freyther8ac04862010-10-11 08:08:58 +0200274 /* If the data does not fit, report an error */
275 if (len - 2 < iei_length)
276 return 0;
277
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800278 switch (iei) {
279 case GSM48_IE_CAUSE:
280 break;
281 case GSM0480_IE_FACILITY:
282 rc = parse_facility_ie(ussd_ie+2, iei_length, req);
283 break;
284 case GSM0480_IE_SS_VERSION:
285 break;
286 default:
287 LOGP(0, LOGL_DEBUG, "Unhandled GSM 04.08 or 04.80 IEI 0x%02x\n",
288 iei);
289 rc = 0;
290 break;
291 }
292
293 return rc;
294}
295
Holger Hans Peter Freyther49ad5002010-10-11 09:06:47 +0200296static int parse_facility_ie(const uint8_t *facility_ie, uint16_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800297 struct ussd_request *req)
298{
299 int rc = 1;
300 uint8_t offset = 0;
301
Holger Hans Peter Freyther4156ec62010-10-11 09:07:50 +0200302 while (offset + 2 <= length) {
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800303 /* Component Type tag - table 3.7 */
304 uint8_t component_type = facility_ie[offset];
305 uint8_t component_length = facility_ie[offset+1];
306
Holger Hans Peter Freyther4156ec62010-10-11 09:07:50 +0200307 /* size check */
308 if (offset + 2 + component_length > length) {
309 LOGP(0, LOGL_ERROR, "Component does not fit.\n");
310 return 0;
311 }
312
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800313 switch (component_type) {
314 case GSM0480_CTYPE_INVOKE:
315 rc &= parse_ss_invoke(facility_ie+2,
316 component_length,
317 req);
318 break;
319 case GSM0480_CTYPE_RETURN_RESULT:
320 break;
321 case GSM0480_CTYPE_RETURN_ERROR:
322 break;
323 case GSM0480_CTYPE_REJECT:
324 break;
325 default:
326 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 Facility "
327 "Component Type 0x%02x\n", component_type);
328 rc = 0;
329 break;
330 }
331 offset += (component_length+2);
Holger Hans Peter Freyther4156ec62010-10-11 09:07:50 +0200332 };
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800333
334 return rc;
335}
336
337/* Parse an Invoke component - see table 3.3 */
Holger Hans Peter Freyther49ad5002010-10-11 09:06:47 +0200338static int parse_ss_invoke(const uint8_t *invoke_data, uint16_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800339 struct ussd_request *req)
340{
341 int rc = 1;
342 uint8_t offset;
343
Holger Hans Peter Freyther7d0bce32010-10-11 09:12:33 +0200344 if (length < 3)
345 return 0;
346
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800347 /* mandatory part */
348 if (invoke_data[0] != GSM0480_COMPIDTAG_INVOKE_ID) {
349 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag "
350 "0x%02x (expecting Invoke ID tag)\n", invoke_data[0]);
351 }
352
353 offset = invoke_data[1] + 2;
354 req->invoke_id = invoke_data[2];
355
Holger Hans Peter Freyther7d0bce32010-10-11 09:12:33 +0200356 /* look ahead once */
357 if (offset + 1 > length)
358 return 0;
359
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800360 /* optional part */
361 if (invoke_data[offset] == GSM0480_COMPIDTAG_LINKED_ID)
362 offset += invoke_data[offset+1] + 2; /* skip over it */
363
364 /* mandatory part */
365 if (invoke_data[offset] == GSM0480_OPERATION_CODE) {
Holger Hans Peter Freyther7d0bce32010-10-11 09:12:33 +0200366 if (offset + 2 > length)
367 return 0;
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800368 uint8_t operation_code = invoke_data[offset+2];
369 switch (operation_code) {
370 case GSM0480_OP_CODE_PROCESS_USS_REQ:
371 rc = parse_process_uss_req(invoke_data + offset + 3,
372 length - offset - 3,
373 req);
374 break;
375 default:
376 LOGP(0, LOGL_DEBUG, "GSM 04.80 operation code 0x%02x "
377 "is not yet handled\n", operation_code);
378 rc = 0;
379 break;
380 }
381 } else {
382 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag 0x%02x "
383 "(expecting Operation Code tag)\n",
384 invoke_data[0]);
385 rc = 0;
386 }
387
388 return rc;
389}
390
391/* Parse the parameters of a Process UnstructuredSS Request */
Holger Hans Peter Freyther49ad5002010-10-11 09:06:47 +0200392static int parse_process_uss_req(const uint8_t *uss_req_data, uint16_t length,
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800393 struct ussd_request *req)
394{
395 int rc = 0;
396 int num_chars;
397 uint8_t dcs;
398
Holger Hans Peter Freytherd65a6982010-10-11 09:23:50 +0200399
400 /* we need at least that much */
401 if (length < 8)
402 return 0;
403
404
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800405 if (uss_req_data[0] == GSM_0480_SEQUENCE_TAG) {
406 if (uss_req_data[2] == ASN1_OCTET_STRING_TAG) {
407 dcs = uss_req_data[4];
408 if ((dcs == 0x0F) &&
409 (uss_req_data[5] == ASN1_OCTET_STRING_TAG)) {
410 num_chars = (uss_req_data[6] * 8) / 7;
411 /* Prevent a mobile-originated buffer-overrun! */
412 if (num_chars > MAX_LEN_USSD_STRING)
413 num_chars = MAX_LEN_USSD_STRING;
414 gsm_7bit_decode(req->text,
415 &(uss_req_data[7]), num_chars);
Holger Hans Peter Freyther00cb5702010-10-09 01:47:15 +0800416 rc = 1;
417 }
418 }
419 }
420 return rc;
421}
Holger Hans Peter Freytherc64970e2010-10-18 16:56:43 +0200422
423struct msgb *gsm0480_create_ussd_resp(uint8_t invoke_id, uint8_t trans_id, const char *text)
424{
425 struct msgb *msg;
426 struct gsm48_hdr *gh;
427 uint8_t *ptr8;
Andreas Eversberg2b0cac42013-07-04 09:51:33 +0200428 int response_len, octet_len;
Holger Hans Peter Freytherc64970e2010-10-18 16:56:43 +0200429
430 msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
431 if (!msg)
432 return NULL;
433
434 /* First put the payload text into the message */
435 ptr8 = msgb_put(msg, 0);
436 response_len = gsm_7bit_encode(ptr8, text);
Andreas Eversberg2b0cac42013-07-04 09:51:33 +0200437 octet_len = response_len*7/8;
438 if (response_len*7%8 != 0)
439 octet_len++;
440 /* Warning, response_len indicates the amount of septets
441 * (characters), we need amount of octets occupied */
442 msgb_put(msg, octet_len);
Holger Hans Peter Freytherc64970e2010-10-18 16:56:43 +0200443
444 /* Then wrap it as an Octet String */
445 msgb_wrap_with_TL(msg, ASN1_OCTET_STRING_TAG);
446
447 /* Pre-pend the DCS octet string */
448 msgb_push_TLV1(msg, ASN1_OCTET_STRING_TAG, 0x0F);
449
450 /* Then wrap these as a Sequence */
451 msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG);
452
453 /* Pre-pend the operation code */
454 msgb_push_TLV1(msg, GSM0480_OPERATION_CODE,
455 GSM0480_OP_CODE_PROCESS_USS_REQ);
456
457 /* Wrap the operation code and IA5 string as a sequence */
458 msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG);
459
460 /* Pre-pend the invoke ID */
461 msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, invoke_id);
462
463 /* Wrap this up as a Return Result component */
464 msgb_wrap_with_TL(msg, GSM0480_CTYPE_RETURN_RESULT);
465
466 /* Wrap the component in a Facility message */
467 msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
468
469 /* And finally pre-pend the L3 header */
470 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
471 gh->proto_discr = GSM48_PDISC_NC_SS | trans_id
472 | (1<<7); /* TI direction = 1 */
473 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
474
475 return msg;
476}