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