Merge commit 'dc0ebdfbdf3b1a381754c6ef4a59b0354eba7705'
[osmocom-bb.git] / src / shared / libosmocore / src / gsm0480.c
1 /* 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
25 #include <osmocore/gsm0480.h>
26 #include <osmocore/gsm_utils.h>
27
28 #include <osmocore/logging.h>
29
30 #include <osmocore/protocol/gsm_04_08.h>
31 #include <osmocore/protocol/gsm_04_80.h>
32
33 #include <string.h>
34
35 static 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
44 static 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  */
62 int 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 */
77 int gsm0480_wrap_facility(struct msgb *msg)
78 {
79         msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
80
81         return 0;
82 }
83
84 struct 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;
88         int len;
89
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);
109         msgb_put(msg, len);
110         ussd_len_ptr[0] = len;
111         /* USSD-String } */
112
113         /* alertingPattern { */
114         msgb_put_u8(msg, ASN1_OCTET_STRING_TAG);
115         msgb_put_u8(msg, 1);
116         msgb_put_u8(msg, alertPattern);
117         /* } alertingPattern */
118
119         seq_len_ptr[0] = 3 + 2 + ussd_len_ptr[0] + 3;
120         /* } SEQUENCE */
121
122         return msg;
123 }
124
125 struct msgb *gsm0480_create_notifySS(const char *text)
126 {
127         struct msgb *msg;
128         uint8_t *data, *tmp_len;
129         uint8_t *seq_len_ptr, *cal_len_ptr, *opt_len_ptr, *nam_len_ptr;
130         int len;
131
132         len = strlen(text);
133         if (len < 1 || len > 160)
134                 return NULL;
135
136         msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
137         if (!msg)
138                 return NULL;
139
140         msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG);
141         seq_len_ptr = msgb_put(msg, 1);
142
143         /* ss_code for CNAP { */
144         msgb_put_u8(msg, 0x81);
145         msgb_put_u8(msg, 1);
146         msgb_put_u8(msg, 0x19);
147         /* } ss_code */
148
149
150         /* nameIndicator { */
151         msgb_put_u8(msg, 0xB4);
152         nam_len_ptr = msgb_put(msg, 1);
153
154         /* callingName { */
155         msgb_put_u8(msg, 0xA0);
156         opt_len_ptr = msgb_put(msg, 1);
157         msgb_put_u8(msg, 0xA0);
158         cal_len_ptr = msgb_put(msg, 1);
159
160         /* namePresentationAllowed { */
161         /* add the DCS value */
162         msgb_put_u8(msg, 0x80);
163         msgb_put_u8(msg, 1);
164         msgb_put_u8(msg, 0x0F);
165
166         /* add the lengthInCharacters */
167         msgb_put_u8(msg, 0x81);
168         msgb_put_u8(msg, 1);
169         msgb_put_u8(msg, strlen(text));
170
171         /* add the actual string */
172         msgb_put_u8(msg, 0x82);
173         tmp_len = msgb_put(msg, 1);
174         data = msgb_put(msg, 0);
175         len = gsm_7bit_encode(data, text);
176         tmp_len[0] = len;
177         msgb_put(msg, len);
178
179         /* }; namePresentationAllowed */
180
181         cal_len_ptr[0] = 3 + 3 + 2 + len;
182         opt_len_ptr[0] = cal_len_ptr[0] + 2;
183         /* }; callingName */
184
185         nam_len_ptr[0] = opt_len_ptr[0] + 2;
186         /* ); nameIndicator */
187
188         /* write the lengths... */
189         seq_len_ptr[0] = 3 + nam_len_ptr[0] + 2;
190
191         return msg;
192 }
193
194 /* Forward declarations */
195 static int parse_ussd(const struct gsm48_hdr *hdr,
196                       uint16_t len, struct ussd_request *req);
197 static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
198                                         struct ussd_request *req);
199 static int parse_facility_ie(const uint8_t *facility_ie, uint16_t length,
200                                         struct ussd_request *req);
201 static int parse_ss_invoke(const uint8_t *invoke_data, uint16_t length,
202                                         struct ussd_request *req);
203 static int parse_process_uss_req(const uint8_t *uss_req_data, uint16_t length,
204                                         struct ussd_request *req);
205
206 /* Decode a mobile-originated USSD-request message */
207 int gsm0480_decode_ussd_request(const struct gsm48_hdr *hdr, uint16_t len,
208                                 struct ussd_request *req)
209 {
210         int rc = 0;
211
212         if (len < sizeof(*hdr) + 2) {
213                 LOGP(0, LOGL_DEBUG, "USSD Request is too short.\n");
214                 return 0;
215         }
216
217         if ((hdr->proto_discr & 0x0f) == GSM48_PDISC_NC_SS) {
218                 req->transaction_id = hdr->proto_discr & 0x70;
219                 rc = parse_ussd(hdr, len, req);
220         }
221
222         if (!rc)
223                 LOGP(0, LOGL_DEBUG, "Error occurred while parsing received USSD!\n");
224
225         return rc;
226 }
227
228 static int parse_ussd(const struct gsm48_hdr *hdr, uint16_t len, struct ussd_request *req)
229 {
230         int rc = 1;
231         uint8_t msg_type = hdr->msg_type & 0xBF;  /* message-type - section 3.4 */
232
233         switch (msg_type) {
234         case GSM0480_MTYPE_RELEASE_COMPLETE:
235                 LOGP(0, LOGL_DEBUG, "USS Release Complete\n");
236                 /* could also parse out the optional Cause/Facility data */
237                 req->text[0] = 0xFF;
238                 break;
239         case GSM0480_MTYPE_REGISTER:
240         case GSM0480_MTYPE_FACILITY:
241                 rc &= parse_ussd_info_elements(&hdr->data[0], len - sizeof(*hdr), req);
242                 break;
243         default:
244                 LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 message-type field 0x%02x\n",
245                         hdr->msg_type);
246                 rc = 0;
247                 break;
248         }
249
250         return rc;
251 }
252
253 static int parse_ussd_info_elements(const uint8_t *ussd_ie, uint16_t len,
254                                     struct ussd_request *req)
255 {
256         int rc = -1;
257         /* Information Element Identifier - table 3.2 & GSM 04.08 section 10.5 */
258         uint8_t iei;
259         uint8_t iei_length;
260
261         iei = ussd_ie[0];
262         iei_length = ussd_ie[1];
263
264         /* If the data does not fit, report an error */
265         if (len - 2 < iei_length)
266                 return 0;
267
268         switch (iei) {
269         case GSM48_IE_CAUSE:
270                 break;
271         case GSM0480_IE_FACILITY:
272                 rc = parse_facility_ie(ussd_ie+2, iei_length, req);
273                 break;
274         case GSM0480_IE_SS_VERSION:
275                 break;
276         default:
277                 LOGP(0, LOGL_DEBUG, "Unhandled GSM 04.08 or 04.80 IEI 0x%02x\n",
278                         iei);
279                 rc = 0;
280                 break;
281         }
282
283         return rc;
284 }
285
286 static int parse_facility_ie(const uint8_t *facility_ie, uint16_t length,
287                                                 struct ussd_request *req)
288 {
289         int rc = 1;
290         uint8_t offset = 0;
291
292         while (offset + 2 <= length) {
293                 /* Component Type tag - table 3.7 */
294                 uint8_t component_type = facility_ie[offset];
295                 uint8_t component_length = facility_ie[offset+1];
296
297                 /* size check */
298                 if (offset + 2 + component_length > length) {
299                         LOGP(0, LOGL_ERROR, "Component does not fit.\n");
300                         return 0;
301                 }
302
303                 switch (component_type) {
304                 case GSM0480_CTYPE_INVOKE:
305                         rc &= parse_ss_invoke(facility_ie+2,
306                                                 component_length,
307                                                 req);
308                         break;
309                 case GSM0480_CTYPE_RETURN_RESULT:
310                         break;
311                 case GSM0480_CTYPE_RETURN_ERROR:
312                         break;
313                 case GSM0480_CTYPE_REJECT:
314                         break;
315                 default:
316                         LOGP(0, LOGL_DEBUG, "Unknown GSM 04.80 Facility "
317                                 "Component Type 0x%02x\n", component_type);
318                         rc = 0;
319                         break;
320                 }
321                 offset += (component_length+2);
322         };
323
324         return rc;
325 }
326
327 /* Parse an Invoke component - see table 3.3 */
328 static int parse_ss_invoke(const uint8_t *invoke_data, uint16_t length,
329                                                 struct ussd_request *req)
330 {
331         int rc = 1;
332         uint8_t offset;
333
334         if (length < 3)
335                 return 0;
336
337         /* mandatory part */
338         if (invoke_data[0] != GSM0480_COMPIDTAG_INVOKE_ID) {
339                 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag "
340                         "0x%02x (expecting Invoke ID tag)\n", invoke_data[0]);
341         }
342
343         offset = invoke_data[1] + 2;
344         req->invoke_id = invoke_data[2];
345
346         /* look ahead once */
347         if (offset + 1 > length)
348                 return 0;
349
350         /* optional part */
351         if (invoke_data[offset] == GSM0480_COMPIDTAG_LINKED_ID)
352                 offset += invoke_data[offset+1] + 2;  /* skip over it */
353
354         /* mandatory part */
355         if (invoke_data[offset] == GSM0480_OPERATION_CODE) {
356                 if (offset + 2 > length)
357                         return 0;
358                 uint8_t operation_code = invoke_data[offset+2];
359                 switch (operation_code) {
360                 case GSM0480_OP_CODE_PROCESS_USS_REQ:
361                         rc = parse_process_uss_req(invoke_data + offset + 3,
362                                                    length - offset - 3,
363                                                    req);
364                         break;
365                 default:
366                         LOGP(0, LOGL_DEBUG, "GSM 04.80 operation code 0x%02x "
367                                 "is not yet handled\n", operation_code);
368                         rc = 0;
369                         break;
370                 }
371         } else {
372                 LOGP(0, LOGL_DEBUG, "Unexpected GSM 04.80 Component-ID tag 0x%02x "
373                         "(expecting Operation Code tag)\n",
374                         invoke_data[0]);
375                 rc = 0;
376         }
377
378         return rc;
379 }
380
381 /* Parse the parameters of a Process UnstructuredSS Request */
382 static int parse_process_uss_req(const uint8_t *uss_req_data, uint16_t length,
383                                         struct ussd_request *req)
384 {
385         int rc = 0;
386         int num_chars;
387         uint8_t dcs;
388
389
390         /* we need at least that much */
391         if (length < 8)
392                 return 0;
393
394
395         if (uss_req_data[0] == GSM_0480_SEQUENCE_TAG) {
396                 if (uss_req_data[2] == ASN1_OCTET_STRING_TAG) {
397                         dcs = uss_req_data[4];
398                         if ((dcs == 0x0F) &&
399                             (uss_req_data[5] == ASN1_OCTET_STRING_TAG)) {
400                                 num_chars = (uss_req_data[6] * 8) / 7;
401                                 /* Prevent a mobile-originated buffer-overrun! */
402                                 if (num_chars > MAX_LEN_USSD_STRING)
403                                         num_chars = MAX_LEN_USSD_STRING;
404                                 gsm_7bit_decode(req->text,
405                                                 &(uss_req_data[7]), num_chars);
406                                 rc = 1;
407                         }
408                 }
409         }
410         return rc;
411 }
412
413 struct msgb *gsm0480_create_ussd_resp(uint8_t invoke_id, uint8_t trans_id, const char *text)
414 {
415         struct msgb *msg;
416         struct gsm48_hdr *gh;
417         uint8_t *ptr8;
418         int response_len;
419
420         msg = msgb_alloc_headroom(1024, 128, "GSM 04.80");
421         if (!msg)
422                 return NULL;
423
424         /* First put the payload text into the message */
425         ptr8 = msgb_put(msg, 0);
426         response_len = gsm_7bit_encode(ptr8, text);
427         msgb_put(msg, response_len);
428
429         /* Then wrap it as an Octet String */
430         msgb_wrap_with_TL(msg, ASN1_OCTET_STRING_TAG);
431
432         /* Pre-pend the DCS octet string */
433         msgb_push_TLV1(msg, ASN1_OCTET_STRING_TAG, 0x0F);
434
435         /* Then wrap these as a Sequence */
436         msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG);
437
438         /* Pre-pend the operation code */
439         msgb_push_TLV1(msg, GSM0480_OPERATION_CODE,
440                         GSM0480_OP_CODE_PROCESS_USS_REQ);
441
442         /* Wrap the operation code and IA5 string as a sequence */
443         msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG);
444
445         /* Pre-pend the invoke ID */
446         msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, invoke_id);
447
448         /* Wrap this up as a Return Result component */
449         msgb_wrap_with_TL(msg, GSM0480_CTYPE_RETURN_RESULT);
450
451         /* Wrap the component in a Facility message */
452         msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
453
454         /* And finally pre-pend the L3 header */
455         gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
456         gh->proto_discr = GSM48_PDISC_NC_SS | trans_id
457                                         | (1<<7);  /* TI direction = 1 */
458         gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
459
460         return msg;
461 }