aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/ussd.c
blob: 97370c1b0de6f876a2e23699f9de66dfd84a10b1 (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
/* Network-specific handling of mobile-originated USSDs. */

/* (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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/* This module defines the network-specific handling of mobile-originated
   USSD messages. */

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

#include <openbsc/gsm_04_80.h>
#include <openbsc/gsm_subscriber.h>
#include <openbsc/debug.h>
#include <openbsc/osmo_msc.h>


#include <osmocore/talloc.h>

#include <osmocom/vty/vty.h>

struct ussd_mapping {
	struct llist_head entry;
	const char *ussd_number;

	uint8_t **l3_frames;
	uint8_t *l3_sizes;
	int frame_sizes;
};

LLIST_HEAD(mapping_list);

/* Declarations of USSD strings to be recognised */
const char USSD_TEXT_OWN_NUMBER[] = "*#100#";

/* Forward declarations of network-specific handler functions */
static int send_own_number(struct gsm_subscriber_connection *conn, const struct msgb *msg, const struct ussd_request *req);
static int ussd_send_custom(struct gsm_subscriber_connection *, struct msgb *, struct ussd_request *);


/* Entrypoint - handler function common to all mobile-originated USSDs */
int handle_rcv_ussd(struct gsm_subscriber_connection *conn, struct msgb *msg)
{
	int rc;
	struct ussd_request req;
	struct gsm48_hdr *gh;

	memset(&req, 0, sizeof(req));
	gh = msgb_l3(msg);
	rc = gsm0480_decode_ussd_request(gh, msgb_l3len(msg), &req);
	if (req.text[0] == 0xFF)  /* Release-Complete */
		return 0;

	if (strstr(USSD_TEXT_OWN_NUMBER, req.text) != NULL) {
		DEBUGP(DMM, "USSD: Own number requested\n");
		rc = send_own_number(conn, msg, &req);
	} else if (ussd_send_custom(conn, msg, &req)) {
		LOGP(DMM, LOGL_NOTICE, "Sending custom L3 message for USSD.\n");
		rc = 1;
	} else {
		DEBUGP(DMM, "Unhandled USSD %s\n", req.text);
		rc = gsm0480_send_ussd_reject(conn, msg, &req);
	}

	/* check if we can release it */
	msc_release_connection(conn);
	return rc;
}

/* A network-specific handler function */
static int send_own_number(struct gsm_subscriber_connection *conn, const struct msgb *msg, const struct ussd_request *req)
{
	char *own_number = conn->subscr->extension;
	char response_string[GSM_EXTENSION_LENGTH + 20];

	/* Need trailing CR as EOT character */
	snprintf(response_string, sizeof(response_string), "Your extension is %s\r", own_number);
	return gsm0480_send_ussd_response(conn, msg, response_string, req);
}


static int ussd_send_custom(struct gsm_subscriber_connection *conn,
			    struct msgb *msg, struct ussd_request *req)
{
	struct msgb *out;
	uint8_t* data;
	struct ussd_mapping *map;

	llist_for_each_entry(map, &mapping_list, entry) {
		if (strstr(map->ussd_number, req->text) == NULL)
			continue;

		/* if we have more than one msg */
		if (map->frame_sizes > 1) {
			conn->ussd_sequence = 1;
			conn->ussd_number = map->ussd_number;
		}

		out = gsm48_msgb_alloc();
		if (!out)
			return -1;


		out->lchan = msg->lchan;
		data = msgb_put(out, map->l3_sizes[0]);
		memcpy(data, map->l3_frames[0], map->l3_sizes[0]);
		gsm0808_submit_dtap(conn, out, 0, 0);
		return 1;
	}

	return 0;
}

extern struct gsm_network *bsc_gsmnet;
int ussd_parse_mapping(const char *number, const uint8_t *msg, int len)
{
	struct ussd_mapping *map;

	llist_for_each_entry(map, &mapping_list, entry) {
		if (strcmp(map->ussd_number, number) != 0)
			continue;

		LOGP(DINP, LOGL_ERROR, "NOT IMPLTELEMEND\n");	
#if 0
		map->l3_frames = talloc_realloc(map, map->l3_sizes, uint8_t, 
		map->l3_sizes = talloc_realloc;	

		map->l3_frames[map->frame_sizes] = talloc_array(map, uint8_t, len);
		memcpy(map->l3_frames[map->frame_sizes], msg, len);

		map->l3_sizes[map->frame_sizes] = len;
		map->frame_sizes += 1;
#endif
		return 0;
	}


	map = talloc_zero(bsc_gsmnet, struct ussd_mapping);
	if (!map)
		return -1;

	llist_add(&map->entry, &mapping_list);
	map->ussd_number = talloc_strdup(map, number);
	map->l3_frames = talloc_array(map, uint8_t*, 1);
	map->l3_sizes = talloc_array(map, uint8_t, 1);

	map->l3_frames[0] = talloc_array(map, uint8_t, len);
	memcpy(map->l3_frames[0], msg, len);
	map->l3_sizes[0] = len;
	map->frame_sizes += 1;

	return 0;
}

int ussd_clear_mapping(const char *number)
{
	struct ussd_mapping *map;

	llist_for_each_entry(map, &mapping_list, entry) {
		if (strcmp(map->ussd_number, number) == 0) {
			llist_del(&map->entry);
			talloc_free(map);
			return 0;
		}
	}

	return 0;
}


int ussd_call_reroute(struct gsm_subscriber_connection *conn, struct msgb *msg)
{
	struct ussd_mapping *map;

	if (!conn->ussd_sequence)
		return 0;

	/* check if the rule is still there, compare number */
	llist_for_each_entry(map, &mapping_list, entry) {
		if (conn->ussd_number == map->ussd_number) {
			return 1;
		}
	}

	return -0;
}

int ussd_dump_mapping(struct vty *vty)
{
	int i = 0;
	struct ussd_mapping *map;

	llist_for_each_entry(map, &mapping_list, entry) {
		vty_out(vty, "USSD Nr: %s nr_frames: %d%s",
			map->ussd_number, map->frame_sizes, VTY_NEWLINE);
		for (i = 0; i < map->frame_sizes; ++i)
			vty_out(vty, " %d: '%s'%s",
				i, hexdump(map->l3_frames[i], map->l3_sizes[i]), VTY_NEWLINE);
	}

	return 0;
}

int ussd_call_rx(struct gsm_subscriber_connection *conn, struct msgb *msg)
{
	LOGP(DINP, LOGL_ERROR, "NOT IMPLTELEMEND\n");	
	return -1;
}