aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/libmsc/gsm_04_80.c
blob: 716fe751fe66a0ca3ef1be807bb2e6083d154611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */

/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
 * (C) 2008, 2009, 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2009 by Mike Haben <michael.haben@btinternet.com>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <openbsc/debug.h>
#include <openbsc/gsm_data.h>
#include <openbsc/gsm_04_08.h>
#include <openbsc/gsm_04_80.h>
#include <openbsc/bsc_api.h>

#include <osmocom/gsm/gsm0480.h>
#include <osmocom/gsm/gsm_utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/gsm/tlv.h>

/* This function can handle ASN1 length up to 255 which is enough for USSD */
static inline unsigned char *msgb_wrap_with_ASN1_TL(struct msgb *msgb, uint8_t tag)
{
	uint16_t origlen = msgb->len;
	uint8_t *data = msgb_push(msgb, (origlen > 0x7f) ? 3 : 2);
	data[0] = tag;
	if (origlen > 0x7f) {
		data[1] = 0x81;
		data[2] = origlen;
	} else {
		data[1] = origlen;
	}
	return data;
}


static inline unsigned char *msgb_wrap_with_TL(struct msgb *msgb, uint8_t tag)
{
	uint8_t *data = msgb_push(msgb, 2);

	data[0] = tag;
	data[1] = msgb->len - 2;
	return data;
}

static inline unsigned char *msgb_push_TLV1(struct msgb *msgb, uint8_t tag,
					    uint8_t value)
{
	uint8_t *data = msgb_push(msgb, 3);

	data[0] = tag;
	data[1] = 1;
	data[2] = value;
	return data;
}

static inline unsigned char *msgb_wrap_with_L(struct msgb *msgb)
{
	uint8_t *data = msgb_push(msgb, 1);

	data[0] = msgb->len - 1;
	return data;
}

/* Compose universial USSD packet invoke/return_result payload */
struct msgb *gsm0480_compose_ussd_component(struct ss_request* req)
{
	struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USSD RSP");
	uint8_t *ptr8;

	/* First put the payload text into the message */
	ptr8 = msgb_put(msg, 0);

	memcpy(ptr8, req->ussd_text, req->ussd_text_len);
	msgb_put(msg, req->ussd_text_len);

	/* Then wrap it as an Octet String */
	msgb_wrap_with_ASN1_TL(msg, ASN1_OCTET_STRING_TAG);

	/* Pre-pend the DCS octet string */
	msgb_push_TLV1(msg, ASN1_OCTET_STRING_TAG, req->ussd_text_language);

	/* Then wrap these as a Sequence */
	msgb_wrap_with_ASN1_TL(msg, GSM_0480_SEQUENCE_TAG);

	if (req->component_type == GSM0480_CTYPE_RETURN_RESULT) {
		/* Pre-pend the operation code */
		msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, req->opcode);

		/* Wrap the operation code and IA5 string as a sequence */
		msgb_wrap_with_ASN1_TL(msg, GSM_0480_SEQUENCE_TAG);

		/* Pre-pend the invoke ID */
		msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id);
	} else if (req->component_type == GSM0480_CTYPE_INVOKE) {
		/* Pre-pend the operation code */
		msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, req->opcode);

		/* Pre-pend the invoke ID */
		msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id);
	} else {
		abort();
	}

	/* Wrap this up as an Invoke or a Return Result component */
	msgb_wrap_with_ASN1_TL(msg, req->component_type);
	return msg;
}

#ifndef NO_GSM0480_SEND_FUNC

int gsm0480_send_component(struct gsm_subscriber_connection *conn,
			   struct msgb *msg,
			   struct ss_header* reqhdr)
{
#if 0
	struct msgb *msg = gsm48_msgb_alloc();
	struct gsm48_hdr *gh;
	uint8_t *ptr8;

	ptr8 = msgb_put(msg, 0);

	memcpy(ptr8, component, reqhdr->component_length);
	msgb_put(msg, reqhdr->component_length);
#endif
	struct gsm48_hdr *gh;

	if (reqhdr->message_type == GSM0480_MTYPE_REGISTER ||
		reqhdr->message_type == GSM0480_MTYPE_RELEASE_COMPLETE) {
		/* Wrap the component in a Facility message, it's not ASN1 */
		msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
	} else if (reqhdr->message_type == GSM0480_MTYPE_FACILITY) {
		/* For GSM0480_MTYPE_FACILITY it's LV not TLV */
		msgb_wrap_with_L(msg);
	} else {
		abort();
	}

	/* And finally pre-pend the L3 header */
	gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
	gh->proto_discr = GSM48_PDISC_NC_SS | reqhdr->transaction_id
					| (1<<7);  /* TI direction = 1 */
	gh->msg_type = reqhdr->message_type;

	DEBUGP(DSS, "Sending SS to mobile: %s\n", msgb_hexdump(msg));

	return gsm0808_submit_dtap(conn, msg, 0, 0);
}

#if 0
/* Compose universial SS packet except Reject opcodes */
int gsm0480_send_ussd(struct gsm_subscriber_connection *conn,
		      struct ss_request* req)
{
	struct msgb *msg = gsm48_msgb_alloc();
	struct gsm48_hdr *gh;
	uint8_t *ptr8;

	/* First put the payload text into the message */
	ptr8 = msgb_put(msg, 0);

	memcpy(ptr8, req->ussd_text, req->ussd_text_len);
	msgb_put(msg, req->ussd_text_len);

	/* Then wrap it as an Octet String */
	msgb_wrap_with_ASN1_TL(msg, ASN1_OCTET_STRING_TAG);

	/* Pre-pend the DCS octet string */
	msgb_push_TLV1(msg, ASN1_OCTET_STRING_TAG, req->ussd_text_language);

	/* Then wrap these as a Sequence */
	msgb_wrap_with_ASN1_TL(msg, GSM_0480_SEQUENCE_TAG);

	if (req->component_type == GSM0480_CTYPE_RETURN_RESULT) {
		/* Pre-pend the operation code */
		msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, req->opcode);

		/* Wrap the operation code and IA5 string as a sequence */
		msgb_wrap_with_ASN1_TL(msg, GSM_0480_SEQUENCE_TAG);

		/* Pre-pend the invoke ID */
		msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id);
	} else if (req->component_type == GSM0480_CTYPE_INVOKE) {
		/* Pre-pend the operation code */
		msgb_push_TLV1(msg, GSM0480_OPERATION_CODE, req->opcode);

		/* Pre-pend the invoke ID */
		msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, req->invoke_id);
	} else {
		abort();
	}

	/* Wrap this up as an Invoke or a Return Result component */
	msgb_wrap_with_ASN1_TL(msg, req->component_type);

	if (req->message_type == GSM0480_MTYPE_REGISTER ||
		req->message_type == GSM0480_MTYPE_RELEASE_COMPLETE) {
		/* Wrap the component in a Facility message, it's not ASN1 */
		msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);
	} else if (req->message_type == GSM0480_MTYPE_FACILITY) {
		/* For GSM0480_MTYPE_FACILITY it's LV not TLV */
		msgb_wrap_with_L(msg);
	} else {
		abort();
	}

	/* And finally pre-pend the L3 header */
	gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
	gh->proto_discr = GSM48_PDISC_NC_SS | req->transaction_id
					| (1<<7);  /* TI direction = 1 */
	gh->msg_type = req->message_type;

	DEBUGP(DSS, "Sending USSD to mobile: %s\n", msgb_hexdump(msg));

	return gsm0808_submit_dtap(conn, msg, 0, 0);
}
#endif

int gsm0480_send_ussd_reject(struct gsm_subscriber_connection *conn,
			     uint8_t invoke_id,
			     uint8_t transaction_id)
{
	struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USSD REJ");
	struct ss_header ssh;

	/* First insert the problem code */
	msgb_push_TLV1(msg, GSM_0480_PROBLEM_CODE_TAG_GENERAL,
			GSM_0480_GEN_PROB_CODE_UNRECOGNISED);

	/* Before it insert the invoke ID */
	msgb_push_TLV1(msg, GSM0480_COMPIDTAG_INVOKE_ID, invoke_id);

	/* Wrap this up as a Reject component */
	msgb_wrap_with_ASN1_TL(msg, GSM0480_CTYPE_REJECT);

	/* Prepare data for L3 header */
	ssh.transaction_id = transaction_id;
	ssh.message_type = GSM0480_MTYPE_RELEASE_COMPLETE;
	return gsm0480_send_component(conn, msg, &ssh);
#if 0
	/* Wrap the component in a Facility message */
	msgb_wrap_with_TL(msg, GSM0480_IE_FACILITY);

	/* And finally pre-pend the L3 header */
	gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
	gh->proto_discr = GSM48_PDISC_NC_SS;
	gh->proto_discr |= transaction_id | (1<<7);  /* TI direction = 1 */
	gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE;

	return gsm0808_submit_dtap(conn, msg, 0, 0);
#endif
}

int msc_send_ussd_notify(struct gsm_subscriber_connection *conn, int level, const char *text)
{
	struct msgb *msg = gsm0480_create_ussd_notify(level, text);
	if (!msg)
		return -1;
	return gsm0808_submit_dtap(conn, msg, 0, 0);
}

int msc_send_ussd_release_complete(struct gsm_subscriber_connection *conn)
{
	struct msgb *msg = gsm0480_create_ussd_release_complete();
	if (!msg)
		return -1;
	return gsm0808_submit_dtap(conn, msg, 0, 0);
}

#endif