blob: 8414dc9fedc1ae542789a3d2f760c16fd7ac1983 [file] [log] [blame]
Harald Welte8c8f7912009-10-26 20:42:55 +01001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte6eafe912009-10-16 08:32:58 +02002 * 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 Freyther680833e2010-07-25 18:08:53 +08005 * (C) 2008, 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte6eafe912009-10-16 08:32:58 +02006 * (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 Welte6eafe912009-10-16 08:32:58 +020032#include <openbsc/debug.h>
33#include <openbsc/gsm_data.h>
Harald Welte6eafe912009-10-16 08:32:58 +020034#include <openbsc/gsm_04_08.h>
35#include <openbsc/gsm_04_80.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080036#include <openbsc/bsc_api.h>
Harald Welte6eafe912009-10-16 08:32:58 +020037
Holger Hans Peter Freyther12eb9b72010-09-30 18:53:47 +080038#include <osmocore/gsm0480.h>
Holger Hans Peter Freytherc0714b82010-09-30 18:48:28 +080039#include <osmocore/gsm_utils.h>
40#include <osmocore/msgb.h>
41#include <osmocore/tlv.h>
42
Harald Welte6eafe912009-10-16 08:32:58 +020043static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, u_int8_t tag)
44{
Holger Hans Peter Freytherac30cc82010-07-26 19:08:59 +080045 uint8_t *data = msgb_push(msgb, 2);
46
47 data[0] = tag;
Holger Hans Peter Freythere6373b72010-07-27 01:25:59 +080048 data[1] = msgb->len - 2;
Holger Hans Peter Freytherac30cc82010-07-26 19:08:59 +080049 return data;
Harald Welte6eafe912009-10-16 08:32:58 +020050}
51
Harald Welte6307b852009-10-16 08:41:51 +020052static inline unsigned char *msgb_push_TLV1(struct msgb *msgb, u_int8_t tag,
53 u_int8_t value)
Harald Welte6eafe912009-10-16 08:32:58 +020054{
Holger Hans Peter Freytherac30cc82010-07-26 19:08:59 +080055 uint8_t *data = msgb_push(msgb, 3);
56
57 data[0] = tag;
58 data[1] = 1;
59 data[2] = value;
60 return data;
Harald Welte6eafe912009-10-16 08:32:58 +020061}
62
63
Harald Welte6eafe912009-10-16 08:32:58 +020064/* Send response to a mobile-originated ProcessUnstructuredSS-Request */
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +080065int gsm0480_send_ussd_response(struct gsm_subscriber_connection *conn,
66 const struct msgb *in_msg, const char *response_text,
67 const struct ussd_request *req)
Harald Welte6eafe912009-10-16 08:32:58 +020068{
69 struct msgb *msg = gsm48_msgb_alloc();
70 struct gsm48_hdr *gh;
71 u_int8_t *ptr8;
72 int response_len;
73
Harald Welte6eafe912009-10-16 08:32:58 +020074 /* First put the payload text into the message */
Holger Hans Peter Freytherba81ab32010-07-26 17:56:55 +080075 ptr8 = msgb_put(msg, 0);
76 response_len = gsm_7bit_encode(ptr8, response_text);
77 msgb_put(msg, response_len);
Harald Welte6eafe912009-10-16 08:32:58 +020078
79 /* Then wrap it as an Octet String */
80 msgb_wrap_with_TL(msg, ASN1_OCTET_STRING_TAG);
81
82 /* Pre-pend the DCS octet string */
83 msgb_push_TLV1(msg, ASN1_OCTET_STRING_TAG, 0x0F);
84
85 /* Then wrap these as a Sequence */
86 msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG);
87
88 /* Pre-pend the operation code */
Harald Welte6307b852009-10-16 08:41:51 +020089 msgb_push_TLV1(msg, GSM0480_OPERATION_CODE,
90 GSM0480_OP_CODE_PROCESS_USS_REQ);
Harald Welte6eafe912009-10-16 08:32:58 +020091
92 /* Wrap the operation code and IA5 string as a sequence */
93 msgb_wrap_with_TL(msg, GSM_0480_SEQUENCE_TAG);
94
95 /* Pre-pend the invoke ID */
Mike Habendc329a62009-10-22 09:56:44 +020096 msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id);
Harald Welte6eafe912009-10-16 08:32:58 +020097
98 /* Wrap this up as a Return Result component */
99 msgb_wrap_with_TL(msg, GSM0480_CTYPE_RETURN_RESULT);
100
101 /* Wrap the component in a Facility message */
102 msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
103
104 /* And finally pre-pend the L3 header */
105 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
Harald Welte8c8f7912009-10-26 20:42:55 +0100106 gh->proto_discr = GSM48_PDISC_NC_SS | req->transaction_id
Mike Habendc329a62009-10-22 09:56:44 +0200107 | (1<<7); /* TI direction = 1 */
Harald Welte6eafe912009-10-16 08:32:58 +0200108 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
109
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100110 return gsm0808_submit_dtap(conn, msg, 0, 0);
Harald Welte6eafe912009-10-16 08:32:58 +0200111}
112
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +0800113int gsm0480_send_ussd_reject(struct gsm_subscriber_connection *conn,
114 const struct msgb *in_msg,
115 const struct ussd_request *req)
Harald Welte6eafe912009-10-16 08:32:58 +0200116{
117 struct msgb *msg = gsm48_msgb_alloc();
118 struct gsm48_hdr *gh;
119
Harald Welte6eafe912009-10-16 08:32:58 +0200120 /* First insert the problem code */
Harald Welte6307b852009-10-16 08:41:51 +0200121 msgb_push_TLV1(msg, GSM_0480_PROBLEM_CODE_TAG_GENERAL,
122 GSM_0480_GEN_PROB_CODE_UNRECOGNISED);
Harald Welte6eafe912009-10-16 08:32:58 +0200123
124 /* Before it insert the invoke ID */
Mike Habendc329a62009-10-22 09:56:44 +0200125 msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id);
Harald Welte6eafe912009-10-16 08:32:58 +0200126
127 /* Wrap this up as a Reject component */
128 msgb_wrap_with_TL(msg, GSM0480_CTYPE_REJECT);
129
130 /* Wrap the component in a Facility message */
131 msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
132
133 /* And finally pre-pend the L3 header */
134 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
Harald Welte6307b852009-10-16 08:41:51 +0200135 gh->proto_discr = GSM48_PDISC_NC_SS;
Mike Habendc329a62009-10-22 09:56:44 +0200136 gh->proto_discr |= req->transaction_id | (1<<7); /* TI direction = 1 */
Harald Welte6eafe912009-10-16 08:32:58 +0200137 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
138
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100139 return gsm0808_submit_dtap(conn, msg, 0, 0);
Harald Welte6eafe912009-10-16 08:32:58 +0200140}
Holger Hans Peter Freytherdaf75342010-07-26 20:01:07 +0800141
Holger Hans Peter Freythere731e1d2010-07-27 18:27:46 +0800142int gsm0480_send_ussdNotify(struct gsm_subscriber_connection *conn, int level, const char *text)
Holger Hans Peter Freytherdaf75342010-07-26 20:01:07 +0800143{
144 struct gsm48_hdr *gh;
145 struct msgb *msg;
146
Holger Hans Peter Freythere731e1d2010-07-27 18:27:46 +0800147 msg = gsm0480_create_unstructuredSS_Notify(level, text);
Holger Hans Peter Freytherdaf75342010-07-26 20:01:07 +0800148 if (!msg)
149 return -1;
150
151 gsm0480_wrap_invoke(msg, GSM0480_OP_CODE_USS_NOTIFY, 0);
152 gsm0480_wrap_facility(msg);
153
154 /* And finally pre-pend the L3 header */
155 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
156 gh->proto_discr = GSM48_PDISC_NC_SS;
157 gh->msg_type = GSM0480_MTYPE_REGISTER;
158
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100159 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freytherdaf75342010-07-26 20:01:07 +0800160}
Holger Hans Peter Freyther68d26792010-07-27 03:31:50 +0800161
162int gsm0480_send_releaseComplete(struct gsm_subscriber_connection *conn)
163{
164 struct gsm48_hdr *gh;
165 struct msgb *msg;
166
167 msg = gsm48_msgb_alloc();
168 if (!msg)
169 return -1;
170
171 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
172 gh->proto_discr = GSM48_PDISC_NC_SS;
173 gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;
174
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100175 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther68d26792010-07-27 03:31:50 +0800176}