Harald Welte | 8c8f791 | 2009-10-26 20:42:55 +0100 | [diff] [blame] | 1 | /* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 2 | * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */ |
| 3 | |
| 4 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 680833e | 2010-07-25 18:08:53 +0800 | [diff] [blame] | 5 | * (C) 2008, 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 6 | * (C) 2009 by Mike Haben <michael.haben@btinternet.com> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 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 General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
| 33 | #include <openbsc/gsm_data.h> |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 34 | #include <openbsc/gsm_04_08.h> |
| 35 | #include <openbsc/gsm_04_80.h> |
Holger Hans Peter Freyther | 9c137a7 | 2010-06-15 13:57:40 +0800 | [diff] [blame] | 36 | #include <openbsc/bsc_api.h> |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 37 | |
Holger Hans Peter Freyther | c0714b8 | 2010-09-30 18:48:28 +0800 | [diff] [blame^] | 38 | #include <osmocore/gsm_utils.h> |
| 39 | #include <osmocore/msgb.h> |
| 40 | #include <osmocore/tlv.h> |
| 41 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 42 | /* Forward declarations */ |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 43 | static int parse_ussd(u_int8_t *ussd, struct ussd_request *req); |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 44 | static int parse_ussd_info_elements(u_int8_t *ussd_ie, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 45 | struct ussd_request *req); |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 46 | static int parse_facility_ie(u_int8_t *facility_ie, u_int8_t length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 47 | struct ussd_request *req); |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 48 | static int parse_ss_invoke(u_int8_t *invoke_data, u_int8_t length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 49 | struct ussd_request *req); |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 50 | static int parse_process_uss_req(u_int8_t *uss_req_data, u_int8_t length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 51 | struct ussd_request *req); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 52 | |
| 53 | static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, u_int8_t tag) |
| 54 | { |
Holger Hans Peter Freyther | ac30cc8 | 2010-07-26 19:08:59 +0800 | [diff] [blame] | 55 | uint8_t *data = msgb_push(msgb, 2); |
| 56 | |
| 57 | data[0] = tag; |
Holger Hans Peter Freyther | e6373b7 | 2010-07-27 01:25:59 +0800 | [diff] [blame] | 58 | data[1] = msgb->len - 2; |
Holger Hans Peter Freyther | ac30cc8 | 2010-07-26 19:08:59 +0800 | [diff] [blame] | 59 | return data; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 62 | static inline unsigned char *msgb_push_TLV1(struct msgb *msgb, u_int8_t tag, |
| 63 | u_int8_t value) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 64 | { |
Holger Hans Peter Freyther | ac30cc8 | 2010-07-26 19:08:59 +0800 | [diff] [blame] | 65 | uint8_t *data = msgb_push(msgb, 3); |
| 66 | |
| 67 | data[0] = tag; |
| 68 | data[1] = 1; |
| 69 | data[2] = value; |
| 70 | return data; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 74 | /* Decode a mobile-originated USSD-request message */ |
Mike Haben | 2449b37 | 2009-10-26 20:36:34 +0100 | [diff] [blame] | 75 | int gsm0480_decode_ussd_request(const struct msgb *msg, struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 76 | { |
| 77 | int rc = 0; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 78 | u_int8_t *parse_ptr = msgb_l3(msg); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 79 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 80 | if ((*parse_ptr & 0x0F) == GSM48_PDISC_NC_SS) { |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 81 | req->transaction_id = *parse_ptr & 0x70; |
| 82 | rc = parse_ussd(parse_ptr+1, req); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (!rc) |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 86 | DEBUGP(DMM, "Error occurred while parsing received USSD!\n"); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 87 | |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 88 | return rc; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 91 | static int parse_ussd(u_int8_t *ussd, struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 92 | { |
| 93 | int rc = 1; |
| 94 | u_int8_t msg_type = ussd[0] & 0xBF; /* message-type - section 3.4 */ |
| 95 | |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 96 | switch (msg_type) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 97 | case GSM0480_MTYPE_RELEASE_COMPLETE: |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 98 | DEBUGP(DMM, "USS Release Complete\n"); |
| 99 | /* could also parse out the optional Cause/Facility data */ |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 100 | req->text[0] = 0xFF; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 101 | break; |
| 102 | case GSM0480_MTYPE_REGISTER: |
| 103 | case GSM0480_MTYPE_FACILITY: |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 104 | rc &= parse_ussd_info_elements(ussd+1, req); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 105 | break; |
| 106 | default: |
| 107 | fprintf(stderr, "Unknown GSM 04.80 message-type field 0x%02x\n", |
| 108 | ussd[0]); |
| 109 | rc = 0; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | return rc; |
| 114 | } |
| 115 | |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 116 | static int parse_ussd_info_elements(u_int8_t *ussd_ie, struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 117 | { |
Holger Hans Peter Freyther | 4f14064 | 2010-07-23 19:35:54 +0800 | [diff] [blame] | 118 | int rc = -1; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 119 | /* Information Element Identifier - table 3.2 & GSM 04.08 section 10.5 */ |
| 120 | u_int8_t iei = ussd_ie[0]; |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 121 | u_int8_t iei_length = ussd_ie[1]; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 122 | |
| 123 | switch (iei) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 124 | case GSM48_IE_CAUSE: |
| 125 | break; |
| 126 | case GSM0480_IE_FACILITY: |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 127 | rc = parse_facility_ie(ussd_ie+2, iei_length, req); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 128 | break; |
| 129 | case GSM0480_IE_SS_VERSION: |
| 130 | break; |
| 131 | default: |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 132 | fprintf(stderr, "Unhandled GSM 04.08 or 04.80 IEI 0x%02x\n", |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 133 | iei); |
| 134 | rc = 0; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | return rc; |
| 139 | } |
| 140 | |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 141 | static int parse_facility_ie(u_int8_t *facility_ie, u_int8_t length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 142 | struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 143 | { |
| 144 | int rc = 1; |
| 145 | u_int8_t offset = 0; |
| 146 | |
| 147 | do { |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 148 | /* Component Type tag - table 3.7 */ |
| 149 | u_int8_t component_type = facility_ie[offset]; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 150 | u_int8_t component_length = facility_ie[offset+1]; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 151 | |
| 152 | switch (component_type) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 153 | case GSM0480_CTYPE_INVOKE: |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 154 | rc &= parse_ss_invoke(facility_ie+2, |
| 155 | component_length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 156 | req); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 157 | break; |
| 158 | case GSM0480_CTYPE_RETURN_RESULT: |
| 159 | break; |
| 160 | case GSM0480_CTYPE_RETURN_ERROR: |
| 161 | break; |
| 162 | case GSM0480_CTYPE_REJECT: |
| 163 | break; |
| 164 | default: |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 165 | fprintf(stderr, "Unknown GSM 04.80 Facility " |
| 166 | "Component Type 0x%02x\n", component_type); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 167 | rc = 0; |
| 168 | break; |
| 169 | } |
| 170 | offset += (component_length+2); |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 171 | } while (offset < length); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 172 | |
| 173 | return rc; |
| 174 | } |
| 175 | |
| 176 | /* Parse an Invoke component - see table 3.3 */ |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 177 | static int parse_ss_invoke(u_int8_t *invoke_data, u_int8_t length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 178 | struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 179 | { |
| 180 | int rc = 1; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 181 | u_int8_t offset; |
| 182 | |
| 183 | /* mandatory part */ |
| 184 | if (invoke_data[0] != GSM0480_COMPIDTAG_INVOKE_ID) { |
| 185 | fprintf(stderr, "Unexpected GSM 04.80 Component-ID tag " |
| 186 | "0x%02x (expecting Invoke ID tag)\n", invoke_data[0]); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 187 | } |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 188 | |
| 189 | offset = invoke_data[1] + 2; |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 190 | req->invoke_id = invoke_data[2]; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 191 | |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 192 | /* optional part */ |
| 193 | if (invoke_data[offset] == GSM0480_COMPIDTAG_LINKED_ID) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 194 | offset += invoke_data[offset+1] + 2; /* skip over it */ |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 195 | |
| 196 | /* mandatory part */ |
| 197 | if (invoke_data[offset] == GSM0480_OPERATION_CODE) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 198 | u_int8_t operation_code = invoke_data[offset+2]; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 199 | switch (operation_code) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 200 | case GSM0480_OP_CODE_PROCESS_USS_REQ: |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 201 | rc = parse_process_uss_req(invoke_data + offset + 3, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 202 | length - offset - 3, |
| 203 | req); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 204 | break; |
| 205 | default: |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 206 | fprintf(stderr, "GSM 04.80 operation code 0x%02x " |
| 207 | "is not yet handled\n", operation_code); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 208 | rc = 0; |
| 209 | break; |
| 210 | } |
| 211 | } else { |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 212 | fprintf(stderr, "Unexpected GSM 04.80 Component-ID tag 0x%02x " |
| 213 | "(expecting Operation Code tag)\n", |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 214 | invoke_data[0]); |
| 215 | rc = 0; |
| 216 | } |
| 217 | |
| 218 | return rc; |
| 219 | } |
| 220 | |
| 221 | /* Parse the parameters of a Process UnstructuredSS Request */ |
Harald Welte | 17e5f97 | 2009-10-26 20:42:07 +0100 | [diff] [blame] | 222 | static int parse_process_uss_req(u_int8_t *uss_req_data, u_int8_t length, |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 223 | struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 224 | { |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 225 | int rc = 0; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 226 | int num_chars; |
| 227 | u_int8_t dcs; |
| 228 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 229 | if (uss_req_data[0] == GSM_0480_SEQUENCE_TAG) { |
| 230 | if (uss_req_data[2] == ASN1_OCTET_STRING_TAG) { |
| 231 | dcs = uss_req_data[4]; |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 232 | if ((dcs == 0x0F) && |
| 233 | (uss_req_data[5] == ASN1_OCTET_STRING_TAG)) { |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 234 | num_chars = (uss_req_data[6] * 8) / 7; |
Mike Haben | 2449b37 | 2009-10-26 20:36:34 +0100 | [diff] [blame] | 235 | /* Prevent a mobile-originated buffer-overrun! */ |
| 236 | if (num_chars > MAX_LEN_USSD_STRING) |
| 237 | num_chars = MAX_LEN_USSD_STRING; |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 238 | gsm_7bit_decode(req->text, |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 239 | &(uss_req_data[7]), num_chars); |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 240 | /* append null-terminator */ |
Holger Hans Peter Freyther | acf8a0c | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 241 | req->text[num_chars+1] = 0; |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 242 | rc = 1; |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 245 | } |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 246 | return rc; |
| 247 | } |
| 248 | |
Holger Hans Peter Freyther | 680833e | 2010-07-25 18:08:53 +0800 | [diff] [blame] | 249 | struct msgb *gsm0480_create_notifySS(const char *text) |
| 250 | { |
| 251 | struct msgb *msg; |
| 252 | uint8_t *data, *tmp_len; |
| 253 | uint8_t *seq_len_ptr, *cal_len_ptr, *opt_len_ptr, *nam_len_ptr; |
| 254 | int len; |
| 255 | |
| 256 | len = strlen(text); |
| 257 | if (len < 1 || len > 160) |
| 258 | return NULL; |
| 259 | |
| 260 | msg = gsm48_msgb_alloc(); |
| 261 | if (!msg) |
| 262 | return NULL; |
| 263 | |
| 264 | msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG); |
| 265 | seq_len_ptr = msgb_put(msg, 1); |
| 266 | |
Holger Hans Peter Freyther | 44d0f19 | 2010-07-27 04:05:29 +0800 | [diff] [blame] | 267 | /* ss_code for CNAP { */ |
| 268 | msgb_put_u8(msg, 0x81); |
| 269 | msgb_put_u8(msg, 1); |
| 270 | msgb_put_u8(msg, 0x19); |
| 271 | /* } ss_code */ |
| 272 | |
Holger Hans Peter Freyther | 680833e | 2010-07-25 18:08:53 +0800 | [diff] [blame] | 273 | |
| 274 | /* nameIndicator { */ |
| 275 | msgb_put_u8(msg, 0xB4); |
| 276 | nam_len_ptr = msgb_put(msg, 1); |
| 277 | |
| 278 | /* callingName { */ |
| 279 | msgb_put_u8(msg, 0xA0); |
| 280 | opt_len_ptr = msgb_put(msg, 1); |
| 281 | msgb_put_u8(msg, 0xA0); |
| 282 | cal_len_ptr = msgb_put(msg, 1); |
| 283 | |
| 284 | /* namePresentationAllowed { */ |
| 285 | /* add the DCS value */ |
| 286 | msgb_put_u8(msg, 0x80); |
| 287 | msgb_put_u8(msg, 1); |
| 288 | msgb_put_u8(msg, 0x0F); |
| 289 | |
| 290 | /* add the lengthInCharacters */ |
| 291 | msgb_put_u8(msg, 0x81); |
| 292 | msgb_put_u8(msg, 1); |
| 293 | msgb_put_u8(msg, strlen(text)); |
| 294 | |
| 295 | /* add the actual string */ |
| 296 | msgb_put_u8(msg, 0x82); |
| 297 | tmp_len = msgb_put(msg, 1); |
| 298 | data = msgb_put(msg, 0); |
| 299 | len = gsm_7bit_encode(data, text); |
| 300 | tmp_len[0] = len; |
| 301 | msgb_put(msg, len); |
| 302 | |
| 303 | /* }; namePresentationAllowed */ |
| 304 | |
| 305 | cal_len_ptr[0] = 3 + 3 + 2 + len; |
| 306 | opt_len_ptr[0] = cal_len_ptr[0] + 2; |
| 307 | /* }; callingName */ |
| 308 | |
| 309 | nam_len_ptr[0] = opt_len_ptr[0] + 2; |
| 310 | /* ); nameIndicator */ |
| 311 | |
| 312 | /* write the lengths... */ |
Holger Hans Peter Freyther | 44d0f19 | 2010-07-27 04:05:29 +0800 | [diff] [blame] | 313 | seq_len_ptr[0] = 3 + nam_len_ptr[0] + 2; |
Holger Hans Peter Freyther | 680833e | 2010-07-25 18:08:53 +0800 | [diff] [blame] | 314 | |
| 315 | return msg; |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | e731e1d | 2010-07-27 18:27:46 +0800 | [diff] [blame] | 318 | struct msgb *gsm0480_create_unstructuredSS_Notify(int alertPattern, const char *text) |
Holger Hans Peter Freyther | 15ef17e | 2010-07-26 18:34:27 +0800 | [diff] [blame] | 319 | { |
| 320 | struct msgb *msg; |
| 321 | uint8_t *seq_len_ptr, *ussd_len_ptr, *data; |
| 322 | int len; |
| 323 | |
| 324 | msg = gsm48_msgb_alloc(); |
| 325 | if (!msg) |
| 326 | return NULL; |
| 327 | |
| 328 | /* SEQUENCE { */ |
| 329 | msgb_put_u8(msg, GSM_0480_SEQUENCE_TAG); |
| 330 | seq_len_ptr = msgb_put(msg, 1); |
| 331 | |
| 332 | /* DCS { */ |
| 333 | msgb_put_u8(msg, ASN1_OCTET_STRING_TAG); |
| 334 | msgb_put_u8(msg, 1); |
| 335 | msgb_put_u8(msg, 0x0F); |
| 336 | /* } DCS */ |
| 337 | |
| 338 | /* USSD-String { */ |
| 339 | msgb_put_u8(msg, ASN1_OCTET_STRING_TAG); |
| 340 | ussd_len_ptr = msgb_put(msg, 1); |
| 341 | data = msgb_put(msg, 0); |
| 342 | len = gsm_7bit_encode(data, text); |
| 343 | msgb_put(msg, len); |
| 344 | ussd_len_ptr[0] = len; |
| 345 | /* USSD-String } */ |
| 346 | |
Holger Hans Peter Freyther | e731e1d | 2010-07-27 18:27:46 +0800 | [diff] [blame] | 347 | /* alertingPattern { */ |
| 348 | msgb_put_u8(msg, ASN1_OCTET_STRING_TAG); |
| 349 | msgb_put_u8(msg, 1); |
| 350 | msgb_put_u8(msg, alertPattern); |
| 351 | /* } alertingPattern */ |
| 352 | |
| 353 | seq_len_ptr[0] = 3 + 2 + ussd_len_ptr[0] + 3; |
Holger Hans Peter Freyther | 15ef17e | 2010-07-26 18:34:27 +0800 | [diff] [blame] | 354 | /* } SEQUENCE */ |
| 355 | |
| 356 | return msg; |
| 357 | } |
| 358 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 359 | /* Send response to a mobile-originated ProcessUnstructuredSS-Request */ |
Holger Hans Peter Freyther | d42c3f2 | 2010-06-17 17:35:57 +0800 | [diff] [blame] | 360 | int gsm0480_send_ussd_response(struct gsm_subscriber_connection *conn, |
| 361 | const struct msgb *in_msg, const char *response_text, |
| 362 | const struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 363 | { |
| 364 | struct msgb *msg = gsm48_msgb_alloc(); |
| 365 | struct gsm48_hdr *gh; |
| 366 | u_int8_t *ptr8; |
| 367 | int response_len; |
| 368 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 369 | /* First put the payload text into the message */ |
Holger Hans Peter Freyther | ba81ab3 | 2010-07-26 17:56:55 +0800 | [diff] [blame] | 370 | ptr8 = msgb_put(msg, 0); |
| 371 | response_len = gsm_7bit_encode(ptr8, response_text); |
| 372 | msgb_put(msg, response_len); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 373 | |
| 374 | /* Then wrap it as an Octet String */ |
| 375 | msgb_wrap_with_TL(msg, ASN1_OCTET_STRING_TAG); |
| 376 | |
| 377 | /* Pre-pend the DCS octet string */ |
| 378 | msgb_push_TLV1(msg, ASN1_OCTET_STRING_TAG, 0x0F); |
| 379 | |
| 380 | /* Then wrap these as a Sequence */ |
| 381 | msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG); |
| 382 | |
| 383 | /* Pre-pend the operation code */ |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 384 | msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, |
| 385 | GSM0480_OP_CODE_PROCESS_USS_REQ); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 386 | |
| 387 | /* Wrap the operation code and IA5 string as a sequence */ |
| 388 | msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG); |
| 389 | |
| 390 | /* Pre-pend the invoke ID */ |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 391 | msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 392 | |
| 393 | /* Wrap this up as a Return Result component */ |
| 394 | msgb_wrap_with_TL(msg, GSM0480_CTYPE_RETURN_RESULT); |
| 395 | |
| 396 | /* Wrap the component in a Facility message */ |
| 397 | msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY); |
| 398 | |
| 399 | /* And finally pre-pend the L3 header */ |
| 400 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
Harald Welte | 8c8f791 | 2009-10-26 20:42:55 +0100 | [diff] [blame] | 401 | gh->proto_discr = GSM48_PDISC_NC_SS | req->transaction_id |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 402 | | (1<<7); /* TI direction = 1 */ |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 403 | gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE; |
| 404 | |
Holger Hans Peter Freyther | d42c3f2 | 2010-06-17 17:35:57 +0800 | [diff] [blame] | 405 | return gsm0808_submit_dtap(conn, msg, 0); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Holger Hans Peter Freyther | 6a4b362 | 2010-07-26 03:41:11 +0800 | [diff] [blame] | 408 | /* wrap an invoke around it... the other way around |
| 409 | * |
| 410 | * 1.) Invoke Component tag |
| 411 | * 2.) Invoke ID Tag |
| 412 | * 3.) Operation |
| 413 | * 4.) Data |
| 414 | */ |
| 415 | int gsm0480_wrap_invoke(struct msgb *msg, int op, int link_id) |
| 416 | { |
| 417 | /* 3. operation */ |
| 418 | msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, op); |
| 419 | |
| 420 | /* 2. invoke id tag */ |
| 421 | msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, link_id); |
| 422 | |
| 423 | /* 1. component tag */ |
| 424 | msgb_wrap_with_TL(msg, GSM0480_CTYPE_INVOKE); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Holger Hans Peter Freyther | b02c89e | 2010-07-26 19:05:56 +0800 | [diff] [blame] | 429 | /* wrap the GSM 04.08 Facility IE around it */ |
| 430 | int gsm0480_wrap_facility(struct msgb *msg) |
| 431 | { |
| 432 | msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Holger Hans Peter Freyther | d42c3f2 | 2010-06-17 17:35:57 +0800 | [diff] [blame] | 437 | int gsm0480_send_ussd_reject(struct gsm_subscriber_connection *conn, |
| 438 | const struct msgb *in_msg, |
| 439 | const struct ussd_request *req) |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 440 | { |
| 441 | struct msgb *msg = gsm48_msgb_alloc(); |
| 442 | struct gsm48_hdr *gh; |
| 443 | |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 444 | /* First insert the problem code */ |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 445 | msgb_push_TLV1(msg, GSM_0480_PROBLEM_CODE_TAG_GENERAL, |
| 446 | GSM_0480_GEN_PROB_CODE_UNRECOGNISED); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 447 | |
| 448 | /* Before it insert the invoke ID */ |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 449 | msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 450 | |
| 451 | /* Wrap this up as a Reject component */ |
| 452 | msgb_wrap_with_TL(msg, GSM0480_CTYPE_REJECT); |
| 453 | |
| 454 | /* Wrap the component in a Facility message */ |
| 455 | msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY); |
| 456 | |
| 457 | /* And finally pre-pend the L3 header */ |
| 458 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
Harald Welte | 6307b85 | 2009-10-16 08:41:51 +0200 | [diff] [blame] | 459 | gh->proto_discr = GSM48_PDISC_NC_SS; |
Mike Haben | dc329a6 | 2009-10-22 09:56:44 +0200 | [diff] [blame] | 460 | gh->proto_discr |= req->transaction_id | (1<<7); /* TI direction = 1 */ |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 461 | gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE; |
| 462 | |
Holger Hans Peter Freyther | d42c3f2 | 2010-06-17 17:35:57 +0800 | [diff] [blame] | 463 | return gsm0808_submit_dtap(conn, msg, 0); |
Harald Welte | 6eafe91 | 2009-10-16 08:32:58 +0200 | [diff] [blame] | 464 | } |
Holger Hans Peter Freyther | daf7534 | 2010-07-26 20:01:07 +0800 | [diff] [blame] | 465 | |
Holger Hans Peter Freyther | e731e1d | 2010-07-27 18:27:46 +0800 | [diff] [blame] | 466 | int gsm0480_send_ussdNotify(struct gsm_subscriber_connection *conn, int level, const char *text) |
Holger Hans Peter Freyther | daf7534 | 2010-07-26 20:01:07 +0800 | [diff] [blame] | 467 | { |
| 468 | struct gsm48_hdr *gh; |
| 469 | struct msgb *msg; |
| 470 | |
Holger Hans Peter Freyther | e731e1d | 2010-07-27 18:27:46 +0800 | [diff] [blame] | 471 | msg = gsm0480_create_unstructuredSS_Notify(level, text); |
Holger Hans Peter Freyther | daf7534 | 2010-07-26 20:01:07 +0800 | [diff] [blame] | 472 | if (!msg) |
| 473 | return -1; |
| 474 | |
| 475 | gsm0480_wrap_invoke(msg, GSM0480_OP_CODE_USS_NOTIFY, 0); |
| 476 | gsm0480_wrap_facility(msg); |
| 477 | |
| 478 | /* And finally pre-pend the L3 header */ |
| 479 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 480 | gh->proto_discr = GSM48_PDISC_NC_SS; |
| 481 | gh->msg_type = GSM0480_MTYPE_REGISTER; |
| 482 | |
| 483 | return gsm0808_submit_dtap(conn, msg, 0); |
| 484 | } |
Holger Hans Peter Freyther | 68d2679 | 2010-07-27 03:31:50 +0800 | [diff] [blame] | 485 | |
| 486 | int gsm0480_send_releaseComplete(struct gsm_subscriber_connection *conn) |
| 487 | { |
| 488 | struct gsm48_hdr *gh; |
| 489 | struct msgb *msg; |
| 490 | |
| 491 | msg = gsm48_msgb_alloc(); |
| 492 | if (!msg) |
| 493 | return -1; |
| 494 | |
| 495 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 496 | gh->proto_discr = GSM48_PDISC_NC_SS; |
| 497 | gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE; |
| 498 | |
| 499 | return gsm0808_submit_dtap(conn, msg, 0); |
| 500 | } |